通过自定义图层启用背投

时间:2019-12-28 12:35:46

标签: tensorflow

在以下位置运行笔记本时:

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/generative/pix2pix)中使用的UNet的“解码器”分支。

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

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

干杯

西蒙(Simon)

1 个答案:

答案 0 :(得分:0)

所以,我解决了。我需要将自定义图层移出模型,让模型以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