# Model input dimension

**URL:** <https://forum.opencv.org/t/model-input-dimension/8650>\
**Category:** C++\
**Tags:** dnn\
**Created:** [May 6, 2022, 1:43am UTC](https://forum.opencv.org/t/model-input-dimension/8650 "2022-05-06T01:43:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdessamad\_ABID](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/abdessamad_abid/32/5019_2.png) [@Abdessamad\_ABID](https://forum.opencv.org/u/Abdessamad_ABID)\
**Post date:** [May 6, 2022, 1:43am UTC](https://forum.opencv.org/t/model-input-dimension/8650/1 "2022-05-06T01:43:58Z")

</div>

im trying to use opencv to do face recognition using facenet512. i converted the model to onnx format using tf2onnx. i know that the input of the model should be an image like :(160,160,3). so i tried doing this using this script :

```auto
void convertDimention(cv::Mat input, cv::Mat &output)
{
    vector<cv::Mat> channels(3);
    cv::split(input, channels);

    int size[3] = { 112, 112, 3 };
    cv::Mat M(3, size, CV_32F, cv::Scalar(0));

    for (int i = 0; i < size[0]; i++) {
      for (int j = 0; j < size[1]; j++) {
        for (int k = 0; k < size[2]; k++) {
          M.at<float>(i,j,k) = channels[k].at<float>(i,j)/255;
        }
      }
    }
    M.copyTo(output);

}

```

after converting the image from (160,160) to (160,160,3) i still get this error :

> error: (-215:Assertion failed) (int)\_numAxes == inputs[0].size() in function ‘getMemoryShapes’

full code :

```auto
#include <iostream>

#include <opencv2/dnn.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <vector>

using namespace std;

void convertDimention(cv::Mat input, cv::Mat &output)
{
    vector<cv::Mat> channels(3);
    cv::split(input, channels);

    int size[3] = { 160, 160, 3 };
    cv::Mat M(3, size, CV_32F, cv::Scalar(0));

    for (int i = 0; i < size[0]; i++) {
      for (int j = 0; j < size[1]; j++) {
        for (int k = 0; k < size[2]; k++) {
          M.at<float>(i,j,k) = channels[k].at<float>(i,j)/255;
        }
      }
    }
    M.copyTo(output);

}

int main()
{ 
    cv::Mat input,input2, output;

    input = cv::imread("image.png");
    cv::resize(input,input, cv::Size(160,160));
    convertDimention(input,input2);

    cv::dnn::Net net = cv::dnn::readNetFromONNX("facenet512.onnx");
    net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
    net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
    cout << input.size << endl;
    cout << input2.size << endl;

    net.setInput(input2);
    output = net.forward();

}

```

I know that i’m doing this in the wrong way(since i’m new to this). Is there any other way to change the dimensions so that it fits the model input ?

thanks in advance. 😊

---

<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:** [May 6, 2022, 5:48am UTC](https://forum.opencv.org/t/model-input-dimension/8650/2 "2022-05-06T05:48:00Z")

</div>

> [@Abdessamad\_ABID](#):
>
> i know that the input of the model should be an image like :(160,160,3).

please dont write loops like this. you also only copy a [112,112] slice

opencv’s dnn usually expects 4d blobs with NCHW order,  
use [dnn::blobFromImage()](https://docs.opencv.org/4.x/d6/d0f/group__dnn.html#ga29f34df9376379a603acd8df581ac8d7) to convert it

can you give us a link to the tf model / code, so we can estimate, what it _really_ wants here ?

[edit]  
in the end, you probably need something like this:

```auto
    cv::dnn::Net net = cv::dnn::readNetFromONNX("facenet512.onnx");

    cv::Mat input = cv::imread("image.png");
    cv::Mat blob = cv::dnn::blobFromImage(input, 1.0, cv::Size(160,160));

    net.setInput(blob);
    output = net.forward().clone();
    // if you want to save the output for comparisons later, 
    // you must `clone()` it, else it gets overwritten by the next forward() pass !!!

```

---

<div class="post-metadata">

**Author:** ![Abdessamad\_ABID](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/abdessamad_abid/32/5019_2.png) [@Abdessamad\_ABID](https://forum.opencv.org/u/Abdessamad_ABID)\
**Post date:** [May 6, 2022, 6:35pm UTC](https://forum.opencv.org/t/model-input-dimension/8650/3 "2022-05-06T18:35:49Z")

</div>

i found the weights of the model in this [link](https://github.com/serengil/deepface/blob/master/deepface/basemodels/Facenet512.py)  
I tried using dnn::blobFromImage() but it gave me the same output error.  
facenet512 is a keras model based on InceptionResNetV2 so the input layer is something like this : `inputs = tensorflow.keras.layers.Input(shape=(160, 160, 3))`

---

<div class="post-metadata">

**Author:** ![andife](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@andife](https://forum.opencv.org/u/andife)\
**Post date:** [May 7, 2022, 1:15pm UTC](https://forum.opencv.org/t/model-input-dimension/8650/4 "2022-05-07T13:15:57Z")

</div>

Hi, I’m also trying to work with opencv and onnx. For the input layer, I used the flag " --inputs-as-nchw" of tf2onnx. Then the dataset would match the format produced by blobFromImage (nchw-format). I wonder if the input layer in really 3d or should be 4d? (You could check with [GitHub - lutzroeder/netron: Visualizer for neural network, deep learning, and machine learning models](https://github.com/lutzroeder/Netron) the input and output shapes of your model)

---

<div class="post-metadata">

**Author:** ![Abdessamad\_ABID](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/abdessamad_abid/32/5019_2.png) [@Abdessamad\_ABID](https://forum.opencv.org/u/Abdessamad_ABID)\
**Post date:** [May 7, 2022, 9:19pm UTC](https://forum.opencv.org/t/model-input-dimension/8650/6 "2022-05-07T21:19:55Z")

</div>

hi again, looks like there is an additionnal dimension when i use netron the output is:  
 ![screen](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/b/bbd36f304b81ed1ea697be745b92149caeef4a91.png)

as adife said using “inputs-as-nchw” solved the issue and netron outputs this now:  
 ![screen2](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/5db037347f0cd2fdb9d2c42a1fd2d57718ed1e60.png)

i just had to replace this :

```auto
model_proto, _ = tf2onnx.convert.from_keras(model, output_path='facenet512.onnx')

```

to this

```auto
nchw_inputs_list = [model.inputs[0].name]
model_proto, _ = tf2onnx.convert.from_keras(model, output_path='facenet512.onnx',inputs_as_nchw=nchw_inputs_list)

```

thank you guys for the help 🤩
