文章目录
- 前言
- 相关资源下载
- OpenCV
- CUDA下载
- CUDNN下载
- 编译
- 错误异常
前言
本文用来记录在linux环境下docker中编译OpenCV with cuda
的过程,同时编译了4.5.4和4.6.0两个版本均可编译通过。
本地是linux环境也可参考本文完成编译。
系统:debian 11
CPU:i7
内存:16G
显卡:Nvidia Quadro M2000
相关资源下载
OpenCV
github直接下载连接:
OpenCV 4.5.4 source code
OpenCV 4.6.0 source code
如需下载其他版本可自行通过下述链接下载:
github release page
CUDA下载
对应CUDA版本最低显卡驱动要求1,最新527显卡驱动有点问题,我下载的是470版本linux驱动,顺利安装运行。
显卡驱动安装可参考下述文章:debian11安装Nvidia驱动及Docker运行
CUDA 11.3.1下载2
https://developer.nvidia.com/cuda-11-3-1-download-archive
wget https://developer.download.nvidia.com/compute/cuda/11.3.1/local_installers/cuda_11.3.1_465.19.01_linux.run
sudo sh cuda_11.3.1_465.19.01_linux.run
历史版本下载汇总页面
CUDNN下载
CUDNN下载需要注册Nvidia账户。
Download cuDNN v8.8.0 (February 7th, 2023), for CUDA 11.x
解压指令:
tar -xvf cudnn-linux-x86_64-8.8.0.121_cuda11-archive.tar.xz
如需其它版本,自行下载对应库:
历史版本下载
解压后复制到CUDA对应目录
sudo apt-get install gcc-9 g++-9sudo ln -s /usr/local/cuda-11.3 cuda
sudo rsync -av /bing_ws/opencv_with_cuda/cudnn/cuda/lib64/* /usr/local/cuda-11.3/lib64/
sudo rsync -av /bing_ws/opencv_with_cuda/cudnn/cuda/include/* /usr/local/cuda-11.3/include/
NVIDIA显卡计算能力一览表
https://developer.nvidia.com/cuda-gpus
编译
compaile opencv 4.5.4
cmake -S ../opencv -B . \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CONFIGURATION_TYPES=Release \
-DCMAKE_INSTALL_PREFIX=/usr/local/opencv \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DOPENCV_EXTRA_MODULES_PATH=/bing_ws/opencv_with_cuda/opencv_contrib-4.5.4/modules \
-DBUILD_EXAMPLES=OFF \
-DBUILD_TEST=OFF \
-DBUILD_opencv_world=ON \
-DOPENCV_ENABLE_NONFREE=ON \
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_python3=ON \
-DWITH_EIGEN=ON \
-DWITH_OPENGL=OFF \
-DCMAKE_LIBRARY_PATH=/usr/local/cuda-11.3/lib64/stubs \
-DWITH_CUDA=ON \
-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.3 \
-DOPENCV_DNN_CUDA=ON \
-DWITH_CUBLAS=ON \
-DWITH_CUDNN=ON \
-DCUDA_FAST_MATH=ON \
-DENABLE_FAST_MATH=ON \
-DCUDA_ARCH_BIN=5.2;6.1;7.5;8.6 \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DWITH_VTK=ON \
-DWITH_GTK=ON \
-DBUILD_JAVA=OFF# 暂时不用,只需上述配置参数
-DCUDA_GENERATION=Auto \
-DCUDA_nppicom_LIBRARY=stdc++ \
-DWITH_GSTREAMER=ON \
-DVIDEOIO_PLUGIN_LIST=gstreamer \
compaile opencv 4.6.0
cmake -S ../opencv-4.6.0 -B . \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CONFIGURATION_TYPES=Release \
-DCMAKE_INSTALL_PREFIX=/usr/local/opencv \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DOPENCV_EXTRA_MODULES_PATH=/bing_ws/opencv_with_cuda/opencv_460/opencv_contrib-4.6.0/modules \
-DBUILD_EXAMPLES=OFF \
-DBUILD_TEST=OFF \
-DBUILD_opencv_world=ON \
-DOPENCV_ENABLE_NONFREE=ON \
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_python3=ON \
-DWITH_EIGEN=ON \
-DWITH_OPENGL=OFF \
-DCMAKE_LIBRARY_PATH=/usr/local/cuda-11.3/lib64/stubs \
-DWITH_CUDA=ON \
-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.3 \
-DOPENCV_DNN_CUDA=ON \
-DWITH_CUBLAS=ON \
-DWITH_CUDNN=ON \
-DCUDA_FAST_MATH=ON \
-DENABLE_FAST_MATH=ON \
-DCUDA_ARCH_BIN=5.2;6.1;7.5;8.6 \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DWITH_VTK=ON \
-DWITH_GTK=ON \
-DBUILD_JAVA=OFF# 暂时不用,只需上述配置参数
-DCUDA_GENERATION=Auto \
-DCUDA_nppicom_LIBRARY=stdc++ \
-DWITH_GSTREAMER=ON \
-DVIDEOIO_PLUGIN_LIST=gstreamer \
install necessary lib
sudo apt install libgtk2.0-dev libgtk-3-dev
export PATH=${PATH}:/usr/local/cuda/bin
export C_INCLUDE_PATH=${C_INCLUDE_PATH}:/usr/local/cuda/include
export CPLUS_INCLUDE_PATH=${CPLUS_INCLUDE_PATH}:/usr/local/cuda/include
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda/lib64
export LIBRARY_PATH=${LIBRARY_PATH}:/usr/local/cuda/lib64vim /etc/ld.so.conf
# add lib
/usr/local/cuda/lib64./predict --image ./images --model ./models/yolov5m-seg.onnx
错误异常
运行程序报错(-217:Gpu API call) cuDNN did not return a suitable algorithm for convolution.
原始错误如下所示:
[ WARN:0@0.317] global /bing_ws/opencv_with_cuda/opencv_460/opencv-4.6.0/modules/dnn/src/op_cuda.cpp (34) initCUDABackend The selected CUDA device does not support FP16 target; switching to FP32 target.
terminate called after throwing an instance of 'cv::Exception'what(): OpenCV(4.6.0) /bing_ws/opencv_with_cuda/opencv_460/opencv-4.6.0/modules/dnn/src/layers/../cuda4dnn/primitives/../csl/cudnn/convolution.hpp:303: error: (-217:Gpu API call) cuDNN did not return a suitable algorithm for convolution. in function 'ConvolutionAlgorithm'
NVIDIA CUDA Toolkit Release Notes ↩︎
CUDA Toolkit 11.3 Update 1 Downloads ↩︎