Generating skull image with DCGAN

gyiernahfufieland
Analytics Vidhya
Published in
5 min readAug 27, 2021
Photo by Mathew Schwartz on Unsplash

What if I tell you there’s an interesting way in having fun while contributing to the data world? Yes ! It’s true. Have you heard of Quick, Draw! It’s an online game developed by Google that gathers picture drawn by players and then using Neural Network AI, the model tries to guess what the drawings represents.

Now, let’s get started!

First, we call the necessary libraries:

And then some usual housekeeping stuffs:

As you can see here, I am using the skull.npy dataset from Quick, Draw! The dataset can be obtained here. There are a total number of 126,174 images in the dataset. We then normalize the images.

Now let’s generate a random image for visualization.

Hmm. Looking great. Next we define our buffer size and batch size, and creates our training dataset.

Next we should start building our Generator and Discriminator. Before we do so, let’s import some libraries.

The Generator that will be created here uses Conv2DTranspose (upsampling) layers to produce and image from a seed. Starting with a dense layer that takes this seed as input, then upsample several times until it reaches the desired image size of 28x28x1. The LeakyReLU activation is used for each layer, except for the output layer which is using tanh.

Let’s try to generate an image using the generator. Note that at this point, the generator has yet to be trained.

hmm. Some training needs to be done obviously. Moving on to build our Discriminator model.

This model will be used to classify the generated images as real or fake. Positive values represent real images, while negative values represent fake images.

Then we define the loss and optimizers.

Since we are classifying between real and fake images (positive and negative values), the BinaryCrossentropy is used. Now, now. To evaluate how our Discriminator model is doing in classification task, we need to calculate the discriminator loss by comparing its predictions on real images to an array of 1s, and predictions on fake images (generated) to an array of 0s.

one_like returns an array of ones with the same shape and type as a given array, while zero_like returns an array of zeros with the same shape and type as given.

And then of course we will have to evaluate our Generator performance, whether it is good enough in tricking the Discriminator. If the generator performs well, discriminator will classify the fake images as real (represented as 1).

Since both networks will be trained separately, the optimizers are defined separately. Adam optimization is used here.

And to have the model parameters saved, we will create checkpoints.

Alright. We are getting near to start training our model. Usual housekeeping process over here:

First, let’s try training the models with 10 epochs and see how well it is doing. Before that let’s create a training loop.

The loop begins with generator receiving random seed as input and generates and image. The discriminator then tries to classify between real and fake images. The loss for both models are calculated, and the gradients are used to update the models.

Life is a constant improvement of yourself ! — Jan Condor

And then we will be saving these generated images, to be used for our gifs creation.

And finally, creating a train function.

Alright ! We are finally training our model ! Starting with 10 epochs.

Let’s try calling our gifs to see how the generated images progress along the way.

Well obviously, with only 10 epochs, it is insufficient to generate a basic skull image. But the framework is there. Let’s try with 192 epochs.

A ! Lot ! Better !

It really is interesting to see how the generated images constantly improve along the way. GAN truly is an interesting model to work with.

And that’s it ! Try creating another model using another dataset. This project was created using the codes from here.

Full codes here.

--

--

gyiernahfufieland
Analytics Vidhya

从我的视野分享我爱的一切。Hey, how are you today?