python - 从 ONNX 运行超分辨率模型时出错
问题描述
我的 ONNX 版本是 1.5.0,onnxruntime 是 1.4.0。Onnxruntime 是使用 pip 安装的。Pytorch 版本是 1.6.0
错误在ort_session = onnxruntime.InferenceSession('/home/itc/pytorch/sub_pixel_cnn_2016/model/super-resolution-10.onnx')
错误在于加载 onnx 模型。
Traceback (most recent call last):
File "test.py", line 73, in <module>
ort_session = onnxruntime.InferenceSession('/home/itc/pytorch/sub_pixel_cnn_2016/model/super-resolution-10.onnx')
File "/home/itc/pytorch/lib/python3.7/site-packages/onnxruntime/capi/session.py", line 158, in __init__
self._load_model(providers or [])
File "/home/itc/pytorch/lib/python3.7/site-packages/onnxruntime/capi/session.py", line 166, in _load_model
True)
RuntimeError: /onnxruntime_src/onnxruntime/core/session/inference_session.cc:238 onnxruntime::InferenceSession::InferenceSession(const onnxruntime::SessionOptions&, const onnxruntime::Environment&, const string&) status.IsOK() was false. Given model could not be parsed while creating inference session. Error message: Protobuf parsing failed.
我该如何解决这个错误?
解决方案
super-resolution-10.onnx
对我来说似乎加载正常。我从https://github.com/onnx/models/blob/master/vision/super_resolution/sub_pixel_cnn_2016/model/super-resolution-10.onnx下载了文件
$ pip install onnxruntime
...
Successfully installed onnxruntime-1.5.1
我也试过pip install onnxruntime==1.4.0
- 也很好用。
然后尝试加载它(有一堆警告,但加载正常):
In [1]: import onnxruntime
In [2]: onnxruntime.InferenceSession("super-resolution-10.onnx")
2020-10-12 23:25:23.486256465 [W:onnxruntime:, graph.cc:1030 Graph] Initializer conv1.bias appears in graph inputs and will not be treated as constant value/weight. This may prevent some of the graph optimizations, like const folding. Move it out of graph inputs if there is no need to override it, by either re-generating the model with latest exporter/converter or with the tool onnxruntime/tools/python/remove_initializer_from_input.py.
2020-10-12 23:25:23.486293664 [W:onnxruntime:, graph.cc:1030 Graph] Initializer conv1.weight appears in graph inputs and will not be treated as constant value/weight. This may prevent some of the graph optimizations, like const folding. Move it out of graph inputs if there is no need to override it, by either re-generating the model with latest exporter/converter or with the tool onnxruntime/tools/python/remove_initializer_from_input.py.
2020-10-12 23:25:23.486308563 [W:onnxruntime:, graph.cc:1030 Graph] Initializer conv2.bias appears in graph inputs and will not be treated as constant value/weight. This may prevent some of the graph optimizations, like const folding. Move it out of graph inputs if there is no need to override it, by either re-generating the model with latest exporter/converter or with the tool onnxruntime/tools/python/remove_initializer_from_input.py.
2020-10-12 23:25:23.486322663 [W:onnxruntime:, graph.cc:1030 Graph] Initializer conv2.weight appears in graph inputs and will not be treated as constant value/weight. This may prevent some of the graph optimizations, like const folding. Move it out of graph inputs if there is no need to override it, by either re-generating the model with latest exporter/converter or with the tool onnxruntime/tools/python/remove_initializer_from_input.py.
2020-10-12 23:25:23.486335363 [W:onnxruntime:, graph.cc:1030 Graph] Initializer conv3.bias appears in graph inputs and will not be treated as constant value/weight. This may prevent some of the graph optimizations, like const folding. Move it out of graph inputs if there is no need to override it, by either re-generating the model with latest exporter/converter or with the tool onnxruntime/tools/python/remove_initializer_from_input.py.
2020-10-12 23:25:23.486348462 [W:onnxruntime:, graph.cc:1030 Graph] Initializer conv3.weight appears in graph inputs and will not be treated as constant value/weight. This may prevent some of the graph optimizations, like const folding. Move it out of graph inputs if there is no need to override it, by either re-generating the model with latest exporter/converter or with the tool onnxruntime/tools/python/remove_initializer_from_input.py.
2020-10-12 23:25:23.486361862 [W:onnxruntime:, graph.cc:1030 Graph] Initializer conv4.bias appears in graph inputs and will not be treated as constant value/weight. This may prevent some of the graph optimizations, like const folding. Move it out of graph inputs if there is no need to override it, by either re-generating the model with latest exporter/converter or with the tool onnxruntime/tools/python/remove_initializer_from_input.py.
2020-10-12 23:25:23.486384161 [W:onnxruntime:, graph.cc:1030 Graph] Initializer conv4.weight appears in graph inputs and will not be treated as constant value/weight. This may prevent some of the graph optimizations, like const folding. Move it out of graph inputs if there is no need to override it, by either re-generating the model with latest exporter/converter or with the tool onnxruntime/tools/python/remove_initializer_from_input.py.
Out[2]: <onnxruntime.capi.session.InferenceSession at 0x7f58367236d0>
我认为您的 ONNX 文件可能已损坏,请尝试使用 Netron 加载以验证。
作为旁注,PyTorch 版本和 onnx 版本应该与加载无关。
推荐阅读
- javascript - 为什么 indexOf 打印 -1 虽然值在数组中?
- javascript - 用于 ava test-runner 和 vue-test-utils 的 NuxtJS 的 Vue 组件导入别名
- javascript - ldapjs-client V0.1.0绑定方法在正常工作几天后抛出UnwillingToPerformError
- azure - 使用 blobproperties.getcreatedtime 时 SparkHDInsights 群集不起作用
- javascript - 从影响 Servicenow 上其他 d3 图表的 AngularJS 脚本下拉
- c++ - Aruco Marker 检测单个标记
- python - 将 (720, 720) 的 pandas DataFrame 重塑为 (518400, ) 2D 到 1D
- asp.net-core - 访问 NuGet 包中的 XML 文件
- javascript - js-datepicker 具有相同类的多个输入
- swift - 横向模式下的 SafariViewController