OpenCV(4.5.5) 错误:cv::dnn::dnn4_v20211220::Net::forward C++ 中断言失败 (!empty())

发布于 2025-01-16 12:28:52 字数 2514 浏览 0 评论 0原文

我最近创建了自己的 TensorFlow 对象检测模型。当我将模型加载到 OpenCV 的 DNN 中时,它在 net.forward() 处出现错误。我不知道是模型训练不当还是我设置的 DNN 模型不正确。

我尝试过其他模型,例如咖啡模型,这似乎有效。

代码 :

#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/dnn/all_layers.hpp>
 
using namespace std;
using namespace cv;
using namespace dnn;
 
 
int main(int, char**) {

    string file_path = "C:/Users/Daniel/source/repos/AR PROJECT/AR PROJECT/TF/";
    vector<string> class_names;
    ifstream ifs(string(file_path + "class.txt").c_str());
    string line;

    // Load in all the classes from the file
    while (getline(ifs, line))
    {   
        cout << line << endl;
        class_names.push_back(line);
    } 
    

    // Read in the neural network from the files
    auto net = readNetFromTensorflow("F:/models-master/research/object_detection/output_inference_graph/frozen_inference_graph.pb",
        "F:/models-master/research/object_detection/training/object-detection.pbtxt");
 

    // Open up the webcam
    VideoCapture cap(1);
 

    // Run on either CPU or GPU
    net.setPreferableBackend(DNN_BACKEND_CUDA);
    net.setPreferableTarget(DNN_TARGET_CUDA_FP16);


    // Set a min confidence score for the detections
    float min_confidence_score = 0.5;


    // Loop running as long as webcam is open and "q" is not pressed
    while (cap.isOpened()) {

        // Load in an image
        Mat image;
        bool isSuccess = cap.read(image);

        // Check if image is loaded in correctly
        if (!isSuccess){
            cout << "Could not load the image!" << endl;
            break;
        }
        
        int image_height = image.cols;
        int image_width = image.rows;

        auto start = getTickCount();

        // Create a blob from the image
        Mat blob = blobFromImage(image, 1.0, Size(300, 300));

        
        // Set the blob to be input to the neural network
        net.setInput(blob);

        // Forward pass of the blob through the neural network to get the predictions
        Mat output = net.forward();

        auto end = getTickCount();

        // Matrix with all the detections
        Mat results(output.size[2], output.size[3], CV_32F, output.ptr<float>());
        
        imshow("image", image);


        int k = waitKey(10);
        if (k == 113){
            break;
        }
    }
    
    cap.release();
    destroyAllWindows();
}

I recently created my own TensorFlow Object Detection Model. When I load the model into OpenCV's DNN it has an error at net.forward(). I don't know if the model is improperly trained or if I setup the DNN model incorrectly.

I have tried other models such as a caffe model and that seemed to work.

Code :

#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/dnn/all_layers.hpp>
 
using namespace std;
using namespace cv;
using namespace dnn;
 
 
int main(int, char**) {

    string file_path = "C:/Users/Daniel/source/repos/AR PROJECT/AR PROJECT/TF/";
    vector<string> class_names;
    ifstream ifs(string(file_path + "class.txt").c_str());
    string line;

    // Load in all the classes from the file
    while (getline(ifs, line))
    {   
        cout << line << endl;
        class_names.push_back(line);
    } 
    

    // Read in the neural network from the files
    auto net = readNetFromTensorflow("F:/models-master/research/object_detection/output_inference_graph/frozen_inference_graph.pb",
        "F:/models-master/research/object_detection/training/object-detection.pbtxt");
 

    // Open up the webcam
    VideoCapture cap(1);
 

    // Run on either CPU or GPU
    net.setPreferableBackend(DNN_BACKEND_CUDA);
    net.setPreferableTarget(DNN_TARGET_CUDA_FP16);


    // Set a min confidence score for the detections
    float min_confidence_score = 0.5;


    // Loop running as long as webcam is open and "q" is not pressed
    while (cap.isOpened()) {

        // Load in an image
        Mat image;
        bool isSuccess = cap.read(image);

        // Check if image is loaded in correctly
        if (!isSuccess){
            cout << "Could not load the image!" << endl;
            break;
        }
        
        int image_height = image.cols;
        int image_width = image.rows;

        auto start = getTickCount();

        // Create a blob from the image
        Mat blob = blobFromImage(image, 1.0, Size(300, 300));

        
        // Set the blob to be input to the neural network
        net.setInput(blob);

        // Forward pass of the blob through the neural network to get the predictions
        Mat output = net.forward();

        auto end = getTickCount();

        // Matrix with all the detections
        Mat results(output.size[2], output.size[3], CV_32F, output.ptr<float>());
        
        imshow("image", image);


        int k = waitKey(10);
        if (k == 113){
            break;
        }
    }
    
    cap.release();
    destroyAllWindows();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2025-01-23 12:28:52

我通过将tensorflow SavedModel 转换为.onnx 使其工作。

这对我有用:

pip install tf2onnx

python -m tf2onnx.convert --saved-model tensorflow-model-path --opset 11 --output model.onnx

--saved-model:是保存模型的文件夹而不是 .pb 文件。

--opset 参数是可选的,但是对于我使用 opencv 4.6 来说,版本 11 工作正常,而版本 15> 则工作正常。没有。

在opencv-python中:

model = cv.dnn.readNetFromONNX("model.onnx")

https://github.com/onnx/tensorflow-onnx

I got it working by converting tensorflow SavedModel to .onnx.

That is what worked for me:

pip install tf2onnx

python -m tf2onnx.convert --saved-model tensorflow-model-path --opset 11 --output model.onnx

--saved-model: is the folder of saved model not the .pb file.

--opset parameter is optional, however for me with opencv 4.6., version 11 worked fine, while version 15> did not.

In opencv-python:

model = cv.dnn.readNetFromONNX("model.onnx")

https://github.com/onnx/tensorflow-onnx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文