Fake Face Image Classification

Original article can be found here (source): Deep Learning on Medium

Don’t trust everything you see, sometimes fake makes more impact than real.

We believe what we see right ? But with the advancement of science and technology we have came to see a lot of new things which we as a regular person didn’t actually see like exoplanets, quarks etc.( except for the scientists though ) but they really exists. let me ask you a question :

Face Image

Can you look at the picture and tell if it is real or not ? You may be thinking it is real but what if I tell you this is a fake image generated using GANs ( Generative Adversarial Networks ) one of the growing field of Machine Learning/Artificial Intelligence. With the rapid development in GANs it became easy to generate fake faces and videos which brings the threats like identity theft and privacy breaches for example face unlock feature of current smart phones, any one can use fake faces to unlock your phone. In this post I will explain how to make a fake face classifier using computer vision.

First thing that matters in computer vision is data ( images ), to make a good classifier you must have decent amount of data. For making fake face classifier I am using 10,000 images of both the classes i.e. real and fake class. The Whole pipeline is divided into two sections :

  1. Identify the faces in an image
  2. classify the faces as real or fake

we will use deep learning based face detection and then will use a CNN classifier for the fake face classification. so lets start building a classifier.

Identifying Faces ( Face Detection ) in Images :

There are multiple techniques available for identifying the face in an image but the preferred one among all are, feature based cascade classifier and deep learning based Multi-task Cascade CNN.

Deep Learning based Multi-task cascade CNN ( MTCNN ) considered to be the state-of-the-art ( Best among all ), so for face detection in an image we will use MTCNN.

MTCNN is the cascade structure of three networks; Proposal Network ( P-Net ), Refine Network ( R-Net ) and Output Network ( O-Net ). The process starts with rescaling the image to a range of different sizes ( referred as image pyramid ) and passing them to the P-Net which proposes the facial regions in image, then R-Net filters the bounding boxes of the images and finally the O-Net proposes facial landmarks i.e. eyes, mouth etc. Implementation of the MTCNN is fairly complex but nothing to worry since its implementation is open source we can directly use it.

Source : Towards Data Sciecne

Python has a module named mtcnn which provide implementation of MTCNN algorithm, it can be installed using pip:- pip install mtcnn

Before doing your hands dirty with code please find the data folders here https://drive.google.com/open?id=1smby8vBB0g8bNtUsQ10OdjF-4FK9k_GS, these folders contains preprocessed images, you can use them directly for model training.

Let’s start coding:

MTCNN Face Detection: code for face detection using single image file is as follows —

Single Image Face Detection

also if you need to prepare data all by yourself you can use the following code:

Folder Wise Image Face Detection

we will use these training and testing set for the training of our network.

Training CNN ( Convolutional Neural Network ) for Fake Face Classifiaction:

For training the CNN classifier you just need to have keras module of python. you can install it using pip : pip install keras

So let’s start making classifier:

  1. Import Dependencies :
Import Dependencies

2. Create Model Layers: This classifier will have 3 convolution layers, 3 maxpool layers, 1 flattening layer and finally an output layer with sigmoid activation.

Model Layers

3. Use Image Data Generator for training the model: We will use Image data generator to simply the training process.

Image DataGenerator

I was able to get 98.3% accuracy in just 8 epochs.

Training Process

4. Save weights and model hierarchy : Finally save the model weights and its structure so that every time when we will use this model we will not have to train it again and again.

Save Model

Fake Face Classification Model Testing:

Since we have created out model and saved it, its time to test our model on different fake and real images.

Test Model

so now you can train and test your Fake Face Classifier in just few lines of codes. So this is it, Thanks for reading this post, feedback is always appriciated.

See you next time !!!❤️