fdstudio-scripts / subset_lerobot.py
Kavin60606's picture
Upload subset_lerobot.py with huggingface_hub
3764de1 verified
Raw History Blame Contribute Delete
2.16 kB
#!/usr/bin/env python3
"""subset_lerobot.py — download the first N episodes of a LeRobot v2.1 dataset + patch its meta,
so a big dataset (e.g. abc-teleop = 6,586 files) becomes a tiny, fast, still-valid local dataset
for a minimal mixture experiment. LeRobot loads it from --out (local root) → no version tag needed.
Usage: python subset_lerobot.py --repo angkul07/abc-teleop --out /workspace/data/yam7h/teleop --n 8
"""
import argparse
import json
import os
from huggingface_hub import snapshot_download
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--repo", required=True)
ap.add_argument("--out", required=True)
ap.add_argument("--n", type=int, default=8)
ap.add_argument("--token", default=os.environ.get("HF_TOKEN"))
a = ap.parse_args()
pats = [f"episode_{i:06d}" for i in range(a.n)]
allow = ["meta/*"] + [f"data/**/{p}.parquet" for p in pats] + [f"videos/**/{p}.mp4" for p in pats]
snapshot_download(a.repo, repo_type="dataset", local_dir=a.out, allow_patterns=allow, token=a.token)
meta = os.path.join(a.out, "meta")
eps = [json.loads(l) for l in open(os.path.join(meta, "episodes.jsonl"))][: a.n]
with open(os.path.join(meta, "episodes.jsonl"), "w") as f:
for e in eps:
f.write(json.dumps(e) + "\n")
stpath = os.path.join(meta, "episodes_stats.jsonl")
if os.path.exists(stpath):
st = [json.loads(l) for l in open(stpath)][: a.n]
with open(stpath, "w") as f:
for s in st:
f.write(json.dumps(s) + "\n")
info = json.load(open(os.path.join(meta, "info.json")))
vkeys = [k for k, v in (info.get("features") or {}).items()
if isinstance(v, dict) and v.get("dtype") == "video"]
info["total_episodes"] = a.n
info["total_frames"] = sum(int(e.get("length", 0)) for e in eps)
info["total_videos"] = a.n * len(vkeys)
info["splits"] = {"train": f"0:{a.n}"}
json.dump(info, open(os.path.join(meta, "info.json"), "w"), indent=4)
print(f"subset {a.repo} -> {a.out}: {a.n} episodes, {info['total_frames']} frames, {len(vkeys)} cams")
if __name__ == "__main__":
main()