Output of .forward() in dnn module in c++

I get the output of yolo_net.forward() as [ 1 x 25200 x 6] . I am using Mat datatype to store the output from .forward(). Next, I need to access confidence values from 4th index position of 25,200 rows. I am not able to do that using .at function in c++. How do I do that?

Secondly, whenever I am trying to access the rows using . rows() function , I get output of my rows and columns as -1. I don’t know how to proceed.

Here is a snippet of my code:

` Mat img = imread(“/home/laddu/307.png”);
cout << "Image size: " << img.size() << endl;

cv::dnn::Net yolo_net = dnn::readNetFromONNX("/home/laddu/Downloads/best.onnx");
resize(img, img, Size(1000, 600));
Mat blob = dnn::blobFromImage(img, 1.0/255, Size(640, 640), Scalar(0, 0, 0), true, false);
net.setInput(blob);
Mat detections = net.forward()

;

vector<int> classes_ids;
vector<float> confidences;
vector<Rect> boxes;

int rows = detections.size[1];
int img_width = img.size().width;
int img_height = img.size().height;
float x_scale = (float)img_width/640;
float y_scale = (float)img_height/640;

for(int i = 0; i < rows; i++) 
{
    Mat row = detections.row(0).rowRange(i, i+1);
    float confidence = row.at<float>(4);
    if (confidence > 0.5) 
    {
        Mat classes_score = row.colRange(5, row.cols);
        Point max_loc;
        double max_val;
        minMaxLoc(classes_score, NULL, &max_val, NULL, &max_loc);
        int ind = max_loc.x;
        if (max_val > 0.5) 
        {
            classes_ids.push_back(ind);
            confidences.push_back(confidence);
            float cx = row.at<float>(0);
            float cy = row.at<float>(1);
            float w = row.at<float>(2);
            float h = row.at<float>(3);
            int x1 = static_cast<int>((cx - w / 2) * x_scale);
            int y1 = static_cast<int>((cy - h / 2) * y_scale);
            int width = static_cast<int>(w * x_scale);
            int height = static_cast<int>(h * y_scale);
            boxes.push_back(Rect(x1, y1, width, height));
        }
    }
}                                                                                                                                                                               I get an exception whenever i enter the first for loop and as soon as I start accessing the rows, exception is thrown.

so, you trained it on a single class ?

that’s normal. rows() & cols() are -1 for a multi(>2) dimensional Mat. use

cout << blob.size << endl;

(w/o braces !) to inspect

then, this is likely wrong:

use a simple:

Mat detections = net.forward();           // 3d output !
detections = detections.reshape(1,25200); // 2d, [25200x6]
....
    Mat row = detections.row(i);          // [1x6]

also note, that for a single class, ind will always be 0, you can drop the minmaxloc, and just:

 float box_confidence = row.at<float>(4);
 float cls_confidence = row.at<float>(5);
 if (box_confidence > 0.5 && cls_confidence > 0.5)
      // collect box proposal

after that, you still need to NMS filter the boxes !

then, i’m curious, how you (re-) trained that… on images / labels of a single class ?

Thanks for the output. i will try that out and get back.

For the question you asked how did I retrain it on single class was to make changes in the configuration file (.yaml file) for single class and then upload the custom dataset yaml file for the training model.