当前位置:网站首页>torch 网络模型转换onnx格式,并可视化
torch 网络模型转换onnx格式,并可视化
2022-06-12 19:30:00 【佐倉】
1. 构建lenet5 网络
import torch.nn as nn
import torch.nn.functional as F
import torch
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class LeNet(nn.Module):
def __init__(self,class_num=10,input_shape=(1,32,32)):
super(LeNet, self).__init__()
self.conv1 = nn.Sequential( #input_size=(1*28*28)
nn.Conv2d(1, 6, 5, 1, 2), #padding=2保证输入输出尺寸相同
nn.ReLU(), #input_size=(6*28*28)
nn.MaxPool2d(kernel_size=2, stride=2), #output_size=(6*14*14)
)
self.conv2 = nn.Sequential(
nn.Conv2d(6, 16, 5), #padding=1输出尺寸变化
nn.ReLU(), #input_size=(16*10*10)
nn.MaxPool2d(2, 2) #output_size=(16*5*5)
)
self.fc1 = nn.Sequential(
nn.Linear(16 * ((input_shape[1]//2-4)//2) * ((input_shape[2]//2-4)//2), 120),
nn.ReLU()
)
self.fc2 = nn.Sequential(
nn.Linear(120, 84),
nn.ReLU()
)
self.fc3 = nn.Linear(84, class_num)
# 定义前向传播过程,输入为x
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
# nn.Linear()的输入输出都是维度为一的值,所以要把多维度的tensor展平成一维
x = x.view(x.size()[0], -1)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
2. 转为onnx格式
input_shape = (1,100,100) #输入数据
model = LeNet(input_shape=input_shape)
torch.save(model, './model_para.pth')
torch_model = torch.load("./model_para.pth") # pytorch模型加载
batch_size = 1 #批处理大小
# set the model to inference mode
torch_model.eval()
x = torch.randn(batch_size,*input_shape) # 生成张量
print (x.shape)
export_onnx_file = "lenet5.onnx" # 目的ONNX文件名
torch.onnx.export(torch_model,
x,
export_onnx_file,
opset_version=10,
do_constant_folding=True, # 是否执行常量折叠优化
input_names=["input"], # 输入名
output_names=["output"], # 输出名
dynamic_axes={"input":{0:"batch_size"}, # 批处理变量
"output":{0:"batch_size"}})
3. 通过netron查看网络结构
3.1 netron安装
pip install netron
3.2 netron可视化
import netron
onnx_path = "lenet5.onnx"
netron.start(file=onnx_path, log=False, browse=True)
边栏推荐
- In 2021, the global revenue of electro-optical modulator (EOM) is about USD 360.3 million, and it is expected to reach USD 704.4 million in 2028
- 选电子工程被劝退,真的没前景了?
- Details of thansmitablethreadlocal
- asp. Net using JSON to interact with API data
- 【观察】华为下一代数据中心,为广西低碳高质量发展“添动能”
- 3GPP RAN第一次F2F会议,都干了些啥?
- 模塊八作業
- 基于微信电子书阅读小程序毕业设计毕设作品(6)开题答辩PPT
- 5G R17标准冻结,主要讲了些啥?
- no available service ‘null‘ found, please make sure registry config correct
猜你喜欢
RT thread simulator builds lvgl development and debugging environment
Download and configuration of nuitka packaging tutorial
Liunx deploy Seata (Nacos version)
Hardware test - why not use grounding clip for ripple test
Uniapp uses the Ali Icon
基于微信电子书阅读小程序毕业设计毕设作品(1)开发概要
[SQL] MySQL query statement execution sequence analysis
Implementation of VGA protocol based on FPGA
[5gc] Introduction to three SSC (session and service continuity) modes
Wechat e-book reading applet graduation design completion works (3) background function
随机推荐
Business opportunities with an annual increase of 3billion - non cage eggs or a new blue ocean for export to ASEAN
Cookie & Session & kaptcha验证码
BannerViewPager
EASYCODE one click plug-in custom template
【观察】华为下一代数据中心,为广西低碳高质量发展“添动能”
no available service ‘null‘ found, please make sure registry config correct
[generation confrontation network learning III] reading notes of Bigan paper and its principle understanding
[5gc] Introduction to three SSC (session and service continuity) modes
Shell programming regular expressions and metacharacters
review. JS ppt math formula cannot be displayed
Is it really hopeless to choose electronic engineering and be discouraged?
How does Eldon's ring of the law get lune quickly? Introduction to the fastest and safest method for obtaining lune
软件确认测试知识分享,上海第三方软件测试机构有哪些?
Native servlet - upload & download of files
Transactions in redis
Wechat e-book reading applet graduation design works (1) development outline
RT thread simulator builds lvgl development and debugging environment
Detailed explanation of IO flow basic knowledge -- file and IO flow principle
First build green, then build city
Leetcodesql: count the number of students in each major