PyTorch神经网络的可视化踩坑

1. 网络结构可视化

参考资料: https://www.freesion.com/article/340667237/

1.1 使用netron可视化工具(对PyTorch的支持较差)

GitHub: https://github.com/lutzroeder/netron

不同平台的安装包下载地址:https://github.com/lutzroeder/netron/releases/tag/v4.9.5

(1)可以在macOS、Linux、Windows等主流操作系统上运行,还有网页版(http://netron.app)。

(2)支持多种主流的深度学习框架,比如ONNX、Keras、Tensorflow Lite、Caffe/Caffe2、Darknet、MNN、ncnn、PaddlePaddle等;对于Tensorflow、Pytorch等框架正在适配中,有测试版本可用。

1.1.1 常规思路: 将模型导出成.pth文件(坑)

  1. 首先将 PyTorch 网络保存成 .pth 文件
import torch
torch.save(net, "my_model.pth") # 这里的net即为你的网络
  1. 使用 netron 导入 my_model.pth 后即可看到成品

坑: 光有结构, 没有标识箭头

1.1.2 曲线救国: 将模型导出成 .onnx 文件(坑)

  1. 首先将 PyTorch 网络保存成 .onnx 文件
torch.onnx.export(net, train_x, "res.onnx", export_params=True, verbose=True, training=False)
  1. 使用 netron 导入 res.onnx 后即可看到成品

坑: 结构有, 箭头有, 标识错误

1.1.3 天降猛男: 将模型导出成 torchscript (好用)

  1. 导出成 .pt 文件, 下面两个都可以用, 都试一下.
traced_script_module = torch.jit.trace(net,train_x)
# 保存模型
traced_script_module.save("model-script.pt")
traced_script_module = torch.jit.script(net)

# 保存模型
traced_script_module.save("model-script2.pt")
  1. 使用 netron 导入 model-script.ptmodel-script2.pt 后即可看到成品

1.2 使用 graphviz

先去 graphviz 官网 下载安装包安装

然后安装这两个依赖包:

pip install graphviz  # 安装graphviz
pip install git+https://github.com/szagoruyko/pytorchviz  # 通过git安装torchviz
  1. 加载并运行一个模型
import torch
from torchvision.models import AlexNet
from torchviz import make_dot
 
x=torch.rand(8,3,256,512)
model=AlexNet()
y=model(x)
  1. 调用make_dot()函数构造图对象(三种方式)
g = make_dot(y)
g = make_dot(y, params=dict(model.named_parameters()))
g = make_dot(y, params=dict(list(model.named_parameters()) + [('x', x)]))
  1. 保存模型,以PDF格式保存(两种方式)
# g.view() # 会生成一个 Digraph.gv.pdf 的PDF文件 (文件名固定)
g.render('espnet_model', view=False) # 会自动保存为一个 espnet.pdf,第二个参数为True,则会自动打开该PDF文件,为False则不打开

2. 神经网络图可视化

参考资料: https://zhuanlan.zhihu.com/p/148896017

2.1 使用 dotnets 脚本

GitHub: https://github.com/martisak/dotnets

# Inspired by
# https://tgmstat.wordpress.com/2013/06/12/draw-neural-network-diagrams-graphviz/

# UPDATE HISTORY
# April, 2018 - 2to3 - Madhavun Candadai

# layers = [3, 5, 5, 5, 2]
layers = [3, 6, 6, 2]
hidden_len = 2

layers_str = ["Input"] + ["Hidden"] * hidden_len   + ["Output"]
layers_col = ["none"] + ["none"] * hidden_len   + ["none"]
layers_fill = ["black"] + ["gray"] * hidden_len  + ["black"]

penwidth = 15
font = "Cambria 10"

print("digraph G {")
# print("\tfontname = \"{}\"".format(font))
print("\trankdir=LR")
print("\tsplines=line")
print("\tnodesep=.08;")
print("\tranksep=1;")
print("\tedge [color=black, arrowsize=.5];")
print("\tnode [fixedsize=true,label=\"\",style=filled," + \
    "color=none,fillcolor=gray,shape=circle]\n")

# Clusters
for i in range(0, len(layers)):
    print(("\tsubgraph cluster_{} {{".format(i)))
    print(("\t\tcolor={};".format(layers_col[i])))
    print(("\t\tnode [style=filled, color=white, penwidth={},"
          "fillcolor={} shape=circle];".format(
              penwidth,
              layers_fill[i])))

    print(("\t\t"), end=' ')

    for a in range(layers[i]):
        print("l{}{} ".format(i + 1, a), end=' ')

    print(";")
    print(("\t\tlabel = {};".format(layers_str[i])))

    print("\t}\n")

# Nodes
for i in range(1, len(layers)):
    for a in range(layers[i - 1]):
        for b in range(layers[i]):
            print("\tl{}{} -> l{}{}".format(i, a, i + 1, b))

print("}")

生成 pdf 的命令:

python dotnets.py | dot -Tpdf > test.pdf

2.2 使用在线工具调

http://alexlenail.me/

2.3 MATLAB (仍在坑中)

需要在PyTorch上将神经网络模型转成.onnx文件
然后再用matlab导入
遂使用此脚本

modelfile = 'res.onnx';
classes = ["neg" "pos"];
net = importONNXNetwork(modelfile,'OutputLayerType','classification','Classes',classes)

然而报错

错误使用 importONNXNetwork (47)
importONNXNetwork 需要 Deep Learning Toolbox Converter for ONNX Model Format 支持包。要安装此支持包,请使用附加功能资源管理
器。

点击下载时,提示登录邮箱(我用的matlab是破解版)。没办法,抱着试一试的心态注册了一个号,结果是顺利将额外的包安上了。

再次输入代码,报错:

错误使用 nnet.internal.cnn.onnx.translateONNX>createInputLayer (440)
Only image inputs are supported for import.

出错 nnet.internal.cnn.onnx.translateONNX (58)
        inputLayer = createInputLayer(thisGraph, inputsWithoutInitializers{i}, UserImageInputSize);

出错 nnet.internal.cnn.onnx.importONNXNetwork (11)
[LayersOrGraph, translationIssues]      = nnet.internal.cnn.onnx.translateONNX(modelProto, OutputLayerType,
UserImageInputSize, true);

出错 importONNXNetwork (52)
Network = nnet.internal.cnn.onnx.importONNXNetwork(modelfile, varargin{:});

https://ww2.mathworks.cn/matlabcentral/answers/501080-error-loading-onnx-model-exported-from-pytorch-to-matlab

P.S. 听说可以将 PyTorch 转换成 MATLAB代码 ↓

https://github.com/albanie/mcnPyTorch

深度学习工具包 Deprecation notice. ----- This toolbox is outdated and no longer maintained. There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/) I would suggest you use one of the tools mentioned above rather than use this toolbox. Best, Rasmus. DeepLearnToolbox ================ A Matlab toolbox for Deep Learning. Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data. It is inspired by the human brain's apparent deep (layered, hierarchical) architecture. A good overview of the theory of Deep Learning theory is [Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf) For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng. * [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007) * [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010) * [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011) If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284) ``` @MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012", } ``` Contact: rasmusbergpalm at gmail dot com Directories included in the toolbox ----------------------------------- `NN/` - A library for Feedforward Backpropagation Neural Networks `CNN/` - A library for Convolutional Neural Networks `DBN/` - A library for Deep Belief Networks `SAE/` - A library for Stacked Auto-Encoders `CAE/` - A library for Convolutional Auto-Encoders `util/` - Utility functions used by the libraries `data/` - Data used by the examples `tests/` - unit tests to verify toolbox is working For references on each library check REFS.md Setup ----- 1. Download. 2. addpath(genpath('DeepLearnToolbox')); Example: Deep Belief Network --------------------- ```matlab function test_example_DBN load mnist_uint8; train_x = double(train_x) / 255; test_x = double(test_x) / 255; train_y = double(train_y); test_y = double(test_y); %% ex1 train a 100 hidden unit RBM and visualize its weights rand('state',0) dbn.sizes = [100]; opts.numepochs = 1; opts.batchsize = 100; opts.momentum = 0; opts.alpha = 1; dbn = dbnsetup(dbn, train_x, opts); dbn = dbntrain(dbn, train_x, opts); figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights %% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NN rand('state',0) %train dbn dbn.sizes = [100 100]; opts.numepochs = 1; opts.batchsize = 100; opts.momentum = 0; opts.alpha = 1; dbn = dbnsetup(dbn, train_x, opts); dbn = dbntrain(dbn, train_x, opts); %unfold dbn to nn nn = dbnunfoldtonn(dbn, 10); nn.activation_function = 'sigm'; %train nn opts.numepochs = 1; opts.batchsize = 100; nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.10, 'Too big error'); ``` Example: Stacked Auto-Encoders --------------------- ```matlab function test_example_SAE load mnist_uint8; train_x = double(train_x)/255; test_x = double(test_x)/255; train_y = double(train_y); test_y = double(test_y); %% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN % Setup and train a stacked denoising autoencoder (SDAE) rand('state',0) sae = saesetup([784 100]); sae.ae{1}.activation_function = 'sigm'; sae.ae{1}.learningRate = 1; sae.ae{1}.inputZeroMaskedFraction = 0.5; opts.numepochs = 1; opts.batchsize = 100; sae = saetrain(sae, train_x, opts); visualize(sae.ae{1}.W{1}(:,2:end)') % Use the SDAE to initialize a FFNN nn = nnsetup([784 100 10]); nn.activation_function = 'sigm'; nn.learningRate = 1; nn.W{1} = sae.ae{1}.W{1}; % Train the FFNN opts.numepochs = 1; opts.batchsize = 100; nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.16, 'Too big error'); ``` Example: Convolutional Neural Nets --------------------- ```matlab function test_example_CNN load mnist_uint8; train_x = double(reshape(train_x',28,28,60000))/255; test_x = double(reshape(test_x',28,28,10000))/255; train_y = double(train_y'); test_y = double(test_y'); %% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% error rand('state',0) cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer }; cnn = cnnsetup(cnn, train_x, train_y); opts.alpha = 1; opts.batchsize = 50; opts.numepochs = 1; cnn = cnntrain(cnn, train_x, train_y, opts); [er, bad] = cnntest(cnn, test_x, test_y); %plot mean squared error figure; plot(cnn.rL); assert(er<0.12, 'Too big error'); ``` Example: Neural Networks --------------------- ```matlab function test_example_NN load mnist_uint8; train_x = double(train_x) / 255; test_x = double(test_x) / 255; train_y = double(train_y); test_y = double(test_y); % normalize [train_x, mu, sigma] = zscore(train_x); test_x = normalize(test_x, mu, sigma); %% ex1 vanilla neural net rand('state',0) nn = nnsetup([784 100 10]); opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples [nn, L] = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.08, 'Too big error'); %% ex2 neural net with L2 weight decay rand('state',0) nn = nnsetup([784 100 10]); nn.weightPenaltyL2 = 1e-4; % L2 weight decay opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex3 neural net with dropout rand('state',0) nn = nnsetup([784 100 10]); nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex4 neural net with sigmoid activation function rand('state',0) nn = nnsetup([784 100 10]); nn.activation_function = 'sigm'; % Sigmoid activation function nn.learningRate = 1; % Sigm require a lower learning rate opts.numepochs = 1; % Number of full sweeps through data opts.batchsize = 100; % Take a mean gradient step over this many samples nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex5 plotting functionality rand('state',0) nn = nnsetup([784 20 10]); opts.numepochs = 5; % Number of full sweeps through data nn.output = 'softmax'; % use softmax output opts.batchsize = 1000; % Take a mean gradient step over this many samples opts.plot = 1; % enable plotting nn = nntrain(nn, train_x, train_y, opts); [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); %% ex6 neural net with sigmoid activation and plotting of validation and training error % split training data into training and validation data vx = train_x(1:10000,:); tx = train_x(10001:end,:); vy = train_y(1:10000,:); ty = train_y(10001:end,:); rand('state',0) nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax output opts.numepochs = 5; % Number of full sweeps through data opts.batchsize = 1000; % Take a mean gradient step over this many samples opts.plot = 1; % enable plotting nn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally) [er, bad] = nntest(nn, test_x, test_y); assert(er < 0.1, 'Too big error'); ``` [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

okfang616

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值