因为一个错误提示信息,才发现默认的opencv是没有把cuda编译进去的.

setUpNet DNN module was not built with CUDA backend; switching to CPU

保险起见,就在docker里面安装编译啥的了.

docker pull ubuntu:22.04
docker run --gpus all -it --name my_ubuntu2204 ubuntu:22.04 /bin/bash

得到一个docker 容器,根据docker ps 可以查看容器的ID是d3开头的,简单起见,就直接docker start d3就可以了.

docker exec -it d3 /bin/bash

安装必要的工具

apt -y update
apt -y upgrade
apt -y install linux-headers-$(uname -r)
apt -y install wget unzip cmake gcc g++
apt -y install python3 python3-dev python3-numpy
apt -y install libavcodec-dev libavformat-dev libswscale-dev
apt -y install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
apt -y install libgtk-3-dev
apt -y install libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev

安装cuda

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda-repo-ubuntu2204-12-4-local_12.4.1-550.54.15-1_amd64.deb
dpkg -i cuda-repo-ubuntu2204-12-4-local_12.4.1-550.54.15-1_amd64.deb
cp /var/cuda-repo-ubuntu2204-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/
apt update
apt -y install cuda-toolkit-12-4
apt-get install -y cuda-drivers

下载opencv4.9.0

wget https://github.com/opencv/opencv/archive/refs/tags/4.9.0.tar.gz
wget https://github.com/opencv/opencv_contrib/archive/refs/tags/4.9.0.zip
unzip opencv_contrib-4.9.0.zip
tar xvf opencv-4.9.0.tar.gz
cd opencv-4.9.0
mkdir build && cd build 
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_CUDNN=ON -D WITH_CUBLAS=ON -D WITH_TBB=ON -D OPENCV_DNN_CUDA=ON -D OPENCV_ENABLE_NONFREE=ON  -D OPENCV_EXTRA_MODULES_PATH=/home/hesy/opencv_contrib-4.9.0/modules -D BUILD_EXAMPLES=OFF -D HAVE_opencv_python3=ON ..

