Add evaluate.py
Browse files- evaluate.py +79 -0
evaluate.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Evaluate Venice-H1 on pre-extracted feature caches.
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
python evaluate.py --checkpoint checkpoints/best.pt \
|
| 7 |
+
--splits data/cached_testA_refcoco_unc_feats.pt \
|
| 8 |
+
data/cached_testB_refcoco_unc_feats.pt
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import argparse
|
| 12 |
+
import json
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
import torch
|
| 16 |
+
from torch.utils.data import DataLoader
|
| 17 |
+
|
| 18 |
+
from venice_h1.model import VeniceH1Reranker
|
| 19 |
+
from train import FeatureCacheDataset, evaluate
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def main():
|
| 23 |
+
parser = argparse.ArgumentParser()
|
| 24 |
+
parser.add_argument("--checkpoint", required=True,
|
| 25 |
+
help="Path to checkpoint .pt file")
|
| 26 |
+
parser.add_argument("--splits", nargs="+", required=True,
|
| 27 |
+
help="One or more feature cache .pt files to evaluate")
|
| 28 |
+
parser.add_argument("--tau", type=float, default=0.5,
|
| 29 |
+
help="Failure Gate threshold")
|
| 30 |
+
parser.add_argument("--no_grid", action="store_true")
|
| 31 |
+
parser.add_argument("--batch_size", type=int, default=512)
|
| 32 |
+
parser.add_argument("--device", default="cuda")
|
| 33 |
+
args = parser.parse_args()
|
| 34 |
+
|
| 35 |
+
device = torch.device(args.device if torch.cuda.is_available() else "cpu")
|
| 36 |
+
|
| 37 |
+
# Load checkpoint
|
| 38 |
+
ckpt = torch.load(args.checkpoint, map_location="cpu")
|
| 39 |
+
state = ckpt["model"] if "model" in ckpt else ckpt
|
| 40 |
+
|
| 41 |
+
# Infer model config from weights
|
| 42 |
+
use_grid = not args.no_grid
|
| 43 |
+
model = VeniceH1Reranker(use_grid=use_grid).to(device)
|
| 44 |
+
model.load_state_dict(state, strict=False)
|
| 45 |
+
model.eval()
|
| 46 |
+
|
| 47 |
+
print(f"Loaded: {args.checkpoint}")
|
| 48 |
+
print(f"Parameters: {model.num_parameters():,} | tau={args.tau}")
|
| 49 |
+
print()
|
| 50 |
+
|
| 51 |
+
results = {}
|
| 52 |
+
for split_path in args.splits:
|
| 53 |
+
if not Path(split_path).exists():
|
| 54 |
+
print(f" [skip] {split_path} not found")
|
| 55 |
+
continue
|
| 56 |
+
|
| 57 |
+
ds = FeatureCacheDataset(split_path, use_grid=use_grid)
|
| 58 |
+
loader = DataLoader(ds, batch_size=args.batch_size,
|
| 59 |
+
shuffle=False, num_workers=4)
|
| 60 |
+
split_name = Path(split_path).stem.replace("cached_", "").replace("_feats", "")
|
| 61 |
+
|
| 62 |
+
metrics = evaluate(model, loader, device, tau=args.tau)
|
| 63 |
+
results[split_name] = metrics
|
| 64 |
+
|
| 65 |
+
print(f" {split_name:<30s} "
|
| 66 |
+
f"AUC={metrics['gate_auc']:.3f} "
|
| 67 |
+
f"harmful={metrics['harmful_switch']*100:.3f}% "
|
| 68 |
+
f"correct_rerank={metrics['correct_rerank']*100:.1f}% "
|
| 69 |
+
f"failure_rate={metrics['failure_rate']*100:.1f}%")
|
| 70 |
+
|
| 71 |
+
print()
|
| 72 |
+
out_path = "eval_results.json"
|
| 73 |
+
with open(out_path, "w") as f:
|
| 74 |
+
json.dump(results, f, indent=2)
|
| 75 |
+
print(f"Saved results → {out_path}")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
main()
|