Download code/scripts/eval/evaluate_binary_checkpoints.py from Shining31/WildGen: direct link, hf CLI and curl.
- Browser
- Download file 2.16 kB
-
https://huggingface.co/datasets/Shining31/WildGen/resolve/main/code/scripts/eval/evaluate_binary_checkpoints.py
- Command line
-
hf download hf://datasets/Shining31/WildGen/code/scripts/eval/evaluate_binary_checkpoints.py
-
curl -L -o evaluate_binary_checkpoints.py https://huggingface.co/datasets/Shining31/WildGen/resolve/main/code/scripts/eval/evaluate_binary_checkpoints.py
2.16 kB
| """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() | |