最新的命令
直接拉官方的docker(https://hub.docker.com/r/nvidia/cuda/tags)

docker pull nvidia/cuda:12.3.2-cudnn9-devel-ubuntu22.04
docker run --gpus all -it --name my-opencv -v /home/hesy:/home/hesy nvidia/cuda:12.3.2-cudnn9-devel-ubuntu22.04 /bin/bash
apt -y update
apt -y upgrade
apt -y install linux-headers-$(uname -r)
apt -y install wget unzip cmake gcc g++
apt -y install python3 python3-dev python3-numpy
apt -y install libavcodec-dev libavformat-dev libswscale-dev
apt -y install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
apt -y install libgtk-3-dev libfreeimage3 libfreeimage-dev
apt -y install libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev libdc1394-dev

mkdir -p /home/hesy/opencv && cd /home/hesy/opencv
wget https://github.com/opencv/opencv/archive/refs/tags/4.9.0.tar.gz
wget https://github.com/opencv/opencv_contrib/archive/refs/tags/4.9.0.zip
unzip opencv_contrib-4.9.0.zip
tar xvf opencv-4.9.0.tar.gz
cd opencv-4.9.0
mkdir build && cd build 
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_CUDNN=ON -D WITH_CUBLAS=ON -D WITH_TBB=ON -D OPENCV_DNN_CUDA=ON -D OPENCV_ENABLE_NONFREE=ON  -D OPENCV_EXTRA_MODULES_PATH=/home/hesy/opencv/opencv_contrib-4.9.0/modules -D BUILD_EXAMPLES=OFF -D HAVE_opencv_python3=ON ..
make

编译报错:(
error: 'cudnnSetRNNDescriptor_v6' was not declared in this scope; did you mean 'cudnnSetRNNDescriptor_v8
error: 'cudnnRNNForwardInference' was not declared in this scope
error: 'cudnnGetRNNWorkspaceSize' was not declared in this scope; did you mean 'cudnnGetRNNWeightSpaceSize'?

在网上( https://github.com/opencv/opencv/issues/24983 )看到相关说法是cudnn9不行,用cudnn8.9.7就可以了.去下载一个8.9.7试试看

重新开一个新的docker,使用nvidia/cuda:12.3.2-devel-ubuntu22.04,不带cudnn版本的容器.在容器执行下面的命令

apt -y update
apt -y upgrade
apt -y install linux-headers-$(uname -r)
apt -y install wget unzip cmake gcc g++
apt -y install python3 python3-dev python3-numpy
apt -y install libavcodec-dev libavformat-dev libswscale-dev
apt -y install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
apt -y install libgtk-3-dev libfreeimage3 libfreeimage-dev
apt -y install libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev libdc1394-dev

下载cudnn8.9.7的本地安装包,官方网站要注册登录才能下载.不复杂,几分钟时间就能搞定.
https://developer.download.nvidia.com/compute/cudnn/secure/8.9.7/local_installers/12.x/cudnn-local-repo-ubuntu2204-8.9.7.29_1.0-1_amd64.deb

dpkg -i cudnn-local-repo-ubuntu2204-8.9.7.29_1.0-1_amd64.deb
cp /var/cudnn-local-repo-*/cudnn-local-*-keyring.gpg /usr/share/keyrings/
apt update
apt -y install libcudnn8 libcudnn8-dev libcudnn8-samples

校验 编译mnistCUDNN

cp -r /usr/src/cudnn_samples_v8/ /home/hesy
cd  /home/hesy/cudnn_samples_v8/mnistCUDNN
make clean && make

执行检测,看到pass表示测试通过

# 在/home/hesy/cudnn_samples_v8/mnistCUDNN中执行./mnistCUDNN进行测试
./mnistCUDNN

测试结果:

root@3a452f9297bd:/home/hesy/cudnn_samples_v8/mnistCUDNN# ./mnistCUDNN 
Executing: mnistCUDNN
cudnnGetVersion() : 8907 , CUDNN_VERSION from cudnn.h : 8907 (8.9.7)
Host compiler version : GCC 11.4.0

There are 1 CUDA capable devices on your machine :
device 0 : sms 128  Capabilities 8.9, SmClock 2520.0 Mhz, MemSize (Mb) 24003, MemClock 10501.0 Mhz, Ecc=0, boardGroupID=0
Using device 0
***
Result of classification: 1 3 5

Test passed!

参考: https://juejin.cn/post/7319717702376882227

本来以为这个东西简简单单,没想到一通折腾就是一二天.

cudnn安装成功了,继续折腾编译opencv

mkdir -p /home/hesy/opencv && cd /home/hesy/opencv
wget https://github.com/opencv/opencv/archive/refs/tags/4.9.0.tar.gz
wget https://github.com/opencv/opencv_contrib/archive/refs/tags/4.9.0.zip
unzip opencv_contrib-4.9.0.zip
tar xvf opencv-4.9.0.tar.gz
cd opencv-4.9.0
mkdir build && cd build 
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_CUDNN=ON -D WITH_CUBLAS=ON -D WITH_TBB=ON -D OPENCV_DNN_CUDA=ON -D OPENCV_ENABLE_NONFREE=ON  -D OPENCV_EXTRA_MODULES_PATH=/home/hesy/opencv/opencv_contrib-4.9.0/modules -D BUILD_EXAMPLES=OFF -D HAVE_opencv_python3=ON ..
make

发现下载IPPICV非常慢,有条件的就上代理吧,另外不知道多线程行不行,所以没敢make -j. 最后发现实在是太慢了,还是

make -j32

快得飞起.但是但是,最后一点点会只用1个cpu,慢得很,只能慢慢等了.前97%感觉没有后3%花的时间多.


export http_proxy="socks5://127.0.0.1:10008"
export https_proxy="socks5://127.0.0.1:10008"
本文链接地址:https://const.net.cn/791.html

标签: none

添加新评论