当前位置:网站首页>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)

边栏推荐
- Original publishing practice of pipeline in Jenkins docking with CMDB interface to obtain host list
- 设备管理-借还模块1
- 基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告
- Equipment management - borrowing / returning module interface code
- “即服务”,未来已来,始于现在 | IT消费新模式,FOD按需计费
- Ctfshow-web265 (deserialization)
- 【生成对抗网络学习 其三】BiGAN论文阅读笔记及其原理理解
- Learning summary in March
- Demand and business model innovation - demand 2- demand basis
- 腾讯云TDP-virt-viewer win客户端的软件使用
猜你喜欢

Reading small program graduation design based on wechat e-book (5) assignment

Negative remainder problem

Demand and business model innovation - demand 2- demand basis

How does Eldon's ring of the law get lune quickly? Introduction to the fastest and safest method for obtaining lune

Business opportunities with an annual increase of 3billion - non cage eggs or a new blue ocean for export to ASEAN

Demand and business model innovation - demand 3- demand engineering process

DACOM G150 dual-mode earphones make sound for love and protect the healthy growth of children's hearing

How do I create my own appender in log4j- How to create my own Appender in log4j?

What did 3GPP ran do in the first F2F meeting?

设备管理-借还模块1
随机推荐
EFCore调优
Process creation fork (), demise wait()
Ctfshow-web265 (deserialization)
【数字IC/FPGA】数据累加输出
Details of thansmitablethreadlocal
解释器文件
Demand and business model analysis-1-business model canvas
基于微信电子书阅读小程序毕业设计毕设作品(8)毕业设计论文模板
Wechat e-book reading applet graduation design work (6) opening defense ppt
Demand and business model innovation - demand 1 - Introduction to demand engineering
DACOM G150 dual-mode earphones make sound for love and protect the healthy growth of children's hearing
Learning summary in March
基于微信电子书阅读小程序毕业设计毕设作品(6)开题答辩PPT
设备管理-借还模块1
RT-Thread 模拟器 simulator 搭建 LVGL 的开发调试环境
进程的创建fork()、消亡wait()
Research Report on the overall scale, major manufacturers, major regions, products and application segments of lifeboats in the global market in 2022
[image denoising] image denoising based on anisotropic filtering with matlab code
vc hacon 聯合編程 GenImage3Extern WriteImage
Microsoft Word 教程,如何在 Word 中插入页眉或页脚?