【问题标题】:tf.nn.conv2d_transpose gives InvalidArgumentError: Conv2DCustomBackpropInput: input and filter must have the same depthtf.nn.conv2d_transpose 给出 InvalidArgumentError: Conv2DCustomBackpropInput: input and filter must have the same depth
【发布时间】:2018-05-19 15:04:33
【问题描述】:

我遇到了让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

【问题讨论】:

  • 你扩展了数组的第 0 维和第 3 维吗?
  • 抱歉听起来像个菜鸟。不我没有。为什么我需要这样做?我的数组是否还不是 conv2d_transpose 所需的格式 - [batches, in_height, in_width, channels] 用于输入,[filter_height, filter_width, out_channels, in_channels] 用于过滤器 -> 文档:tensorflow.org/api_docs/python/tf/nn/conv2d_transpose

标签: python tensorflow conv-neural-network convolution deconvolution


【解决方案1】:

此问题与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 ShapeInput Shape 不同时,填充应设置为“VALID”而不是“SAME”。

下面提到的是工作代码(为了便于实施,用零做了):

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])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-26
    • 2018-07-28
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多