09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

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

Generative Adversarial Networks

The complete GAN is made by combining the two:

class DCGAN():

def __init__(self, rows, cols, channels, z = 100):

# Input shape

self.img_rows = rows

self.img_cols = cols

self.channels = channels

self.img_shape = (self.img_rows, self.img_cols, self.channels)

self.latent_dim = z

optimizer = Adam(0.0002, 0.5)

# Build and compile the discriminator

self.discriminator = self.build_discriminator()

self.discriminator.compile(loss='binary_crossentropy',

optimizer=optimizer,

metrics=['accuracy'])

# Build the generator

self.generator = self.build_generator()

# The generator takes noise as input and generates imgs

z = Input(shape=(self.latent_dim,))

img = self.generator(z)

# For the combined model we will only train the generator

self.discriminator.trainable = False

# The discriminator takes generated images as input and

# determines validity

valid = self.discriminator(img)

# The combined model (stacked generator and discriminator)

# Trains the generator to fool the discriminator

self.combined = Model(z, valid)

self.combined.compile(loss='binary_crossentropy',

optimizer=optimizer)

The GAN is trained in the same manner as before; first random noise is fed to the

generator. The output of the generator is added with real images to initially train the

discriminator, and then the generator is trained to give an image that can fool the

discriminator. The process is repeated for the next batch of images. The GAN takes

between a few hundred to thousands of epochs to train:

[ 204 ]

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

Saved successfully!

Ooh no, something went wrong!