How to make Deepfakes for free!

Tack that pretty face of yours onto your favorite celebrity, with Google Colab.

Arnav Shirodkar
CodeX
7 min readAug 8, 2021

--

Image by ComFreak from Pixabay [Link]

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.

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.

Video from Guardian [Link]

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:

  1. 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?)
  2. 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.
Image from GIPHY [Link]

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.

Developed by Aliaksandr Siarohin, Stéphane Lathuilière, Sergey Tulyakov, Elisa Ricci and Nicu Sebe [Link]

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.

Make sure the runtime is set to GPU

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

2. Import the required libraries

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.

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.

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"

The MAIN code (Python-pun-intended)

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

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).

Download your generated deepfake

References:

--

--

Arnav Shirodkar
CodeX

Freelance Developer💻 and Percussionist 🥁. Music-Tech and Machine Learning Enthusiast. Undergraduate at Bard College, NY. Learn new stuff with me!