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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# Deepfake Face Detection Project Details

## 1. Project Overview

This project detects whether a face image is **real** or **fake** using a deep learning image classifier built with **PyTorch** and deployed with **Gradio**.

The project includes:

- a notebook-based training pipeline
- a notebook for descriptive analytics and visualization
- a Gradio app for interactive prediction
- Grad-CAM visualization for interpretability
- saved training artifacts such as metrics, ROC data, and model checkpoint

The current implementation uses a **frozen ResNet18 backbone** with a custom classifier head for binary classification.

---

## 2. Problem Statement

Deepfakes are AI-generated or AI-manipulated media that can look highly realistic. In face-based deepfakes, synthetic images can be used for misinformation, impersonation, fraud, and other forms of misuse.

The aim of this project is to build a system that:

- takes a face image as input
- predicts whether it is **real** or **fake**
- explains the prediction visually using Grad-CAM
- presents performance analytics in a simple interface

---

## 3. Dataset Used

Dataset source:

- Kaggle: `140k Real and Fake Faces`
- URL: <https://www.kaggle.com/datasets/xhlulu/140k-real-and-fake-faces>

### 3.1 Dataset Summary

According to the Kaggle dataset card, the dataset contains:

- `70,000` real face images
- `70,000` fake face images
- total: `140,000` images

The dataset description states that:

- the **real** images come from the Flickr face dataset collected by NVIDIA
- the **fake** images are sampled from a larger set of StyleGAN-generated fake faces
- the images were resized to `256px`
- the dataset was already split into training, validation, and test sets
- CSV files were provided for convenience

### 3.2 Split Used in This Project

This project uses the provided CSV files and folder structure. The current split in the repository is:

| Split | Total | Real | Fake |
|---|---:|---:|---:|
| Train | 100,000 | 50,000 | 50,000 |
| Validation | 20,000 | 10,000 | 10,000 |
| Test | 20,000 | 10,000 | 10,000 |

Approximate percentages:

- Train: `71.43%`
- Validation: `14.29%`
- Test: `14.29%`

### 3.3 Dataset Layout in This Project

```text
deepfakedetection/
|-- train.csv
|-- valid.csv
|-- test.csv
`-- real_vs_fake/
    `-- real-vs-fake/
        |-- train/
        |   |-- fake/
        |   `-- real/
        |-- valid/
        |   |-- fake/
        |   `-- real/
        `-- test/
            |-- fake/
            `-- real/
```

### 3.4 Labels

This project uses:

- `fake -> 0`
- `real -> 1`

---

## 4. High-Level Project Architecture

The overall project flow is:

```text
Dataset
  -> CSV-driven sample loading
  -> Image preprocessing and augmentation
  -> ResNet18-based classifier
  -> Training and validation
  -> Best checkpoint + metrics saved in artifacts/
  -> Gradio app loads best model
  -> User uploads image
  -> Prediction + confidence + Grad-CAM
  -> Analytics charts shown in app
```

### 4.1 Main Components

1. **Dataset Loader**
   Reads image paths and labels from `train.csv`, `valid.csv`, and `test.csv`.

2. **Preprocessing and Augmentation**
   Applies resizing, random crop, horizontal flip, color jitter, normalization, and evaluation transforms.

3. **Model**
   Uses ResNet18 as the feature extractor and a custom fully connected head for binary classification.

4. **Training Pipeline**
   Handles dataloaders, loss, optimizer, scheduler, checkpointing, and early stopping.

5. **Evaluation**
   Computes loss, accuracy, F1-score, ROC-AUC, confusion matrix, and ROC curve points.

6. **Deployment**
   Loads the saved checkpoint in a Gradio app for inference.

7. **Interpretability**
   Uses Grad-CAM to highlight image regions that contributed most to the prediction.

8. **Analytics**
   Displays confusion matrix, ROC curve, and training curves in notebooks and the app.

---

## 5. ResNet Architecture Used

The current model is based on **ResNet18**.

### 5.1 Why ResNet

ResNet is a strong choice for this problem because:

- it is a proven architecture for image classification
- residual connections improve training stability
- transfer learning from ImageNet is straightforward
- it offers a good balance between performance and complexity
- it is easier to explain than heavier or more experimental architectures

### 5.2 ResNet18 Core Structure

ResNet18 mainly uses:

- `7 x 7` convolution in the first layer
- `3 x 3` convolutions in residual blocks
- `1 x 1` convolutions in shortcut/downsampling paths where needed

