东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 3564|回复: 0
打印 上一主题 下一主题

[课堂笔记] pytorch模型转换为onnx

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14429
QQ
跳转到指定楼层
楼主
发表于 2020-8-3 17:50:59 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式


pytorch模型转换为onnx





  1. import torch.onnx
  2. from config import get_config_lite
  3. from Learner import face_learner


  4. if __name__ == '__main__':
  5.     # input_model = "./work_space_03_lite/save/model_2020-07-18-02-41_accuracy:0.9261636148230064_step:3311920_final.pth"
  6.     output_onnx = './work_space_03_lite/save/FaceRecog_dfy.onnx'
  7.     print("==> Exporting model to ONNX format at '{}'".format(output_onnx))
  8.     conf = get_config_lite(training=False)
  9.     # conf = get_config_dfy(training=False)
  10.     print("easydict的配置参数:", conf)
  11.     learner = face_learner(conf, inference=True)
  12.     fixed_str = "2020-07-18-02-41_accuracy:0.9261636148230064_step:3311920_final.pth"
  13.     learner.load_state(conf, fixed_str, model_only=True, from_save_folder=True)
  14.     # 开始:pytorch模型转换为onnx
  15.     input_names = ["input0"]
  16.     output_names = ["output0"]
  17.     inputs = torch.randn(1, 3, 112, 112).to(conf.device)
  18.     # torch1.5.1 ---> onnx
  19.     torch_out = torch.onnx._export(learner.model, inputs, output_onnx, export_params=True, verbose=False,
  20.                                    input_names=input_names, output_names=output_names)

  21. # pytorch ---> onnx
  22. # onnx    ---> caffe2
  23. # pip3 install onnx-caffe2
复制代码



import onnx
import torch
import torch.onnx


有些情况既需要torch.onnx 又需要 onnx


onnx_torch.onnx.png (168.08 KB, 下载次数: 115)

onnx_torch.onnx.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-20 18:17 , Processed in 0.195864 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表