转换onnx模型的时候遇到_thnn_fused_lstm_cell的报错,刚开始的报错是提示Keyerror:_thnn_fused_lstm_cell,然后在https://github.com/pytorch/pytorch/issues/25533看到有人说是pytorch和CUDA版本太低的原因以及当使用CPU就不会报错,然后我首先尝试将pytorch升级到最新,CUDA也升级到10.2,最终还是报错Exporting the operator _thnn_fused_lstm_cell to ONNX opset version 9 is not supported. Please open a bug to request ONNX export support for the missing operator.

之后我定位到这个错误的产生是转换onnx模型时使用了torch.nn.LSTMCell()这个方法,如果使用torch.nn.LSTM()就不会报错,但是如果在CPU环境下将torch.nn.LSTMCell()转换成onnx则不会报错。总的来说就是torch.nn.LSTMCell()方法目前不支持在GPU环境下转换成onnx模型。目前已经给pytorch的GitHub提交issue,等待官方出解决方法。

下面是报错复现的列子:

import torch
import torch.nn as nnclass StackedLSTM(nn.Module):def __init__(self, num_layers, input_size, rnn_size, dropout):super(StackedLSTM, self).__init__()self.dropout = nn.Dropout(dropout)self.num_layers = num_layersself.layers = nn.ModuleList()for _ in range(num_layers):self.layers.append(nn.LSTMCell(input_size, rnn_size))input_size = rnn_sizedef forward(self, input_feed, hidden):h_0, c_0 = hiddenh_1, c_1 = [], []for i, layer in enumerate(self.layers):h_1_i, c_1_i = layer(input_feed, (h_0[i], c_0[i]))input_feed = h_1_iif i + 1 != self.num_layers:input_feed = self.dropout(input_feed)h_1 += [h_1_i]c_1 += [c_1_i]h_1 = torch.stack(h_1)c_1 = torch.stack(c_1)return input_feed, (h_1, c_1)#下面是在CPU环境下转换onnx模型,可以成功转换
lstm = StackedLSTM(2, 580, 500, 0.3)  #实例化StackedLSTM
h_t = torch.randn(2, 10, 500)  #隐状态h
h_c = torch.randn(2, 10, 500)  #隐状态c
hidden = (h_t, h_c)
a = torch.randn(10, 580)  #输入
torch.onnx.export(lstm, (a, hidden), f='LSTM.onnx', input_names=['input', 'hidden'], output_names=['output', 'h_t', 'h_c'])  #转换onnx模型下面是在GPU环境下转换onnx模型,报错!
lstm = StackedLSTM(2, 580, 500, 0.3).cuda()  #实例化StackedLSTM
h_t = torch.randn(2, 10, 500).cuda()  #隐状态h
h_c = torch.randn(2, 10, 500).cuda()  #隐状态c
hidden = (h_t, h_c)
a = torch.randn(10, 580).cuda()  #输入
torch.onnx.export(lstm, (a, hidden), f='LSTM.onnx', input_names=['input', 'hidden'], output_names=['output', 'h_t', 'h_c'])  #转换onnx模型

