How to do it...

The discriminator takes the image as input and outputs a decision (real or fake). We'll cover the general construction of the discriminator class (hint: it'll look pretty similar to our previous discriminator recipes):

  1. First and foremost, we import all of the components we will use in this class:
#!/usr/bin/env python3
import sys
import numpy as np
from keras.layers import Input, Dense, Reshape, Flatten, Dropout,
BatchNormalization, Lambda, concatenate
from keras.layers.core import Activation
from keras_contrib.layers.normalization import InstanceNormalization
from keras.layers.convolutional import Convolution2D
from keras.layers.advanced_activations import LeakyReLU
from keras.models import Sequential, Model
from keras.optimizers import Adam, SGD,Nadam, Adamax
import keras.backend as K
from keras.utils import plot_model

In this import section, we are also importing the custom layer from the contributors package. If you have a few free moments, check out some of the other available layers in this package. The open source community certainly works hard to implement the more obscure layers.

  1. Instantiate the Discriminator class in a similar way—these are quite a few less parameters for CycleGAN:
class Discriminator(object):
def __init__(self, width = 28, height= 28, channels = 1):
self.W = width
self.H = height
self.C = channels
self.CAPACITY = width*height*channels
self.SHAPE = (width,height,channels)

self.Discriminator = self.model()
self.OPTIMIZER = Adam(lr=2e-4, beta_1=0.5)
self.Discriminator.compile(loss='mse',
optimizer=self.OPTIMIZER, metrics=['accuracy'] )

self.save_model()
self.summary()

You may be used to seeing the latent space named in each of the classes. For the style transfer domain, we will not generate our images from a randomly sampled set of noise but rather we will be using the image as an input. 

Another note is that the authors used the mean squared error (MSE) optimizer for their base discriminator implementation; this is different from previous models but a common function used in the deep learners. Moving on to the third step:

  1. Moving on to the model method in this class definition—start with our typical model development:
def model(self):
input_layer = Input(self.SHAPE)
  1. This should start to look more or less as a template for the discriminator—a few 2D convolutional layers until we get to our output layer:
up_layer_1 = Convolution2D(64, kernel_size=4, strides=2, 
padding='same',activation=LeakyReLU(alpha=0.2))(input_layer)

up_layer_2 = Convolution2D(64*2, kernel_size=4, strides=2,
padding='same',activation=LeakyReLU(alpha=0.2))(up_layer_1)
norm_layer_1 = InstanceNormalization()(up_layer_2)

up_layer_3 = Convolution2D(64*4, kernel_size=4, strides=2,
padding='same',activation=LeakyReLU(alpha=0.2))(norm_layer_1)
norm_layer_2 = InstanceNormalization()(up_layer_3)

up_layer_4 = Convolution2D(64*8, kernel_size=4, strides=2,
padding='same',activation=LeakyReLU(alpha=0.2))(norm_layer_2)
norm_layer_3 =InstanceNormalization()(up_layer_4)

As an aside, why don't we make the number of convolutional layers variable? There's one simple reason we can name right off the top of our head—the network would have enough capacity to essentially memorize the input. There is a trade-off between the number of layers and generalizability. If we memorize the input data, the model would diverge.

  1. The final few layers will bring us back to our output variable and appropriately flatten it:
output_layer = Convolution2D(1, kernel_size=4, strides=1,    
padding='same')(norm_layer_3)
output_layer_1 = Flatten()(output_layer)
output_layer_2 = Dense(1, activation='sigmoid')(output_layer_1)

return Model(input_layer,output_layer_2)

The return statement in this method should look similar to the generator. We need this type of structure to make our architecture easier when we start stitching these models together.

  1. Let's finish this class off with a few of our common helper functions:
def summary(self):
return self.Discriminator.summary()

def save_model(self):
plot_model(self.Discriminator.model,
to_file='/data/Discriminator_Model.png')
..................Content has been hidden....................

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