Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a model from checkpoint fails?

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.

like image 530
Debajyoti Sengupta Avatar asked Sep 01 '25 01:09

Debajyoti Sengupta


1 Answers

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)
like image 64
Adam Gerber Avatar answered Sep 04 '25 18:09

Adam Gerber