我读了What is the difference between 'SAME' and 'VALID' padding in tf.nn.max_pool of tensorflow?,但这对我的实验而言并非如此。
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)
,因为在过滤范围之外没有多余的值,没有任何填充。
td.keras.layers.Conv2D的padding ='same'且步幅> 1的行为如何?
我想知道确切的答案及其证据。
答案 0 :(得分:2)
Keras使用TensorFlow实现填充。所有详细信息都可在文档here和here中找到。
首先,考虑“ SAME”填充方案。详细说明 其背后的原因在these notes中给出。在这里,我们总结 这种填充方案的原理。使用“ 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表示在某些情况下, 两侧的填充(顶部与底部,右侧与左侧)均减一。 在这种情况下,底部和右侧始终会获得一个 填充像素。例如,当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]))
并且不使用填充。
答案 1 :(得分:0)
在张量流中,对于步幅s
和输入大小n
,用相同的填充进行填充:
⌈n/s⌉
或输入大小的上限除以步幅。