File size: 4,160 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 105 106 | #!/usr/bin/env python3
"""Verify the regenerated descriptive questions.
python3 verify_descriptive.py
Checks, and exits non-zero on any failure:
1. question count per video is unchanged vs the shipped files
2. every non-descriptive question is byte-identical to the shipped file
3. descriptive questions carry no `choices` (open-ended, like CLEVRER)
4. every descriptive answer is exactly one word from answer_vocab.json
5. every descriptive question has question_subtype + question_family_index
6. no phrasing/family has a 100% majority answer
7. the no-video majority baseline is reported and compared to CLEVRER
"""
from __future__ import annotations
import collections
import json
import sys
PAIRS = [
("questions/questions_train.json", "out/questions/questions_train.json"),
("questions/questions_val.json", "out/questions/questions_val.json"),
("intervened/questions/questions_train.json",
"out/intervened/questions/questions_train.json"),
]
CLEVRER_BASELINE = 0.416 # measured on CLEVRER val descriptive, n=54,990
fails = []
def check(ok, msg):
print((" PASS " if ok else " FAIL ") + msg)
if not ok:
fails.append(msg)
def main():
vocab = set(json.load(open("out/questions/answer_vocab.json"))["answer_vocab"])
print(f"answer vocabulary: {len(vocab)} tokens\n")
grand_t = grand_m = 0
for old_p, new_p in PAIRS:
old = json.load(open(old_p))
new = json.load(open(new_p))
print(f"--- {new_p}")
ov, nv = old["scene_annotation"], new["scene_annotation"]
check(len(ov) == len(nv), f"video count {len(ov)} == {len(nv)}")
same_len = all(len(a["questions"]) == len(b["questions"])
for a, b in zip(ov, nv))
check(same_len, "question count per video unchanged")
# non-descriptive must be untouched
diff = 0
for a, b in zip(ov, nv):
ka = [q for q in a["questions"] if q["question_type"] != "descriptive"]
kb = [q for q in b["questions"] if q["question_type"] != "descriptive"]
if ka != kb:
diff += 1
check(diff == 0, f"non-descriptive questions identical ({diff} videos differ)")
desc = [q for v in nv for q in v["questions"]
if q["question_type"] == "descriptive"]
check(all("choices" not in q for q in desc),
f"no descriptive question has `choices` (n={len(desc)})")
multi = [q["answer"] for q in desc if len(str(q["answer"]).split()) != 1]
check(not multi, f"all descriptive answers are single-word "
f"({len(multi)} violations)")
bad = sorted({q["answer"] for q in desc if q["answer"] not in vocab})
check(not bad, f"all answers in vocabulary (offenders: {bad[:6]})")
miss = [q["question_family"] for q in desc
if "question_subtype" not in q or "question_family_index" not in q]
check(not miss, "all descriptive have subtype + family_index")
byfam = collections.defaultdict(list)
for q in desc:
byfam[q["question_family"]].append(q["answer"])
worst = max(byfam.items(), key=lambda kv: collections.Counter(kv[1])
.most_common(1)[0][1] / len(kv[1]))
wmaj = collections.Counter(worst[1]).most_common(1)[0][1] / len(worst[1])
check(wmaj < 1.0, f"no family is 100% guessable "
f"(worst: {worst[0]} at {wmaj:.1%})")
tot = sum(len(v) for v in byfam.values())
maj = sum(collections.Counter(v).most_common(1)[0][1] for v in byfam.values())
grand_t += tot
grand_m += maj
print(f" n={tot} families={len(byfam)} "
f"no-video baseline={maj/tot:.1%}\n")
base = grand_m / grand_t
print(f"=== overall no-video majority baseline: {base:.1%} "
f"(CLEVRER descriptive: {CLEVRER_BASELINE:.1%})")
check(base < 0.55, "overall baseline below 55%")
print("\n" + ("ALL CHECKS PASSED" if not fails else f"{len(fails)} FAILURE(S)"))
sys.exit(0 if not fails else 1)
if __name__ == "__main__":
main()
|