How to do it...

The GAN model is vastly simplified in comparison to the building of the generator and discriminator. Essentially, this class will put the generator and discriminator into adversarial training along with the custom loss functions.

Take the following steps:

  1. Use the python3 interpreter and import the necessary imports for the implementation, as follows:
#!/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
from loss import Loss
  1. Create the GAN class and create a few initialized variables, as follows:
class GAN(object):
def __init__(self, model_inputs=[],model_outputs=[], name='gan'):
self.OPTIMIZER = SGD(lr=2e-4,nesterov=True)
self.NAME=name
  1. Take the arrays of the model inputs and outputs from the instantiation and make them internal variables to the class, as follows:
self.inputs = model_inputs
self.outputs = model_outputs

It's important that the model inputs and outputs are arrays so we can pass an array of models into the GAN model.

  1. Create the model, add the optimizer, and compile the model with the loss functions, as follows:
        self.gan_model = Model(inputs = self.inputs, outputs = self.outputs)
self.OPTIMIZER = SGD(lr=0.001)
self.gan_model.compile(loss=[Loss.self_regularization_loss, Loss.self_regularization_loss],
optimizer=self.OPTIMIZER)
  1. Save the model graphic and write a summary to the screen, as follows:
        self.save_model_graph()
self.summary()
  1. As in previous sections, add the following definition in order to display the summary:
    def summary(self):
return self.gan_model.summary()
  1. Add another definition for saving the model graphic, as follows:
    def save_model_graph(self):
plot_model(self.gan_model, to_file='/out/GAN_Model.png')
  1. Add the following functionality. This will allow us to save the model if we want to use it later:
def save_model(self,epoch,batch):
self.gan_model.save('/out/'+self.NAME+'_Epoch_'+epoch+'_Batch_'+batch+'model.h5')
..................Content has been hidden....................

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