Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pytorchviz and Netron to visualize pytorch network structure

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to use pytorchviz and Netron to visualize the pytorch network structure. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Using pytorchviz to visualize installation dependencies and pytorchviz

Pip install graphviz

Pip install tochviz (or pip install git+ https://github.com/szagoruyko/pytorchviz)

Graphviz is an open source graphic visualization software developed by AT&T, which can draw an intuitive tree diagram according to the undirected graph (showing the simplest relationship between objects) drawn in the dot scripting language.

To install Graphviz in Windows, you need to download the Release package and configure the environment variables, otherwise an error will be reported:

Graphviz.backend.ExecutableNotFound: failed to execute ['dot','-Tpng','- Orange, '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, unzip it into a "release" folder, add the "release\ bin" directory to the system environment variable, and then enter "dot-V" in the terminal to display the following information to indicate that the Graphviz configuration is successful:

Torchviz Visualization torch Network structure

# Created by Makino 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 ('W1Christ, nn.Linear (16,1))

X = torch.randn (1pc8)

Vis_graph = make_dot (model (x), params=dict (model.named_parameters ()

Vis_graph.view () # saves a "Digraph.gv.pdf" file in the current directory and opens it 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 the "make_dot" method to create a dot object, which is displayed using the "view" method.

The use of "torch.jit.get_trace_graph" in pytorch2.2 and version 1.3 may result in errors, version 1.1 ok.

AttributeError: 'torch._C.Value' object has no attribute' uniqueName'

Visualization results:

Second, visualization using Netron

Netron open source address: https://github.com/lutzroeder/Netron

The developer of Netron is Lutz Roeder, a handsome guy from Microsoft's Visual Studio team:

Netron is a model visualization artifact that supports offline viewing of "various" neural network frameworks, including:

ONNX (.onnx, .pb, .pbtxt)

Keras (.h6, .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, .h6)

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)

Well, that's enough.

Netron is easy to use. The author provides installation packages for each platform, which can be opened after installation and dragged into the saved model files.

Taking the above model as an example, save the pytorch model first:

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 ('W1Christ, nn.Linear (16,1))

Torch.save (model, 'model.pth') # Save the model

Then open the saved "model.pth" with Netron:

The network structure is clear and clear at a glance, and further information about the operation can be displayed on the right.

After reading the above, do you have any further understanding of how to use pytorchviz and Netron to visualize the pytorch network structure? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report