pip install graphviz
pip install tochviz ( or pip install git+https://github.com/szagoruyko/pytorchviz)
Graphviz yes AT&T Developed an open source graphics visualization software , According to dot An undirected graph drawn in a scripting language ( Shows the simplest relationships between objects ) Draw an intuitive tree diagram .
Graphviz stay Windows Installation in needs to be downloaded Release package , And configure environment variables , Otherwise, an error will be reported :
graphviz.backend.ExecutableNotFound: failed to execute [‘dot’, ‘-Tpng’, ‘-O’, ‘tmp’], make sure the Graphviz executables are on your systems’ PATH
Graphviz Download address https://graphviz.gitlab.io/_pages/Download/Download_windows.html
After downloading, it's a “release” Folder , hold “release\bin” Directories adding to system environment variables , After that, input “dot -V”, Display the following information to indicate Graphviz Configuration is successful :
# Created by Pastoral CSDN
import torch
from torch import nn
from torchviz import make_dot, make_dot_from_trace
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)
vis_graph = make_dot(model(x), params=dict(model.named_parameters()))
vis_graph.view() # Will save one in the current directory “Digraph.gv.pdf” file , And open... In the default browser
with torch.onnx.set_training(model, False):
trace, _ = torch.jit.get_trace_graph(model, args=(x,))
make_dot_from_trace(trace)
call “make_dot” Method to create a dot object , Use “view” The method shows .
pytorch1.2 and 1.3 Used in version “torch.jit.get_trace_graph” There may be a mistake ,1.1 edition ok.
AttributeError: 'torch._C.Value' object has no attribute 'uniqueName'
Visualization results :
Netron Open source address : https://github.com/lutzroeder/Netron
Netron The developers are Lutz Roeder, One from Microsoft Visual Studio The handsome guy of the team :
Netron Is a support offline view “ Various ” Model visualization artifact of neural network framework , Among them “ Various ” Include :
- ONNX (.onnx, .pb, .pbtxt)
- Keras (.h5, .keras)
- Core ML (.mlmodel)
- Caffe (.caffemodel, .prototxt)
- Caffe2 (predict_net.pb, predict_net.pbtxt)
- MXNet (.model, -symbol.json)
- NCNN (.param)
- TensorFlow Lite (.tflite)
- TorchScript (.pt, .pth)
- PyTorch (.pt, .pth)
- Torch (.t7)
- Arm NN (.armnn)
- BigDL (.bigdl, .model)
- Chainer, (.npz, .h5)
- CNTK (.model, .cntk)
- Deeplearning4j (.zip)
- Darknet (.cfg)
- ML.NET (.zip)
- MNN (.mnn)
- OpenVINO (.xml)
- PaddlePaddle (.zip, __model__)
- scikit-learn (.pkl)
- TensorFlow.js (model.json, .pb)
- TensorFlow (.pb, .meta, .pbtxt)
Um. , That's enough .
Netron It's easy to use , The author provides installation packages for each platform , Open after installation , Drag the saved model file in .
Take the model above as an example , The first pytorch The model is saved :
import torch
from torch import nn
from torchviz import make_dot, make_dot_from_trace
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))
torch.save(model, 'model.pth') # Save the model
After use Netron Open the saved “model.pth”:
The network structure is very clear , Be clear at a glance , Further information about the operation can also be displayed on the right side .
If you're too lazy to install , You can also use the author's online Netron viewer , Address :https://lutzroeder.github.io/netron/