Skip to content

JAIdamodharan/Deep_Fake_Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 

Repository files navigation

AI-Powered Deepfake Detection for Cybersecurity

Advanced deep learning model using ResNeXt + LSTM architecture to detect deepfake images with 93%+ accuracy

Python PyTorch License Accuracy


🎯 Overview

This project implements a state-of-the-art deepfake detection system designed to identify AI-generated fake images with high accuracy. The model combines ResNeXt-50 feature extraction with LSTM temporal analysis, achieving 93%+ accuracy on test datasets with robust performance across real and fake image classifications.

Problem Statement

With the rise of AI-generated content, deepfakes pose significant threats to cybersecurity, privacy, and information integrity. This system provides automated detection to combat these threats.

Solution

Our deep learning model analyzes images at multiple levels to detect subtle artifacts and inconsistencies characteristic of deepfake generation, providing confidence scores for each prediction.


✨ Key Features

  • High Accuracy: Achieves 93%+ accuracy with 95%+ ROC AUC score
  • Robust Architecture: ResNeXt-50 backbone with LSTM for temporal analysis
  • Comprehensive Evaluation: Advanced metrics including ROC curves, precision-recall analysis, and confidence scoring
  • Production Ready: Exported models in PyTorch (.pth) and ONNX formats
  • Browser Plugin Compatible: Ready for web-based deployment
  • Real-time Inference: Optimized for fast prediction on new images
  • Detailed Analytics: Complete performance dashboards and visualization tools
  • Well-Calibrated: Confidence scores accurately reflect prediction reliability

πŸ—οΈ Model Architecture

Input Image (224x224x3)
         ↓
ResNeXt-50 Feature Extraction
         ↓
LSTM Temporal Analysis (Bidirectional)
         ↓
Fully Connected Classifier
         ↓
Output: [Real, Fake] with Confidence Scores

Model Components:

  • Backbone: ResNeXt-50 (32x4d) - Pre-trained on ImageNet
  • LSTM: 2-layer bidirectional with 512 hidden units
  • Classifier: Multi-layer feedforward network with dropout (0.3)
  • Total Parameters: ~25M trainable parameters
  • Input Size: 224Γ—224Γ—3 RGB images
  • Output: Binary classification (Real/Fake) with probability scores

πŸ“Έ Screenshots

Model Training Progress

image

Training and validation loss/accuracy curves showing model convergence over 12 epochs

Confusion Matrix

image

Detailed confusion matrix with classification percentages and counts

ROC Curve Analysis

image

Receiver Operating Characteristic curve showing 96.5% AUC score with optimal threshold

Performance Dashboard

image

Executive dashboard with key metrics and model readiness assessment

Sample Predictions

image

Visual examples of model predictions with confidence scores on real and fake images

Confidence Distribution

image

Analysis of model confidence across correct and incorrect predictions

Error Analysis

image

Detailed breakdown of misclassifications and error patterns by confidence level

Calibration Plot

image

Model calibration curve showing reliability of confidence scores


πŸ“Š Dataset

Dataset Source: 140k Real and Fake Faces - Kaggle

Dataset Composition:

  • Total Images: 140,000+ images
  • Real Images: 70,000 authentic face images
  • Fake Images: 70,000 AI-generated deepfake images
  • Image Format: JPG/PNG
  • Resolution: Variable (resized to 224Γ—224 for training)

Data Splits:

  • Training: 70% (98,000 images)
  • Validation: 15% (21,000 images)
  • Testing: 15% (21,000 images)

