We will cover the following tasks in 1 hour and 8 minutes:
Introduction & Importing Libraries
Unlike the previous series on TensorFlow on Rhyme, this series on the Cloud AI Platform must be taken in a sequence because even though we are looking at a new concept in this project, a lot of things related to the Cloud platform are going to be in continuation so not everything may make sense to you if you take this project independently of the series. So, just in case this is the first project you are doing in the series, I’d recommend that you go back and start the series from the beginning with the first project and then come back to this one.
We want to start leveraging GPUs for our model training from now on. This is because CPUs can be quite slow and limited in performance when working with Tensors but GPUs do much much better. In fact, in most cases, you can improve the speed of your computations by 100 times or more. So, certain deep learning models just aren’t trainable JUST on CPUs for that reason. And one really cool thing about the Google Cloud AI Platform is that it allows you to provision different types of GPUs for your Notebook instances. Rather for the Virtual Machines that your Notebook instances are using.
Importing & Processing the Data
We have worked with the MNIST dataset a few times before, so you should be quite familiar with it by now. Let’s import it. Even though we are quite familiar with the dataset, let’s take a look at some of the images. Note that we are going to create a Convolutional Network for our GAN, so we will need channel information on the image examples.
The idea is that the generator will have to become better and better as it trains to try and fool the discriminator. In turn, the discriminator keeps learning as well and tries to be able to recognise the fake images even if they seem like the real images. Over time, as both models train, they both keep improving and ultimately we get to a point where the generator starts creating very real-like images.
So, we will start by creating a generator model. We will start by taking a random sample of 100 values and our generator model should be able to create a new 28 by 28 image out of that input. This means that this model will scale up the information and fill in the blanks to generate a lot more information from the given input.
Now we want to create a model which will be able to distinguish between real and fake images as it trains. This would look more like a regular convolutional network that we are used to. The model should take a 28 by 28 image as input and give us a binary yes or no output. While the generator will be trained as part of the final GAN model that we are yet to create, the discriminator will be trained independently even though it will still be a part of the GAN model. I know it may sound a bit confusing, but it will become clearer in the next task.
Generative Adversarial Network
Now that we have the generator and the descriminator, all we have to do in order to create the GAN is basically just connect the two togerher. As always, we will use Keras’ functional API to do this. Once small detail to note here is that when we train this GAN later, we essentially want to train JUST the generator on a given batch. The discriminator must be trained separately of course but when it’s used as part of the GAN, it does not undergo any training and is used just to, well, find out how many fake and real images are there in the input. So, to that effect, first thing we will do is set the discriminator to not-trainable. We will change it later in the training loop but only on the part where the discriminator is independently trained.
When we train the GAN, we might want to take a look at the generated images at every epoch. This will help us see how the generator is performing. So, let’s create a function called
plot and we will call it later from the training loop. Of course the images right now are just random noise because the generator isn’t trained yet. Let’s do the training in the next task.
Training the GAN
We have created the GAN and now all we have to do is train it and take a look at the results! What we want to do here is first generate a bunch of noise vectors to feed into the generator. Then, combine the generated images along with some real images from the MNIST dataset. We will also create labels: 0 for the fake images and 1 for the real images. This will give us the training set for the discriminator to learn from. There’s a small detail here I’ll get to when writing the code.
Then we will again generate some noise vectors and input to the GAN this time. This time we will set all the labels to TRUE - forcing the generator to try and figure out a way to best beat the currently trained discriminator. Note that we won’t update or train the discriminator at this stage when training the GAN. The training will take a little bit of time. Specifically, if you’re using the same K80 GPU that I am using, you should see every epoch taking between 90-100 seconds.
Also, if you run the plot function - you get a few new images from the generator. A lot of times when you see super realistic deep fakes, you have to understand that not every single example generated by a GAN generator is going to be a realistic one even after a long training with a lot of data. But some examples will be very very close to the real images. So, often researchers will display the best results which are hand-picked and not ALL the generated results and that may make it seem like the generators are a bit better than they actually are. This is still a pretty amazing technique of course and one that I hope will be useful to you in future.
About the Host (Amit Yadav)
I am a machine learning engineer with focus in computer vision and sequence modelling for automated signal processing using deep learning techniques. My previous experiences include leading chatbot development for a large corporation.