• 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

Python – onnx导出模型出现RuntimeError: Exporting the operator pad_sequence to ONNX opset version 13 is not supported错误

模型部署 发布于2022-12-01 阅读 5,218次 0次评论 1次点赞 本文共1414个字,阅读需要4分钟。

1 onnx导出模型出现RuntimeError: Exporting the operator pad_sequence to ONNX opset version 13 is not supported错误

今天在pytorch中导出模型为onnx时,由于使用了from torch.nn.utils.rnn import pad_sequence,在执行torch.onnx.export导出模型时出现了以下的错误

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

意思就是这个算法pad_seqeunce在onnx中不支持,我们可以自定义一下这个函数,就是将一个列表中不等长的Tensor自动补齐到最大长度,自定义的pad_seqeunce代码如下

def pad_sequence(sequences, batch_first=False, max_len=None, padding_value=0):
    """
        对一个List中的元素进行padding
            sequences:
            batch_first: 是否把batch_size放到第一个维度
            padding_value:
            max_len : 最大句子长度,默认为None,即在每个batch中以最长样本的长度对其它样本进行padding;
            当指定max_len的长度小于一个batch中某个样本的长度,那么在这个batch中还是会以最长样本的长度对其它样本进行padding
            建议指定max_len的值为整个数据集中最长样本的长度
        Returns:
        """
    max_size = sequences[0].size()
    trailing_dims = max_size[1:]
    length = max_len
    max_len = max([s.size(0) for s in sequences])
    if length is not None:
        max_len = max(length, max_len)
    if batch_first:
        out_dims = (len(sequences), max_len) + trailing_dims
    else:
        out_dims = (max_len, len(sequences)) + trailing_dims
    out_tensor = sequences[0].data.new(*out_dims).fill_(padding_value)
    for i, tensor in enumerate(sequences):
        length = tensor.size(0)
        # use index notation to prevent duplicate references to the tensor
        if batch_first:
            out_tensor[i, :length, ...] = tensor
        else:
            out_tensor[:length, i, ...] = tensor

    return out_tensor

使用自定义的pad_sequence函数替换掉from torch.nn.utils.rnn import pad_sequence函数,重新训练并导出为onnx即可。

参考连接

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:Python – onnx导出模型出现RuntimeError: Exporting the operator pad_sequence to ONNX opset version 13 is not supported错误

原文链接:https://www.stubbornhuang.com/2430/

发布于:2022年12月01日 13:26:42

修改于:2023年06月21日 17:47:49

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
如何回看PLTV/m3u8直播源
其他
下一篇
TensorRT - 转换onnx模型出现Slice_74 requires bool or uint8 I/O but node can not be handled by Myelin错误
TensorRT
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号