#!/usr/bin/env python3 """Keep the trained models and a few rollout videos of every finished run in experiments/results/keep/. python3 experiments/lerobot/keep_artifacts.py [--per-task N] For each run folder in experiments/results/phase2 whose evaluations all finished (every eval_*/eval_info.json exists), this keeps: keep//model/ the final pretrained_model (weights + config; the optimizer state is skipped) keep//videos//task_{success,failure}_ep.mp4 up to N of each per task (default 1) keep//run.json, keep//_eval_info.json, task_descriptions Files are hard links, so the copy costs no disk space, and a later re-run of a folder (which deletes and rewrites its outputs) leaves the kept files intact. Smoke runs are skipped. Safe to re-run: existing kept files are left alone. """ import argparse import json import os import shutil from pathlib import Path ROOT = Path(__file__).resolve().parents[2] RES = ROOT / "experiments/results/phase2" KEEP = ROOT / "experiments/results/keep" def link(src: Path, dst: Path) -> None: if dst.exists(): return dst.parent.mkdir(parents=True, exist_ok=True) try: os.link(src, dst) except OSError: # another filesystem: fall back to a real copy shutil.copy2(src, dst) def keep_run(run: Path, per_task: int) -> str | None: evals = sorted(p for p in run.glob("eval_*") if p.is_dir()) if not evals or not all((e / "eval_info.json").exists() for e in evals): return None # still running, or never evaluated out = KEEP / run.name model = run / "train/checkpoints/last/pretrained_model" if not model.exists(): # G0/G1 came back from Modal without the 'last' symlink steps = sorted((run / "train/checkpoints").glob("[0-9]*/pretrained_model")) model = steps[-1] if steps else None if model: for f in model.rglob("*"): if f.is_file(): link(f, out / "model" / f.relative_to(model)) n_vid = 0 for e in evals: suite = e.name.removeprefix("eval_") link(e / "eval_info.json", out / f"{suite}_eval_info.json") if (e / "task_descriptions.json").exists(): link(e / "task_descriptions.json", out / f"{suite}_task_descriptions.json") for t in json.loads((e / "eval_info.json").read_text())["per_task"]: m = t["metrics"] for want, tag in ((True, "success"), (False, "failure")): picked = [k for k, s in enumerate(m["successes"]) if s == want][:per_task] for k in picked: # recorded paths may be from another machine (Modal: /repo/...); keep only the tail tail = Path(m["video_paths"][k]).parts[-2:] src = e / "videos" / Path(*tail) if src.exists(): link(src, out / "videos" / suite / f"task{t['task_id']}_{tag}_ep{k}.mp4") n_vid += 1 if (run / "run.json").exists(): link(run / "run.json", out / "run.json") return f"{run.name}: model={'yes' if model else 'no (eval-only run)'}, videos={n_vid}" def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--per-task", type=int, default=1, help="successes and failures kept per task") a = ap.parse_args() for run in sorted(p for p in RES.iterdir() if p.is_dir() and "smoke" not in p.name and not p.name.startswith("_")): msg = keep_run(run, a.per_task) print(msg or f"{run.name}: skipped (evaluations not finished)") if __name__ == "__main__": main()