CaveWorld3D / project /scripts /ablate_dual_cave_training.py
xiaojie06's picture
Archive CaveWorld3D project files 路 5
1d15e3f verified
Raw History Blame Contribute Delete
3.59 kB
"""Isolate readout training from its inputs with a matched zero-step model and residual strength."""
import argparse
from collections import defaultdict
from pathlib import Path
import time
import numpy as np
import torch
from benchmark_cave_versions import Perception
from dual_cave_records import read, write
from train_dual_cave_metrics import build_model, artifact, load_rows, measure, summary
from evaluate_dual_cave_metrics import paired
@torch.inference_mode()
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("root", type=Path)
p.add_argument("branch")
a = p.parse_args()
r, branch = a.root, a.branch
while not (r / "scores" / branch / "groups.json").is_file():
time.sleep(10)
d = r / "data" / branch
idx = read(d / "test.json") + (
read(d / "audit.json") if (d / "audit.json").is_file() else []
)
if branch == "single" and (d / "audit_full.json").exists():
idx += read(d / "audit_full.json")
out = r / "scores" / branch
actual = {(v["group"], v["key"]): v for v in read(out / "units.json")}
strength = read(r / "weights" / branch / "complete.json")["best"]["strength"]
torch.manual_seed(80371)
net = (
build_model(
branch,
read(r / "weights" / branch / "complete.json").get("design", "bands"),
)
.cuda()
.eval()
)
per = Perception()
side = 64 if branch == "voyager" else 96
groups = defaultdict(list)
for key in idx:
g = (
key["group"]
if key["split"] != "audit" or branch != "single"
else "auxiliary_photo_audit"
)
if key["group"].startswith("full_sensor_"):
g = "full_sensor_audit"
groups[g].append(key)
totals = {}
for g, rows in groups.items():
data = []
for v in load_rows(d, rows, side):
image = artifact(net, v, "bands", strength)
m = measure(image, v, per, side)
ori = v["stack"][1].copy()
if v["preserve"] is not None:
ori[v["preserve"]] = v["stack"][0][v["preserve"]]
proj = measure(ori, v, per, side)
data.append(
dict(
key=v["key"],
initial=m,
projection=proj,
trained=actual[g, v["key"]]["metrics"]["candidate"],
)
)
totals[g] = {
k: summary([x[k] for x in data], branch)
for k in ("initial", "projection", "trained")
}
totals[g]["training_gain"] = paired(totals[g]["initial"], totals[g]["trained"])
totals[g]["counts"] = dict(
both=sum(
all(x["trained"][m] < x["initial"][m] for m in ("mae", "lpips"))
for x in data
if x["trained"]["lpips"] is not None
)
)
if branch == "voyager":
for s in (0, 42):
items = [v for g, v in totals.items() if g.endswith("_seed" + str(s))]
vals = {
k: {
metric: float(np.mean([v[k][metric] for v in items]))
for metric in ("mae", "lpips")
}
for k in ("initial", "projection", "trained")
}
totals["V13_seed" + str(s)] = dict(
**vals, training_gain=paired(vals["initial"], vals["trained"])
)
write(out / "attribution.json", totals)
print(branch, len(totals), flush=True)
if __name__ == "__main__":
main()