简体   繁体   English

错误:(-215:断言失败) (int)_numAxes == inputs[0].size() in function 'getMemoryShapes'

[英]error: (-215:Assertion failed) (int)_numAxes == inputs[0].size() in function 'getMemoryShapes'

im trying to use opencv to do face recognition using fac.net512.我正在尝试使用 opencv 通过 fac.net512 进行人脸识别。 i converted the model to onnx format using tf2onnx.我使用 tf2onnx 将 model 转换为 onnx 格式。 i know that the input of the model should be an image like:(160,160,3).我知道 model 的输入应该是这样的图像:(160,160,3)。 so i tried doing this using this script:所以我尝试使用此脚本执行此操作:

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

}

after converting the image from (160,160) to (160,160,3) i still get this error:将图像从 (160,160) 转换为 (160,160,3) 后,我仍然收到此错误:

error: (-215:Assertion failed) (int)_numAxes == inputs[0].size() in function 'getMemoryShapes'错误:(-215:断言失败) (int)_numAxes == inputs[0].size() in function 'getMemoryShapes'

full code:完整代码:

#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?有没有其他方法可以更改尺寸以适合 model 输入?

thanks in advance.提前致谢。

using.netron i was able to visualize the input of the model:使用.netron 我能够可视化 model 的输入: 在此处输入图像描述

the origin of the problem was from the conversion of the model i just had to change this:问题的根源是 model 的转换,我只需要更改它:

model_proto, _ = tf2onnx.convert.from_keras(model, output_path='fac.net512.onnx')

to this:对此:

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

i checked again in.netron and the result were:我再次检查了 in.netron,结果是:

在此处输入图像描述

i can now use the model from opencv using cv::dnn::BlobFromImage()我现在可以使用 cv::dnn::BlobFromImage() 从 opencv 使用 model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM