Python – onnx导出模型出现RuntimeError: Exporting the operator pad_sequence to ONNX opset version 13 is not supported错误
本文作者: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
修改于:2022年12月01日 13:26:42
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即可。
参考连接
当前分类随机文章推荐
- TensorRT - Invalid Node - TopK,This version of TensorRT only supports input K as an initializer 阅读216次,点赞0次
- 移动端Android/ios深度学习模型部署框架总结 阅读235次,点赞0次
- OnnxRuntime – 模型部署笔记3,总结OnnxRuntime模型推理流程 阅读270次,点赞0次
- TensorRT - TensorRT was linked against cuBLAS/cuBLAS LT 11.6.1 but loaded cuBLAS/cuBLAS LT 11.5.4错误提示 阅读172次,点赞0次
- TensorRT - 转换onnx模型出现Slice_74 requires bool or uint8 I/O but node can not be handled by Myelin错误 阅读428次,点赞0次
- TensorRT - 使用C++ SDK出现无法解析的外部符号 "class sample::Logger sample::gLogger"错误 阅读381次,点赞0次
- TensorRT - 喜大普奔,TensorRT8.2 EA起开始支持Einsum爱因斯坦求和算子 阅读1703次,点赞0次
- NCNN - 在ncnn中实现Pytorch中相同的图片归一化,减均值,除方差预处理 阅读187次,点赞0次
- TensorRT - 计算模型推理时间 阅读254次,点赞1次
- TensorRT - workspace的作用 阅读526次,点赞0次
全站随机文章推荐
- C++ - 在CTC解码算法后移除相邻重复和blank索引 阅读406次,点赞0次
- 资源分享 - 机器学习 (西瓜书) 周志华著PDF下载 阅读31965次,点赞31次
- Pytorch - torch.cat函数 阅读319次,点赞0次
- Blender - 为模型贴透明贴图 阅读593次,点赞0次
- VPS - 使用XShell连接VPS 阅读3276次,点赞0次
- 资源分享 - Game AI Pro 3 - Collected Wisdom of Game AI Professionals 英文高清PDF下载 阅读2252次,点赞0次
- 资源分享 - Polygon Mesh Processing英文高清PDF下载 阅读7364次,点赞1次
- Python - 列表list遍历方法总结 阅读528次,点赞0次
- Google Adsense - 后台统计面板中CTR/CPC/RPM的含义 阅读2565次,点赞0次
- Mediapipe - 全身包含身体、手部、面部所有关键点标注位置对应图 阅读5964次,点赞4次
评论
169