16.03.2021 Views

Advanced Deep Learning with Keras

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Generative Adversarial Networks (GANs)

# Returns

Model: Discriminator Model

"""

kernel_size = 5

layer_filters = [32, 64, 128, 256]

x = inputs

y = Dense(image_size * image_size)(y_labels)

y = Reshape((image_size, image_size, 1))(y)

x = concatenate([x, y])

for filters in layer_filters:

# first 3 convolution layers use strides = 2

# last one uses strides = 1

if filters == layer_filters[-1]:

strides = 1

else:

strides = 2

x = LeakyReLU(alpha=0.2)(x)

x = Conv2D(filters=filters,

kernel_size=kernel_size,

strides=strides,

padding='same')(x)

x = Flatten()(x)

x = Dense(1)(x)

x = Activation('sigmoid')(x)

# input is conditioned by y_labels

discriminator = Model([inputs, y_labels],

x,

name='discriminator')

return discriminator

Following listing highlights the code changes to incorporate the conditioning onehot

labels in the generator builder function. The Model instance is modified for the

z-vector and one-hot vector inputs.

Listing 4.3.2, cgan-mnist-4.3.1.py shows us the CGAN generator. In highlight

are the changes made in DCGAN:

def build_generator(inputs, y_labels, image_size):

"""Build a Generator Model

[ 118 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!