当前位置:网站首页>Torch network model is converted to onnx format and visualized
Torch network model is converted to onnx format and visualized
2022-06-12 19:40:00 【Sakura】
1. structure lenet5 The Internet
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 Make sure the input and output dimensions are the same
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 Output size change
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)
# Define the forward propagation process , Input is x
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
# nn.Linear() The input and output of are all values with one dimension , So we need to put the multi-dimensional tensor Flatten into one dimension
x = x.view(x.size()[0], -1)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
2. To onnx Format
input_shape = (1,100,100) # input data
model = LeNet(input_shape=input_shape)
torch.save(model, './model_para.pth')
torch_model = torch.load("./model_para.pth") # pytorch Model loading
batch_size = 1 # Batch size
# set the model to inference mode
torch_model.eval()
x = torch.randn(batch_size,*input_shape) # Generating tensor
print (x.shape)
export_onnx_file = "lenet5.onnx" # Purpose ONNX file name
torch.onnx.export(torch_model,
x,
export_onnx_file,
opset_version=10,
do_constant_folding=True, # Whether to perform constant folding optimization
input_names=["input"], # Enter the name
output_names=["output"], # Output name
dynamic_axes={"input":{0:"batch_size"}, # Batch variables
"output":{0:"batch_size"}})
3. adopt netron Look at the network structure
3.1 netron install
pip install netron
3.2 netron visualization
import netron
onnx_path = "lenet5.onnx"
netron.start(file=onnx_path, log=False, browse=True)

边栏推荐
- Analysis report on market demand and investment strategy of China's re guarantee industry 2022-2028
- Equipment management - borrowing and returning module 1
- torch 网络模型转换onnx格式,并可视化
- 选电子工程被劝退,真的没前景了?
- exec函数、shell的实现
- What are the third-party software testing organizations in Shanghai that share knowledge about software validation testing?
- PostgreSQL数据库复制——后台一等公民进程WalReceiver pg_stat_wal_receiver视图
- [observation] Huawei's next generation data center "adds momentum" to Guangxi's low-carbon and high-quality development
- Add, delete, modify and query mysql, common MySQL commands
- review. JS ppt math formula cannot be displayed
猜你喜欢

RT-Thread 模拟器 simulator 搭建 LVGL 的开发调试环境

2022年最新宁夏建筑安全员模拟题库及答案

Detailed explanation of IO flow basic knowledge -- file and IO flow principle

leetcodeSQL:602. Friend application II: who has the most friends
![[5gc] Introduction to three SSC (session and service continuity) modes](/img/98/6e08986269c5dc1f5ce192cdef3e9f.png)
[5gc] Introduction to three SSC (session and service continuity) modes

Wechat e-book reading applet graduation design completion works (3) background function

基於分布式數據庫本身的定時備份方法

Leetcodesql: count the number of students in each major

Shell 编程正则表达式及元字符

Dacom G150双模耳机,为爱发声,呵护孩子听力健康成长
随机推荐
exec函数、shell的实现
How does Eldon's ring of the law get lune quickly? Introduction to the fastest and safest method for obtaining lune
Demand and business model innovation-4-strategy
选电子工程被劝退,真的没前景了?
Shell programming regular expressions and metacharacters
什么是数据驱动
Is it really hopeless to choose electronic engineering and be discouraged?
Process creation fork (), demise wait()
First build green, then build city
[digital ic/fpga] data accumulation output
开源深度学习框架PlaidML安装及测试
Demand and business model innovation - demand 2- demand basis
Wechat e-book reading applet graduation design work (6) opening defense ppt
RT-Thread 模拟器 simulator 搭建 LVGL 的开发调试环境
review. JS ppt math formula cannot be displayed
Market scale forecast and future competitive trend outlook report of China's plastic and cosmetic industry 2022-2028
Download and configuration of nuitka packaging tutorial
Demand and business model analysis-3-design
运算器的基本结构
EFCore调优