【问题标题】:Tensorflow conv2d_transpose: Size of out_backprop doesn't match computedTensorflow conv2d_transpose:out_backprop 的大小与计算的不匹配
【发布时间】:2018-07-03 03:20:21
【问题描述】:

当我构建 FCN 进行分割时,我希望图像保持输入数据的原始大小,因此我使用全卷积层。当我选择固定输入大小时,例如 (224, 224),转置 conv 工作正常。但是,当我将使用 (224, 224) 的代码更改为 (h, w) 时,遇到以下错误。我之前google了,但我没有弄明白。谁能帮我?谢谢。

错误信息:

InvalidArgumentError (see above for traceback): Conv2DSlowBackpropInput: Size 
of out_backprop doesn't match computed: actual = 62, computed = 
63spatial_dim: 2 input: 500 filter: 16 output: 62 stride: 8 dilation: 1
     [[Node: deconv_layer/conv2d_transpose_2 = 
Conv2DBackpropInput[T=DT_FLOAT, data_format="NCHW", dilations=[1, 1, 1, 1], 
padding="SAME", strides=[1, 1, 8, 8], use_cudnn_on_gpu=true, 
_device="/job:localhost/replica:0/task:0/device:GPU:0"] 
(deconv_layer/conv2d_transpose_2-0-VecPermuteNHWCToNCHW- 
LayoutOptimizer/_1961, deconv_layer/deconv3/kernel/read, 
deconv_layer/Add_1)]]
     [[Node: losses/_2091 = _Recv[client_terminated=false, 
recv_device="/job:localhost/replica:0/task:0/device:CPU:0", 
send_device="/job:localhost/replica:0/task:0/device:GPU:0", 
send_device_incarnation=1, tensor_name="edge_4480_losses", 
tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"] 
()]]

代码:

with tf.variable_scope("deconv_layer"):
    deconv_shape1 = block2.get_shape()
    W_t1 = deconv_utils.weight_variable([4, 4, deconv_shape1[3].value, 2048], 
                                        name="deconv1/kernel")
    b_t1 = deconv_utils.bias_variable([deconv_shape1[3].value], 
                                      name="deconv1/biases")
    deconv_t1 = deconv_utils.conv2d_transpose_strided(block4, W_t1, b_t1, 
                                       output_shape=tf.shape(block2))
    fuse1 = tf.add(deconv_t1, block2)
    print("deconv_t1: ", deconv_t1.shape)
    print("fuse_1: ", fuse1.shape)
    tf.identity(fuse1, name="fuse1")

    deconv_shape2 = block1.get_shape()
    W_t2 = deconv_utils.weight_variable([4, 4, deconv_shape2[3].value, 
                        deconv_shape1[3].value], name="deconv2/kernel")
    b_t2 = deconv_utils.bias_variable([deconv_shape2[3].value], 
                                      name="deconv2/biases")
    deconv_t2 = deconv_utils.conv2d_transpose_strided(fuse1, W_t2, b_t2, 
                        output_shape=tf.shape(block1))
    fuse2 = tf.add(deconv_t2, block1)
    print("deconv_t2: ", deconv_t2.shape)
    print("fuse2: ", fuse2.shape)
    tf.identity(fuse2, name="fuse2")

    shape = tf.shape(features)
    deconv_shape3 = tf.stack([shape[0], shape[1], shape[2], num_classes])
    W_t3 = deconv_utils.weight_variable([16, 16, num_classes, 
                       deconv_shape2[3].value], name="deconv3/kernel")
    b_t3 = deconv_utils.bias_variable([num_classes], name="deconv3/biases")
    deconv_t3 = deconv_utils.conv2d_transpose_strided(fuse2, W_t3, b_t3, 
                       output_shape=deconv_shape3, stride=8)
    print("deconv_t3: ", deconv_t3.shape)

没有自定义函数的版本在这里:

    with tf.variable_scope("deconv_layer"):
    deconv1_shape = block2.get_shape()
    shape1 = [4, 4, deconv1_shape[3].value, 2048]
    deconv1_kernel = tf.Variable(initial_value=tf.truncated_normal(shape1, 
                                 stddev=0.02),
                                 trainable=True,
                                 name="deconv1/kernel")
    deconv1 = tf.nn.conv2d_transpose(value=block4,
                                     filter=deconv1_kernel,
                                     # output_shape=[BATCH_SIZE, 
                             tf.shape(block2)[1], tf.shape(block2)[2], 512],
                                     output_shape=tf.shape(block2),
                                     strides=[1, 2, 2, 1],
                                     padding='SAME',
                                     data_format='NHWC'
                                     )
    print('deconv1', deconv1.shape)
    fuse1 = tf.add(deconv1, block2)  # fuse1 = pool4 + deconv2(pool5)
    tf.identity(fuse1, name="fuse1")

    deconv2_shape = block1.get_shape()
    shape2 = [4, 4, deconv2_shape[3].value, deconv1_shape[3].value]
    deconv2_kernel = tf.Variable(initial_value=tf.truncated_normal(shape2, 
                                 stddev=0.02),
                                 trainable=True,
                                 name="deconv2/kernel")
    deconv2 = tf.nn.conv2d_transpose(value=fuse1,
                                     filter=deconv2_kernel,
                                     output_shape=tf.shape(block1),
                                     strides=[1, 2, 2, 1],
                                     padding='SAME',
                                     data_format='NHWC'
                                     )
    print('deconv2', deconv2.shape)
    fuse2 = tf.add(deconv2, block1)
    tf.identity(fuse2, name="fuse2")

    deconv3_shape = tf.stack([tf.shape(features)[0], tf.shape(features)[1], 
                              tf.shape(features)[2], num_classes])
    shape3 = [16, 16, num_classes, deconv2_shape[3].value]
    deconv_final_kernel = tf.Variable(initial_value=tf.truncated_normal(shape3, stddev=0.02),
                                      trainable=True,
                                      name="deconv3/kernel")

    seg_logits = tf.nn.conv2d_transpose(value=fuse2,
                                        filter=deconv_final_kernel,
                                        output_shape=deconv3_shape,
                                        strides=[1, 8, 8, 1],
                                        padding='SAME',
                                        data_format='NHWC') 

