TensorFlow中卷积的自定义填充 [英] Custom padding for convolutions in TensorFlow
问题描述
在tensorflow函数中 tf.nn.conv2d ,填充选项只有 SAME和 VALID。
In tensorflow function tf.nn.conv2d, the padding option just has 'SAME' and 'VALID'.
但是在Caffe的转换层中,有 pad选项可以定义(隐式)添加到输入的每一侧的像素数。
But in the conv layer of Caffe, there is pad option can define the number of pixels to (implicitly) add to each side of the input.
如何在Tensorflow中实现这一点?
How to achieve that in Tensorflow?
非常感谢。
推荐答案
您可以使用 tf.pad()
(请参见 doc )在应用 tf.nn.conv2d(...,padding = VALID)
之前填充张量(有效填充表示无填充)。
You can use tf.pad()
(see the doc) to pad the Tensor before applying tf.nn.conv2d(..., padding="VALID")
(valid padding means no padding).
例如,如果要填充高2像素,宽1像素的图像,然后使用5x5内核:
For instance, if you want to pad the image with 2 pixels in height, and 1 pixel in width, and then apply a convolution with a 5x5 kernel:
input = tf.placeholder(tf.float32, [None, 28, 28, 3])
padded_input = tf.pad(input, [[0, 0], [2, 2], [1, 1], [0, 0]], "CONSTANT")
filter = tf.placeholder(tf.float32, [5, 5, 3, 16])
output = tf.nn.conv2d(padded_input, filter, strides=[1, 1, 1, 1], padding="VALID")
输出
将具有形状 [无,28、26、16]
,因为您的填充宽度只有1。
output
will have shape [None, 28, 26, 16]
, because you have only a padding of 1 in width.
这篇关于TensorFlow中卷积的自定义填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!