快速入門使用tikz繪制深度學(xué)習(xí)網(wǎng)絡(luò)圖
【GiantPandaCV導(dǎo)語(yǔ)】本文主要介紹最最最基礎(chǔ)的tikz命令和一些繪制CNN時(shí)需要的基礎(chǔ)的LaTeX知識(shí),希望能在盡可能短的時(shí)間內(nèi)學(xué)會(huì)并實(shí)現(xiàn)使用tikz這個(gè)LaTeX工具包來(lái)繪制卷積神經(jīng)網(wǎng)絡(luò)示意圖。

之前看到tikz可以畫出這種圖,感覺(jué)特別專業(yè),所以萌發(fā)出了解一下tikz的想法。
1. overleaf平臺(tái)
在電腦上安裝過(guò)LaTeX都知道,LaTeX安裝包巨大,并且安裝速度緩慢,下載和安裝的時(shí)間需要幾乎一下午才能完成。慶幸的是有一個(gè)平臺(tái)可以在線編譯文檔,那就是overleaf,如今overleaf也推出了中文版本網(wǎng)站:https://cn.overleaf.com/ 以下代碼全部是在overleaf平臺(tái)上編寫運(yùn)行得到的。


最左側(cè)是項(xiàng)目文件列表,中間是代碼編輯區(qū),右側(cè)是可視化區(qū),十分方便,只要網(wǎng)絡(luò)通常,就可以方便地得到結(jié)果。并且這個(gè)平臺(tái)提供了好多模板,可以直接使用,太太太太太棒啦。
2. 快速入門tikz
快速熟悉還是要推薦《minimaltikz》這本電子書,可以直接訪問(wèn)http://cremeronline.com/LaTeX/minimaltikz.pdf獲取或者在后臺(tái)回復(fù)latex獲取。

