https://github.com/szagoruyko/pytorchviz
import torch
from torch import nn
from torchvision import models
from torchviz import make_dot, make_dot_from_trace
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('device',device)
简易神经网络可视化
##构建模型
model = nn.Sequential()
model.add_module('w0', nn.Linear(8, 16))
model.add_module('tanh', nn.Tanh())
model.add_module('w1', nn.Linear(16, 1))
##可视化
x = torch.randn(1, 8)
make_dot(model(x), params=dict(model.named_parameters()))
卷积神经网络可视化
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv_layers = nn.Sequential(
nn.Conv2d(1, 10, kernel_size=5),#图片的大小:28-5=23
nn.MaxPool2d(2),#图片的大小:23/2=12
nn.ReLU(),
nn.Conv2d(10, 20, kernel_size=5),#图片的大小:12-5=7
nn.Dropout(),
nn.MaxPool2d(2),#图片的大小:7/2=4
nn.ReLU(),
)
self.fc_layers = nn.Sequential(
nn.Linear(320, 50),
nn.ReLU(),
nn.Dropout(),
nn.Linear(50, 10),
nn.Softmax(dim=1)
)
def forward(self, x):
x = self.conv_layers(x)#1*20*4*4
x = x.view(-1, 320)#1*320
x = self.fc_layers(x)#1*10
return x
model = Net()
x = torch.randn(1, 1, 28, 28).to(device)
make_dot(model(x), params=dict(model.named_parameters()))
LSTM可视化
lstm_cell = nn.LSTMCell(128, 128)
x = torch.randn(1, 128)
make_dot(lstm_cell(x), params=dict(lstm_cell.named_parameters()))
对Resnet18进行可视化
model = models.resnet18(pretrained=True)
model = model.eval().to(device)
x = torch.randn(1, 3, 256, 256).to(device)
make_dot(model(x), params=dict(model.named_parameters()))
对自己训练的模型进行可视化
model = torch.load('checkpoints/fruit30_pytorch_20230123.pth')#自己训练的模型
model = model.eval().to(device)
x = torch.randn(1, 3, 256, 256).to(device)
make_dot(model(x), params=dict(model.named_parameters()))
Pytorch模型转ONNX模型
x = torch.randn(1, 3, 256, 256).to(device)
with torch.no_grad():
torch.onnx.export(
model, # 要转换的模型
x, # 模型的任意一组输入
'resnet18.onnx', # 导出的 ONNX 文件名
opset_version=11, # ONNX 算子集版本
input_names=['input'], # 输入 Tensor 的名称(自己起名字)
output_names=['output'] # 输出 Tensor 的名称(自己起名字)
)
验证onnx模型导出成功
import onnx
# 读取 ONNX 模型
onnx_model = onnx.load('resnet18.onnx')
# 检查模型格式是否正确
onnx.checker.check_model(onnx_model)
print('无报错,onnx模型载入成功')
https://github.com/paulgavrikov/visualkeras
https://towardsdatascience.com/tensorboard-visualizing-learning-ad1b6667585
http://alexlenail.me/NN-SVG/index.html
https://github.com/HarisIqbal88/PlotNeuralNet
https://netron.app/
https://zetane.com/
把神经网络的当成一个图,对图进行降维和聚类之后的效果
https://www.graphcore.ai/posts/what-does-machine-learning-look-like
https://github.com/julrog/nn_vis
链接:https://pan.baidu.com/s/1Ih6i3bVhwA_wCxmjmCgLhA
提取码:cae2
本文主要介绍了11个神经网络结构可视化工具和它们的可视化效果展示,包括: