Jetson Nano编译安装opencv4.3.0并使能cuDNN加速

前情提要:
  在使用opencv时,尝试利用cuda进行相关模型加载处理的加速,但是在实际的使用时出现以下问题:

Traceback (most recent call last):
File "/home/colin/works/face_detect/detect_faces_video.py", line 70, in <module>
CatchUsbVideo("Find face", 0, 0)
File "/home/colin/works/face_detect/detect_faces_video.py", line 56, in CatchUsbVideo
resImage = detect_faces_image.face_detector(frame, 1)
File "/home/colin/works/face_detect/detect_faces_image.py", line 81, in face_detector
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
AttributeError: module 'cv2.dnn' has no attribute 'DNN_BACKEND_CUDA'
GST_ARGUS: Cleaning up

  在已安装的opencv中并没有关于“DNN_BACKEND_CUDA”的定义。Jetson nano中原系统已安装的opencv的version 是4.1.1,此时使用的还是原来系统的版本还未更新,但是4.2.0 版本的更新中才新增了DNN module,详见:OpenCV 4.2.0 发布,Intel 开源的计算机视觉库 和OpenCV 4.2.0的pull requestCUDA backend for the DNN module #14827
  OK,Now,先把原系统安装的OpenCV给卸了:

sudo apt-get remove libopencv*

  关于安装支持cuda的opencv,jetsonhacks大佬有相关的帖子分享,其中有细述,并且还给出了安装的脚本,但是由于网络之类的问题,尝试用这个脚本没安装成功,还是自行下载安装的,想尝试的可以参考该帖子:https://www.jetsonhacks.com/2019/11/22/opencv-4-cuda-on-jetson-nano/
,把这大佬的帖子的关键点摘录出来

There are already some good resources for building OpenCV on the Jetson. First, there is the “official” NVIDIA script in this Github repository: https://github.com/AastaNV/JEP As of this writing, the OpenCV script is in the scripts directory, the name of the script is install_opencv4.x.x_Jetson.sh

  本人安装的是4.4.0版本。其实最开始安装安装的是4.3.0,但是由于各种bug,更换到4.4.0就好使了,不过这整个个安装的过程包括opencv4.3.0的部分还一起记一下流水账。
1、下载opencv-4.3.0和opencv_contrib-4.3.0。

curl -L https://github.com/opencv/opencv/archive/4.3.0.zip -o opencv-4.3.0.zip
curl -L https://github.com/opencv/opencv_contrib/archive/4.3.0.zip -o opencv_contrib-4.3.0.zip

2、解压文件

unzip opencv-4.3.0.zip
unzip opencv_contrib-4.3.0.zip

3、编译

cmake -D WITH_CUDA=ON -D CUDA_ARCH_BIN="5.3" -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.3.0/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python2=ON -D BUILD_opencv_python3=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

Error:编译过程中出现错误

[ 25%] Building CXX object modules/core/CMakeFiles/opencv_core_pch_dephelp.dir/opencv_core_pch_dephelp.cxx.o
In file included from /home/colin/tool/opencv-4.2.0/modules/core/src/precomp.hpp:55:0,
from /home/colin/tool/opencv-4.2.0/release/modules/core/opencv_core_pch_dephelp.cxx:1:
/home/colin/tool/opencv-4.2.0/modules/core/include/opencv2/core/private.hpp:66:12: fatal error: Eigen/Core: No such file or directory
# include <Eigen/Core>
^~~~~~~~~~~~
compilation terminated.

  Eigen库找不到?

将安装的Eigen库进行链接:
cd /usr/include
sudo ln -sf eigen3/Eigen Eigen

4、安装

make &&  sudo make install

  好在接下来的安装并没有出什么岔子,但是在实际调用的时候仍然出现了问题,程序运行的时候提示:

setUpNet DNN module was not built with CUDA backend; switching to CPU
  其实在上述的过程中也出现一个提示,不过没直接报错竟让被我给忽略掉了,如下图所示:
在这里插入图片描述

Could NOT find CUDNN: Found unsuitable version "..", but required is at least "7.5" (found /usr/local/cuda-10.2/lib64/libcudnn.so)

检查一下系统是否正常安装好了cuDNN:dpkg -l | grep -i cudnn
在这里插入图片描述
cuDNN是有正常安装的,且版本为8.0,满足opencv安装使用的 at least "7.5"的需求。参考NVIDIA开发者论坛的一个帖子,声明cuDNN的版本进行安装,帖子的传送门如下:https://forums.developer.nvidia.com/t/opencv-4-2-0-and-cudnn-for-jetson-nano/112281/32,Oh,It’s worked。

cmake -D WITH_CUDA=ON -D CUDA_ARCH_BIN="5.3"-D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON  -D CUDNN_VERSION='8.0' -D CUDNN_INCLUDE_DIR='/usr/include/' -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.3.0/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python2=ON -D BUILD_opencv_python3=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..          

再次cmake进行编译,能找到cuDNN:
在这里插入图片描述
and
在这里插入图片描述
但是编译过程中还是出现了其他问题:

/home/colin/tool/opencv-4.3.0/modules/dnn/src/layers/…/cuda4dnn/primitives/…/csl/cudnn/convolution.hpp:266:21: error: ‘CUDNN_CONVOLUTION_FWD_PREFER_FASTEST’ was not declared in this scope
/home/colin/tool/opencv-4.3.0/modules/dnn/src/layers/…/cuda4dnn/primitives/…/csl/cudnn/transpose_convolution.hpp:42:21: error: ‘CUDNN_CONVOLUTION_BWD_DATA_PREFER_FASTEST’ was not declared in this scope

  看英伟达论坛的帖子,应该是opencv和cuDNN版本适配的问题,详细可见:cuda4dnn(build): add basic support for cuDNN 8 #17685,update the opencv version to 4.4.0, opencv的4.4.0版本添加了相关对于cuDNN 8版本的支持,至此安装opencv 4.4.0,相关的问题都解决了。
OK, now 使用GPU加速的系统状况的前后对比:
开启cuda加速前:
在这里插入图片描述
在这里插入图片描述
开启cuda加速后:
在这里插入图片描述
在这里插入图片描述
  效果明显,原本单纯在CPU跑的人脸检测,CPU四个核基本上都占用到70~80%,但是GPU确实0%占用,开启加速之后,CPU占用最多的一个核才55%,GPU也跑起来了。

猜你喜欢

转载自blog.csdn.net/qq_33475105/article/details/111659692
今日推荐