使用PlotNeuralNet绘制深度学习网络图的基本操作(二)

使用PlotNeuralNet绘制深度学习网络图的基本操作(二)

接下来我们利用pycharm来绘制当中我们的神经网络模型架构,目标是直接将.tex文件生成为pdf和png。我在学习的过程中参考了一些学习视频,觉得这个up主讲的还不错:
1.PlotNeuralNet_1_环境搭建与演示
2.PlotNeuralNet_2_概览与优化
3.PlotNeuralNet_3_分析与自定义绘图(完结)
视频会教你如何使用和修改当中的很多函数,并且直接将模型生成.pdf文件,我这里还进行了一些配置可以用代码将.pdf文件直接转化成为.png,希望能给大家提供一些帮助。
要想将pdf转换成png,需要你到官网下载这个文件:Ghostscript releases,配置完成后,我们就可以进行绘图啦。
代码中都有注释,大家可以仔细看看。

1.cnn网络框架

import sys
import os
import subprocess
sys.path.append('../')
from pycore.tikzeng import *


# 定义神经网络架构
arch = [
    to_head('..'),
    to_cor(),
    to_begin(),
    # 输入图像的名字
    to_input("1.jpeg", to='(-5,0,0)', width=6, height=6, name="temp"),
    to_input("2.jpeg", to='(-4,0,0)', width=6, height=6, name="temp"),
    to_input("3.jpeg", to='(-3,0,0)', width=6, height=6, name="temp"),
    to_input("4.jpeg", to='(-2,0,0)', width=6, height=6, name="temp"),
    to_input("5.jpeg", to='(-1,0,0)', width=6, height=6, name="temp"),

    # 调用当中的函数绘画模型的内容
    to_Conv("conv1", s_filer=256, n_filer=3, offset="(0,0,0)", to="(0,0,0)", height=50, depth=50, width=3, caption='CONV1'),
    to_Pool("pool1", offset="(0,0,0)", to="(conv1-east)", height=32, depth=32, width=3, caption="MaxPool1"),

    to_Conv("conv2", s_filer=63, n_filer=16, offset="(3,0,0)", to="(pool1-east)", height=32, depth=32, width=3, caption='CONV2'),
    to_connection("pool1", "conv2"),
    to_Pool("pool2", offset="(0,0,0)", to="(conv2-east)", height=16, depth=16, width=3, caption="MaxPool2"),

    to_Conv("conv3", s_filer=15, n_filer=64, offset="(3,0,0)", to="(pool2-east)", height=16, depth=16, width=3, caption='CONV3'),
    to_connection("pool2", "conv3"),
    to_Pool("pool3", offset="(0,0,0)", to="(conv3-east)", height=10, depth=10, width=3, caption="MaxPool3"),

    to_SoftMax(name='fc1', s_filer=64, offset="(4,0,0)", to="(pool3-east)", width=1.5, height=1.5, depth=100,
               opacity=0.8, caption='FC1'),
    to_connection("pool3", "fc1"),

    to_SoftMax(name='fc2', s_filer=10, offset="(2,0,0)", to="(fc1-east)", width=1.5, height=1.5, depth=50,
               opacity=0.8, caption='FC2'),
    to_connection("fc1", "fc2"),

    to_SoftMax(name='fc3', s_filer=5, offset="(2,0,0)", to="(fc2-east)", width=1.5, height=1.5, depth=5,
               opacity=0.8, caption='FC3'),
    to_connection("fc2", "fc3"),

    to_end()
]


def main():
    # 获取文件名
    namefile = str(sys.argv[0]).split('.')[0]

    # 转换成为.tex文件
    to_generate(arch, namefile + '.tex')

    # 使用 LaTeX 编译器将 .tex 文件转换为 .pdf 文件
    subprocess.call([r'D:\MiKTeX\install\miktex\bin\x64\pdflatex.exe', namefile + '.tex'])

    # 生成pdf
    pdf_file = namefile + '.pdf'
    # 生成png
    image_file = namefile + '.png'

    # 将pdf转化成为png
    subprocess.call([r'D:\ghostscript\gs10.01.1\bin\gswin64c.exe', '-sDEVICE=pngalpha', '-o', image_file, '-r300', pdf_file])

    # 删除中间生成的文件
    cleanup(namefile)


# 删除中间生成的文件
def cleanup(namefile):
    extensions = ['.aux', '.log', '.tex']
    for ext in extensions:
        filename = namefile + ext
        if os.path.exists(filename):
            os.remove(filename)


if __name__ == '__main__':
    main()

在这里插入图片描述

AlexNet网络框架

import sys
import os
import subprocess
sys.path.append('../')
from pycore.tikzeng import *


