【问题标题】:Tensorflow conv2d_transpose output_shapeTensorFlow conv2d_transpose output_shape
【发布时间】:2017-06-21 10:51:08
【问题描述】:

我想实现一个输入大小不固定的生成对抗网络 (GAN),例如 4-D Tensor (Batch_size, None, None, 3)

但是我在使用conv2d_transpose的时候,有一个参数output_shape,这个参数必须通过反卷积运算后的true size

例如,如果the size of batch_img is (64, 32, 32, 128), w is weight with (3, 3, 64, 128),则在

deconv = tf.nn.conv2d_transpose(batch_img, w, output_shape=[64, 64, 64, 64],stride=[1,2,2,1], padding='SAME')

所以,我得到deconvsize (64, 64, 64, 64),如果我通过true size of output_shape 就可以了。

但是,我想使用不固定的输入大小(64, None, None, 128),并用(64, None, None, 64) 得到deconv

但是,它会引发如下错误。

TypeError: Failed to convert object of type <type'list'> to Tensor...

那么,我该怎么做才能避免 deconv 中的这个参数呢?或者还有其他方法可以实现 unfixed GAN?

【问题讨论】:

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


    【解决方案1】:
    • 输出形状列表不接受在列表中包含 None,因为 None 对象无法转换为张量对象
    • 只允许在tf.placeholder的形状中
    • 对于不同大小的 output_shape 而不是 None 试试 -1 例如你想要 size(64, None, None, 128) 所以试试 [64, -1, -1, 128]... 我不确定这是否会工作......它对我有用,因为我的第一个参数不是固定大小的 batch_size 所以我使用 -1
    • 怎么还有一个用于转置卷积的高级apitf.layers.conv2d_transpose()
    • 我相信高级 api tf.layers.conv2d_transpose() 会为您工作,因为它需要不同输入的张量
    • 您甚至不需要指定output-shape,只需指定要使用的output_channelkernel
    • 更多详情:https://www.tensorflow.org/api_docs/python/tf/layers/conv2d_transpose...希望对您有所帮助

    【讨论】:

      【解决方案2】:

      我也遇到了这个问题。正如此处其他答案中所建议的那样,使用 -1 不起作用。相反,您必须获取传入张量的形状并构造 output_size 参数。这是我写的一个测试的摘录。在这种情况下,它是未知的第一个维度,但它应该适用于已知和未知参数的任何组合。

      output_shape = [8, 8, 4] # width, height, channels-out. Handle batch size later
      xin = tf.placeholder(dtype=tf.float32, shape = (None, 4, 4, 2), name='input')
      filt = tf.placeholder(dtype=tf.float32, shape = filter_shape, name='filter')
      
      ## Find the batch size of the input tensor and add it to the front
      ## of output_shape
      dimxin = tf.shape(xin)
      ncase = dimxin[0:1]
      oshp = tf.concat([ncase,output_shape], axis=0)
      
      z1 = tf.nn.conv2d_transpose(xin, filt, oshp, strides=[1,2,2,1], name='xpose_conv')
      

      【讨论】:

        【解决方案3】:

        我找到了一个解决方案,将 tf.shape 用于未指定的形状,将 get_shape() 用于指定的形状。

        def get_deconv_lens(H, k, d):
            return tf.multiply(H, d) + k - 1
        
        def deconv2d(x, output_shape, k_h=2, k_w=2, d_h=2, d_w=2, stddev=0.02, name='deconv2d'):
            # output_shape: the output_shape of deconv op
            shape = tf.shape(x)
            H, W = shape[1], shape[2]
            N, _, _, C = x.get_shape().as_list()
            H1 = get_deconv_lens(H, k_h, d_h)
            W1 = get_deconv_lens(W, k_w, d_w)
        
            with tf.variable_scope(name):
                w = tf.get_variable('weights', [k_h, k_w, C, x.get_shape()[-1]], initializer=tf.random_normal_initializer(stddev=stddev))
                biases = tf.get_variable('biases', shape=[C], initializer=tf.zeros_initializer())
        
            deconv = tf.nn.conv2d_transpose(x, w, output_shape=[N, H1, W1, C], strides=[1, d_h, d_w, 1], padding='VALID')
            deconv = tf.nn.bias_add(deconv, biases)
        
            return deconv
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-05
          相关资源
          最近更新 更多