How to do it...

The code is quite simple but the power of Keras really shines here—we are able to place six separate models into adversarial training in under 50 lines of code.

These are the steps for this:

  1. Make sure to get your imports for the implementation phase of the code:
#!/usr/bin/env python3
import sys
import numpy as np
from keras.models import Sequential, Model
from keras.layers import Input
from keras.optimizers import Adam, SGD
from keras.utils import plot_model
  1. Instantiate the class like we have in the past—notice the lambda variables:
class GAN(object):
def __init__(self, model_inputs=[],model_outputs=
[],lambda_cycle=10.0,lambda_id=1.0):
self.OPTIMIZER = SGD(lr=2e-4,nesterov=True)

self.inputs = model_inputs
self.outputs = model_outputs

lambda _cycle and lambda_id refer to the values of the loss functions for the X to Y generation and X to Y to X reconstruction generation, respectively. The lambda_id parameter should be 10% (according to the paper) of the lambda_cycle variable.

  1. Create a model with the input and output passed from the training class:
self.gan_model = Model(self.inputs,self.outputs)
self.OPTIMIZER = Adam(lr=2e-4, beta_1=0.5)

In this case, self.inputs are represented by an array of two Keras input classes instantiated in the training class and passed to the GAN class.

  1. The output array is six models, four generators, and two discriminators in an adversarial setup. In the array coming up, you can see how the models are stitched together:
self.gan_model.compile(loss=['mse', 'mse',
'mae', 'mae',
'mae', 'mae'],
loss_weights=[ 1, 1,lambda_cycle, lambda_cycle,
lambda_id, lambda_id ], optimizer=self.OPTIMIZER)
  1. Basic functionality of saving the model and print a summary that we have used in the chapters:
self.save_model()
self.summary()
  1. We use the same functionality from the previous two chapters:
def summary(self):
return self.gan_model.summary()
  1. This is the final method in our class:
def save_model(self):
plot_model(self.gan_model.model, to_file='/data/GAN_Model.png')

Considering we've implemented one of the more complicated architectures in basic GANs, this is concise representation!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.149.238