在tf.nn中“SAME”和“VALID”填充之间的区别是什么?tensorflow的Max_pool ?

在我看来,'VALID'意味着当我们做max pool时,边缘外不会有零填充。

根据深度学习卷积算法指南,它说池操作符中不会有填充,即只使用tensorflow的“VALID”。 但什么是'SAME'填充的最大池张量流量?


TensorFlow Convolution的例子概述了SAME和VALID的区别:

对于相同的填充,输出的高度和宽度计算如下: Out_height = ceil(float(in_height) / float(strides[1])) Out_width = ceil(float(in_width) / float(strides[2]))

And

对于VALID填充,输出高度和宽度的计算如下: Out_height = ceil(float(in_height - filter_height + 1) / float(strides[1])) Out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))

我举个例子来说明:

X:输入形状[2,3]的图像,1通道 valid_pad: max pool with 2x2 kernel, stride 2和VALID padding。 same_pad: max pool with 2x2 kernel, stride 2和SAME padding(这是经典的方法)

输出形状为:

Valid_pad:这里没有填充,所以输出形状是[1,1] Same_pad:在这里,我们将图像填充到形状[2,4](使用-inf,然后应用Max pool),因此输出形状是[1,2]


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.]

如果你喜欢ascii艺术:

"VALID" = without padding: inputs: 1 2 3 4 5 6 7 8 9 10 11 (12 13) |________________| dropped |_________________| "SAME" = with zero padding: pad| |pad inputs: 0 |1 2 3 4 5 6 7 8 9 10 11 12 13|0 0 |________________| |_________________| |________________|

在这个例子中:

输入宽度= 13 滤镜宽度= 6 步幅= 5

注:

"VALID"只删除最右边的列(或最底部的行)。 “SAME”尝试均匀地左右填充,但如果要添加的列的数量是奇数,它会将额外的列添加到右侧,就像本例中的情况一样(垂直方向上的逻辑相同:底部可能有额外的一行零)。

编辑:

关于名字:

对于“SAME”填充,如果你使用的步幅为1,该层的输出将具有与其输入相同的空间维度。 使用“VALID”填充,就没有“编造”填充输入。该层只使用有效的输入数据。

根据这里的解释和Tristan的回答,我通常使用这些快速函数进行完整性检查。

# a function to help us stay clean
def getPaddings(pad_along_height,pad_along_width):
    # if even.. easy..
    if pad_along_height%2 == 0:
        pad_top = pad_along_height / 2
        pad_bottom = pad_top
    # if odd
    else:
        pad_top = np.floor( pad_along_height / 2 )
        pad_bottom = np.floor( pad_along_height / 2 ) +1
    # check if width padding is odd or even
    # if even.. easy..
    if pad_along_width%2 == 0:
        pad_left = pad_along_width / 2
        pad_right= pad_left
    # if odd
    else:
        pad_left = np.floor( pad_along_width / 2 )
        pad_right = np.floor( pad_along_width / 2 ) +1
        #
    return pad_top,pad_bottom,pad_left,pad_right

# strides [image index, y, x, depth]
# padding 'SAME' or 'VALID'
# bottom and right sides always get the one additional padded pixel (if padding is odd)
def getOutputDim (inputWidth,inputHeight,filterWidth,filterHeight,strides,padding):
    if padding == 'SAME':
        out_height = np.ceil(float(inputHeight) / float(strides[1]))
        out_width  = np.ceil(float(inputWidth) / float(strides[2]))
        #
        pad_along_height = ((out_height - 1) * strides[1] + filterHeight - inputHeight)
        pad_along_width = ((out_width - 1) * strides[2] + filterWidth - inputWidth)
        #
        # now get padding
        pad_top,pad_bottom,pad_left,pad_right = getPaddings(pad_along_height,pad_along_width)
        #
        print 'output height', out_height
        print 'output width' , out_width
        print 'total pad along height' , pad_along_height
        print 'total pad along width' , pad_along_width
        print 'pad at top' , pad_top
        print 'pad at bottom' ,pad_bottom
        print 'pad at left' , pad_left
        print 'pad at right' ,pad_right

    elif padding == 'VALID':
        out_height = np.ceil(float(inputHeight - filterHeight + 1) / float(strides[1]))
        out_width  = np.ceil(float(inputWidth - filterWidth + 1) / float(strides[2]))
        #
        print 'output height', out_height
        print 'output width' , out_width
        print 'no padding'


