File size: 3,320 Bytes
2a7bf26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/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()