pytorch模型转onnx Exporting the operator _thnn_fused_lstm_cell to ONNX opset version 9 is not supported相关推荐

  1. silu to ONNX opset version 12 is not supported

    yolov7导出onnx时报错: Starting ONNX export with onnx 1.12.0... ONNX export failure: Exporting the operato ...

  2. 【pytorch】——exporting the operator relu6 to onnx opset version 13 is not supported

    pytorch1.8.0, onnx F.relu6无法导出到onnx.应该是pytorch1.8.0没有定义relu6的导出规则,但是用nn.relu6是可以的. import torch impo ...

  3. RuntimeError: Exporting the operator count_nonzero to ONNX opset version 13 is not supported. Please

    这个问题找了很久 一步一步试出来的 其实就是里面用到的某个函数onnx不支持,换一种形式就可以了 其中tensor.to_llist()也不支持

  4. Pytorch模型(.pth)转onnx模型(.onnx)

    简介 Open Neural Network Exchange(ONNX,开放神经网络交换)格式,是一个用于表示深度学习模型的标准,可使模型在不同框架之间进行转移. ONNX是一种针对机器学习所设计的 ...

  5. 【yolov5】pytorch模型导出为onnx模型

    博主想拿官网的yolov5训练好pt模型,然后转换成rknn模型,然后在瑞芯微开发板上调用模型检测.但是官网的版本对npu不友好,所以采用改进结构的版本: 将Focus层改成Conv层 将Swish激 ...

  6. YOLOv5的pytorch模型文件转换为ONNX文件

    YOLOv5 YOLOv5下载与测试运行 导出ONNX格式文件 ONNX转为为IR中间格式 环境: Windows 10 Anaconda 2.0.4 OpenVINO 工具包 2021.2 Pyth ...

  7. PyTorch模型部署:pth转onnx跨框架部署详解+代码

    文章目录 引言 基础概念 onnx:跨框架的模型表达标准 onnxruntime:部署模型的推理引擎 示例代码 0)安装onnx和onnxruntime 1)pytorch模型转onnx模型 2)on ...

  8. onnx实现对pytorch模型推理加速

    向AI转型的程序员都关注了这个号???????????? 人工智能大数据与深度学习  公众号:datayx 微软宣布将多平台通用ONNX机器学习引擎开源,此举将让机器学习框架,向着机器学习框架的标准化 ...

  9. pytorch模型(.pt)转onnx模型(.onnx)的方法详解(1)

    1. pytorch模型转换到onnx模型 2.运行onnx模型 3.比对onnx模型和pytorch模型的输出结果 我这里重点是第一点和第二点,第三部分  比较容易 首先你要安装 依赖库:onnx ...

  10. 【TensorRT】PyTorch模型转换为ONNX及TensorRT模型

    文章目录 1. PyTorch模型转TensorRT模型流程 2. PyTorch模型转ONNX模型 3. ONNX模型转TensorRT模型 3.1 TensorRT安装 3.2 将ONNX模型转换 ...

最新文章

  1. Java 构造方法与成员方法的区别
  2. http304缓存 php,通过http头设置http缓存
  3. 在envi做随机森林_随机森林原理介绍与适用情况(综述篇)
  4. jdbc_servlet基础增删改分页2(userinfo表的)
  5. JDK7和JDK9流中异常的处理
  6. object的classid收集
  7. 建个数据中心就想发展IDC?没那么简单!
  8. Scala tuple
  9. cocos2dx 物理碰撞
  10. spring Access denied for user ‘xx‘@‘localhost‘ (using password: YES)
  11. ngram模型中文语料实验step by step(3)-ngram模型的光滑处理
  12. [NAACL16]RNN文法
  13. 深入理解和使用nginx
  14. 遗传算法求解TSP问题(C++实现)
  15. ASC计算机比赛报名,新闻|2019 ASC 世界大学生超级计算机竞赛(ASC19)报名通知|信息与软件工程学院...
  16. 有点甜用计算机怎么谈,有点甜造句
  17. 【docker入门】
  18. DTP加载/ODS激活时持续黄灯的解决办法
  19. 北邮邮箱配置客户端教程(如Windows自带邮件)
  20. 基于SVM的车牌识别

热门文章

  1. 浪潮服务器销售案例ppt,浪潮服务器产品线介绍(ppt 105页)
  2. 用计算机运行搜索Ip的方法,怎样查ip地址 几种查ip地址的方法【图文】
  3. JavaScript中常见的设计模式
  4. 计算机入侵有什么方法,教你个一看就会的入侵方法 -电脑资料
  5. Flutter实现app自动升级
  6. html半透明遮罩,div半透明遮罩效果
  7. 关于动漫的HTML网页设计作业——动漫网页(刀剑神域6个页面)
  8. AspNetPager的使用
  9. Oracle 监控索引使用率脚本分享
  10. AI:2020年6月24日北京智源大会演讲分享之强化学习专题论坛——11: 40-12: 10俞扬教授《更好的环境模型,更好的强化学习》