這本書一共24頁(yè),算是盡量壓縮了內(nèi)容了,在這一節(jié)中將分析一下其中給的幾個(gè)例子,用于快速入門:
所有tikz繪制圖像的代碼都應(yīng)該在tikzpicture這個(gè)環(huán)境中使用。
\begin{tikzpicture}
...
\end{tikzpicture}
直角坐標(biāo)系下:($$)的形式代表二維坐標(biāo)系中的一個(gè)點(diǎn),單位是cm。
極坐標(biāo)系下:(:$
\coordinate可以對(duì)某個(gè)點(diǎn)進(jìn)行重命名如:
\coordinate (s) at (0,1);
2.1 直線
那最基礎(chǔ)的畫幾條線的實(shí)現(xiàn)是通過(guò)\draw完成:
\begin{tikzpicture}
\draw[help lines] (0,0) grid(3,3);
\coordinate (a) at (0,1);
\coordinate (b) at (3,3);
\coordinate (c) at (2,0);
\draw (a) -- (b) -- (c) --cycle;
\end{tikzpicture}
--符號(hào)代表兩點(diǎn)之間的連線,可以連續(xù)鏈接多段。cycle代表讓路徑回到起點(diǎn),生成閉合路徑。

\draw還可以添加選項(xiàng),比如讓線變粗、變紅、箭頭等需求,都很簡(jiǎn)單。
\begin{tikzpicture}[scale=1]
\draw[help lines] (0,0) grid(5,5);
\draw (0,0) -- (1,2)--(3,0) --(5,5);
\draw [->] (0,0) -- (2,1);
\draw [<-] (2,3) -- (5,0);
\draw [|->] (0.5,3) -- (0,4);
\draw [<->] (0,6) -- (0,0) -- (6,0);
\end{tikzpicture}

\begin{tikzpicture}
\draw[help lines] (0,0) grid(5,5);
\draw[thick] (0.5, 0.5) -- (3,3);
% [ultra thick, thick, thin, very thick]
\draw[line width=0.2cm] (1,0) -- (3,2);
\end{tikzpicture}

\begin{tikzpicture}
\draw[help lines] (0,0) grid(5,5);
\draw[ultra thick, dotted] (0,0) -- (2,3);
\draw[line width=0.2cm, dotted,red] (2,2) -- (4,0);
%[red, blue, green, cyan, magenta, yellow, black, gray, darkgray, lightgray, browbn, lime, olive, orange, pink, purple, teal, violet, white]
\end{tikzpicture}

2.2 曲線
畫一些曲線就需要使用circle、rectangle、arc等進(jìn)行約束。
\begin{tikzpicture}
\draw[help lines] (0,0) grid(5,5);
\draw[blue] (1,1) rectangle(3,3); % 正方形 需要左下角坐標(biāo)和右上角坐標(biāo)
\draw[red] (2,2) circle[radius=2]; %圓形 需要圓心坐標(biāo)和半徑
\draw[green] (1,0) arc [radius=1,start angle=180,end angle=360];
\draw[<->, rounded corners, thick, purple] (0,5) -- (0,0) -- (5,0);
\end{tikzpicture}

\begin{tikzpicture}
\draw[help?lines]?(0,0)?grid(6,3);
\draw[blue,?thick]?(0,0)?to[out=90,in=180]?(1,1)?to[in=270,out=360]?(2,2)
to[in=180,out=90]?(3,3)?to[in=90,out=360]?(4,2)?to[in=180,out=270]?(5,1)?
to[in=90,?out=0]?(6,0);
\end{tikzpicture}
這是練習(xí)畫弧線的時(shí)候想練習(xí)的一個(gè)例子,結(jié)果如下

in代表進(jìn)入的角度,out代表出來(lái)時(shí)候的角度,為了方便,筆者畫了一個(gè)輔助圖,對(duì)照代碼方便理解。

2.3 畫函數(shù)曲線
\begin{tikzpicture}[xscale=6,yscale=6]
\draw[<->] (0,0.8) -- (0,0) -- (0.8,0);
\draw[green,thick,domain=0:0.5]
plot(\x, {0.025+\x*\x});
\draw[red, thick, domain=0:0.5]
plot(\x, {sqrt(\x)});
\draw[blue, thick, domain=0:0.5]
plot(\x, {abs(\x)});
\end{tikzpicture}
domain限制變量范圍,然后可以畫圖,結(jié)果如下:

2.4 填充
\begin{tikzpicture}
\draw[fill=red,ultra thick] (0,0) rectangle(1,1);
\draw[fill=red,ultra thin, red] (2,0) rectangle(3,1);
\draw[fill] (5,0) circle[radius=1];
\draw [fill=orange] (9,0) rectangle (11,1);
\draw [fill=white] (9.25,0.25) rectangle (10,1.5);
\path [fill=gray] (0,-2) rectangle (1.5,-3);
\draw [fill=yellow] (2,-2) rectangle (3.5,-3);
\end{tikzpicture}
通過(guò)fill參數(shù)控制結(jié)果,效果如下:

2.6 添加文字
使用\node
\node?[]?()?at?()?{};
舉個(gè)例子:
\begin{tikzpicture}[scale=2]
\draw [thick, <->] (0,1) -- (0,0) -- (1,0);
\draw[fill] (1,1) circle [radius=0.025];
\node [below right, red] at (.5,.75) {below right};
\node [above left, green] at (.5,.75) {above left};
\node [below left, purple] at (.5,.75) {below left};
\node [above right, magenta] at (.5,.75) {above right};
\end{tikzpicture}

其實(shí)CNN畫圖主要用的是畫一條線的功能,下面來(lái)看如何畫CNN。
3. 繪制一個(gè)CNN模塊
對(duì)于一個(gè)初學(xué)者來(lái)說(shuō),https://github.com/HarisIqbal88/PlotNeuralNet 這個(gè)庫(kù)雖然畫的很好,但是難度曲線太高了,退而求其次,使用https://github.com/pprp/SimpleCVReproduction/tree/master/tikz_cnn 進(jìn)行解析。
首先介紹一個(gè)LaTeX中用于封裝的命令,\newcommand,當(dāng)我們不希望寫很長(zhǎng)的命令,那就需要類似函數(shù)的一個(gè)方式,封裝好固定的操作,根據(jù)傳入?yún)?shù)完成執(zhí)行。
\newcommand<命令>[<參數(shù)個(gè)數(shù)>][<首參數(shù)默認(rèn)值>]{<具體的定義>}
舉一個(gè)例子:
\newcommand\loves[2]{#1 喜歡 #2}
\loves{我}{你}
輸出結(jié)果就是:我喜歡你
\newcommand{\networkLayer}[9]{
% Define the macro.
% 1st argument: Height and width of the layer rectangle slice.
% 2nd argument: Depth of the layer slice
% 3rd argument: X Offset --> use it to offset layers from previously drawn layers.
% 4th argument: Y Offset --> Use it when an output needs to be fed to multiple layers that are on the same X offset.
% 5th argument: Z Offset --> Use to offset layers from previous
% 6th argument: Options for filldraw.
% 7th argument: Text to be placed below this layer.
% 8th argument: Name of coordinates. When name = "start" this resets the offset counter
% 9th argument: list of nodes to connect to (previous layers)
% 全局變量
\xdef\totalOffset{\totalOffset}
\ifthenelse{\equal{#8} {start}}
{\FPset{totalOffset}{0}}
{}
\FPeval\currentOffset{0+(totalOffset)+(#3)}
\def\hw{#1} % Used to distinguish input resolution for current layer.
\def\b{0.02}
\def\c{#2} % Width of the cube to distinguish number of input channels for current layer.
\def\x{\currentOffset} % X offset for current layer.
\def\y{#4} % Y offset for current layer.
\def\z{#5} % Z offset for current layer.
\def\inText{#7}
% Define references to points on the cube surfaces
\coordinate (#8_front) at (\x+\c , \z , \y);
\coordinate (#8_back) at (\x , \z , \y);
\coordinate (#8_top) at (\x+\c/2, \z+\hw/2, \y);
\coordinate (#8_bottom) at (\x+\c/2, \z-\hw/2, \y);
% Define cube coords
\coordinate (blr) at (\c+\x, -\hw/2+\z, -\hw/2+\y); %back lower right
\coordinate (bur) at (\c+\x, \hw/2+\z, -\hw/2+\y); %back upper right
\coordinate (bul) at (0 +\x, \hw/2+\z, -\hw/2+\y); %back upper left
\coordinate (fll) at (0 +\x, -\hw/2+\z, \hw/2+\y); %front lower left
\coordinate (flr) at (\c+\x, -\hw/2+\z, \hw/2+\y); %front lower right
\coordinate (fur) at (\c+\x, \hw/2+\z, \hw/2+\y); %front upper right
\coordinate (ful) at (0 +\x, \hw/2+\z, \hw/2+\y); %front upper left
% Draw connections from other points to the back of this node
\ifthenelse{\equal{#9} {}}
{} % 為空什么都不做
{ % 非空 開始畫層與層之間的連線
\foreach \val in #9
% \val = start_front
\draw[line width=0.3mm] (\val)--(#8_back);
}
% Draw the layer body.
% back plane
\draw[line width=0.3mm](blr) -- (bur) -- (bul);
% front plane
\draw[line width=0.3mm](fll) -- (flr) node[midway,below] {\inText} -- (fur) -- (ful) -- (fll);
\draw[line width=0.3mm](blr) -- (flr);
\draw[line width=0.3mm](bur) -- (fur);
\draw[line width=0.3mm](bul) -- (ful);
% Recolor visible surfaces
% front plane
\filldraw[#6] ($(fll)+(\b,\b,0)$) -- ($(flr)+(-\b,\b,0)$) -- ($(fur)+(-\b,-\b,0)$) -- ($(ful)+(\b,-\b,0)$) -- ($(fll)+(\b,\b,0)$);
\filldraw[#6] ($(ful)+(\b,0,-\b)$) -- ($(fur)+(-\b,0,-\b)$) -- ($(bur)+(-\b,0,\b)$) -- ($(bul)+(\b,0,\b)$);
% Colored slice.
\ifthenelse {\equal{#6} {}}
{} % Do not draw colored slice if #6 is blank.
% Else, draw a colored slice.
{\filldraw[#6] ($(flr)+(0,\b,-\b)$) -- ($(blr)+(0,\b,\b)$) -- ($(bur)+(0,-\b,\b)$) -- ($(fur)+(0,-\b,-\b)$);}
\FPeval\totalOffset{0+(currentOffset)+\c}
\draw[ultra thick, red] (#8_back) circle[radius=0.02];
\node[left] at (#8_back) {back};
\draw[ultra thick, red] (#8_top) circle[radius=0.02];
\node[above] at (#8_top) {top};
\draw[ultra thick, red] (#8_bottom) circle[radius=0.02];
\node[below] at (#8_bottom) {bottom};
\draw[ultra thick, red] (#8_front) circle[radius=0.02];
\node[left] at (#8_front) {front};
}
假設(shè)以下命令調(diào)用,結(jié)果會(huì)是什么?
\begin{tikzpicture}[scale=2]
\draw[help lines] (0,0) grid(2,2);
\draw[->,thick] (0,0,0) -- (0,0,2);
\draw[->,thick] (0,0,0) -- (0,2,0);
\draw[->,thick] (0,0,0) -- (2,0,0);
\draw[->,thick] (0,0,0) -- (2,2,0);
\draw[->,thick] (0,0,0) -- (1,2,0);
\draw[->,thick] (0,0,0) -- (0,2,2);
\draw[->,thick] (0,0,0) -- (2,0,2);
\draw[dotted,thick] (0,0,2) -- (0,2,2);
\draw[dotted,thick] (0,2,0) -- (0,2,2);
\draw[dotted,thick] (0,0,2) -- (2,0,2);
\draw[dotted,thick] (2,0,0) -- (2,0,2);
\draw[dotted,thick] (1,0,0) -- (1,0,2);
\draw[dotted,thick] (0,0,1) -- (2,0,1);
\draw[<->, thick] (0,2) -- (0,0) -- (2,0);
%HW -D - x- y- z - fill color - text - 坐標(biāo) - 鏈接
\networkLayer{1}{0.5}{0}{0}{0}{color=green!20}{conv1}{}{}
\end{tikzpicture}
顯示結(jié)果如下:

卷積神經(jīng)網(wǎng)絡(luò)的示意圖實(shí)際上是一個(gè)個(gè)立方體構(gòu)成的,立方體之間可能會(huì)有額外連線,代表特征融合;還可能需要題注,為這個(gè)特征圖立方體進(jìn)行命名;必須要有立方體的位置信息,長(zhǎng)寬高;還需要顏色填充的功能;
綜合以上需求,這個(gè)函數(shù)提供了9個(gè)參數(shù)分別是:
#1 H&W 控制立方體右側(cè)這一面的高度,默認(rèn)為正方形。 #2 Depth 控制深度 #3 X 方向上的偏置 #4 Y方向上的偏置 #5 Z方向上的偏置 #6 填充的顏色 #7 Text展示的文本,放在最下側(cè) #8 坐標(biāo)名稱,通過(guò)命名便于#9訪問(wèn) #9 通過(guò)名稱指定連接位置,用于連接前方層的時(shí)候使用

由于每繪制一個(gè)立方體,右側(cè)立方體的X偏置就應(yīng)該加上左側(cè)立方體的Depth值,這部分代碼這樣處理的。
\FPset{totalOffset}{0} % 設(shè)置全局變量totaloffset
\xdef\totalOffset{\totalOffset}
\ifthenelse{\equal{#8} {start}}
% 如果#8坐標(biāo)名稱為start,那么將totaloffset歸零
{\FPset{totalOffset}{0}}
{}% 否則什么都不做
\FPeval\currentOffset{0+(totalOffset)+(#3)}
% 計(jì)算當(dāng)前offset也就是#3 X+totalOffset
賦值過(guò)程:
\def\hw{#1} % Used to distinguish input resolution for current layer.
\def\b{0.02}
\def\c{#2} % Width of the cube to distinguish number of input channels for current layer.
\def\x{\currentOffset} % X offset for current layer.
\def\y{#4} % Y offset for current layer.
\def\z{#5} % Z offset for current layer.
\def\inText{#7}
計(jì)算立方體表面坐標(biāo)(將點(diǎn)可視化是額外添加的,為了便于理解)

% Define references to points on the cube surfaces
\coordinate (#8_front) at (\x+\c , \z , \y);
\coordinate (#8_back) at (\x , \z , \y);
\coordinate (#8_top) at (\x+\c/2, \z+\hw/2, \y);
\coordinate (#8_bottom) at (\x+\c/2, \z-\hw/2, \y);
計(jì)算7個(gè)頂點(diǎn)位置,被擋住的也可以計(jì)算,但是因?yàn)檫@里不打算繪制所以不計(jì)算。

%?Define?cube?coords
\coordinate?(blr)?at?(\c+\x,??-\hw/2+\z,??-\hw/2+\y);?%back?lower?right
\coordinate?(bur)?at?(\c+\x,???\hw/2+\z,??-\hw/2+\y);?%back?upper?right
\coordinate?(bul)?at?(0?+\x,???\hw/2+\z,??-\hw/2+\y);?%back?upper?left
\coordinate?(fll)?at?(0?+\x,??-\hw/2+\z,???\hw/2+\y);?%front?lower?left
\coordinate?(flr)?at?(\c+\x,??-\hw/2+\z,???\hw/2+\y);?%front?lower?right
\coordinate?(fur)?at?(\c+\x,???\hw/2+\z,???\hw/2+\y);?%front?upper?right
\coordinate?(ful)?at?(0?+\x,???\hw/2+\z,???\hw/2+\y);?%front?upper?left
繪制立方塊之間的連線:
% Draw connections from other points to the back of this node
\ifthenelse{\equal{#9} {}}
{} % 為空什么都不做
{ % 非空 開始畫層與層之間的連線
\foreach \val in #9
% \val = start_front
\draw[line width=0.3mm] (\val)--(#8_back);
}
繪制立方體主體部分,也就是將7個(gè)點(diǎn)連接起來(lái)。
% back plane
\draw[line width=0.3mm](blr) -- (bur) -- (bul);
% front plane
\draw[line width=0.3mm](fll) -- (flr) node[midway,below] {\inText} -- (fur) -- (ful) -- (fll);
\draw[line width=0.3mm](blr) -- (flr);
\draw[line width=0.3mm](bur) -- (fur);
\draw[line width=0.3mm](bul) -- (ful);
填充顏色:
% front plane
\filldraw[#6] ($(fll)+(\b,\b,0)$) -- ($(flr)+(-\b,\b,0)$) -- ($(fur)+(-\b,-\b,0)$) -- ($(ful)+(\b,-\b,0)$) -- ($(fll)+(\b,\b,0)$);
\filldraw[#6] ($(ful)+(\b,0,-\b)$) -- ($(fur)+(-\b,0,-\b)$) -- ($(bur)+(-\b,0,\b)$) -- ($(bul)+(\b,0,\b)$);
% Colored slice.
\ifthenelse {\equal{#6} {}}
{} % Do not draw colored slice if #6 is blank.
% Else, draw a colored slice.
{\filldraw[#6] ($(flr)+(0,\b,-\b)$) -- ($(blr)+(0,\b,\b)$) -- ($(bur)+(0,-\b,\b)$) -- ($(fur)+(0,-\b,-\b)$);}

上邊的圖是通過(guò)以下代碼生成的:
\begin{tikzpicture}
% INPUT
\networkLayer{3.0}{0.03}{0.0}{0.0}{0.0}{color=gray!80}{}{start}{}
% ENCODER
\networkLayer{3.0}{0.1}{0.5}{0.0}{0.0}{color=white}{conv}{}{} % S1
\networkLayer{3.0}{0.1}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2
\networkLayer{2.5}{0.2}{0.1}{0.0}{0.0}{color=white}{conv}{}{} % S1
\networkLayer{2.5}{0.2}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2
\networkLayer{2.0}{0.4}{0.1}{0.0}{0.0}{color=white}{conv}{}{} % S1
\networkLayer{2.0}{0.4}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2
\networkLayer{1.5}{0.8}{0.1}{0.0}{0.0}{color=white}{conv}{}{} % S1
\networkLayer{1.5}{0.8}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2
\networkLayer{1.0}{1.5}{0.1}{0.0}{0.0}{color=white}{conv}{}{} % S1
\networkLayer{1.0}{1.5}{0.1}{0.0}{0.0}{color=white}{}{mid}{} % S2
\networkLayer{1.0}{0.5}{1.5}{0.0}{-1.5}{color=green!50}{}{bot}{{mid_front}}
\networkLayer{1.0}{0.5}{-0.5}{0.0}{1.5}{color=green!50}{}{top}{{mid_front}}
\networkLayer{1.0}{0.5}{1.5}{0.0}{0.0}{color=blue!50}{sum}{}{{bot_front,top_front}}
% DECODER
\networkLayer{1.0}{1.5}{0.1}{0.0}{0.0}{color=white}{deconv}{}{} % S1
\networkLayer{1.0}{1.5}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2
\networkLayer{1.5}{0.8}{0.1}{0.0}{0.0}{color=white}{deconv}{}{} % S1
\networkLayer{1.5}{0.8}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2
\networkLayer{2.0}{0.4}{0.1}{0.0}{0.0}{color=white}{}{}{} % S1
\networkLayer{2.0}{0.4}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2
\networkLayer{2.5}{0.2}{0.1}{0.0}{0.0}{color=white}{}{}{} % S1
\networkLayer{2.5}{0.2}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2
\networkLayer{3.0}{0.1}{0.1}{0.0}{0.0}{color=white}{}{}{} % S1
\networkLayer{3.0}{0.1}{0.1}{0.0}{0.0}{color=white}{}{}{} % S2
% OUTPUT
\networkLayer{3.0}{0.05}{0.9}{0.0}{0.0}{color=red!40}{}{}{} % Pixelwise segmentation with classes.
\end{tikzpicture}
需要注意的是#8和#9命令,mid_front代表的是鏈接#8=mid的front部分,front也可以被top、back、bottom取代。

4. 資源推薦
https://cn.overleaf.com/project/5e8c38c31cccb20001a4998d
https://cn.overleaf.com/project/5f50b21ae802b6000155ec4f
https://github.com/HarisIqbal88/PlotNeuralNet
https://github.com/pprp/SimpleCVReproduction/tree/master/tikz_cnn
- END -對(duì)文章有疑問(wèn)或者希望加群交流,可以添加筆者微信
