dlib在人脸的很多代码中用到。
在ubuntu上安装的时候关于dlib遇到了一些问题,以下将适用自己的解决方案做一个整理记录。
主要使用conda的虚拟环境
遇到的问题做了个汇总,是因为一个个解决了以后总有下一个问题,最后是用一个总的方案这些问题都消失了。
问题汇总
1. conda找不到dlib库
可能是源的问题
conda install -c menpo dlib
# 或者
pip install dlib
2. conda创建的虚拟环境无法安装dlib
报错信息为:
RuntimeError:
*******************************************************************
CMake must be installed to build the following extensions: dlib
*******************************************************************
3. conda创建的虚拟环境无法安装dlib
报错信息为
1 error detected in the compilation of "/tmp/tmpxft_0002b980_00000000-6_cuda_dlib.cpp1.ii".
CMake Error at dlib_generated_cuda_dlib.cu.o.Release.cmake:279 (message):
Error generating file
/tmp/pip-install-1mezk9zs/dlib/build/temp.linux-x86_64-3.8/dlib_build/CMakeFiles/dlib.dir/cuda/./dlib_generated_cuda_dlib.cu.o
dlib_build/CMakeFiles/dlib.dir/build.make:63: recipe for target 'dlib_build/CMakeFiles/dlib.dir/cuda/dlib_generated_cuda_dlib.cu.o' failed
make[2]: *** [dlib_build/CMakeFiles/dlib.dir/cuda/dlib_generated_cuda_dlib.cu.o] Error 1
make[2]: *** Waiting for unfinished jobs....
CMakeFiles/Makefile2:145: recipe for target 'dlib_build/CMakeFiles/dlib.dir/all' failed
make[1]: *** [dlib_build/CMakeFiles/dlib.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-1mezk9zs/dlib/setup.py", line 223, in
setup(
File "/opt/conda/envs/torch1.5/lib/python3.8/site-packages/setuptools/__init__.py", line 144, in setup
return distutils.core.setup(**attrs)
File "/opt/conda/envs/torch1.5/lib/python3.8/distutils/core.py", line 148, in setup
dist.run_commands()
File "/opt/conda/envs/torch1.5/lib/python3.8/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/opt/conda/envs/torch1.5/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/opt/conda/envs/torch1.5/lib/python3.8/site-packages/setuptools/command/install.py", line 61, in run
return orig.install.run(self)
File "/opt/conda/envs/torch1.5/lib/python3.8/distutils/command/install.py", line 545, in run
self.run_command('build')
File "/opt/conda/envs/torch1.5/lib/python3.8/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/opt/conda/envs/torch1.5/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/opt/conda/envs/torch1.5/lib/python3.8/distutils/command/build.py", line 135, in run
self.run_command(cmd_name)
File "/opt/conda/envs/torch1.5/lib/python3.8/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/opt/conda/envs/torch1.5/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/tmp/pip-install-1mezk9zs/dlib/setup.py", line 135, in run
self.build_extension(ext)
File "/tmp/pip-install-1mezk9zs/dlib/setup.py", line 175, in build_extension
subprocess.check_call(cmake_build, cwd=build_folder)
File "/opt/conda/envs/torch1.5/lib/python3.8/subprocess.py", line 364, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '--build', '.', '--config', 'Release', '--', '-j80']' returned non-zero exit status 2.
----------------------------------------
ERROR: Command errored out with exit status 1: /opt/conda/envs/torch1.5/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-1mezk9zs/dlib/setup.py'"'"'; __file__='"'"'/tmp/pip-install-1mezk9zs/dlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-t54jnsix/install-record.txt --single-version-externally-managed --compile --install-headers /opt/conda/envs/torch1.5/include/python3.8/dlib Check the logs for full command output.
4. 从dlib源码安装的报错
cmake和python setup.py均有报错,报错信息为
[ 1%] Building NVCC (Device) object dlib/CMakeFiles/dlib.dir/cuda/dlib_generated_cuda_dlib.cu.o
/home/alex/Desktop/dlib/dlib/cuda/cuda_dlib.cu(1691): error: calling a constexpr __host__ function("log1p") from a __device__ function("cuda_log1pexp") is not allowed. The experimental flag '--expt-relaxed-constexpr' can be used to allow this.
1 error detected in the compilation of "/tmp/tmpxft_0000635c_00000000-6_cuda_dlib.cpp1.ii".
CMake Error at dlib_generated_cuda_dlib.cu.o.Release.cmake:279 (message):
Error generating file
/home/alex/Desktop/dlib/build/dlib/CMakeFiles/dlib.dir/cuda/./dlib_generated_cuda_dlib.cu.o
dlib/CMakeFiles/dlib.dir/build.make:583: recipe for target 'dlib/CMakeFiles/dlib.dir/cuda/dlib_generated_cuda_dlib.cu.o' failed
make[2]: *** [dlib/CMakeFiles/dlib.dir/cuda/dlib_generated_cuda_dlib.cu.o] Error 1
CMakeFiles/Makefile2:93: recipe for target 'dlib/CMakeFiles/dlib.dir/all' failed
make[1]: *** [dlib/CMakeFiles/dlib.dir/all] Error 2
Makefile:151: recipe for target 'all' failed
make: *** [all] Error 2
5. 使用conda安装dlib,opencv,pillow之后,opencv提示找不到
报错信息为
no module named cv2
解决方案
1. 全部使用conda的解决方案
apt install cmake
conda create -n tf1.14 python=3.6
source activate tf1.14
conda install tensorflow-gpu=1.14 keras=2.3
conda install -c menpo dlib # dlib需要先安装,后装就有BUG了
conda install pillow
pip install opencv-python # opencv使用conda里的pip装
conda install requests tqdm
2. 从源码安装dlib的解决方案
apt install cmake # 先装cmake总是对的
git clone https://github.com/davisking/dlib.git
cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1 -DCUDA_NVCC_FLAGS="--expt-relaxed-constexpr"
cmake --build . --config Release
python setup.py install --set DLIB_USE_CUDA=1 --set USE_AVX_INSTRUCTIONS=1 --set CUDA_NVCC_FLAGS="--expt-relaxed-constexpr" #我的python是链接到conda环境里的python的
参考