Ubuntu 18.4 running on GPU with dnn

In order to use the dnn module of OpenCV for model reasoning of deep learning, we need to install opencv ﹣ contrib. This paper records how to install opencv and opencv ﹣ contrib on Ubuntu 18.4, generate opencv that can be called by python and C + +, and run it on GPU.

Environmental Science

  • ubuntu18.04
  • opencv4.2.0
  • opencv_contrib4.2.0
  • cuda10.0
  • cudnn7.6.4 (GPU can be enabled when using dnn only when cudnn version is greater than 7.5)

setup script

1. Install related dependent Libraries

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake unzip pkg-config
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libv4l-dev libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3-dev

2. Download the opencv source code

 cd ~
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.2.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.2.0.zip
unzip opencv.zip
unzip opencv_contrib.zip
mv opencv-4.2.0 opencv
mv opencv_contrib-4.2.0 opencv_contrib

3.Determine CUDA architecture version
Link to https://developer.nvidia.com/cuda-gpus , find the Compute Capability version of the corresponding NVIDIA GPU, which will be useful when configuring and installing.

For example, my GPU is GeForce GTX 1060, and the corresponding Compute Capability is: 6.1
You can enter the command at the terminal to view the GPU type

nvidia-smi

display

$ nvidia-smi
Thu Mar 12 13:36:44 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 410.78       Driver Version: 410.78       CUDA Version: 10.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 106...  Off  | 00000000:01:00.0  On |                  N/A |
| 27%   42C    P8    10W / 120W |    652MiB /  6075MiB |      1%      Default |
+-------------------------------+----------------------+----------------------+

4. Configuration and installation

cd ~/opencv
mkdir build
cd build
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D OPENCV_ENABLE_NONFREE=ON -D WITH_CUDA=ON -D WITH_CUDNN=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_DNN_CUDA=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D CUDA_ARCH_BIN=6.1 -D WITH_CUBLAS=1 -D OPENCV_EXTRA_MODULES_PATH=~/Desktop/opencv_contrib/modules -D HAVE_opencv_python3=ON -D PYTHON_EXECUTABLE=~/.virtualenvs/opencv_cuda/bin/python -D BUILD_EXAMPLES=ON -D CUDA_CUDA_LIBRARY=/usr/local/cuda-10.0/lib64/stubs/libcuda.so ..

cmake Configuration Description:
1) Modify opencv? Extra? Modules? Path and CUDA? CUDA? Library to the corresponding path
2) opencv.pc is not generated by default above opencv4, so you need to specify opencv ﹣ generate ﹣ pkgconfig = on to generate the corresponding version of opencv4.pc, so you can compile through the g + + link. After the opencv is compiled and installed, you can view it under the path / usr/local/lib/pkgconfig /. If multiple versions of OpenCV are installed, you can specify the version when compiling with g + +:

g++ xxxx.cpp -o xxxx `pkg-config --cflags --libs opencv4`

If opencv4 is installed below, the default generation should be opencv.pc, then the g + + compile time will be changed to:

g++ xxxx.cpp -o xxxx `pkg-config --cflags --libs opencv`

3) CUDA? Arch? Bin = 6.1 the version here must specify a pair, otherwise even if the installation is successful, GPU cannot be called, and 6.1 is the version of Compute Capability described in point 3 above.

5. Compile and install cmake after success

sudo make -j8
sudo make install
sudo ldconfig

6. Add the opencv library to the path
Reference resources https://blog.csdn.net/broliao/article/details/104718030

sudo gedit /etc/ld.so.conf.d/opencv.conf 

After executing this command, you may open a blank file. Regardless, you only need to add at the end of the file:

/usr/local/lib

Update after save

sudo ldconfig

7. configure bash

sudo gedit /etc/bash.bashrc 

Add at the end

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH 

Save, execute the following command to make the configuration effective

source /etc/bash.bashrc

To update

sudo updatedb

8. Create a link for opencv for python
If you don't need to use the python interface, this part can be omitted. The above configuration of OpenCV Python can also be omitted. If you want to use the python interface, you need to link the compiled opencv so file. The linking process is as follows:
1) To view so files:
Go to the directory / usr/local/lib/python3.6/dist-packages/cv2/python-3.6 /. There should be a file similar to cv2.cpython-36m-x86_-linux-gnu.so, which is the so file we are looking for.
2) Create link

cd /usr/local/lib/python3.6/dist-packages
sudo ln -s /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

9. test
python:

broliao@ljx:~$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.2.0'
>>> 

c++:
Reference resources https://blog.csdn.net/broliao/article/details/104718030

10. When using, select whether to use GPU or CPU by setting the following parameters
GPU:

Net net = readNet(modelPath, configPath, framework);
net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);

CPU:

Net net = readNet(modelPath, configPath, framework);
net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);

Article reference:
https://www.pyimagesearch.com/2020/02/03/how-to-use-opencvs-dnn-module-with-nvidia-gpus-cuda-and-cudnn/
https://blog.csdn.net/broliao/article/details/104718030

Published 12 original articles, won praise 0, visited 288
Private letter follow

Tags: OpenCV sudo Python cmake

Posted on Thu, 12 Mar 2020 04:50:33 -0400 by timolein