Detect AI-generated and manipulated facial images (deepfakes) using deep learning. Built with TensorFlow/Keras, leveraging transfer learning (MobileNetV2), advanced augmentation, and robust evaluation metrics.
- Project Overview
- Project Structure
- Core AI Concepts
- Model Performance
- Results
- Setup & Run
- Future Work
- Acknowledgements
Deepfakes use generative models (GANs, diffusion models) to synthesize realistic human faces. This project uses CNNs and transfer learning to detect subtle artifacts in such images.
- Dataset: Uses Kaggle: deepfake-and-real-images
- Splitting: The notebook organizes images into
train
,val
, andtest
folders, each withFake
andReal
subfolders - Loading: Uses TensorFlow's
image_dataset_from_directory
with explicit class names - Augmentation: Applies random flips, rotations, and zooms for generalization
- Model: MobileNetV2 (transfer learning, head training, fine-tuning)
- Evaluation: Confusion matrix, ROC-AUC, classification report, precision-recall curve
Pipeline highlights:
- Modular codebase (data, models, training, evaluation)
- tf.data pipelines for efficient ingestion and augmentation
- Transfer learning with MobileNetV2
- Fine-tuning for domain adaptation
- Evaluation: confusion matrix, ROC-AUC, classification report
- Reproducible experiments: logging, checkpoints, plots
deepfake_detector/
│
├── src/
│ ├── data/
│ │ ├── split_dataset.py # Dataset splitting & preparation
│ │ └── dataloader.py # tf.data pipeline & augmentation
│ ├── models/
│ │ └── mobilenetv2.py # Model architecture (transfer learning)
│ ├── train.py # Training (head + fine-tuning)
│ ├── evaluate.py # Evaluation metrics & visualization
│ └── utils.py # Helper utilities (plots, checkpoints)
│
├── data/ # Place datasets here
├── outputs/ # Logs, checkpoints, plots
├── notebooks/ # Colab notebooks
├── requirements.txt
├── README.md
└── .gitignore
- CNNs pretrained on ImageNet capture low-level features (edges, textures, color blobs).
- These features are reused and adapted to deepfake classification.
- Training is done in two phases:
- Head training – freeze backbone, train dense layers
- Fine-tuning – unfreeze backbone with small learning rate for domain-specific adaptation
- Random flips, rotations, and zooms to prevent overfitting
- Augmentations simulate natural image variations and improve generalization
- Modular code with src/ structure (dataset, models, training, evaluation separated)
- tf.data pipelines with prefetching (AUTOTUNE) for GPU efficiency
- Logging, checkpoints, and plots for experiment tracking
- Accuracy is not sufficient in imbalanced datasets
- Metrics used:
- Confusion Matrix – per-class errors
- ROC Curve and AUC – threshold-independent separability
- Classification Report (precision, recall, F1)
- CNNs as feature extractors: local receptive fields, parameter sharing
- Fine-tuning as domain adaptation
- Deepfake detection as a distribution shift problem (detecting artifacts not seen in natural photos)
- Project design emphasizes explainability and scalability (future: ResNet, EfficientNet, Vision Transformers)
- Classification Metrics: Shows precision, recall, and F1-score for each class.
- ROC Curve: Area under curve (AUC = 0.977) shows excellent class separation.
- Confusion Matrix: Shows number of correct and incorrect predictions for each class.
- Training Accuracy: ~92%
- Validation Accuracy: ~90%
- ROC-AUC: ~0.95
- Classification Report:
- Precision, recall, F1-score for each class (see notebook output)
- Confusion Matrix:
- Shows per-class errors (see notebook heatmap)
- ROC Curve:
- Plots true positive rate vs. false positive rate
- Precision-Recall Curve:
- Plots precision vs. recall, with average precision (AP) score
All metrics and plots are generated using scikit-learn and matplotlib, as shown in the notebook's evaluation cells.
-
Install requirements:
pip install -r requirements.txt
-
Download and split dataset: - For Colab: Use the notebook cells to download from Kaggle and split into
train_data
,val_data
, andtest_data
folders withFake
andReal
subfolders. - For local: Runbash python -m src.data.split_dataset
-
Train model:
python src/train.py
-
Evaluate model:
python src/evaluate.py
Outputs are saved in:
outputs/
├── checkpoints/
├── logs/
└── plots/
- Extend to video deepfake detection (temporal inconsistencies)
- Experiment with Vision Transformers (ViT) for long-range feature capture
- Add explainability tools (Grad-CAM, saliency maps)
- Deploy as an API service with FastAPI or Flask
- Source code:
src/
- Example notebook:
notebooks/optimized_deepfake_detector_colab.ipynb
— shows real data workflow, splitting, augmentation, training, and evaluation - Configuration:
configs/train.yaml
- Dataset: Kaggle – Deepfake and Real Images
- Backbone: MobileNetV2 – Google AI Research
- Libraries: TensorFlow, scikit-learn, matplotlib, seaborn