# 定义神经网络架构
arch = [
    to_head('..'),
    to_cor(),
    to_begin(),
    to_input("1.jpeg", to='(-5,0,0)', width=6, height=6, name="temp"),
    to_input("2.jpeg", to='(-4,0,0)', width=6, height=6, name="temp"),
    to_input("3.jpeg", to='(-3,0,0)', width=6, height=6, name="temp"),
    to_input("4.jpeg", to='(-2,0,0)', width=6, height=6, name="temp"),
    to_input("5.jpeg", to='(-1,0,0)', width=6, height=6, name="temp"),

    # s_filer表示该层的图像大小 (需要自己计算),n_filer表示输入通道和输出通道大小 (自己设定)
    to_Conv("conv1", s_filer=256, n_filer=3, offset="(0,0,0)", to="(0,0,0)", height=50, depth=50, width=3, caption='CONV1'),
    to_Pool("pool1", offset="(0,0,0)", to="(conv1-east)", height=32, depth=32, width=3, caption="MaxPool1"),

    to_Conv("conv2", s_filer=63, n_filer=64, offset="(3,0,0)", to="(pool1-east)", height=32, depth=32, width=3, caption='CONV2'),
    to_connection("pool1", "conv2"),
    to_Pool("pool2", offset="(0,0,0)", to="(conv2-east)", height=16, depth=16, width=3, caption="MaxPool2"),

    to_Conv("conv3", s_filer=31, n_filer=192, offset="(3,0,0)", to="(pool2-east)", height=16, depth=16, width=3, caption='CONV3'),
    to_connection("pool2", "conv3"),

    to_Conv("conv4", s_filer=15, n_filer=384, offset="(3,0,0)", to="(conv3-east)", height=16, depth=16, width=3,caption='CONV4'),
    to_connection("conv3", "conv4"),

    to_Conv("conv5", s_filer=15, n_filer=256, offset="(3,0,0)", to="(conv4-east)", height=16, depth=16, width=3,caption='CONV5'),
    to_connection("conv4", "conv5"),
    to_Pool("pool3", offset="(0,0,0)", to="(conv5-east)", height=8, depth=8, width=3, caption="MaxPool3"),

    to_Pool("pool4", offset="(3,0,0)", to="(pool3-east)", height=6, depth=6, width=3, caption="AdaptiveAvgPool"),
    to_connection("pool3", "pool4"),

    to_SoftMax(name='fc1', s_filer=4096, offset="(3,0,0)", to="(pool4-east)", width=1.5, height=1.5, depth=100,
               opacity=0.8, caption='FC1'),
    to_connection("pool4", "fc1"),

    to_SoftMax(name='fc2', s_filer=4096, offset="(2,0,0)", to="(fc1-east)", width=1.5, height=1.5, depth=50,
               opacity=0.8, caption='FC2'),
    to_connection("fc1", "fc2"),

    to_SoftMax(name='fc3', s_filer=5, offset="(2,0,0)", to="(fc2-east)", width=1.5, height=1.5, depth=5,
               opacity=0.8, caption='FC3'),
    to_connection("fc2", "fc3"),

    to_end()
]


def main():
    namefile = str(sys.argv[0]).split('.')[0]
    to_generate(arch, namefile + '.tex')

    # 使用 LaTeX 编译器将 .tex 文件转换为 .pdf 文件
    subprocess.call([r'D:\MiKTeX\install\miktex\bin\x64\pdflatex.exe', namefile + '.tex'])

    pdf_file = namefile + '.pdf'
    image_file = namefile + '.png'

    subprocess.call([r'D:\ghostscript\gs10.01.1\bin\gswin64c.exe', '-sDEVICE=pngalpha', '-o', image_file, '-r300', pdf_file])

    # 删除中间生成的文件
    cleanup(namefile)


def cleanup(namefile):
    # 删除中间生成的文件
    extensions = ['.aux', '.log', '.tex']
    for ext in extensions:
        filename = namefile + ext
        if os.path.exists(filename):
            os.remove(filename)


if __name__ == '__main__':
    main()

在这里插入图片描述

3.VGG16网络框架

import sys
import os
import subprocess
sys.path.append('../')
from pycore.tikzeng import *


