gpt4 book ai didi

python - 填充为 ='same' 且步幅 > 1 的 tf.keras.layers.Conv2D 表现如何?

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:25 25 4
gpt4 key购买 nike

我读了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),因为没有任何填充的过滤器范围之外没有额外的值。

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

最佳答案

Keras 使用 TensorFlow 实现填充。文档中提供了所有详细信息 here

First, consider the 'SAME' padding scheme. A detailed explanation ofthe reasoning behind it is given in these notes. Here, we summarizethe mechanics of this padding scheme. When using 'SAME', the outputheight and width are computed as:

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

The total padding applied along the height and width is computed as:

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)

Finally, the padding on the top, bottom, left and right are:

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

Note that the division by 2 means that there might be cases when thepadding on both sides (top vs bottom, right vs left) are off by one.In this case, the bottom and right sides always get the one additionalpadded pixel. For example, when pad_along_height is 5, we pad 2 pixelsat the top and 3 pixels at the bottom. Note that this is differentfrom existing libraries such as cuDNN and Caffe, which explicitlyspecify the number of padded pixels and always pad the same number ofpixels on both sides.

For the 'VALID' scheme, the output height and width are computed as:

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

and no padding is used.

关于python - 填充为 ='same' 且步幅 > 1 的 tf.keras.layers.Conv2D 表现如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53819528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com