3.5 C
New York
Saturday, January 4, 2025

R Deepfake Detection Problem



Introduction

Working with video datasets, significantly with respect to AI-based pretend object detection, could be very difficult as a consequence of correct body choice and face detection. To deal with this problem from R, you can also make use of the capabilities supplied by OpenCV, magickand keras.

Our method consists of the next consequent steps:

  • learn all of the movies
  • Seize and extract photographs from movies
  • detect faces from extracted photographs
  • reduce out faces
  • construct a picture classification mannequin with Keras

Let’s shortly introduce the non-deep studying libraries we’re utilizing. OpenCV is a pc imaginative and prescient library that features:

Then again, magick is the open supply picture processing library that may assist learn and extract helpful options from video datasets:

  • Learn video recordsdata
  • Extracts frames per second from video.
  • Lower out the faces from the pictures.

Earlier than going into an in depth rationalization, readers ought to know that there isn’t any want to repeat and paste code snippets. As a result of on the finish of the put up you will discover a hyperlink to Google Colab with GPU acceleration. This kernel permits everybody to run and reproduce the identical outcomes.

Information exploration

The info set What we will talk about is supplied by AWS, Fb, Microsoft, the Affiliation on AI’s Media Integrity Steering Committee, and varied lecturers.

Accommodates each actual and AI-generated pretend movies. The overall measurement is greater than 470 GB. Nevertheless, the 4 GB pattern knowledge set is obtainable individually.

The movies within the folders are within the format of mp4 and so they have varied lengths. Our process is to find out the variety of photographs to seize per second of a video. We often shoot 1 to three fps for every video.

Be aware: Set fps to NULL if you wish to extract all frames.

video = magick::image_read_video("aagfhgtpmv.mp4",fps = 2)
vid_1 = video((1))
vid_1 = magick::image_read(vid_1) %>% image_resize('1000x1000')

We solely noticed the primary body. What about the remainder of them?

Trying on the gif you possibly can see that some fakes are very simple to inform aside, however a small fraction look fairly life like. That is one other problem throughout knowledge preparation.

Face detection

At first, face areas ought to be decided utilizing bounding bins, utilizing OpenCV. Magic is then used to robotically extract them from all the pictures.

# get face location and calculate bounding field
library(opencv)
unconf <- ocv_read('frame_1.jpg')
faces <- ocv_face(unconf)
facemask <- ocv_facemask(unconf)
df = attr(facemask, 'faces')
rectX = (df$x - df$radius) 
rectY = (df$y - df$radius)
x = (df$x + df$radius) 
y = (df$y + df$radius)

# draw with crimson dashed line the field
imh  = image_draw(image_read('frame_1.jpg'))
rect(rectX, rectY, x, y, border = "crimson", 
     lty = "dashed", lwd = 2)
dev.off()

If face areas are discovered, it is rather simple to extract all of them.

edited = image_crop(imh, "49x49+66+34")
edited = image_crop(imh, paste(x-rectX+1,'x',x-rectX+1,'+',rectX, '+',rectY,sep = ''))
edited

deep studying mannequin

After making ready the dataset, it’s time to create a deep studying mannequin with Keras. We are able to shortly place all the pictures into folders and, utilizing picture turbines, feed faces to a pre-trained Keras mannequin.

train_dir = 'fakes_reals'
width = 150L
top = 150L
epochs = 10

train_datagen = image_data_generator(
  rescale = 1/255,
  rotation_range = 40,
  width_shift_range = 0.2,
  height_shift_range = 0.2,
  shear_range = 0.2,
  zoom_range = 0.2,
  horizontal_flip = TRUE,
  fill_mode = "nearest",
  validation_split=0.2
)


train_generator <- flow_images_from_directory(
  train_dir,                  
  train_datagen,             
  target_size = c(width,top), 
  batch_size = 10,
  class_mode = "binary"
)

# Construct the mannequin ---------------------------------------------------------

conv_base <- application_vgg16(
  weights = "imagenet",
  include_top = FALSE,
  input_shape = c(width, top, 3)
)

mannequin <- keras_model_sequential() %>% 
  conv_base %>% 
  layer_flatten() %>% 
  layer_dense(models = 256, activation = "relu") %>% 
  layer_dense(models = 1, activation = "sigmoid")

mannequin %>% compile(
  loss = "binary_crossentropy",
  optimizer = optimizer_rmsprop(lr = 2e-5),
  metrics = c("accuracy")
)

historical past <- mannequin %>% fit_generator(
  train_generator,
  steps_per_epoch = ceiling(train_generator$samples/train_generator$batch_size),
  epochs = 10
)

Play in a Laptop computer

Conclusion

This put up exhibits find out how to classify movies from R. The steps had been:

  • Learn movies and extract photographs from the info set.
  • Apply OpenCV to detect faces
  • Extract faces utilizing bounding bins
  • Construct a deep studying mannequin

Nevertheless, readers ought to be conscious that implementing the next steps can dramatically enhance mannequin efficiency:

  • extract all frames from video recordsdata
  • load completely different pretrained weights or use completely different pretrained fashions
  • use different expertise to detect faces (e.g. “MTCNN face detector”

Be happy to strive these choices within the Deepfake Detection Problem and share your leads to the feedback part!

Thanks for studying!

Corrections

For those who see errors or wish to counsel modifications, please create an issue within the supply repository.

Re-use

Textual content and figures are licensed beneath a Artistic Commons Attribution license. CC BY 4.0. The supply code is obtainable at https://github.com/henry090/Deepfake-from-Runtil in any other case indicated. Figures which were reused from different sources should not lined by this license and might be acknowledged by a observe of their caption: “Determine of…”.

Quotation

For attribution, please cite this work as

Abdullayev (2020, Aug. 18). Posit AI Weblog: Deepfake detection problem from R. Retrieved from https://blogs.rstudio.com/tensorflow/posts/2020-08-18-deepfake/

BibTeX Quotation

@misc{abdullayev2020deepfake,
  creator = {Abdullayev, Turgut},
  title = {Posit AI Weblog: Deepfake detection problem from R},
  url = {https://blogs.rstudio.com/tensorflow/posts/2020-08-18-deepfake/},
  yr = {2020}
}

Related Articles

Latest Articles