# use like so
getOutputDim (80,80,4,4,[1,1,1,1],'SAME')

当stride为1时(卷积比池化更典型),我们可以想到以下区别:

“SAME”:输出大小与输入大小相同。这就要求滤镜窗口要在输入贴图外滑动,因此需要垫片。 "VALID":过滤器窗口保持在输入映射中的有效位置,因此输出大小缩小为filter_size - 1。没有填充。

有三种填充选择:有效(无填充),相同(或一半),满。你可以在这里(Theano)找到解释: http://deeplearning.net/software/theano/tutorial/conv_arithmetic.html

有效或无填充:

有效填充不涉及零填充,因此它只覆盖有效输入,不包括人工生成的零。对于内核大小为k的步幅s=1,则输出长度为((输入长度)- (k-1))。

相同或半填充:

当s=1时,相同的填充使输出的大小与输入的大小相同。如果s=1,补零的个数为(k-1)。

完全填充:

完全填充意味着内核运行整个输入,因此在结束时,内核可能只满足一个输入,其他为零。如果s=1,填充的零的数量是2(k-1)。如果s=1,则输出长度为((输入长度)+ (k-1))。

因此,填充的数量:(有效)<=(相同)<=(满)

填充是一种增加输入数据大小的操作。在一维数据中,你只需要在数组前加上一个常数,在2-dim中,你用这些常数包围矩阵。在n-dim中,用常数包围n-dim超立方体。在大多数情况下,这个常数是零,它被称为零填充。

下面是一个应用于2-d张量的p=1的零填充的例子:


你可以为你的内核使用任意填充,但是有些填充值比其他填充值使用得更频繁:

有效的填充。最简单的情况,意味着根本没有填充。让你的数据保持原样。 相同填充有时称为半填充。之所以称为SAME,是因为对于stride=1的卷积(或池化),它应该产生与输入相同大小的输出。之所以叫HALF是因为对于一个大小为k的核 FULL填充是最大填充,它不会导致对刚刚填充的元素进行卷积。对于一个大小为k的核,这个填充值等于k - 1。


要在TF中使用任意填充,可以使用TF .pad()

我引用了官方tensorflow文档https://www.tensorflow.org/api_guides/python/nn#Convolution中的答案 对于'SAME'填充,输出高度和宽度的计算如下:

out_height = ceil(float(in_height) / float(strides[1]))
out_width  = ceil(float(in_width) / float(strides[2]))

顶部和左侧的填充被计算为:

pad_along_height = max((out_height - 1) * strides[1] +
                    filter_height - in_height, 0)
pad_along_width = max((out_width - 1) * strides[2] +
                   filter_width - in_width, 0)
pad_top = pad_along_height // 2
pad_bottom = pad_along_height - pad_top
pad_left = pad_along_width // 2
pad_right = pad_along_width - pad_left

对于'VALID'填充,输出高度和宽度的计算如下:

out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))
out_width  = ceil(float(in_width - filter_width + 1) / float(strides[2]))

填充值总是0。

快速的解释

VALID:不要应用任何填充,也就是说,假设所有的维度都是有效的,这样输入的图像就会被你指定的过滤器和stride完全覆盖。

SAME:应用填充到输入(如果需要),以便输入图像被过滤器和步幅完全覆盖。对于stride 1,这将确保输出图像大小与输入相同。

笔记

This applies to conv layers as well as max pool layers in same way The term "valid" is bit of a misnomer because things don't become "invalid" if you drop part of the image. Sometime you might even want that. This should have probably be called NO_PADDING instead. The term "same" is a misnomer too because it only makes sense for stride of 1 when output dimension is same as input dimension. For stride of 2, output dimensions will be half, for example. This should have probably be called AUTO_PADDING instead. In SAME (i.e. auto-pad mode), Tensorflow will try to spread padding evenly on both left and right. In VALID (i.e. no padding mode), Tensorflow will drop right and/or bottom cells if your filter and stride doesn't full cover input image.

