Hello world, it's Siraj. And, there's some
serious hype lately about this AI algorithm
called DeepFakes, that's able to swap the face
of anyone in a video with anybody else.
Some of the example I've seen are amazingly
realistic. I'm gonna how this
algorithm works through theory and code
and then talk a bit about the moral
implications of this
at the end of the video. Last month a Redditor by the
name of DeepFakes, of course, was making a
name for himself by posting convincing
let's say, not safe for work videos that swapped
the face of the person in the original video
with a famous celebrity, like say, Taylor Swift.
He did this by training his deep learning algorithm on some
publicly available videos with just his
home computer, and in the past few weeks,
the practise of doing this has exploded
There's an entire GitHub and sub reddit
community dedicated to maintaining this,
ensuring the results of this algorithm. The subreddit
grew massively in size, and there's even this
desktop application called FakeApp, that lets anyone
recreate one of these videos with their own
dataset, no knowledge of programming necessary.
Because of this, people are putting Nicholas Cage's
face on literally everything
and making a meme out of it, because, it's the internet.
So, how does the deepfake algorithm work?
Let's say we got this video of Harrison Ford in
Indiana Jones and we wanna replace his face
with Nicholas Cage, because, why the hell not
Our first step is going to be to gather some training data.
specifically, images of both, Harrison Ford
and Nicholas Cage, to later train our
models on. Possible sources of these images
could include Google, DuckDuckGo or Bing image search.
Luckily, the face swap repository has
scripts to automatically download large amounts
of images from one of these sources to our
home directory. Once we got a couple hundred
images, we can place them each in their respective
folder. Notice, though, that these images
full of stuff not related to our characters.
There are all sorts of objects and distractions in the environments
of these images that have nothing to do
with our characters. We're going to want to
perform some face detection on each
of these images. Pretty much all cameras that have been
created in the past decade, have some sort of
real time face detection algorithm.
The open computer vision, or opencv library
will allow us to easily recognise faces in images
with just a function call. But the way it's doing this in the
background, is by using a method called histogram of
oriented gradients or hog for short
It starts by making our image black and white to simplify it.
We don't need color data to find faces.
Then it looks at every single pixel in the image on
one at a time. For every pixel, it looks at
the pixels directly surrounding it. The goal is to figure out
how dark the current pixel is compared
to the pixel directly surrounding it. Then,
it draws an arrow showing in which direction the image is
getting darker. Once it does this process for
every single pixel in the image, we'll end up with every
pixel being replaced by an arrow.
These arrows show the flow from light to dark
across the entire image. If we analyse
the pixels directly, then really dark images and
really light images of the same person would have totally
different pixel values. But because we're only considering
the direction that brightness changes,
both types of images end up with the same representation.
making the problem easier to solve.
Saving all these directions is too space intesive.
So we break the image into smaller squares.