tf.keras.layers.Conv2D如何使用padding ='same'并大步> 1行为? [英] How does tf.keras.layers.Conv2D with padding='same' and strides > 1 behave?

查看:1022
本文介绍了tf.keras.layers.Conv2D如何使用padding ='same'并大步> 1行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了张量流的tf.nn.max_pool中的'SAME'和'VALID'填充有什么区别?但是我的实验不正确。

I read What is the difference between 'SAME' and 'VALID' padding in tf.nn.max_pool of tensorflow? but this is not true to my experiment.

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),因为没有超出范围的过滤器范围内没有多余的值。

. However following the above link produces (1, 31, 31, 6) because there is no extra values outside filter ranges without any padding.

tf.keras.layers.Conv2D的padding ='same'且步幅> 1的行为如何?

我想知道确切的答案及其证据。

How does tf.keras.layers.Conv2D with padding='same' and strides > 1 behave?
I want to know the exact answer and its evidence.

推荐答案

Keras使用TensorFlow实现填充。所有详细信息均可在文档此处此处

Keras uses TensorFlow implementation of padding. All the details are available in the documentation here and here.


首先,考虑 SAME填充方案。 这些说明中给出了
背后原因的详细说明。 。在这里,我们总结了
这种填充方案的机制。使用'SAME'时,输出
的高度和宽度的计算方式如下:

First, consider the 'SAME' padding scheme. A detailed explanation of the reasoning behind it is given in these notes. Here, we summarize the mechanics of this padding scheme. When using 'SAME', 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]))

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

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

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

Note that the division by 2 means that there might be cases when the padding 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 additional padded pixel. For example, when pad_along_height is 5, we pad 2 pixels at the top and 3 pixels at the bottom. Note that this is different from existing libraries such as cuDNN and Caffe, which explicitly specify the number of padded pixels and always pad the same number of pixels 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]))

并且不使用填充。

这篇关于tf.keras.layers.Conv2D如何使用padding ='same'并大步> 1行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