卷积神经网络中的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

相关文章
相关标签/搜索