09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Generative Adversarial Networks

DCGAN for MNIST digits

Let us now build a DCGAN for generating handwritten digits. We first see the

code for the generator. The generator is built by adding the layers sequentially. The

first layer is a dense layer that takes the noise of 100 dimensions as an input. The

100-dimensional input is expanded to a flat vector of size 128 × 7 × 7. This is done so

that finally we get an output of size 28 × 28, the standard size of MNIST handwritten

digits. The vector is reshaped to a tensor of size 7 × 7 × 128. This vector is then

upsampled using TensorFlow Keras UpSampling2D layer. Please note that this layer

simply scales up the image by doubling rows and columns. The layer has no weights,

so it is computationally cheap.

The Upsampling2D layer will now double the rows and columns of the 7 × 7 × 128

(rows × columns × channels) image, yielding an output of size 14 × 14 × 128. The

upsampled image is passed to a convolutional layer. This convolutional layer learns

to fill in the details in the upsampled image. The output of convolution is passed

to batch normalization for better gradient flow. The batch normalized output then

undergoes ReLU activation in all the intermediate layers. We repeat the structure,

that is, upsampling | convolution | batch normalization | ReLU. In the following

generator we have two such structures, the first with 128 filters, and the second

with 64 filters in the convolution operation. The final output is obtained from

a pure convolutional layer with 3 filters and tan hyperbolic activation, yielding

an image of size 28 × 28 × 1:

def build_generator(self):

model = Sequential()

model.add(Dense(128 * 7 * 7, activation="relu", input_dim=self.

latent_dim))

model.add(Reshape((7, 7, 128)))

model.add(UpSampling2D())

model.add(Conv2D(128, kernel_size=3, padding="same"))

model.add(BatchNormalization(momentum=0.8))

model.add(Activation("relu"))

model.add(UpSampling2D())

model.add(Conv2D(64, kernel_size=3, padding="same"))

model.add(BatchNormalization(momentum=0.8))

model.add(Activation("relu"))

model.add(Conv2D(self.channels, kernel_size=3, padding="same"))

model.add(Activation("tanh"))

model.summary()

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

img = model(noise)

return Model(noise, img)

[ 200 ]

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

Saved successfully!

Ooh no, something went wrong!