To train the adversarial network, we need both input values and ground truth values. The input values to the network are batchA and batchB. The ground truth values are real_labels, real_labels, batchA, batchB, batchA, batchB, as follows:
g_loss = adversarial_model.train_on_batch([batchA, batchB],
[real_labels, real_labels, batchA, batchB, batchA, batchB])
This step will train the generator network without training the generating networks.
After the completion of a single iteration (loop) over each mini-batch, store the losses in lists called dis_losses and gen_losses, as follows:
dis_losses.append(d_loss)
gen_losses.append(g_loss)
After every 10 epochs, use the generator networks to generate a set of images:
# Sample and save images after every 10 epochs
if epoch % 10 == 0:
# Get a batch of test data
batchA, batchB = load_test_batch(data_dir=data_dir, batch_size=2)
# Generate images
generatedB = generatorAToB.predict(batchA)
generatedA = generatorBToA.predict(batchB)
# Get reconstructed images
reconsA = generatorBToA.predict(generatedB)
reconsB = generatorAToB.predict(generatedA)
# Save original, generated and reconstructed images
for i in range(len(generatedA)):
save_images(originalA=batchA[i], generatedB=generatedB[i], recosntructedA=reconsA[i],
originalB=batchB[i], generatedA=generatedA[i], reconstructedB=reconsB[i],
path="results/gen_{}_{}".format(epoch, i))
Put the preceding code block inside the epochs loop. After every 10 epochs, it will generate a batch of fake images and save them to the results directory.
Next, store the average losses to TensorBoard for visualization. Store both losses: the average loss for the generator network, and the average loss for the discriminator network, as shown in the following example:
write_log(tensorboard, 'discriminator_loss', np.mean(dis_losses),
epoch)
write_log(tensorboard, 'generator_loss', np.mean(gen_losses), epoch)
Put the preceding code block inside the epochs loop.