解决ONNX报错问题的方法和技巧

onnxruntime报错:

onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument:
[ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Failed to load model with
error: Unknown model file format version.

安装了onnxonnxruntime之后还是报错,upgrade到最新版本还是报错。
发现是因为之前导出的.onnx模型和现在的版本不匹配,所以需要重新export一下,结果又报下面的错:

ONNX: export failure ❌ 0.0s: Unsupported ONNX opset version: 17

这个是因为opset的版本问题,把export时默认的17改成11就可以了。

python3 export.py --include onnx --weight /root/yolov5/runs/train-cls/exp4/weights/best.pt --opset 11

另外记得export的时候要指定imgsz,不然就是默认的640,之后用的时候就要匹配。

我没有原始pt文件,我只有onnx文件,所以没办法重新export,运行报错:

onnxruntime.capi.onnxruntime_pybind11_state.InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : This is an invalid model. Error in Node:Identity_0 : No Op registered for Identity with domain_version of 13

可以通过下面的代码查看支持的opset version

python
import onnxruntime
print("Supported onnxruntime version: ", onnxruntime.__version__)
print("Supported Opset versions: ", onnxruntime.get_available_providers())

显示支持但就是不行,就卸载重装,可能是安装位置之类的问题

pip uninstall onnxruntime
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple onnxruntime

安装完显示只支持cpu,不支持cuda,但是onnxruntime-gpu明明装了的,一样的,卸载重装:

pip uninstall onnxruntime-gpu
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple onnxruntime-gpu
物联沃分享整理
物联沃-IOTWORD物联网 » 解决ONNX报错问题的方法和技巧

发表评论