High-level structure:

1. Input image: `224 x 224 x 3`
2. Initial convolution: `7 x 7`, stride `2`
3. Max-pooling layer
4. Four residual stages with increasing channels:
   - `64`
   - `128`
   - `256`
   - `512`
5. Global average pooling
6. Fully connected classifier head

### 5.3 Classifier Head Used in This Project

The default ImageNet classifier is replaced with a custom head:

```text
in_features -> 256 -> 64 -> 2
```

With activation and regularization:

```text
Dropout
Linear(in_features, 256)
ReLU
Dropout
Linear(256, 64)
ReLU
Dropout
Linear(64, 2)
```

Output classes:

- `fake`
- `real`

### 5.4 Frozen Backbone Strategy

In the current setup:

- the pretrained ResNet18 backbone remains **frozen**
- only the classifier head is trained

This reduces training cost and makes the model more stable for the current project setup.

---

## 6. Training Pipeline

Training is implemented in:

- `deepfake_training.ipynb`

### 6.1 Preprocessing and Augmentation

Training transform:

- resize to slightly larger size
- random resized crop
- random horizontal flip
- light color jitter
- tensor conversion
- ImageNet normalization

Evaluation transform:

- resize to target input size
- tensor conversion
- ImageNet normalization

### 6.2 Current Training Configuration

Current configuration from `artifacts/config.json`:

| Parameter | Value |
|---|---|
| Model | `resnet18` |
| Image size | `224` |
| Batch size | `32` |
| Epochs | `10` |
| Learning rate | `3e-4` |
| Weight decay | `1e-4` |
| Num workers | `0` |
| Patience | `3` |
| Dropout | `0.3` |
| Label smoothing | `0.05` |
| Seed | `42` |
| Freeze backbone epochs | `10` |

### 6.3 Loss Function

The model uses:

- `CrossEntropyLoss`
- with label smoothing

Why:

- the task is binary classification
- the model outputs `2` logits
- CrossEntropyLoss is the standard choice for this setup

### 6.4 Softmax Usage

Softmax is used during evaluation and inference to convert logits into probabilities.

Training does **not** manually apply softmax before loss because `CrossEntropyLoss` handles that internally.

### 6.5 Optimizer and Scheduler

Optimizer:

- `AdamW`

Scheduler:

- `ReduceLROnPlateau`

Purpose:

- `AdamW` helps with stable optimization
- `ReduceLROnPlateau` lowers the learning rate if validation performance stops improving

### 6.6 Early Stopping

The training pipeline uses patience-based early stopping logic.

Current patience:

- `3`

Meaning:

- if validation performance does not improve for `3` consecutive epochs, training can stop early

---

## 7. Evaluation Metrics

The project computes the following metrics:

- loss
- accuracy
- F1-score
- ROC-AUC
- confusion matrix
- classification report
- ROC curve points

### 7.1 Best Validation Metrics

From `artifacts/best_valid_metrics.json`:

- Loss: `0.4169`
- Accuracy: `0.83685`
- F1-score: `0.83678`
- ROC-AUC: `0.91698`

### 7.2 Final Test Metrics

From `artifacts/test_metrics.json`:

- Loss: `0.41783`
- Accuracy: `0.83755`
- F1-score: `0.83801`
- ROC-AUC: `0.91600`

### 7.3 Confusion Matrix

Current test confusion matrix:

```text
[[8347, 1653],
 [1596, 8404]]
```

Interpretation:

- correctly predicted fake: `8347`
- fake predicted as real: `1653`
- real predicted as fake: `1596`
- correctly predicted real: `8404`

These results are much more realistic than the earlier near-perfect runs and suggest better-balanced generalization within this dataset setup.

---

## 8. Artifacts Produced

The training pipeline saves outputs into `artifacts/`.

Current important files:

- `best_model.pt`
- `best_valid_metrics.json`
- `best_valid_roc_curve.json`
- `test_metrics.json`
- `best_model_roc_curve.json`
- `history.json`
- `config.json`

### 8.1 What Each Artifact Contains

- `best_model.pt`
  Best saved model checkpoint

- `best_valid_metrics.json`
  Metrics on the best validation epoch

- `best_valid_roc_curve.json`
  ROC curve points for the best validation checkpoint

- `test_metrics.json`
  Final evaluation metrics on the test set

- `best_model_roc_curve.json`
  ROC curve points for the best saved model on the test split

- `history.json`
  Epoch-wise training and validation history

