Jetson Nano compile and install opencv4.3.0 and enable cuDNN acceleration

Preliminary summary:
  When using opencv, try to use cuda to speed up the loading process of related models, but the following problems occur in actual use:

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

  There is no definition of "DNN_BACKEND_CUDA" in the installed opencv. The version of opencv installed in the original system of Jetson nano is 4.1.1, and the version of the original system used at this time has not been updated, but the DNN module was added in the update of version 4.2.0, see: OpenCV 4.2.0 Released, Intel's open source computer vision library and OpenCV 4.2.0 pull request CUDA backend for the DNN module #14827 .
  OK, now, first uninstall the OpenCV installed on the original system:

sudo apt-get remove libopencv*

  Regarding the installation of opencv that supports cuda, jetsonhacks has a related post to share, which has a detailed description, and also gives the installation script, but due to network problems, try to use this script to install successfully, or download and install it by yourself Yes, you can refer to this post if you want to try: https://www.jetsonhacks.com/2019/11/22/opencv-4-cuda-on-jetson-nano/
, extract the key points of this boss’s post

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

  I installed version 4.4.0. In fact, the initial installation was 4.3.0, but due to various bugs, it was easy to change to 4.4.0, but the whole installation process including the part of opencv4.3.0 will also be recorded together.
1. Download opencv-4.3.0 and 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 the file

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

3. Compile

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: An error occurred during compilation

[ 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 library not found?

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

4. Installation

make &&  sudo make install

  Fortunately, there was nothing wrong with the next installation, but there were still problems during the actual call. When the program is running, it prompts:

setUpNet DNN module was not built with CUDA backend; switching to CPU
  In fact, there was a prompt in the above process, but I ignored it without directly reporting an error, as shown in the following figure:
Insert picture description here

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

Check whether the system is properly installed with cuDNN: dpkg -l | grep -i cudnn
Insert picture description here
cuDNN is normally installed, and the version is 8.0, which meets the at least "7.5" requirement for opencv installation. Refer to a post on the NVIDIA Developer Forum stating that the version of cuDNN is installed. The portal of the post is as follows: 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 ..          

Compiling again with cmake, cuDNN can be found:
Insert picture description here
and
Insert picture description here
but other problems still occur during the compilation process:

/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

  Look at the posts on the Nvidia forum, it should be a problem of opencv and cuDNN version adaptation. For details, see: cuda4dnn(build): add basic support for cuDNN 8 #17685 , update the opencv version to 4.4.0, add opencv version 4.4.0 The related support for the cuDNN 8 version has been installed so far, and the related problems have been solved by installing opencv 4.4.0.
OK, now before and after comparison of the system conditions using GPU acceleration: Before
turning on cuda acceleration: After
Insert picture description here
Insert picture description here
turning on cuda acceleration: The
Insert picture description here
Insert picture description here
  effect is obvious. The face detection that was originally purely running on the CPU basically occupied 70~80% of the four cores of the CPU. But the GPU does occupy 0%. After the acceleration is turned on, the CPU occupies only 55% of the core, and the GPU is also running.

Guess you like

Origin blog.csdn.net/qq_33475105/article/details/111659692