Tensorflow更改输出图像大小与训练输入大小不同

时间:2017-04-23 04:25:09

标签: python tensorflow

我试图从GitHub修改tensorflow项目,以便我可以使用64x64图像进行训练,然后创建更大的输出图像,例如384x384输出图像。当我更改输出图像大小(通过命令行)时,我收到此错误:

尺寸必须相等,但对于Mul'尺寸为384和64。 (op:' Mul')输入形状:[?,384,384,3],[21,64,64,3]。

它与代码中的这一行有关:

self.contextual_loss = tf.reduce_sum(
        tf.contrib.layers.flatten(
            tf.abs(tf.mul(self.mask, self.G) - tf.mul(self.mask, self.images))), 1)

我尝试将tf.mul()更改为tf.matmul(),但仍然遇到类似的错误。我也试过tf.reshape()给了我一个不同的错误。谢谢你的帮助。

def build_model(self):
    self.images = tf.placeholder(
        tf.float32, [None] + self.image_shape, name='real_images')

    self.sample_images= tf.placeholder(
        tf.float32, [None] + self.image_shape, name='sample_images')
    self.z = tf.placeholder(tf.float32, [None, self.z_dim], name='z')
    self.z_sum = tf.histogram_summary("z", self.z)

    self.G = self.generator(self.z)
    self.D, self.D_logits = self.discriminator(self.images)

    self.sampler = self.sampler(self.z)
    self.D_, self.D_logits_ = self.discriminator(self.G, reuse=False)

    self.d_sum = tf.histogram_summary("d", self.D)
    self.d__sum = tf.histogram_summary("d_", self.D_)
    self.G_sum = tf.image_summary("G", self.G)

    self.d_loss_real = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(self.D_logits,
                                                tf.ones_like(self.D)))
    self.d_loss_fake = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(self.D_logits_,
                                                tf.zeros_like(self.D_)))
    self.g_loss = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(self.D_logits_,
                                                tf.ones_like(self.D_)))

    self.d_loss_real_sum = tf.scalar_summary("d_loss_real", self.d_loss_real)
    self.d_loss_fake_sum = tf.scalar_summary("d_loss_fake", self.d_loss_fake)

    self.d_loss = self.d_loss_real + self.d_loss_fake

    self.g_loss_sum = tf.scalar_summary("g_loss", self.g_loss)
    self.d_loss_sum = tf.scalar_summary("d_loss", self.d_loss)

    t_vars = tf.trainable_variables()

    self.d_vars = [var for var in t_vars if 'd_' in var.name]
    self.g_vars = [var for var in t_vars if 'g_' in var.name]

    self.saver = tf.train.Saver(max_to_keep=1)

    # Completion.
    self.mask = tf.placeholder(tf.float32, [None] + self.image_shape, name='mask')
    self.contextual_loss = tf.reduce_sum(
        tf.contrib.layers.flatten(
            tf.abs(tf.mul(self.mask, self.G) - tf.mul(self.mask, self.images))), 1)
    self.perceptual_loss = self.g_loss
    self.complete_loss = self.contextual_loss + self.lam*self.perceptual_loss
    self.grad_complete_loss = tf.gradients(self.complete_loss, self.z)

1 个答案:

答案 0 :(得分:1)

{"post":[{"id":"5","name":"name_of_post"}]} tf.mul()会产生不同的结果。前者是逐次乘法,而后者是矩阵乘法。

在引发错误的行中,您有以下两个表达式:

tf.matmul()

由于tf.mul(self.mask, self.G) tf.mul(self.mask, self.images) self.mask具有完全相同的形状(在占位符中定义为self.images),我猜测问题出在

[None] + self.image_shape

您更改了图片的尺寸,但您还需要更改tf.mul(self.mask, self.G) 的尺寸以匹配图片的尺寸。根据错误,似乎你没有改变它们(因为它们的形状是[21,64,64,3])。

相关问题