honi05 commited on
Commit
a64cf6f
Β·
verified Β·
1 Parent(s): dc2d40d

Add model card with tags, metrics, usage examples and ablation results

Browse files
Files changed (1) hide show
  1. README.md +265 -0
README.md ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ license: mit
5
+ library_name: pytorch
6
+ pipeline_tag: image-classification
7
+ tags:
8
+ - deepfake-detection
9
+ - deepfake
10
+ - media-forensics
11
+ - video-forensics
12
+ - face-detection
13
+ - computer-vision
14
+ - image-classification
15
+ - video-classification
16
+ - efficientnet
17
+ - grad-cam
18
+ - explainable-ai
19
+ - xai
20
+ - pytorch
21
+ - gradio
22
+ - celeb-df
23
+ datasets:
24
+ - celeb-df-v2
25
+ metrics:
26
+ - accuracy
27
+ - f1
28
+ - roc_auc
29
+ model-index:
30
+ - name: EfficientNet-B4 Deepfake Detector
31
+ results:
32
+ - task:
33
+ type: image-classification
34
+ name: Deepfake Detection (frame-level)
35
+ dataset:
36
+ name: Celeb-DF v2
37
+ type: celeb-df-v2
38
+ metrics:
39
+ - type: roc_auc
40
+ value: 0.9933
41
+ name: Frame-Level AUC-ROC
42
+ - type: accuracy
43
+ value: 0.9746
44
+ name: Frame Accuracy
45
+ - type: f1
46
+ value: 0.9855
47
+ name: Frame F1 Score
48
+ - task:
49
+ type: video-classification
50
+ name: Deepfake Detection (video-level)
51
+ dataset:
52
+ name: Celeb-DF v2
53
+ type: celeb-df-v2
54
+ metrics:
55
+ - type: roc_auc
56
+ value: 0.9990
57
+ name: Video-Level AUC-ROC
58
+ ---
59
+
60
+ # EfficientNet-B4 Deepfake Detector with Grad-CAM Explainability
61
+
62
+ A high-accuracy deepfake face detector trained on **Celeb-DF v2**, combining an EfficientNet-B4 backbone with Grad-CAM spatial attribution and a deterministic forensic report generator. The model classifies face images as real or fake and highlights *which facial region* triggered the decision.
63
+
64
+ **Bachelor project β€” Sapienza UniversitΓ  di Roma, AI & Applied Computer Science**
65
+
66
+ ---
67
+
68
+ ## Model Performance
69
+
70
+ | Metric | Score |
71
+ |---|---|
72
+ | Frame-Level AUC-ROC | **0.9933** |
73
+ | Video-Level AUC-ROC | **0.9990** |
74
+ | Frame Accuracy | **97.46%** |
75
+ | Frame F1 Score | **98.55%** |
76
+ | False Negative Rate | **0.44%** (37 / 8,475 fakes missed) |
77
+
78
+ > Video-level scores are computed by mean-aggregating frame probabilities per video ID, which suppresses single-frame noise and reflects real-world deployment.
79
+
80
+ ---
81
+
82
+ ## What Makes This Different
83
+
84
+ - **Explainable predictions** β€” Grad-CAM heatmaps highlight the exact facial zone (forehead, eyes, nose, jaw, or hairline) that triggered the detection.
85
+ - **Forensic text output** β€” A template engine converts confidence + activated zones into a structured human-readable forensic report (4 confidence tiers).
86
+ - **Video-level reasoning** β€” Frame scores are aggregated per video for a single robust verdict.
87
+ - **Interactive demo** β€” Gradio app supports both image and video input.
88
+
89
+ ---
90
+
91
+ ## Architecture
92
+
93
+ ```
94
+ Input (224Γ—224 face crop)
95
+ └─ EfficientNet-B4 backbone (ImageNet pretrained)
96
+ β”œβ”€ Blocks 0–4 β†’ frozen (feature extraction)
97
+ └─ Blocks 5–8 β†’ fine-tuned (LR = 1e-4)
98
+ └─ Global Average Pooling
99
+ └─ Dropout(0.4) β†’ Linear(1792β†’256) β†’ ReLU β†’ Dropout(0.2) β†’ Linear(256β†’1)
100
+ └─ Sigmoid β†’ probability [0, 1] (β‰₯ 0.5 = Fake)
101
+ ```
102
+
103
+ - **Loss:** Focal Loss (Ξ±=0.25, Ξ³=2.0) β€” handles the 5:1 fake/real imbalance
104
+ - **Optimizer:** AdamW with differential learning rates (backbone 1e-4, head 5e-4)
105
+ - **Scheduler:** CosineAnnealingLR over 20 epochs with early stopping (patience=5)
106
+ - **GPU:** NVIDIA RTX A4000
107
+
108
+ ---
109
+
110
+ ## Dataset
111
+
112
+ **Celeb-DF v2** β€” 590 real celebrity videos + 5,639 high-quality deepfake videos.
113
+
114
+ - 15 frames extracted per video (uniform temporal sampling)
115
+ - MTCNN face detection β†’ 224Γ—224 crops, 20 px margin
116
+ - Split **by video ID** (80/10/10) β€” prevents identity leakage between train and test
117
+ - ~74,000 real face crops Β· ~477,000 fake face crops
118
+
119
+ ---
120
+
121
+ ## Usage
122
+
123
+ ### Quick inference (image)
124
+
125
+ ```python
126
+ import torch
127
+ from torchvision import transforms
128
+ from PIL import Image
129
+ from huggingface_hub import hf_hub_download
130
+
131
+ # Download checkpoint
132
+ ckpt_path = hf_hub_download(repo_id="honi05/deepfake-detection", filename="best_model.pt")
133
+
134
+ # Load model
135
+ from src.model import DeepfakeClassifier
136
+ model = DeepfakeClassifier(freeze_blocks=5, dropout=0.4, backbone='b4')
137
+ state = torch.load(ckpt_path, map_location="cpu", weights_only=True)
138
+ model.load_state_dict(state["model_state_dict"])
139
+ model.eval()
140
+
141
+ # Preprocess
142
+ transform = transforms.Compose([
143
+ transforms.Resize((224, 224)),
144
+ transforms.ToTensor(),
145
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
146
+ ])
147
+
148
+ img = Image.open("face.jpg").convert("RGB")
149
+ x = transform(img).unsqueeze(0)
150
+
151
+ with torch.no_grad():
152
+ logit = model(x)
153
+ prob = torch.sigmoid(logit).item()
154
+
155
+ print(f"Fake probability: {prob:.3f}")
156
+ print("Verdict:", "FAKE" if prob >= 0.5 else "REAL")
157
+ ```
158
+
159
+ ### Grad-CAM explainability
160
+
161
+ ```python
162
+ from src.gradcam import GradCAM
163
+
164
+ grad_cam = GradCAM(model)
165
+ heatmap, confidence = grad_cam.compute(img_tensor) # (224,224) heatmap in [0,1]
166
+ overlay = grad_cam.overlay(img_pil, heatmap) # PIL image with jet overlay
167
+
168
+ top_zones = grad_cam.get_top_zones(heatmap, top_k=2)
169
+ print("Most activated zones:", top_zones)
170
+ ```
171
+
172
+ ### Forensic report
173
+
174
+ ```python
175
+ from src.forensic_text import generate_forensic_report
176
+
177
+ report = generate_forensic_report(confidence=0.91, zone1="eyes", zone2="jaw")
178
+ print(report)
179
+ # HIGH CONFIDENCE FAKE (91.0%) β€” Eyes region shows unnatural reflection/texture
180
+ # patterns inconsistent with genuine facial geometry. Jaw area exhibits visible
181
+ # blending seam characteristic of face-swap artefacts.
182
+ ```
183
+
184
+ ### Gradio demo (image + video)
185
+
186
+ ```bash
187
+ python demo/app.py
188
+ ```
189
+
190
+ ---
191
+
192
+ ## Explainability β€” Facial Zones
193
+
194
+ The model maps Grad-CAM activations to 5 facial zones (pixel rows in the 224Γ—224 crop):
195
+
196
+ | Zone | Rows | Common deepfake artefacts |
197
+ |---|---|---|
198
+ | Forehead | 0–60 | Hair boundary blending, skin tone mismatch |
199
+ | Eyes | 60–100 | Unnatural reflection, pupil shape, lash generation |
200
+ | Nose | 100–145 | Texture discontinuity, geometry distortion |
201
+ | Jaw | 145–185 | Blending seam at jaw-line, edge softening |
202
+ | Hairline | 185–224 | Hair generation artefacts, boundary warping |
203
+
204
+ The top-2 activated zones are included in the forensic report.
205
+
206
+ ---
207
+
208
+ ## Ablation Results
209
+
210
+ | Configuration | Test AUC | vs Baseline |
211
+ |---|---|---|
212
+ | **Baseline (this model)** | **0.9933** | β€” |
213
+ | No data augmentation | 0.9701 | βˆ’2.32% |
214
+ | EfficientNet-B0 backbone | 0.9612 | βˆ’3.21% |
215
+ | BCE loss (no focal) | 0.9814 | βˆ’1.19% |
216
+ | Fully fine-tuned (no freezing) | 0.9878 | βˆ’0.55% |
217
+
218
+ Key findings: data augmentation and the larger B4 backbone provide the biggest gains. Focal loss measurably improves handling of the class imbalance. Selective freezing slightly outperforms full fine-tuning (likely due to overfitting risk with the large backbone).
219
+
220
+ ---
221
+
222
+ ## Limitations
223
+
224
+ - Binary classification only (real vs. fake) β€” does not identify the generation method
225
+ - No temporal modelling β€” each frame is classified independently
226
+ - Trained on Celeb-DF v2 only β€” may not generalise equally to StyleGAN or diffusion-based fakes
227
+ - High-compression video can suppress the artefacts the model relies on
228
+ - False Positive Rate of ~15.9% on the test set
229
+
230
+ ---
231
+
232
+ ## Files
233
+
234
+ | File | Description |
235
+ |---|---|
236
+ | `best_model.pt` | Trained weights (`model_state_dict` + training metadata) |
237
+ | `app.py` | Gradio demo (image + video tabs) |
238
+ | `requirements.txt` | Python dependencies |
239
+
240
+ Full source code: [github.com/Honi05/DeepFakeDetector](https://github.com/Honi05/DeepFakeDetector)
241
+
242
+ ---
243
+
244
+ ## Citation
245
+
246
+ If you use this model, please cite:
247
+
248
+ ```bibtex
249
+ @misc{arora2026deepfake,
250
+ title = {Deepfake Detection with Explainable Forensic Analysis Using EfficientNet-B4 and Grad-CAM},
251
+ author = {Arora, Honi},
252
+ year = {2026},
253
+ url = {https://huggingface.co/honi05/deepfake-detection}
254
+ }
255
+ ```
256
+
257
+ ---
258
+
259
+ ## Acknowledgements
260
+
261
+ - [Celeb-DF v2](https://github.com/yuezunli/celeb-deepfakeforensics) β€” Li et al., CVPR 2020
262
+ - [EfficientNet](https://arxiv.org/abs/1905.11946) β€” Tan & Le, ICML 2019
263
+ - [Grad-CAM](https://arxiv.org/abs/1610.02391) β€” Selvaraju et al., ICCV 2017
264
+ - [Focal Loss](https://arxiv.org/abs/1708.02002) β€” Lin et al., ICCV 2017
265
+ - [facenet-pytorch](https://github.com/timesler/facenet-pytorch) β€” MTCNN implementation