wordpress 整站转移,厦门建设局举报投诉,网络品牌推广多少钱,网站建设需要哪些材料对于神经网络架构的可视化是很有意义的#xff0c;可以在很大程度上帮助到我们清晰直观地了解到整个架构#xff0c;我们在前面的 PyTorch的ONNX结合MNIST手写数字数据集的应用(.pth和.onnx的转换与onnx运行时) 有介绍#xff0c;可以将模型架构文件(常见的格式都可以)在线上…对于神经网络架构的可视化是很有意义的可以在很大程度上帮助到我们清晰直观地了解到整个架构我们在前面的 PyTorch的ONNX结合MNIST手写数字数据集的应用(.pth和.onnx的转换与onnx运行时) 有介绍可以将模型架构文件(常见的格式都可以)在线上传到 https://netron.app/将会生成架构示意图比如将yolov5s.pt这个预训练模型上传之后将出现下面这样的图片(局部) 这种属于非常简单的层的连接展示也能够直观知道整个架构是由哪些层组成虽然每层可以查看一些属性不过对于每层的具体细节并没有那么直观展现在图片当中。 接下来介绍的这两款都会生成漂亮的可视化神经网络图可以用来绘制报告和演示使用效果非常棒。
1、NN-SVG
NN-SVG生成神经网络架构的地址http://alexlenail.me/NN-SVG/AlexNet.html 显示可能很慢最好科学上网进去之后我们可以看到有三种神经网络架构可以进行设置FCNN、LeNet、AlexNet 我们分别来看下
1.1、FCNN
第一种就是最基础的全连接神经网络FCNN输入层--隐藏层(若干)--输出层截图如下 左侧边栏可以进行一些颜色、形状、透明度等设置也可以很方便的增加和减少层。右边就会实时的显示出操作的效果。
1.2、LeNet
LeNet是一种经典的卷积神经网络最初用来识别手写数字我们来看下其结构 可以看到架构主要是由卷积层组成输入层--卷积层--最大池化层--...--全连接层--输出层。 左边同样的都是可以设置颜色透明度等可以增减层数在每层里可以设置数量、高宽以及卷积核大小还可以指定是否显示层的名称这样就更加清楚的知道架构是由哪些具体的层组成了。
1.3、AlexNet
AlexNet是辛顿和他的学生Alex Krizhevsky设计的CNN在2012年ImageNet的竞赛中获得冠军它是在LeNet的基础上应用了ReLU激活函数(取代Sigmoid)、Dropout层(避免过拟合)、LRN层(增强泛化能力)等的一种神经网络截图如下 同样的可以直观看到每个层的数量、宽高、卷积核的大小这些直观的神经网络示意图尤其对于初学者来说可以很好的理解某个神经网络的整个计算过程。 最后的这些都是可以点击Download SVG将其下载成svg格式(一种XML格式)的文件。
2、PlotNeuralNet
2.1、安装
首先确认自己的操作系统然后对应着进行安装后面出现的示例是本人的Ubuntu 18.04版本上做的。
Ubuntu 16.04
sudo apt-get install texlive-latex-extra
Ubuntu 18.04.2 基于本网站请安装以下软件包包含一些字体包
sudo apt-get install texlive-latex-base
sudo apt-get install texlive-fonts-recommended
sudo apt-get install texlive-fonts-extra
sudo apt-get install texlive-latex-extraWindows或其他系统
下载安装MiKTeXhttps://miktex.org/download
下载安装Git bashhttps://git-scm.com/download/win 或者Cygwinhttps://www.cygwin.com/ 准备就绪之后运行即可
cd pyexamples/
bash ../tikzmake.sh test_simple
2.2、克隆运行
上面的Latex安装好了之后就克隆PlotNeuralNet
git clone https://github.com/HarisIqbal88/PlotNeuralNet.git 我们先来执行自带的一个测试文件
cd pyexamples/
bash ../tikzmake.sh test_simple
将生成test_simple.pdf截图如下 2.3、test_simple.py
我们来看下自带的test_simple.py内容
import sys
sys.path.append(../)
from pycore.tikzeng import *# defined your arch
arch [to_head( .. ),to_cor(),to_begin(),to_Conv(conv1, 512, 64, offset(0,0,0), to(0,0,0), height64, depth64, width2 ),to_Pool(pool1, offset(0,0,0), to(conv1-east)),to_Conv(conv2, 128, 64, offset(1,0,0), to(pool1-east), height32, depth32, width2 ),to_connection( pool1, conv2), to_Pool(pool2, offset(0,0,0), to(conv2-east), height28, depth28, width1),to_SoftMax(soft1, 10 ,(3,0,0), (pool1-east), captionSOFT ),to_connection(pool2, soft1), to_Sum(sum1, offset(1.5,0,0), to(soft1-east), radius2.5, opacity0.6),to_connection(soft1, sum1),to_end()]def main():namefile str(sys.argv[0]).split(.)[0]to_generate(arch, namefile .tex )if __name__ __main__:main()
代码比较简单导入库之后就是定义架构然后就自定义的每一层都写在arch这个列表中的 to_begin() 和 to_end() 之间然后就通过函数 to_generate() 将arch列表生成.tex文件最后就是通过bash自动转换成pdf文件我们查看下bash文件内容cat tikzmake.sh
#!/bin/bashpython $1.py
pdflatex $1.texrm *.aux *.log *.vscodeLog
rm *.texif [[ $OSTYPE darwin* ]]; thenopen $1.pdf
elsexdg-open $1.pdf
fi
2.4、自定义网络架构
接下来我们自定义一个网络架构测试下tony.py
import sys
sys.path.append(../)
from pycore.tikzeng import *# defined your arch
arch [to_head(..),to_cor(),to_begin(),to_input(dog.png, width18, height14),to_Conv(conv1, 512, 64, offset(1,0,0), to(0,0,0), height64, depth64, width10,captionConv1 Layer),to_Pool(pool1, offset(0,0,0), to(conv1-east),captionPool1 Layer),to_Conv(conv2, 128, 64, offset(4,0,0), to(pool1-east), height32, depth32, width5,captionConv2 Layer),to_connection(pool1, conv2),to_Pool(pool2, offset(0,0,0), to(conv2-east), height28, depth28, width1,captionPool2 Layer),to_SoftMax(soft1, 10 ,(8,0,0), (pool1-east), captionSoftmax Layer),to_connection(pool2, soft1),to_skip(ofpool1,topool2,pos1.25),to_end()]def main():namefile str(sys.argv[0]).split(.)[0]to_generate(arch, namefile .tex )if __name__ __main__:main()
其中一些代码的解释 to_input可以指定输入图片to(conv1-east)表示当前层在conv1的东边(右边)to_connection( pool1, conv2)在两者之间画连接线caption标题to_skip做跳线其中pos大于1表示向上进行画线小于1就是向下这个可以自己进行调试 如果对一些方法不明确其有哪些参数可以使用帮助help to_input(pathfile, to(-3,0,0), width8, height8, nametemp) to_SoftMax(name, s_filer10, offset(0,0,0), to(0,0,0), width1.5, height3, depth25, opacity0.8, caption ) 当然这里的需要命令行进入到PlotNeuralNet目录因为需要加载from pycore.tikzeng import * 其他层需要加入依葫芦画瓢即可很简单比如to_UnPool(Unpool, offset(5,0,0), to(0,0,0),height64, width2, depth64, captionUnpool), to_ConvRes(ConvRes, s_filer512, n_filer64, offset(10,0,0), to(0,0,0), height64, width2, depth64, captionConvRes), to_ConvSoftMax(ConvSoftMax, s_filer512, offset(15,0,0), to(0,0,0), height64, width2, depth64, captionConvSoftMax), to_Sum(sum, offset(5,0,0), to(ConvSoftMax-east), radius2.5, opacity0.6),... 2.5、tikzeng.py
我们来查看下tikzeng.py代码
import osdef to_head( projectpath ):pathlayers os.path.join( projectpath, layers/ ).replace(\\, /)return r
\documentclass[border8pt, multi, tikz]{standalone}
\usepackage{import}
\subimport{ pathlayers r}{init}
\usetikzlibrary{positioning}
\usetikzlibrary{3d} %for including external image
def to_cor():return r
\def\ConvColor{rgb:yellow,5;red,2.5;white,5}
\def\ConvReluColor{rgb:yellow,5;red,5;white,5}
\def\PoolColor{rgb:red,1;black,0.3}
\def\UnpoolColor{rgb:blue,2;green,1;black,0.3}
\def\FcColor{rgb:blue,5;red,2.5;white,5}
\def\FcReluColor{rgb:blue,5;red,5;white,4}
\def\SoftmaxColor{rgb:magenta,5;black,7}
\def\SumColor{rgb:blue,5;green,15}
def to_begin():return r
\newcommand{\copymidarrow}{\tikz \draw[-Stealth,line width0.8mm,draw{rgb:blue,4;red,1;green,1;black,3}] (-0.3,0) -- (0.3,0);}\begin{document}
\begin{tikzpicture}
\tikzstyle{connection}[ultra thick,every node/.style{sloped,allow upside down},draw\edgecolor,opacity0.7]
\tikzstyle{copyconnection}[ultra thick,every node/.style{sloped,allow upside down},draw{rgb:blue,4;red,1;green,1;black,3},opacity0.7]
# layers definitiondef to_input( pathfile, to(-3,0,0), width8, height8, nametemp ):return r
\node[canvas is zy plane at x0] ( name ) at to {\includegraphics[width str(width)cm,height str(height)cm]{ pathfile }};
# Conv
def to_Conv( name, s_filer256, n_filer64, offset(0,0,0), to(0,0,0), width1, height40, depth40, caption ):return r
\pic[shift{ offset }] at to {Box{name name ,caption caption r,xlabel{{ str(n_filer) , }},zlabel str(s_filer) ,fill\ConvColor,height str(height) ,width str(width) ,depth str(depth) }};
# Conv,Conv,relu
# Bottleneck
def to_ConvConvRelu( name, s_filer256, n_filer(64,64), offset(0,0,0), to(0,0,0), width(2,2), height40, depth40, caption ):return r
\pic[shift{ offset }] at to {RightBandedBox{name name ,caption caption ,xlabel{{ str(n_filer[0]) , str(n_filer[1]) }},zlabel str(s_filer) ,fill\ConvColor,bandfill\ConvReluColor,height str(height) ,width{ str(width[0]) , str(width[1]) },depth str(depth) }};
# Pool
def to_Pool(name, offset(0,0,0), to(0,0,0), width1, height32, depth32, opacity0.5, caption ):return r
\pic[shift{ offset }] at to {Box{namename,caption caption r,fill\PoolColor,opacity str(opacity) ,height str(height) ,width str(width) ,depth str(depth) }};
# unpool4,
def to_UnPool(name, offset(0,0,0), to(0,0,0), width1, height32, depth32, opacity0.5, caption ):return r
\pic[shift{ offset }] at to {Box{name name r,caption caption r,fill\UnpoolColor,opacity str(opacity) ,height str(height) ,width str(width) ,depth str(depth) }};
def to_ConvRes( name, s_filer256, n_filer64, offset(0,0,0), to(0,0,0), width6, height40, depth40, opacity0.2, caption ):return r
\pic[shift{ offset }] at to {RightBandedBox{name name ,caption caption ,xlabel{{ str(n_filer) , }},zlabel str(s_filer) r,fill{rgb:white,1;black,3},bandfill{rgb:white,1;black,2},opacity str(opacity) ,height str(height) ,width str(width) ,depth str(depth) }};
# ConvSoftMax
def to_ConvSoftMax( name, s_filer40, offset(0,0,0), to(0,0,0), width1, height40, depth40, caption ):return r
\pic[shift{ offset }] at to {Box{name name ,caption caption ,zlabel str(s_filer) ,fill\SoftmaxColor,height str(height) ,width str(width) ,depth str(depth) }};
# SoftMax
def to_SoftMax( name, s_filer10, offset(0,0,0), to(0,0,0), width1.5, height3, depth25, opacity0.8, caption ):return r
\pic[shift{ offset }] at to {Box{name name ,caption caption ,xlabel{{ ,dummy}},zlabel str(s_filer) ,fill\SoftmaxColor,opacity str(opacity) ,height str(height) ,width str(width) ,depth str(depth) }};
def to_Sum( name, offset(0,0,0), to(0,0,0), radius2.5, opacity0.6):return r
\pic[shift{ offset }] at to {Ball{name name ,fill\SumColor,opacity str(opacity) ,radius str(radius) ,logo$$}};
def to_connection( of, to):return r
\draw [connection] (of-east) -- node {\midarrow} (to-west);
def to_skip( of, to, pos1.25):return r
\path ( of -southeast) -- ( of -northeast) coordinate[pos str(pos) ] ( of -top) ;
\path ( to -south) -- ( to -north) coordinate[pos str(pos) ] ( to -top) ;
\draw [copyconnection] (of-northeast)
-- node {\copymidarrow}(of-top)
-- node {\copymidarrow}(to-top)
-- node {\copymidarrow} (to-north);
def to_end():return r
\end{tikzpicture}
\end{document}
def to_generate( arch, pathnamefile.tex ):with open(pathname, w) as f: for c in arch:print(c)f.write( c )
从这些代码也可以看出通过这些方法返回的是Latex代码来进行绘制的。 运行命令bash ../tikzmake.sh tony 生成如图 可以看到生成的可视化架构图相比较于以前手工做图来说真的大大提高了效率。更多详情可以去看具体源码。
githubPlotNeuralNet