Cainvas
Model Files
real_vs_fake.h5
keras
Model
deepSea Compiled Models
real_vs_fake.exe
deepSea
Ubuntu

DeepFake Face Detection

Credit: AITS Cainvas Community

Photo by Javier Jaén, Svetikd on The New Yorker

We have seen after the development of GANs, Deepfakes came into existence.Though the development of these techniques were primarily to increase the amount of training data but many people were found misusing these techniques for criminal activities. So, it is the need of the hour to develop one such model which can differentiate between real and deepfake faces.

Import Dataset and Necessary Libraries

In [1]:
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/realVSfake.zip"
!unzip -qo realVSfake.zip 
!rm realVSfake.zip
--2020-10-28 05:32:41--  https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/realVSfake.zip
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.62.120
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.62.120|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 97144373 (93M) [application/zip]
Saving to: ‘realVSfake.zip’

realVSfake.zip      100%[===================>]  92.64M  24.3MB/s    in 3.8s    

2020-10-28 05:32:44 (24.3 MB/s) - ‘realVSfake.zip’ saved [97144373/97144373]

In [2]:
import numpy as np
import pandas as pd
from keras.applications.mobilenet import preprocess_input
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Dense,BatchNormalization, Flatten, MaxPool2D
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau, Callback
from keras.layers import Conv2D, Reshape
from keras.utils import Sequence
from keras.backend import epsilon
import tensorflow as tf
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from keras.layers import Convolution2D, Conv2D, MaxPooling2D, GlobalAveragePooling2D
import cv2


from tqdm.notebook import tqdm_notebook as tqdm

import os
In [3]:
print(os.listdir("realVSfake/real_and_fake_face"))
['training_fake', 'training_real']
In [4]:
real = "realVSfake/real_and_fake_face/training_real/"
fake = "realVSfake/real_and_fake_face/training_fake/"

real_path = os.listdir(real)
fake_path = os.listdir(fake)

Visulaizing the real and fake faces

In [5]:
def load_img(path):
    image = cv2.imread(path)
    image = cv2.resize(image,(224, 224))
    return image[...,::-1]
In [6]:
fig = plt.figure(figsize=(10, 10))

for i in range(16):
    plt.subplot(4, 4, i+1)
    plt.imshow(load_img(real + real_path[i]), cmap='gray')
    plt.suptitle("Real faces",fontsize=20)
    plt.axis('off')

plt.show()
In [7]:
fig = plt.figure(figsize=(10,10))

for i in range(16):
    plt.subplot(4, 4, i+1)
    plt.imshow(load_img(fake + fake_path[i]), cmap='gray')
    plt.suptitle("Fakes faces",fontsize=20)
    plt.title(fake_path[i][:4])
    plt.axis('off')

plt.show()