Install OpenCV 4.5.1 with CUDA and cuDNN acceleration, with GStreamer and Python 3 bindings on PopOS 20.10
OpenCV is awesome. OpenCV + CUDA = More awesome. OpenCV + CUDA + cuDNN (deep neural networks) = MIND BLOWN.
What is cuDNN?
The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers.
Compiling from source is always a pain. I’m here to make your life easier. Today I’m going to show you how to build and install OpenCV 4.5.1 with CUDA and cuDNN acceleration, with GStreamer and Python 3 bindings on PopOS 20.10. Since PopOS is based on Ubuntu, the instructions will work on that as well.
LET’S DO THIS.
My workstation specs are:
AMD Ryzen 3950X.
NVIDIA RTX 2080TI.
Fresh install of PopOS 20.10 with NVIDIA drivers.
Check your GPUs CUDA version support
Go to NVIDIA’s website to check your which version of CUDA it supports:
Make a note of the version number as we’ll need this information when we compile OpenCV. For my RTX 2080TI, the CUDA version is 7.5.
sudo apt updatesudo apt dist-upgrade -y --autoremove
Install CUDA and cuDNN libraries
Installing cuDNN will also install matching CUDA version.
sudo apt install system76-cudnn-11.1
If for some reason CUDA was not installed, you can install it with:
sudo apt install system76-cuda-11.1
Download cuDNN source files from NVIDIA.
These source files are needed to compile OpenCV.
The file you want is: cudnn-11.1-linux-x64-v18.104.22.168.tgz
Put the file in your home directory and we’ll move these into the right place:
cd ~mv cudnn-11.1-linux-x64-v22.214.171.124.tgz cudnn.tgztar -xvf cudnn.tgz
Make the appropriate directories:
sudo mkdir -p /usr/local/cuda/lib64/sudo mkdir -p /usr/local/cuda/include
Copy the extracted files to the folders we created:
cd ~/cuda/lib64sudo cp * /usr/local/cuda/lib64/
cd ~/cuda/includesudo cp * /usr/local/cuda/include
Create Python virtual environment
Install virtual environment tools:
sudo pip install virtualenv virtualenvwrappersudo rm -rf ~/.cache/pip
Add the appropriate entries to bashrc:
echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrcecho "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrcecho "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrcecho "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrcsource ~/.bashrc
Create a virtual environment called CV:
mkvirtualenv cv -p python3workon cv
Install numpy in our virtual environment as it will be need to compile OpenCV:
pip install numpy
sudo apt install -y \
Prepare OpenCV source files
Make our working folder in home:
cd ~mkdir build_opencvcd build_opencv
Clone OpenCV into our working directory:
git clone — depth 1 — branch “4.5.1” https://github.com/opencv/opencv.git
Clone OpenCV libraries:
git clone --depth 1 --branch "4.5.1" https://github.com/opencv/opencv_contrib.git
Make build folder inside the OpenCV folder:
cd opencvmkdir buildcd build
Configure and build OpenCV
Make sure you have the right GPU version number from earlier and change this line to match your GPU CUDA version:
-D CUDA_ARCH_BIN=7.5 \
Configure the build:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D ENABLE_CXX11=ON \
-D CMAKE_C_COMPILER=/usr/bin/gcc-9 \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_cudacodec=OFF \
-D CUDA_ARCH_BIN=7.5 \
-D CUDA_ARCH_PTX= \
-D CUDNN_VERSION='8.0.5' \
-D CUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so.8.0.5 \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \
-D CUDA_FAST_MATH=1 \
-D ENABLE_FAST_MATH=1 \
-D OPENCV_DNN_CUDA=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/build_opencv/opencv_contrib/modules \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D WITH_QT=OFF \
-D WITH_TBB=ON \
-D WITH_CUBLAS=1 \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D WITH_GSTREAMER=ON \
-D WITH_LIBV4L=ON \
-D WITH_OPENGL=ON \
-D BUILD_EXAMPLES=ON .. \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
If the configuration completed without errors, we can build it. Change the number to the number of cpu threads to use. I have a 16 core, 32 thread CPU so I use 30 threads to leave some cpu headroom for me to watch YouTube videos while its building:
sudo make installsudo ldconfig
Let’s check to see if the OpenCV Python module is installed in the right path:
ls /usr/local/lib/python3.8/site-packages/cv2/python-3.8/# cv2.cpython-38-x86_64-linux-gnu.so cv2.so
We’ll rename the module and link it into our virtual environment:
cd /usr/local/lib/python3.8/site-packages/cv2/python-3.8/sudo mv cv2.cpython-38-x86_64-linux-gnu.so cv2.so
Lets create a symbolic link to our virtual environment:
cd ~/.virtualenvs/cv/lib/python3.8/site-packages/ln -s /usr/local/lib/python3.8/site-packages/cv2/python-3.8/cv2.so cv2.so
pkg-config — modversion opencv# 4.5.1
Check with python:
python3 -c "import cv2; print(cv2.__version__)"# 4.5.1