Repository: luanshiyinyang/PlotNeuralNet Branch: master Commit: 34bf5df88bbb Files: 22 Total size: 87.0 KB Directory structure: gitextract_0_96fyzl/ ├── PlotNeuralNet/ │ ├── .gitignore │ ├── README.md │ ├── examples/ │ │ ├── HED/ │ │ │ └── HED.tex │ │ ├── SoftmaxLoss/ │ │ │ └── SoftmaxLoss.tex │ │ ├── Unet/ │ │ │ └── Unet.tex │ │ ├── Unet_Ushape/ │ │ │ └── Unet_ushape.tex │ │ ├── VGG16/ │ │ │ └── vgg16.tex │ │ ├── fcn32s/ │ │ │ └── fcn32.tex │ │ └── fcn8s/ │ │ └── fcn8.tex │ ├── layers/ │ │ ├── Ball.sty │ │ ├── Box.sty │ │ ├── RightBandedBox.sty │ │ └── init.tex │ ├── pycore/ │ │ ├── __init__.py │ │ ├── blocks.py │ │ └── tikzeng.py │ ├── pyexamples/ │ │ ├── test_simple.py │ │ ├── test_simple.tex │ │ ├── unet.py │ │ └── unet.tex │ └── tikzmake.sh └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: PlotNeuralNet/.gitignore ================================================ *.aux *.log *.gz __pycache__ books project # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ ================================================ FILE: PlotNeuralNet/README.md ================================================ # PlotNeuralNet [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.2526396.svg)](https://doi.org/10.5281/zenodo.2526396) Latex code for drawing neural networks for reports and presentation. Have a look into examples to see how they are made. Additionally, lets consolidate any improvements that you make and fix any bugs to help more people with this code. ## TODO - [X] Python interfaz - [ ] Add easy legend functionality - [ ] Add more layer shapes like TruncatedPyramid, 2DSheet etc - [ ] Add examples for RNN and likes. ## Latex Usage see examples ## PyUsage mkdir my_project cd my_project vim my_arch.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)", 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() bash ../tikzmake.sh my_arch ## Examples Following are some network representations:

FCN-8

FCN-32

