File size: 2,164 Bytes
f9ed2b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Evaluate one final binary checkpoint on the fixed WildGen validation split."""
import argparse
import json
from pathlib import Path
import torch
from torch.utils.data import DataLoader
from evaluate_robustness import (BinarySplitDataset, build_model, load_checkpoint, load_test_rows, make_transform, evaluate)

def validate_rows(rows):
    real = sum(r["label"] == 0 for r in rows)
    fake = sum(r["label"] == 1 for r in rows)
    if (real, fake) != (8000, 34200):
        raise ValueError(f"Expected 8,000 Real and 34,200 AI validation images; got {real}, {fake}")
    if len({r["path"] for r in rows}) != len(rows):
        raise ValueError("Repeated validation image paths")

def main():
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument("--model-type", choices=["univfd", "cnndetect"], required=True)
    p.add_argument("--checkpoint", type=Path, required=True)
    p.add_argument("--split-file", type=Path, required=True)
    p.add_argument("--output", type=Path, required=True)
    p.add_argument("--batch-size", type=int, default=64)
    p.add_argument("--num-workers", type=int, default=4)
    p.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu")
    a = p.parse_args()
    expected = "fc_weights_final.pth" if a.model_type == "univfd" else "model_final.pth"
    if a.checkpoint.name != expected:
        p.error(f"Expected {expected}")
    rows = load_test_rows(a.split_file)
    validate_rows(rows)
    device = torch.device(a.device)
    model = build_model(a.model_type, a.device).to(device)
    load_checkpoint(model, a.model_type, a.checkpoint)
    loader = DataLoader(BinarySplitDataset(rows, make_transform(a.model_type)), batch_size=a.batch_size, num_workers=a.num_workers, shuffle=False)
    result = {"checkpoint": str(a.checkpoint), "split_file": str(a.split_file), "reporting_policy": "final_epoch", "metrics": evaluate(model, loader, device)}
    a.output.parent.mkdir(parents=True, exist_ok=True)
    a.output.write_text(json.dumps(result, indent=2), encoding="utf-8")
    print(json.dumps(result, indent=2))

if __name__ == "__main__": main()