[ShapeInferenceError] Mismatch between number of source and target dimensions. Source=1 Target=0
Pytorch轉(zhuǎn)ONNX模型正常,但使用onnxruntime加載onnx模型,會(huì)輸出錯(cuò)誤:
onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Exception during loading: /onnxruntime_src/onnxruntime/core/graph/function.cc:420 onnxruntime::FunctionImpl::FunctionImpl(const onnxruntime::Graph&, const NodeIndex&, const onnx::FunctionProto&, const onnxruntime::logging::Logger&) status.IsOK() was false. Resolve subgraph failed:Node (0x2b97890_109) Op (Loop) [TypeInferenceError] Graph attribute inferencing failed: Node:0x2b97890_109 Output:cond [ShapeInferenceError] Mismatch between number of source and target dimensions. Source=1 Target=0
相關(guān)issues:https://github.com/microsoft/onnxruntime/issues/1164
從報(bào)錯(cuò)的描述,大致可以定位是輸出的維度有問題:于是我詳細(xì)Debug一下模型輸出的'boxes', 'scores'的維度變化:
scores輸出維度是(1,13375,2),對(duì)應(yīng)(batch_size,num_anchors,num_class)
boxes輸出維度是(13375,4),沒有batch_size這個(gè)維度,這是因?yàn)槲疫M(jìn)行boxes.data.squeeze(0)處理,不小心把batch_size去掉了
于是我修改源代碼,保持'boxes', 'scores'都含有batch_size這個(gè)維度,變成
scores輸出維度是(1,13375,2),對(duì)應(yīng)(batch_size,num_anchors,num_class)
boxes輸出維度是(1,13375,4),對(duì)應(yīng)(batch_size,num_anchors,(x,y,cx,cy))
done,重新生成ONNX模型,并使用onnxruntime加載模型,就沒有錯(cuò)誤了
結(jié)論:轉(zhuǎn)換ONNX模型時(shí),輸入輸出盡量保留batch_size這個(gè)維度,盡管batch_size這個(gè)維度經(jīng)常為1
總結(jié)
以上是生活随笔為你收集整理的[ShapeInferenceError] Mismatch between number of source and target dimensions. Source=1 Target=0的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: COCO数据格式说明
- 下一篇: 图像抠图Image Matting算法调