PlotNeuralNet

repository·master·Indexed 12 days ago

https://github.com/harisiqbal88/plotneuralnet

A tool for generating LaTeX code to create high-quality visual representations of neural network architectures. It provides a Python API with functions such as to_Conv, to_Pool, and to_SoftMax to programmatically define architectures and export them as .tex files for compilation.

Tokens
816
Snippets
3
Records
3
Agent score
48%

What's inside PlotNeuralNet

  1. Install PlotNeuralNet dependencies

    master

    PlotNeuralNet generates LaTeX code to draw neural networks. You must have a LaTeX distribution installed on your system to compile the output.

    Ubuntu

    For Ubuntu 16.04:

    sudo apt-get install texlive-latex-extra

    For 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-extra

    Windows

    1. Download and install MikTeX.
    2. Install a bash runner such as Git bash or Cygwin.
    sudo apt-get install texlive-latex-extra
  2. Use the Python API to define a neural network architecture

    master

    You can programmatically define neural network architectures using Python. This involves importing functions from pycore.tikzeng and building an arch list containing layer definitions.

    Workflow

    1. Create a new directory and a Python file.
    2. Import tikzeng (ensure the project root is in sys.path).
    3. Define an arch list using functions like to_Conv, to_Pool, to_SoftMax, and to_connection.
    4. Use to_generate(arch, filename) to produce the .tex file.
    5. Run the script using the tikzmake.sh helper script to compile the LaTeX into an image.

    Example Architecture Definition

    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)", height=64, depth=64, width=2 ),
        to_Pool("pool1", offset="(0,0,0)", to="(conv1-east)"),
        to_Conv("conv2", 128, 64, offset="(1,0,0)", to="(pool1-east)", height=32, depth=32, width=2 ),
        to_connection( "pool1", "conv2"),
        to_Pool("pool2", offset="(0,0,0)", to="(conv2-east)", height=28, depth=28, width=1),
        to_SoftMax("soft1", 10 ,"(3,0,0)", "(pool1-east)", caption="SOFT"  ),
        to_connection("pool2", "soft1"),
        to_end()
        ]
    
    def main():
        namefile = str(sys.argv[0]).split('.')[0]
        to_generate(arch, namefile + '.tex' )
    
    if __name__ == '__main__':
        main()

    Compiling the output

    After writing your script (e.g., my_arch.py), run it via the shell script:

    bash ../tikzmake.sh my_arch
    import sys
    sys.path.append('../')
    from pycore.tikzeng import *
    
    arch = [
        to_head( '..' ),
        to_cor(),
        to_begin(),
        to_Conv("conv1", 512, 64, offset="(0,0,0)", to="(0,0,0)", height=64, depth=64, width=2 ),
        to_Pool("pool1", offset="(0,0,0)", to="(conv1-east)"),
        to_Conv("conv2", 128, 64, offset="(1,0,0)", to="(pool1-east)", height=32, depth=32, width=2 ),
        to_connection( "pool1", "conv2"),
        to_Pool("pool2", offset="(0,0,0)", to="(conv2-east)", height=28, depth=28, width=1),
        to_SoftMax("soft1", 10 ,"(3,0,0)", "(pool1-east)", caption="SOFT"  ),
        to_connection("pool2", "soft1"),
        to_end()
        ]
    
    def main():
        namefile = str(sys.argv[0]).split('.')[0]
        to_generate(arch, namefile + '.tex' )
    
    if __name__ == '__main__':
        main()