Save and Load GAN model for continued training using Keras
Asked Answered
O

0

8

I am trying to save a GAN model so that I can continue the training later.

Basically I am saving the discriminator and generator separately after the training loop, with these commands:

discriminator.save("discriminatorTrained.h5")
generator.save("generatorTrained.h5")

Then when I want to continue training I am loading them like this:

# Load Discriminator and Generator
discriminator = load_model('discriminatorTrained.h5')
generator = load_model('generatorTrained.h5')
discriminator.trainable = False

And then I am making a new GAN with the loaded discriminator and generator like this:

#Make new GAN from trained discriminator and generator
gan_input = Input(shape=(noise_dim,))
fake_image = generator(gan_input)
gan_output = discriminator(fake_image)
gan = Model(gan_input, gan_output)
gan.compile(loss='binary_crossentropy', optimizer=optimizer)

And then run the same training script as I did from the start.

I don't get any error message, and it seems to work, however, if comparing the results, for example saving and loading and continue training 10 times, seems to produce less good results from the generator, than if I just run a single training for 10 as many epochs.

So I am suspecting, that I might be missing something here, is some training information lost in this process, perhaps in the recreation of the GAN model?

Overabound answered 22/1, 2020 at 11:6 Comment(3)
Did you try loading the weights instead of the loading and compiling a new model? because it seems as if the model is trained from scratch.Palinode
You mean, not saving and loading the compiled generator and discriminator, but just the weights, and then creating new generator and discriminator with these weights and compile them? However, I don't think the model is trained from scratch, I can see that multiple training rounds (saving and loading) improves the generator, but just not as much as a single run with same number of total epochs.Overabound
Do we need to save both discriminator and generator or only a generator is enough? Why do you need to save the discriminator?Risley

© 2022 - 2024 — McMap. All rights reserved.