Ubuntu16.04+CUDA9.0+Python2.7环境下安装OpenCV3.4
花了两天时间终于安装好了opencv,遇到好几个bug发现能找到的解决方案比较少,同时需要*,所以写下这个笔记记录一下。我是根据这个教程来安装OpenCV的,进行到第四步OpenCV的配置和编译开始报错:
报错1.Cmake编译opencv源码时出现以下报错:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nppi_LIBRARY (ADVANCED)
linked by target "opencv_cudev" in directory D:/Cproject/opencv/opencv/sources/modules/cudev
linked by target "opencv_cudev" in directory D:/Cproject/opencv/opencv/sources/modules/cudev
linked by target "opencv_test_cudev" in directory D:/Cproject/opencv/opencv/sources/modules/cudev/test
linked by target "opencv_core" in directory D:/Cproject/opencv/opencv/sources/modules/core
..........................................
linked by target "opencv_createsamples" in directory D:/Cproject/opencv/opencv/sources/apps/createsamples
linked by target "opencv_annotation" in directory D:/Cproject/opencv/opencv/sources/apps/annotation
linked by target "opencv_visualisation" in directory D:/Cproject/opencv/opencv/sources/apps/visualisation
linked by target "opencv_version" in directory D:/Cproject/opencv/opencv/sources/apps/version
报错原因:cuda9不再支持2.0架构,比如nppi库在9.0中被分成了很多小的库,因此这里需要将nppi库改动一下。
解决方案:
1). 在Cmake文件夹下找到FindCUDA.cmake文件并修改
#找到如下这一行
find_cuda_helper_libs(nppi)
#将这一行更改为:
find_cuda_helper_libs(nppial)
find_cuda_helper_libs(nppicc)
find_cuda_helper_libs(nppicom)
find_cuda_helper_libs(nppidei)
find_cuda_helper_libs(nppif)
find_cuda_helper_libs(nppig)
find_cuda_helper_libs(nppim)
find_cuda_helper_libs(nppist)
find_cuda_helper_libs(nppisu)
find_cuda_helper_libs(nppitc)
#找到如下这一行
set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppi_LIBRARY};${CUDA_npps_LIBRARY}")
#将其更改为:
set(CUDA_npp_LIBRARY "${CUDA_nppc_LIBRARY};${CUDA_nppial_LIBRARY};${CUDA_nppicc_LIBRARY};${CUDA_nppicom_LIBRARY};${CUDA_nppidei_LIBRARY};${CUDA_nppif_LIBRARY};${CUDA_nppig_LIBRARY};${CUDA_nppim_LIBRARY};${CUDA_nppist_LIBRARY};${CUDA_nppisu_LIBRARY};${CUDA_nppitc_LIBRARY};${CUDA_npps_LIBRARY}")
#找到如下这一行
unset(CUDA_nppi_LIBRARY CACHE)
#将其更改为:
unset(CUDA_nppial_LIBRARY CACHE)
unset(CUDA_nppicc_LIBRARY CACHE)
unset(CUDA_nppicom_LIBRARY CACHE)
unset(CUDA_nppidei_LIBRARY CACHE)
unset(CUDA_nppif_LIBRARY CACHE)
unset(CUDA_nppig_LIBRARY CACHE)
unset(CUDA_nppim_LIBRARY CACHE)
unset(CUDA_nppist_LIBRARY CACHE)
unset(CUDA_nppisu_LIBRARY CACHE)
unset(CUDA_nppitc_LIBRARY CACHE)
2).同一个文件夹中找到OpenCVDetectCUDA.cmake文件并修改,这里做修改是因为cuda9.0架构已经不支持“Fermi”2.0版本
#将如下几行
...
set(__cuda_arch_ptx "")
if(CUDA_GENERATION STREQUAL "Fermi")
set(__cuda_arch_bin "2.0")
elseif(CUDA_GENERATION STREQUAL "Kepler")
set(__cuda_arch_bin "3.0 3.5 3.7")
...
#修改为:
...
set(__cuda_arch_ptx "")
if(CUDA_GENERATION STREQUAL "Kepler")
set(__cuda_arch_bin "3.0 3.5 3.7")
elseif(CUDA_GENERATION STREQUAL "Maxwell")
set(__cuda_arch_bin "5.0 5.2")
...
#将如下一行
set(__cuda_arch_bin "2.0 3.0 3.5 3.7 5.0 5.2 6.0 6.1")
修改为:
set(__cuda_arch_bin "3.0 3.5 3.7 5.0 5.2 6.0 6.1")
3).cuda9中有一个单独的halffloat(cuda_fp16.h)头文件,也应该被包括在opencv的目录里,即将头文件cuda_fp16.h添加至opencv\modules\cudev\include\opencv2\cudev\common.hpp,即在common.hpp中添加:
#include <cuda_fp16.h>
然后重新生成即可。
报错2.Cmake编译opencv源码依然报错:
See also "/root/opencv/build/CMakeFiles/CMakeOutput.log".
See also "/root/opencv/build/CMakeFiles/CMakeError.log".
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_CUDA_LIBRARY (ADVANCED)
linked by target "example_gpu_alpha_comp" in directory /root/opencv/samples/gpu
linked by target "example_gpu_bgfg_segm" in directory /root/opencv/samples/gpu
linked by target "example_gpu_cascadeclassifier" in directory /root/opencv/samples/gpu
linked by target "example_gpu_cascadeclassifier_nvidia_api" in directory /root/opencv/samples/gpu
linked by target "example_gpu_driver_api_multi" in directory /root/opencv/samples/gpu
........................
linked by target "example_gpu_surf_keypoint_matcher" in directory /root/opencv/samples/gpu
linked by target "example_gpu_video_reader" in directory /root/opencv/samples/gpu
linked by target "example_gpu_video_writer" in directory /root/opencv/samples/gpu
报错原因:CUDA_CUDA_LIBRARY没找到,因此需要在cmake的时候加入路径,参考。
#即在原始指令中加入最后一行的参数
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D BUILD_EXAMPLES=ON
-DCMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs .. #添加项
以上完成之后make时继续报错:
报错1.
Building NVCC (Device) object src/gromacs/CMakeFiles/libgromacs.dir/gmxlib/cuda_tools/./libgromacs_generated_copyrite_gpu.cu.o
nvcc fatal : Unsupported gpu architecture 'compute_20'
CMake Error at libgromacs_generated_copyrite_gpu.cu.o.cmake:208 (message):
Error generating
/opt/gromacs/build-gromacs/src/gromacs/CMakeFiles/libgromacs.dir/gmxlib/cuda_tools/./libgromacs_generated_copyrite_gpu.cu.o
src/gromacs/CMakeFiles/libgromacs.dir/build.make:55: recipe for target 'src/gromacs/CMakeFiles/libgromacs.dir/gmxlib/cuda_tools/./libgromacs_generated_copyrite_gpu.cu.o' failed
make[2]: *** [src/gromacs/CMakeFiles/libgromacs.dir/gmxlib/cuda_tools/./libgromacs_generated_copyrite_gpu.cu.o] Error 1
CMakeFiles/Makefile2:1938: recipe for target 'src/gromacs/CMakeFiles/libgromacs.dir/all' failed
make[1]: *** [src/gromacs/CMakeFiles/libgromacs.dir/all] Error 2
Makefile:143: recipe for target 'all' failed
make: *** [all] Error 2
这里是make到百分之33左右的时候报错,这个错误解决了好久才大概明白原因,同时也对之前的报错的原因有了更深的了解,报错的原因是compute_20已经不被CUDA9.0支持了,因此其实是第一步教程中CMAKE的时候一个参数没有被指定导致的。
#因此在最后一行加入
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D BUILD_EXAMPLES=ON
-DCMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs
-D CUDA_ARCH_BIN="6.1".. #添加项,此处具体的数字需在官网查看对应GPU所支持的数字
报错2.
modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/build.make:290: recipe for target ‘modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/graphcuts.cpp.o’ failed
make[2]: *** [modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/src/graphcuts.cpp.o] Error
报错原因:CUDA配置出错,GraphCut在cuda7.5中弃用,在cuda8.0以上的版本中中被移除, 修改~/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp文件。
把第45行修改成:
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)
即在 #if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) 加上 || (CUDART_VERSION >= 8000),然后重新执行 make -j8 即可。
总结:以上报错的主要原因是CUDA升级导致库发生变化无法找到,以及版本升级后一部分配置不再支持导致报错,CMAKE时需要指定特定配置防止出错,同时修改库文件并注意头文件的添加。