如何处理 C++ 中的 "DNN module was not built with CUDA backend; switching to CPU" 警告?

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

我正在尝试 运行 YOLOv3 Visual Studio 2019 使用 CUDA 10.2 和 Windows 10 使用 NVidia GeForce 930M 的 cuDNN v7.6.5。这是我使用的部分代码。

#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));
}

虽然我在两个 C/C++Linker,将 CUDNN_HALF;CUDNN; 添加到 C/C++>预处理器定义,并添加 cudnn.lib;Linker>Input,我仍然收到此警告:

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

它 运行 在 CPU 而不是 GPU 上运行,谁能帮我解决这个问题?

大约一周前我遇到了类似的问题,但我当时使用的是 Python 和 Tensorflow。尽管这些语言与 C++ 相比有所不同,但我确实遇到了同样的错误。为了解决这个问题,我卸载了 CUDA 10.2 并降级到 CUDA 10.1。根据我的发现,CUDA 可能存在依赖性问题,或者在您的情况下,OpenCV 尚未创建对最新版本 CUDA 的支持。

编辑

经过进一步研究,这似乎是 Opencv 而不是 CUDA 的问题。参考这个github thread,如果你用cmake安装Opencv,删除配置文件中低于7的arch bin版本,然后rebuild/reinstall Opencv。 但是,如果这不起作用,另一种选择是删除 CUDA arch bin 版本 < 5.3 并重建。

我使用 CMake 解决了它,但我必须先添加这个 opencv_contrib 然后使用 Visual Studio[= 重建它21=]。确保这些 WITH_CUDA, WITH_CUBLAS, WITH_CUDNN, OPENCV_DNN_CUDA, BUILD_opencv_worldCMake.