tensorflow:卷积函数----tf.nn.conv2d

1. 卷积概念

卷积的过程:如下图所示,用一个3*3的卷积核5*5的图像上做卷积的过程。

卷积核如下,大小3*3,在原图上滑动的步长为1。(求解过程是:对应位置相成,然后相加

我们再看一个在三通道图像上的卷积过程,如下:

计算步骤解释如下,原图大小为7*7,通道数为3:,卷积核大小为3*3,Input Volume中的蓝色方框和Filter W0中红色方框的对应位置元素相乘再求和得到res(即,下图中的步骤1.res的计算),再把res和Bias b0进行相加(即,下图中的步骤2),得到最终的Output Volume。

2. 卷积函数tf.nn.conv2d

函数源码目录如下(TensorFlow安装方式参考 TensorFlow 学习笔记-01):

C:\Anaconda3\envs\tensorflow\Lib\site-packages\tensorflow\python\ops\gen_nn_ops.py

[python] view plain copy

  1. def conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None,  
  2.            data_format=None, name=None):  
  3.   r"""Computes a 2-D convolution given 4-D `input` and `filter` tensors. 
  4.  
  5.   Given an input tensor of shape `[batch, in_height, in_width, in_channels]` 
  6.   and a filter / kernel tensor of shape 
  7.   `[filter_height, filter_width, in_channels, out_channels]`, this op 
  8.   performs the following: 
  9.  
  10.   1. Flattens the filter to a 2-D matrix with shape 
  11.      `[filter_height * filter_width * in_channels, output_channels]`. 
  12.   2. Extracts image patches from the input tensor to form a *virtual* 
  13.      tensor of shape `[batch, out_height, out_width, 
  14.      filter_height * filter_width * in_channels]`. 
  15.   3. For each patch, right-multiplies the filter matrix and the image patch 
  16.      vector. 
  17.  
  18.   In detail, with the default NHWC format, 
  19.  
  20.       output[b, i, j, k] = 
  21.           sum_{di, dj, q} input[b, strides[1] * i + di, strides[2] * j + dj, q] * 
  22.                           filter[di, dj, q, k] 
  23.  
  24.   Must have `strides[0] = strides[3] = 1`.  For the most common case of the same 
  25.   horizontal and vertices strides, `strides = [1, stride, stride, 1]`. 
  26.  
  27.   Args: 
  28.     input: A `Tensor`. Must be one of the following types: `half`, `float32`. 
  29.       A 4-D tensor. The dimension order is interpreted according to the value 
  30.       of `data_format`, see below for details. 
  31.     filter: A `Tensor`. Must have the same type as `input`. 
  32.       A 4-D tensor of shape 
  33.       `[filter_height, filter_width, in_channels, out_channels]` 
  34.     strides: A list of `ints`. 
  35.       1-D tensor of length 4.  The stride of the sliding window for each 
  36.       dimension of `input`. The dimension order is determined by the value of 
  37.         `data_format`, see below for details. 
  38.     padding: A `string` from: `"SAME", "VALID"`. 
  39.       The type of padding algorithm to use. 
  40.     use_cudnn_on_gpu: An optional `bool`. Defaults to `True`. 
  41.     data_format: An optional `string` from: `"NHWC", "NCHW"`. Defaults to `"NHWC"`. 
  42.       Specify the data format of the input and output data. With the 
  43.       default format "NHWC", the data is stored in the order of: 
  44.           [batch, height, width, channels]. 
  45.       Alternatively, the format could be "NCHW", the data storage order of: 
  46.           [batch, channels, height, width]. 
  47.     name: A name for the operation (optional). 
  48.  
  49.   Returns: 
  50.     A `Tensor`. Has the same type as `input`. 
  51.     A 4-D tensor. The dimension order is determined by the value of 
  52.     `data_format`, see below for details. 
  53.   """  
  54.   result = _op_def_lib.apply_op("Conv2D", input=input, filter=filter,  
  55.                                 strides=strides, padding=padding,  
  56.                                 use_cudnn_on_gpu=use_cudnn_on_gpu,  
  57.                                 data_format=data_format, name=name)  
  58.   return result  
  59.   
  60.   
  61. _conv2d_backprop_filter_outputs = ["output"]

