首页 > 解决方案 > 在 opencv dnn 中加载 onnx 模型

问题描述

使用给定代码在 C++ 中加载 onnx 模型时出现以下错误。我究竟做错了什么?请帮我解决这个问题。(opencv 版本:C++ 和 Python 中的 4.5.3)

抛出'cv :: Exception'实例后调用终止
what():OpenCV(4.1.0)/home/Downloads/opencv-4.1.0/modules/dnn/src/dnn.cpp:524:错误:(- 2:未指定的错误)无法在函数“getLayerInstance”中创建“Squeeze”类型的层“StatefulPartitionedCall/inception_resnet_v1/AvgPool/Mean_Squeeze__1416:0”

中止(核心转储)

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

using namespace std;
using namespace cv;
using namespace dnn;

int main()
{
    // load the neural network model
    cv::dnn::Net net = cv::dnn::readNetFromONNX("model.onnx");
}

我使用以下转换转换获得了 model.onnx:python3 -m tf2onnx.convert --saved-model /savedmodel/model --output model.onnx

在 python 中使用以下代码,我可以加载模型:

import onnx
onnx_model = onnx.load('model.onnx')
onnx.checker.check_model(onnx_model)

但是在python中使用cv2.dnn,我得到以下错误

#net = cv2.dnn.readNet('model.onnx')
net = cv2.dnn.readNetFromONNX('model.onnx')

[错误:0]全局/tmp/pip-req-build-tjxnaiom/opencv/modules/dnn/src/onnx/onnx_importer.cpp(2125)handleNode DNN/ONNX:处理节点期间出现3个输入和1个输出的错误:[ Concat]:(StatefulPartitionedCall/inception_resnet_v1/Mixed_6a/concat:0) Traceback(最近一次调用最后):文件“./use_onnx_in_cv2.py”,第 36 行,在 net = cv2.dnn.readNetFromONNX('model.onnx') cv2 .error:OpenCV(4.5.3)/tmp/pip-req-build-tjxnaiom/opencv/modules/dnn/src/onnx/onnx_importer.cpp:2146:错误:(-2:未指定的错误)函数'handleNode'

节点 [Concat]:(StatefulPartitionedCall/inception_resnet_v1/Mixed_6a/concat:0) 解析错误:OpenCV(4.5.3) /tmp/pip-req-build-tjxnaiom/opencv/modules/dnn/src/layers/concat_layer.cpp: 102:错误:(-201:输入数组的大小不正确)函数“getMemoryShapes”中 ConcatLayer 的形状不一致

标签: c++python-3.xopencvonnxtf2onnx

解决方案


推荐阅读