ONNX模型结构的查看(Pytorch模型转onnx以及转torchscript模型)

关于ONNX:不废话了,不了解的onnx的去知乎或者百度去查询(不要杠,杠就是你赢了)。

  1. 是不是还在烦恼模型的结构没有直观的感受,为什么别人对模型的理解这么深刻,讲道理,视觉上的感受一般比文字的感受更加深刻,所以神器在哪里?查看模型结构的神器在这里:Netron ONNX模型结构的查看(Pytorch模型转onnx以及转torchscript模型)https://netron.app/;

ONNX模型结构的查看(Pytorch模型转onnx以及转torchscript模型)
  1. 如果担心模型被拷贝或者泄露,可以下载离线的版本(速度不行的话,请使用科学上网)

GitHub – lutzroeder/netron: Visualizer for neural network, deep learning, and machine learning models ONNX模型结构的查看(Pytorch模型转onnx以及转torchscript模型)https://github.com/lutzroeder/netron ; 3. 你看,这个结构多清晰,一步一步的(想起了TF的tensor数据流),好了神器给你了,慢慢研究。

ONNX模型结构的查看(Pytorch模型转onnx以及转torchscript模型)

Pytorch模型转onnx怎么办?怎么设置参数?多输入的怎么办?

import torch

model.load_state_dict(torch.load("name.pth"))
model.eval()
dummy_input = torch.randn(1, 3, 32, 32)
dummy_input_2 = torch.randn(1, 3, 64, 64)
torch.onnx.export(model, dummy_input, 'name.onnx',verbose=True,opset_version=9/10/11)
多输入,opset_version根据torch的版本决定,转onnx的时候提示的
torch.onnx.export(model, [dummy_input,dummy_input_2], 'name.onnx',verbose=True,opset_version=9/10/11)

指定输入和输出导出onnx
input_names = [ "input_1"]
output_names = [ "output1" ]
torch.onnx.export(model, (dummy_input1, dummy_input2), "name.onnx", verbose=True, input_names=input_names, output_names=output_names)

我一般就根据模型自己去搜索输入和输出,其中还有几个参数可以选着,根据自己的情况去导出模型就行
(1) input_names (list of strings, default empty list) – 按顺序分配名称到图中的输入节点
(2) output_names (list of strings, default empty list) –按顺序分配名称到图中的输出节点

转换onnx后有很多的胶水op,可以用ONNX Simplifier简化模型

GitHub – daquexian/onnx-simplifier: Simplify your onnx model ONNX模型结构的查看(Pytorch模型转onnx以及转torchscript模型)https://github.com/daquexian/onnx-simplifier ; 安装pip3 install -U pip && pip3 install onnx-simplifier(注意onnx的版本),简化还是比较有必要的

ONNX模型结构的查看(Pytorch模型转onnx以及转torchscript模型)

Pytorch模型转torchscript模型

去这位博主的文章看看,写的很清楚了,不再赘述。那为什么要转torchscript模型?将pytorch模型转化成torchscript目的就是为了可以在c++环境中调用pytorch模型,同时可以在netron中查看模型的结构啦!!!(2条消息) pytorch模型转torchscript_superjfhc的博客-CSDN博客_torchscript ONNX模型结构的查看(Pytorch模型转onnx以及转torchscript模型)https://blog.csdn.net/weixin_41779359/article/details/109009192?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164735194616780261958314%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164735194616780261958314&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-109009192.142%5Ev2%5Epc_search_result_control_group,143%5Ev4%5Econtrol&utm_term=Pytorch%E6%A8%A1%E5%9E%8B%E8%BD%ACtorchscript%E6%A8%A1%E5%9E%8B&spm=1018.2226.3001.4187 ;

Original: https://blog.csdn.net/weixin_43999691/article/details/123512536
Author: 呆呆珝
Title: ONNX模型结构的查看(Pytorch模型转onnx以及转torchscript模型)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/650049/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球