File size: 8,254 Bytes
4701daf d823377 4701daf d823377 |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
"""
Minimal example: convert dataset to the LeRobot format.
CLI Example (using the *arrange_flowers* task as an example):
python convert_libero_to_lerobot.py \
--repo-name arrange_flowers_repo \
--raw-dataset /path/to/arrange_flowers \
--frame-interval 1 \
Notes:
- If you plan to push to the Hugging Face Hub later, handle that outside this script.
"""
import argparse
import json
import shutil
from pathlib import Path
from typing import Any, Dict, List
import cv2
import numpy as np
from lerobot.common.datasets.lerobot_dataset import LEROBOT_HOME, LeRobotDataset
def load_jsonl(path: Path) -> List[Dict[str, Any]]:
"""Load a JSONL file into a list of dicts."""
with path.open("r", encoding="utf-8") as f:
return [json.loads(line) for line in f]
def create_lerobot_dataset(
repo_name: str,
robot_type: str,
fps: float,
height: int,
width: int,
) -> LeRobotDataset:
"""
Create a LeRobot dataset with custom feature schema
"""
dataset = LeRobotDataset.create(
repo_id=repo_name,
robot_type=robot_type,
fps=fps,
features={
"global_image": {
"dtype": "image",
"shape": (height, width, 3),
"names": ["height", "width", "channel"],
},
"wrist_image": {
"dtype": "image",
"shape": (height, width, 3),
"names": ["height", "width", "channel"],
},
"right_image": {
"dtype": "image",
"shape": (height, width, 3),
"names": ["height", "width", "channel"],
},
"state": {
"dtype": "float32",
"shape": (7,), # for ee_pose and gripper width
"names": ["state"],
},
"actions": {
"dtype": "float32",
"shape": (7,), # for ee_pose and gripper width
"names": ["actions"],
},
},
image_writer_threads=32,
image_writer_processes=16,
)
return dataset
def process_episode_dir(
episode_path: Path,
dataset: LeRobotDataset,
frame_interval: int,
prompt: str,
) -> None:
"""
Process a single episode directory and append frames to the given dataset.
episode_path : Path
Episode directory containing `states/states.jsonl` and `videos/*.mp4`.
dataset : LeRobotDataset
Target dataset to which frames are added.
frame_interval : int
Sampling stride (>=1).
prompt : str
Language instruction of this episode.
"""
# Modify if your dataset consists of bimanual data.
states_path = episode_path / "states" / "states.jsonl"
videos_dir = episode_path / "videos"
ep_states = load_jsonl(states_path)
# adjust them to match your dataset’s actual naming.
wrist_video = cv2.VideoCapture(str(videos_dir / "arm_realsense_rgb.mp4"))
global_video = cv2.VideoCapture(str(videos_dir / "global_realsense_rgb.mp4"))
right_video = cv2.VideoCapture(str(videos_dir / "right_realsense_rgb.mp4"))
wrist_frames_count = int(wrist_video.get(cv2.CAP_PROP_FRAME_COUNT))
global_frames_count = int(global_video.get(cv2.CAP_PROP_FRAME_COUNT))
right_frames_count = int(right_video.get(cv2.CAP_PROP_FRAME_COUNT))
n_states = len(ep_states)
# assert all lengths match
assert (
n_states == wrist_frames_count == global_frames_count == right_frames_count
), (
f"Mismatch in episode {episode_path.name}: "
f"states={n_states}, wrist={wrist_frames_count}, "
f"global={global_frames_count}, right={right_frames_count}"
)
# write frames to the episode of lerobot dataset
for idx in range(frame_interval, n_states, frame_interval):
# Build pose
pose = np.concatenate(
(np.asarray(ep_states[idx]["end_effector_pose"]), [ep_states[idx]["gripper_width"]])
)
last_pose = np.concatenate(
(np.asarray(ep_states[idx - frame_interval]["end_effector_pose"]),
[ep_states[idx - frame_interval]["gripper_width"]])
)
# Read frames && BGR -> RGB
# Resize as needed, but update the LeRobot feature shape accordingly.
_, wrist_image = wrist_video.read()
_, global_image = global_video.read()
_, right_image = right_video.read()
wrist_image = cv2.cvtColor(wrist_image, cv2.COLOR_BGR2RGB)
global_image = cv2.cvtColor(global_image, cv2.COLOR_BGR2RGB)
right_image = cv2.cvtColor(right_image, cv2.COLOR_BGR2RGB)
dataset.add_frame(
{
"global_image": global_image,
"wrist_image": wrist_image,
"right_image": right_image,
"state": last_pose.astype(np.float32, copy=False),
"actions": pose.astype(np.float32, copy=False),
}
)
wrist_video.release()
global_video.release()
right_video.release()
dataset.save_episode(task=prompt)
def main(
repo_name: str,
raw_dataset: Path,
frame_interval: int = 1,
overwrite_repo: bool = False,
) -> None:
"""
Convert a dataset directory into LeRobot format.
repo_name : str
Output repo/dataset name (saved under $LEROBOT_HOME / repo_name).
raw_dataset : Path
Path to the raw dataset root directory.
frame_interval : int, default=1
Sample every N frames (kept identical).
overwrite_repo : bool, default=False
If True, remove the existing dataset directory before writing.
"""
assert frame_interval >= 1, "frame_interval must be >= 1"
# overwrite repo
dst_dir = LEROBOT_HOME / repo_name
if overwrite_repo and dst_dir.exists():
print(f"removing existing dataset at {dst_dir}")
shutil.rmtree(dst_dir)
# Load task_infos
task_info_path = raw_dataset / "meta" / "task_info.json"
with task_info_path.open("r", encoding="utf-8") as f:
task_info = json.load(f)
robot_type = task_info["task_desc"]["task_tag"][2] # "ARX5"
video_info = task_info["video_info"]
video_info["width"] = 640 # TODO: derive from task_info or actual videos
video_info["height"] = 480
fps = float(video_info["fps"])
prompt = task_info["task_desc"]["prompt"]
# Create dataset, define feature in the form you need.
# - proprio is stored in `state` and actions in `action`
# - LeRobot assumes that dtype of image data is `image`
dataset = create_lerobot_dataset(
repo_name=repo_name,
robot_type=robot_type,
fps=fps,
height=video_info["height"],
width=video_info["width"],
)
# populate the dataset to lerobot dataset
data_root = raw_dataset / "data"
for episode_path in data_root.iterdir():
if not episode_path.is_dir():
continue
print(f"Processing episode: {episode_path.name}")
process_episode_dir(
episode_path=episode_path,
dataset=dataset,
frame_interval=frame_interval,
prompt=prompt,
)
dataset.consolidate(run_compute_stats=False)
print("Done. Dataset saved to: {dst_dir}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert a custom dataset to LeRobot format."
)
parser.add_argument(
"--repo-name",
required=True,
help="Name of the output dataset (under $LEROBOT_HOME).",
)
parser.add_argument(
"--raw-dataset",
required=True,
type=str,
help="Path to the raw dataset root.",
)
parser.add_argument(
"--frame-interval",
type=int,
default=1,
help="Sample every N frames. Default: 1",
)
parser.add_argument(
"--overwrite-repo",
action="store_true",
help="Remove existing output directory if it exists.",
)
args = parser.parse_args()
main(
repo_name=args.repo_name,
raw_dataset=Path(args.raw_dataset),
frame_interval=args.frame_interval,
overwrite_repo=args.overwrite_repo,
)
|