2

I am trying to draw a CNN network like this picture: enter image description here

I have drawn the rectangles but I don't know how to add arrows and labels between them.

\documentclass[tikz,border=3.14pt]{standalone}
\usetikzlibrary{3d,decorations.text,shapes.arrows,positioning,fit,backgrounds}
\tikzset{pics/fake box/.style args={% #1=color, #2=x dimension, #3=y dimension, #4=z dimension
#1 with dimensions #2 and #3 and #4}{
code={
\draw[gray,ultra thin,fill=#1]  (0,0,0) coordinate(-front-bottom-left) to
++ (0,#3,0) coordinate(-front-top-right) --++
(#2,0,0) coordinate(-front-top-right) --++ (0,-#3,0) 
coordinate(-front-bottom-right) -- cycle;
\draw[gray,ultra thin,fill=#1] (0,#3,0)  --++ 
 (0,0,#4) coordinate(-back-top-left) --++ (#2,0,0) 
 coordinate(-back-top-right) --++ (0,0,-#4)  -- cycle;
\draw[gray,ultra thin,fill=#1!80!black] (#2,0,0) --++ (0,0,#4) coordinate(-back-bottom-right)
--++ (0,#3,0) --++ (0,0,-#4) -- cycle;
\path[gray,decorate,decoration={text effects along path,text={CONV}}] (#2/2,{2+(#3-2)/2},0) -- (#2/2,0,0);
}
}}
% from https://tex.stackexchange.com/a/52856/121799
\tikzset{circle dotted/.style={dash pattern=on .05mm off 2mm,
                                         line cap=round}}
\begin{document}

\begin{tikzpicture}[x={(1,0)},y={(0,1)},z={({cos(60)},{sin(60)})},
font=\sffamily\small,scale=2]

\foreach \X/\Col in {6.5/red,6.7/green,6.9/blue}
{\draw[canvas is yz plane at x = \X, transform shape, draw = red, fill =
\Col!50!white, opacity = 0.5] (0,0.5) rectangle (2,-1.5);}

\end{tikzpicture}
\end{document}
Harry
  • 349
  • 6
    In general, you will increase the willingness of people to help you if you disclose the source(s) of your code. –  Feb 06 '19 at 16:35
  • 6
    You didn't try. This is a copy of: https://tex.stackexchange.com/questions/439170/drawing-a-cnn-with-tikz (which the author of course remembers :) ) – c.p. Feb 06 '19 at 17:02
  • 1
    You can start from these examples in TeXample.net: http://texample.net/tikz/examples/polarizing-microscope/, http://texample.net/tikz/examples/polarization-controller/, http://texample.net/tikz/examples/events/ – Ignasi Feb 07 '19 at 08:08

1 Answers1

23

None of this is particularly difficult. You would make your life much simpler if you would, instead of copying some code, try to understand it. There is no reason to copy, say, the fake box pic since your desired output does not seem to contain boxes. And yes, giving credit to others does not hurt.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d}
\definecolor{dgreen}{RGB}{63,127,0}
\definecolor{dred}{RGB}{144,14,3}
\begin{document}
\tdplotsetmaincoords{60}{50}
\begin{tikzpicture}[tdplot_main_coords,line join=miter,font=\sffamily]
\path[tdplot_screen_coords] (-2,0); 
\pgfmathsetmacro{\xstretch}{4}
\edef\Cols{red,dgreen,purple,yellow,orange}
\edef\LstCols{{"red","dgreen","purple","yellow","orange"}}
\foreach \XX in {1,2}
 {\begin{scope}[canvas is yz plane at x=-0.8+0.4*\XX]
   \pgfmathtruncatemacro{\fullness}{120-20*\XX}
   \draw[fill=dred!\fullness!red]
   (-1,-1) rectangle (1,1);
  \end{scope}}
\foreach \Col [count=\X starting from 0,remember=\X as \LastX] in \Cols
 {\foreach \XX in {1,2,3,4}
 {\draw[thick,\Col] (\xstretch*\X+0.4*\XX-0.4,0,0) -- (\xstretch*\X+0.4*\XX,0,0);
  \begin{scope}[canvas is yz plane at x=\xstretch*\X+0.4*\XX]
   \pgfmathtruncatemacro{\fullness}{120-20*\XX}
   \draw[fill=\Col!\fullness] (-1,-1) rectangle (1,1);
  \end{scope}}
  \node[anchor=west] at (\xstretch*\X,1,1){$x_\X$};
  \draw[thick,\Col] (\xstretch*\X+0.4*4,0,0) coordinate(front-\X) 
   -- (\xstretch*\X+1+0.4*4,0,0) coordinate(back-\X);
  \begin{scope}[canvas is xy plane at z=0]
   \ifnum\X>0
    \foreach \Z in {0,...,\LastX}
    {\pgfmathsetmacro{\mycol}{\LstCols[\Z]}
    \draw[thick,\mycol,overlay] (front-\Z) to[out=-90,in=-90,looseness=1.5] (back-\X);}
   \fi
  \end{scope}
  \begin{scope}[canvas is yz plane at x=\xstretch*\X+1+0.4*4,transform shape]
   \ifnum\X=4
    \node[draw,fill=white] (BN-\X) at (0,0) {Transition layer};
   \else
    \node[draw,fill=white] (BN-\X) at (0,0) {BN--RelU--Conv};
   \fi
  \end{scope}
  \pgfmathtruncatemacro{\NextX}{\X+1}
  \ifnum\X<4
   \node[anchor=south west] at (BN-\X.north east) {$H_\NextX$};
   \pgfmathsetmacro{\NextCol}{\LstCols[\NextX]}
  \else 
   \pgfmathsetmacro{\NextCol}{"black"} 
  \fi 
  \draw[thick,\NextCol] (\xstretch*\X+1+0.4*4,0,0) -- ({\xstretch*(\X+1)+0.4*4},0,0);
  }
\end{tikzpicture}
\end{document}

enter image description here

The mandatory animation.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d}
\definecolor{dgreen}{RGB}{63,127,0}
\definecolor{dred}{RGB}{144,14,3}
\begin{document}
\foreach \Angle in {0,10,...,350}
{\tdplotsetmaincoords{60}{90-50*cos(\Angle)}
\begin{tikzpicture}[tdplot_main_coords,line join=miter,font=\sffamily]
\path[tdplot_screen_coords,use as bounding box] (-18,-12) rectangle (18,3); 
\pgfmathsetmacro{\xstretch}{4}
\edef\Cols{red,dgreen,purple,yellow,orange}
\edef\LstCols{{"red","dgreen","purple","yellow","orange"}}
\foreach \XX in {1,2}
 {\begin{scope}[canvas is yz plane at x=-0.8+0.4*\XX]
   \pgfmathtruncatemacro{\fullness}{120-20*\XX}
   \draw[fill=dred!\fullness!red]
   (-1,-1) rectangle (1,1);
  \end{scope}}
\foreach \Col [count=\X starting from 0,remember=\X as \LastX] in \Cols
 {\foreach \XX in {1,2,3,4}
 {\draw[thick,\Col] (\xstretch*\X+0.4*\XX-0.4,0,0) -- (\xstretch*\X+0.4*\XX,0,0);
  \begin{scope}[canvas is yz plane at x=\xstretch*\X+0.4*\XX]
   \pgfmathtruncatemacro{\fullness}{120-20*\XX}
   \draw[fill=\Col!\fullness] (-1,-1) rectangle (1,1);
  \end{scope}}
  \node[anchor=west] at (\xstretch*\X,1,1){$x_\X$};
  \draw[thick,\Col] (\xstretch*\X+0.4*4,0,0) coordinate(front-\X) 
   -- (\xstretch*\X+1+0.4*4,0,0) coordinate(back-\X);
  \begin{scope}[canvas is xy plane at z=0]
   \ifnum\X>0
    \foreach \Z in {0,...,\LastX}
    {\pgfmathsetmacro{\mycol}{\LstCols[\Z]}
    \draw[thick,\mycol,overlay] (front-\Z) to[out=-90,in=-90,looseness=1.5] (back-\X);}
   \fi
  \end{scope}
  \begin{scope}[canvas is yz plane at x=\xstretch*\X+1+0.4*4,transform shape]
   \ifnum\X=4
    \node[draw,fill=white] (BN-\X) at (0,0) {Transition layer};
   \else
    \node[draw,fill=white] (BN-\X) at (0,0) {BN--RelU--Conv};
   \fi
  \end{scope}
  \pgfmathtruncatemacro{\NextX}{\X+1}
  \ifnum\X<4
   \node[anchor=south west] at (BN-\X.north east) {$H_\NextX$};
   \pgfmathsetmacro{\NextCol}{\LstCols[\NextX]}
  \else 
   \pgfmathsetmacro{\NextCol}{"black"} 
  \fi 
  \draw[thick,\NextCol] (\xstretch*\X+1+0.4*4,0,0) -- ({\xstretch*(\X+1)+0.4*4},0,0);
  }
\end{tikzpicture}}
\end{document}

enter image description here