# 定义神经网络架构
arch = [
    to_head('..'),
    to_cor(),
    to_begin(),
    to_input("1.jpeg", to='(-5,0,0)', width=6, height=6, name="temp"),
    to_input("2.jpeg", to='(-4,0,0)', width=6, height=6, name="temp"),
    to_input("3.jpeg", to='(-3,0,0)', width=6, height=6, name="temp"),
    to_input("4.jpeg", to='(-2,0,0)', width=6, height=6, name="temp"),
    to_input("5.jpeg", to='(-1,0,0)', width=6, height=6, name="temp"),

    # s_filer表示该层的图像大小 (需要自己计算),n_filer表示输入通道和输出通道大小 (自己设定)
    to_Conv("conv1", s_filer=256, n_filer=3, offset="(0,0,0)", to="(0,0,0)", height=50, depth=50, width=3, caption='CONV1'),
    to_Conv("conv2", s_filer=256, n_filer=64, offset="(0,0,0)", to="(conv1-east)", height=50, depth=50, width=3, caption=''),
    to_Pool("pool1", offset="(0,0,0)", to="(conv2-east)", height=30, depth=30, width=3, caption=""),

    to_Conv("conv3", s_filer=128, n_filer=64, offset="(3,0,0)", to="(pool1-east)", height=30, depth=30, width=3,caption='CONV2'),
    to_connection("pool1", "conv3"),
    to_Conv("conv4", s_filer=128, n_filer=128, offset="(0,0,0)", to="(conv3-east)", height=30, depth=30, width=3,caption=''),
    to_Pool("pool2", offset="(0,0,0)", to="(conv4-east)", height=18, depth=18, width=3, caption=""),

    to_Conv("conv5", s_filer=64, n_filer=128, offset="(3,0,0)", to="(pool2-east)", height=30, depth=30, width=3,caption=''),
    to_connection("pool2", "conv5"),
    to_Conv("conv6", s_filer=64, n_filer=256, offset="(0,0,0)", to="(conv5-east)", height=30, depth=30, width=3,caption='CONV3'),
    to_Conv("conv7", s_filer=64, n_filer=256, offset="(0,0,0)", to="(conv6-east)", height=30, depth=30, width=3,caption=''),
    to_Pool("pool3", offset="(0,0,0)", to="(conv7-east)", height=16, depth=16, width=3, caption=""),

    to_Conv("conv8", s_filer=32, n_filer=256, offset="(3,0,0)", to="(pool3-east)", height=16, depth=16, width=3,caption=''),
    to_connection("pool3", "conv8"),
    to_Conv("conv9", s_filer=32, n_filer=512, offset="(0,0,0)", to="(conv8-east)", height=16, depth=16, width=3,caption='CONV4'),
    to_Conv("conv10", s_filer=32, n_filer=512, offset="(0,0,0)", to="(conv9-east)", height=16, depth=16, width=3,caption=''),
    to_Pool("pool4", offset="(0,0,0)", to="(conv10-east)", height=8, depth=8, width=3, caption=""),

    to_Conv("conv11", s_filer=16, n_filer=512, offset="(3,0,0)", to="(pool4-east)", height=8, depth=8, width=3,caption=''),
    to_connection("pool4", "conv11"),
    to_Conv("conv12", s_filer=16, n_filer=512, offset="(0,0,0)", to="(conv11-east)", height=8, depth=8, width=3, caption='CONV5'),
    to_Conv("conv13", s_filer=16, n_filer=512, offset="(0,0,0)", to="(conv12-east)", height=8, depth=8, width=3, caption=''),
    to_Pool("pool5", offset="(0,0,0)", to="(conv13-east)", height=6, depth=6, width=3, caption=""),

    to_Pool("pool6", offset="(2,0,0)", to="(pool5-east)", height=6, depth=6, width=3, caption="AdaptiveAvgPool"),
    to_connection("pool5", "pool6"),

    to_SoftMax(name='fc1', s_filer=4096, offset="(4,0,0)", to="(pool6-east)", width=1.5, height=1.5, depth=100,
               opacity=0.8, caption='FC1'),
    to_connection("pool6", "fc1"),

    to_SoftMax(name='fc2', s_filer=4096, offset="(2,0,0)", to="(fc1-east)", width=1.5, height=1.5, depth=50,
               opacity=0.8, caption='FC2'),
    to_connection("fc1", "fc2"),

    to_SoftMax(name='fc3', s_filer=5, offset="(2,0,0)", to="(fc2-east)", width=1.5, height=1.5, depth=5,
               opacity=0.8, caption='FC3'),
    to_connection("fc2", "fc3"),

    to_end()
]


def main():
    namefile = str(sys.argv[0]).split('.')[0]
    to_generate(arch, namefile + '.tex')

    # 使用 LaTeX 编译器将 .tex 文件转换为 .pdf 文件
    subprocess.call([r'D:\MiKTeX\install\miktex\bin\x64\pdflatex.exe', namefile + '.tex'])

    pdf_file = namefile + '.pdf'
    image_file = namefile + '.png'

    subprocess.call([r'D:\ghostscript\gs10.01.1\bin\gswin64c.exe', '-sDEVICE=pngalpha', '-o', image_file, '-r300', pdf_file])

    # 删除中间生成的文件
    cleanup(namefile)


def cleanup(namefile):
    # 删除中间生成的文件
    extensions = ['.aux', '.log', '.tex']
    for ext in extensions:
        filename = namefile + ext
        if os.path.exists(filename):
            os.remove(filename)


