文章目录 Netron简介 Netron加载模型类型 Netron使用方式 Netron功能介绍 完整案例 总结
Netron简介
Netron是一个支持PyTorch的可视化工具,它的开发者是微软的Lutz Roeder,操作简单快捷,就像保存文件、打开文件一样,简单高效。 Netron支持跨平台如Linux、macOS、Window。
Netron加载模型类型
Netron支持以下常见格式:ONNX (.onnx)、TensorFlow (.pb)、Keras (.h5)、PyTorch (需转化为ONNX格式)。其他支持格式:Caffe、CoreML、Darknet 等(完整列表见官方文档)。 模型输入格式要求:torch.onnx.export()
的第一个参数需要是 PyTorch 模型实例(torch.nn.Module
),而不是模型文件(如.pth
或.dict
)。如果您的模型保存在 .pth 或 .dict 文件中,需要先通过 torch.load() 加载参数,并赋值到模型结构中
model = YourModelClass( )
state_dict = torch. load( 'model.pth' )
model. load_state_dict( state_dict)
支持的模型参数文件格式: .pth
文件:通常是 PyTorch 的 state_dict
(参数字典)或完整模型(包含结构和参数)。如果是 state_dict
,需配合模型结构使用。.dict
文件:可能是用户自定义的扩展名,实际内容应为 PyTorch 支持的序列化格式(与 .pth
类似),需通过torch.load()
加载。
torch. save( model. state_dict( ) , 'model.pth' )
torch. save( model, 'full_model.pth' )
Netron需要onnx文件格式,ONNX 导出注意事项:对于 torch.onnx.export()
来说,原模型文件本身不需要特定格式 ,因为输入参数 model
必须是一个已经加载到内存中的 PyTorch 模型(nn.Module
实例)。
输入张量 :torch.randn(1, 3, 640, 640)
是示例输入,需与模型实际输入尺寸一致(YOLOv5 的默认输入为 [batch, 3, height, width]
)。动态维度 :若需支持动态 batch 或尺寸,可通过 dynamic_axes
参数指定:torch. onnx. export( model, torch. randn( 1 , 3 , 640 , 640 ) , 'model.onnx' , dynamic_axes= { 'input' : { 0 : 'batch' } , 'output' : { 0 : 'batch' } }
)
例如:通过torch.onnx.export()生成.onnx文件,再通过netron.start()打开.onnx文件
import torch
from torchvision. models import AlexNet
import netronmodel = AlexNet( ) input = torch. ones( ( 1 , 3 , 224 , 224 ) ) torch. onnx. export( model, input , f= 'AlexNet.onnx' )
netron. start( 'AlexNet.onnx' )
默认会启动http://localhost:8080/
网页,可以
Netron使用方式
在线使用:可以先将文件转化为.onnx
格式,再通过访问在线netron上传模型进行使用。 安装客户端本地使用:访问netron releases,下载安装包进行使用 python环境中安装使用:激活anaconda环境执行pip install netron
Netron功能介绍
Netron层次化展示模型结构,可以查看的具体信息包括,该层的名称,所属模块,参数属性、输入的参数和输出的参数。 左侧可以到处图片,展示展示或隐藏相关参数信息。也可以将模型结构横向展示。
完整案例
pip list
import math
import netron
import torch. onnx
import torch. nn as nn
from torch. autograd import Variabledefault_cfg = { 11 : [ 64 , 'M' , 128 , 'M' , 256 , 256 , 'M' , 512 , 512 , 'M' , 512 , 512 ] , 13 : [ 64 , 64 , 'M' , 128 , 128 , 'M' , 256 , 256 , 'M' , 512 , 512 , 'M' , 512 , 512 ] , 16 : [ 64 , 64 , 'M' , 128 , 128 , 'M' , 256 , 256 , 256 , 'M' , 512 , 512 , 512 , 'M' , 512 , 512 , 512 ] , 19 : [ 64 , 64 , 'M' , 128 , 128 , 'M' , 256 , 256 , 256 , 256 , 'M' , 512 , 512 , 512 , 512 , 'M' , 512 , 512 , 512 , 512 ] , } class vgg ( nn. Module) : def __init__ ( self, dataset= 'cifar10' , depth= 19 , init_weights= True , cfg= None ) : super ( vgg, self) . __init__( ) if cfg is None : cfg = default_cfg[ depth] self. feature = self. make_layers( cfg, True ) if dataset == 'cifar10' : num_classes = 10 elif dataset == 'cifar100' : num_classes = 100 self. classifier = nn. Linear( cfg[ - 1 ] , num_classes) if init_weights: self. _initialize_weights( ) def make_layers ( self, cfg, batch_norm= False ) : layers = [ ] in_channels = 3 for v in cfg: if v == 'M' : layers += [ nn. MaxPool2d( kernel_size= 2 , stride= 2 ) ] else : conv2d = nn. Conv2d( in_channels, v, kernel_size= 3 , padding= 1 , bias= False ) if batch_norm: layers += [ conv2d, nn. BatchNorm2d( v) , nn. ReLU( inplace= True ) ] else : layers += [ conv2d, nn. ReLU( inplace= True ) ] in_channels = vreturn nn. Sequential( * layers) def forward ( self, x) : x = self. feature( x) x = nn. AvgPool2d( 2 ) ( x) x = x. view( x. size( 0 ) , - 1 ) y = self. classifier( x) return ydef _initialize_weights ( self) : for m in self. modules( ) : if isinstance ( m, nn. Conv2d) : n = m. kernel_size[ 0 ] * m. kernel_size[ 1 ] * m. out_channelsm. weight. data. normal_( 0 , math. sqrt( 2. / n) ) if m. bias is not None : m. bias. data. zero_( ) elif isinstance ( m, nn. BatchNorm2d) : m. weight. data. fill_( 0.5 ) m. bias. data. zero_( ) elif isinstance ( m, nn. Linear) : m. weight. data. normal_( 0 , 0.01 ) m. bias. data. zero_( ) if __name__ == '__main__' : net = vgg( ) x = Variable( torch. FloatTensor( 16 , 3 , 40 , 40 ) ) y = net( x) print ( y. data. shape) onnx_path = "onnx_model_name.onnx" torch. onnx. export( net, x, onnx_path) netron. start( onnx_path)
总结
模型文件本身不需要特定格式 ,但需通过 PyTorch 正确加载为 nn.Module
实例。.pth
或 .dict
文件需配合模型结构代码使用(除非保存的是完整模型)。ONNX 导出要求模型实例和示例输入张量的形状匹配。