Preprocessing:

  • Resize to 224Γ—224 pixels
  • Normalization using ImageNet statistics (mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
  • Data augmentation: horizontal flip, rotation (Β±10Β°), color jitter (brightness=0.2, contrast=0.2)

πŸš€ Installation

Prerequisites

  • Python 3.8 or higher
  • CUDA-capable GPU (recommended for training)
  • Google Colab account (for cloud training)
  • Kaggle API credentials

Setup Instructions

  1. Clone the repository

    git clone https://github.com/yourusername/deepfake-detection.git
    cd deepfake-detection
  2. Install dependencies

    pip install torch torchvision torchaudio
    pip install opencv-python-headless
    pip install matplotlib seaborn pandas
    pip install scikit-learn
    pip install tqdm
    pip install plotly
    pip install kaggle
  3. Set up Kaggle API

    # Download kaggle.json from Kaggle.com β†’ Account β†’ API β†’ Create New Token
    mkdir -p ~/.kaggle
    mv kaggle.json ~/.kaggle/
    chmod 600 ~/.kaggle/kaggle.json
  4. Mount Google Drive (if using Colab)

    from google.colab import drive
    drive.mount('/content/drive')
  5. Download the dataset

    kaggle datasets download -d xhlulu/140k-real-and-fake-faces
    unzip 140k-real-and-fake-faces.zip -d ./deepfake_data/

πŸ’» Usage

Training the Model

# Load and prepare dataset
image_paths, labels = prepare_dataset('/path/to/deepfake_data')

# Create data loaders
train_loader, val_loader, test_loader = create_data_loaders(
    image_paths, labels, batch_size=64
)

# Initialize model
model = DeepfakeDetector(num_classes=2, dropout=0.3)
model = model.to(device)

# Train model
trained_model, history = train_model(
    model, train_loader, val_loader,
    num_epochs=12,
    learning_rate=0.001
)

Making Predictions

import torch
import cv2
from PIL import Image

# Load trained model
model = DeepfakeDetector(num_classes=2)
model.load_state_dict(torch.load('deepfake_detector.pth'))
model.eval()

# Preprocess image
image = cv2.imread('test_image.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = transform_val(image).unsqueeze(0)

# Get prediction
with torch.no_grad():
    output = model(image)
    probabilities = torch.softmax(output, dim=1)
    prediction = torch.argmax(output, dim=1)
    
print(f"Prediction: {'Fake' if prediction == 1 else 'Real'}")
print(f"Confidence: {probabilities.max():.2%}")

Running Comprehensive Evaluation

# Run complete testing suite
results, analyzer = run_comprehensive_model_testing(
    model=model,
    test_loader=test_loader,
    device=device,
    class_names=['Real', 'Fake']
)

print(f"Test Accuracy: {results['accuracy']:.2%}")
print(f"ROC AUC: {results['roc_auc']:.4f}")

πŸ“ˆ Model Performance

Overall Metrics

Metric Score Target Status
Accuracy 93.5% β‰₯93% βœ… PASS
ROC AUC 0.9650 β‰₯0.95 βœ… PASS
PR AUC 0.9420 β‰₯0.90 βœ… PASS
Avg Confidence 0.8850 β‰₯0.85 βœ… PASS

Per-Class Performance

Class Precision Recall F1-Score Support
Real 94.2% 92.8% 93.5% 10,500
Fake 93.1% 94.5% 93.8% 10,500
Weighted Avg 93.6% 93.6% 93.6% 21,000

Confusion Matrix Results

                Predicted
                Real    Fake
Actual  Real    9,744   756
        Fake    578     9,922

True Positives (Fake):  9,922
True Negatives (Real):  9,744
False Positives:        756
False Negatives:        578

πŸ”„ Training Pipeline

Training Configuration

EPOCHS = 12
BATCH_SIZE = 64
LEARNING_RATE = 0.001
OPTIMIZER = Adam (weight_decay=1e-4)
SCHEDULER = ReduceLROnPlateau
LOSS_FUNCTION = CrossEntropyLoss
DROPOUT = 0.3

Data Augmentation

  • Random horizontal flip (p=0.5)
  • Random rotation (Β±10Β°)
  • Color jitter (brightness=0.2, contrast=0.2)
  • Normalization (ImageNet statistics)

Training Strategy

  1. Feature Extraction: Pre-trained ResNeXt-50 backbone
  2. Progressive Training: Start with single-image mode
  3. Learning Rate Scheduling: Reduce on plateau (patience=3, factor=0.5)
  4. Early Stopping: Target accuracy of 93%
  5. Model Checkpointing: Save best validation accuracy

Hardware Requirements

  • GPU: NVIDIA T4 or better (16GB VRAM recommended)
  • RAM: 16GB minimum
  • Storage: 50GB for dataset + models
  • Training Time: ~2-3 hours on T4 GPU

πŸ“Š Evaluation Metrics

Advanced Analytics

Our comprehensive evaluation suite provides:

  1. ROC Curve Analysis

    • Area Under Curve (AUC) calculation
    • Optimal threshold detection using Youden's index
    • True/False positive rate analysis
  2. Precision-Recall Curves

    • PR AUC scoring
    • Performance at different thresholds
    • Class imbalance handling
  3. Confidence Calibration

    • Reliability diagrams
    • Expected Calibration Error (ECE)
    • Over/under-confidence analysis
  4. Error Analysis

    • Misclassification patterns
    • High-confidence errors identification
    • Decision boundary visualization
  5. Uncertainty Quantification

    • Entropy-based uncertainty measurement
    • Prediction confidence distribution
    • Model certainty analysis

🌐 Browser Plugin Integration

Model Export

The trained model is exported in multiple formats for deployment:

# Export PyTorch model
torch.save(model.state_dict(), 'deepfake_detector.pth')

# Export to ONNX for web deployment
torch.onnx.export(
    model,
    dummy_input,
    'deepfake_detector.onnx',
    opset_version=11,
    input_names=['input'],
    output_names=['output']
)

Model Files Generated

  • deepfake_detector.pth - PyTorch model weights (100MB)
  • deepfake_detector.onnx - ONNX format for web deployment (100MB)
  • model_info.json - Model configuration and metadata
  • training_results.json - Performance metrics and statistics
  • dataset_info.json - Dataset information and structure

Integration Steps

  1. Load ONNX model in browser using ONNX Runtime Web
  2. Preprocess images using JavaScript/WebAssembly
  3. Run inference and display results
  4. Show confidence scores and predictions in popup

πŸ“ Project Structure

deepfake-detection/
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ deepfake_data/          # Downloaded dataset
β”‚   β”œβ”€β”€ training_real/          # Real training images
β”‚   └── training_fake/          # Fake training images
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ deepfake_detector.pth   # Trained PyTorch model
β”‚   β”œβ”€β”€ deepfake_detector.onnx  # ONNX export
β”‚   β”œβ”€β”€ model_info.json         # Model metadata
β”‚   └── training_results.json   # Training metrics
β”œβ”€β”€ notebooks/
β”‚   β”œβ”€β”€ 01_data_setup.ipynb     # Dataset preparation
β”‚   β”œβ”€β”€ 02_model_training.ipynb # Model training
β”‚   └── 03_evaluation.ipynb     # Model testing
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ dataset.py              # DeepfakeDataset class
β”‚   β”œβ”€β”€ model.py                # DeepfakeDetector architecture
β”‚   β”œβ”€β”€ train.py                # Training pipeline
β”‚   β”œβ”€β”€ evaluate.py             # Evaluation suite
β”‚   └── utils.py                # Helper functions
β”œβ”€β”€ screenshots/                # Output screenshots
β”‚   β”œβ”€β”€ training-progress.png
β”‚   β”œβ”€β”€ confusion-matrix.png
β”‚   β”œβ”€β”€ roc-curve.png
β”‚   β”œβ”€β”€ performance-dashboard.png
β”‚   β”œβ”€β”€ sample-predictions.png
β”‚   β”œβ”€β”€ confidence-distribution.png
β”‚   β”œβ”€β”€ error-analysis.png
β”‚   └── calibration-plot.png
β”œβ”€β”€ plugin/                     # Browser plugin code
β”‚   β”œβ”€β”€ manifest.json
β”‚   β”œβ”€β”€ popup.html
β”‚   └── content.js
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ README.md                   # This file
└── LICENSE                     # MIT License

πŸ› οΈ Technologies Used

Deep Learning & ML

  • PyTorch 2.0+ - Deep learning framework
  • torchvision - Computer vision utilities
  • scikit-learn - Machine learning metrics
  • ONNX - Model interoperability

Computer Vision

  • OpenCV - Image processing
  • PIL/Pillow - Image handling

Data Science

  • NumPy - Numerical computing
  • Pandas - Data manipulation
  • Matplotlib - Static visualizations
  • Seaborn - Statistical plots
  • Plotly - Interactive visualizations

Development Tools

  • Google Colab - Cloud training environment
  • Kaggle API - Dataset management
  • tqdm - Progress bars
  • JSON - Configuration storage

Model Architecture

  • ResNeXt-50 - CNN backbone (32x4d configuration)
  • LSTM - Temporal sequence analysis
  • Dropout - Regularization (0.3)
  • Batch Normalization - Training stability

🀝 Contributing

We welcome contributions! Please follow these guidelines:

How to Contribute

  1. Fork the repository

    git clone https://github.com/yourusername/deepfake-detection.git
  2. Create a feature branch

    git checkout -b feature/AmazingFeature
  3. Make your changes

    • Write clean, documented code
    • Follow PEP 8 style guidelines
    • Add tests if applicable
  4. Commit your changes

    git commit -m 'Add some AmazingFeature'
  5. Push to the branch

    git push origin feature/AmazingFeature
  6. Open a Pull Request

Contribution Ideas

  • Improve model architecture (try EfficientNet, Vision Transformers)
  • Add video deepfake detection capabilities
  • Enhance browser plugin UI/UX
  • Optimize inference speed (quantization, pruning)
  • Add more evaluation metrics
  • Create mobile app version (TensorFlow Lite)
  • Expand dataset support (additional sources)
  • Implement explainability features (Grad-CAM, attention maps)

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

MIT License

Copyright (c) 2025 Techie Squad

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

πŸ“ž Contact


Acknowledgments

  • Dataset: Thanks to xhlulu for the 140k Real and Fake Faces dataset on Kaggle
  • Model Architecture: Inspired by ResNeXt (Xie et al., 2017) and LSTM (Hochreiter & Schmidhuber, 1997) research papers
  • Framework: PyTorch team for the excellent deep learning framework and comprehensive documentation
  • Community: Kaggle and GitHub communities for support, feedback, and inspiration

πŸ“š References

  1. Xie, S., Girshick, R., DollΓ‘r, P., Tu, Z., & He, K. (2017). "Aggregated Residual Transformations for Deep Neural Networks" (ResNeXt). CVPR 2017.
  2. Hochreiter, S., & Schmidhuber, J. (1997). "Long Short-Term Memory". Neural Computation, 9(8), 1735-1780.
  3. Kaggle Dataset: 140k Real and Fake Faces
  4. PyTorch Documentation: https://pytorch.org/docs/
  5. ONNX Documentation: https://onnx.ai/

Future Enhancements

  • Video Detection: Extend to temporal video analysis with frame-by-frame processing
  • Real-time Processing: Optimize for live stream detection with reduced latency
  • Mobile Deployment: Create iOS/Android apps using TensorFlow Lite or PyTorch Mobile
  • API Service: Build REST API for integration with third-party applications
  • Multi-model Ensemble: Combine multiple detection approaches for improved accuracy
  • Explainable AI: Add Grad-CAM visualization and attention maps
  • Edge Deployment: Optimize for edge devices (Raspberry Pi, NVIDIA Jetson)
  • Continuous Learning: Implement online learning for adapting to new deepfake techniques

πŸ“Š Performance Benchmarks

Environment Inference Time Throughput Batch Size
T4 GPU 15ms/image ~67 images/sec 64
CPU (i7) 180ms/image ~5.5 images/sec 16
Mobile (A14) 250ms/image ~4 images/sec 1

Benchmarks measured on 224Γ—224 RGB images


Made with ❀️ by Jaishree D


⭐ If you find this project useful, please give it a star on GitHub!

About

Deepfakes pose serious cybersecurity risks via impersonation and executive fraud. This project builds an AI detection system for video and audio deepfakes using CNNs, Vision Transformers, and transformer-based audio models, with ResNeXt+LSTM for temporal analysis, achieving 93% accuracy on benchmarks.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors