【问题标题】:Getting the output shape of deconvolution layer using tf.nn.conv2d_transpose in tensorflow在张量流中使用 tf.nn.conv2d_transpose 获取反卷积层的输出形状
【发布时间】:2016-06-29 01:10:32
【问题描述】:

根据这个paper,输出形状是N + H - 1N是输入高度或宽度,H是内核高度或宽度。这显然是卷积的逆过程。这个tutorial 给出了一个计算卷积输出形状的公式,它是(W−F+2P)/S+1W - 输入大小,F - 滤波器大小,P - 填充大小,S - 步幅。但是在Tensorflow,有这样的测试用例:

  strides = [1, 2, 2, 1]

  # Input, output: [batch, height, width, depth]
  x_shape = [2, 6, 4, 3]
  y_shape = [2, 12, 8, 2]

  # Filter: [kernel_height, kernel_width, output_depth, input_depth]
  f_shape = [3, 3, 2, 3]

所以我们使用y_shapef_shapex_shape,根据公式(W−F+2P)/S+1计算填充大小P。从(12 - 3 + 2P) / 2 + 1 = 6,我们得到P = 0.5,它不是一个整数。反卷积在 Tensorflow 中是如何工作的?

【问题讨论】:

    标签: neural-network tensorflow deep-learning conv-neural-network


    【解决方案1】:

    对于反卷积,

    output_size = strides * (input_size-1) + kernel_size - 2*padding
    

    strides、input_size、kernel_size、padding 都是整数 '有效'的填充为零

    【讨论】:

    • 我认为“SAME”的填充是零,但“VALID”是一些值。
    • @VatsalAggarwal 请验证您的评论。 'SAME' 的大小相同。所以添加了填充来维护它
    【解决方案2】:

    本教程中输出大小的公式假定填充 P 在图像前后相同(左右或上下)。 然后,放置内核的位置数为: W (size of the image) - F (size of the kernel) + P (additional padding before) + P (additional padding after).

    但是 tensorflow 还可以处理需要在一侧填充比另一侧更多像素的情况,以便内核正确拟合。您可以在docs 中阅读有关选择填充("SAME""VALID")的策略的更多信息。您所说的测试使用方法"VALID"

    【讨论】:

      【解决方案3】:

      这个讨论真的很有帮助。只需添加一些附加信息。 padding='SAME' 也可以让底部和右侧得到一个额外的填充像素。根据TensorFlow document,以及下面的测试用例

      strides = [1, 2, 2, 1]
      # Input, output: [batch, height, width, depth]
      x_shape = [2, 6, 4, 3]
      y_shape = [2, 12, 8, 2]
      
      # Filter: [kernel_height, kernel_width, output_depth, input_depth]
      f_shape = [3, 3, 2, 3]
      

      正在使用 padding='SAME'。我们可以将 padding='SAME' 解释为:

      (W−F+pad_along_height)/S+1 = out_height,
      (W−F+pad_along_width)/S+1 = out_width.
      

      所以(12 - 3 + pad_along_height) / 2 + 1 = 6,我们得到pad_along_height=1。和pad_top=pad_along_height/2 = 1/2 = 0(整数除法),pad_bottom=pad_along_height-pad_top=1

      至于padding='VALID',顾名思义,我们在适当的时候使用padding。首先,我们假设填充像素 = 0,如果这不起作用,那么我们在原始输入图像区域之外的任何值处添加 0 填充。比如下面的测试用例,

      strides = [1, 2, 2, 1]
      
      # Input, output: [batch, height, width, depth]
      x_shape = [2, 6, 4, 3]
      y_shape = [2, 13, 9, 2]
      
      # Filter: [kernel_height, kernel_width, output_depth, input_depth]
      f_shape = [3, 3, 2, 3]
      

      conv2d的输出形状为

      out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))
                 = ceil(float(13 - 3 + 1) / float(3)) = ceil(11/3) = 6
                 = (W−F)/S + 1.
      

      因为(W−F)/S+1 = (13-3)/2+1 = 6,结果是一个整数,我们不需要在图片边框周围加0像素,而TensorFlow documentpadding='VALID'部分的pad_top=1/2pad_left=1/2是都是 0。

      【讨论】:

      • 答案是关于tf.nn.conv2dtf.nn.conv2d_transpose 的填充模式如何工作? tf.nn.conv2d_transpose 将使输出张量大于输入。
      猜你喜欢
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多