[Neural network drawing tool] Detailed explanation of PlotNeuralNet

1. Introduction to PlotNeuralNet

1. Introduction

The PlotNeuralNet library is a tool library for drawing neural networks. The neural network drawn by it is relatively clean and tidy, and is more suitable for writing scientific research papers. Here, the author sorts out the usage methods of the library, and hopes that more friends can use it This library draws more beautiful neural networks.

Here are some Neural Netowrk Figures drawn by the library's code:

F C N − 8 FCN-8 FCN8

F C N − 32 FCN-32 FCN32

H o l i s t i c a l l y − N e s t e d E d g e D e t e c t i o n Holistically-Nested Edge Detection HolisticallyNestedEdgeDetection

2. Composition and principle of PlotNeuralNet

2.1 Code composition

The code of this repository is drawn with the help of three languages:

  • pythonFor designing your own neural networks, as the main organizing script file
  • shellUsed to organize the running of python and latex, as middleware. (more common and common on Linux systems)
  • LaTexUsed to draw the neural network, call it through python

2.2 Operating principle

The project uses python to call the latex code for drawing. Here is a short example to understand the working principle:

# Conv
def to_Conv( name, s_filer=256, n_filer=64, offset="(0,0,0)", to="(0,0,0)", width=1, height=40, depth=40, 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) +"""
        }
    };
"""

p y c o r e . t i k z e n g . p y pycore.tikzeng.py pycore.tikzeng.py

In fact, it can be seen from here that by setting function parameters and returning a latex string, it is equivalent to calling latex for drawing. (At the end there is code for tissue drawing)

2.3 Linux is friendly, Windows has some problems

However, it is worth noting that the original warehouse design is a solution for Linux users, which can run smoothly under the Ubuntu system, but there may be certain problems when running on the Windows system.

Therefore, the author himself forked the warehouse and made some modifications so that it can run on the windows system.

3. forked Github Repository

The source warehouse is a neural network visualization tool for Linux systems, and there may be certain error messages when running on windows.

The following is the address of the code warehouse after the author modified: (fork from the original warehouse)

SamuraiBUPT: PlotNeuralNet-Windows

Of course, you can also go directly to the original warehouse to browse:

HarisIqbal88: PlotNeuralNet

2. Quick Start-Windows

The following is the startup scheme for windows users.

1. Pre-settings

  • Make sure you have LaTex installed on your computer. The official warehouse recommends installing MikTeX , but I just installed texlive and it can run normally~
  • Make sure your Windows OS has an environment to run shell scripts. Git bash or Cygwin is recommended

2. Clone the repository

You can use git clone https://github.com/SamuraiBUPT/PlotNeuralNet-Windows.gitto complete the clone operation of the repository.

3. Follow the example below to understand how to run~

  • Open the cloned project folder with IDE
  • Enable the bash environment (if you already have Git Bash, and you have configured the environment variables, you can directly enter in the terminal bashor shenter the running environment of the shell script. You will see that the indicator in front of the terminal changes from the black and white of the windows system to other colors .
  • Enter pyexamplesthe path, ( cd pyexamples/)
  • Enter bash ../tikzmake.sh test_simplethe command to see the results of your first neural network plot.

4. Interpretation bash ../tikzmake.sh test_simpleof

As you can see, after you have created your own python script under the pyexamples path, you only need to call the tikzmake.shscript in the upper path under this path to run it.

  • bashdirective is to specify the shell script's compiler
  • ../tikzmake.shtikzmake.shIt is specified: execute the script in the upper directory
  • test_simpleIt is the file name of a python file in the current path, pay attention to .pythe suffix that does not need to be added! . Here, this file name will be passed in as a parameter of the shell script, which determines which file the target of the shell script execution is.

3. Usage

After understanding how to get started with the project code, we can start to draw our own neural network:

normal initialization

A regular python code for organizing a neural network should have the following skeleton :

import sys

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

# defined your arch
arch = [
    to_head('..'),
    to_cor(),
    to_begin(),

    # your architecture here...
    # ...
    # ...
    # ...

    to_end()
]


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


if __name__ == '__main__':
    main()

As you can see, the only thing you actually need to change is archthe contents of the list, between to_begin()the and to_end()functions , where you do your own neural network design.

4. Function description

You can refer to this blog for a description of the function , it is very detailed!

The author will also update the API documentation of the relevant functions in the future~

Guess you like

Origin blog.csdn.net/Samurai_Bushido/article/details/129924714