【问题标题】:Loading a model from checkpoint fails?从检查点加载模型失败?
【发布时间】:2019-11-18 18:25:13
【问题描述】:

https://www.tensorflow.org/beta/tutorials/generative/dcgan 我正在关注 DCGAN 的本教程,并尝试从我保存的检查点恢复模型。但是当我尝试加载模型时,它给了我一个错误:

AssertionError: Nothing except the root object matched a checkpointed value. Typically this means that the checkpoint does not match the Python program. The following objects have no matching checkpointed value: [<tensorflow.python.keras.layers.advanced_activations.LeakyReLU object at 0x7f05b545fc88>, <tf.Variable 'conv2d_transpose_5/kernel:0' shape=(3, 3, 1, 64) dtype=float32, numpy=.......

我试图通过只保留生成器部分来修改检查点,因为这就是我需要的全部内容。在那之后,我做

latest = tf.train.latest_checkpoint(checkpoint_dir)
gen_mod = make_generator_model() #This is already defined in the code
gen_mod.load_weights(latest)
sample = gen_mod(noise,training=False)

这给了我错误。有没有办法只加载生成器部分? 我想要的是能够使用生成器模型从给定的检查点生成图像。

【问题讨论】:

  • 有什么解决办法吗?我无法从使用 c api 生成的检查点将权重加载到 keras 中
  • 首先我很抱歉,因为我应该用我找到的“解决方案”更新帖子。我的问题是我使用的是子类模型,它们是一个相当棘手的集合。在此处 (tensorflow.org/beta/guide/keras/saving_and_serializing) 阅读有关保存和重新加载模型的更多信息。也许你会发现一些东西。再次抱歉帮不上忙。
  • 是的,我对这个 stackoverflow.com/questions/57993832/… 感到很疯狂,我可以完全使用 C api,但是一旦我尝试在 python 中使用这些权重,我就可以让它们成功加载,不管加载方式

标签: tensorflow2.0 generative-adversarial-network


【解决方案1】:

正如您所说,由于 DCGAN 教程创建了两个模型(一个生成器和一个判别器)的快照,load_weights 必须选择仅与您的生成器相关的权重,并且它缺乏执行此操作的上下文。

您可以restore 检查点,然后从那里访问生成器(或鉴别器),而不是尝试将权重直接加载到模型中:

# from the DCGAN tutorial
checkpoint = tf.train.Checkpoint(
    generator_optimizer=generator_optimizer,
    discriminator_optimizer=discriminator_optimizer,
    generator=generator,
    discriminator=discriminator,
)

latest = tf.train.latest_checkpoint(checkpoint_dir)
checkpoint.restore(latest)

# classify an image
checkpoint.discriminator(training_images[0:2])

# generate an image
checkpoint.generator(noise)

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 2018-10-10
    • 2020-11-24
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 2021-06-25
    相关资源
    最近更新 更多