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

关于pytorch转onnx经常出现的问题

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

关于pytorch转onnx经常出现的问题

1. F.avg_pool2d以及F.max_pool2d的转换

直接转会出现以下的错误:

RuntimeError: Failed to export an ONNX attribute 'onnx::Gather', since it's not constant, please try to make things (e.g., kernel size) static if possible

解决办法:

  • nn.AdaptiveAvgPool2d(1)替换 F.avg_pool2d
  • nn.AdaptiveMaxPool2d(1)替换F.max_pool2d
2. 转换过程种不能有动态的非tensor的数值

我的模型有一段是需要将scale在channel维度split成不均等的份数,代码是这么写的:

        scale = F.sigmoid(channel_att_sum).unsqueeze(2).unsqueeze(3)
        start = 0
        out = []
        for x in inputs:
            x = x * scale[:,start:start+x.size(1),:,:]
            start = start + x.size(1)
            out.append(x)

然后再转onnx的时候会出现:

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.

主要就是start是一个int的数值,在for循环种是迭代的。最后修改代码为如下,即固定好每一个split的份数。

        x1, x2, x3, x4, x5 = inputs 
        x1 = x1 *scale[:,self.start[0]:self.end[0],:,:]
        x2 = x2* scale[:,self.start[1]:self.end[1],:,:]
        x3 = x3* scale[:,self.start[2]:self.end[2],:,:]
        x4 = x4* scale[:,self.start[3]:self.end[3],:,:]
        x5 = x5* scale[:,self.start[4]:self.end[4],:,:]
        return x1, x2, x3, x4, x5

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

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

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