concept generator in category deep learning

appears as: generator, The generator, generator
Deep Learning for Vision Systems

This is an excerpt from Manning's book Deep Learning for Vision Systems.

The generator takes in some random data and try to mimic the training dataset to generate fake images. Its goal is to trick the discriminator by trying to generate images that are perfect replicas from the training dataset. As it gets trained it gets better and better after each iteration. On the other hand, the discriminator is being trained at the same time so the generator has to keep improving as the discriminator learns its tricks.

Figure 8.7: The generator model of the GAN.

As you might have noticed from Figure 8.7, the generator model looks like an inverted ConvNet. The generator takes a vector input with some random noise data, reshapes it to cube volume that has a width, height and depth. This volume is meant to be treated as a feature map that will be fed to several convolutional layers that will create the final image.

def generator_model():
# instantiate a sequential model and name it generator
       generator = Sequential()
# add the Dense layer that has a number of neurons = 128x7x7
       generator.add(Dense(128 * 7 * 7, activation="relu", input_dim=100))
# reshape the image dimensions to 7 x 7 x 128
       generator.add(Reshape((7, 7, 128)))
# upsampling layer to double the size of the image dimensions to 14 x 14
       generator.add(UpSampling2D(size=(2,2)))
 
# add a convolutional layer to run the convolutional process + batchNorm
       generator.add(Conv2D(128, kernel_size=3, padding="same", activation="relu"))
       generator.add(BatchNormalization(momentum=0.8))
# upsample the image dimensions to 28 x 28
       generator.add(UpSampling2D(size=(2,2)))
 
# conv + batchNorm layers
# notice that we didn’t add upsampling here because we have the image size of 28 x 28 which is equal to the image size in the MNIST dataset. You can adjust this for your own problem.
       generator.add(Conv2D(64, kernel_size=3, padding="same", activation="relu"))
       generator.add(BatchNormalization(momentum=0.8))
 
# convolutional layer with filters = 1
       generator.add(Conv2D(1, kernel_size=3, padding="same", activation="relu"))
 
# print the model summary
       generator.summary()
 
# generate the input noise vector of length = 100
# we chose 100 here to create a simple network
       noise = Input(shape=(100,))
# run the generator model to create the fake image
       fake_image = generator(noise)
# return a model that takes the noise vector as an input and outputs the fake image
       return Model(noise, fake_image)

GANs plays a minmax game, where the entire network attempts to optimize the function V(D,G) in Figure 8.14a. The goal of the discriminator (D) is to maximize the probability of getting the correct label of the image. Whereas, the generator’s (G) goal on the other hand is to minimize the chances of getting caught, hence the name minmax objective function.