当前位置: 代码迷 >> 综合 >> ubuntu 16.04/18.04 python2/python3 编译安装boost, GPU版dlib (cuda9)
  详细解决方案

ubuntu 16.04/18.04 python2/python3 编译安装boost, GPU版dlib (cuda9)

热度:92   发布时间:2023-12-15 16:18:43.0
# 安装boost
# 下载boost: https://dl.bintray.com/boostorg/release/1.66.0/source/boost_1_66_0.tar.gz
# 解压
$ bunzip2 boost_1_66_0.tar.bz2
$ sudo tar -xvf boost_1_66_0.tar
$ cd boost_1_66_0/
# 我使用的是默认路径安装,安装到:/usr/local。其他安装选项见:https://www.boost.org/doc/libs/1_66_0/more/getting_started/unix-variants.html#get-boost
$ sudo ./bootstrap.sh
$ sudo ./b2 install以上在python2环境下安装成功。如果中间安装失败,请使用 $ sudo ./b2 clean之后重新安装。
(python3的conda环境可能会报找不到pyconfig.h的错误。)如果是python3也可以先这么安装。
临时解决:将/usr/bin/python链接到python2,在python2的环境下安装boost# 查看boost版本
$ dpkg -S /usr/include/boost/version.hpp
查看版本有问题,直接使用:$ ls /usr/local/lib | grep boost 看后缀即为版本号
参考:https://blog.csdn.net/guotianqing/article/details/104455929# python3安装boost:https://askubuntu.com/questions/944035/installing-libboost-python-dev-for-python3-without-installing-python2-7# 安装依赖
$ sudo apt-get -y install libopenblas-dev# gcc g++降级到5.4
$ gcc --version
$ which gcc
$ sudo rm /usr/bin/g++
$ sudo rm /usr/bin/gcc
# 此时源的情况:/etc/apt/sources.listdeb http://archive.ubuntu.com/ubuntu trusty main restricteddeb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial main
# deb-src http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu xenial main
deb http://archive.ubuntu.com/ubuntu bionic universe$ sudo apt install gcc-5 g++-5
$ sudo ln -s /usr/bin/gcc-5 /usr/bin/gcc
$ sudo ln -s /usr/bin/g++-5 /usr/bin/g++
# 安装dlib cuda版
$ pip uninstall dlib
$ wget https://github.com/davisking/dlib/archive/master.zip
$ unzip master.zip
$ cd dlib-master/
# 如果之前build过,先删除之前build的:$ sudo rm -r build
$ sudo mkdir build
$ cd build/
$ sudo cmake .. -DUSE_SSE4_INSTRUCTIONS=ON
$ sudo cmake --build . --config Release
$ cd ..# 在这一步决定你是给python2安装dlib还是给python3.具体取决于下面这个python你链接的是Python2还是python3
# 比如给python3装可以直接改成:sudo python3 setup.py install
# 亲测 18.04系统,16.04系统都可以这么装
$ sudo python setup.py install

验证:

$ python
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dlib
>>> dlib.DLIB_USE_CUDA
True
>>> dlib.cuda.get_num_devices()
4

如何在GPU上跑dlib人脸检测:
官网下载预训练模型:http://dlib.net/files/mmod_human_face_detector.dat.bz2

# to use gpu
detect_model = 'dlib_models/mmod_human_face_detector.dat'
assert os.path.exists(detect_model), 'No face detection model!'
detector = dlib.cnn_face_detection_model_v1(detect_model)
...image = cv2.imread(image_path)
gray = image
try:face_locations = detector(gray, 1)
except:import tracebacktraceback.print_exec()return -1
...
for (i, face_location) in enumerate(face_locations):# to use gpuface_location = face_location.rect# 人脸关键点预测# shape = args.predictor(gray, face_location)

关键点预测(CPU)

path_to_dlib_model = "dlib_models/shape_predictor_68_face_landmarks.dat"
assert os.path.exists(path_to_dlib_model), 'no dlib model!'
predictor = dlib.shape_predictor(path_to_dlib_model)
print('Dlib model loaded.')

参考:
http://devdoc.net/c/dlib-19.7/python/
https://blog.csdn.net/meng_zhi_xiang/article/details/90698243

报错1:E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.

$ sudo apt-get install libboost-all-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:The following packages have unmet dependencies:libboost-all-dev : Depends: libboost-container-dev but it is not going to be installedDepends: libboost-coroutine-dev but it is not going to be installedDepends: libboost-exception-dev but it is not going to be installedDepends: libboost-fiber-dev but it is not going to be installedDepends: libboost-iostreams-dev but it is not going to be installedDepends: libboost-locale-dev but it is not going to be installedDepends: libboost-log-dev but it is not going to be installedDepends: libboost-math-dev but it is not going to be installedDepends: libboost-program-options-dev but it is not going to be installedDepends: libboost-python-dev but it is not going to be installedDepends: libboost-random-dev but it is not going to be installedDepends: libboost-regex-dev but it is not going to be installedDepends: libboost-stacktrace-dev but it is not going to be installedDepends: libboost-timer-dev but it is not going to be installedDepends: libboost-type-erasure-dev but it is not going to be installedlibgirepository-1.0-1 : Breaks: python-gi (< 3.34.0-4~) but 3.20.0-0ubuntu1 is to be installedlibpython2.7-stdlib : Breaks: python-numpy (< 1:1.12.1-3.1ubuntu1) but 1:1.11.0-1ubuntu1 is to be installedBreaks: python-tk (< 2.7.14~rc1-1~) but 2.7.12-1~16.04 is to be installed
E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.

解决:如上文所示,使用源码编译安装boost

报错2:CUDA was found but your compiler failed to compile a simple CUDA program so dlib isn’t going to use CUDA.

$ sudo cmake .. -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1
...
-- Building a CUDA test project to see if your compiler is compatible with CUDA...
-- *****************************************************************************************************************
-- *** CUDA was found but your compiler failed to compile a simple CUDA program so dlib isn't going to use CUDA.
-- *** The output of the failed CUDA test compile is shown below:
-- ***
-- ***   Change Dir: /home/user1/Downloads/dlib-master/build/dlib/cuda_test_build
...

解决:如上文所示,GCC和G++要降级,我从9.x版本降级的

参考:
https://www.learnopencv.com/speeding-up-dlib-facial-landmark-detector/
https://www.boost.org/doc/libs/1_66_0/more/getting_started/unix-variants.html#get-boost
https://blog.csdn.net/qq_33495762/article/details/106479169