Upload prep_lerobot.py with huggingface_hub
Browse files- prep_lerobot.py +80 -0
prep_lerobot.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""prep_lerobot.py — bridge fd-studio's flat transform output to the layout to_lerobot_v21 +
|
| 3 |
+
multiview expect, then those scripts run unmodified.
|
| 4 |
+
|
| 5 |
+
fd-studio's transform writes /workspace/transform/retargeted/clip_file-XXXXXX/retargeted.hdf5
|
| 6 |
+
(flat, one dir per episode; the hdf5 carries attrs task=<category> and clip="file-XXXXXX").
|
| 7 |
+
to_lerobot_v21.discover() wants /workspace/retargeted/<cat>/clip_file-FFF_epN/retargeted.hdf5 and
|
| 8 |
+
finds the ego video at <EGO>/<cat>/videos/observation.images.camera/chunk-CCC/file-FFF.mp4.
|
| 9 |
+
|
| 10 |
+
This script, per retargeted clip:
|
| 11 |
+
• re-derives the source (chunk, file) via transform_batch._episode_range (the same mapping the
|
| 12 |
+
transform used), so file-FFF matches the ego data+video file index,
|
| 13 |
+
• symlinks the hdf5 into /workspace/retargeted/<cat>/clip_file-FFF_epN/,
|
| 14 |
+
• downloads that clip's ego VIDEO into the cache (transform only fetched keypoints).
|
| 15 |
+
EGO for to_lerobot is then just the cache's repo dir (already <cat>/videos + <cat>/meta).
|
| 16 |
+
|
| 17 |
+
Runs in the retarget venv (h5py). Usage:
|
| 18 |
+
python prep_lerobot.py --flat /workspace/transform/retargeted --cache /workspace/ego_cache \
|
| 19 |
+
[--retgt-out /workspace/retargeted]
|
| 20 |
+
Prints a JSON line: {"clips": N, "retgt": ..., "ego": ...}.
|
| 21 |
+
"""
|
| 22 |
+
import argparse
|
| 23 |
+
import glob
|
| 24 |
+
import json
|
| 25 |
+
import os
|
| 26 |
+
import sys
|
| 27 |
+
from pathlib import Path
|
| 28 |
+
|
| 29 |
+
import h5py
|
| 30 |
+
|
| 31 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 32 |
+
import transform_batch as tb # noqa: E402 (reuse _episode_range/_ensure/REPO/PREFIX)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def main():
|
| 36 |
+
ap = argparse.ArgumentParser()
|
| 37 |
+
ap.add_argument("--flat", required=True, help="/workspace/transform/retargeted")
|
| 38 |
+
ap.add_argument("--cache", required=True, help="/workspace/ego_cache")
|
| 39 |
+
ap.add_argument("--retgt-out", default="/workspace/retargeted")
|
| 40 |
+
a = ap.parse_args()
|
| 41 |
+
|
| 42 |
+
cache = Path(a.cache)
|
| 43 |
+
ego_root = cache / tb.REPO.replace("/", "__") # to_lerobot EGO = this (<cat>/videos + meta)
|
| 44 |
+
done, vids, skipped = 0, 0, 0
|
| 45 |
+
|
| 46 |
+
for hp in sorted(glob.glob(f"{a.flat}/*/retargeted.hdf5")):
|
| 47 |
+
try:
|
| 48 |
+
with h5py.File(hp, "r") as h:
|
| 49 |
+
cat = str(h.attrs["task"])
|
| 50 |
+
ep = int(str(h.attrs["clip"]).split("-")[1])
|
| 51 |
+
r = tb._episode_range(cache, cat, ep)
|
| 52 |
+
ci, fi = r["data/chunk_index"], r["data/file_index"]
|
| 53 |
+
fyy = f"file-{fi:03d}"
|
| 54 |
+
|
| 55 |
+
# 1) retargeted → category layout with the file-FFF_epN name to_lerobot parses
|
| 56 |
+
dst = Path(a.retgt_out) / cat / f"clip_{fyy}_ep{ep}"
|
| 57 |
+
dst.mkdir(parents=True, exist_ok=True)
|
| 58 |
+
link = dst / "retargeted.hdf5"
|
| 59 |
+
if link.is_symlink() or link.exists():
|
| 60 |
+
link.unlink()
|
| 61 |
+
os.symlink(os.path.abspath(hp), link)
|
| 62 |
+
|
| 63 |
+
# 2) download this clip's ego video (into cache, same repo layout)
|
| 64 |
+
vid_rel = f"{tb.PREFIX}{cat}/videos/observation.images.camera/chunk-{ci:03d}/{fyy}.mp4"
|
| 65 |
+
try:
|
| 66 |
+
tb._ensure(cache, vid_rel)
|
| 67 |
+
vids += 1
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"[warn] video dl {vid_rel}: {e}", file=sys.stderr)
|
| 70 |
+
done += 1
|
| 71 |
+
except Exception as e:
|
| 72 |
+
skipped += 1
|
| 73 |
+
print(f"[warn] {hp}: {e}", file=sys.stderr)
|
| 74 |
+
|
| 75 |
+
print(json.dumps({"clips": done, "videos": vids, "skipped": skipped,
|
| 76 |
+
"retgt": a.retgt_out, "ego": str(ego_root)}))
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
main()
|