Output from cv::dnn::Net.forward() is multidimensional with yolov5

I have successfully exported a yolov5 model to ONNX and was able to read the model using readNetFromONNX(). I then set input using a test image and ran net.forward() which returned a Mat. I am now working on postprocessing to interpret the data contained in the returned Mat.

Most of the examples that I have found that illustrate calling forward() and interpreting the results assume that the returned Mat is 2D. In contrast, the Mat that I am getting is 3D. More specifically, the rank of the Mat is 3. The sizes of these three dimensions are 1x25200x8.

I have not been able to find any information about how to interpret such a result and was wondering if anyone has any suggestions?

Thank you.

1 Like

you can simply reshape the output to 2d like:

Mat res = output.reshape(1,25200); // [8x25200]

yolov5 has 25200 possible boxes, each row in the 2d Mat is:

cx, cy, w, h, box_prob, p1, p2, p3,  ..., pn

where p1 … pn are N class probabilities (3 in your case ?)

someone supplied example code here:

1 Like

Beautiful! Thank you so much!

1 Like