DNN module was not built with CUDA backend

  • OpenCV => 4.5.2
  • Operating System / Platform => Ubuntu 20.04
  • Compiler => Visual Studio Code (cmake)
  • Cuda Version => 11.2
  • GPU => GTX 1660 TI
  • CPU => Ryzen 5 2600
  • GPU ARCH => 7.5
  • Cudnn Version => 8.2.1

I have built OpenCV over 10 times, and even went as far as to install the OS 5 times (This was to get a new fresh install of Linux to erase any mistakes I made), I have been on this problem for over a week, followed three installation guides on how to get OpenCV with Cuda support on DNN.
Here are the step by step guilds I followed.

I have tried cuda 10.1, 10.2, 11, 11.2 and 11.4 to no avail. I have tried different gpu arch and even build opencv with ever single arch at once yesterday, but I still get the same error after all of this.
Here is the error I am trying to solve:

[ WARN:0] global …/modules/dnn/src/dnn.cpp (1363) setUpNet DNN module was not built with CUDA backend; switching to CPU

Solved!
Previous cmake command:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON -D WITH_CUBLAS=ON -D WITH_CUDA=ON -D CUDA_ARCH_BIN=7.5 -D BUILD_opencv_cudacodec=OFF -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -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 OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python -D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib-4.5.2/modules -D INSTALL_PYTHON_EXAMPLES=OFF -D INSTALL_C_EXAMPLES=OFF -D BUILD_EXAMPLES=OFF -D OPENCV_GENERATE_PKGCONFIG=ON -D OpenCV_DIR=~/Downloads/opencv-4.5.2/build ..

The result:
pkg-config --modversion opencv4
4.2.0

pkg-config --modversion opencv
4.5.2

The one that I used to fix my issue:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON -D WITH_CUBLAS=ON -D WITH_CUDA=ON -D CUDA_ARCH_BIN=7.5 -D BUILD_opencv_cudacodec=OFF -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -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=opencv4.pc -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_PYTHON3_INSTALL_PATH=~/.virtualenvs/cv/lib/python3.8/site-packages -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python -D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib-4.5.2/modules -D INSTALL_PYTHON_EXAMPLES=OFF -D INSTALL_C_EXAMPLES=OFF -D BUILD_EXAMPLES=OFF -D OPENCV_GENERATE_PKGCONFIG=ON -D OpenCV_DIR=~/Downloads/opencv-4.5.2/build ..

The result:
pkg-config --modversion opencv4
4.5.2

pkg-config --modversion opencv
4.5.2

DNN with Cuda now works. Thanks Devs for making OpenCV!

i closed your other question about it, please spare us duplicates in the future, thank you.

1 Like

If I may ask, I am having the same problem of “DNN module not built with CUDA backend”.

My steps:

  1. cmake - worked (no error)

…:~$ cd tk_ws/opencv-4.5.4/build/
…/tk_ws/opencv-4.5.4/build$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON -D WITH_CUBLAS=ON -D WITH_CUDA=ON -D CUDA_ARCH_BIN=8.7 -D BUILD_opencv_cudacodec=OFF -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -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=opencv4.pc -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_PYTHON3_INSTALL_PATH=/usr/lib/python3.8/dist-packages -D PYTHON3_EXECUTABLE=/usr/bin/python3 -D OPENCV_EXTRA_MODULES_PATH=/home/…/tk_ws/opencv_contrib-4.5.4/modules -D INSTALL_PYTHON_EXAMPLES=OFF -D INSTALL_C_EXAMPLES=OFF -D BUILD_EXAMPLES=OFF -D BUILD_OPENCV_DNN=ON -D BUILD_OPENCV_WORLD=ON -D CUDNN_VERSION=8.6.0 -D CUDNN_LIBRARY=/usr/lib/aarch64-linux-gnu/libcudnn.so -D CUDNN_INCLUDE_DIR=/usr/include -D PYTHON3_INCLUDE_DIR=/usr/include/python3.8 -D PYTHON3_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.8.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages -D OpenCV_DIR=/home/…/tk_ws/opencv-4.5.4/build …

  1. VERBOSE=1 make -j12 ( no error)
  2. sudo make install (no error)
  3. python3
    >>>import cv2
    >>>cv2.version
    ‘4.5.4’
  4. pkg-config --modversion opencv4
    4.5.4
  5. pkg-config --modversion opencv
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc’
    to the PKG_CONFIG_PATH environment variable
    No package ‘opencv’ found

I am not sure how to get this pkg-config --modversion opencv result as 4.5.4.

Requesting for help.

Regards.