Tensorflow中两种Padding运算方式

  • Post author:
  • Post category:其他


原文:

https://blog.csdn.net/jiandanjinxin/article/details/77519629



边界补充问题



原始图片尺寸为7*7,卷积核的大小为3*3,当卷积核沿着图片滑动后只能滑动出一个5*5的图片出来,这就造成了卷积后的图片和卷积前的图片尺寸不一致,这显然不是我们想要的结果,所以为了避免这种情况,需要先对原始图片做边界填充处理。在上面的情况中,我们需要先把原始图像填充为9*9的尺寸。

常用的区域填充方法包括:

用3*3定义原始图像的尺寸,补充为9*9的尺寸,图片上的颜色只为方便观看,并没有任何其他含义。

原始图像:


这里写图片描述


(1)补零

这里写图片描述

(2)边界复制

这里写图片描述

(3)镜像

这里写图片描述

(4)块复制

这里写图片描述


更多理解见


理解图像卷积操作的意义



卷积操作有两个问题:


1. 图像越来越小;

2. 图像边界信息丢失,即有些图像角落和边界的信息发挥作用较少。因此需要padding。



卷积核大小通常为奇数


一方面是为了方便same卷积padding对称填充,左右两边对称补零;

n+2p-f+1=n

p=(f-1)/2

另一方面,奇数过滤器有中心像素,便于确定过滤器的位置。



padding





padding的方式:




这里写图片描述





备注


"VALID" only ever drops the right-most columns (or bottom-most rows).
    "SAME" tries to pad evenly left and right, but if the amount of columns to be added is odd, it will add the extra column to the right, as is the case in this example (the same logic applies vertically: there may be an extra row of zeros at the bottom).
不同的padding方式,VALID是采用丢弃的方式,
比如上述的input_width=13,只允许滑动2次,多余的元素全部丢掉




Tensorflow中的定义



The TensorFlow Convolution example gives an overview about the difference between SAME and VALID :
    For the SAME padding, the output height and width are computed as:
    out_height = ceil(float(in_height) / float(strides[1]))
    out_width = ceil(float(in_width) / float(strides[2]))
And
    For the VALID padding, the output height and width are computed as:
    out_height = ceil(float(in_height - filter_height + 1) / float(strides1))
    out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))





备注


#SAME 向上取整
#VALID 向下取整
输入:n*c0*w0*h0
输出:n*c1*w1*h1
其中,c1就是参数中的num_output,生成的特征图个数
 w1=floor((w0+2*pad-kernel_size)/stride)+1;向下取整
 h1=floor((h0+2*pad-kernel_size)/stride)+1;向下取整
如果设置stride为1,前后两次卷积部分存在重叠。如果设置pad=(kernel_size-1)/2,则运算后,宽度和高度不变。
由pad, kernel_size和stride三者共同决定。



更多细节可见卷积步长

x = tf.constant([[1., 2., 3.],
                 [4., 5., 6.]])

x = tf.reshape(x, [1, 2, 3, 1])  # give a shape accepted by tf.nn.max_pool

valid_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')
same_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

valid_pad.get_shape() == [1, 1, 1, 1]  # valid_pad is [5.]
same_pad.get_shape() == [1, 1, 2, 1]   # same_pad is  [5., 6.]





参考文献:





TensorFlow中CNN的两种padding方式“SAME”和“VALID”



https://stackoverflow.com/questions/37674306/what-is-the-difference-between-same-and-valid-padding-in-tf-nn-max-pool-of-t



Tensorflow中padding的两种类型SAME和VALID



版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jiandanjinxin/article/details/77519629