File size: 2,119 Bytes
7a35d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Turn the pick demonstrations from tools/gen_demos.mjs into a LeRobot dataset (no images).

usage:
    python train/make_dataset.py demos/pick_tabletop.jsonl data/xlerobot_sim_pick [--repo-id local/xlerobot_sim_pick]
"""

import argparse
import json
import shutil
from pathlib import Path

import numpy as np
from lerobot.datasets.lerobot_dataset import LeRobotDataset

TASK = "Pick up the object from the table."


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    parser.add_argument("demos", type=Path)
    parser.add_argument("root", type=Path)
    parser.add_argument("--repo-id", default="local/xlerobot_sim_pick")
    args = parser.parse_args()

    spec = json.loads(args.demos.with_suffix("").with_suffix(".features.json").read_text())
    names = spec["features"]
    features = {
        "observation.state": {"dtype": "float32", "shape": (len(names["state"]),), "names": names["state"]},
        "observation.environment_state": {"dtype": "float32", "shape": (len(names["environment"]),), "names": names["environment"]},
        "action": {"dtype": "float32", "shape": (len(names["action"]),), "names": names["action"]},
    }
    if args.root.exists():
        shutil.rmtree(args.root)
    dataset = LeRobotDataset.create(args.repo_id, fps=spec["fps"], features=features, root=args.root, robot_type="xlerobot", use_videos=False)
    episodes = 0
    with args.demos.open() as f:
        for line in f:
            ep = json.loads(line)
            for s, e, a in zip(ep["state"], ep["environment"], ep["action"]):
                dataset.add_frame({
                    "observation.state": np.asarray(s, dtype=np.float32),
                    "observation.environment_state": np.asarray(e, dtype=np.float32),
                    "action": np.asarray(a, dtype=np.float32),
                    "task": TASK,
                })
            dataset.save_episode()
            episodes += 1
    dataset.finalize()
    print(f"{episodes} episodes, {dataset.meta.total_frames} frames at {spec['fps']} fps in {args.root}")


if __name__ == "__main__":
    main()