Download scripts/resolution_check.py from jimchen2/jimchen2-pendulum-shift: direct link, hf CLI and curl.
- Browser
- Download file 3.32 kB
-
https://huggingface.co/datasets/jimchen2/jimchen2-pendulum-shift/resolve/main/scripts/resolution_check.py
- Command line
-
hf download hf://datasets/jimchen2/jimchen2-pendulum-shift/scripts/resolution_check.py
-
curl -L -o resolution_check.py https://huggingface.co/datasets/jimchen2/jimchen2-pendulum-shift/resolve/main/scripts/resolution_check.py
3.32 kB
| #!/usr/bin/env python3 | |
| """Numerical-stability check: answers must survive 2x integration resolution. | |
| python3 resolution_check.py | |
| The shipped labels are produced at 8 substeps/frame (96 Hz). This re-simulates | |
| every video at 16 substeps/frame (192 Hz) and re-derives each descriptive | |
| answer. Any change means the answer sits on a numerical knife-edge. | |
| """ | |
| from __future__ import annotations | |
| import collections | |
| import csv | |
| import json | |
| import sys | |
| from multiprocessing import Pool | |
| import numpy as np | |
| sys.path.insert(0, "scripts") | |
| import pendulum_gen as G | |
| import questions_v2 as Q | |
| from regenerate_descriptive import cfg_from_row | |
| CHANGED = collections.Counter() | |
| CHECKED = collections.Counter() | |
| def to_array(rows): | |
| cols = collections.defaultdict(list) | |
| for r in rows: | |
| for k, v in r.items(): | |
| cols[k].append(v) | |
| return {k: np.asarray(v, dtype=float) for k, v in cols.items()} | |
| def one(args): | |
| vid, cfg, emitted, kind = args | |
| A8 = to_array(G.simulate(cfg, frames=G.EXT_FRAMES, substeps=8)) | |
| A16 = to_array(G.simulate(cfg, frames=G.EXT_FRAMES, substeps=16)) | |
| bad = [] | |
| checked = collections.Counter() | |
| for q in emitted: | |
| fam = q["question_family"] | |
| hi, lo = (Q.CUT_FRAME, 0) if kind else (Q.FRAMES, 0) | |
| try: | |
| a8 = _ans(fam, Q.analyze(A8, hi=hi, lo=lo), kind) | |
| a16 = _ans(fam, Q.analyze(A16, hi=hi, lo=lo), kind) | |
| except Exception: | |
| continue | |
| checked[fam] += 1 | |
| if a8 is None or a16 is None: | |
| continue | |
| if str(a8) != str(a16) or str(a16) != str(q["answer"]): | |
| bad.append((vid, fam, q["answer"], a16)) | |
| return bad, checked | |
| def _ans(fam, facts, kind): | |
| if kind: | |
| for f in Q.INTERVENTION_FAMILIES: | |
| if f[1] == fam: | |
| return f[4](kind) | |
| for f in Q.FAMILIES: | |
| if f[1] == fam: | |
| return f[4](facts) | |
| return None | |
| def main(): | |
| idx = {r["video_id"]: r for r in csv.DictReader(open("index.csv"))} | |
| itv_kind = {r["video_id"]: r["intervention_type"] for r in | |
| csv.DictReader(open("intervened/index_intervention.csv"))} | |
| jobs = [] | |
| for src in ("out/questions/questions_train.json", | |
| "out/questions/questions_val.json", | |
| "out/intervened/questions/questions_train.json"): | |
| for v in json.load(open(src))["scene_annotation"]: | |
| vid = v["video_id"] | |
| if vid not in idx: | |
| continue | |
| desc = [q for q in v["questions"] if q["question_type"] == "descriptive"] | |
| jobs.append((vid, cfg_from_row(idx[vid]), desc, | |
| itv_kind.get(vid) if src.startswith("out/intervened") else None)) | |
| with Pool(8) as p: | |
| results = p.map(one, jobs, chunksize=20) | |
| bad = [b for r, _ in results for b in r] | |
| for _, c in results: | |
| CHECKED.update(c) | |
| print(f"videos: {len(jobs)} answers checked: {sum(CHECKED.values())}") | |
| print(f"families checked: {len(CHECKED)}") | |
| for vid, fam, was, now in bad[:15]: | |
| print(f" CHANGED {vid} {fam}: 96Hz={was!r} 192Hz={now!r}") | |
| print(f"\nchanged under 2x resolution: {len(bad)} {dict(CHANGED)}") | |
| print("STABLE" if not bad else f"{len(bad)} UNSTABLE") | |
| sys.exit(0 if not bad else 1) | |
| if __name__ == "__main__": | |
| main() | |