How to deal with "DNN module was not built with CUDA backend; switching to CPU" warning in C++?
Asked Answered
K

2

7

I am trying to run YOLOv3 on Visual Studio 2019 using CUDA 10.2 with cuDNN v7.6.5 on Windows 10 using NVidia GeForce 930M. Here is part of the code I used.

#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;
using namespace dnn;
using namespace std;

int main()
{
    // Load names of classes
    string classesFile = "coco.names";
    ifstream ifs(classesFile.c_str());
    string line;
    while (getline(ifs, line)) classes.push_back(line);

    // Give the configuration and weight files for the model
    String modelConfiguration = "yolovs.cfg";
    String modelWeights = "yolov3.weights";

    // Load the network
    Net net = readNetFromDarknet(modelConfiguration, modelWeights);
    net.setPreferableBackend(DNN_BACKEND_CUDA);
    net.setPreferableTarget(DNN_TARGET_CUDA);

    // Open the video file
    inputFile = "vid.mp4";
    cap.open(inputFile);

    // Get frame from the video
    cap >> frame;

    // Create a 4D blob from a frame
    blobFromImage(frame, blob, 1 / 255.0, Size(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);

    // Sets the input to the network
    net.setInput(blob);

    // Runs the forward pass to get output of the output layers
    vector<Mat> outs;
    net.forward(outs, getOutputsNames(net));
}

Although I add $(CUDNN)\include;$(cudnn)\include; to Additional Include Directories in both C/C++ and Linker, added CUDNN_HALF;CUDNN; to C/C++>Preprocessor Definitions, and added cudnn.lib; to Linker>Input, I still get this warning:

DNN module was not built with CUDA backend; switching to CPU

and it runs on CPU instead of GPU, can anyone help me with this problem?

Kimura answered 8/4, 2020 at 8:3 Comment(1)
This question is about building an OpenCV module, not CUDA and not CUDNN. That is why I removed the tags. There is no CUDA or CUDNN related programming question hereOracle
K
5

I solved it by using CMake, but I had first to add this opencv_contrib then rebuilding it using Visual Studio. Make sure that these WITH_CUDA, WITH_CUBLAS, WITH_CUDNN, OPENCV_DNN_CUDA, BUILD_opencv_world are checked in CMake.

Kimura answered 20/4, 2020 at 7:28 Comment(6)
DNN module was not built with Cuda backend switching to CPU.I was trying it on pyimage yolo object detection.Maturate
@rakshitks Try to check these parameters in CMake " WITH_CUDA, WITH_CUBLAS, WITH_CUDNN, OPENCV_DNN_CUDA, BUILD_opencv_world"Kimura
ya everything is there but still when i run the file same problem comes.Maturate
@rakshitks What is the specifications of your machine? and what is the operating system? and which compiler do you use?Kimura
i7 processor 8Gb ram 2Gb graphics cardMaturate
@rakshitks even I tried with pyimage search it didnt worked for me either. It throws error of missing libcodec.so... Please update if you have any resolutionPhonic
B
0

I had a similar issue happen to me about a week ago, but I was using Python and Tensorflow. Although the languages were different compared to C++, I did get the same error. To fix this, I uninstalled CUDA 10.2 and downgraded to CUDA 10.1. From what I have found, there might be a dependency issue with CUDA, or in your case, OpenCV hasn't created support yet for the latest version of CUDA.

EDIT

After some further research it seems to be an issue with Opencv rather than CUDA. Referencing this github thread, if you installed Opencv with cmake, remove the arch bin version below 7 on the config file, then rebuild/reinstall Opencv. However, if that doesn't work, another option would be to remove CUDA arch bin version < 5.3 and rebuild.

Brash answered 8/4, 2020 at 15:3 Comment(5)
You completely uninstalled CUDA 10.2, using their GUI, and restarted? Before you installed CUDA 10.1?Brash
Yes, no differenceKimura
@AbdelAzizAbdelLatef I made some edits, hope they help.Brash
I am using Windows and installed OpenCV without cmake, so this doesn't work for me.Kimura
please, how i can remove the arch bin version below 7 on the config file ?Epinephrine

© 2022 - 2024 — McMap. All rights reserved.