c++ - 在 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 的形状不一致
解决方案
推荐阅读
- powershell - SSRS Powershell Get-RsFolderContent - [Deserialized.SSRS.ReportingService2010] 不包含名为“ListChildren”的方法
- android - 在特定时间刷新我的片段在android中不起作用
- python - 如何将两个数据框组合成pairplot?
- testing - 如何将 Jmeter 与计时器一起使用
- angular - 如何从延迟加载的模块组件路由中获取路径前缀
- javascript - 时刻js:获取期间之间的天数
- flask - 使用 redis-py 将 redis 与烧瓶应用程序集成
- react-native - React-navigation 不会在更改时以编程方式更改标题颜色
- r - 如何改变 R 列表对象中的值?
- nexus - Nexus OSS - 无法浏览存储库