当前位置:   article > 正文

Linux/Debian/Ubuntu-OpenCV(4.5.4/4.6.0)+CUDA(11.3)配置编译全流程_linux安装带cuda的opencv

linux安装带cuda的opencv


前言

本文用来记录在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
  • 1
  • 2

历史版本下载汇总页面
在这里插入图片描述

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++-9

sudo 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/
  • 1
  • 2
  • 3
  • 4
  • 5

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 \
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

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 \
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

install necessary lib

sudo apt install libgtk2.0-dev libgtk-3-dev
  • 1
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/lib64

vim /etc/ld.so.conf
# add lib
/usr/local/cuda/lib64

./predict --image ./images --model ./models/yolov5m-seg.onnx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

错误异常

运行程序报错(-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'
  • 1
  • 2
  • 3

  1. NVIDIA CUDA Toolkit Release Notes ↩︎

  2. CUDA Toolkit 11.3 Update 1 Downloads ↩︎

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/778312
推荐阅读
相关标签
  

闽ICP备14008679号