通过conv2d源码我们可以发现一共有7个参数,参数的详细分析如下:

第一个参数:input

[python] view plain copy

  1. input: A `Tensor`. Must be one of the following types: `half`, `float32`.  
  2.       A 4-D tensor. The dimension order is interpreted according to the value  
  3.       of `data_format`, see below for details.

[python] view plain copy

  1. input tensor of shape `[batch, in_height, in_width, in_channels]`

通过源码中的描述(如上),我们可以知道input就是需要做卷积的图像(这里要求用Tensor来表示输入图像,并且Tensor(一个4维的Tensor,要求类型为half或者float32)的shape为[batch, in_height, in_width, in_channels]具体含义[训练时一个batch图像的数量,图像高度,图像宽度, 图像通道数])。

第二个参数:filter

[python] view plain copy

  1. filter: A `Tensor`. Must have the same type as `input`.  
  2.       A 4-D tensor of shape  
  3.       `[filter_height, filter_width, in_channels, out_channels]` 

通过源码中的描述(如上),我们可以知道filter就是卷积核(这里要求用Tensor来表示卷积核,并且Tensor(一个4维的Tensor,要求类型与input相同)的shape为[filter_height, filter_width, in_channels, out_channels]具体含义[卷积核高度,卷积核宽度,图像通道数,卷积核个数],这里的图片通道数也就input中的图像通道数,二者相同。)

第三个参数:strides

[python] view plain copy

  1. strides: A list of `ints`.  
  2.       1-D tensor of length 4.  The stride of the sliding window for each  
  3.       dimension of `input`. The dimension order is determined by the value of  
  4.         `data_format`, see below for details.

通过源码中的描述(如上),我们可以知道strides就是卷积操作时在图像每一维的步长,strides是一个长度为4的一维向量。

第四个参数:padding

[python] view plain copy

  1. padding: A `string` from: `"SAME", "VALID"`.  
  2.       The type of padding algorithm to use.

通过源码中的描述(如上),我们知道padding是一个string类型的变量,只能是 "SAME" 或者 "VALID",决定了两种不同的卷积方式。下面我们来介绍 "SAME" 和 "VALID" 的卷积方式,如下图我们使用单通道的图像,图像大小为5*5,卷积核用3*3

 "VALID" 卷积方式

具体卷积操作如下图(也是文中一开始用到的图),我们考虑卷积核中心点(这里卷积核大小是3*3,)走过的位置,

如下所示,红色#表示卷积核中心点在图像上的滑动过程。最后得到3*3的图像大小。

#####

#####

#####

#####

#####

"SAME"卷积方式

对于上图,图像的每一个点都作为卷积核的中心。最后得到5*5的结果,如下图:

通俗的来说:首先在原图外层补一圈0,将原图的第一点作为卷积核中心,若一圈0不够,继续补一圈0。

第五个参数:use_cudnn_on_gpu

[python] view plain copy

  1. use_cudnn_on_gpu: An optional `bool`. Defaults to `True`.

通过源码中的描述(如上),我们知道use_cudnn_on_gpu就是选择是否用GPU进行运算加速。默认为True。

第六个参数:data_format

[python] view plain copy

  1. data_format: An optional `string` from: `"NHWC", "NCHW"`. Defaults to `"NHWC"`.  
  2.       Specify the data format of the input and output data. With the  
  3.       default format "NHWC", the data is stored in the order of:  
  4.           [batch, height, width, channels].  
  5.       Alternatively, the format could be "NCHW", the data storage order of:  
  6.           [batch, channels, height, width].

通过源码中的描述(如上),我们知道data_format就是input的Tensor格式,一般默认就可以了。都采用NHWC。

第七个参数:name

[python] view plain copy

  1. name: A name for the operation (optional).

就是用以指定该操作的name,仅此而已。

函数返回值:

[python] view plain copy

  1. Returns:  
  2.     A `Tensor`. Has the same type as `input`.  
  3.     A 4-D tensor. The dimension order is determined by the value of  
  4.     `data_format`, see below for details.

返回卷积操作后的特征图

猜你喜欢

转载自blog.csdn.net/haoji007/article/details/81157382