A fully offline, explainable deepfake detection system built on EfficientNet-B0 with Grad-CAM visual explanations, a polished Streamlit UI, and a FastAPI backend for real-time inference.
Deepfake technology uses generative AI to create highly realistic synthetic faces in images and videos. While powerful, it poses serious risks โ misinformation, identity fraud, impersonation, and reputational harm.
DeepShield addresses this with a privacy-first, fully offline detection pipeline that:
- Classifies images and videos as โ Real or ๐จ Fake
- Provides confidence scores and P(Real) / P(Fake) probabilities
- Explains decisions visually using Grad-CAM heatmaps
- Runs entirely on your machine โ no cloud calls, no data leaves your device
https://drive.google.com/drive/folders/1kyWpFCtVWmF7qZGzEp4oOrDCnI8K6OAN?role=writer
โโโโโโโโโโโโโโโโโโโโโโโ
โ Input (Image / โ
โ Video / Webcam) โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโ
โ Face Detection โ โ OpenCV Haar Cascade (face_detector.py)
โ & Frame Sampling โ โ Frame extractor (frame_extractor.py)
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโ
โ Image Preprocessingโ โ Resize 224ร224, ImageNet normalize
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DeepfakeCNN Model โ
โ โ
โ EfficientNet-B0 (Spatial Branch) โ โ 1280-dim features
โ + โ
โ FrequencyBranch (FFT Spectrum) โ โ 128-dim features [opt-in]
โ โ
โ Fused โ Linear head โ Binary logit โ
โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโ
โ Classification โ Real / Fake + confidence score
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโ
โ Grad-CAM Module โ Visual heatmap over suspicious regions
โโโโโโโโโโโโโโโโโโโโโโโ
| Feature | Details |
|---|---|
| EfficientNet-B0 backbone | ImageNet-pretrained, two-phase fine-tuning |
| Frequency-domain analysis | Optional FFT branch detects GAN grid artefacts |
| Face detection | OpenCV Haar cascade โ crops to face before inference |
| Grad-CAM explanations | Heatmap overlay showing which regions drove the decision |
| Full video analysis | Samples N frames evenly, aggregates with majority vote + timeline chart |
| Live webcam | streamlit-webrtc in the UI + CLI realtime script |
| FastAPI backend | REST + WebSocket endpoints for image, video, and frame streaming |
| Fully offline | No internet connection required for inference |
| MPS / CUDA / CPU | Auto-detects Apple Silicon, NVIDIA GPU, or CPU |
DeepShield/
โ
โโโ api/ โ FastAPI backend
โ โโโ main.py โ App entry point, model loaded at startup
โ โโโ schemas.py โ Pydantic response models
โ โโโ routes/
โ โโโ predict.py โ POST /predict/image, POST /predict/video
โ โโโ stream.py โ WS /ws/webcam (real-time frame inference)
โ
โโโ model/
โ โโโ cnn_model.py โ DeepfakeCNN (EfficientNet-B0 + optional FrequencyBranch)
โ โโโ frequency_branch.py โ FFT-based spectral feature extractor
โ โโโ loss.py
โ
โโโ inference/
โ โโโ predict.py โ load_model, predict, predict_image, predict_video, predict_with_gradcam
โ โโโ realtime_inference.py โ CLI webcam / video loop with frame skipping
โ
โโโ training/
โ โโโ train.py โ Two-phase EfficientNet fine-tuning
โ โโโ evaluate.py โ Test-set evaluation with tqdm progress
โ โโโ dataset.py โ DataLoader, balanced subset sampling
โ โโโ metrics.py โ Accuracy, precision, recall, F1, confusion matrix
โ โโโ early_stopping.py
โ
โโโ preprocessing/
โ โโโ face_detector.py โ detect_and_crop_face() using OpenCV Haar cascade
โ โโโ frame_extractor.py โ Extract 30 frames/video with multiprocessing
โ โโโ dataset_split.py โ Sort raw videos โ real/ fake/ using metadata.json
โ โโโ split_train_val_test.py โ 70/15/15 split grouped by video ID
โ โโโ augmentations.py
โ
โโโ explainability/
โ โโโ gradcam.py โ Grad-CAM with forward + backward hooks
โ โโโ heatmap_utils.py โ Heatmap colormap overlay
โ
โโโ saved_models/
โ โโโ best_model.pth โ Best checkpoint saved during training
โ
โโโ app.py โ Streamlit UI (Image / Video / Webcam tabs)
โโโ requirements.txt
โโโ README.md
| Category | Tools |
|---|---|
| Deep Learning | PyTorch, TorchVision |
| Model | EfficientNet-B0 (ImageNet pretrained) |
| Computer Vision | OpenCV |
| Frequency Analysis | PyTorch FFT (torch.fft.fft2, fftshift) |
| Explainability | Grad-CAM (backward hooks) |
| Frontend | Streamlit, streamlit-webrtc, Plotly |
| Backend API | FastAPI, Uvicorn, WebSockets |
| Data / Metrics | NumPy, Pandas, Scikit-learn |
| Training utilities | tqdm, early stopping |
Trained on the 140k Real vs Fake Faces dataset (Kaggle):
| Metric | Score |
|---|---|
| Accuracy | ~91โ92% |
| Precision | โ |
| Recall | โ |
| F1 Score | โ |
Run
python -m training.evaluateafter training to get exact numbers on your test split.
This project uses the 140k Real vs Fake Faces dataset from Kaggle.
Download link: https://www.kaggle.com/datasets/xhlulu/140k-real-and-fake-faces
After downloading, place it at the project root:
DeepShield/
โโโ 140k-faces/
โโโ real_vs_fake/
โโโ real-vs-fake/
โโโ train/
โ โโโ real/
โ โโโ fake/
โโโ valid/
โ โโโ real/
โ โโโ fake/
โโโ test/
โโโ real/
โโโ fake/
The
140k-faces/folder is in.gitignoreand must be placed manually on each machine.
brew install xz cmake libomppyenv install 3.12.2
pyenv local 3.12.2python3 -m venv venv
source venv/bin/activate # macOS / Linux
# venv\Scripts\activate # Windowspip install -r requirements.txtpython -m training.trainTrains DeepfakeCNN using two-phase EfficientNet fine-tuning for up to 50 epochs. Best checkpoint is saved to saved_models/best_model.pth whenever validation accuracy improves.
Training phases:
- Phase 1 (epochs 1โ5): Backbone frozen, only the classifier head trains at lr=1e-4
- Phase 2 (epoch 6+): Last two EfficientNet blocks unfrozen, full model trains at lr=1e-5
Sample output:
Epoch 1/50 [Ph1] train_loss=0.512 val_loss=0.431 val_acc=0.8120
...
Epoch 20/50 [Ph2] train_loss=0.214 val_loss=0.198 val_acc=0.9167
python -m training.evaluateLoads saved_models/best_model.pth and reports Accuracy, Precision, Recall, F1, and Confusion Matrix on the held-out test set. Includes a tqdm progress bar.
Sample output:
Evaluating: 100%|โโโโโโโโโโโโ| 625/625 [05:23<00:00]
Test set evaluation
----------------------------------------
Accuracy: 0.9167
Precision: 0.9210
Recall: 0.9140
F1: 0.9175
streamlit run app.pyOpens the full UI at http://localhost:8501. Three tabs:
- Upload any face image (JPG/PNG)
- Shows verdict card with confidence %, P(Real), P(Fake)
- Enable Grad-CAM in sidebar to see which facial regions influenced the decision
- Plotly donut chart shows Real/Fake probability split
- Upload a video (MP4/AVI/MOV)
- Choose how many frames to analyze (4โ32)
- Summary metrics: frames analyzed, avg P(Real), real/fake frame counts
- Interactive P(Real) timeline chart (per-frame line chart with 0.5 threshold)
- Frame distribution histogram showing score spread
- Collapsible per-frame detail table
- Live webcam feed via
streamlit-webrtc - Inference every 3rd frame to keep stream smooth
- Bottom banner shows Real/Fake label + confidence
- Top bar shows P(Real) as a fill indicator
- Falls back gracefully if
streamlit-webrtcis not installed
Sidebar options:
- Toggle Grad-CAM overlay
- Score interpretation table (what P(Real) ranges mean)
uvicorn api.main:app --reload --host 0.0.0.0 --port 8000Model is loaded once at startup and reused for all requests.
Interactive API docs: http://localhost:8000/docs
| Endpoint | Method | Description |
|---|---|---|
/health |
GET | Check if model is loaded and which device is in use |
/predict/image |
POST | Upload image โ {label, confidence, prob_real} |
/predict/video |
POST | Upload video โ aggregated + per-frame results |
/ws/webcam |
WebSocket | Send JPEG bytes โ receive JSON predictions in real-time |
Example request (image):
curl -X POST http://localhost:8000/predict/image \
-F "file=@face.jpg"Example response:
{
"label": "Fake",
"confidence": 0.9312,
"prob_real": 0.0688
}# Webcam
python -m inference.realtime_inference
# Video file
python -m inference.realtime_inference --video path/to/video.mp4
# With Grad-CAM overlay
python -m inference.realtime_inference --video path/to/video.mp4 --gradcamPress Q to quit. Inference runs every 3rd frame for smooth display.
# โโ Environment โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
python3 -m venv venv && source venv/bin/activate
pip install -r requirements.txt
# โโ Train โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
python -m training.train
# โโ Evaluate โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
python -m training.evaluate
# โโ Streamlit UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
streamlit run app.py
# โโ FastAPI backend โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
uvicorn api.main:app --reload --port 8000
# โโ CLI webcam / video โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
python -m inference.realtime_inference [--video <path>] [--gradcam]EfficientNet-B0 (pretrained on ImageNet)
โโโ features[0..8] (MBConv blocks)
โโโ classifier
โโโ Dropout(0.4)
โโโ Linear(1280 โ 1) # Default mode
Optional: use_frequency=True
EfficientNet features (1280-dim)
+ FrequencyBranch (128-dim)
โ Linear(1408 โ 256) โ ReLU โ Dropout(0.4) โ Linear(256 โ 1)
Detects spectral artefacts characteristic of GAN-generated images:
torch.fft.fft2โ 2D Fast Fourier Transformfftshiftโ centres low-frequency content for spatially-consistent conv filterslog1pโ compresses extreme dynamic range of FFT magnitudes- Two Conv2D + BatchNorm + MaxPool blocks
- Fully connected โ 128-dim feature vector
| Phase | Epochs | LR | Backbone |
|---|---|---|---|
| Phase 1 (warm-up) | 1โ5 | 1e-4 | Fully frozen |
| Phase 2 (fine-tune) | 6+ | 1e-5 | Last 2 blocks unfrozen |
| Metric | Description |
|---|---|
| Accuracy | Overall correct classifications |
| Precision | Of predicted fakes, how many were actually fake |
| Recall | Of actual fakes, how many were caught |
| F1 Score | Harmonic mean of precision and recall |
| Confusion Matrix | True/False Positive/Negative breakdown |
- Social media content verification
- News authenticity validation
- Digital identity protection
- Cybercrime and fraud detection
- Media forensics and journalism
- Temporal modeling with 3D CNN or Vision Transformer across video frames
- Audio-visual consistency check (voice + face sync)
- Browser extension for in-page detection
- Mobile deployment (CoreML / TFLite)
- Confidence calibration and uncertainty estimation
This project is released under the MIT License.