7

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

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

虽然我添加了 $(CUDNN)\include;$(cudnn)\include; C/C++Linker附加包含目录,添加了CUDNN_HALF;CUDNN; C/C++>Preprocessor Definitions,并添加cudnn.lib; Linker>Input,我仍然收到此警告:

DNN 模块不是用 CUDA 后端构建的;切换到 CPU

它在 CPU 而不是 GPU 上运行,有人可以帮我解决这个问题吗?

4

2 回答 2

5

我通过使用CMake解决了它,但我必须先添加这个opencv_contrib然后使用Visual Studio重建它。确保在CMake中检查了这些WITH_CUDAWITH_CUBLASWITH_CUDNNOPENCV_DNN_CUDABUILD_opencv_world

于 2020-04-20T07:28:42.400 回答
0

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

编辑

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

于 2020-04-08T15:03:01.123 回答