【问题标题】:Even sized kernels with SAME padding in Tensorflow在 Tensorflow 中具有相同填充的均匀大小的内核
【发布时间】:2018-12-10 10:11:35
【问题描述】:

在 Tensorflow 中,SAME 填充旨在产生与输入相同大小的输出,给定步幅 = 1,通过适当地用零填充输入。对于奇数大小的内核,例如 5x5,它将内核的中心 (2,2) 放在输入 (0,0) 的第一个像素上并开始卷积。在 x 和 y 坐标中,则需要 2 个像素的零填充。

如果使用偶数内核(例如 6x6)会怎样?它不会有一个像素的中心作为它的实际中心。 VALID 填充如何处理这个问题?例如,根据Image convolution with even-sized kernel,一般图像处理文献中的约定是在零之前多放置一个像素,如本例中的-3 -2 -1 0 1 2。填充区域中将命中三个像素。我为此参考了 Tensorflow 文档,但找不到明确的答案。

【问题讨论】:

    标签: python tensorflow conv-neural-network zero-padding


    【解决方案1】:

    就像你说的,文档似乎没有明确说明。看二维卷积核的源码(conv_ops.cc),有评论解释:

    // Total padding on rows and cols is
    // Pr = (R' - 1) * S + (Kr - 1) * Dr + 1 - R
    // Pc = (C' - 1) * S + (Kc - 1) * Dc + 1 - C
    // where (R', C') are output dimensions, (R, C) are input dimensions, S
    // is stride, (Dr, Dc) are dilations, (Kr, Kc) are filter dimensions.
    // We pad Pr/2 on the left and Pr - Pr/2 on the right, Pc/2 on the top
    // and Pc - Pc/2 on the bottom.  When Pr or Pc is odd, this means
    // we pad more on the right and bottom than on the top and left.
    

    因此,您似乎会在右列和底行使用偶数大小的内核获得一个额外的填充。我们可以看一个例子:

    import tensorflow as tf
    
    input_ = tf.ones((1, 10, 10, 1), dtype=tf.float32)
    kernel = tf.ones((6, 6, 1, 1), dtype=tf.float32)
    conv = tf.nn.conv2d(input_, kernel, [1, 1, 1, 1], 'SAME')
    with tf.Session() as sess:
        print(sess.run(conv)[0, :, :, 0])
    

    输出:

    [[16. 20. 24. 24. 24. 24. 24. 20. 16. 12.]
     [20. 25. 30. 30. 30. 30. 30. 25. 20. 15.]
     [24. 30. 36. 36. 36. 36. 36. 30. 24. 18.]
     [24. 30. 36. 36. 36. 36. 36. 30. 24. 18.]
     [24. 30. 36. 36. 36. 36. 36. 30. 24. 18.]
     [24. 30. 36. 36. 36. 36. 36. 30. 24. 18.]
     [24. 30. 36. 36. 36. 36. 36. 30. 24. 18.]
     [20. 25. 30. 30. 30. 30. 30. 25. 20. 15.]
     [16. 20. 24. 24. 24. 24. 24. 20. 16. 12.]
     [12. 15. 18. 18. 18. 18. 18. 15. 12.  9.]]
    

    确实,看起来确实在右侧和底部添加了额外的零。

    【讨论】:

      猜你喜欢
      • 2021-02-05
      • 2017-10-15
      • 2013-06-06
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多