|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| """
|
| Records a dataset. Actions for the robot can be either generated by teleoperation or by a policy.
|
|
|
| Example:
|
|
|
| ```shell
|
| lerobot-record \
|
| --robot.type=so100_follower \
|
| --robot.port=/dev/tty.usbmodem58760431541 \
|
| --robot.cameras="{laptop: {type: opencv, index_or_path: 0, width: 640, height: 480, fps: 30}}" \
|
| --robot.id=black \
|
| --dataset.repo_id=<my_username>/<my_dataset_name> \
|
| --dataset.num_episodes=2 \
|
| --dataset.single_task="Grab the cube" \
|
| --display_data=true
|
| # <- Teleop optional if you want to teleoperate to record or in between episodes with a policy \
|
| # --teleop.type=so100_leader \
|
| # --teleop.port=/dev/tty.usbmodem58760431551 \
|
| # --teleop.id=blue \
|
| # <- Policy optional if you want to record with a policy \
|
| # --policy.path=${HF_USER}/my_policy \
|
| ```
|
|
|
| Example recording with bimanual so100:
|
| ```shell
|
| lerobot-record \
|
| --robot.type=bi_so100_follower \
|
| --robot.left_arm_port=/dev/tty.usbmodem5A460851411 \
|
| --robot.right_arm_port=/dev/tty.usbmodem5A460812391 \
|
| --robot.id=bimanual_follower \
|
| --robot.cameras='{
|
| left: {"type": "opencv", "index_or_path": 0, "width": 640, "height": 480, "fps": 30},
|
| top: {"type": "opencv", "index_or_path": 1, "width": 640, "height": 480, "fps": 30},
|
| right: {"type": "opencv", "index_or_path": 2, "width": 640, "height": 480, "fps": 30}
|
| }' \
|
| --teleop.type=bi_so100_leader \
|
| --teleop.left_arm_port=/dev/tty.usbmodem5A460828611 \
|
| --teleop.right_arm_port=/dev/tty.usbmodem5A460826981 \
|
| --teleop.id=bimanual_leader \
|
| --display_data=true \
|
| --dataset.repo_id=${HF_USER}/bimanual-so100-handover-cube \
|
| --dataset.num_episodes=25 \
|
| --dataset.single_task="Grab and handover the red cube to the other arm"
|
| ```
|
| """
|
|
|
| import logging
|
| import time
|
| from dataclasses import asdict, dataclass, field
|
| from pathlib import Path
|
| from pprint import pformat
|
| from typing import Any
|
|
|
| from lerobot.cameras import (
|
| CameraConfig,
|
| )
|
| from lerobot.cameras.opencv.configuration_opencv import OpenCVCameraConfig
|
| from lerobot.cameras.realsense.configuration_realsense import RealSenseCameraConfig
|
| from lerobot.configs import parser
|
| from lerobot.configs.policies import PreTrainedConfig
|
| from lerobot.datasets.image_writer import safe_stop_image_writer
|
| from lerobot.datasets.lerobot_dataset import LeRobotDataset
|
| from lerobot.datasets.pipeline_features import aggregate_pipeline_dataset_features, create_initial_features
|
| from lerobot.datasets.utils import build_dataset_frame, combine_feature_dicts
|
| from lerobot.datasets.video_utils import VideoEncodingManager
|
| from lerobot.policies.factory import make_policy, make_pre_post_processors
|
| from lerobot.policies.pretrained import PreTrainedPolicy
|
| from lerobot.policies.utils import make_robot_action
|
| from lerobot.processor import (
|
| PolicyAction,
|
| PolicyProcessorPipeline,
|
| RobotAction,
|
| RobotObservation,
|
| RobotProcessorPipeline,
|
| make_default_processors,
|
| )
|
| from lerobot.processor.rename_processor import rename_stats
|
| from lerobot.robots import (
|
| Robot,
|
| RobotConfig,
|
| bi_so100_follower,
|
| hope_jr,
|
| koch_follower,
|
| make_robot_from_config,
|
| so100_follower,
|
| so101_follower,
|
| )
|
| from lerobot.teleoperators import (
|
| Teleoperator,
|
| TeleoperatorConfig,
|
| bi_so100_leader,
|
| homunculus,
|
| koch_leader,
|
| make_teleoperator_from_config,
|
| so100_leader,
|
| so101_leader,
|
| )
|
| from lerobot.teleoperators.keyboard.teleop_keyboard import KeyboardTeleop
|
| from lerobot.utils.constants import ACTION, OBS_STR
|
| from lerobot.utils.control_utils import (
|
| init_keyboard_listener,
|
| is_headless,
|
| predict_action,
|
| sanity_check_dataset_name,
|
| sanity_check_dataset_robot_compatibility,
|
| )
|
| from lerobot.utils.import_utils import register_third_party_devices
|
| from lerobot.utils.robot_utils import busy_wait
|
| from lerobot.utils.utils import (
|
| get_safe_torch_device,
|
| init_logging,
|
| log_say,
|
| )
|
| from lerobot.utils.visualization_utils import init_rerun, log_rerun_data
|
|
|
|
|
| @dataclass
|
| class DatasetRecordConfig:
|
|
|
| repo_id: str
|
|
|
| single_task: str
|
|
|
| root: str | Path | None = None
|
|
|
| fps: int = 30
|
|
|
| episode_time_s: int | float = 60
|
|
|
| reset_time_s: int | float = 60
|
|
|
| num_episodes: int = 50
|
|
|
| video: bool = True
|
|
|
| push_to_hub: bool = True
|
|
|
| private: bool = False
|
|
|
| tags: list[str] | None = None
|
|
|
|
|
|
|
|
|
| num_image_writer_processes: int = 0
|
|
|
|
|
|
|
| num_image_writer_threads_per_camera: int = 4
|
|
|
|
|
| video_encoding_batch_size: int = 1
|
|
|
| rename_map: dict[str, str] = field(default_factory=dict)
|
|
|
| def __post_init__(self):
|
| if self.single_task is None:
|
| raise ValueError("You need to provide a task as argument in `single_task`.")
|
|
|
|
|
| @dataclass
|
| class RecordConfig:
|
| robot: RobotConfig
|
| dataset: DatasetRecordConfig
|
|
|
| teleop: TeleoperatorConfig | None = None
|
|
|
| policy: PreTrainedConfig | None = None
|
|
|
| display_data: bool = False
|
|
|
| play_sounds: bool = True
|
|
|
| resume: bool = False
|
|
|
| def __post_init__(self):
|
|
|
| policy_path = parser.get_path_arg("policy")
|
| if policy_path:
|
| cli_overrides = parser.get_cli_overrides("policy")
|
| self.policy = PreTrainedConfig.from_pretrained(policy_path, cli_overrides=cli_overrides)
|
| self.policy.pretrained_path = policy_path
|
|
|
| if self.teleop is None and self.policy is None:
|
| raise ValueError("Choose a policy, a teleoperator or both to control the robot")
|
|
|
| @classmethod
|
| def __get_path_fields__(cls) -> list[str]:
|
| """This enables the parser to load config from the policy using `--policy.path=local/dir`"""
|
| return ["policy"]
|
|
|
|
|
| """ --------------- record_loop() data flow --------------------------
|
| [ Robot ]
|
| V
|
| [ robot.get_observation() ] ---> raw_obs
|
| V
|
| [ robot_observation_processor ] ---> processed_obs
|
| V
|
| .-----( ACTION LOGIC )------------------.
|
| V V
|
| [ From Teleoperator ] [ From Policy ]
|
| | |
|
| | [teleop.get_action] -> raw_action | [predict_action]
|
| | | | |
|
| | V | V
|
| | [teleop_action_processor] | |
|
| | | | |
|
| '---> processed_teleop_action '---> processed_policy_action
|
| | |
|
| '-------------------------.-------------'
|
| V
|
| [ robot_action_processor ] --> robot_action_to_send
|
| V
|
| [ robot.send_action() ] -- (Robot Executes)
|
| V
|
| ( Save to Dataset )
|
| V
|
| ( Rerun Log / Loop Wait )
|
| """
|
|
|
|
|
| @safe_stop_image_writer
|
| def record_loop(
|
| robot: Robot,
|
| events: dict,
|
| fps: int,
|
| teleop_action_processor: RobotProcessorPipeline[
|
| tuple[RobotAction, RobotObservation], RobotAction
|
| ],
|
| robot_action_processor: RobotProcessorPipeline[
|
| tuple[RobotAction, RobotObservation], RobotAction
|
| ],
|
| robot_observation_processor: RobotProcessorPipeline[
|
| RobotObservation, RobotObservation
|
| ],
|
| dataset: LeRobotDataset | None = None,
|
| teleop: Teleoperator | list[Teleoperator] | None = None,
|
| policy: PreTrainedPolicy | None = None,
|
| preprocessor: PolicyProcessorPipeline[dict[str, Any], dict[str, Any]] | None = None,
|
| postprocessor: PolicyProcessorPipeline[PolicyAction, PolicyAction] | None = None,
|
| control_time_s: int | None = None,
|
| single_task: str | None = None,
|
| display_data: bool = False,
|
| ):
|
| if dataset is not None and dataset.fps != fps:
|
| raise ValueError(f"The dataset fps should be equal to requested fps ({dataset.fps} != {fps}).")
|
|
|
| teleop_arm = teleop_keyboard = None
|
| if isinstance(teleop, list):
|
| teleop_keyboard = next((t for t in teleop if isinstance(t, KeyboardTeleop)), None)
|
| teleop_arm = next(
|
| (
|
| t
|
| for t in teleop
|
| if isinstance(
|
| t,
|
| (so100_leader.SO100Leader | so101_leader.SO101Leader | koch_leader.KochLeader),
|
| )
|
| ),
|
| None,
|
| )
|
|
|
| if not (teleop_arm and teleop_keyboard and len(teleop) == 2 and robot.name == "lekiwi_client"):
|
| raise ValueError(
|
| "For multi-teleop, the list must contain exactly one KeyboardTeleop and one arm teleoperator. Currently only supported for LeKiwi robot."
|
| )
|
|
|
|
|
| if policy is not None and preprocessor is not None and postprocessor is not None:
|
| policy.reset()
|
| preprocessor.reset()
|
| postprocessor.reset()
|
|
|
| timestamp = 0
|
| start_episode_t = time.perf_counter()
|
| while timestamp < control_time_s:
|
| start_loop_t = time.perf_counter()
|
|
|
| if events["exit_early"]:
|
| events["exit_early"] = False
|
| break
|
|
|
|
|
| obs = robot.get_observation()
|
|
|
|
|
| obs_processed = robot_observation_processor(obs)
|
|
|
| if policy is not None or dataset is not None:
|
| observation_frame = build_dataset_frame(dataset.features, obs_processed, prefix=OBS_STR)
|
|
|
|
|
| if policy is not None and preprocessor is not None and postprocessor is not None:
|
| action_values = predict_action(
|
| observation=observation_frame,
|
| policy=policy,
|
| device=get_safe_torch_device(policy.config.device),
|
| preprocessor=preprocessor,
|
| postprocessor=postprocessor,
|
| use_amp=policy.config.use_amp,
|
| task=single_task,
|
| robot_type=robot.robot_type,
|
| )
|
|
|
| act_processed_policy: RobotAction = make_robot_action(action_values, dataset.features)
|
|
|
| elif policy is None and isinstance(teleop, Teleoperator):
|
| act = teleop.get_action()
|
|
|
|
|
| act_processed_teleop = teleop_action_processor((act, obs))
|
|
|
| elif policy is None and isinstance(teleop, list):
|
| arm_action = teleop_arm.get_action()
|
| arm_action = {f"arm_{k}": v for k, v in arm_action.items()}
|
| keyboard_action = teleop_keyboard.get_action()
|
| base_action = robot._from_keyboard_to_base_action(keyboard_action)
|
| act = {**arm_action, **base_action} if len(base_action) > 0 else arm_action
|
| act_processed_teleop = teleop_action_processor((act, obs))
|
| else:
|
| logging.info(
|
| "No policy or teleoperator provided, skipping action generation."
|
| "This is likely to happen when resetting the environment without a teleop device."
|
| "The robot won't be at its rest position at the start of the next episode."
|
| )
|
| continue
|
|
|
|
|
| if policy is not None and act_processed_policy is not None:
|
| action_values = act_processed_policy
|
| robot_action_to_send = robot_action_processor((act_processed_policy, obs))
|
| else:
|
| action_values = act_processed_teleop
|
| robot_action_to_send = robot_action_processor((act_processed_teleop, obs))
|
|
|
|
|
|
|
|
|
|
|
| _sent_action = robot.send_action(robot_action_to_send)
|
|
|
|
|
| if dataset is not None:
|
| action_frame = build_dataset_frame(dataset.features, action_values, prefix=ACTION)
|
| frame = {**observation_frame, **action_frame, "task": single_task}
|
| dataset.add_frame(frame)
|
|
|
| if display_data:
|
| log_rerun_data(observation=obs_processed, action=action_values)
|
|
|
| dt_s = time.perf_counter() - start_loop_t
|
| busy_wait(1 / fps - dt_s)
|
|
|
| timestamp = time.perf_counter() - start_episode_t
|
|
|
|
|
| @parser.wrap()
|
| def record(cfg: RecordConfig) -> LeRobotDataset:
|
| init_logging()
|
| logging.info(pformat(asdict(cfg)))
|
| if cfg.display_data:
|
| init_rerun(session_name="recording")
|
|
|
| robot = make_robot_from_config(cfg.robot)
|
| teleop = make_teleoperator_from_config(cfg.teleop) if cfg.teleop is not None else None
|
|
|
| teleop_action_processor, robot_action_processor, robot_observation_processor = make_default_processors()
|
|
|
| dataset_features = combine_feature_dicts(
|
| aggregate_pipeline_dataset_features(
|
| pipeline=teleop_action_processor,
|
| initial_features=create_initial_features(
|
| action=robot.action_features
|
| ),
|
| use_videos=cfg.dataset.video,
|
| ),
|
| aggregate_pipeline_dataset_features(
|
| pipeline=robot_observation_processor,
|
| initial_features=create_initial_features(observation=robot.observation_features),
|
| use_videos=cfg.dataset.video,
|
| ),
|
| )
|
|
|
| if cfg.resume:
|
| dataset = LeRobotDataset(
|
| cfg.dataset.repo_id,
|
| root=cfg.dataset.root,
|
| batch_encoding_size=cfg.dataset.video_encoding_batch_size,
|
| )
|
|
|
| if hasattr(robot, "cameras") and len(robot.cameras) > 0:
|
| dataset.start_image_writer(
|
| num_processes=cfg.dataset.num_image_writer_processes,
|
| num_threads=cfg.dataset.num_image_writer_threads_per_camera * len(robot.cameras),
|
| )
|
| sanity_check_dataset_robot_compatibility(dataset, robot, cfg.dataset.fps, dataset_features)
|
| else:
|
|
|
| sanity_check_dataset_name(cfg.dataset.repo_id, cfg.policy)
|
| dataset = LeRobotDataset.create(
|
| cfg.dataset.repo_id,
|
| cfg.dataset.fps,
|
| root=cfg.dataset.root,
|
| robot_type=robot.name,
|
| features=dataset_features,
|
| use_videos=cfg.dataset.video,
|
| image_writer_processes=cfg.dataset.num_image_writer_processes,
|
| image_writer_threads=cfg.dataset.num_image_writer_threads_per_camera * len(robot.cameras),
|
| batch_encoding_size=cfg.dataset.video_encoding_batch_size,
|
| )
|
|
|
|
|
| policy = None if cfg.policy is None else make_policy(cfg.policy, ds_meta=dataset.meta)
|
| preprocessor = None
|
| postprocessor = None
|
| if cfg.policy is not None:
|
| preprocessor, postprocessor = make_pre_post_processors(
|
| policy_cfg=cfg.policy,
|
| pretrained_path=cfg.policy.pretrained_path,
|
| dataset_stats=rename_stats(dataset.meta.stats, cfg.dataset.rename_map),
|
| preprocessor_overrides={
|
| "device_processor": {"device": cfg.policy.device},
|
| "rename_observations_processor": {"rename_map": cfg.dataset.rename_map},
|
| },
|
| )
|
|
|
| robot.connect()
|
| if teleop is not None:
|
| teleop.connect()
|
|
|
| listener, events = init_keyboard_listener()
|
|
|
| with VideoEncodingManager(dataset):
|
| recorded_episodes = 0
|
| while recorded_episodes < cfg.dataset.num_episodes and not events["stop_recording"]:
|
| log_say(f"Recording episode {dataset.num_episodes}", cfg.play_sounds)
|
| record_loop(
|
| robot=robot,
|
| events=events,
|
| fps=cfg.dataset.fps,
|
| teleop_action_processor=teleop_action_processor,
|
| robot_action_processor=robot_action_processor,
|
| robot_observation_processor=robot_observation_processor,
|
| teleop=teleop,
|
| policy=policy,
|
| preprocessor=preprocessor,
|
| postprocessor=postprocessor,
|
| dataset=dataset,
|
| control_time_s=cfg.dataset.episode_time_s,
|
| single_task=cfg.dataset.single_task,
|
| display_data=cfg.display_data,
|
| )
|
|
|
|
|
|
|
| if not events["stop_recording"] and (
|
| (recorded_episodes < cfg.dataset.num_episodes - 1) or events["rerecord_episode"]
|
| ):
|
| log_say("Reset the environment", cfg.play_sounds)
|
| record_loop(
|
| robot=robot,
|
| events=events,
|
| fps=cfg.dataset.fps,
|
| teleop_action_processor=teleop_action_processor,
|
| robot_action_processor=robot_action_processor,
|
| robot_observation_processor=robot_observation_processor,
|
| teleop=teleop,
|
| control_time_s=cfg.dataset.reset_time_s,
|
| single_task=cfg.dataset.single_task,
|
| display_data=cfg.display_data,
|
| )
|
|
|
| if events["rerecord_episode"]:
|
| log_say("Re-record episode", cfg.play_sounds)
|
| events["rerecord_episode"] = False
|
| events["exit_early"] = False
|
| dataset.clear_episode_buffer()
|
| continue
|
|
|
| dataset.save_episode()
|
| recorded_episodes += 1
|
|
|
| log_say("Stop recording", cfg.play_sounds, blocking=True)
|
|
|
| robot.disconnect()
|
| if teleop is not None:
|
| teleop.disconnect()
|
|
|
| if not is_headless() and listener is not None:
|
| listener.stop()
|
|
|
| if cfg.dataset.push_to_hub:
|
| dataset.push_to_hub(tags=cfg.dataset.tags, private=cfg.dataset.private)
|
|
|
| log_say("Exiting", cfg.play_sounds)
|
| return dataset
|
|
|
|
|
| def main():
|
| register_third_party_devices()
|
| record()
|
|
|
|
|
| if __name__ == "__main__":
|
| main()
|
|
|