How to make Deepfakes for free!
Tack that pretty face of yours onto your favorite celebrity, with Google Colab.
⛔ DISCALIMER⛔: Deepfakes are highly controversial and should not be created with the purpose of spreading misinformation. This tutorial is intended for educational purposes only.
What is a Deepfake exactly?
If you’ve reached this article, I’m assuming you already know what a Deepfake is. But just in case you don’t :
“A deepfake is a form of media where a person in an existing image, video, or GIF is distorted to resemble something or someone else. These are most often created via machine learning methods that employ generative neural network architectures. ~ Wikipedia ”
Deepfakes are tricky
When Artificial Intelligence(AI) first came into its own as an academic discipline in the 1950s, researchers were probably thinking that we might be ushering in a new era of mankind’s development. Nearly 70 years on, AI has drastically altered the way we live and experience the world around us, but not necessarily in the ways we might have imagined.
FUNFACT: When the term “deepfake” was first coined at the end of 2017, most of the deepfakes being circulated were of celebrity faces being (unfortunately) swapped onto pornstars. The non-pornographic deepfake content that was swirling on the internet was primarily made from Nicolas Cage’s face swapped into various movies.
The last thing any of those researchers would have foretold is that today, a 12-year-old kid can now download an app and plaster their face on top of Donald Trump at the click of a button.
Today, deepfakes continue to be rooted in controversy as much as they have become incredibly popular in entertainment media. Check out this video made by an anonymous user who created a deepfake of Bill Hader morphing into Tom Cruise and other celebrities on David Letterman’s Saturday Night Live.
All this to say one thing: Deepfakes are fun to play around with but they can be extremely damaging when used with malicious intent.
You’ve been warned.
Why not use an app?
If you’ve looked into creating deepfakes, the first thing you probably encountered is a whole host of deepfake making apps like Reface and DeepFaceWeb, etc. If these fit your needs, go ahead and get started! That said, here are some reasons why I chose to make the deepfakes without the help of a third-party app:
- Cost. Thankfully, many deepfake mobile apps/web apps are free. The most convincing services often involve subscription fees baked into their platform. Some of these apps might even do a generic face swap that hardly looks authentic, (and you want the real deal, don’t you?)
- Security. Many of these third-party apps become privy to your data and pictures when you use them. I don’t feel the most comfortable knowing that my content is being held by a platform that specializes in deepfakes.
Lastly, what’s the fun in that? If you’re on Medium, chances are that you like to learn new things. Why use a third-party app to create a deepfake for you when you could actually do it yourself and learn something new in the process?
Still want to make your own deepfakes? Keep reading!
Old Laptop? Don’t worry
Under normal circumstances, creating your own deepfakes would require access to some powerful GPUs. To make this tutorial as accessible as possible,(especially for folks like me who use decade-old laptops that are miraculously still functional???) we’ll be using Google Colab.
Google Colab is an incredible platform provided by (you guessed it) Google to democratize access to powerful GPUs. It even allows you to mount your Google Drive files onto the coding environment, giving you easy access to whatever media you might want to use from your cloud storage.
NOTE: GPU usage on Google Colab is not unlimited! For free users, Google Colab has limited RAM and Disk Space. However, the free tier is more than enough to get us through this tutorial.
With all that said, let’s jump right in!
Getting Started
Note: I’m only going to be covering how you can create your own deepfakes with pre-trained models in this article. For how to train your own models for deepfakes, stay tuned!
For the purpose of this tutorial, will be referencing this repository on Github, animating a static source photo with a driving video.
Once you create a new notebook in Google Colab, go to Runtime > Change Runtime type and ensure GPU is selected. This is will defer all the computation for the deepfake to the GPU provided by Google.
Next, we’ll run each of the blocks of Python code below to get our notebook up and running. Simply copy and paste the blocks below and run them on Google Colab!
1. Clone the Repository and Install additional dependencies
!pip install ffmpy &> /dev/null
!git init -q .
!git remote add origin https://github.com/AliaksandrSiarohin/first-order-model
!git pull -q origin master
!git clone -q https://github.com/graphemecluster/first-order-model-demo demo
2. Import the required libraries
import os
import imageio
import PIL.Image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import skimage.transform.resize as ski_resize
from skimage import img_as_ubyte
from demo import load_checkpoints, make_animation
from ffmpy import FFmpeg
from IPython.display import HTML
from google.colab import drive
3. Download the Model Checkpoint(s)
Before we get any further, go ahead and download the tar files at this link and save them on your google drive. These are the checkpoint files that our AI model will use to actually create our deepfake. This way we can leverage pre-trained models rather than do our own training! For this guide, we’ll be using the checkpoint from the Vox Celeb dataset named vox-cpk.pth.tar.
ADDITIONAL: The link provided has several other checkpoint files for models trained with different datasets and slightly different purposes. Give them a shot for some different results!
4. Mount Your Google Drive Files and load the checkpoint.
To make your files in Google Drive accessible to your notebook, run the following line of code: drive.mount(‘/content/gdrive’)
. Upon execution, you should be provided a link for google authentication purposes. Go ahead and follow the instructions as required.
Afterward, run the commandls
and check that there is a folder listed called gdrive/
If you see it, your files are now available from within your notebook and you can execute the block below to load the checkpoint file.
generator, kp_detector = load_checkpoints(
config_path = 'config/vox- 256.yaml',
checkpoint_path = "/content/gdrive/My Drive/{PATH TO FILE}/vox-cpk.pth.tar")
When executing the block above, make sure to specify where exactly your checkpoint file is! Eg. If the file is stored in a folder called “deepfake-checkpoints”, your checkpoint_path would look like this:
".../My Drive/deepfake-checkpoints/vox-cpk.pth.tar"
ADDITIONAL: Google has also added mounting as an in-built feature within Colab. Expand the folder directory on the left and you should see a button with the google drive logo. Click on it to mount your Google Drive files.
The MAIN code (Python-pun-intended)
def resize(image, size=(256, 256)):
w, h = image.size
d = min(w, h)
r = ((w - d) // 2, (h - d) // 2, (w + d) // 2, (h + d) // 2)
return image.resize(size, resample=PIL.Image.LANCZOS, box=r)def get_image(filepath):
selected_image = resize(PIL.Image.open(filepath).convert("RGB"))
source_image = ski_resize(np.asarray(selected_image), (256, 256))
return source_imagedef get_video(filepath):
selected_video = filepath
reader = imageio.get_reader(
selected_video, mode='I', format='FFMPEG')
fps = reader.get_meta_data()['fps']
frames = []
for frame in reader:
frames.append(frame)
driving_video= [ski_resize(frame, (256, 256)) for frame in frames]
return driving_video, fpsdef display(source, driving, generated=None):
fig = plt.figure(figsize = (8+4*(generated is not None), 6))
ims = []
for i in range(len(driving)):
cols = [source]
cols.append(driving[i])
if generated is not None:
cols.append(generated[i])
im = plt.imshow(np.concatenate(cols, axis = 1),animated = True)
plt.axis("off")
ims.append([im])
ani = animation.ArtistAnimation(
fig, ims, interval = 50,repeat_delay=1000)
plt.close()
return ani
The above are the functions that will handle all of the work required to generate our deepfake. For those keen to know what’s going on, here’s a brief overview:
- Resize() & Get_image() → Process a provided source image and resize it to a 256 by 256 shape
- Get_video() → Retrieves a provided driving video and ensures it is also resized to a 256 by 256 shape
- Display() → Creates a simple HTML display of the source image, driving video, and the generated deepfake within your notebook
5. Choose your source image and driving video
NOTE: To create as convincing a deepfake as possible, the dimensions of our provided image and video are important. As much as possible, it is best to stick to media that is 256 x 256 pixels in size.
For creating a video of a specific size, I used Screencast-o-Matic with Zoom. It’s an intuitive screen recording tool that allows you to specify the size of the recording window.
6. Making the deepfake
if not os.path.isdir("output"): os.mkdir("output")source_image = get_image(
"gdrive/My Drive/{PATH TO YOUR SOURCE IMAGE}")driving_video,fps = get_video(
"gdrive/My Drive/{PATH TO YOUR DRIVING VIDEO}")predictions = make_animation(
source_image, driving_video, generator, kp_detector,relative=True)imageio.mimsave('output/Deepfake.mp4',
[img_as_ubyte(frame) for frame in predictions],fps = fps)HTML(display(source_image, driving_video, predictions).to_html5_video())
Provide the paths to your source_image & driving video respectively in the code above (I stored them within my Google Drive for convenience) and run the code block. Voila! You should have a deepfake called “Deepfake.mp4” in the output folder of your Colab Notebook. You can then go ahead and download the deepfake to your computer (Shown below).