
I am a software engineer.
Hey 😊,
This is our first post of the New Year. Happy New Year! 🥂 I wish you all a prosperous year. It’s also our 30th chapter, the big 30 🤗. So we’re celebrating twice! 🎉
We’ll celebrate by building our very own Generative Adversarial Network GAN as promised 👽. We’ve learned about GANs in Chapter 24. You can check it out here to refresh your memory. This will be our last neural network implementation before we delve into Transformers and Large Language Models properly 🦾.
Problem
Create a Generative Adversarial Network to generate anime characters’ faces.
Dataset
We’ll use an anime face dataset from Kaggle, with 63,632 anime character faces.
Training dataset: https://www.kaggle.com/datasets/splcher/animefacedataset
Solution
We used to use Google Colab, but for this task, using Kaggle is the best solution I could find because it offers the best speed and processing power for free, which you’ll need a lot of. It’s over a hundred times faster than the Colab free version, at least for this task. You can create a new notebook at https://kaggle.com. You can use any other ML text editor, like Jupyter, on your device if it’s powerful enough.
You can find the complete notebook code PDF with the outputs of each step here.
Data preprocessing
a. Load dataset: Here, we get the training dataset from Kaggle directly since we’re using the Kaggle Notebook. We then create a list of the image paths for direct access.
The images would then need to be preprocessed to be represented with an array of numbers that the model can understand.
b. Data Preprocessing*:* The images are already in 64×64 size, but we’ll ensure that by resizing to that dimension, then convert them to numpy arrays and add them to a list we’ll call train_images.
Then we’ll reshape the 64×64 numpy arrays to a machine learning format like so (number_of_images, height, width, channels), so something like (63565, 64, 64, 3). The channels here represent the colors RGB. Afterwards, we normalise the values from 0 to 255 to -1 to 1.
Build Generator: As we already know, a generator and a discriminator in GAN, we’ll start the generator model. Basically, here we first create a dense one-dimensional layer with 8 × 8 × 512 = 32768 values. Then we use Conv2DTranspose, which is a type of Convolutional Neural Network to upsample images, which basically means generating a bigger image from a smaller image.
We create a method to build our generator model. We use a Convolutional Neural Network for our generator model, so I believe we already know how it works. If not, catch up here.
We start with an 8×8 feature map for the image to generate. We use Conv2DTranspose, which does upsampling of feature maps. Upsampling means increasing the number of dimensions, so here we start with an 8×8 image matrix as aseedand in each neural network layer, we increase the dimension by doubling the previous matrix,strides=(2,2)which means doubling the width and doubling the height. So at the end, we create a 64×64 matrix that represents an image 🤖. The last layer uses a tanh activation function to help normalize the final image output to values between -1 and 1, which is consistent with image pixel values.Build Discriminator: The discriminator model checks the images created by the generator and decides if they are fake or real.
We still use a CNN here to build our discriminator model as well. Here we use Conv2D, which downsamples, that is, the opposite of what the generator model does. Downsampling is reducing spatial dimensions, so we want the discriminator to be able to take an image and break it down to decide whether it is a real or fake image. We still use
strides=(2, 2)This time, this reduces the width and height by half at each neural network layer, so we start with a 64×64 image, and we break it down to an 8×8 image matrix.
The last layer takes the 3D feature map and turns it into a 1D vector. Then the sigmoid activation function layer finally returns an output of either 0 or 1, which decides if the image is fake or real.Train Model: We first start by creating a DCGAN. A DCGAN (Deep Convolutional Generative Adversarial Network) is a popular type of GAN that uses convolutional layers (Conv2D + Conv2DTranspose) instead of fully-connected layers to generate images.
The DCGAN passes random noise to the Generator model to generate fake images, then sends a mixture of real and fake images to the Discriminator model to get predictions. It uses the predictions to train the Generator to create better images to fool the Discriminator, and train the Discriminator to correctly predict real and fake images. This is repeated hundreds and thousands of times.
Here, we can see we have 50 epochs, each containing 1,987 batches. So each epoch goes through 1,987 batches; this happens 50 times, which totals to 99,350 batches 🥲 . Now you see why we need more GPU compute power, right? 👽
Results: Finally, we use the the dcgan.generator model we’ve trained to create new images.
The new model created 49 new anime images very similar to the anime images in the dataset! Welcome to your first Generative AI model from the ground up 🤗. You can now create new anime characters from your model 🥂
This is a big deal for us to be able to achieve these milestones even with the multiple detours and breaks. We have built our Machine Learning (AI) knowledge from zero to one, as the famous Peter Thiel would say. Now we are fortified to go full throttle into the breakthrough technology of modern ML, Large Language Models (LLMs) proper! We can now rapidly scale from one to one hundred.
See you on the next one 🦾




