-2 opencv config_error ai_generated partial

cv2.error: OpenCV(4.7.0) /modules/dnn/src/dnn.cpp: error: (-2:Unspecified error) DNN module was not built with CUDA backend; switched to CPU

ID: opencv/dnn-cuda-backend-missing

Also available as: JSON · Markdown · 中文
75%Fix Rate
90%Confidence
1Evidence
2023-08-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.0 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

The OpenCV installation does not include CUDA support for DNN, so GPU acceleration is unavailable and falls back to CPU.

generic

中文

OpenCV 安装未包含 DNN 的 CUDA 支持,因此 GPU 加速不可用,回退到 CPU。

Official Documentation

https://docs.opencv.org/4.x/d2/d58/tutorial_dnn_googlenet.html

Workarounds

  1. 80% success Rebuild OpenCV from source with CUDA enabled: set -DWITH_CUDA=ON and -DWITH_CUDNN=ON in CMake, and ensure CUDA toolkit and cuDNN are installed.
    Rebuild OpenCV from source with CUDA enabled: set -DWITH_CUDA=ON and -DWITH_CUDNN=ON in CMake, and ensure CUDA toolkit and cuDNN are installed.
  2. 70% success Use a pre-built OpenCV package that includes CUDA support, such as from conda-forge (opencv with cuda variant) or NVIDIA's official Docker images.
    Use a pre-built OpenCV package that includes CUDA support, such as from conda-forge (opencv with cuda variant) or NVIDIA's official Docker images.

中文步骤

  1. Rebuild OpenCV from source with CUDA enabled: set -DWITH_CUDA=ON and -DWITH_CUDNN=ON in CMake, and ensure CUDA toolkit and cuDNN are installed.
  2. Use a pre-built OpenCV package that includes CUDA support, such as from conda-forge (opencv with cuda variant) or NVIDIA's official Docker images.

Dead Ends

Common approaches that don't work:

  1. Set environment variable OPENCV_DNN_CUDA=1 95% fail

    This environment variable does not exist; CUDA support must be compiled into OpenCV.

  2. Install CUDA toolkit after OpenCV installation 85% fail

    CUDA toolkit must be present at build time; installing it after does not add CUDA support to an existing OpenCV build.

  3. Use cv2.dnn.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) to force CUDA 90% fail

    If the DNN module was not built with CUDA, this call will throw the same error; it does not enable CUDA at runtime.