- `config.json`
  Exact training configuration used for the saved model

---

## 9. Gradio Application

Deployment is implemented in:

- `app.py`

### 9.1 What the App Does

The app:

- loads the saved checkpoint from `artifacts/best_model.pt`
- preprocesses an uploaded image
- predicts whether the image is real or fake
- shows prediction confidence
- generates a Grad-CAM overlay
- renders descriptive analytics charts under the main interface

### 9.2 Current UI Outputs

Main inference section:

- face image input
- prediction summary
- Grad-CAM overlay

Analytics section:

- confusion matrix
- ROC curve
- training curves by epoch

### 9.3 Grad-CAM

Grad-CAM is used to improve interpretability.

Purpose:

- highlights image regions that influenced the model prediction
- helps verify whether the model focuses on face regions instead of irrelevant image areas

Target layer used:

- the last block of `layer4` in ResNet

---

## 10. Descriptive Analytics Notebook

Analytics are implemented in:

- `descriptive_analytics.ipynb`

Current notebook contents:

- dataset split and class balance summary
- best validation and final test metrics summary
- training curves
- confusion matrix
- per-class metrics chart
- ROC curve for the best model

This notebook is meant for:

- project presentation
- analysis of training behavior
- metric visualization
- report preparation

---

## 11. Project File Structure

Important project files:

```text
deepfakedetection/
|-- app.py
|-- deepfake_training.ipynb
|-- descriptive_analytics.ipynb
|-- requirements.txt
|-- README.md
|-- train.csv
|-- valid.csv
|-- test.csv
|-- artifacts/
|   |-- best_model.pt
|   |-- best_valid_metrics.json
|   |-- best_valid_roc_curve.json
|   |-- test_metrics.json
|   |-- best_model_roc_curve.json
|   |-- history.json
|   `-- config.json
|-- imgForDemo/
|   |-- fake/
|   `-- real/
`-- real_vs_fake/
    `-- real-vs-fake/
        |-- train/
        |-- valid/
        `-- test/
```

---

## 12. Requirements

From `requirements.txt`, the current Python dependencies are:

- `torch`
- `torchvision`
- `gradio`
- `huggingface_hub`
- `Pillow`
- `tqdm`
- `numpy`
- `matplotlib`
- `jupyter`

### 12.1 Software Requirements

- Python
- Jupyter Notebook
- PyTorch
- Torchvision
- Gradio
- NumPy
- Matplotlib
- PIL / Pillow

### 12.2 Hardware Requirements

Recommended:

- GPU-enabled system for training
- sufficient disk space for the dataset and artifacts

GPU used in this project:

- `NVIDIA GeForce RTX 4050 Laptop GPU`

---

## 13. How to Run the Project

### 13.1 Training

Open:

- `deepfake_training.ipynb`

Run the notebook cells from top to bottom.

### 13.2 Analytics

Open:

- `descriptive_analytics.ipynb`

Run the cells to view charts and summaries.

### 13.3 Gradio App

Run:

```powershell
python app.py --checkpoint artifacts/best_model.pt
```

Then open:

```text
http://127.0.0.1:7860
```

---

## 14. Current Strengths of the Project

- clean notebook-based training workflow
- balanced dataset split
- reproducible saved artifacts
- Grad-CAM-based interpretability
- browser-based deployment with Gradio
- integrated analytics for presentation and reporting
- realistic evaluation metrics after reducing over-optimistic earlier behavior

---

## 15. Current Limitations

- the current model uses only a frozen ResNet18 backbone
- generalization to completely different deepfake datasets is not yet tested
- the project focuses on image-level deepfake detection, not video-level detection
- no face-crop specific preprocessing pipeline is currently used
- the dataset may still contain dataset-specific shortcuts not fully removed

---

## 16. Possible Future Improvements

- partially unfreeze the backbone for fine-tuning
- compare ResNet18 with ResNet34 or ResNet50
- add face detection and train on cropped faces only
- evaluate on another external deepfake dataset
- add multiple checkpoint selection in the app
- extend from image-based detection to video deepfake detection
- add confidence calibration and deeper bias analysis

---

## 17. Source and Attribution

Dataset used:

- Kaggle dataset page: <https://www.kaggle.com/datasets/xhlulu/140k-real-and-fake-faces>

Dataset description used for this document was based on the Kaggle dataset card, which states that the dataset contains `70k` real faces and `70k` StyleGAN-generated fake faces, resized and split into train, validation, and test sets with CSV support.