【问题讨论】:

  • 我将在此处添加我的评论而不是答案。确保您使用与训练数据生成器相同的参数来生成测试数据。就我而言,我必须确保输入大小和种子都匹配。

标签: python tensorflow conv-neural-network deconvolution


【解决方案1】:

FCN 中的 conv Net 和 Deconv Net 由不同的结构构建而成,可能彼此不一致。在这种情况下,conv 网络使用带有padding='VALID' 的 conv,而 deconv 网络使用带有padding='SAME 的所有 conv_transpose。因此形状不一样,导致上述问题。

【讨论】:

    【解决方案2】:

    这是因为你的步幅> 1.计算不可能总是正确的。这个GitHub 帖子解释了它。

    【讨论】:

    • 根据我目前的知识,在 FCN 进行分割时,上采样是通过 conv2d_transpose 和“stride > 1”完成的。所以我认为你提到的可能不是我遇到问题的关键。
    • 您发布的错误消息的步幅为stride: 8。所以你使用 stride > 1.
    • 我确实使用了stride > 1。我的观点是,在构建 FCN 进行分段时,需要使用conv2d_transposestride > 1 进行上采样。 This code 那样使用它。所以我相信我遇到的错误不是stride > 1引起的。
    • 您不必使用 stride > 1 进行上采样。但是当你有更大的步幅时,你会更快地采样。也许他们使用特殊情况,您可以在其中计算输出形状,但众所周知,您不能
    【解决方案3】:

    我在尝试在 tensorflow 中复制 pytorch 的 transposeconv2d 函数时遇到了类似的问题。在传递给 conv2d_transpose() 函数并在反卷积输出上再次进行填充之前,我试图对输入进行填充。这就是为什么图形被正确初始化但在计算梯度时出现错误的原因。我通过删除所有手动填充并更改函数内部的 padding="SAME" 解决了该错误。我想这是在函数内部处理的。如果我错了,请纠正我。我不知道这对实际输出有多大影响。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多