├── main.pdf ├── lenna.png ├── output.png ├── vermeer.jpg ├── output_z.png ├── Makefile ├── README.md ├── LICENSE ├── .gitignore └── main.tex /main.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jettan/tikz_cnn/HEAD/main.pdf -------------------------------------------------------------------------------- /lenna.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jettan/tikz_cnn/HEAD/lenna.png -------------------------------------------------------------------------------- /output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jettan/tikz_cnn/HEAD/output.png -------------------------------------------------------------------------------- /vermeer.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jettan/tikz_cnn/HEAD/vermeer.jpg -------------------------------------------------------------------------------- /output_z.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jettan/tikz_cnn/HEAD/output_z.png -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | pdflatex main.tex 3 | pdflatex main.tex 4 | pdflatex main.tex 5 | 6 | clean: 7 | rm -f *.aux *.log *.toc *.pdf 2>/dev/null 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tikz_cnn 2 | 3 | Draw nice looking architectures of your neural network using this tikz macro. 4 | 5 | Example output: 6 | 7 | ![](output.png?raw=true) 8 | 9 | Example output for Z: 10 | ![](output_z.png?raw=true) 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Jethro Tan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Core latex/pdflatex auxiliary files: 2 | *.aux 3 | *.lof 4 | *.log 5 | *.lot 6 | *.fls 7 | *.out 8 | *.toc 9 | *.fmt 10 | *.fot 11 | *.cb 12 | *.cb2 13 | 14 | ## Intermediate documents: 15 | *.dvi 16 | *-converted-to.* 17 | # these rules might exclude image files for figures etc. 18 | # *.ps 19 | # *.eps 20 | # *.pdf 21 | 22 | ## Bibliography auxiliary files (bibtex/biblatex/biber): 23 | *.bbl 24 | *.bcf 25 | *.blg 26 | *-blx.aux 27 | *-blx.bib 28 | *.brf 29 | *.run.xml 30 | 31 | ## Build tool auxiliary files: 32 | *.fdb_latexmk 33 | *.synctex 34 | *.synctex.gz 35 | *.synctex.gz(busy) 36 | *.pdfsync 37 | 38 | ## Auxiliary and intermediate files from other packages: 39 | # algorithms 40 | *.alg 41 | *.loa 42 | 43 | # achemso 44 | acs-*.bib 45 | 46 | # amsthm 47 | *.thm 48 | 49 | # beamer 50 | *.nav 51 | *.snm 52 | *.vrb 53 | 54 | # cprotect 55 | *.cpt 56 | 57 | # fixme 58 | *.lox 59 | 60 | #(r)(e)ledmac/(r)(e)ledpar 61 | *.end 62 | *.?end 63 | *.[1-9] 64 | *.[1-9][0-9] 65 | *.[1-9][0-9][0-9] 66 | *.[1-9]R 67 | *.[1-9][0-9]R 68 | *.[1-9][0-9][0-9]R 69 | *.eledsec[1-9] 70 | *.eledsec[1-9]R 71 | *.eledsec[1-9][0-9] 72 | *.eledsec[1-9][0-9]R 73 | *.eledsec[1-9][0-9][0-9] 74 | *.eledsec[1-9][0-9][0-9]R 75 | 76 | # glossaries 77 | *.acn 78 | *.acr 79 | *.glg 80 | *.glo 81 | *.gls 82 | *.glsdefs 83 | 84 | # gnuplottex 85 | *-gnuplottex-* 86 | 87 | # hyperref 88 | *.brf 89 | 90 | # knitr 91 | *-concordance.tex 92 | # TODO Comment the next line if you want to keep your tikz graphics files 93 | *.tikz 94 | *-tikzDictionary 95 | 96 | # listings 97 | *.lol 98 | 99 | # makeidx 100 | *.idx 101 | *.ilg 102 | *.ind 103 | *.ist 104 | 105 | # minitoc 106 | *.maf 107 | *.mlf 108 | *.mlt 109 | *.mtc 110 | *.mtc[0-9] 111 | *.mtc[1-9][0-9] 112 | 113 | # minted 114 | _minted* 115 | *.pyg 116 | 117 | # morewrites 118 | *.mw 119 | 120 | # mylatexformat 121 | *.fmt 122 | 123 | # nomencl 124 | *.nlo 125 | 126 | # sagetex 127 | *.sagetex.sage 128 | *.sagetex.py 129 | *.sagetex.scmd 130 | 131 | # sympy 132 | *.sout 133 | *.sympy 134 | sympy-plots-for-*.tex/ 135 | 136 | # pdfcomment 137 | *.upa 138 | *.upb 139 | 140 | # pythontex 141 | *.pytxcode 142 | pythontex-files-*/ 143 | 144 | # thmtools 145 | *.loe 146 | 147 | # TikZ & PGF 148 | *.dpth 149 | *.md5 150 | *.auxlock 151 | 152 | # todonotes 153 | *.tdo 154 | 155 | # xindy 156 | *.xdy 157 | 158 | # xypic precompiled matrices 159 | *.xyc 160 | 161 | # endfloat 162 | *.ttt 163 | *.fff 164 | 165 | # Latexian 166 | TSWLatexianTemp* 167 | 168 | ## Editors: 169 | # WinEdt 170 | *.bak 171 | *.sav 172 | 173 | # Texpad 174 | .texpadtmp 175 | 176 | # Kile 177 | *.backup 178 | 179 | # KBibTeX 180 | *~[0-9]* 181 | -------------------------------------------------------------------------------- /main.tex: -------------------------------------------------------------------------------- 1 | \documentclass[a4paper, 10pt]{article} 2 | 3 | \usepackage{graphicx} 4 | \usepackage{color} 5 | \usepackage{tikz} 6 | \usepackage{pgfplots} 7 | \usepackage{pgf-umlsd} 8 | \usepackage{ifthen} 9 | \usepackage[]{fp} 10 | 11 | \usetikzlibrary{matrix,patterns,spy,fit,calc} 12 | \usepgfplotslibrary{groupplots} 13 | \pgfplotsset{compat=newest} 14 | 15 | \FPset{totalOffset}{0} 16 | 17 | \begin{document} 18 | 19 | \begin{figure} 20 | \centering 21 | \noindent\resizebox{\textwidth}{!}{ 22 | \begin{tikzpicture} 23 | %\draw[use as bounding box, transparent] (-1.8,-1.8) rectangle (17.2, 3.2); 24 | 25 | \newcommand{\networkLayer}[9]{ 26 | % Define the macro. 27 | % 1st argument: Height and width of the layer rectangle slice. 28 | % 2nd argument: Depth of the layer slice 29 | % 3rd argument: X Offset --> use it to offset layers from previously drawn layers. 30 | % 4th argument: Y Offset --> Use it when an output needs to be fed to multiple layers that are on the same X offset. 31 | % 5th argument: Z Offset --> Use to offset layers from previous 32 | % 6th argument: Options for filldraw. 33 | % 7th argument: Text to be placed below this layer. 34 | % 8th argument: Name of coordinates. When name = "start" this resets the offset counter 35 | % 9th argument: list of nodes to connect to (previous layers) 36 | \xdef\totalOffset{\totalOffset} 37 | \ifthenelse{\equal{#8} {start}} 38 | {\FPset{totalOffset}{0}} 39 | {} 40 | \FPeval\currentOffset{0+(totalOffset)+(#3)} 41 | 42 | \def\hw{#1} % Used to distinguish input resolution for current layer. 43 | \def\b{0.02} 44 | \def\c{#2} % Width of the cube to distinguish number of input channels for current layer. 45 | \def\x{\currentOffset} % X offset for current layer. 46 | \def\y{#4} % Y offset for current layer. 47 | \def\z{#5} % Z offset for current layer. 48 | \def\inText{#7} 49 | 50 | % Define references to points on the cube surfaces 51 | \coordinate (#8_front) at (\x+\c , \z , \y); 52 | \coordinate (#8_back) at (\x , \z , \y); 53 | \coordinate (#8_top) at (\x+\c/2, \z+\hw/2, \y); 54 | \coordinate (#8_bottom) at (\x+\c/2, \z-\hw/2, \y); 55 | 56 | % Define cube coords 57 | \coordinate (blr) at (\c+\x, -\hw/2+\z, -\hw/2+\y); %back lower right 58 | \coordinate (bur) at (\c+\x, \hw/2+\z, -\hw/2+\y); %back upper right 59 | \coordinate (bul) at (0 +\x, \hw/2+\z, -\hw/2+\y); %back upper left 60 | \coordinate (fll) at (0 +\x, -\hw/2+\z, \hw/2+\y); %front lower left 61 | \coordinate (flr) at (\c+\x, -\hw/2+\z, \hw/2+\y); %front lower right 62 | \coordinate (fur) at (\c+\x, \hw/2+\z, \hw/2+\y); %front upper right 63 | \coordinate (ful) at (0 +\x, \hw/2+\z, \hw/2+\y); %front upper left 64 | 65 | % Draw connections from other points to the back of this node 66 | \ifthenelse{\equal{#9} {}} 67 | {}{ 68 | \foreach \val in #9 69 | \draw[line width=0.3mm] (\val)--(#8_back); 70 | } 71 | 72 | % Draw the layer body. 73 | % back plane 74 | \draw[line width=0.3mm](blr) -- (bur) -- (bul); 75 | % front plane 76 | \draw[line width=0.3mm](fll) -- (flr) node[midway,below] {\inText} -- (fur) -- (ful) -- (fll); 77 | \draw[line width=0.3mm](blr) -- (flr); 78 | \draw[line width=0.3mm](bur) -- (fur); 79 | \draw[line width=0.3mm](bul) -- (ful); 80 | 81 | % Recolor visible surfaces 82 | % front plane 83 | \filldraw[#6] ($(fll)+(\b,\b,0)$) -- ($(flr)+(-\b,\b,0)$) -- ($(fur)+(-\b,-\b,0)$) -- ($(ful)+(\b,-\b,0)$) -- ($(fll)+(\b,\b,0)$); 84 | \filldraw[#6] ($(ful)+(\b,0,-\b)$) -- ($(fur)+(-\b,0,-\b)$) -- ($(bur)+(-\b,0,\b)$) -- ($(bul)+(\b,0,\b)$); 85 | 86 | % Colored slice. 87 | \ifthenelse {\equal{#6} {}}{} % Do not draw colored slice if #4 is blank. 88 | % Else, draw a colored slice. 89 | {\filldraw[#6] ($(flr)+(0,\b,-\b)$) -- ($(blr)+(0,\b,\b)$) -- ($(bur)+(0,-\b,\b)$) -- ($(fur)+(0,-\b,-\b)$);} 90 | 91 | \FPeval\totalOffset{0+(currentOffset)+\c} 92 | } 93 | 94 | %\networkLayer{2.0}{0.5}{0.0}{0.0}{2.5}{color=red!50}{}{start}{} 95 | %\networkLayer{2.0}{0.25}{1.5}{0.0}{0.0}{color=green!50}{}{bot}{{start_front}} 96 | %\networkLayer{2.0}{0.25}{0.15}{0.0}{0.0}{color=green!50}{}{}{} 97 | %\networkLayer{2.0}{0.5}{0.15}{0.0}{0.0}{color=green!50}{}{end}{} 98 | %\networkLayer{2.0}{0.5}{-(2.8)/2}{0.0}{5.0}{color=green!50}{}{top}{{start_front}} 99 | %\networkLayer{2.0}{0.5}{2.0}{0.0}{2.5}{color=blue!50}{}{add}{{end_front,top_front}} 100 | %\networkLayer{1.0}{0.5}{0.15}{0.0}{2.5}{color=blue!50}{}{}{} 101 | %\networkLayer{0.75}{0.5}{0.15}{0.0}{2.5}{color=blue!50}{}{}{} 102 | %\networkLayer{0.5}{0.5}{0.15}{0.0}{2.5}{color=blue!50}{}{}{} 103 | % INPUT 104 | \networkLayer{3.0}{0.03}{0.0}{0.0}{0.0}{color=gray!80}{}{start}{} 105 | 106 | % ENCODER 107 | \networkLayer{3.0}{0.1}{0.5}{0.0}{0.0}{color=white}{conv}{}{} % S1 108 | \networkLayer{3.0}{0.1}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2 109 | \networkLayer{2.5}{0.2}{0.1}{0.0}{0.0}{color=white}{conv}{}{} % S1 110 | \networkLayer{2.5}{0.2}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2 111 | \networkLayer{2.0}{0.4}{0.1}{0.0}{0.0}{color=white}{conv}{}{} % S1 112 | \networkLayer{2.0}{0.4}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2 113 | \networkLayer{1.5}{0.8}{0.1}{0.0}{0.0}{color=white}{conv}{}{} % S1 114 | \networkLayer{1.5}{0.8}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2 115 | \networkLayer{1.0}{1.5}{0.1}{0.0}{0.0}{color=white}{conv}{}{} % S1 116 | \networkLayer{1.0}{1.5}{0.1}{0.0}{0.0}{color=white}{}{mid}{} % S2 117 | 118 | \networkLayer{1.0}{0.5}{1.5}{0.0}{-1.5}{color=green!50}{}{bot}{{mid_front}} 119 | \networkLayer{1.0}{0.5}{-0.5}{0.0}{1.5}{color=green!50}{}{top}{{mid_front}} 120 | \networkLayer{1.0}{0.5}{1.5}{0.0}{0.0}{color=blue!50}{sum}{}{{bot_front,top_front}} 121 | 122 | % DECODER 123 | \networkLayer{1.0}{1.5}{0.1}{0.0}{0.0}{color=white}{deconv}{}{} % S1 124 | \networkLayer{1.0}{1.5}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2 125 | \networkLayer{1.5}{0.8}{0.1}{0.0}{0.0}{color=white}{deconv}{}{} % S1 126 | \networkLayer{1.5}{0.8}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2 127 | \networkLayer{2.0}{0.4}{0.1}{0.0}{0.0}{color=white}{}{}{} % S1 128 | \networkLayer{2.0}{0.4}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2 129 | \networkLayer{2.5}{0.2}{0.1}{0.0}{0.0}{color=white}{}{}{} % S1 130 | \networkLayer{2.5}{0.2}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2 131 | \networkLayer{3.0}{0.1}{0.1}{0.0}{0.0}{color=white}{}{}{} % S1 132 | \networkLayer{3.0}{0.1}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2 133 | 134 | % OUTPUT 135 | \networkLayer{3.0}{0.05}{0.9}{0.0}{0.0}{color=red!40}{}{}{} % Pixelwise segmentation with classes. 136 | 137 | \end{tikzpicture} 138 | } 139 | \caption{Example CNN.} 140 | \label{fig:cnn} 141 | \end{figure} 142 | 143 | \end{document} 144 | --------------------------------------------------------------------------------