CoolFace
Modelpublic

mhamza-007/cvit_deepfake_detection

sourceHugging Facemitupdated 1y agoView on Hugging Face
1likes
Model Card

๐Ÿ” Convolutional Vision Transformer (CViT) for Deepfake Detection

The Convolutional Vision Transformer (CViT) is a hybrid architecture combining the powerful spatial feature extraction capabilities of CNNs with the long-range dependency modeling of Vision Transformers (ViT). This model is purpose-built for detecting deepfake videos and is trained on DFDC.


Model Architecture

1. Feature Learning (FL) Module - CNN Backbone

  • โ€”Composed of 17 convolutional operations.
  • โ€”Unlike traditional VGG architectures, FL focuses purely on feature extraction, not classification.
  • โ€”Accepts input of size 224 ร— 224 ร— 3 (RGB image).
  • โ€”Outputs a 512 ร— 7 ร— 7 feature map.
  • โ€”Contains 10.8 million learnable parameters.

2. Vision Transformer (ViT) Module

  • โ€”Receives CNN output (512 ร— 7 ร— 7) as its input.
  • โ€”Converts the 7ร—7 patches into a 1 ร— 1024 sequence using linear embedding.
  • โ€”Adds positional embeddings of shape (2 ร— 1024).
  • โ€”ViT Encoder uses:
  • โ€”Multi-Head Self Attention (MSA) with 8 attention heads.
  • โ€”MLP blocks with:
  • โ€”First linear layer of 2048 units.
  • โ€”Final linear layer of 2 units (binary classification: Fake / Real).
  • โ€”ReLU activation and Softmax for final probabilities.

๐Ÿงช Experimental Results

The CViT model was tested and evaluated across multiple deepfake datasets:

๐Ÿ“Š FaceForensics++ Accuracy

DatasetAccuracy
FaceForensics++ FaceSwap69%
FaceForensics++ DeepFakeDetection91%
FaceForensics++ Deepfake93%
FaceForensics++ FaceShifter46%
FaceForensics++ NeuralTextures60%
Note: Poor performance on the FaceShifter dataset is attributed to the model's difficulty in learning subtle visual artifacts.

๐Ÿงช DFDC Evaluation

ModelValidationTest
CViT87.25%91.5%
  • โ€”Unseen DFDC test videos: 400
  • โ€”Accuracy: 91.5%
  • โ€”AUC Score: 0.91

๐Ÿงช UADFV AUC Comparison

ModelValidationFaceSwapFace2Face
CViT93.75%69.69%69.39%

โš™๏ธ Training Configuration

  • โ€”Loss Function: Binary Cross Entropy (BCE)
  • โ€”Optimizer: Adam
  • โ€”Learning Rate: 1e-4
  • โ€”Weight Decay: 1e-6
  • โ€”Batch Size: 32
  • โ€”Epochs: 50
  • โ€”Learning Rate Scheduler: Reduces LR by factor of 0.1 every 15 epochs
  • โ€”Normalization:
  • โ€”Mean: [0.485, 0.456, 0.406]
  • โ€”Std: [0.229, 0.224, 0.225]

๐Ÿงช Inference Setup

  • โ€”Input: 30 normalized facial images (per video)
  • โ€”Classification:
  • โ€”Uses log loss function to compute confidence.
  • โ€”Output is a probability y โˆˆ [0, 1]
  • โ€”0 < y < 0.5: Real
  • โ€”0.5 โ‰ค y โ‰ค 1: Fake
  • โ€”Log loss penalizes:
  • โ€”Random guesses
  • โ€”Confident but incorrect predictions

๐Ÿ›  Inference Example

python
from huggingface_hub import hf_hub_download
import torch

# Download model
model_path = hf_hub_download(
    repo_id="mhamza-007/cvit_deepfake_detection",
    filename="cvit2_deepfake_detection_ep_50.pth"
)

# Load model (example)
model = torch.load(model_path, map_location='cpu')
model.eval()