问题描述
在使用 torch.onnx.export()
将 Pytorch 版 shufflenet v2 转为 onnx 的时候,出现了一个错误。
- 出错代码
torch.onnx.export(model,
input_example,
"{}.onnx".format(modelpath),
input_names = None,
output_names = None,
)
- 报错信息
Traceback (most recent call last):
File "convert_model.py", line 159, in <module>
cvt_shuffle_2_onnx(model_path["shufflenetv2_0_5"])
File "convert_model.py", line 142, in cvt_shuffle_2_onnx
output_names = None,
File "/home/learning/miniconda3/envs/tsm/lib/python3.6/site-packages/torch/onnx/__init__.py", line 168, in export
custom_opsets, enable_onnx_checker, use_external_data_format)
File "/home/learning/miniconda3/envs/tsm/lib/python3.6/site-packages/torch/onnx/utils.py", line 69, in export
use_external_data_format=use_external_data_format)
File "/home/learning/miniconda3/envs/tsm/lib/python3.6/site-packages/torch/onnx/utils.py", line 488, in _export
fixed_batch_size=fixed_batch_size)
File "/home/learning/miniconda3/envs/tsm/lib/python3.6/site-packages/torch/onnx/utils.py", line 351, in _model_to_graph
fixed_batch_size=fixed_batch_size, params_dict=params_dict)
File "/home/learning/miniconda3/envs/tsm/lib/python3.6/site-packages/torch/onnx/utils.py", line 154, in _optimize_graph
graph = torch._C._jit_pass_onnx(graph, operator_export_type)
File "/home/learning/miniconda3/envs/tsm/lib/python3.6/site-packages/torch/onnx/__init__.py", line 199, in _run_symbolic_function
return utils._run_symbolic_function(*args, **kwargs)
File "/home/learning/miniconda3/envs/tsm/lib/python3.6/site-packages/torch/onnx/utils.py", line 740, in _run_symbolic_function
return op_fn(g, *inputs, **attrs)
File "/home/learning/miniconda3/envs/tsm/lib/python3.6/site-packages/torch/onnx/symbolic_helper.py", line 129, in wrapper
return fn(g, *args)
File "/home/learning/miniconda3/envs/tsm/lib/python3.6/site-packages/torch/onnx/symbolic_opset9.py", line 1461, in slice
raise RuntimeError('Unsupported: ONNX export of Slice with dynamic inputs. DynamicSlice '
RuntimeError: Unsupported: ONNX export of Slice with dynamic inputs. DynamicSlice is a deprecated experimental op. Please use statically allocated variables or export to a higher opset version.
解决方法
根据报错信息中的最后一条提示,在执行这个函数的时候使用更高版本的 opset
:
torch.onnx.export(model,
input_example,
"{}.onnx".format(modelpath),
opset_version=10, # 使用版本 10
input_names = None,
output_names = None,
)