首页 > 解决方案 > 通过自定义层启用反向投影

问题描述

运行笔记本时,可在以下位置获得:

https://colab.research.google.com/drive/1rMYs5bQzr1ubCnsWTgJNecYwEQamXwsn

我收到以下错误:

    ValueError: No gradients provided for any variable: ['sequential/conv2d/kernel:0', 'sequential_1/conv2d_1/kernel:0', 'sequential_1/batch_normalization/gamma:0', 'sequential_1/batch_normalization/beta:0', 'sequential_2/conv2d_2/kernel:0', 'sequential_2/batch_normalization_1/gamma:0', 'sequential_2/batch_normalization_1/beta:0', 'sequential_3/conv2d_3/kernel:0', 'sequential_3/batch_normalization_2/gamma:0', 'sequential_3/batch_normalization_2/beta:0', 'sequential_4/conv2d_4/kernel:0', 'sequential_4/batch_normalization_3/gamma:0', 'sequential_4/batch_normalization_3/beta:0', 'sequential_5/conv2d_5/kernel:0', 'sequential_5/batch_normalization_4/gamma:0', 'sequential_5/batch_normalization_4/beta:0', 'sequential_6/conv2d_6/kernel:0', 'sequential_6/batch_normalization_5/gamma:0', 'sequential_6/batch_normalization_5/beta:0', 'Variable:0', 'Variable:0'].

我怀疑这是因为我的最后一层是高度定制的,这导致 TensorFlow 在反向投影时遇到困难。不过,我对此很陌生,所以我可能会弄错。

我创建了一个最终层,它将圆心、矩形中心和矩形旋转转换为图像蒙版,以便在 GAN 中使用。目的是替换 Pix2Pix 示例 ( https://www.tensorflow.org/tutorials/generation/pix2pix ) 中使用的 UNet 的“解码器”臂。

我把我的自定义层全部连接起来了,但不幸的是,优化器似乎不喜欢它。我怀疑这是因为它不知道如何通过我的图层支持项目。有没有办法在 TensorFlow 中定义自定义反投影?如果这是可能的,那么我可以告诉神经网络如何向后遍历它,并希望它会起作用。

任何提示/反馈/建议将不胜感激。

干杯,

西蒙

标签: tensorflow

解决方案


所以,我解决了。我需要将我的自定义层移出模型,让模型以 5 个参数(场中心 x、场中心 y、场旋转、bb_centre x 和 bb_centre y)结束。然后,当我想将图像传递给train_step函数内的鉴别器时,我会解码到掩码中:

@tf.function
def train_step(input_image, target_mask, target_encoding, epoch):
    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
        gen_output = generator(input_image, training=True)

        gen_output_image = decode_batch(gen_output)

        disc_real_output = discriminator([input_image, target_mask], training=True)
        disc_generated_output = discriminator([input_image, gen_output_image], training=True)

        gen_total_loss, gen_gan_loss, gen_l1_loss = generator_loss(disc_generated_output, gen_output, target_encoding)
        disc_loss = discriminator_loss(disc_real_output, disc_generated_output)

    generator_gradients = gen_tape.gradient(gen_total_loss,
                                          generator.trainable_variables)
    discriminator_gradients = disc_tape.gradient(disc_loss,
                                               discriminator.trainable_variables)

    generator_optimizer.apply_gradients(zip(generator_gradients,
                                          generator.trainable_variables))
    discriminator_optimizer.apply_gradients(zip(discriminator_gradients,
                                              discriminator.trainable_variables))

    with summary_writer.as_default():
        tf.summary.scalar('gen_total_loss', gen_total_loss, step=epoch)
        tf.summary.scalar('gen_gan_loss', gen_gan_loss, step=epoch)
        tf.summary.scalar('gen_l1_loss', gen_l1_loss, step=epoch)
        tf.summary.scalar('disc_loss', disc_loss, step=epoch)

可以在以下位置找到不再引发错误(但仍需要工作)的笔记本:

https://colab.research.google.com/drive/1ozCjHHL5weA9daESMPgPhmbNx1rZWngc


推荐阅读