Setting up the class

Follow these steps to set up your class and initialize your training method:

  1. The imports in the training function are primarily not Keras and simply NumPy and other helper libraries:
#!/usr/bin/env python3
from gan import GAN
from generator import Generator
from discriminator import Discriminator
from keras.layers import Input
from keras.datasets import mnist
from random import randint
import numpy as np
import matplotlib.pyplot as plt
from copy import deepcopy
import os
from PIL import Image
import random
import numpy as np
  1. The Trainer class starts out with the same types of inputs from other models:
class Trainer:
def __init__(self, height = 256, width = 256, channels=3,
epochs = 50000, batch = 1, checkpoint = 50,
train_data_path = '',test_data_path=''):
self.EPOCHS = epochs
self.BATCH = batch
self.H = height
self.W = width
self.C = channels
self.CHECKPOINT = checkpoint
  1. Read in the dataset for training and another set of dataset for comparing when we use the checkpoint plots:
self.X_train_A, self.X_train_B = self.load_data(train_data_path)
self.X_test_A, self.X_test_B = self.load_data(test_data_path)
  1. Instantiate the Generator object within the training class:
self.generator = Generator(height=self.H, width=self.W, 
channels=self.C)
  1. Create two input with the shape of our input images—in this case, our images have the exact same size, shape, and number of channels:
self.orig_A = Input(shape=(self.W, self.H, self.C))
self.orig_B = Input(shape=(self.W, self.H, self.C))
  1. Here's the conditional training part—we use orig_b as the input to the generator:
self.fake_A = self.generator.Generator(self.orig_B)7.
  1. Next, we build the Discriminator object and set the discriminator network trainable to False:
self.discriminator = Discriminator(height=self.H, width=self.W, 
channels=self.C)
self.discriminator.trainable = False
  1. With this discriminator object, we then point the inputs to the fake_A and orig_B networks we created:
self.valid = 
self.discriminator.Discriminator([self.fake_A,self.orig_B])
  1. Finally, we have all the correct connections setup. We now create our adversarial model with the inputs of orig_A and orig_B and the outputs of valid and fake_A:
model_inputs = [self.orig_A,self.orig_B]
model_outputs = [self.valid, self.fake_A]
self.gan = GAN(model_inputs=model_inputs,model_outputs=model_outputs)

Now, we move onto how we are going to train this network with the main training method!

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

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