栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Exporting the operator hardsigmoid to ONNX opset version 11 is not supported

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Exporting the operator hardsigmoid to ONNX opset version 11 is not supported

问题:

在将 MobileNetv3 模型从 PyTorch 转为 ONNX 时报错:

Error: RuntimeError: Exporting the operator hardsigmoid to ONNX opset version 11 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub.

运行环境:

PyTorch 版本:1.8.0

在网上找了很多文章,大部分都是写的一样的,就是将nn.Hardswish替换为自己实现的 Hardswitch,实际上,这个版本的 PyTorch 中,nn.Hardswish是可以转换成功的(老版本成不成功不清楚),报错的根本原因是 mobilenetv3 代码中使用了 F.hardsigmoid函数,解决办法是将代码中的 hardsigmoid 函数替换为 hardtanh 函数,如下:

class SqueezeExcitation(nn.Module):

    def __init__(self, input_channels: int, squeeze_factor: int = 4):
        super().__init__()
        squeeze_channels = _make_divisible(input_channels // squeeze_factor, 8)
        self.fc1 = nn.Conv2d(input_channels, squeeze_channels, 1)
        self.relu = nn.ReLU(inplace=True)
        self.fc2 = nn.Conv2d(squeeze_channels, input_channels, 1)

    def _scale(self, input: Tensor, inplace: bool) -> Tensor:
        scale = F.adaptive_avg_pool2d(input, 1)
        scale = self.fc1(scale)
        scale = self.relu(scale)
        scale = self.fc2(scale)
        return F.hardsigmoid(scale, inplace=inplace)  # 此行报错
        # return F.hardtanh(scale + 3, 0., 6., inplace=inplace) / 6.  # 更换为此行

    def forward(self, input: Tensor) -> Tensor:
        scale = self._scale(input, True)
        return scale * input

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026232.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号