Python – onnx导出模型出现RuntimeError: Exporting the operator pad_sequence to ONNX opset version 13 is not supported错误
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
当前分类随机文章推荐
- OnnxRuntime - 如何部署多个输入和多个输出tensor的onnx模型 阅读145次,点赞0次
- OnnxRuntime - 模型部署笔记1,OnnxRuntime简介 阅读838次,点赞0次
- TensorRT - Windows下TensorRT下载与配置 阅读2014次,点赞0次
- TensorRT - 使用torch普通算子组合替代torch.einsum爱因斯坦求和约定算子的一般性方法 阅读3472次,点赞3次
- Python - 使用onnxruntime加载和推理onnx模型 阅读763次,点赞0次
- OnnxRuntime - Linux配置OnnxRuntime开发环境 阅读66次,点赞0次
- TensorRT - 计算模型推理时间 阅读525次,点赞1次
- TensortRT - 转换模型出现Could not locate zlibwapi.dll. Please make sure it is in your library path!错误 阅读1176次,点赞0次
- TensorRT - 使用TensorRT C++ SDK部署模型时推理时间波动不稳定或者推理速度越来越慢的问题 阅读255次,点赞0次
- OnnxRuntime – 模型部署笔记2,在Visual Studio中配置Onnx Rumtime C++开发环境 阅读661次,点赞0次
全站随机文章推荐
- TensorRT - 喜大普奔,TensorRT8.2 EA起开始支持Einsum爱因斯坦求和算子 阅读1991次,点赞0次
- WordPress - 异步发送邮件,解决发送邮件阻塞耗时的问题 阅读502次,点赞0次
- Python - opencv-python统计一个文件夹以及所有子文件夹下所有视频的帧率和帧数 阅读720次,点赞0次
- OpenCV - 读取一张图片显示,并将其重写为灰度图 阅读4967次,点赞0次
- 资源分享 - Game Engine Gems 3英文高清PDF下载 阅读3010次,点赞1次
- Python - argparse命令行参数解析库用法总结 阅读960次,点赞0次
- Pytorch - torch.topk参数详解与使用 阅读538次,点赞0次
- 资源分享 - GPU Pro 360 - Guide to Image Space 英文高清PDF下载 阅读2382次,点赞1次
- TensorRT - TensorRT was linked against cuBLAS/cuBLAS LT 11.6.1 but loaded cuBLAS/cuBLAS LT 11.5.4错误提示 阅读389次,点赞0次
- Modern OpenGL - GLSL着色语言4:GLSL中的数据存储限制符 阅读2393次,点赞0次
评论
169