Holistically-Nested Edge Detection
================================================ FILE: PlotNeuralNet/examples/HED/HED.tex ================================================ \documentclass[border=15pt, multi, tikz]{standalone} \usepackage{import} \subimport{../../layers/}{init} \usetikzlibrary{positioning} \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\DcnvColor{rgb:blue,5;green,2.5;white,5} \def\SoftmaxColor{rgb:magenta,5;black,7} \def\SumColor{rgb:blue,5;green,15} \def\poolsep{1} \begin{document} \begin{tikzpicture} \tikzstyle{connection}=[ultra thick,every node/.style={sloped,allow upside down},draw=\edgecolor,opacity=0.6] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Layer Blocks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % conv1_1,conv1_2,%pool1 \pic[shift={(0,0,0)}] at (0,0,0) {RightBandedBox={name=cr1,% xlabel={{"64","64"}},zlabel=I,fill=\ConvColor,bandfill=\ConvReluColor,% height=40,width={2,2},depth=40}}; \pic[shift={(\poolsep,0,0)}] at (cr1-east) {Box={name=p1,% fill=\PoolColor,opacity=0.5,height=30,width=1,depth=30}}; % conv2_1,conv2_2,pool2 \pic[shift={(1,0,0)}] at (p1-east) {RightBandedBox={name=cr2,% xlabel={{"64","64"}},zlabel=I/2,fill=\ConvColor,bandfill=\ConvReluColor,% height=30,width={3,3},depth=30}}; \pic[shift={(\poolsep,0,0)}] at (cr2-east) {Box={name=p2,% fill=\PoolColor,opacity=0.5,height=23,width=1,depth=23}}; % conv3_1,conv3_2,pool3 \pic[shift={(1,0,0)}] at (p2-east) {RightBandedBox={name=cr3,% xlabel={{"256","256","256"}},zlabel=I/4,fill=\ConvColor,bandfill=\ConvReluColor,% height=23,width={4,4,4},depth=23}}; \pic[shift={(\poolsep,0,0)}] at (cr3-east) {Box={name=p3,% fill=\PoolColor,opacity=0.5,height=14,width=1,depth=14}}; % conv4_1,conv4_2,conv4_3,pool4 \pic[shift={(1,0,0)}] at (p3-east) {RightBandedBox={name=cr4,% xlabel={{"512","512","512"}},zlabel=I/8,fill=\ConvColor,bandfill=\ConvReluColor,% height=14,width={7,7,7},depth=14}}; \pic[shift={(\poolsep,0,0)}] at (cr4-east) {Box={name=p4,% fill=\PoolColor,opacity=0.5,height=8,width=1,depth=8}}; % conv5_1,conv5_2,conv5_3,pool5 \pic[shift={(1,0,0)}] at (p4-east) {RightBandedBox={name=cr5,% xlabel={{"512","512","512"}},fill=\ConvColor,bandfill=\ConvReluColor,% height=8,width={7,7,7},depth=8}}; %% fc8 -> cr8 (score32) \pic[shift={(0,0,0)}] at (cr5-east) {Box={name=score16,% xlabel={{"K","dummy"}},fill=\ConvColor,% height=8,width=2,depth=8,zlabel=I/16}}; %% Upsampling Deconv Layer %% Dcnv16 \pic[shift={(2.5,0,0)}] at (score16-east) {Box={name=d16,% xlabel={{"","dummy"}},fill=\DcnvColor,opacity=0.7,height=40,width=0.5,depth=40}}; %% Dcnv8 \pic[shift={(.25,0,0)}] at (d16-east) {Box={name=d8,% xlabel={{"","dummy"}},fill=\DcnvColor,opacity=0.7,height=40,width=0.5,depth=40}}; %% Dcnv4 \pic[shift={(.25,0,0)}] at (d8-east) {Box={name=d4,% xlabel={{"","dummy"}},fill=\DcnvColor,opacity=0.7,height=40,width=0.5,depth=40}}; %% Dcnv2 \pic[shift={(.25,0,0)}] at (d4-east) {Box={name=d2,% xlabel={{"","dummy"}},fill=,opacity=0.01,height=40,width=0.5,depth=40}}; %% Dcnv envelope \pic[shift={(-0.2,0,0)}] at (d16-west) {Box={name=env,caption=concatenation of deconvolved feature maps,% xlabel={{"","dummy"}},fill=,opacity=0.2,height=42,width={8},depth=42}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \def\skipshift{6.5} %%Joining with previous streams (fcn-16) %% score16 \pic[shift={(0,0,3+\skipshift)}] at (cr5-anchor) {Box={name=score8,% xlabel={{"K","dummy"}},fill=\ConvColor,height=14,width=2,depth=14,zlabel=I/8}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%Joining with previous streams (fcn-8) %% score8 \pic[shift={(0,0,8+\skipshift)}] at (cr4-east) {Box={name=score4,% xlabel={{"K","dummy"}},fill=\ConvColor,height=23,width=2,depth=23,zlabel=I/4}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%Joining with previous streams (fcn-4) %% score4 \pic[shift={(1,0,13+\skipshift)}] at (cr3-east) {Box={name=score2,% xlabel={{"K","dummy"}},fill=\ConvColor,height=30,width=2,depth=30,zlabel=I/2}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%Joining with previous streams (fcn-2) %% score2 \pic[shift={(0.3,0,22+\skipshift)}] at (p2-east) {Box={name=score,% xlabel={{"K","dummy"}},fill=\ConvColor,height=40,width=2,depth=40,zlabel=I}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Final convolution \pic[shift={(3,0,0)}] at (d2-east) {Box={name=output,% xlabel={{"K","dummy"}},fill=\ConvColor,height=40,width=2,depth=40,zlabel=I}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Draw connections %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \draw [connection] (cr1-east) -- node {\midarrow} (p1-west); \draw [connection] (p1-east) -- node {\midarrow} (cr2-west); \draw [connection] (cr2-east) -- node {\midarrow} (p2-west); \draw [connection] (p2-east) -- node {\midarrow} (cr3-west); \draw [connection] (cr3-east) -- node {\midarrow} (p3-west); \draw [connection] (p3-east) -- node {\midarrow} (cr4-west); \draw [connection] (cr4-east) -- node {\midarrow} (p4-west); \draw [connection] (p4-east) -- node {\midarrow} (cr5-west); \draw [connection] (score16-east) -- node {\midarrow} (d16-west); % \path (cr4-east) -- (p4-west) coordinate[pos=0.4] (after4) ; \draw (d16-near)++(0,0,-1+\skipshift) coordinate (d16h); \draw [connection] (after4) -- node {\midarrow} ++(0,0,3+\skipshift) -- node {\midarrow} (score8-west); \draw [connection] (score8-east) -- node{\midarrow} (d16h) -- node{\midarrow}(d16-near); % \path (cr3-east) -- (p3-west) coordinate[pos=0.4] (after3) ; \draw (d8-near)++(0,0,4+\skipshift) coordinate (d8h); \draw [connection] (after3) -- node {\midarrow} ++(0,0,8+\skipshift) -- node {\midarrow} (score4-west); \draw [connection] (score4-east) -- node{\midarrow} (d8h) -- node{\midarrow}(d8-near); % \path (cr2-east) -- (p2-west) coordinate[pos=0.4] (after2) ; \draw (d4-near)++(0,0,9+\skipshift) coordinate (d4h); \draw [connection] (after2) -- node {\midarrow} ++(0,0,13+\skipshift) -- node {\midarrow} (score2-west); \draw [connection] (score2-east) -- node{\midarrow} (d4h) -- node{\midarrow}(d4-near); % \path (cr1-east) -- (p1-west) coordinate[pos=0.4] (after1) ; \draw (d2-near)++(0,0,18+\skipshift) coordinate (d2h); \draw [connection] (after1) -- node {\midarrow} ++(0,0,22+\skipshift) -- node {\midarrow} (score-west); \draw [connection] (score-east) -- node{\midarrow} (d2h) -- node{\midarrow}(d2-near); \draw [connection] (d2-east) -- node {\midarrow} (output-west); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \end{tikzpicture} \end{document}\grid ================================================ FILE: PlotNeuralNet/examples/SoftmaxLoss/SoftmaxLoss.tex ================================================ \documentclass[border=15pt, multi, tikz]{standalone} \usepackage{import} \subimport{../../layers/}{init} \usetikzlibrary{positioning} \newcommand{\up}{0.25} \newcommand{\down}{0.25} \newcommand{\arrowlength}{4} \begin{document} \begin{tikzpicture} \tikzstyle{connection}=[ultra thick,every node/.style={sloped,allow upside down},draw=\edgecolor,opacity=0.7] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw previous connections %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \draw [connection] (-\arrowlength,\up,0) node[anchor=south west,scale=2.1]{$p(x^{(t)})$} -- node {\midarrow} (0,\up,0); \draw [connection] (0,-\down,0) -- node {\midarrow} ++(-\arrowlength.0,0) node[anchor=north west,inner sep = 10, xshift=-25,scale=2.3] { $\frac{\partial L}{\partial E_\mathcal{S}}\frac{\partial E_\mathcal{S}}{\partial p}$ }; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Layer Blocks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \pic[shift={(0,0,0)}] at (0,0,0) {Box={name=crp1,caption=SoftmaxLoss: $E_\mathcal{S}$ ,% fill={rgb:blue,1.5;red,3.5;green,3.5;white,5},opacity=0.5,height=20,width=7,depth=20}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw next connections %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \draw [connection] (crp1-east)++(0,\up,0) -- node {\midarrow} ++(\arrowlength.0,0) node [anchor=south east,scale=2.1]{$E_\mathcal{S} [p;\theta]$}; \draw [connection] (crp1-east)++(\arrowlength,-\down,0) node[anchor=north east,inner sep = 10, xshift=25,scale=2.3] { $\frac{\partial L}{\partial E_\mathcal{S}} = \lambda_\mathcal{S}$ } -- node {\midarrow} ++(-\arrowlength,0,0); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \end{tikzpicture} \end{document} ================================================ FILE: PlotNeuralNet/examples/Unet/Unet.tex ================================================ \documentclass[border=8pt, multi, tikz]{standalone} %\usepackage{blocks} \usepackage{import} \subimport{../../layers/}{init} \usetikzlibrary{positioning} \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} \newcommand{\copymidarrow}{\tikz \draw[-Stealth,line width =0.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,opacity=0.7] \tikzstyle{copyconnection}=[ultra thick,every node/.style={sloped,allow upside down},draw={rgb:blue,4;red,1;green,1;black,3},opacity=0.7] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Encoder %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % conv1_1,conv1_2 \pic[shift={(0,0,0)}] at (0,0,0) {RightBandedBox={name=cr1,% xlabel={{"64","64"}},zlabel=I,fill=\ConvColor,bandfill=\ConvReluColor,% height=40,width={2,2},depth=40}}; %pool1 \pic[shift={(0,0,0)}] at (cr1-east) {Box={name=p1,% fill=\PoolColor,opacity=0.5,height=32,width=1,depth=32}}; %%%%%%%%%% % conv2_1,conv2_2 \pic[shift={(1,0,0)}] at (p1-east) {RightBandedBox={name=cr2,% xlabel={{"128","128"}},zlabel=I/2,fill=\ConvColor,bandfill=\ConvReluColor,% height=32,width={3.5,3.5},depth=32}}; %pool2 \pic[shift={(0,0,0)}] at (cr2-east) {Box={name=p2,% fill=\PoolColor,opacity=0.5,height=25,width=1,depth=25}}; %%%%%%%%%% % conv3_1,conv3_2 \pic[shift={(0.75,0,0)}] at (p2-east) {RightBandedBox={name=cr3,% xlabel={{"256","256"}},zlabel=I/4,fill=\ConvColor,bandfill=\ConvReluColor,% height=25,width={4.5,4.5},depth=25}}; %pool3 \pic[shift={(0,0,0)}] at (cr3-east) {Box={name=p3,% fill=\PoolColor,opacity=0.5,height=16,width=1,depth=16}}; %%%%%%%%%% % conv4_1,conv4_2,conv4_3 \pic[shift={(0.5,0,0)}] at (p3-east) {RightBandedBox={name=cr4,% xlabel={{"512","512"}},zlabel=I/8,fill=\ConvColor,bandfill=\ConvReluColor,% height=16,width={6,6},depth=16}}; %pool4 \pic[shift={(0,0,0)}] at (cr4-east) {Box={name=p4,% fill=\PoolColor,opacity=0.5,height=8,width=1,depth=8}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Bottleneck %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% conv5_1,conv5_2,conv5_3 \pic[shift={(0.75,0,0)}] at (p4-east) {RightBandedBox={name=cr5,caption=Bottleneck Conv,% xlabel={{"1024","1024"}},zlabel=I/16,fill=\ConvColor,bandfill=\ConvReluColor,% height=8,width={8,8},depth=8}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Decoder %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% unpool4, \pic[shift={(1.2,0,0)}] at (cr5-east) {Box={name=up4,% fill=\UnpoolColor,opacity=0.6,height=16,width=1,depth=16}}; \pic[shift={(0,0,0)}] at (up4-east) {RightBandedBox={name=ucr4,% xlabel={{"512","dummy"}},fill=\ConvColor,bandfill=\ConvReluColor,% height=16,width=6,depth=16}}; \pic[shift={(0,0,0)}] at (ucr4-east) {RightBandedBox={name=cat4,% xlabel={{"512",""}},fill={rgb:white,1;black,3},bandfill={rgb:white,1;black,2},opacity=0.2,height=16,width=6,depth=16}}; \pic[shift={(0,0,0)}] at (cat4-east) {RightBandedBox={name=ucr4a,% xlabel={{"512","512"}},zlabel=I/8,fill=\ConvColor,bandfill=\ConvReluColor,% height=16,width={6,6},depth=16}}; %%%%%%%%%% %% unpool3, \pic[shift={(1.5,0,0)}] at (ucr4a-east) {Box={name=up3,% fill=\UnpoolColor,opacity=0.6,height=25,width=1,depth=25}}; \pic[shift={(0,0,0)}] at (up3-east) {RightBandedBox={name=ucr3,% xlabel={{"256","dummy"}},fill=\ConvColor,bandfill=\ConvReluColor,% height=25,width=4.5,depth=25}}; \pic[shift={(0,0,0)}] at (ucr3-east) {RightBandedBox={name=cat3,% xlabel={{"256",""}},fill={rgb:white,1;black,3},bandfill={rgb:white,1;black,2},opacity=0.2,height=25,width=4.5,depth=25}}; \pic[shift={(0,0,0)}] at (cat3-east) {RightBandedBox={name=ucr3a,% xlabel={{"256","256"}},zlabel=I/4,fill=\ConvColor,bandfill=\ConvReluColor,% height=25,width={4.5,4.5},depth=25}}; %%%%%%%%%% %% unpool2, \pic[shift={(1,0,0)}] at (ucr3a-east) {Box={name=up2,% fill=\UnpoolColor,opacity=0.6,height=32,width=1,depth=32}}; \pic[shift={(0,0,0)}] at (up2-east) {RightBandedBox={name=ucr2,% xlabel={{"128","dummy"}},fill=\ConvColor,bandfill=\ConvReluColor,% height=32,width=3.5,depth=32}}; \pic[shift={(0,0,0)}] at (ucr2-east) {RightBandedBox={name=cat2,% xlabel={{"128",""}},fill={rgb:white,1;black,3},bandfill={rgb:white,1;black,2},opacity=0.2,height=32,width=3.5,depth=32}}; \pic[shift={(0,0,0)}] at (cat2-east) {RightBandedBox={name=ucr2a,% xlabel={{"128","128"}},zlabel=I/2,fill=\ConvColor,bandfill=\ConvReluColor,% height=32,width={3.5,3.5},depth=32}}; %%%%%%%%%% %% unpool1, \pic[shift={(1.5,0,0)}] at (ucr2a-east) {Box={name=up1,% fill=\UnpoolColor,opacity=0.6,height=40,width=1,depth=40}}; \pic[shift={(0,0,0)}] at (up1-east) {RightBandedBox={name=ucr1,% xlabel={{"64","dummy"}},fill=\ConvColor,bandfill=\ConvReluColor,% height=40,width=2.5,depth=40}}; \pic[shift={(0,0,0)}] at (ucr1-east) {RightBandedBox={name=cat1,% xlabel={{"64",""}},fill={rgb:white,1;black,3},bandfill={rgb:white,1;black,2},opacity=0.2,height=40,width=2.5,depth=40}}; \pic[shift={(0,0,0)}] at (cat1-east) {RightBandedBox={name=ucr1a,% xlabel={{"64","64"}},fill=\ConvColor,bandfill=\ConvReluColor,% height=40,width={2.5,2.5},depth=40}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Classifier %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \pic[shift={(0.75,0,0)}] at (ucr1a-east) {Box={name=out,caption=Softmax,% zlabel=I,fill=\SoftmaxColor,height=40,width=1,depth=40}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Draw connections %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \draw [connection] (p1-east) -- node {\midarrow} (cr2-west); \draw [connection] (p2-east) -- node {\midarrow} (cr3-west); \draw [connection] (p3-east) -- node {\midarrow} (cr4-west); \draw [connection] (p4-east) -- node {\midarrow} (cr5-west); \draw [connection] (cr5-east) -- node {\midarrow} (up4-west); \draw [connection] (ucr4a-east) -- node {\midarrow} (up3-west); \draw [connection] (ucr3a-east) -- node {\midarrow} (up2-west); \draw [connection] (ucr2a-east) -- node {\midarrow} (up1-west); \draw [connection] (ucr1a-east) -- node {\midarrow} (out-west); %\draw [connection] (out-east) -- node {\midarrow} ++(2,0,0); \path (cr4-southeast) -- (cr4-northeast) coordinate[pos=1.25] (cr4-top) ; \path (cr3-southeast) -- (cr3-northeast) coordinate[pos=1.25] (cr3-top) ; \path (cr2-southeast) -- (cr2-northeast) coordinate[pos=1.25] (cr2-top) ; \path (cr1-southeast) -- (cr1-northeast) coordinate[pos=1.25] (cr1-top) ; \path (cat4-south) -- (cat4-north) coordinate[pos=1.25] (cat4-top) ; \path (cat3-south) -- (cat3-north) coordinate[pos=1.25] (cat3-top) ; \path (cat2-south) -- (cat2-north) coordinate[pos=1.25] (cat2-top) ; \path (cat1-south) -- (cat1-north) coordinate[pos=1.25] (cat1-top) ; % \draw [copyconnection] (cr4-northeast) -- node {\copymidarrow}(cr4-top) -- node {\copymidarrow}(cat4-top) -- node {\copymidarrow} (cat4-north); % \draw [copyconnection] (cr3-northeast) -- node {\copymidarrow}(cr3-top) -- node {\copymidarrow}(cat3-top) -- node {\copymidarrow} (cat3-north); % \draw [copyconnection] (cr2-northeast) -- node {\copymidarrow}(cr2-top) -- node {\copymidarrow}(cat2-top) -- node {\copymidarrow} (cat2-north); % \draw [copyconnection] (cr1-northeast) -- node {\copymidarrow}(cr1-top) -- node {\copymidarrow}(cat1-top) -- node {\copymidarrow} (cat1-north); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \end{tikzpicture} \end{document} ================================================ FILE: PlotNeuralNet/examples/Unet_Ushape/Unet_ushape.tex ================================================ \documentclass[border=8pt, multi, tikz]{standalone} %\usepackage{blocks} \usepackage{import} \subimport{../../layers/}{init} \usetikzlibrary{positioning} \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\ConcatColor{rgb:blue,5;red,2.5;white,5} \def\FcReluColor{rgb:blue,5;red,5;white,4} \def\SoftmaxColor{rgb:magenta,5;black,7} \newcommand{\copymidarrow}{\tikz \draw[-Stealth,line width =0.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,opacity=0.7] \tikzstyle{copyconnection}=[ultra thick,every node/.style={sloped,allow upside down},draw={rgb:blue,4;red,1;green,1;black,3},opacity=0.7] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Encoder %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % conv1_1,conv1_2 \pic[shift={(0,0,0)}] at (0,0,0) {RightBandedBox={name=cr1,% xlabel={{"64","64"}},caption=I,fill=\ConvColor,bandfill=\ConvReluColor,% height=40,width={2,2},depth=40}}; %pool1 \pic[shift={(1.2,-10,0)}] at (cr1-east) {Box={name=p1,% fill=\PoolColor,opacity=0.6,height=32,width=1,depth=32}}; %%%%%%%%%% % conv2_1,conv2_2 \pic[shift={(0,0,0)}] at (p1-east) {RightBandedBox={name=cr2,% xlabel={{"128","128"}},caption=I/2,fill=\ConvColor,bandfill=\ConvReluColor,% height=32,width={3.5,3.5},depth=32}}; %pool2 \pic[shift={(1.2,-8.5,0)}] at (cr2-east) {Box={name=p2,% fill=\PoolColor,opacity=0.6,height=25,width=1,depth=25}}; %%%%%%%%%% % conv3_1,conv3_2 \pic[shift={(0,0,0)}] at (p2-east) {RightBandedBox={name=cr3,% xlabel={{"256","256"}},caption=I/4,fill=\ConvColor,bandfill=\ConvReluColor,% height=25,width={4.5,4.5},depth=25}}; %pool3 \pic[shift={(1.2,-6.5,0)}] at (cr3-east) {Box={name=p3,% fill=\PoolColor,opacity=0.6,height=16,width=1,depth=16}}; %%%%%%%%%% % conv4_1,conv4_2,conv4_3 \pic[shift={(0,0,0)}] at (p3-east) {RightBandedBox={name=cr4,% xlabel={{"512","512"}},caption=I/8,fill=\ConvColor,bandfill=\ConvReluColor,% height=16,width={6,6},depth=16}}; %pool4 \pic[shift={(1.2,-3,0)}] at (cr4-east) {Box={name=p4,% fill=\PoolColor,opacity=0.6,height=8,width=1,depth=8}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Bottleneck %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% conv5_1,conv5_2,conv5_3 \pic[shift={(0,0,0)}] at (p4-east) {RightBandedBox={name=cr5,caption=I/16,% xlabel={{"1024","1024"}},fill=\ConvColor,bandfill=\ConvReluColor,% height=8,width={8,8},depth=8}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Decoder %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% unpool4, \pic[shift={(0,0,0)}] at (cr5-east) {Box={name=up4,% fill=\UnpoolColor,opacity=0.6,height=16,width=1,depth=16}}; \pic[shift={(0,0,0)}] at (up4-east) {RightBandedBox={name=ucr4,% xlabel={{"512","dummy"}},caption=I/8,fill=\ConvColor,bandfill=\ConvReluColor,% height=16,width=6,depth=16}}; \pic[shift={(0,3,0)}] at (ucr4-anchor) {Ball={name=cat4,fill=\ConcatColor,radius=2.5,logo=$||$}}; \pic[shift={(1.4,0,0)}] at (cat4-east) {RightBandedBox={name=ucr4a,% xlabel={{"512","512"}},caption=I/8,fill=\ConvColor,bandfill=\ConvReluColor,% height=16,width={6,6},depth=16}}; %%%%%%%%%% %% unpool3, \pic[shift={(0,0,0)}] at (ucr4a-east) {Box={name=up3,% fill=\UnpoolColor,opacity=0.6,height=25,width=1,depth=25}}; \pic[shift={(0,0,0)}] at (up3-east) {RightBandedBox={name=ucr3,% xlabel={{"256","dummy"}},caption=I/4,fill=\ConvColor,bandfill=\ConvReluColor,% height=25,width=4.5,depth=25}}; \pic[shift={(0,6.5,0)}] at (ucr3-anchor) {Ball={name=cat3,fill=\ConcatColor,radius=2.5,logo=$||$}}; \pic[shift={(1.5,0,0)}] at (cat3-east) {RightBandedBox={name=ucr3a,% xlabel={{"256","256"}},caption=I/4,fill=\ConvColor,bandfill=\ConvReluColor,% height=25,width={4.5,4.5},depth=25}}; %%%%%%%%%% %% unpool2, \pic[shift={(0,0,0)}] at (ucr3a-east) {Box={name=up2,% fill=\UnpoolColor,opacity=0.6,height=32,width=1,depth=32}}; \pic[shift={(0,0,0)}] at (up2-east) {RightBandedBox={name=ucr2,% xlabel={{"128","dummy"}},caption=I/2,fill=\ConvColor,bandfill=\ConvReluColor,% height=32,width=3.5,depth=32}}; \pic[shift={(0,8.5,0)}] at (ucr2-anchor) {Ball={name=cat2,fill=\ConcatColor,radius=2.5,logo=$||$}}; \pic[shift={(1.8,0,0)}] at (cat2-east) {RightBandedBox={name=ucr2a,% xlabel={{"128","128"}},caption=I/2,fill=\ConvColor,bandfill=\ConvReluColor,% height=32,width={3.5,3.5},depth=32}}; %%%%%%%%%% %% unpool1, \pic[shift={(0,0,0)}] at (ucr2a-east) {Box={name=up1,% fill=\UnpoolColor,opacity=0.6,height=40,width=1,depth=40}}; \pic[shift={(0,0,0)}] at (up1-east) {RightBandedBox={name=ucr1,% xlabel={{"64","dummy"}},caption=I,fill=\ConvColor,bandfill=\ConvReluColor,% height=40,width=2.5,depth=40}}; \pic[shift={(0,10,0)}] at (ucr1-anchor) {Ball={name=cat1,fill=\ConcatColor,radius=2.5,logo=$||$}}; \pic[shift={(2,0,0)}] at (cat1-east) {RightBandedBox={name=ucr1a,% xlabel={{"64","64"}},caption=I,fill=\ConvColor,bandfill=\ConvReluColor,% height=40,width={2.5,2.5},depth=40}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Classifier %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \pic[shift={(2,0,0)}] at (ucr1a-east) {Box={name=out,caption=Softmax,% zlabel=I,fill=\SoftmaxColor,height=40,width=1,depth=40}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Draw connections %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \path (cr1-east) -- (p1-west|-cr1-west) coordinate[pos=0.5] (crp1-mid) ; \path (cr2-east) -- (p2-west|-cr2-west) coordinate[pos=0.5] (crp2-mid) ; \path (cr3-east) -- (p3-west|-cr3-west) coordinate[pos=0.5] (crp3-mid) ; \path (cr4-east) -- (p4-west|-cr4-west) coordinate[pos=0.5] (crp4-mid) ; \draw[connection](cr1-east)--node{\midarrow}(crp1-mid)--node{\midarrow}(p1-west-|crp1-mid)--node{\midarrow}(p1-west); \draw[connection](cr2-east)--node{\midarrow}(crp2-mid)--node{\midarrow}(p2-west-|crp2-mid)--node{\midarrow}(p2-west); \draw[connection](cr3-east)--node{\midarrow}(crp3-mid)--node{\midarrow}(p3-west-|crp3-mid)--node{\midarrow}(p3-west); \draw[connection](cr4-east)--node{\midarrow}(crp4-mid)--node{\midarrow}(p4-west-|crp4-mid)--node{\midarrow}(p4-west); %\draw [connection] (cr5-east) -- node {\midarrow} (up4-west); %\draw [connection] (ucr4a-east) -- node {\midarrow} (up3-west); %\draw [connection] (ucr3a-east) -- node {\midarrow} (up2-west); %\draw [connection] (ucr2a-east) -- node {\midarrow} (up1-west); \draw [connection] (ucr1a-east) -- node {\midarrow} (out-west); %\draw [connection] (out-east) -- node {\midarrow} ++(2,0,0); \draw [copyconnection] (cr4-east) -- node {\copymidarrow} (cat4-west); \draw [copyconnection] (cr3-east) -- node {\copymidarrow} (cat3-west); \draw [copyconnection] (cr2-east) -- node {\copymidarrow} (cat2-west); \draw [copyconnection] (cr1-east) -- node {\copymidarrow} (cat1-west); \draw [copyconnection] (cat4-east) -- node {\copymidarrow} (ucr4a-west); \draw [copyconnection] (cat3-east) -- node {\copymidarrow} (ucr3a-west); \draw [copyconnection] (cat2-east) -- node {\copymidarrow} (ucr2a-west); \draw [copyconnection] (cat1-east) -- node {\copymidarrow} (ucr1a-west); \draw [copyconnection] (ucr4-north) -- node {\copymidarrow} (cat4-south); \draw [copyconnection] (ucr3-north) -- node {\copymidarrow} (cat3-south); \draw [copyconnection] (ucr2-north) -- node {\copymidarrow} (cat2-south); \draw [copyconnection] (ucr1-north) -- node {\copymidarrow} (cat1-south); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \end{tikzpicture} \end{document} ================================================ FILE: PlotNeuralNet/examples/VGG16/vgg16.tex ================================================ \documentclass[border=15pt, multi, tikz]{standalone} %\usepackage{blocks} \usepackage{import} \subimport{../../layers/}{init} \usetikzlibrary{positioning} \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\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} \begin{document} \begin{tikzpicture} \tikzstyle{connection}=[ultra thick,every node/.style={sloped,allow upside down},draw=\edgecolor,opacity=0.7] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Layer Blocks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % conv1_1,conv1_2 \pic[shift={(0,0,0)}] at (0,0,0) {RightBandedBox={name=cr1,caption=conv1,% xlabel={{"64","64"}},ylabel=224,zlabel=224,fill=\ConvColor,bandfill=\ConvReluColor,% height=40,width={2,2},depth=40}}; %pool1 \pic[shift={(0,0,0)}] at (cr1-east) {Box={name=p1,% fill=\PoolColor,opacity=0.5,height=35,width=1,depth=35}}; %%%%%%%%%% % conv2_1,conv2_2 \pic[shift={(2,0,0)}] at (p1-east) {RightBandedBox={name=cr2,caption=conv2,% xlabel={{"64","64"}},zlabel=112,fill=\ConvColor,bandfill=\ConvReluColor,% height=35,width={3,3},depth=35}}; %pool2 \pic[shift={(0,0,0)}] at (cr2-east) {Box={name=p2,% fill=\PoolColor,opacity=0.5,height=30,width=1,depth=30}}; %%%%%%%%%% % conv3_1,conv3_2 \pic[shift={(2,0,0)}] at (p2-east) {RightBandedBox={name=cr3,caption=conv3,% xlabel={{"256","256","256"}},zlabel=56,fill=\ConvColor,bandfill=\ConvReluColor,% height=30,width={4,4,4},depth=30}}; %pool3 \pic[shift={(0,0,0)}] at (cr3-east) {Box={name=p3,% fill=\PoolColor,opacity=0.5,height=23,width=1,depth=23}}; %%%%%%%%%% % conv4_1,conv4_2,conv4_3 \pic[shift={(1.8,0,0)}] at (p3-east) {RightBandedBox={name=cr4,caption=conv4,% xlabel={{"512","512","512"}},zlabel=28,fill=\ConvColor,bandfill=\ConvReluColor,% height=23,width={7,7,7},depth=23}}; %pool4 \pic[shift={(0,0,0)}] at (cr4-east) {Box={name=p4,% fill=\PoolColor,opacity=0.5,height=15,width=1,depth=15}}; %%%%%%%%%% % conv5_1,conv5_2,conv5_3 \pic[shift={(1.5,0,0)}] at (p4-east) {RightBandedBox={name=cr5,caption=conv5,% xlabel={{"512","512","512"}},zlabel=14,fill=\ConvColor,bandfill=\ConvReluColor,% height=15,width={7,7,7},depth=15}}; %pool5 \pic[shift={(0,0,0)}] at (cr5-east) {Box={name=p5,% fill=\PoolColor,opacity=0.5,height=10,width=1,depth=10}}; %%%%%%%%%% % fc6 \pic[shift={(3,0,0)}] at (p5-east) {RightBandedBox={name=fc6,caption=fc6,% xlabel={{"1",""}},zlabel=4096,fill=\FcColor,bandfill=\FcReluColor,% height=3,width=3,depth=100}}; %%%%%%%%%% % fc7 \pic[shift={(2,0,0)}] at (fc6-east) {RightBandedBox={name=fc7,caption=fc7,% xlabel={{"1","dummy"}},zlabel=4096,fill=\FcColor,bandfill=\FcReluColor,% height=3,width=3,depth=100}}; %%%%%%%%%% % fc8 \pic[shift={(1.5,0,0)}] at (fc7-east) {RightBandedBox={name=fc8,caption=fc8+softmax,% xlabel={{"1","dummy"}},fill=\FcColor,bandfill=\FcReluColor,% height=3,width=3,depth=25}}; %%%%%%%%%% % softmax \pic[shift={(0,0,0)}] at (fc8-east) {Box={name=softmax,% xlabel={{"","dummy"}},zlabel=K,opacity=0.8,fill=\SoftmaxColor,% height=3,width=1.5,depth=25}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Arrow Connections %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \draw [connection] (p1-east) -- node {\midarrow} (cr2-west); \draw [connection] (p2-east) -- node {\midarrow} (cr3-west); \draw [connection] (p3-east) -- node {\midarrow} (cr4-west); \draw [connection] (p4-east) -- node {\midarrow} (cr5-west); \draw [connection] (p5-east) -- node {\midarrow} (fc6-west); \draw [connection] (fc6-east) -- node {\midarrow} (fc7-west); \draw [connection] (fc7-east) -- node {\midarrow} (fc8-west); \draw [connection] (softmax-east) -- node {\midarrow} ++(1.5,0,0); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Dotted Edges %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \draw[densely dashed] (fc6-west)++(0, 1.5*.2, 1.5*.2) coordinate(a) -- (p5-nearnortheast) (fc6-west)++(0,-1.5*.2, 1.5*.2) coordinate(b) -- (p5-nearsoutheast) (fc6-west)++(0,-1.5*.2,-1.5*.2) coordinate(c) -- (p5-farsoutheast) (fc6-west)++(0, 1.5*.2,-1.5*.2) coordinate(d) -- (p5-farnortheast) (a)--(b)--(c)--(d) ; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \end{tikzpicture} \end{document} ================================================ FILE: PlotNeuralNet/examples/fcn32s/fcn32.tex ================================================ \documentclass[border=15pt, multi, tikz]{standalone} \usepackage{import} \subimport{../../layers/}{init} \usetikzlibrary{positioning} \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\DcnvColor{rgb:blue,5;green,2.5;white,5} \def\SoftmaxColor{rgb:magenta,5;black,7} \begin{document} \begin{tikzpicture} \tikzstyle{connection}=[ultra thick,every node/.style={sloped,allow upside down},draw=\edgecolor,opacity=0.7] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Layer Blocks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % conv1_1,conv1_2 \pic[shift={(0,0,0)}] at (0,0,0) {RightBandedBox={name=cr1,caption=conv1,% xlabel={{"64","64"}},zlabel=I,fill=\ConvColor,bandfill=\ConvReluColor,% height=40,width={2,2},depth=40}}; %pool1 \pic[shift={(0,0,0)}] at (cr1-east) {Box={name=p1,% fill=\PoolColor,opacity=0.5,height=35,width=1,depth=35}}; %%%%%%%%%% % conv2_1,conv2_2 \pic[shift={(2,0,0)}] at (p1-east) {RightBandedBox={name=cr2,caption=conv2,% xlabel={{"64","64"}},zlabel=I/2,fill=\ConvColor,bandfill=\ConvReluColor,% height=35,width={3,3},depth=35}}; %pool2 \pic[shift={(0,0,0)}] at (cr2-east) {Box={name=p2,% fill=\PoolColor,opacity=0.5,height=30,width=1,depth=30}}; %%%%%%%%%% % conv3_1,conv3_2 \pic[shift={(2,0,0)}] at (p2-east) {RightBandedBox={name=cr3,caption=conv3,% xlabel={{"256","256","256"}},zlabel=I/4,fill=\ConvColor,bandfill=\ConvReluColor,% height=30,width={4,4,4},depth=30}}; %pool3 \pic[shift={(0,0,0)}] at (cr3-east) {Box={name=p3,% fill=\PoolColor,opacity=0.5,height=23,width=1,depth=23}}; %%%%%%%%%% % conv4_1,conv4_2,conv4_3 \pic[shift={(1.8,0,0)}] at (p3-east) {RightBandedBox={name=cr4,caption=conv4,% xlabel={{"512","512","512"}},zlabel=I/8,fill=\ConvColor,bandfill=\ConvReluColor,% height=23,width={7,7,7},depth=23}}; %pool4 \pic[shift={(0,0,0)}] at (cr4-east) {Box={name=p4,% fill=\PoolColor,opacity=0.5,height=15,width=1,depth=15}}; %%%%%%%%%% % conv5_1,conv5_2,conv5_3 \pic[shift={(1.5,0,0)}] at (p4-east) {RightBandedBox={name=cr5,caption=conv5,% xlabel={{"512","512","512"}},zlabel=I/16,fill=\ConvColor,bandfill=\ConvReluColor,% height=15,width={7,7,7},depth=15}}; %pool5 \pic[shift={(0,0,0)}] at (cr5-east) {Box={name=p5,% fill=\PoolColor,opacity=0.5,height=10,width=1,depth=10}}; %%%%%%%%%% %% fc6, fc7 -> cr6, cr7 \pic[shift={(1,0,0)}] at (p5-east) {RightBandedBox={name=cr6_7,caption=fc to conv,% xlabel={{"4096","4096"}},fill=\ConvColor,bandfill=\ConvReluColor,% height=10,width={10,10},depth=10}}; %%%%%%%%%% %% fc8 -> cr8 \pic[shift={(1,0,0)}] at (cr6_7-east) {Box={name=c8,caption=fc8 to conv,% xlabel={{"K","dummy"}},fill=\ConvColor,% height=10,width=2,depth=10,zlabel=I/32}}; %%%%%%%%%% %% Dcnv32 \pic[shift={(2.5,0,0)}] at (c8-east) {Box={name=d32,caption=Deconv,% xlabel={{"K","dummy"}},fill=\DcnvColor,% height=40,width=2,depth=40}}; %%%%%%%%%% %% softmax \pic[shift={(1,0,0)}] at (d32-east) {Box={name=softmax,caption=softmax,% xlabel={{"K","dummy"}},fill=\SoftmaxColor,% height=40,width=2,depth=40,zlabel=I}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw connections %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \draw [connection] (p1-east) -- node {\midarrow} (cr2-west); \draw [connection] (p2-east) -- node {\midarrow} (cr3-west); \draw [connection] (p3-east) -- node {\midarrow} (cr4-west); \draw [connection] (p4-east) -- node {\midarrow} (cr5-west); \draw [connection] (p5-east) -- node {\midarrow} (cr6_7-west); \draw [connection] (cr6_7-east) -- node {\midarrow} (c8-west); \draw [connection] (c8-east) -- node {\midarrow} (d32-west); \draw [connection] (d32-east) -- node {\midarrow} (softmax-west); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Dotted Edges %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \draw[densely dashed] (c8-nearnortheast) -- (d32-nearnorthwest) (c8-nearsoutheast) -- (d32-nearsouthwest) (c8-farsoutheast) -- (d32-farsouthwest) (c8-farnortheast) -- (d32-farnorthwest) ; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \end{tikzpicture} \end{document}\grid ================================================ FILE: PlotNeuralNet/examples/fcn8s/fcn8.tex ================================================ \documentclass[border=15pt, multi, tikz]{standalone} \usepackage{import} \subimport{../../layers/}{init} \usetikzlibrary{positioning} \usetikzlibrary{3d} %for including external image \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\DcnvColor{rgb:blue,5;green,2.5;white,5} \def\SoftmaxColor{rgb:magenta,5;black,7} \def\SumColor{rgb:blue,5;green,15} \begin{document} \begin{tikzpicture} \tikzstyle{connection}=[ultra thick,every node/.style={sloped,allow upside down},draw=\edgecolor,opacity=0.7] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw Layer Blocks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \node[canvas is zy plane at x=0] (temp) at (-3,0,0) {\includegraphics[width=8cm,height=8cm]{cats.jpg}}; % conv1_1,conv1_2,%pool1 \pic[shift={(0,0,0)}] at (0,0,0) {RightBandedBox={name=cr1,caption=conv1,% xlabel={{"64","64"}},zlabel=I,fill=\ConvColor,bandfill=\ConvReluColor,% height=40,width={2,2},depth=40}}; \pic[shift={(0,0,0)}] at (cr1-east) {Box={name=p1,% fill=\PoolColor,opacity=0.5,height=35,width=1,depth=35}}; % conv2_1,conv2_2,pool2 \pic[shift={(2,0,0)}] at (p1-east) {RightBandedBox={name=cr2,caption=conv2,% xlabel={{"64","64"}},zlabel=I/2,fill=\ConvColor,bandfill=\ConvReluColor,% height=35,width={3,3},depth=35}}; \pic[shift={(0,0,0)}] at (cr2-east) {Box={name=p2,% fill=\PoolColor,opacity=0.5,height=30,width=1,depth=30}}; % conv3_1,conv3_2,pool3 \pic[shift={(2,0,0)}] at (p2-east) {RightBandedBox={name=cr3,caption=conv3,% xlabel={{"256","256","256"}},zlabel=I/4,fill=\ConvColor,bandfill=\ConvReluColor,% height=30,width={4,4,4},depth=30}}; \pic[shift={(0,0,0)}] at (cr3-east) {Box={name=p3,% fill=\PoolColor,opacity=0.5,height=23,width=1,depth=23}}; % conv4_1,conv4_2,conv4_3,pool4 \pic[shift={(1.8,0,0)}] at (p3-east) {RightBandedBox={name=cr4,caption=conv4,% xlabel={{"512","512","512"}},zlabel=I/8,fill=\ConvColor,bandfill=\ConvReluColor,% height=23,width={7,7,7},depth=23}}; \pic[shift={(0,0,0)}] at (cr4-east) {Box={name=p4,% fill=\PoolColor,opacity=0.5,height=15,width=1,depth=15}}; % conv5_1,conv5_2,conv5_3,pool5 \pic[shift={(1.5,0,0)}] at (p4-east) {RightBandedBox={name=cr5,caption=conv5,% xlabel={{"512","512","512"}},zlabel=I/16,fill=\ConvColor,bandfill=\ConvReluColor,% height=15,width={7,7,7},depth=15}}; \pic[shift={(0,0,0)}] at (cr5-east) {Box={name=p5,% fill=\PoolColor,opacity=0.5,height=10,width=1,depth=10}}; %% fc6, fc7 -> cr6, cr7 \pic[shift={(1,0,0)}] at (p5-east) {RightBandedBox={name=cr6_7,caption=fc to conv,% xlabel={{"4096","4096"}},fill=\ConvColor,bandfill=\ConvReluColor,% height=10,width={10,10},depth=10}}; %% fc8 -> cr8 (score32) \pic[shift={(1,0,0)}] at (cr6_7-east) {Box={name=score32,caption=fc8 to conv,% xlabel={{"K","dummy"}},fill=\ConvColor,% height=10,width=2,depth=10,zlabel=I/32}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%Joining with previous streams (fcn-16s) %% Upsampling Deconv Layer %% Dcnv32 \pic[shift={(1.5,0,0)}] at (score32-east) {Box={name=d32,% xlabel={{"K","dummy"}},fill=\DcnvColor,% height=15,width=2,depth=15,zlabel=I/16}}; %% score16 \pic[shift={(0,-4,0)}] at (d32-west) {Box={name=score16,% xlabel={{"K","dummy"}},fill=\ConvColor,% height=15,width=2,depth=15,zlabel=I/16}}; %% Elementwise sum between score16 and up32 \pic[shift={(1.5,0,0)}] at (d32-east) {Ball={name=elt1,% fill=\SumColor,opacity=0.6,% radius=2.5,logo=$+$}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%Joining with previous streams (fcn-8s) %% Upsampling Deconv Layer %% Dcnv16 \pic[shift={(1.5,0,0)}] at (elt1-east) {Box={name=d16,% xlabel={{"K","dummy"}},fill=\DcnvColor,% height=23,width=2,depth=23,zlabel=I/8}}; %% score8 \pic[shift={(0,-6,0)}] at (d16-west) {Box={name=score8,% xlabel={{"K","dummy"}},fill=\ConvColor,% height=23,width=2,depth=23,zlabel=I/8}}; %% Elementwise sum between score16 and up32 \pic[shift={(1.5,0,0)}] at (d16-east) {Ball={name=elt2,% fill=\SumColor,opacity=0.6,% radius=2.5,logo=$+$}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Output %%%%%%%%%% %% Dcnv8 \pic[shift={(2.5,0,0)}] at (elt2-east) {Box={name=d8,% xlabel={{"K","dummy"}},fill=\DcnvColor,% height=40,width=2,depth=40}}; %%%%%%%%%% %% softmax \pic[shift={(1,0,0)}] at (d8-east) {Box={name=softmax,caption=softmax,% xlabel={{"K","dummy"}},fill=\SoftmaxColor,% height=40,width=2,depth=40,zlabel=I}}; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Draw connections %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \draw [connection] (p1-east) -- node {\midarrow} (cr2-west); \draw [connection] (p2-east) -- node {\midarrow} (cr3-west); \draw [connection] (p3-east) -- node {\midarrow} (cr4-west); \draw [connection] (p4-east) -- node {\midarrow} (cr5-west); \draw [connection] (p5-east) -- node {\midarrow} (cr6_7-west); \draw [connection] (cr6_7-east) -- node {\midarrow} (score32-west); \draw [connection] (score32-east) -- node {\midarrow} (d32-west); \path (p4-east) -- (cr5-west) coordinate[pos=0.25] (between4_5) ; \draw [connection] (between4_5) -- node {\midarrow} (score16-west-|between4_5) -- node {\midarrow} (score16-west); \draw [connection] (d32-east) -- node {\midarrow} (elt1-west); \draw [connection] (score16-east) -- node {\midarrow} (score16-east -| elt1-south) -- node {\midarrow} (elt1-south); \draw [connection] (elt1-east) -- node {\midarrow} (d16-west); \path (p3-east) -- (cr4-west) coordinate[pos=0.25] (between3_4) ; \draw [connection] (between3_4) -- node {\midarrow} (score8-west-|between3_4) -- node {\midarrow} (score8-west); \draw [connection] (d16-east) -- node {\midarrow} (elt2-west); \draw [connection] (score8-east) -- node {\midarrow} (score8-east -| elt2-south)-- node {\midarrow} (elt2-south); \draw [connection] (elt2-east) -- node {\midarrow} (d8-west); \draw [connection] (d8-east) -- node{\midarrow} (softmax-west); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \end{tikzpicture} \end{document}\grid ================================================ FILE: PlotNeuralNet/layers/Ball.sty ================================================ \ProvidesPackage{Ball} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %This Block can draw small Ball %Elementwise or reduction operations can be drawn with this %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \tikzset{Ball/.pic={\tikzset{/sphere/.cd,#1} \pgfmathsetmacro{\r}{\radius*\scale} \shade[ball color=\fill,opacity=\opacity] (0,0,0) circle (\r); \draw (0,0,0) circle [radius=\r] node[scale=4*\r] {\logo}; \coordinate (\name-anchor) at ( 0 , 0 , 0) ; \coordinate (\name-east) at ( \r, 0 , 0) ; \coordinate (\name-west) at (-\r, 0 , 0) ; \coordinate (\name-north) at ( 0 , \r , 0) ; \coordinate (\name-south) at ( 0 , -\r, 0) ; \path (\name-south) + (0,-20pt) coordinate (caption-node) edge ["\textcolor{black}{\bf \caption}"'] (caption-node); %Ball caption }, /sphere/.search also={/tikz}, /sphere/.cd, radius/.store in=\radius, scale/.store in=\scale, caption/.store in=\caption, name/.store in=\name, fill/.store in=\fill, logo/.store in=\logo, opacity/.store in=\opacity, logo=$\Sigma$, fill=green, opacity=0.10, scale=0.2, radius=0.5, caption=, name=, } ================================================ FILE: PlotNeuralNet/layers/Box.sty ================================================ \ProvidesPackage{Box} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This Block can draw simple block of boxes with custom colors. % Can be used for conv, deconv etc %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \tikzset{Box/.pic={\tikzset{/boxblock/.cd,#1} \tikzstyle{box}=[every edge/.append style={pic actions, densely dashed, opacity=.7},fill opacity=\opacity, pic actions,fill=\fill] \pgfmathsetmacro{\y}{\cubey*\scale} \pgfmathsetmacro{\z}{\cubez*\scale} %Multiple concatenated boxes \foreach[count=\i,% evaluate=\i as \xlabel using {array({\boxlabels},\i-1)},% evaluate=\unscaledx as \k using {\unscaledx*\scale+\prev}, remember=\k as \prev (initially 0)] \unscaledx in \cubex { \pgfmathsetmacro{\x}{\unscaledx*\scale} \coordinate (a) at (\k-\x , \y/2 , \z/2); \coordinate (b) at (\k-\x ,-\y/2 , \z/2); \coordinate (c) at (\k ,-\y/2 , \z/2); \coordinate (d) at (\k , \y/2 , \z/2); \coordinate (e) at (\k , \y/2 ,-\z/2); \coordinate (f) at (\k ,-\y/2 ,-\z/2); \coordinate (g) at (\k-\x ,-\y/2 ,-\z/2); \coordinate (h) at (\k-\x , \y/2 ,-\z/2); \draw [box] (d) -- (a) -- (b) -- (c) -- cycle (d) -- (a) -- (h) -- (e) -- cycle %dotted edges (f) edge (g) (b) edge (g) (h) edge (g) ; \path (b) edge ["\xlabel"',midway] (c); \xdef\LastEastx{\k} %\k persists as \LastEastx after loop }%Loop ends \draw [box] (d) -- (e) -- (f) -- (c) -- cycle; %East face of last box \coordinate (a1) at (0 , \y/2 , \z/2); \coordinate (b1) at (0 ,-\y/2 , \z/2); \tikzstyle{depthlabel}=[pos=0,text width=14*\z,text centered,sloped] \path (c) edge ["\small\zlabel"',depthlabel](f); %depth label \path (b1) edge ["\ylabel",midway] (a1); %height label \tikzstyle{captionlabel}=[text width=15*\LastEastx/\scale,text centered] \path (\LastEastx/2,-\y/2,+\z/2) + (0,-25pt) coordinate (cap) edge ["\textcolor{black}{ \bf \caption}"',captionlabel](cap) ; %Block caption/pic object label %Define nodes to be used outside on the pic object \coordinate (\name-west) at (0,0,0) ; \coordinate (\name-east) at (\LastEastx, 0,0) ; \coordinate (\name-north) at (\LastEastx/2,\y/2,0); \coordinate (\name-south) at (\LastEastx/2,-\y/2,0); \coordinate (\name-anchor) at (\LastEastx/2, 0,0) ; \coordinate (\name-near) at (\LastEastx/2,0,\z/2); \coordinate (\name-far) at (\LastEastx/2,0,-\z/2); \coordinate (\name-nearwest) at (0,0,\z/2); \coordinate (\name-neareast) at (\LastEastx,0,\z/2); \coordinate (\name-farwest) at (0,0,-\z/2); \coordinate (\name-fareast) at (\LastEastx,0,-\z/2); \coordinate (\name-northeast) at (\name-north-|\name-east); \coordinate (\name-northwest) at (\name-north-|\name-west); \coordinate (\name-southeast) at (\name-south-|\name-east); \coordinate (\name-southwest) at (\name-south-|\name-west); \coordinate (\name-nearnortheast) at (\LastEastx, \y/2, \z/2); \coordinate (\name-farnortheast) at (\LastEastx, \y/2,-\z/2); \coordinate (\name-nearsoutheast) at (\LastEastx,-\y/2, \z/2); \coordinate (\name-farsoutheast) at (\LastEastx,-\y/2,-\z/2); \coordinate (\name-nearnorthwest) at (0, \y/2, \z/2); \coordinate (\name-farnorthwest) at (0, \y/2,-\z/2); \coordinate (\name-nearsouthwest) at (0,-\y/2, \z/2); \coordinate (\name-farsouthwest) at (0,-\y/2,-\z/2); }, /boxblock/.search also={/tikz}, /boxblock/.cd, width/.store in=\cubex, height/.store in=\cubey, depth/.store in=\cubez, scale/.store in=\scale, xlabel/.store in=\boxlabels, ylabel/.store in=\ylabel, zlabel/.store in=\zlabel, caption/.store in=\caption, name/.store in=\name, fill/.store in=\fill, opacity/.store in=\opacity, fill={rgb:red,5;green,5;blue,5;white,15}, opacity=0.4, width=2, height=13, depth=15, scale=.2, xlabel={{"","","","","","","","","",""}}, ylabel=, zlabel=, caption=, name=, } ================================================ FILE: PlotNeuralNet/layers/RightBandedBox.sty ================================================ \ProvidesPackage{RightBandedBox} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This Block can draw simple block of boxes with custom colors. % Can be used for conv, deconv etc %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \tikzset{RightBandedBox/.pic={\tikzset{/block/.cd,#1} \tikzstyle{box}=[every edge/.append style={pic actions, densely dashed, opacity=.7},fill opacity=\opacity, pic actions,fill=\fill] \tikzstyle{band}=[every edge/.append style={pic actions, densely dashed, opacity=.7},fill opacity=\bandopacity, pic actions,fill=\bandfill,draw=\bandfill] \pgfmathsetmacro{\y}{\cubey*\scale} \pgfmathsetmacro{\z}{\cubez*\scale} %Multiple concatenated boxes \foreach[count=\i,% evaluate=\i as \xlabel using {array({\boxlabels},\i-1)},% evaluate=\unscaledx as \k using {\unscaledx*\scale+\prev}, remember=\k as \prev (initially 0)] \unscaledx in \cubex { \pgfmathsetmacro{\x}{\unscaledx*\scale} \coordinate (a) at (\k-\x , \y/2 , \z/2); \coordinate (art) at (\k-\x/3 , \y/2 , \z/2); %a_right_third \coordinate (b) at (\k-\x ,-\y/2 , \z/2); \coordinate (brt) at (\k-\x/3 ,-\y/2 , \z/2); %b_right_third \coordinate (c) at (\k ,-\y/2 , \z/2); \coordinate (d) at (\k , \y/2 , \z/2); \coordinate (e) at (\k , \y/2 ,-\z/2); \coordinate (f) at (\k ,-\y/2 ,-\z/2); \coordinate (g) at (\k-\x ,-\y/2 ,-\z/2); \coordinate (h) at (\k-\x , \y/2 ,-\z/2); \coordinate (hrt) at (\k-\x/3 , \y/2 ,-\z/2); %h_right_third %fill box color \draw [box] (d) -- (a) -- (b) -- (c) -- cycle (d) -- (a) -- (h) -- (e) -- cycle; %dotted edges \draw [box] (f) edge (g) (b) edge (g) (h) edge (g); %fill band color \draw [band] (d) -- (art) -- (brt) -- (c) -- cycle (d) -- (art) -- (hrt) -- (e) -- cycle; %draw edges again which were covered by band \draw [box,fill opacity=0] (d) -- (a) -- (b) -- (c) -- cycle (d) -- (a) -- (h) -- (e) -- cycle; \path (b) edge ["\xlabel"',midway] (c); \xdef\LastEastx{\k} %\k persists as \LastEastx after loop }%Loop ends \draw [box] (d) -- (e) -- (f) -- (c) -- cycle; %East face of last box \draw [band] (d) -- (e) -- (f) -- (c) -- cycle; %East face of last box \draw [pic actions] (d) -- (e) -- (f) -- (c) -- cycle; %East face edges of last box \coordinate (a1) at (0 , \y/2 , \z/2); \coordinate (b1) at (0 ,-\y/2 , \z/2); \tikzstyle{depthlabel}=[pos=0,text width=14*\z,text centered,sloped] \path (c) edge ["\small\zlabels"',depthlabel](f); %depth label \path (b1) edge ["\ylabel",midway] (a1); %height label \tikzstyle{captionlabel}=[text width=15*\LastEastx/\scale,text centered] \path (\LastEastx/2,-\y/2,+\z/2) + (0,-25pt) coordinate (cap) edge ["\textcolor{black}{ \bf \caption}"',captionlabel] (cap); %Block caption/pic object label %Define nodes to be used outside on the pic object \coordinate (\name-west) at (0,0,0) ; \coordinate (\name-east) at (\LastEastx, 0,0) ; \coordinate (\name-north) at (\LastEastx/2,\y/2,0); \coordinate (\name-south) at (\LastEastx/2,-\y/2,0); \coordinate (\name-anchor) at (\LastEastx/2, 0,0) ; \coordinate (\name-near) at (\LastEastx/2,0,\z/2); \coordinate (\name-far) at (\LastEastx/2,0,-\z/2); \coordinate (\name-nearwest) at (0,0,\z/2); \coordinate (\name-neareast) at (\LastEastx,0,\z/2); \coordinate (\name-farwest) at (0,0,-\z/2); \coordinate (\name-fareast) at (\LastEastx,0,-\z/2); \coordinate (\name-northeast) at (\name-north-|\name-east); \coordinate (\name-northwest) at (\name-north-|\name-west); \coordinate (\name-southeast) at (\name-south-|\name-east); \coordinate (\name-southwest) at (\name-south-|\name-west); \coordinate (\name-nearnortheast) at (\LastEastx, \y/2, \z/2); \coordinate (\name-farnortheast) at (\LastEastx, \y/2,-\z/2); \coordinate (\name-nearsoutheast) at (\LastEastx,-\y/2, \z/2); \coordinate (\name-farsoutheast) at (\LastEastx,-\y/2,-\z/2); \coordinate (\name-nearnorthwest) at (0, \y/2, \z/2); \coordinate (\name-farnorthwest) at (0, \y/2,-\z/2); \coordinate (\name-nearsouthwest) at (0,-\y/2, \z/2); \coordinate (\name-farsouthwest) at (0,-\y/2,-\z/2); }, /block/.search also={/tikz}, /block/.cd, width/.store in=\cubex, height/.store in=\cubey, depth/.store in=\cubez, scale/.store in=\scale, xlabel/.store in=\boxlabels, ylabel/.store in=\ylabel, zlabel/.store in=\zlabels, caption/.store in=\caption, name/.store in=\name, fill/.store in=\fill, bandfill/.store in=\bandfill, opacity/.store in=\opacity, bandopacity/.store in=\bandopacity, fill={rgb:red,5;green,5;blue,5;white,15}, bandfill={rgb:red,5;green,5;blue,5;white,5}, opacity=0.4, bandopacity=0.6, width=2, height=13, depth=15, scale=.2, xlabel={{"","","","","","","","","",""}}, ylabel=, zlabel=, caption=, name=, } ================================================ FILE: PlotNeuralNet/layers/init.tex ================================================ %\ProvidesPackage{init} \usetikzlibrary{quotes,arrows.meta} \usetikzlibrary{positioning} \def\edgecolor{rgb:blue,4;red,1;green,4;black,3} \newcommand{\midarrow}{\tikz \draw[-Stealth,line width =0.8mm,draw=\edgecolor] (-0.3,0) -- ++(0.3,0);} \usepackage{Ball} \usepackage{Box} \usepackage{RightBandedBox} ================================================ FILE: PlotNeuralNet/pycore/__init__.py ================================================ ================================================ FILE: PlotNeuralNet/pycore/blocks.py ================================================ from .tikzeng import * #define new block def block_2ConvPool( name, botton, top, s_filer=256, n_filer=64, offset="(1,0,0)", size=(32,32,3.5), opacity=0.5 ): return [ to_ConvConvRelu( name="ccr_{}".format( name ), s_filer=str(s_filer), n_filer=(n_filer,n_filer), offset=offset, to="({}-east)".format( botton ), width=(size[2],size[2]), height=size[0], depth=size[1], ), to_Pool( name="{}".format( top ), offset="(0,0,0)", to="(ccr_{}-east)".format( name ), width=1, height=size[0] - int(size[0]/4), depth=size[1] - int(size[0]/4), opacity=opacity, ), to_connection( "{}".format( botton ), "ccr_{}".format( name ) ) ] def block_Unconv( name, botton, top, s_filer=256, n_filer=64, offset="(1,0,0)", size=(32,32,3.5), opacity=0.5 ): return [ to_UnPool( name='unpool_{}'.format(name), offset=offset, to="({}-east)".format(botton), width=1, height=size[0], depth=size[1], opacity=opacity ), to_ConvRes( name='ccr_res_{}'.format(name), offset="(0,0,0)", to="(unpool_{}-east)".format(name), s_filer=str(s_filer), n_filer=str(n_filer), width=size[2], height=size[0], depth=size[1], opacity=opacity ), to_Conv( name='ccr_{}'.format(name), offset="(0,0,0)", to="(ccr_res_{}-east)".format(name), s_filer=str(s_filer), n_filer=str(n_filer), width=size[2], height=size[0], depth=size[1] ), to_ConvRes( name='ccr_res_c_{}'.format(name), offset="(0,0,0)", to="(ccr_{}-east)".format(name), s_filer=str(s_filer), n_filer=str(n_filer), width=size[2], height=size[0], depth=size[1], opacity=opacity ), to_Conv( name='{}'.format(top), offset="(0,0,0)", to="(ccr_res_c_{}-east)".format(name), s_filer=str(s_filer), n_filer=str(n_filer), width=size[2], height=size[0], depth=size[1] ), to_connection( "{}".format( botton ), "unpool_{}".format( name ) ) ] def block_Res( num, name, botton, top, s_filer=256, n_filer=64, offset="(0,0,0)", size=(32,32,3.5), opacity=0.5 ): lys = [] layers = [ *[ '{}_{}'.format(name,i) for i in range(num-1) ], top] for name in layers: ly = [ to_Conv( name='{}'.format(name), offset=offset, to="({}-east)".format( botton ), s_filer=str(s_filer), n_filer=str(n_filer), width=size[2], height=size[0], depth=size[1] ), to_connection( "{}".format( botton ), "{}".format( name ) ) ] botton = name lys+=ly lys += [ to_skip( of=layers[1], to=layers[-2], pos=1.25), ] return lys ================================================ FILE: PlotNeuralNet/pycore/tikzeng.py ================================================ import os def to_head( projectpath ): pathlayers = os.path.join( projectpath, 'layers/' ) return r""" \documentclass[border=8pt, 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 to_begin(): return r""" \newcommand{\copymidarrow}{\tikz \draw[-Stealth,line width=0.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,opacity=0.7] \tikzstyle{copyconnection}=[ultra thick,every node/.style={sloped,allow upside down},draw={rgb:blue,4;red,1;green,1;black,3},opacity=0.7] """ # layers definition def to_input( pathfile, to='(-3,0,0)', width=8, height=8, name="temp" ): return r""" \node[canvas is zy plane at x=0] (""" + name + """) at """+ to +""" {\includegraphics[width="""+ str(width)+"cm"+""",height="""+ str(height)+"cm"+"""]{"""+ pathfile +"""}}; """ # 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) +""" } }; """ # Conv,Conv,relu # Bottleneck def to_ConvConvRelu( name, s_filer=256, n_filer=(64,64), offset="(0,0,0)", to="(0,0,0)", width=(2,2), height=40, depth=40, 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)", width=1, height=32, depth=32, opacity=0.5, caption=" "): return r""" \pic[shift={ """+ offset +""" }] at """+ to +""" {Box={ name="""+name+""", 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)", width=1, height=32, depth=32, opacity=0.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_filer=256, n_filer=64, offset="(0,0,0)", to="(0,0,0)", width=6, height=40, depth=40, opacity=0.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_filer=40, 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 +""", zlabel="""+ str(s_filer) +""", fill=\SoftmaxColor, height="""+ str(height) +""", width="""+ str(width) +""", depth="""+ str(depth) +""" } }; """ # SoftMax def to_SoftMax( name, s_filer=10, offset="(0,0,0)", to="(0,0,0)", width=1.5, height=3, depth=25, opacity=0.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_connection( of, to): return r""" \draw [connection] ("""+of+"""-east) -- node {\midarrow} ("""+to+"""-west); """ def to_skip( of, to, pos=1.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, pathname="file.tex" ): with open(pathname, "w") as f: for c in arch: print(c) f.write( c ) ================================================ FILE: PlotNeuralNet/pyexamples/test_simple.py ================================================ import sys sys.path.append('../') # 添加自定义库的目录 from pycore.tikzeng import * # 导入自定义库 # defined your arch arch = [ # 添加头 to_head( '..' ), to_cor(), to_begin(), # 添加卷积层conv1 to_Conv("conv1", 512, 64, offset="(0,0,0)", to="(0,0,0)", height=64, depth=64, width=2, caption="conv1"), # 卷积层conv1东侧添加池化层pool1 to_Pool("pool1", offset="(0,0,0)", to="(conv1-east)", caption="pool1"), # 池化层pool1东侧添加卷积层conv2 to_Conv("conv2", 128, 64, offset="(1,0,0)", to="(pool1-east)", height=32, depth=32, width=2, caption="conv2"), # 建立pool1到conv2的连接箭头 to_connection( "pool1", "conv2"), # conv2东侧添加pool2 to_Pool("pool2", offset="(0,0,0)", to="(conv2-east)", height=28, depth=28, width=1, caption="pool2"), # pool1东侧添加softmax层但是偏移3单位 to_SoftMax("soft1", 10 ,"(3,0,0)", "(pool1-east)", caption="softmax"), # 建立pool2到soft1的连接箭头 to_connection("pool2", "soft1"), # 结束 to_end() ] def main(): namefile = str(sys.argv[0]).split('.')[0] to_generate(arch, namefile + '.tex' ) if __name__ == '__main__': main() ================================================ FILE: PlotNeuralNet/pyexamples/test_simple.tex ================================================ \documentclass[border=8pt, multi, tikz]{standalone} \usepackage{import} \subimport{../layers/}{init} \usetikzlibrary{positioning} \usetikzlibrary{3d} %for including external image \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} \newcommand{\copymidarrow}{\tikz \draw[-Stealth,line width=0.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,opacity=0.7] \tikzstyle{copyconnection}=[ultra thick,every node/.style={sloped,allow upside down},draw={rgb:blue,4;red,1;green,1;black,3},opacity=0.7] \pic[shift={(0,0,0)}] at (0,0,0) {Box={ name=conv1, caption=conv1, xlabel={{64, }}, zlabel=512, fill=\ConvColor, height=64, width=2, depth=64 } }; \pic[shift={ (0,0,0) }] at (conv1-east) {Box={ name=pool1, caption=pool1, fill=\PoolColor, opacity=0.5, height=32, width=1, depth=32 } }; \pic[shift={(1,0,0)}] at (pool1-east) {Box={ name=conv2, caption=conv2, xlabel={{64, }}, zlabel=128, fill=\ConvColor, height=32, width=2, depth=32 } }; \draw [connection] (pool1-east) -- node {\midarrow} (conv2-west); \pic[shift={ (0,0,0) }] at (conv2-east) {Box={ name=pool2, caption=pool2, fill=\PoolColor, opacity=0.5, height=28, width=1, depth=28 } }; \pic[shift={(3,0,0)}] at (pool1-east) {Box={ name=soft1, caption=softmax, xlabel={{" ","dummy"}}, zlabel=10, fill=\SoftmaxColor, opacity=0.8, height=3, width=1.5, depth=25 } }; \draw [connection] (pool2-east) -- node {\midarrow} (soft1-west); \end{tikzpicture} \end{document} ================================================ FILE: PlotNeuralNet/pyexamples/unet.py ================================================ import sys sys.path.append('../') from pycore.tikzeng import * from pycore.blocks import * arch = [ # 开头 to_head('..'), to_cor(), to_begin(), # 添加输入层 to_input( '../examples/fcn8s/cats.jpg' ), # 添加block1包含一个二重卷积接relu to_ConvConvRelu( name='ccr_b1', s_filer=500, n_filer=(64,64), offset="(0,0,0)", to="(0,0,0)", width=(2,2), height=40, depth=40 ), to_Pool(name="pool_b1", offset="(0,0,0)", to="(ccr_b1-east)", width=1, height=32, depth=32, opacity=0.5), # 添加三个block,每个包含三个二卷积加一池化 *block_2ConvPool( name='b2', botton='pool_b1', top='pool_b2', s_filer=256, n_filer=128, offset="(1,0,0)", size=(32,32,3.5), opacity=0.5 ), *block_2ConvPool( name='b3', botton='pool_b2', top='pool_b3', s_filer=128, n_filer=256, offset="(1,0,0)", size=(25,25,4.5), opacity=0.5 ), *block_2ConvPool( name='b4', botton='pool_b3', top='pool_b4', s_filer=64, n_filer=512, offset="(1,0,0)", size=(16,16,5.5), opacity=0.5 ), # 瓶颈,为block5 to_ConvConvRelu( name='ccr_b5', s_filer=32, n_filer=(1024,1024), offset="(2,0,0)", to="(pool_b4-east)", width=(8,8), height=8, depth=8, caption="Bottleneck" ), to_connection( "pool_b4", "ccr_b5"), # 解码器 # 多个block,每个为unconv *block_Unconv( name="b6", botton="ccr_b5", top='end_b6', s_filer=64, n_filer=512, offset="(2.1,0,0)", size=(16,16,5.0), opacity=0.5 ), to_skip( of='ccr_b4', to='ccr_res_b6', pos=1.25), *block_Unconv( name="b7", botton="end_b6", top='end_b7', s_filer=128, n_filer=256, offset="(2.1,0,0)", size=(25,25,4.5), opacity=0.5 ), to_skip( of='ccr_b3', to='ccr_res_b7', pos=1.25), *block_Unconv( name="b8", botton="end_b7", top='end_b8', s_filer=256, n_filer=128, offset="(2.1,0,0)", size=(32,32,3.5), opacity=0.5 ), to_skip( of='ccr_b2', to='ccr_res_b8', pos=1.25), *block_Unconv( name="b9", botton="end_b8", top='end_b9', s_filer=512, n_filer=64, offset="(2.1,0,0)", size=(40,40,2.5), opacity=0.5 ), to_skip( of='ccr_b1', to='ccr_res_b9', pos=1.25), to_ConvSoftMax( name="soft1", s_filer=512, offset="(0.75,0,0)", to="(end_b9-east)", width=1, height=40, depth=40, caption="SOFT" ), to_connection( "end_b9", "soft1"), # 结束 to_end() ] def main(): namefile = str(sys.argv[0]).split('.')[0] to_generate(arch, namefile + '.tex' ) if __name__ == '__main__': main() ================================================ FILE: PlotNeuralNet/pyexamples/unet.tex ================================================ \documentclass[border=8pt, multi, tikz]{standalone} \usepackage{import} \subimport{../layers/}{init} \usetikzlibrary{positioning} \usetikzlibrary{3d} %for including external image \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} \newcommand{\copymidarrow}{\tikz \draw[-Stealth,line width=0.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,opacity=0.7] \tikzstyle{copyconnection}=[ultra thick,every node/.style={sloped,allow upside down},draw={rgb:blue,4;red,1;green,1;black,3},opacity=0.7] \node[canvas is zy plane at x=0] (temp) at (-3,0,0) {\includegraphics[width=8cm,height=8cm]{../examples/fcn8s/cats.jpg}}; \pic[shift={ (0,0,0) }] at (0,0,0) {RightBandedBox={ name=ccr_b1, caption= , xlabel={{ 64, 64 }}, zlabel=500, fill=\ConvColor, bandfill=\ConvReluColor, height=40, width={ 2 , 2 }, depth=40 } }; \pic[shift={ (0,0,0) }] at (ccr_b1-east) {Box={ name=pool_b1, caption= , fill=\PoolColor, opacity=0.5, height=32, width=1, depth=32 } }; \pic[shift={ (1,0,0) }] at (pool_b1-east) {RightBandedBox={ name=ccr_b2, caption= , xlabel={{ 128, 128 }}, zlabel=256, fill=\ConvColor, bandfill=\ConvReluColor, height=32, width={ 3.5 , 3.5 }, depth=32 } }; \pic[shift={ (0,0,0) }] at (ccr_b2-east) {Box={ name=pool_b2, caption= , fill=\PoolColor, opacity=0.5, height=24, width=1, depth=24 } }; \draw [connection] (pool_b1-east) -- node {\midarrow} (ccr_b2-west); \pic[shift={ (1,0,0) }] at (pool_b2-east) {RightBandedBox={ name=ccr_b3, caption= , xlabel={{ 256, 256 }}, zlabel=128, fill=\ConvColor, bandfill=\ConvReluColor, height=25, width={ 4.5 , 4.5 }, depth=25 } }; \pic[shift={ (0,0,0) }] at (ccr_b3-east) {Box={ name=pool_b3, caption= , fill=\PoolColor, opacity=0.5, height=19, width=1, depth=19 } }; \draw [connection] (pool_b2-east) -- node {\midarrow} (ccr_b3-west); \pic[shift={ (1,0,0) }] at (pool_b3-east) {RightBandedBox={ name=ccr_b4, caption= , xlabel={{ 512, 512 }}, zlabel=64, fill=\ConvColor, bandfill=\ConvReluColor, height=16, width={ 5.5 , 5.5 }, depth=16 } }; \pic[shift={ (0,0,0) }] at (ccr_b4-east) {Box={ name=pool_b4, caption= , fill=\PoolColor, opacity=0.5, height=12, width=1, depth=12 } }; \draw [connection] (pool_b3-east) -- node {\midarrow} (ccr_b4-west); \pic[shift={ (2,0,0) }] at (pool_b4-east) {RightBandedBox={ name=ccr_b5, caption=Bottleneck, xlabel={{ 1024, 1024 }}, zlabel=32, fill=\ConvColor, bandfill=\ConvReluColor, height=8, width={ 8 , 8 }, depth=8 } }; \draw [connection] (pool_b4-east) -- node {\midarrow} (ccr_b5-west); \pic[shift={ (2.1,0,0) }] at (ccr_b5-east) {Box={ name=unpool_b6, caption= , fill=\UnpoolColor, opacity=0.5, height=16, width=1, depth=16 } }; \pic[shift={ (0,0,0) }] at (unpool_b6-east) {RightBandedBox={ name=ccr_res_b6, caption= , xlabel={{ 512, }}, zlabel=64, fill={rgb:white,1;black,3}, bandfill={rgb:white,1;black,2}, opacity=0.5, height=16, width=5.0, depth=16 } }; \pic[shift={(0,0,0)}] at (ccr_res_b6-east) {Box={ name=ccr_b6, caption= , xlabel={{512, }}, zlabel=64, fill=\ConvColor, height=16, width=5.0, depth=16 } }; \pic[shift={ (0,0,0) }] at (ccr_b6-east) {RightBandedBox={ name=ccr_res_c_b6, caption= , xlabel={{ 512, }}, zlabel=64, fill={rgb:white,1;black,3}, bandfill={rgb:white,1;black,2}, opacity=0.5, height=16, width=5.0, depth=16 } }; \pic[shift={(0,0,0)}] at (ccr_res_c_b6-east) {Box={ name=end_b6, caption= , xlabel={{512, }}, zlabel=64, fill=\ConvColor, height=16, width=5.0, depth=16 } }; \draw [connection] (ccr_b5-east) -- node {\midarrow} (unpool_b6-west); \path (ccr_b4-southeast) -- (ccr_b4-northeast) coordinate[pos=1.25] (ccr_b4-top) ; \path (ccr_res_b6-south) -- (ccr_res_b6-north) coordinate[pos=1.25] (ccr_res_b6-top) ; \draw [copyconnection] (ccr_b4-northeast) -- node {\copymidarrow}(ccr_b4-top) -- node {\copymidarrow}(ccr_res_b6-top) -- node {\copymidarrow} (ccr_res_b6-north); \pic[shift={ (2.1,0,0) }] at (end_b6-east) {Box={ name=unpool_b7, caption= , fill=\UnpoolColor, opacity=0.5, height=25, width=1, depth=25 } }; \pic[shift={ (0,0,0) }] at (unpool_b7-east) {RightBandedBox={ name=ccr_res_b7, caption= , xlabel={{ 256, }}, zlabel=128, fill={rgb:white,1;black,3}, bandfill={rgb:white,1;black,2}, opacity=0.5, height=25, width=4.5, depth=25 } }; \pic[shift={(0,0,0)}] at (ccr_res_b7-east) {Box={ name=ccr_b7, caption= , xlabel={{256, }}, zlabel=128, fill=\ConvColor, height=25, width=4.5, depth=25 } }; \pic[shift={ (0,0,0) }] at (ccr_b7-east) {RightBandedBox={ name=ccr_res_c_b7, caption= , xlabel={{ 256, }}, zlabel=128, fill={rgb:white,1;black,3}, bandfill={rgb:white,1;black,2}, opacity=0.5, height=25, width=4.5, depth=25 } }; \pic[shift={(0,0,0)}] at (ccr_res_c_b7-east) {Box={ name=end_b7, caption= , xlabel={{256, }}, zlabel=128, fill=\ConvColor, height=25, width=4.5, depth=25 } }; \draw [connection] (end_b6-east) -- node {\midarrow} (unpool_b7-west); \path (ccr_b3-southeast) -- (ccr_b3-northeast) coordinate[pos=1.25] (ccr_b3-top) ; \path (ccr_res_b7-south) -- (ccr_res_b7-north) coordinate[pos=1.25] (ccr_res_b7-top) ; \draw [copyconnection] (ccr_b3-northeast) -- node {\copymidarrow}(ccr_b3-top) -- node {\copymidarrow}(ccr_res_b7-top) -- node {\copymidarrow} (ccr_res_b7-north); \pic[shift={ (2.1,0,0) }] at (end_b7-east) {Box={ name=unpool_b8, caption= , fill=\UnpoolColor, opacity=0.5, height=32, width=1, depth=32 } }; \pic[shift={ (0,0,0) }] at (unpool_b8-east) {RightBandedBox={ name=ccr_res_b8, caption= , xlabel={{ 128, }}, zlabel=256, fill={rgb:white,1;black,3}, bandfill={rgb:white,1;black,2}, opacity=0.5, height=32, width=3.5, depth=32 } }; \pic[shift={(0,0,0)}] at (ccr_res_b8-east) {Box={ name=ccr_b8, caption= , xlabel={{128, }}, zlabel=256, fill=\ConvColor, height=32, width=3.5, depth=32 } }; \pic[shift={ (0,0,0) }] at (ccr_b8-east) {RightBandedBox={ name=ccr_res_c_b8, caption= , xlabel={{ 128, }}, zlabel=256, fill={rgb:white,1;black,3}, bandfill={rgb:white,1;black,2}, opacity=0.5, height=32, width=3.5, depth=32 } }; \pic[shift={(0,0,0)}] at (ccr_res_c_b8-east) {Box={ name=end_b8, caption= , xlabel={{128, }}, zlabel=256, fill=\ConvColor, height=32, width=3.5, depth=32 } }; \draw [connection] (end_b7-east) -- node {\midarrow} (unpool_b8-west); \path (ccr_b2-southeast) -- (ccr_b2-northeast) coordinate[pos=1.25] (ccr_b2-top) ; \path (ccr_res_b8-south) -- (ccr_res_b8-north) coordinate[pos=1.25] (ccr_res_b8-top) ; \draw [copyconnection] (ccr_b2-northeast) -- node {\copymidarrow}(ccr_b2-top) -- node {\copymidarrow}(ccr_res_b8-top) -- node {\copymidarrow} (ccr_res_b8-north); \pic[shift={ (2.1,0,0) }] at (end_b8-east) {Box={ name=unpool_b9, caption= , fill=\UnpoolColor, opacity=0.5, height=40, width=1, depth=40 } }; \pic[shift={ (0,0,0) }] at (unpool_b9-east) {RightBandedBox={ name=ccr_res_b9, caption= , xlabel={{ 64, }}, zlabel=512, fill={rgb:white,1;black,3}, bandfill={rgb:white,1;black,2}, opacity=0.5, height=40, width=2.5, depth=40 } }; \pic[shift={(0,0,0)}] at (ccr_res_b9-east) {Box={ name=ccr_b9, caption= , xlabel={{64, }}, zlabel=512, fill=\ConvColor, height=40, width=2.5, depth=40 } }; \pic[shift={ (0,0,0) }] at (ccr_b9-east) {RightBandedBox={ name=ccr_res_c_b9, caption= , xlabel={{ 64, }}, zlabel=512, fill={rgb:white,1;black,3}, bandfill={rgb:white,1;black,2}, opacity=0.5, height=40, width=2.5, depth=40 } }; \pic[shift={(0,0,0)}] at (ccr_res_c_b9-east) {Box={ name=end_b9, caption= , xlabel={{64, }}, zlabel=512, fill=\ConvColor, height=40, width=2.5, depth=40 } }; \draw [connection] (end_b8-east) -- node {\midarrow} (unpool_b9-west); \path (ccr_b1-southeast) -- (ccr_b1-northeast) coordinate[pos=1.25] (ccr_b1-top) ; \path (ccr_res_b9-south) -- (ccr_res_b9-north) coordinate[pos=1.25] (ccr_res_b9-top) ; \draw [copyconnection] (ccr_b1-northeast) -- node {\copymidarrow}(ccr_b1-top) -- node {\copymidarrow}(ccr_res_b9-top) -- node {\copymidarrow} (ccr_res_b9-north); \pic[shift={(0.75,0,0)}] at (end_b9-east) {Box={ name=soft1, caption=SOFT, zlabel=512, fill=\SoftmaxColor, height=40, width=1, depth=40 } }; \draw [connection] (end_b9-east) -- node {\midarrow} (soft1-west); \end{tikzpicture} \end{document} ================================================ FILE: PlotNeuralNet/tikzmake.sh ================================================ #!/bin/bash python $1.py pdflatex $1.tex rm *.aux *.log *.vscodeLog rm *.tex xdg-open $1.pdf ================================================ FILE: README.md ================================================ # 卷积神经网络结构可视化 - 简介 - 本文介绍的工具是针对卷积神经网络示意图可视化的,不包括算图。(示意图一般出现在论文中) - 常见的卷积神经网络示意图绘制工具不少,常用的主要有NN SVG、ConvNetDraw、PlotNeuralNet等。 - 写这篇重点介绍PlotNeuralNet的教程的原因是国内关于它的教程很少并且大都只是列举了官方demo。 - 常见工具 - NN SVG - [官方地址](http://alexlenail.me/NN-SVG/) - ![](https://img-blog.csdnimg.cn/20190621171606586.png) - 过去一段时间内比较喜欢用的,特点是很方便,提供给用户的是个交互式的Web页面。 - 特点 - 方便,各层直接界面控制增减及变化。 - 支持三种风格,选择空间大。 - 支持SVG格式下载。 - 缺点 - 可视化界面的最大问题就是很多用户期待的功能为考虑全面,定制程度低。 - 各层连接不是很好看。 - ConvNetDraw - [官方地址](https://cbovar.github.io/ConvNetDraw/) - ![](https://img-blog.csdnimg.cn/20190621172249187.png) - 从未使用过,很多博主推荐,但是观感劝退了我。 - 特点 - 脚本化控制 - 尺度自定义 - 直观 - 缺点 - 既没有做到脚本化的自由度,又没有做到交互界面的观感。 - 不好看。 - PlotNeuralNet - 官方地址 - https://github.com/HarisIqbal88/PlotNeuralNet.git - ![FCN-8效果图](https://img-blog.csdnimg.cn/20190621172931297.png) - 这是我极力推荐的工具,尽管它的上手难度略高于之前两个,但学会之后很好用,不少论文就是使用这个工具可视化的。 - 特点 - 脚本化,使用LaTex编写或者使用Python脚本编写结构模型,自由度高。 - 缺点 - 无交互界面,上手略有难度。 - 使用教程 - 说明 - 基于Linux或者有bash的环境,我只在Ubuntu系统下测试成功。(事实上,深度学习首选的环境之一就是Linux的Ubuntu) - 只介绍Python脚本绘制的方式,不介绍LaTex方式。 - 需要安装前置软件,如LaTex解析器。 - 前置准备 - 安装textlive - 调用LaTex解析生成PDF,需要安装LaTex,这里使用TextLive。 - `wget https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/Images/texlive2019.iso` - 上面命令为下载镜像到当前目录,失败则可能换源,去掉上述链接的最后文件,在Web中查看合适文件下载即可。 - `sudo apt-get install perl-tk` - 安装图形界面 - `sudo mount -o loop texlive.iso /mnt` - 挂载镜像 - `cd /mnt` - 切换到挂载目录 - `sudo ./install-tl -gui` - 使用图形界面安装 - `sudo apt-get install texlive-latex-extra` - 安装扩展包 - 下载源码 - `git clone https://github.com/HarisIqbal88/PlotNeuralNet.git` - 源码目录结构 - ![](https://img-blog.csdnimg.cn/2019062117383748.png) - 其中pycore下的tikzeng.py是核心文件,定义了绘图过程(所有to开头的函数),可以绘制的层,py脚本向LaTex的转换。 - 代码比较易懂,这里不做解析了。 - 绘制 - 一般将自己写的py脚本放在clone的项目的pyexamples目录下。 - 源目录下有两个py脚本,对其进行详细注释,包含了常用的语法。 - 代码1-test_simple.py - cd到pyexamples目录执行`bash ../tikzmake.sh test_simple`(**注意不加py后缀,且有些错误正常,观察是否生成pdf文件即可**) - ```python import sys sys.path.append('../') # 添加自定义库的目录 from pycore.tikzeng import * # 导入自定义库 # defined your arch arch = [ # 添加头 to_head( '..' ), to_cor(), to_begin(), # 添加卷积层conv1 to_Conv("conv1", 512, 64, offset="(0,0,0)", to="(0,0,0)", height=64, depth=64, width=2 ), # 卷积层conv1东侧添加池化层pool1 to_Pool("pool1", offset="(0,0,0)", to="(conv1-east)"), # 池化层pool1东侧添加卷积层conv2 to_Conv("conv2", 128, 64, offset="(1,0,0)", to="(pool1-east)", height=32, depth=32, width=2 ), # 建立pool1到conv2的连接箭头 to_connection( "pool1", "conv2"), # conv2东侧添加pool2 to_Pool("pool2", offset="(0,0,0)", to="(conv2-east)", height=28, depth=28, width=1), # pool1东侧添加softmax层但是偏移3单位 to_SoftMax("soft1", 10 ,"(3,0,0)", "(pool1-east)", caption="SOFT" ), # 建立pool2到soft1的连接箭头 to_connection("pool2", "soft1"), # 结束 to_end() ] def main(): namefile = str(sys.argv[0]).split('.')[0] to_generate(arch, namefile + '.tex' ) if __name__ == '__main__': main() ``` - 执行结果 - ![](https://img-blog.csdnimg.cn/2019062122263957.png) - 代码2-unet.py - ```python import sys sys.path.append('../') from pycore.tikzeng import * from pycore.blocks import * arch = [ # 开头 to_head('..'), to_cor(), to_begin(), # 添加输入层 to_input( '../examples/fcn8s/cats.jpg' ), # 添加block1包含一个二重卷积接relu to_ConvConvRelu( name='ccr_b1', s_filer=500, n_filer=(64,64), offset="(0,0,0)", to="(0,0,0)", width=(2,2), height=40, depth=40 ), to_Pool(name="pool_b1", offset="(0,0,0)", to="(ccr_b1-east)", width=1, height=32, depth=32, opacity=0.5), # 添加三个block,每个包含三个二卷积加一池化 *block_2ConvPool( name='b2', botton='pool_b1', top='pool_b2', s_filer=256, n_filer=128, offset="(1,0,0)", size=(32,32,3.5), opacity=0.5 ), *block_2ConvPool( name='b3', botton='pool_b2', top='pool_b3', s_filer=128, n_filer=256, offset="(1,0,0)", size=(25,25,4.5), opacity=0.5 ), *block_2ConvPool( name='b4', botton='pool_b3', top='pool_b4', s_filer=64, n_filer=512, offset="(1,0,0)", size=(16,16,5.5), opacity=0.5 ), # 瓶颈,为block5 to_ConvConvRelu( name='ccr_b5', s_filer=32, n_filer=(1024,1024), offset="(2,0,0)", to="(pool_b4-east)", width=(8,8), height=8, depth=8, caption="Bottleneck" ), to_connection( "pool_b4", "ccr_b5"), # 解码器 # 多个block,每个为unconv *block_Unconv( name="b6", botton="ccr_b5", top='end_b6', s_filer=64, n_filer=512, offset="(2.1,0,0)", size=(16,16,5.0), opacity=0.5 ), to_skip( of='ccr_b4', to='ccr_res_b6', pos=1.25), *block_Unconv( name="b7", botton="end_b6", top='end_b7', s_filer=128, n_filer=256, offset="(2.1,0,0)", size=(25,25,4.5), opacity=0.5 ), to_skip( of='ccr_b3', to='ccr_res_b7', pos=1.25), *block_Unconv( name="b8", botton="end_b7", top='end_b8', s_filer=256, n_filer=128, offset="(2.1,0,0)", size=(32,32,3.5), opacity=0.5 ), to_skip( of='ccr_b2', to='ccr_res_b8', pos=1.25), *block_Unconv( name="b9", botton="end_b8", top='end_b9', s_filer=512, n_filer=64, offset="(2.1,0,0)", size=(40,40,2.5), opacity=0.5 ), to_skip( of='ccr_b1', to='ccr_res_b9', pos=1.25), to_ConvSoftMax( name="soft1", s_filer=512, offset="(0.75,0,0)", to="(end_b9-east)", width=1, height=40, depth=40, caption="SOFT" ), to_connection( "end_b9", "soft1"), # 结束 to_end() ] def main(): namefile = str(sys.argv[0]).split('.')[0] to_generate(arch, namefile + '.tex' ) if __name__ == '__main__': main() ``` - 执行结果 - ![](https://img-blog.csdnimg.cn/20190621222723766.png) - 补充说明 - 封装的各类层函数都在tikzeng.py文件中。 - 具体的代码和结果可以在我的Github找到,欢迎star或者fork。 - 实际项目请按照官方项目更新为主。