I have been looking around and haven’t been able to find the answer to this one. I am having trouble trying to resume training on a CGAN that I am working with after loading the h5 file. When I try to start training the model again after loading the files, the generator loss will begin to move towards zero very quickly, within 3-4 epochs.
Below is some of the code for loading the models and resuming training. Any help or suggestions would be greatly appreciated!
Loading Models:
d_model = load_model('Aeon5/cgan_model/discriminator_0_to_83.h5') g_model = load_model('Aeon5/cgan_model/generator_0_to_83.h5') gen_noise, gen_label = g_model.input gen_output = g_model.output gan_output = d_model([gen_output, gen_label]) combined = Model([gen_noise, gen_label], gan_output) opt = Adam(lr=0.0002, beta_1=0.5) combined.compile(loss=['binary_crossentropy'], optimizer=opt)
Resuming Training:
def resume_train(epochs, start, generator, discriminator, combined_model, latent_dim, data_loader, name_append, batch_size=50): for epoch in range(start, epochs): random = np.random.randint(0, 11) for index in range(int(50000/batch_size)): valid = np.ones((batch_size, 1)) fake = np.zeros((batch_size, 1)) idx = np.random.randint(0, 50000, batch_size) x_train = data_loader.get_img_batch(idx) y_train = data_loader.get_label_batch(idx) x_train = (x_train.astype(np.float32) - 127.5)/127.5 if index % 100 == random: valid = np.zeros((batch_size, 1)) + (np.random.random()*0.1) fake = np.ones((batch_size, 1)) - (np.random.random()*0.1) noise = np.random.randn(batch_size, latent_dim) gen_img = generator.predict([noise, y_train]) d_loss_real, _ = discriminator.train_on_batch([x_train, y_train], valid) d_loss_fake, _ = discriminator.train_on_batch([gen_img, y_train], fake) d_loss = 0.5*(np.add(d_loss_real, d_loss_fake)) sample_label = np.random.randint(0, 10, batch_size).reshape(-1, 1) valid = np.ones((batch_size, 1)) g_loss = combined_model.train_on_batch([noise, sample_label], valid) if index % (batch_size) == 0: sample_images(epoch, latent_dim, generator, data_loader) print("%d [D loss: %f] [G loss: %f]" % (epoch, d_loss, g_loss)) #Save the combined model and the generator name = './cgan_model/combined_' + name_append + '.h5' combined_model.save(name) name = './cgan_model/generator_' + name_append + '.h5' generator.save(name) name = './cgan_model/discriminator_' + name_append + '.h5' discriminator.save(name)