DeepFake Detection using Convolutional Neural Networks

With this Machine Learning Project, we will be building a deepfake detection system. The deepfake detection system is used to detect whether the image is a real image or a deepfake image. This can be used in a lot of places. This can help police to solve crimes. Deepfakes are a very serious issue that needs to be solved.

So, let’s build this deepfake detection system using convolutional neural networks.

Deepfake Detection System

So, what is this deepfake Detection System?

A deepfake detection system is a system that will predict whether an image is a real image or it is a deepfake image.

What is a deepfake image?

A deepfake image is an image in which a person’s face is swapped with someone else’s using neural networks. Deepfakes can be used by criminals to do crimes by using someone else’s face. It can be used by news media to spread fake news. It can be used by bad people and they can swap your face in a porn video and then viral it which will just take seconds to destroy your life. That’s why a deepfake detection system is really necessary.

So, let’s build our system with these convolutional neural networks.

Convolutional Neural Networks

A neural network is an algorithm of Machine Learning which is at the heart of deep learning algorithms. These algorithms are used in a lot of machine learning algorithms. When working with image classification, these appear to outperform in every parameter. They are very easy to train. With just a few lines of code, you can create neural networks using the inbuilt functions of the Keras library.

Convolutional Neural Networks are composed of node layers, convolution layer, pooling layer, hidden layer, and output layer. Our image passes through all these layers and every step has its own importance. It is in these layers where all the feature extraction of the image occurs. Each layer contains nodes that have weights within them. These weights get updated with each backward pass.

There are two passes in the neural networks.

  1. Forward Pass – It is the propagation from the input layer to the output layer. It refers to the calculation of variables as we move towards the output layer. At the output layer, the data loss is calculated as an error. With the help of this error, weights on the nodes are updated.
  2. Backward Pass – It is the propagation from the output layer to the input layer. It is the correcting and updating of the value of the weights based on the error rate that resulted in the forward pass.

CNN is highly used in image classification problems and object recognition problems because it is very good in feature extraction leveraging principles of linear algebra, specifically matrix multiplication to identify patterns in the image but requires a lot of computation power to do the tasks.

The Model Architecture

We are using the Convolutional Neural Networks to build the model. They have superior performance in comparison to other neural networks in image, speech, and audio inputs. CNN’s are formed using 3 types of layers.

With each layer, the complexity of the neural networks increases. The initial layer of the neural network is useful for identifying the color and edges in the image. But as the network goes deeper into the layers, it starts identifying more portions of the image and starts identifying objects, shapes, etc.

Three Layers of the Convolutional Neural Networks:

  1. Convolution Layer: This layer is the building block of CNN. This is the layer where the majority of the computations occur. This layer has 3 inputs. The first is input data. The second is a filter and the third is a feature map. The convolution process occurs on input data that is the image using filters. Filters are nothing but a 3×3 matrix that is iterated over the input data which is also a matrix. As the filter is fed over the area of the input matrix, there is an activation function that calculates the result of the filter and the area of the image. The output is then stored in the feature matrix.
  2. Pooling Layer: This layer is used for downsampling that reduces the number of parameters in our input image. It works in a similar way to the convolution layer. The only difference is that the filters in convolution layers contain some weights on which computation is performed by activation function whereas in the pooling layer we either take the max value in the area of the input matrix or we take the average value in the area of the matrix. There are two types of pooling:
    1. Max Pooling– In this pooling, we take the maximum of all the values in the patch of the matrix.
    2. Average Pooling– In this pooling, we take the average of all the values in the patch of the matrix.
  3. Fully Connected Layer– In this layer, finally all the calculations we have done in the previous steps will pay off in this step. This layer finally classifies the image. In this layer, our input matrix is flattened and it is passed through hidden layers of the neural networks. The previous Layers use ReLu functions for all computation while this layer uses the Softmax function for multiclassification problems and the Sigmoid function for binary classification problem.

So, this is how our model is working.

Project Prerequisites

It is necessary that you have Python 3.6 installed on your computer as well as Anaconda.

The required modules for this project are –

  1. Numpy – pip install numpy
  2. Tensorflow – pip install tensorflow
  3. Keras – pip install keras

We only need that for our Convolutional neural networks. The Tensorflow module contains all the functions required for creating an optimized Convolutional Neural Network.

DeepFake Detection Project

We have already created the dataset for you. It can be downloaded from the link below. It contains three folders for training, testing, and verifying the model that we have built is working or not. Please download deep fake detection machine learning code and dataset from the following link: DeepFake Detection Project

Steps to Implement DeepFake Detection Project

1. As part of the project, import the modules you need.

import numpy as np
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator

2. Using the imageDataGenerator described above, we are reading the training dataset.

train_datagen = ImageDataGenerator(
    rescale=1./255,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip=True)
training_set = train_datagen.flow_from_directory('train',target_size=(224,224),batch_size=32,shuffle=True,class_mode='binary')

3. This is just reading the dataset.

test_datagen = ImageDataGenerator(rescale=1./255)

test_set = test_datagen.flow_from_directory('test',target_size=(224,224),batch_size=16,shuffle=False,class_mode='binary')

4. Our neural network is being built here. Tensorflow provides all the functions for easily creating neural networks. This is merely a trigger. Clearly, all the functions explained above are called here & nothing is particularly difficult.

cnn = tf.keras.models.Sequential()
 
cnn.add(tf.keras.layers.Conv2D(filters=224 , kernel_size=3 , activation='relu' , input_shape=[224,224,3]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
 
cnn.add(tf.keras.layers.Conv2D(filters=224 , kernel_size=3 , activation='relu' ))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2 , strides=2))
 
cnn.add(tf.keras.layers.Dropout(0.5))
cnn.add(tf.keras.layers.Flatten())

cnn.add(tf.keras.layers.Dense(units=128, activation='relu'))
cnn.add(tf.keras.layers.Dense(units=1 , activation='sigmoid'))
cnn.compile(optimizer = 'Adam' , loss = 'binary_crossentropy' , metrics = ['accuracy'])

5. As we trigger the fit function, the training dataset will be fitted into our neural networks, and the test dataset will be used to test the network.

cnn.fit(x = training_set , validation_data = test_set , epochs = 50)

6. As a result of the successful training and testing of our model. Here are some examples to predict. Using this code, we load the image through Keras’ load_img function and then we convert it into an array to be passed through neural networks.
To predict the outcome, we called cnn.predict.

from keras.preprocessing import image
test_image = tf.keras.utils.load_img('prediction/deepfake1.jpg',target_size=(224,224))
test_image = tf.keras.utils.img_to_array(test_image)
test_image = np.expand_dims(test_image,axis=0)
 
result = cnn.predict(test_image)
training_set.class_indices

print(result)
plt.imshow(image)

Summary

In this Machine Learning project, we built a deepfake detection system. We used Convolutional Neural Networks to build our neural network in our project. We hope you learn how to make a deepfake detection system.