if __name__ == '__main__':
    main()

在这里插入图片描述

4.ResNet网络框架

import sys
import os
import subprocess
sys.path.append('../')
from pycore.tikzeng import *


def main():
    namefile = str(sys.argv[0]).split('.')[0]  # 获取当前文件名

    # 使用 LaTeX 编译器将 .tex 文件转换为 .pdf 文件
    subprocess.call([r'D:\MiKTeX\install\miktex\bin\x64\pdflatex.exe', namefile + '.tex'])

    pdf_file = namefile + '.pdf'
    image_file = namefile + '.png'

    subprocess.call([r'D:\ghostscript\gs10.01.1\bin\gswin64c.exe', '-sDEVICE=pngalpha', '-o', image_file, '-r300', pdf_file])

    # 删除中间生成的文件
    cleanup(namefile)


def cleanup(namefile):
    # 删除中间生成的文件
    # extensions = ['.aux', '.log', '.tex']
    extensions = ['.aux', '.log']
    for ext in extensions:
        filename = namefile + ext
        if os.path.exists(filename):
            os.remove(filename)


if __name__ == '__main__':
    main()

在这里插入图片描述

5.DenseNet网络框架

import sys
import os
import subprocess
sys.path.append('../')
from pycore.tikzeng import *


def main():
    namefile = str(sys.argv[0]).split('.')[0]  # 获取当前文件名

    # 使用 LaTeX 编译器将 .tex 文件转换为 .pdf 文件
    subprocess.call([r'D:\MiKTeX\install\miktex\bin\x64\pdflatex.exe', namefile + '.tex'])

    pdf_file = namefile + '.pdf'
    image_file = namefile + '.png'

    subprocess.call([r'D:\ghostscript\gs10.01.1\bin\gswin64c.exe', '-sDEVICE=pngalpha', '-o', image_file, '-r300', pdf_file])

    # 删除中间生成的文件
    cleanup(namefile)


def cleanup(namefile):
    # 删除中间生成的文件
    # extensions = ['.aux', '.log', '.tex']
    extensions = ['.aux', '.log']
    for ext in extensions:
        filename = namefile + ext
        if os.path.exists(filename):
            os.remove(filename)


if __name__ == '__main__':
    main()

在这里插入图片描述
注意:ResNet和DenseNet中一些划线的操作,需要你在.tex文件中进行修改和定义,这需要自行去学习latex的一些操作。

%定位
\pic[shift={(0,-5,0)}] at (sum1-west) 
{
	Box={
		name=score1,%
		fill=\PoolColor,%
		opacity=0,height=0.01,width=0.01,depth=0.01
	}
};


% 划线
\path (conv2-east) -- (sum1-south) coordinate[pos=-0.5] (between4_5) ;
\draw [connection]  (between4_5)    -- node {\midarrow} (score1-west-|between4_5) -- node {\midarrow} (score1-west);
\draw [connection]  (score1-east) -- node {\midarrow} (score1-east -| sum1-south) -- node {\midarrow} (sum1-south);
  • 4
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
PlotNeuralNet是一个用于绘制卷积神经网络的Python库。通过使用该库,您可以轻松地创建漂亮的卷积神经网络结构图。您可以使用PlotNeuralNet的API来定义网络的每个层次和连接。具体步骤如下: 1. 首先,安装PlotNeuralNet库。您可以在中找到面向PythonPlotNeuralNet教程,其中提供了详细的安装说明和使用示例。 2. 导入PlotNeuralNet库并创建一个新的网络图对象。 3. 使用API定义网络的每个层次。您可以使用PlotNeuralNet提供的各种函数来添加卷积层、池化层、全连接层等。根据您的网络结构和需求,您可以自由地调整每个层次的参数。 4. 使用API定义网络的连接。您可以使用PlotNeuralNet提供的函数来定义网络中每个层次之间的连接关系。您可以指定连接的输入和输出层次以及连接的类型(如卷积连接、池化连接等)。 5. 最后,使用API绘制网络图。您可以使用PlotNeuralNet提供的函数将网络图绘制为图像文件或在Jupyter Notebook中显示。 具体的使用示例可以在和中找到。这些示例提供了使用PlotNeuralNet绘制卷积神经网络的代码和详细说明。 综上所述,您可以使用PlotNeuralNet库的API来绘制卷积神经网络结构图。通过定义每个层次和连接,您可以创建自定义的网络图,并使用提供的函数将其绘制出来。 参考文献: 面向PythonPlotNeuralNet教程 使用PlotNeuralNet绘制深度学习网络图 【论文作图】使用PlotNeuralNet绘制卷积神经网络——以VGG-F为例

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

红狐狸的北北记

红狐狸背着行囊上路,感谢支持!

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

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

打赏作者

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

抵扣说明:

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

余额充值