the operator silu to ONNX opset version 10 is not supported

68 篇文章 27 订阅

目录

网上参考方法,没成功:

ok的解决方法:


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)

yolov7导出时也报错,我改了上面也继续报错。

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

ok的解决方法:

class SiLU(nn.Module):
    """export-friendly version of nn.SiLU()"""

    @staticmethod
    def forward(x):
        return x * torch.sigmoid(x)
def replace_module(module, replaced_module_type, new_module_type, replace_func=None) -> nn.Module:
    """
    Replace given type in module to a new type. mostly used in deploy.

    Args:
        module (nn.Module): model to apply replace operation.
        replaced_module_type (Type): module type to be replaced.
        new_module_type (Type)
        replace_func (function): python function to describe replace logic. Defalut value None.

    Returns:
        model (nn.Module): module that already been replaced.
    """

    def default_replace_func(replaced_module_type, new_module_type):
        return new_module_type()

    if replace_func is None:
        replace_func = default_replace_func

    model = module
    if isinstance(module, replaced_module_type):
        model = replace_func(replaced_module_type, new_module_type)
    else:  # recurrsively replace
        for name, child in module.named_children():
            new_child = replace_module(child, replaced_module_type, new_module_type)
            if new_child is not child:  # child is already replaced
                model.add_module(name, new_child)

    return model

model = replace_module(model, nn.SiLU, SiLU)
model = replace_module(model, nn.Hardsigmoid, HardSigmoid)

来自:

PPYOLOE_pytorch/model_utils.py at 545b6a68ef5765769f693dedb4cd0e086ad4eea2 · Nioolek/PPYOLOE_pytorch · GitHub

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值