卷積神經網絡中的channel 和filter

在深度學習的算法學習中,都會提到 channels 這個概念。在通常的深度學習框架的 conv2d 中,如 tensorflow 、mxnetchannels 都是必填的一個參數。html

channels 該如何理解?先看一看不一樣框架中的解釋文檔。python

首先,是 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). ——tensorflowchrome

其次,mxnet 中提到的,通常 channels 的含義是,每一個卷積層中卷積核的數量。apache

channels (int) : The dimensionality of the output space, i.e. the number of output channels (filters) in the convolution. ——mxnetapi

爲了更直觀的理解,下面舉個例子,圖片使用自 吳恩達老師的深度學習課程 。框架

以下圖,假設現有一個爲 6×6×36×6×3 的圖片樣本,使用 3×3×33×3×3 的卷積核(filter)進行卷積操做。此時輸入圖片的 channels 爲 33 ,而卷積核中的 in_channels 與 須要進行卷積操做的數據的 channels 一致(這裏就是圖片樣本,爲3)。學習

cnn

接下來,進行卷積操做,卷積核中的27個數字與分別與樣本對應相乘後,再進行求和,獲得第一個結果。依次進行,最終獲得 4×44×4 的結果。spa

單個卷積核

上面步驟完成後,因爲只有一個卷積核,因此最終獲得的結果爲 4×4×14×4×1 , out_channels 爲 11 。code

在實際應用中,都會使用多個卷積核。這裏若是再加一個卷積核,就會獲得 4×4×24×4×2 的結果。

多個卷積核

總結一下,我偏好把上面提到的 channels 分爲三種:

  1. 最初輸入的圖片樣本的 channels ,取決於圖片類型,好比RGB;
  2. 卷積操做完成後輸出的 out_channels ,取決於卷積核的數量。此時的 out_channels 也會做爲下一次卷積時的卷積核的 in_channels
  3. 卷積核中的 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] 。

裏面的含義是否是很清楚了?

conv2d

相關文章
相關標籤/搜索