the operator silu to ONNX opset version 10 is not supported

x33g5p2x  于2022-06-06 转载在 其他  
字(1.0k)|赞(0)|评价(0)|浏览(340)
  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)
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实际安装位置来更改)。

修改成如下:

class SiLU(Module):
 
    __constants__ = ['inplace']
    inplace: bool
 
    def __init__(self, inplace: bool = False):
        super(SiLU, self).__init__()
        self.inplace = inplace
 
    def forward(self, input: Tensor) -> Tensor:
        # ------------------------------------- #
        # 把F.silu替换掉,修改后如下
        return input * torch.sigmoid(input)
 
        #原来的代码
        return F.silu(input, inplace=self.inplace)

————————————————

原文链接:https://blog.csdn.net/weixin_37889356/article/details/121671974

相关文章

微信公众号

最新文章

更多