有效填充:这是零填充。希望没有混淆。

x = tf.constant([[1., 2., 3.], [4., 5., 6.],[ 7., 8., 9.], [ 7., 8., 9.]])
x = tf.reshape(x, [1, 4, 3, 1])
valid_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')
print (valid_pad.get_shape()) # output-->(1, 2, 1, 1)

相同填充:首先,这有点难以理解,因为我们必须分别考虑官方文档中提到的两个条件。

假设输入为,输出为,填充为,步幅为,内核大小为(只考虑单个维度)

案例01::

案例02::

被计算为可用于填充的最小值。由于的值是已知的,可以用这个公式求出值。

让我们来做这个例子:

x = tf.constant([[1., 2., 3.], [4., 5., 6.],[ 7., 8., 9.], [ 7., 8., 9.]])
x = tf.reshape(x, [1, 4, 3, 1])
same_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')
print (same_pad.get_shape()) # --> output (1, 2, 2, 1)

这里x的维数是(3,4)那么如果取水平方向(3):

若取垂直方向(4):

希望这将有助于理解实际上相同填充是如何在TF中工作的。

Padding on/off. Determines the effective size of your input. VALID: No padding. Convolution etc. ops are only performed at locations that are "valid", i.e. not too close to the borders of your tensor. With a kernel of 3x3 and image of 10x10, you would be performing convolution on the 8x8 area inside the borders. SAME: Padding is provided. Whenever your operation references a neighborhood (no matter how big), zero values are provided when that neighborhood extends outside the original tensor to allow that operation to work also on border values. With a kernel of 3x3 and image of 10x10, you would be performing convolution on the full 10x10 area.

这里W和H是输入的宽和高, F为滤波器维数, P是填充大小(即要填充的行数或列数)

对于相同的填充:

对于有效填充:

总之,“有效”填充意味着没有填充。卷积层的输出大小根据输入大小和内核大小而缩小。

相反,“相同”填充意味着使用填充。当stride设置为1时,卷积层的输出大小保持为输入大小,在计算卷积时在输入数据周围附加一定数量的“0-border”。

希望这个直观的描述能有所帮助。

Tensorflow 2.0兼容答案:上面已经提供了关于“有效”和“相同”填充的详细解释。

但是,我将在Tensorflow 2中指定不同的池化函数和它们各自的命令。X(>= 2.0),为社区的利益。

1.x中的函数:

tf.nn.max_pool

tf.keras.layers.MaxPool2D

tf中平均池值=>无。神经网络,tf.keras.layers.AveragePooling2D

2.x中的函数:

tf.nn。Max_pool如果在2中使用。如果从1迁移,则tf. compat_v1 .nn.max_pool_v2或tf. compat_v2 .nn.max_pool。X到2。X。

tf.keras.layers。MaxPool2D如果在2中使用。x和

tf. compat_v1 .keras.layers. maxpooling2d或tf. compat_v1 .keras.layers. maxpooling2d或tf. compat_v2 .keras.layers. maxpooling2d或tf. compat_v2 .keras.layers. maxpooling2d,如果从1迁移。X到2。X。

平均池=> tf.nn。Avg_pool2d或tf.keras.layers。如果在TF 2中使用AveragePooling2D。x和

tf. compat_v1 . dn .avg_pool_v2或tf. compat_v2 .v2. dn .avg_pool或tf. compat_v1 .keras.layers. averagepooling2d或tf. compat_v1 .keras.layers. avgpool2d或tf. compat_v2 .keras.layers. averagepooling2d或tf. compat_v2 .keras.layers. avgpool2d,如果从1迁移。X到2。X。

有关Tensorflow迁移的更多信息。X到2。请参考本迁移指南。

为了补充YvesgereY的回答,我发现这个可视化非常有用:

填充'valid'是第一个数字。滤镜窗口停留在图像内部。

填充'same'是第三个数字。输出是相同的大小。


在这篇文章里找到的

可视化致谢:vdumoulin@GitHub

有效填充是没有填充。 相同的填充在某种程度上是输出与输入大小相同的填充。