# ONNX forward method returns 1\*-1\*-1 FLOAT32 in c++ opencv

**URL:** <https://forum.opencv.org/t/onnx-forward-method-returns-1-1-1-float32-in-c-opencv/1788>\
**Category:** Python\
**Tags:** dnn, mat, core\
**Created:** [February 25, 2021, 3:55am UTC](https://forum.opencv.org/t/onnx-forward-method-returns-1-1-1-float32-in-c-opencv/1788 "2021-02-25T03:55:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahand.abs](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@sahand.abs](https://forum.opencv.org/u/sahand.abs)\
**Post date:** [February 25, 2021, 3:55am UTC](https://forum.opencv.org/t/onnx-forward-method-returns-1-1-1-float32-in-c-opencv/1788/1 "2021-02-25T03:55:06Z")

</div>

System information (version)

```
OpenCV => 4.5
Operating System / Platform => Windows 64 Bit
Compiler => Visual Studio 2019

```

Detailed description

I have an ONNX model that was initially trained with pytorch.  
I have converted the model to ONNX.  
When I try to run forward in opencv python everything looks fine and the dimension of the output is (1, 3, 12, 18, 6) but when I try to run the forward method in opencv c++, the output dimension is {FLOAT32, 1 x -1 x -1}.  
I will be glad if you help me.  
Steps to reproduce

c++ code:

```
int inpWidth = 576; 
int inpHeight = 384;
Net net = readNetFromONNX(cv::String("f:\\yolov3\\testoutput\\last1.onnx"));;
cv::Mat _image = cv::imread("C:\\Users\\sahand\\Desktop\\yoloconf\\out\\val_00034.jpg");
Mat frame, blob;
_image.copyTo(frame);
blobFromImage(frame, blob, 1 / 255.0, cvSize(inpWidth, inpHeight), Scalar(0, 0, 0), true, false);
net.setInput(blob);
vector<vector<Mat>> out;
net.forward(out, {"126"});

```

python code:

```
dim = (576,384)
model_path = "f:\\yolov3\\testoutput\\last1.onnx"
inp = cv.imread("C:\\Users\\sahand\\Desktop\\yoloconf\\out\\val_00034.jpg")
model = cv.dnn.readNetFromONNX(model_path)
model.setInput(cv.dnn.blobFromImage(inp, 1/255, dim, [0,0,0], 1, crop=False))
out = model.forward("126")
```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [February 25, 2021, 10:05am UTC](https://forum.opencv.org/t/onnx-forward-method-returns-1-1-1-float32-in-c-opencv/1788/2 "2021-02-25T10:05:50Z")

</div>

looks like you got fooled by the behaviour of the `Size()` function (or the resp. rows,cols members), which only can represent 2 dimensions (W,H) - if there are more dimensions, they are set to -1 !  
try to use the `dims()` function and the `size` **member**

```auto
Mat res = outs[0]; // we have a vector
for (int d=0; d<res.dims(); d++)
    cout << res.size[d] << " "; // there is a print overload for the whole thing, too !
cout << endl;

```
