tf.nn.conv2d_transpose给出InvalidArgumentError:Conv2DCustomBackpropInput:输入和过滤器的深度必须相同

问题描述 投票:2回答:1

我在使tf.nn.conv2d_transpose正常工作时遇到了问题。这是我要做的事情的小复制:

import tensorflow as tf
import numpy as np

# Shape (2, 3, 3, 1) == (batch_sz, height, width, channels)
inp = tf.Variable(np.array(
    [
        [
            [[1], [2], [3]],
            [[2], [3], [4]],
            [[7], [8], [9]]
        ],
        [
            [[3], [2], [1]],
            [[2], [7], [2]],
            [[3], [2], [0]]
        ]
    ], dtype = np.float32
))
# Shape (5, 5, 3, 1) == (kH, kW, out_channels, in_channels)
ker = tf.Variable(np.array(
    [
        [[[1],[2],[1]], [[2],[2],[2]], [[1],[2],[1]], [[2],[1],[1]], [[1],[1],[1]]],
        [[[1],[2],[1]], [[2],[2],[2]], [[1],[2],[1]], [[2],[1],[1]], [[1],[1],[1]]],
        [[[1],[2],[1]], [[2],[2],[2]], [[1],[2],[1]], [[2],[1],[1]], [[1],[1],[1]]],
        [[[1],[2],[1]], [[2],[2],[2]], [[1],[2],[1]], [[2],[1],[1]], [[1],[1],[1]]],
        [[[1],[2],[1]], [[2],[2],[2]], [[1],[2],[1]], [[2],[1],[1]], [[1],[1],[1]]]
    ], dtype = np.float32
))
out = tf.nn.conv2d_transpose(inp, ker, (2, 7, 7, 1), (1, 1, 1, 1), padding='SAME', data_format='NHWC', name='conv_transpose')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    output, kernel, input = sess.run([out, ker, inp])

我想要的是使用三个5x5x1滤波器对3x3x1输入执行转置卷积。我期望输出的形状为7x7x3-但是,我收到一条错误消息:

InvalidArgumentError: Conv2DCustomBackpropInput: input and filter must have the same depth
     [[Node: conv_transpose_2 = Conv2DBackpropInput[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](conv_transpose_2/output_shape, Variable_21/read, Variable_20/read)]]

输入和过滤器深度都不等于1吗?我看不到我在做什么错-任何提示将不胜感激。我特别想使用tf.nn.conv2d_transpose而不是tf.layers.conv2d_transpose

python tensorflow conv-neural-network convolution deconvolution
1个回答
0
投票

此问题与此Stack Overflow Issue相似。

您应进行以下更改以使代码运行。

  1. inp的形状应为(2,3,3,3)而不是(2,3,3,1)
  2. ker的形状应为(5,5,1,3)而不是(5,5,3,1)
  3. 仅应将填充设置为“有效”而不是“相同”,然后Output Shape将不同于Input Shape

下面提到的是工作代码(为了便于实现,使用零进行了编码:

import tensorflow as tf
import numpy as np

# Shape (2, 3, 3, 3) == (batch_sz, height, width, channels)
inp = tf.Variable(np.array(np.zeros((2, 3, 3, 3)) , dtype = np.float32))

# Shape (5, 5, 3, 1) == (kH, kW, out_channels, in_channels)
ker = tf.Variable(np.zeros((5,5,1,3)) , dtype = np.float32)

out = tf.nn.conv2d_transpose(value = inp, filter = ker, output_shape=(2, 7, 7, 1), 
                             strides=(1, 1, 1, 1), padding='VALID', data_format='NHWC', name='conv_transpose')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    output, kernel, input = sess.run([out, ker, inp])
© www.soinside.com 2019 - 2024. All rights reserved.