Build a Pix2Pix GAN With Python
Tags: Machine Learning, Generative Adversarial

Build the network

A GAN network is composed of a generator and a discriminator. A generator is like a student, trying to mimic a masterpiece as his homework. The discriminator then serves as a teacher, giving feedbacks like Good ✅ (‘It looks real!’) or Bad ❌ (‘Nah, it’s so fake.’) to the student’s work. The student does his homework again and again, while the teacher tells him whether he is doing well each time. Once the teacher cannot distinguish the work of the student and the actual masterpiece, we then consider the student is able to create images that are good enough (Poor student 😿)

Q. If discriminator knows so well about how an image is good or bad, why don't it generate the image on its own? argmax D(x), which aims to maximize the score of classifying real and generated images. This function turns out to be too complicated to address if we do not define any limitation. Since this limitation will also restrict the model's capacity, people find it easier to replace solving the argmax D(x) function with a separate generator.



👨🏼‍🏫 A. To be honest, yes it can generate images. However, since the discriminator excels at seeing a big picture, creating them from pixels becomes an arduous work for it. To generate an image using a discriminator, we have to solve the functionwhich aims to maximize the score of classifying real and generated images. This function turns out to be too complicated to address if we do not define any limitation. Since this limitation will also restrict the model's capacity, people find it easier to replace solving thefunction with a separate generator.👨🏼‍🏫 Here is a lecture about it

1. Define discriminator

ReLU and Leaky ReLU, leak and extending the range of ReLU. If you are interested in the difference betweenand here is a well-explained article 🌈 In short, Leaky ReLU changes the slope of ReLU where x < 0, causing aand extending the range of ReLU.

From the code above, it’s not hard to tell that a discriminator is basically a classifier. It takes an input image then categorizes it into “Real” or “Fake”.

2. Define Generator

As for generator, its structure is more complicated 😣 Its implementation combines a decoder and an encoder (Its a U-Net structure here). The encoder tries to break the input image down into smaller pieces. From these pieces, the decoder later tries to scale it up and generate a new image in the end.

UpSampling2D instead of Conv2DTranspose in other generators. The key difference between these two functions is that, UpSampling2D is just a simple scaling up of the image by using nearest neighbour or bilinear upsampling. But Conv2DTranspose will not only upsample its input but also learn what is the best upsampling kernel. (from You may also seeinstead ofin other generators. The key difference between these two functions is that, UpSampling2D is just a simple scaling up of the image by using nearest neighbour or bilinear upsampling. But Conv2DTranspose will not only upsample its input but also learn what is the best upsampling kernel. (from this Stack Overflow answer

3. Define GAN

Putting discriminator and generator together, now we have our GAN. An interesting note here is that, we have to set discriminator as “not trainable”. Because if it is trainable, the generator will adjust discriminator’s weights and make it easier to fool it 😱 No, we don’t want this 🤪🤪🤪