'OpenCV(4.5.5) Error: Assertion failed (!empty()) in cv::dnn::dnn4_v20211220::Net::forward C++

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source