在深度學習的算法學習中,都會提到 channels
這個概念。在一般的深度學習框架的 conv2d
中,如 tensorflow 、mxnet,channels
都是必填的一個參數。
channels
該如何理解?先看一看不同框架中的解釋文檔。
首先,是 tensorflow 中給出的,對於輸入樣本中 channels
的含義。一般的RGB圖片,channels
數量是 3 (紅、綠、藍);而monochrome圖片,channels
數量是 1 。
channels : Number of color channels in the example images. For color images, the number of channels is 3 (red, green, blue). For monochrome images, there is just 1 channel (black). ——tensorflow
其次,mxnet 中提到的,一般 channels
的含義是,每個卷積層中卷積核的數量。
channels (int) : The dimensionality of the output space, i.e. the number of output channels (filters) in the convolution. ——mxnet
為了更直觀的理解,下面舉個例子,圖片使用自 吳恩達老師的深度學習課程 。
如下圖,假設現有一個為 6×6×36×6×3 的圖片樣本,使用 3×3×33×3×3 的卷積核(filter)進行卷積操作。此時輸入圖片的 channels
為 33 ,而卷積核中的 in_channels
與 需要進行卷積操作的數據的 channels
一致(這里就是圖片樣本,為3)。
接下來,進行卷積操作,卷積核中的27個數字與分別與樣本對應相乘后,再進行求和,得到第一個結果。依次進行,最終得到 4×44×4 的結果。
上面步驟完成后,由於只有一個卷積核,所以最終得到的結果為 4×4×14×4×1 , out_channels
為 11 。
在實際應用中,都會使用多個卷積核。這里如果再加一個卷積核,就會得到 4×4×24×4×2 的結果。
總結一下,我偏好把上面提到的 channels
分為三種:
- 最初輸入的圖片樣本的
channels
,取決於圖片類型,比如RGB; - 卷積操作完成后輸出的
out_channels
,取決於卷積核的數量。此時的out_channels
也會作為下一次卷積時的卷積核的in_channels
; - 卷積核中的
in_channels
,剛剛2中已經說了,就是上一次卷積的out_channels
,如果是第一次做卷積,就是1中樣本圖片的channels
。
說到這里,相信已經把 channels
講的很清楚了。在CNN中,想搞清楚每一層的傳遞關系,主要就是 height
,width
的變化情況,和 channels
的變化情況。
最后再看看 tensorflow 中 tf.nn.conv2d
的 input
和 filter
這兩個參數。 input : [batch, in_height, in_width, in_channels]
, filter : [filter_height, filter_width, in_channels, out_channels]
。
里面的含義是不是很清楚了?