Download project/scripts/audit_cave_failure_cases.py from xiaojie06/CaveWorld3D: direct link, hf CLI and curl.
- Browser
- Download file 8.01 kB
-
https://huggingface.co/xiaojie06/CaveWorld3D/resolve/main/project/scripts/audit_cave_failure_cases.py
- Command line
-
hf download hf://xiaojie06/CaveWorld3D/project/scripts/audit_cave_failure_cases.py
-
curl -L -o audit_cave_failure_cases.py https://huggingface.co/xiaojie06/CaveWorld3D/resolve/main/project/scripts/audit_cave_failure_cases.py
8.01 kB
| """Rank paired, archived predictions before fitting a new candidate. All metrics use real target views.""" | |
| import argparse | |
| import json | |
| from collections import Counter, defaultdict | |
| from pathlib import Path | |
| import numpy as np | |
| def read(p): | |
| return json.loads(p.read_text()) | |
| def write(p, obj): | |
| p.write_text(json.dumps(obj, indent=2, ensure_ascii=False, allow_nan=False) + "\n") | |
| def mean(xs): | |
| finite = [v for v in xs if v is not None and np.isfinite(v)] | |
| return float(np.mean(finite)) if finite else None | |
| def case_rows(root, cohort): | |
| surface = root / "surface_outpaint_20261201" | |
| if cohort in ("voyager", "mapped"): | |
| rows = read( | |
| root | |
| / "texture_color_light_20261130/evidence" | |
| / f"final-{cohort}-units.json" | |
| ) | |
| for r in rows: | |
| yield dict( | |
| cohort=cohort, | |
| key=r["file"], | |
| source=r["source"], | |
| scene=r["group"], | |
| group=r["group"], | |
| base=r["metrics"]["fixed"], | |
| parent=r["metrics"]["parent"], | |
| old=r["metrics"]["new"], | |
| target_sha=r["reference"], | |
| mask_sha=r["mask"], | |
| ) | |
| else: | |
| for r in read(surface / "tables" / f"release-{cohort}.json"): | |
| yield dict( | |
| cohort=cohort, | |
| key=r["case"], | |
| source=r["source"], | |
| scene=r["scene"], | |
| group=r["group"], | |
| base=r["scores"]["official_lama"], | |
| parent=r["scores"]["previous_photo"], | |
| old=r["scores"]["released_photo"], | |
| outside=r["outside"], | |
| scale=r.get("scale"), | |
| ) | |
| def level(rows, model, m, branch): | |
| valid = [r[model] for r in rows if r[model].get(m) is not None] | |
| if m == "mae" and branch == "voyager" and valid: | |
| return sum(v["error"] for v in valid) / ( | |
| 3 * 255 * sum(v["pixels"] for v in valid) | |
| ) | |
| return mean(v[m] for v in valid) | |
| def groups(rows, k): | |
| out = defaultdict(list) | |
| for r in rows: | |
| out[r[k]].append(r) | |
| return out | |
| def summarize(rows, branch): | |
| res = {} | |
| for m in ( | |
| "mae", | |
| "lpips", | |
| "dark", | |
| "bright", | |
| "bias", | |
| "de00", | |
| "gradient_ratio", | |
| "fine_corr", | |
| "near_mae", | |
| "far_mae", | |
| "boundary", | |
| ): | |
| good = [ | |
| r | |
| for r in rows | |
| if r["old"].get(m) is not None and r["parent"].get(m) is not None | |
| ] | |
| if not good: | |
| continue | |
| improvements = np.array([r["parent"][m] - r["old"][m] for r in good]) | |
| vals = np.array([r["old"][m] for r in good]) | |
| by = groups(good, "scene") | |
| res[m] = dict( | |
| n=len(good), | |
| lower=int((improvements > 1e-12).sum()), | |
| same=int((np.abs(improvements) <= 1e-12).sum()), | |
| upper=int((improvements < -1e-12).sum()), | |
| median=float(np.median(vals)), | |
| p90=float(np.quantile(vals, 0.9)), | |
| macro={ | |
| v: mean(level(rr, v, m, branch) for rr in by.values()) | |
| for v in ("base", "parent", "old") | |
| }, | |
| by_scene={ | |
| s: {v: level(rr, v, m, branch) for v in ("base", "parent", "old")} | |
| for s, rr in by.items() | |
| }, | |
| ) | |
| return res | |
| def audit(root, dest): | |
| dest.mkdir(parents=True, exist_ok=True) | |
| out = {} | |
| chosen = {} | |
| full = {} | |
| for c in ("heldout", "unseen", "stress", "recorded", "voyager", "mapped"): | |
| rows = list(case_rows(root, c)) | |
| full[c] = rows | |
| diag = summarize(rows, c) | |
| ids = [r["key"] for r in rows] | |
| assert len(set(ids)) == len(ids) | |
| eligible = [ | |
| r | |
| for r in rows | |
| if r["old"]["lpips"] is not None and r["parent"]["lpips"] is not None | |
| ] | |
| ma = lambda r: r["old"]["mae"] - r["parent"]["mae"] | |
| lp = lambda r: r["old"]["lpips"] - r["parent"]["lpips"] | |
| outcomes = Counter( | |
| ("lower" if ma(r) < -1e-12 else "higher" if ma(r) > 1e-12 else "equal") | |
| + "_" | |
| + ("lower" if lp(r) < -1e-12 else "higher" if lp(r) > 1e-12 else "equal") | |
| for r in eligible | |
| ) | |
| selected = [] | |
| for m in ("mae", "lpips", "bright", "fine_corr"): | |
| ok = [ | |
| r | |
| for r in rows | |
| if r["old"].get(m) is not None and r["parent"].get(m) is not None | |
| ] | |
| if m == "bright": | |
| ok = [r for r in ok if r["old"].get("bright_n", 0) >= 100] | |
| sign = -1 if m == "fine_corr" else 1 | |
| for key, fun in [ | |
| ("absolute", lambda r: sign * r["old"][m]), | |
| ("regression", lambda r: sign * (r["old"][m] - r["parent"][m])), | |
| ]: | |
| if ok: | |
| r = max(sorted(ok, key=lambda v: v["key"]), key=fun) | |
| selected.append( | |
| dict( | |
| metric=m, | |
| criterion=key, | |
| key=r["key"], | |
| source=r["source"], | |
| scene=r["scene"], | |
| base=r["base"].get(m), | |
| parent=r["parent"][m], | |
| old=r["old"][m], | |
| ) | |
| ) | |
| if eligible: | |
| r = min(eligible, key=lp) | |
| selected.append( | |
| dict( | |
| metric="lpips", | |
| criterion="improvement_control", | |
| key=r["key"], | |
| source=r["source"], | |
| scene=r["scene"], | |
| base=r["base"]["lpips"], | |
| parent=r["parent"]["lpips"], | |
| old=r["old"]["lpips"], | |
| ) | |
| ) | |
| chosen[c] = selected | |
| out[c] = dict( | |
| rows=len(rows), | |
| sources=len({r["source"] for r in rows}), | |
| scene_counts=Counter(r["scene"] for r in rows), | |
| shared_target_hashes=( | |
| len(rows) - len({r["target_sha"] for r in rows}) | |
| if "target_sha" in rows[0] | |
| else None | |
| ), | |
| metrics=diag, | |
| joint_outcomes=outcomes, | |
| ) | |
| if c == "stress": | |
| out[c]["by_width"] = { | |
| str(sc): summarize(items, c) | |
| for sc, items in groups(rows, "scale").items() | |
| } | |
| atlas = [ | |
| r | |
| for r in read( | |
| root / "surface_outpaint_20261201/tables/release-atlas-heldout.json" | |
| ) | |
| if r["role"] == "test" | |
| ] | |
| out["atlas"] = [ | |
| dict( | |
| key=r["case"], | |
| source=r["source"], | |
| scene=r["scene"], | |
| coverage=r["coverage"], | |
| matches=r["matches"], | |
| single={ | |
| k: r["scores"]["photo"].get(k) | |
| for k in ("mae", "lpips", "bright", "fine_corr") | |
| }, | |
| mapped={ | |
| k: r["scores"]["map"].get(k) | |
| for k in ("mae", "lpips", "bright", "fine_corr") | |
| }, | |
| ) | |
| for r in atlas | |
| ] | |
| write(dest / "diagnosis.json", out) | |
| write(dest / "case-list.json", chosen) | |
| write(dest / "paired-units.json", full) | |
| for c in chosen: | |
| summary = out[c] | |
| print( | |
| c, | |
| summary["rows"], | |
| summary["metrics"]["lpips"]["n"], | |
| summary["joint_outcomes"], | |
| ) | |
| print( | |
| "locked visual criteria: worst absolute value, worst signed change, and best LPIPS change; highlights require 100 real bright pixels" | |
| ) | |
| if __name__ == "__main__": | |
| a = argparse.ArgumentParser(description=__doc__) | |
| a.add_argument("results", type=Path) | |
| a.add_argument("out", type=Path) | |
| v = a.parse_args() | |
| audit(v.results, v.out) | |