#!/usr/bin/env python3 """Evaluate Cosmos-Reason2 as a task-success verifier on procedural tabletop scenes. Protocol: pick the decision threshold on a calibration split (seed A), report metrics on a held-out split (seed B) so the threshold isn't tuned on test data. MUJOCO_GL=egl HF_TOKEN=... python scripts/eval_verifier.py --n 50 --out results/verifier_cr2b.jsonl """ import argparse import json import sys import time from collections import defaultdict from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) import torch # noqa: E402 from robotics.eval.stats import wilson # noqa: E402 from robotics.sim.tabletop import make_suite # noqa: E402 from robotics.vlm.cosmos_reason import CosmosVerifier # noqa: E402 def auroc(pos: list[float], neg: list[float]) -> float: wins = sum((p > n) + 0.5 * (p == n) for p in pos for n in neg) return wins / (len(pos) * len(neg)) def score_split(v, seed, n, size, tag, split): rows = [] for i, s in enumerate(make_suite(n, seed=seed, size=size)): t = time.perf_counter() p = v.score(s.image, s.task) rows.append({"run": tag, "split": split, "seed": seed, "family": s.task.split()[0], "kind": s.kind, "task": s.task, "gt": s.success, "p": round(p, 4), "latency_s": round(time.perf_counter() - t, 3)}) print(f"[{split} {i + 1}/{2 * n}] gt={s.success!s:5} p={p:.3f} {s.kind:22} {s.task}", flush=True) return rows def best_threshold(rows): cands = sorted({r["p"] for r in rows} | {0.5}) return max(cands, key=lambda t: sum((r["p"] >= t) == r["gt"] for r in rows)) def report(rows, thr): def acc(rs): k = sum((r["p"] >= thr) == r["gt"] for r in rs) lo, hi = wilson(k, len(rs)) return f"{k / len(rs):6.1%} [{lo:.0%},{hi:.0%}] n={len(rs)}" pos = [r["p"] for r in rows if r["gt"]] neg = [r["p"] for r in rows if not r["gt"]] print(f"\nthreshold={thr:.3f} accuracy {acc(rows)} AUROC={auroc(pos, neg):.3f}") print(f" TPR (successes accepted) {acc([r for r in rows if r['gt']])}") print(f" TNR (failures rejected) {acc([r for r in rows if not r['gt']])}") by = defaultdict(list) for r in rows: by[(r["family"], r["kind"])].append(r) for k in sorted(by): print(f" {k[0]:6} {k[1]:22} {acc(by[k])}") lat = sorted(r["latency_s"] for r in rows) print(f" latency median {lat[len(lat) // 2]:.2f}s p90 {lat[int(len(lat) * .9)]:.2f}s") def main(): ap = argparse.ArgumentParser() ap.add_argument("--n", type=int, default=50, help="scenes per task family per split") ap.add_argument("--size", type=int, default=448) ap.add_argument("--calib-seed", type=int, default=100) ap.add_argument("--test-seed", type=int, default=200) ap.add_argument("--tag", default="cosmos_reason2_2b") ap.add_argument("--out", default="results/verifier_cosmos_reason2_2b.jsonl") a = ap.parse_args() torch.set_num_threads(max(1, torch.get_num_threads())) v = CosmosVerifier() calib = score_split(v, a.calib_seed, a.n, a.size, a.tag, "calib") test = score_split(v, a.test_seed, a.n, a.size, a.tag, "test") thr = best_threshold(calib) Path(a.out).parent.mkdir(parents=True, exist_ok=True) with open(a.out, "w") as f: for r in calib + test: f.write(json.dumps({**r, "threshold": thr}) + "\n") print("\n=== calibration split (threshold chosen here) ===") report(calib, thr) print("\n=== held-out test split ===") report(test, thr) print("\n=== held-out test split @ default 0.5 ===") report(test, 0.5) if __name__ == "__main__": main()