10

我读了 tensorflow 的 tf.nn.max_pool 中的“SAME”和“VALID”填充有什么区别?但这不适用于我的实验。

import tensorflow as tf

inputs = tf.random_normal([1, 64, 64, 3])
print(inputs.shape)
conv = tf.keras.layers.Conv2D(6, 4, strides=2, padding='same')
outputs = conv(inputs)
print(outputs.shape)

生产

(1, 64, 64, 3)
(1, 32, 32, 6)

. 但是,按照上面的链接会产生(1, 31, 31, 6),因为在没有任何填充的过滤器范围之外没有额外的值。

padding='same' 和 strides > 1 的 tf.keras.layers.Conv2D 表现如何?
我想知道确切的答案及其证据。

4

2 回答 2

9

Keras 使用 TensorFlow 实现填充。所有详细信息都可在此处的文档中找到

首先,考虑“SAME”填充方案。这些注释中给出了其背后原因的详细解释。在这里,我们总结了这种填充方案的机制。使用“SAME”时,输出高度和宽度计算为:

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

沿高度和宽度应用的总填充计算如下:

if (in_height % strides[1] == 0):
  pad_along_height = max(filter_height - strides[1], 0)
else:
  pad_along_height = max(filter_height - (in_height % strides[1]), 0)
if (in_width % strides[2] == 0):
  pad_along_width = max(filter_width - strides[2], 0)
else:
  pad_along_width = max(filter_width - (in_width % strides[2]), 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

请注意,除以 2 意味着可能存在两侧(顶部与底部、右侧与左侧)的填充都减 1 的情况。在这种情况下,底部和右侧总是得到一个额外的填充像素。例如,当 pad_along_height 为 5 时,我们在顶部填充 2 个像素,在底部填充 3 个像素。请注意,这与 cuDNN 和 Caffe 等现有库不同,后者明确指定填充像素的数量并始终在两侧填充相同数量的像素。

对于“有效”方案,输出高度和宽度计算为:

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

并且不使用填充。

于 2018-12-17T18:01:54.323 回答
2

在 tensorflow 中,对于 strides和 input size n,使用相同的填充给出:

⌈n/s⌉

或输入大小的上限除以步幅。

于 2018-12-17T18:06:54.653 回答