"""Pack the 14 per-member checkpoints + gate config into ONE file: final/bundle.pt. Load it with: GatedEnsemble("final/bundle.pt"). Run: python final/bundle_ensemble.py """ import os, sys, glob, json import torch HERE = os.path.dirname(os.path.abspath(__file__)); CKPT = os.path.join(HERE, "checkpoints") man = json.load(open(os.path.join(HERE, "ensemble.json"))) bundle = {"tau": man["gate"]["tau"], "members": {}, "meta": {"pipeline": man["pipeline"], "description": man["description"], "results": man["results"], "feature_dims": man["feature_dims"]}} n = 0 for group in ("base_perch", "arm_harmonic", "arm_birdmae", "arm_bgwhiten"): bundle["members"][group] = [] for cp in sorted(glob.glob(os.path.join(CKPT, f"{group}_s*.pt"))): st = torch.load(cp, map_location="cpu", weights_only=False) bundle["members"][group].append({ "state": st["model_state_dict"], "mean": st["mean"], "std": st["std"], "standardize": st["standardize"], "arch": st["arch"], "feature_dim": st["feature_dim"], "seed": st["seed"]}) n += 1 out = os.path.join(HERE, "bundle.pt") torch.save(bundle, out) mb = os.path.getsize(out) / 1e6 print(f"bundled {n} member checkpoints + gate(tau={bundle['tau']}) -> {out} ({mb:.1f} MB)") print("groups:", {g: len(v) for g, v in bundle["members"].items()})