File size: 3,886 Bytes
1920805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Evaluation Metrics

This project uses [scikit-learn](https://scikit-learn.org/stable/modules/model_evaluation.html) to measure how well the deepfake detector performs on a **labeled test set**.

## Labels

| Value | Meaning |
|-------|---------|
| `0` | Real video |
| `1` | Fake (deepfake) video |

Precision, recall, and F1 are reported for the **fake** class (`1`) by default.

## What each metric means

| Metric | What it measures | Good when… |
|--------|------------------|------------|
| **Accuracy** | Fraction of correct predictions (real or fake) | Higher is better |
| **Precision** | Of videos called fake, how many were actually fake | Fewer false alarms |
| **Recall** | Of all real fakes, how many the model caught | Fewer missed fakes |
| **F1** | Balance between precision and recall | Higher is better |
| **AUC** | How well the model ranks fakes above reals (uses probability scores, not just hard labels) | Closer to 1.0 is better |

**Example:** High recall but low precision means the model catches most fakes but also flags many real videos as fake.

## How metrics are computed

1. Load the **test split** from `data/metadata.csv` (via `EyeSequenceDataset`).
2. Run the trained model on every test sample.
3. Collect three arrays:
   - `y_true` — ground-truth labels from the dataset
   - `y_pred` — predicted class (`0` or `1`)
   - `y_prob` — probability the video is fake (`softmax` score for class `1`)
4. Pass those arrays to scikit-learn in `src/eval/evaluate.py`.

During **training**, only validation **accuracy** is logged each epoch (`src/train/train.py`). Full metrics are run after training with the evaluate script below.

## Run evaluation (numbers only)

```bash
source .venv311/bin/activate
python -m src.eval.evaluate \
  --checkpoint outputs/best.pt \
  --config configs/train/aat_pgd.yaml
```

Example output:

```json
{
  "accuracy": 0.91,
  "precision": 0.89,
  "recall": 0.93,
  "f1": 0.91,
  "auc": 0.96
}
```

Results are also saved to `outputs/eval/metrics.json`.

## Visual representation (plots)

Add `--plots` to generate PNG charts from the same run:

```bash
python -m src.eval.evaluate \
  --checkpoint outputs/best.pt \
  --config configs/train/aat_pgd.yaml \
  --plots
```

This writes:

| File | What you see |
|------|----------------|
| `outputs/eval/confusion_matrix.png` | Grid of true vs predicted counts (correct top-left / bottom-right, errors off-diagonal) |
| `outputs/eval/roc_curve.png` | Trade-off between true-positive and false-positive rate; curve closer to the top-left is better |
| `outputs/eval/metrics.json` | Numeric metrics |
| `outputs/eval/y_*.npy` | Raw label arrays (for re-plotting or custom analysis) |


### Re-generate plots without re-running the model

If you already have saved arrays:

```bash
python -m src.eval.plots \
  --y-true outputs/eval/y_true.npy \
  --y-pred outputs/eval/y_pred.npy \
  --y-prob outputs/eval/y_prob.npy \
  --out-dir outputs/eval
```

### Custom output folder

```bash
python -m src.eval.evaluate \
  --checkpoint outputs/best.pt \
  --config configs/train/aat_pgd.yaml \
  --out-dir outputs/my_run \
  --plots
```

## Prerequisites

- A trained checkpoint at `outputs/best.pt` (or pass another path).
- Test data: `data/metadata.csv` with a `test` split and `.npz` eye-sequence files on disk.
- Dependencies: `scikit-learn`, `matplotlib`, `seaborn` (see `requirements.txt`).

## Where the code lives

| File | Role |
|------|------|
| `src/eval/evaluate.py` | Runs model on test set, computes metrics, saves JSON/arrays |
| `src/eval/plots.py` | Builds confusion matrix and ROC curve images |
| `src/train/train.py` | Training-time validation accuracy only |

## Further reading

- [scikit-learn: Metrics and scoring](https://scikit-learn.org/stable/modules/model_evaluation.html)
- `docs/results_template.md` — table template for thesis/report numbers