在 Visual Studio 中使用 CUDA 编译和运行 OpenCV 项目时出现问题,net.forward();

问题描述 投票:0回答:1

我在 Visual Studio 中使用 CUDA 编译和运行 OpenCV 项目时遇到问题。我在 Windows 11 中使用 Visual Studio 2022 并使用 CUDA 12.1 和 CUDNN 8.9.3 配置 OpenCV 4.8.0 。尽管遵循了设置说明,但我在编译/执行期间收到错误。我遇到的错误消息是

Unhandled exception at 0x00007FFD4697DF51 (cudnn64_8.dll) in TestTensorflow.exe: Fatal program exit requested
.,它发生在
outputs = net.forward()
;。我已经验证了 CUDA 安装并确保正确设置了必要的环境变量和路径。任何有关如何解决此问题的见解或建议将不胜感激

#include <opencv2/core.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <fstream>
#include <iostream>
#include <opencv2/core/utils/logger.hpp>
#include<opencv2/opencv.hpp>
#include<opencv2/dnn.hpp>
#include<opencv2/dnn/all_layers.hpp>

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

const float INPUT_WIDTH = 640.0;
const float INPUT_HEIGHT = 640.0;

std::vector<std::string> load_class_list()
{
    vector<string> class_names;
    ifstream ifs(string("namesPpe.txt").c_str()); 
    string line;
   
        while (getline(ifs, line))
        {
            cout<<line <<endl;
            class_names.push_back(line);
        }
        return class_names;
    // Load in all the classes from the file
}


cv::dnn::Net load_net() {
    auto net = cv::dnn::readNetFromONNX("yolov8x.onnx");

    if (cv::cuda::getCudaEnabledDeviceCount()) {
        cout << "cuda esta disponible";
        net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
        net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
        //net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
    }
    else {
        cout << "cuda NO esta disponible";
    }
    return net;
}
//************************************************************************************************************************
Mat pre_process(Mat& input_image, Net& net)
{
    //Convert to blob.
    Mat blob = blobFromImage(input_image, 1.0, Size(INPUT_WIDTH, INPUT_HEIGHT), Scalar(104.0, 117.0, 123.0), true, false);;
    net.setInput(blob);
    Mat outputs;
    outputs = net.forward();
    return outputs;
}

int main() {
    //imprimimos y mostramos las classes


    std::vector<string> class_names = load_class_list();
    cv::dnn::Net net = load_net();
    cv::Mat input_image = cv::imread("image.jpg");
    cv::Mat outputs = pre_process(input_image, net);
    return 0;
}
c++ opencv cuda
1个回答
0
投票

我发现了问题,首先 y 更改了

Mat outputs;
net.forward();
vector<Mat> outputs; net.forward(outputs, net.getUnconnectedOutLayersNames());
,所以我的函数返回
vector<Mat>
,然后控制台中出现一条消息 无法找到 zlibwapi.dll。请确保它在你的库路径中,我用过这个 论坛 无法找到 zlibwapi.dll。请确保它位于您的库路径中以解决该问题。

修正功能:

vector<Mat> pre_process(Mat& input_image, Net& net)
{
    //Convert to blob.
    Mat blob = blobFromImage(input_image, 1.0, Size(INPUT_WIDTH, INPUT_HEIGHT), Scalar(104.0, 117.0, 123.0), true, false);;
    net.setInput(blob);
    vector<Mat> outputs;
    net.forward(outputs, net.getUnconnectedOutLayersNames());
    //outputs = net.forward();
    return outputs;
}
© www.soinside.com 2019 - 2024. All rights reserved.