4

https://www.tensorflow.org/beta/tutorials/generative/dcgan I am following this tutorial for a DCGAN and am trying to restore a model from the checkpoint I saved. But when I try to load the model, it gives me an error:

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=.......

I tried to modify the checkpoint by only keeping the generator part since that is all I need from here. After that, I do

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)

which gives me the error. Is there a way to just load the generator part? What I want is to be able to generate images with the generator model from a given checkpoint.

  • Any solution to this? I can't load weights into keras from checkpoint generated with c api – jregalad Sep 19 '19 at 12:20
  • Well first I am sorry because I should have updated the post with the 'solution' I found. My issue was that I was using a subclassed models and they are a rather tricky bunch. Read more here (https://www.tensorflow.org/beta/guide/keras/saving_and_serializing) about saving and reloading models. Maybe you will find something. Sorry again to be of little help. – Debajyoti Sengupta Sep 19 '19 at 12:27
  • yeah I am goign crazy over this https://stackoverflow.com/questions/57993832/load-weights-from-checkpoint-not-working-in-keras-model I can los perfectly fine in with the C api but as soon as I try to use those weights in python I just can get them to load successfully regardeless of the loading method – jregalad Sep 19 '19 at 12:31

1 Answers1

3

Since, as you said, the DCGAN tutorial creates a snapshot of two models (a generator and a discriminator), load_weights would have to select the weights relevant only to your generator and it lacks the context to do that.

Instead of trying to load the weights directly into a model, you can restore the checkpoint and, from there, access the generator (or discriminator):

# 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)