onnx: step = 1 is currently not supported

举报
风吹稻花香 发表于 2022/09/01 00:51:41 2022/09/01
【摘要】 1. 在yolov5s的pytorch模型转换onnx模型时报如下错误: RuntimeError: step!=1 is currently not supported 原因主要是低版本的opset不支持切片操作导致的; 把模型转换的代码改成如下所示即可,即使用版本11以上的opset: torch.onnx.export(...

1. 在yolov5s的pytorch模型转换onnx模型时报如下错误:

RuntimeError: step!=1 is currently not supported
原因主要是低版本的opset不支持切片操作导致的;

把模型转换的代码改成如下所示即可,即使用版本11以上的opset:

torch.onnx.export(model, img, "xxx.onnx", verbose=True,opset_version=11,export_params=True)

我这个onnx不支持这个参数;

解决方法是:


  
  1. class OnnxExportApiArgs:
  2. """
  3. configuration for torch onnx export api invocation
  4. """
  5. def __init__(self, opset_version: int = 11, input_names: List[str] = ['inpout'], output_names: List[str] = ['output']):
  6. """
  7. Refer torch documentation https://pytorch.org/docs/1.7.1/onnx.html?highlight=onnx%20export#torch.onnx.export
  8. :param opset_version: onnx opset version to use to export the model
  9. :param input_names: names to assign to the input nodes of the onnx graph, in order
  10. :param output_names: names to assign to the output nodes of the graph, in order
  11. """
  12. self.opset_version = opset_version
  13. self.input_names = input_names
  14. self.output_names = output_names
  15. @property
  16. def kwargs(self):
  17. """
  18. formats all override options into kwarg format to appended to onnx export call
  19. """
  20. return {'opset_version': self.opset_version,
  21. 'input_names': self.input_names,
  22. 'output_names': self.output_names}

调用:


  
  1. if onnx_export_args is None:
  2. onnx_export_args = OnnxExportApiArgs()
  3. torch.onnx.export(model, dummy_input, temp_file, enable_onnx_checker=False,**onnx_export_args.kwargs)


2. 解决该问题后可能会继续出现如下错误:

RuntimeError: Exporting the operator silu to ONNX opset version 11 is not supported. Please open a bug to request ONNX export support for the missing operator.
这是因为onnx与pytorch一些方法不兼容导致的,onnx不支持silu,把激活函数换一种写法即可。

解决方法如下:

pytorch的激活函数源码位置:X:\anaconda3\envs\python3.7\Lib\sitepackages\torch\nn\modules\activation.py(此处结合自己的anaconda实际安装位置来更改)。

修改成如下:


  
  1. class SiLU(Module):
  2.  
  3.     __constants__ = ['inplace']
  4.     inplace: bool
  5.  
  6.     def __init__(self, inplace: bool = False):
  7.         super(SiLU, self).__init__()
  8.         self.inplace = inplace
  9.  
  10.     def forward(self, input: Tensor) -> Tensor:
  11.         # ------------------------------------- #
  12.         # 把F.silu替换掉,修改后如下
  13.         return input * torch.sigmoid(input)
  14.  
  15.         #原来的代码
  16.         #return F.silu(input, inplace=self.inplace)


————————————————
版权声明:本文为CSDN博主「小码乔」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_37889356/article/details/121671974

文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/126615988

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。