diff --git a/spatial/cli.py b/spatial/cli.py index 8e8da56..8999cb2 100644 --- a/spatial/cli.py +++ b/spatial/cli.py @@ -216,7 +216,7 @@ def cmd_eval(args) -> int: def cmd_report(args) -> int: from . import report - report.report(args.config, n=args.n) + report.report(args.config, n=args.n, ablate=args.ablate) return 0 @@ -233,6 +233,8 @@ def main(argv=None) -> int: sp.add_argument("--resume", action="store_true") if name == "report": sp.add_argument("--n", type=int, default=128) + sp.add_argument("--ablate", default=None, + choices=[None, "dangling", "scene", "edge_feat"]) sp.set_defaults(fn=fn) args = p.parse_args(argv) return args.fn(args) diff --git a/spatial/report.py b/spatial/report.py index 26f52f3..ed2bde3 100644 --- a/spatial/report.py +++ b/spatial/report.py @@ -29,11 +29,15 @@ ANSWERS = ("true", "false", "unknown") @torch.no_grad() -def family_row(model, family: str, seed: int, n: int, max_entities: int = 32) -> dict: +def family_row(model, family: str, seed: int, n: int, max_entities: int = 32, + ablate: str = None) -> dict: batch = [generator.make(family, seed=seed, index=i) for i in range(n)] device = next(model.parameters()).device t = collate(batch, max_entities) t = {k: (v.to(device) if torch.is_tensor(v) else v) for k, v in t.items()} + if ablate: + # a mechanism that survives its own removal was never used + t[ablate] = torch.zeros_like(t[ablate]) out = model(t["feat"], t["pad_mask"], t["edge_index"], t["edge_type"], t["edge_mask"], t["q_ents"], t["q_kind"], t["edit_op"], t["edit_idx"], t["edge_removed"], t["edge_feat"], t["scene"], t["dangling"]) @@ -70,7 +74,7 @@ def family_row(model, family: str, seed: int, n: int, max_entities: int = 32) -> } -def report(config_path: str, n: int = 128) -> None: +def report(config_path: str, n: int = 128, ablate: str = None) -> None: root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ckpt = os.path.join(root, "out_spatial", os.path.basename(config_path).replace(".json", ""), "ckpt.pt") @@ -78,12 +82,16 @@ def report(config_path: str, n: int = 128) -> None: model.eval() print(f"step {step} families: train={cfg['data']['train_families']} " f"val={cfg['data']['val_families']}") - head = f"{'family':11s} {'n':>3s} {'dec':>4s} {'unk':>4s} {'acc':>5s} {'abst':>5s} " \ - f"{'cert':>5s} {'#true':>5s} {'conf':>5s}" + if ablate: + print(f"ABLATION: {ablate} zeroed at eval time") + head = f"{'family':11s} {'n':>3s} {'dec':>4s} {'unk':>4s} {'dang':>4s} {'acc':>5s} " \ + f"{'abst':>5s} {'cert':>5s} {'#true':>5s} {'conf':>5s}" print(head) print("-" * len(head)) for fam in generator.FAMILIES: - r = family_row(model, fam, cfg["seed"], n, cfg["model"]["max_entities"]) + ndang = sum(len(generator.make(fam, cfg["seed"], i).obs.dangling) for i in range(8)) / 8 + r = family_row(model, fam, cfg["seed"], n, cfg["model"]["max_entities"], ablate) print(f"{r['family']:11s} {r['n']:3d} {r['decidable']:4d} {r['unknown']:4d} " + f"{ndang:4.1f} " f"{r['acc']:5.3f} {r['abst']:5.3f} {r['cert']:5.3f} {r['n_true']:5d} " f"{r['conf']:5.3f}")