Tensorflow conv2d_transpose:out_backprop的大小与计算出的不匹配

时间:2018-07-03 03:20:22

标签: python tensorflow conv-neural-network deconvolution

在构建用于分割的FCN时,我希望图像保持输入数据的原始大小,因此我使用完全卷积层。当我选择固定的输入大小,例如(224,224)时,转置转换效果很好。但是,当我将使用(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') 

3 个答案:

答案 0 :(得分:0)

这是因为您的步幅>1。计算不能始终正确。这篇GitHub帖子对此进行了解释。

答案 1 :(得分:0)

FCN中的conv Net和Deconv Net由不同的结构构建,可能彼此不一致。在这种情况下,conv网络将mysql.infoschema与conv一起使用,而deconv网络将所有#1449 - The user specified as a definer ('mysql.infoschema'@'localhost') does not exist与conv_transpose一起使用。因此形状不一样,从而导致上述问题。

答案 2 :(得分:0)

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