File size: 9,249 Bytes
af83d87 | 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | #!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Convert conflict_maniskill datasets (parquet + embedded PNG bytes, no videos)
into GR00T-compatible LeRobot v2 format:
- Extract embedded PNG bytes -> MP4 videos (one per camera per episode)
- Rename parquet columns to standard names (observation.state, action)
- Add annotation column for language (annotation.human.task_description)
- Write modality.json
- Update info.json with video counts
- Copy meta files (episodes.jsonl, tasks.jsonl) as-is
Output per category:
<output_root>/<category>/ <- groot-compatible LeRobot v2 dataset
Usage:
python prepare_conflict_data.py \
--src_root /lustre/.../conflict_maniskill/demo_conflict \
--out_root /lustre/.../groot17_data \
--categories color_object color_size ...
[--num_demos 300]
"""
import argparse
import io
import json
import shutil
from pathlib import Path
import av
import numpy as np
import pandas as pd
from PIL import Image
from tqdm import tqdm
CATEGORIES = [
"color_object",
"color_size",
"color_spatial",
"size_object",
"spatial_object",
"spatial_size",
"verb_color",
"verb_object",
"verb_size",
"verb_spatial",
]
# Panda arm: 7 joints + 1 gripper = 8-dim state/action
# state dims 0-6: joint positions, dim 7: gripper width
MODALITY_JSON = {
"state": {
"arm": {"start": 0, "end": 7},
"gripper": {"start": 7, "end": 8},
},
"action": {
"arm": {"start": 0, "end": 7},
"gripper": {"start": 7, "end": 8},
},
"video": {
"image": {"original_key": "observation.images.image"},
"wrist_image": {"original_key": "observation.images.wrist_image"},
},
"annotation": {
"human.task_description": {"original_key": "annotation.human.task_description"},
},
}
def decode_image(cell) -> np.ndarray:
"""Decode a parquet image cell (dict with 'bytes' key or raw bytes) -> uint8 HWC numpy."""
if isinstance(cell, dict):
raw = cell["bytes"]
elif isinstance(cell, bytes):
raw = cell
else:
raise ValueError(f"Unknown image cell type: {type(cell)}")
img = Image.open(io.BytesIO(raw)).convert("RGB")
return np.array(img, dtype=np.uint8)
def frames_to_mp4(frames: list[np.ndarray], out_path: Path, fps: int = 30) -> None:
"""Write a list of HWC uint8 numpy frames as an H264 MP4."""
out_path.parent.mkdir(parents=True, exist_ok=True)
h, w, _ = frames[0].shape
container = av.open(str(out_path), mode="w")
stream = container.add_stream("libx264", rate=fps)
stream.width = w
stream.height = h
stream.pix_fmt = "yuv420p"
stream.options = {"crf": "18", "preset": "fast"}
for frame_arr in frames:
frame = av.VideoFrame.from_ndarray(frame_arr, format="rgb24")
for packet in stream.encode(frame):
container.mux(packet)
for packet in stream.encode(None):
container.mux(packet)
container.close()
def prepare_category(
src_root: Path,
out_root: Path,
category: str,
num_demos: int,
fps: int = 30,
) -> None:
src_dataset = src_root / category / str(num_demos) / "huggingface_data" / category / "conflict"
out_dataset = out_root / category
print(f"\n{'='*60}")
print(f"Processing: {category} ({num_demos} demos)")
print(f" src: {src_dataset}")
print(f" out: {out_dataset}")
if not src_dataset.exists():
print(f" SKIP: source not found")
return
meta_src = src_dataset / "meta"
data_src = src_dataset / "data"
# Load metadata
with open(meta_src / "info.json") as f:
info = json.load(f)
with open(meta_src / "tasks.jsonl") as f:
tasks = [json.loads(line) for line in f if line.strip()]
task_map = {t["task_index"]: t["task"] for t in tasks}
with open(meta_src / "episodes.jsonl") as f:
episodes_meta = [json.loads(line) for line in f if line.strip()]
total_episodes = info["total_episodes"]
chunk_size = info["chunks_size"]
data_path_pattern = info["data_path"]
# Prepare output dirs
out_meta = out_dataset / "meta"
out_meta.mkdir(parents=True, exist_ok=True)
out_data = out_dataset / "data"
out_data.mkdir(parents=True, exist_ok=True)
total_videos_created = 0
# Process each episode
for ep_meta in tqdm(episodes_meta, desc=category):
ep_idx = ep_meta["episode_index"]
chunk_idx = ep_idx // chunk_size
# Load source parquet
src_parquet = src_dataset / data_path_pattern.format(
episode_chunk=chunk_idx, episode_index=ep_idx
)
df = pd.read_parquet(src_parquet)
# -- Build converted parquet --
new_df = pd.DataFrame()
new_df["observation.state"] = df["state"]
new_df["action"] = df["actions"]
new_df["timestamp"] = df["timestamp"]
new_df["frame_index"] = df["frame_index"]
new_df["episode_index"] = df["episode_index"]
new_df["index"] = df["index"]
new_df["task_index"] = df["task_index"]
# Add annotation column (task index -> same integer, loader looks up tasks.jsonl)
new_df["annotation.human.task_description"] = df["task_index"]
# Save converted parquet (same chunk structure)
out_chunk_dir = out_data / f"chunk-{chunk_idx:03d}"
out_chunk_dir.mkdir(parents=True, exist_ok=True)
out_parquet = out_chunk_dir / f"episode_{ep_idx:06d}.parquet"
new_df.to_parquet(out_parquet, index=False)
# -- Extract videos per camera --
n_frames = len(df)
for cam_key in ("image", "wrist_image"):
frames = [decode_image(df[cam_key].iloc[i]) for i in range(n_frames)]
vid_dir = (
out_dataset
/ "videos"
/ f"chunk-{chunk_idx:03d}"
/ f"observation.images.{cam_key}"
)
vid_path = vid_dir / f"episode_{ep_idx:06d}.mp4"
frames_to_mp4(frames, vid_path, fps=fps)
total_videos_created += 1
print(f" Created {total_videos_created} MP4 files")
# -- Write modality.json --
with open(out_meta / "modality.json", "w") as f:
json.dump(MODALITY_JSON, f, indent=4)
# -- Write updated info.json --
new_info = dict(info)
new_info["total_videos"] = total_videos_created
new_info["video_path"] = (
"videos/chunk-{episode_chunk:03d}/{video_key}/episode_{episode_index:06d}.mp4"
)
new_info["features"] = {
"observation.state": {
"dtype": "float32",
"shape": [8],
"names": [
"joint0", "joint1", "joint2", "joint3",
"joint4", "joint5", "joint6", "gripper",
],
},
"action": {
"dtype": "float32",
"shape": [8],
"names": [
"joint0", "joint1", "joint2", "joint3",
"joint4", "joint5", "joint6", "gripper",
],
},
"observation.images.image": {
"dtype": "video",
"shape": [256, 256, 3],
"names": ["height", "width", "channels"],
},
"observation.images.wrist_image": {
"dtype": "video",
"shape": [256, 256, 3],
"names": ["height", "width", "channels"],
},
"timestamp": {"dtype": "float32", "shape": [1], "names": None},
"frame_index": {"dtype": "int64", "shape": [1], "names": None},
"episode_index": {"dtype": "int64", "shape": [1], "names": None},
"index": {"dtype": "int64", "shape": [1], "names": None},
"task_index": {"dtype": "int64", "shape": [1], "names": None},
"annotation.human.task_description": {"dtype": "int64", "shape": [1], "names": None},
}
with open(out_meta / "info.json", "w") as f:
json.dump(new_info, f, indent=4)
# -- Copy episodes.jsonl and tasks.jsonl verbatim --
shutil.copy(meta_src / "episodes.jsonl", out_meta / "episodes.jsonl")
shutil.copy(meta_src / "tasks.jsonl", out_meta / "tasks.jsonl")
print(f" Done: {out_dataset}")
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--src_root",
type=Path,
default=Path(
"/lustre/fsw/portfolios/nvr/users/jtremblay/yu/conflict_maniskill/demo_conflict"
),
)
parser.add_argument(
"--out_root",
type=Path,
default=Path("/lustre/fsw/portfolios/nvr/users/jtremblay/yu/groot17_data"),
)
parser.add_argument("--categories", nargs="+", default=CATEGORIES)
parser.add_argument("--num_demos", type=int, default=300)
parser.add_argument("--fps", type=int, default=30)
args = parser.parse_args()
args.out_root.mkdir(parents=True, exist_ok=True)
for cat in args.categories:
prepare_category(
src_root=args.src_root,
out_root=args.out_root,
category=cat,
num_demos=args.num_demos,
fps=args.fps,
)
print("\nAll done.")
if __name__ == "__main__":
main()
|