Cartoonify Image using GAN(Generative Adversarial Network) | by Himanshu Sharma | Towards Data Science

GAN is basically an unsupervised learning algorithm that works on a neural network and generates samples from an image and also differentiates it from the original images which are used. For this, it uses two neural networks first one is a generator, and the second one is a discriminator.
In this article, we will use GAN to create a cartoon of an image that we will provide to the model. We will generate different cartoons by running the model on different epochs.
Before starting kindly follow me on medium by clicking here and stay updated about my new articles in the field of data science.
We will use Google Colab for this project, let us start with some basic commands which will be required for setting up google collab for our project.
#Used for Importing Tensorflow Version 1%tensorflow_version 1.x
After this, we will clone a GitHub repository that contains stylegan2 which we will use in our project. We will also enter the directory where stylegan2 is imported.
!git clone https://github.com/justinpinkney/stylegan2%cd stylegan2!nvcc test_nvcc.cu -o test_nvcc -run
The next step will be creating empty folders which will store our images, the original one as well as the generated ones.
!mkdir raw!mkdir aligned!mkdir generated
We will import the required libraries and download the model saved in the pickle version.
import pretrained_networksblended_url = “https://drive.google.com/uc?id=1H73TfV5gQ9ot7slSed_l-lim9X7pMRiU" ffhq_url = “http://d36zk2xti64re0.cloudfront.net/stylegan2/networks/stylegan2-ffhq-config-f.pkl"_, _, Gs_blended = pretrained_networks.load_networks(blended_url)_, _, Gs = pretrained_networks.load_networks(ffhq_url)
Now you need to add/upload your photos in the raw folder we created above and then run the command given below.
!python align_images.py raw aligned
The next command will be for training the model on1000 epochs by using aligned images.
!python project_images.py — num-steps 1000 aligned generated
Now we will define the main function where we will write the code for generating the cartoon version of the image we provided. We will also import some libraries which are required.
import numpy as npfrom PIL import Imageimport dnnlibimport dnnlib.tflib as tflibfrom pathlib import Path latent_dir = Path(“generated”)latents = latent_dir.glob(“*.npy”)for latent_file in latents:  latent = np.load(latent_file)  latent = np.expand_dims(latent,axis=0)  synthesis_kwargs =  dict(output_transform=dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=False), minibatch_size=8)  images = Gs_blended.components.synthesis.run(latent, randomize_noise=False, **synthesis_kwargs)  Image.fromarray(images.transpose((0,2,3,1))[0], ‘RGB’).save(latent_file.parent / (f”{latent_file.stem}-toon.jpg”))
This is the code that will generate the required images i.e the cartoon version of our images. Now comes the fun part where we will display the cartoonized images which are generated using the code above.
from IPython.display import Image embedded = Image(filename="generated/example_01.png", width=256)display(embedded)tooned = Image(filename="generated/example_01-toon.jpg", width=256)display(tooned)
See how GAN created this hilarious cartoon in my friend's image. Go ahead try this for different epochs to create different images and share your experiences in the response section.
This post is in collaboration with Piyush Ingale and he is the same person you see in the cartoon form above.
Thanks for reading! If you want to get in touch with me, feel free to reach me on hmix13@gmail.com or my LinkedIn Profile. You can view my Github profile for different data science projects and packages tutorials. Also, feel free to explore my profile and read different articles I have written related to Data Science.