Install OpenCV 4.5.1 with CUDA 11.1 in Ubuntu 20.04 LTS
This blog emphasizes to discover how to enable OpenCV to use the Nvidia GPUs on Ubuntu system.
OpenCV is a very popular image processing open source library. However, the distribution package available are only supported for using CPU only for computation. Hence if one wishes to enforce OpenCV to use CUDA libraries such as cuDNN for OpenCV “Deep Neural Network” (DNN) it requires to build OpenCV from the source using Nvidia’s CUDA toolkit and cuDNN library.
Prerequisites
As any other lessons or thread, this blog will also expects prerequisites setup on the system before we proceed to the next steps ahead which includes building/installing OpenCV.
Here’s the detail of the system which I have used:
- GPU -> Nvidia RTX 3060 Ti
- Nvidia Driver 460.32.03
- CUDA Toolkit 11.1
- cuDNN 8.0.5
Note: There’s a very strict version dependency with the CUDA Toolkit and cuDNN library, Please refer cuDNN Support Matrix to find out the versions which should be used on your system.
let’s then get started by gathering prerequisites first.
Step 1: Preparing System
Perform below steps to make sure all the packages in the systems are up-to-date.
sudo apt update # updates local repository cache
sudo apt upgrade # updates all the packages
In case these steps are executed after fresh installation, system restart might needed.
Now that moving forward there are some generic libraries which is required to run all of such package, so that while executing, compiling or installation any expected error or exception due to the package dependencies can be prevented.
$ sudo apt install build-essential git cmake pkg-config unzip yasm checkinstall# Image libraries
$ sudo apt install libjpeg-dev libpng-dev libtiff-dev# Audio/Video Libs
$ sudo apt install libavcodec-dev libavformat-dev libswscale-dev libavresample-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev python3-dev libatlas-base-dev gfortran libgtk-3-dev libv4l-dev# OpenCV Optimization libraries
$ sudo apt-get install libatlas-base-dev gfortran# Python3 libraries
$ sudo apt-get install python3-dev python3-pip python3-testresources
$ pip3 install -U pip numpy# Installing Virtual Environment packages for python
$ sudo pip install virtualenv virtualenvwrapper
For Quick Installation, use this script cuda_opencv_prerequisites_install.sh which includes above commands just perform below action in such case:
$ wget https://github.com/gahan9/Blog/blob/master/cuda_opencv_prerequisites_install.sh
$ sudo chmod +x cuda_opencv_prerequisites_install.sh
$ bash cuda_opencv_prerequisites_install.sh
Step 2: Installing Prerequisites (Nvidia Driver, CUDA Toolkit and cuDNN)
Before Proceeding do not forget to make sure which versions you will need by referring cuDNN Support Matrix.
Visit the Nvidia Driver Download web page to find out the correct driver for your GPU and download supported version as per cuDNN Support Matrix. For any more Required Information visit Installing Nvidia Graphics Driver.
Download CUDA Toolkit from CUDA Toolkit Downloads Refer to NVIDIA CUDA Installation Guide for Linux for detailed information. Also, Note that after successful installation of CUDA Toolkit, on screen it prompts to configure paths on the ~/.bashrc file and those steps needs to be performed.
In order to download cuDNN, ensure you are registered for the NVIDIA Developer Program.
You could use NVIDIA cuDNN home page to download the latest version of the cuDNN. For Ubuntu user of 16.04 and above Alternate method using Package Manager is best way (considering existence of stable internet connection and sufficient bandwidth)
Step 3: Verifying all the installation from Step 2
Once the NVIDIA Driver is installed, if execute the command nvidia-smi, which be able to get result as shown in below snapshot
Step 4: Downloading OpenCV and OpenCV Contrib
# Specify OpenCV version
$ OPENCV_VERSION=4.5.1
$ WORKING_DIR=${PWD}
$ cd "${WORKING_DIR}"
$ echo Working Directory is "${WORKING_DIR}"
# Download openvc
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/$OPENCV_VERSION.zip# Download openvc_contrib
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/$OPENCV_VERSION.zip# Extract opencv and opencv_contrib files
$ unzip opencv.zip
$ unzip opencv_contrib.zip
$ mv opencv-${OPENCV_VERSION} opencv
$ mv opencv_contrib-${OPENCV_VERSION} opencv_contrib
Step 5: Virtual Environment using Python (virtualenvwrapper)
OpenCV also requires to have Python support, and to work isolate the installation virtual environment is very useful feature, refer more on need of Virtual environment at this link. Some may also prefer the use of conda environment (Anaconda).
Here in this example virtualenvwrapper will be used. It is assumed that you have already configured installation of virtualenvwrapper, if not then Please refer detailed step by step usage and installation read the docs.
Creating Environment
$ mkvirtualenv env1
Now switch to virtual environment created using command:
$ workon env1
Note: use of virtual environment is recommended not a mandatory one, it’s still okay to use system’s python and packages rather than having isolated executable and packages under virtual environment.
Step 6: Finding out the compute capability of GPU
So, As by now it’s clear that Nvidia has pretty complex GPU architecture, however, with every architectural change they makes the modification in compute capability which defines for the GPU.
Run below command:
$ nvcc --list-gpu-arch
It will list out supported compute capability of the GPU as below:
As in above snapshot highlighted the last entry compute_86 which means that the compute capability for the Nvidia RTX 3060 Ti is 8.6. Alternatively visit this link to find out the same.
Step 7: Building OpenCV from source downloaded in Step 4
Go to directory (We stored the path in variable WORKING_DIR as well) where OpenCV is installed.
$ cd ${WORKING_DIR}/opencv
During the build process of opencv error(s) or exception may occur and it may require to clean up all the temporarily generated output result files. Hence as to ease this process, create a seperate build directory under ${WORKING_DIR}/opencv
# make build directory and switch to it
$ mkdir build
$ cd build
Now it’s time to configure the build, but before that let’s make the variables out of the data which needs to be configured first:
# Similar to CUDA_ARCH_BIN=8.6 just calculate dynamically
$ OUTPUT=$(nvcc --list-gpu-arch | tail -1)
$ CUDA_ARCH_BIN=${OUTPUT: -2:-1}.${OUTPUT: -1}
$ echo CUDA Compute Capability is "${CUDA_ARCH_BIN}"
# Setup the below variable/paths as per your environment
$ PYTHON_EXECUTABLE=~/.virtualenvs/env1/bin/python3
$OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/env1/lib/python3.8/site-packages
$ OPENCV_EXTRA_MODULES_PATH=${WORKING_DIR}/opencv_contrib/modules
Run cmake configuration script from the build folder:
Before Proceeding, Please have look at how to install gcc-7 or lower in Ubuntu 20.04
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_C_COMPILER=/usr/bin/gcc-6 \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D WITH_TBB=ON \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=OFF \
-D BUILD_SHARED_LIBS=OFF \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_nppicom_LIBRARY=true\
-D HAVE_opencv_python3=ON \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D OPENCV_PYTHON3_INSTALL_PATH=${OPENCV_PYTHON3_INSTALL_PATH} \
-D OPENCV_EXTRA_MODULES_PATH="${OPENCV_EXTRA_MODULES_PATH}" \
-D PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE} \
-D CUDA_ARCH_BIN="${CUDA_ARCH_BIN}" \
-D BUILD_TIFF=ON \
-D BUILD_EXAMPLES=ON ..
All these flags!! I get it, please refer to Building OpenCV for more details on these flags and why it’s value is supposed to be set.
Observe below output at the end of the cmake configuration as highlighted in below snapshot:
If somehow any of this is NO instead of YES or cmake command is failing then, go through again from Step 4 or explore some basic errors and try to fix it, because it could be some issue on the system configuration as well.
Now build all the cmake configured list using below command:
# Go to build directory
cd "${WORKING_DIR}"/opencv/build
# Run the job (configured from cmake)
$ make -j4
Note: if there are more than 4 cpu cores on the system then it can take as many jobs (max 4 jobs -j4) to make flag to accelerate the process i.e. make -j12 if there are 12 CPU cores
Here’s comes another crucial stage, this is the command where if anything can goes sideways if anything is improperly configured in earlier steps or skipped. Ensure that it does executes successfully before executing the next command. Some debugging may needed -> perform make clean operation over and fix the cause of the error. If there’s any issue, dig enough to find the root cause of such problem in order to complete the installation as debugging with failures of make command or process is not the scope of this tutorial blog.
Install the library component using:
$ sudo make install
$ sudo ldconfig
If the opencv built with virtual environment python, to have it with system python interpreter rather then rebuilding, just execute below command:
# Link with System Interpreter
$ PY_VERSION=$(python3 --version)
$ PY3_VERSION=${PY_VERSION: -5:-2}
$ sudo cp -r cd ${OPENCV_PYTHON3_INSTALL_PATH}/cv2 /usr/local/lib/python"${PY3_VERSION}"/dist-packages
Now edit the config file for the system python interpreter:
To open the file execute command:
$ sudo nano /usr/local/lib/python"${PY3_VERSION}"/dist-packages/cv2/config-"${PY3_VERSION}".py
And add/update below lines
PYTHON_EXTENSIONS_PATHS = [
os.path.join('/usr/local/lib/python3.8/dist-packages/cv2', 'python-3.8')
] + PYTHON_EXTENSIONS_PATHS
References
- https://virtualenvwrapper.readthedocs.io/en/latest/
- https://www.pyimagesearch.com/2020/02/03/how-to-use-opencvs-dnn-module-with-nvidia-gpus-cuda-and-cudnn/
- https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
- https://docs.opencv.org/master/d2/de6/tutorial_py_setup_in_ubuntu.html
- https://docs.opencv.org/4.5.1/d6/d15/tutorial_building_tegra_cuda.html
- https://gist.github.com/raulqf/f42c718a658cddc16f9df07ecc627be7
- https://opencv.org/releases/
- https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html
- https://docs.nvidia.com/deeplearning/cudnn/support-matrix/index.html
- https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html