Download leaf/code/v1.5/v1.4/validate_dataset.py from COCOMatrix/LEAF-Bench: direct link, hf CLI and curl.
- Browser
- Download file 3.08 kB
-
https://huggingface.co/datasets/COCOMatrix/LEAF-Bench/resolve/main/leaf/code/v1.5/v1.4/validate_dataset.py
- Command line
-
hf download hf://datasets/COCOMatrix/LEAF-Bench/leaf/code/v1.5/v1.4/validate_dataset.py
-
curl -L -o validate_dataset.py https://huggingface.co/datasets/COCOMatrix/LEAF-Bench/resolve/main/leaf/code/v1.5/v1.4/validate_dataset.py
3.08 kB
| """Validate the exported dataset against the plan and the raw recordings (lerobot env). | |
| python validate_dataset.py <dataset_root> <work_dir> [n_samples]""" | |
| import json | |
| import os | |
| import random | |
| from pathlib import Path | |
| import sys | |
| import av | |
| from video_frame import frame_at | |
| import numpy as np | |
| from lerobot.datasets.lerobot_dataset import LeRobotDataset | |
| root, work = sys.argv[1], sys.argv[2] | |
| n_samples = int(sys.argv[3]) if len(sys.argv) > 3 else 60 | |
| plan = json.load(open(os.path.join(work, "plan.json"))) | |
| eps = plan["episodes"] | |
| ds = LeRobotDataset(plan["repo_id"], root=root) | |
| rep = dict(codebase_version=ds.meta.info["codebase_version"], episodes=ds.num_episodes, frames=ds.num_frames, | |
| plan_episodes=len(eps), plan_frames=plan["total_frames"]) | |
| assert ds.num_episodes == len(eps) and ds.num_frames == plan["total_frames"], rep | |
| lens = ds.meta.episodes["length"] | |
| assert [int(x) for x in lens] == [e["n"] for e in eps], "episode lengths differ from the plan" | |
| lff = os.path.join(root, "lff") | |
| for e in eps: | |
| i = e["index"] | |
| assert os.path.exists(os.path.join(lff, "annotations", f"episode_{i:06d}.json")) | |
| s = np.load(os.path.join(lff, "sim_state", f"episode_{i:06d}.npy"), mmap_mode="r") | |
| assert s.shape[0] == e["n"], (i, s.shape, e["n"]) | |
| rng = random.Random(0) | |
| worst_img, worst_num = 0.0, 0.0 | |
| new_eps = [e for e in eps if e["index"] >= 596] if Path(root).name == "dataset" else eps | |
| old_eps = [e for e in eps if e["index"] < 596] if Path(root).name == "dataset" else [] | |
| n_old = 50 if old_eps else 0 | |
| assert n_samples >= len(new_eps) + n_old | |
| sample_eps = new_eps + [rng.choice(new_eps) for _ in range(n_samples - n_old - len(new_eps))] + [rng.choice(old_eps) for _ in range(n_old)] | |
| for sample_i, e in enumerate(sample_eps): | |
| k = rng.randrange(e["n"]) | |
| item = ds[e["from"] + k] | |
| assert int(item["episode_index"]) == e["index"] and int(item["frame_index"]) == k | |
| fr = np.load(os.path.join(e["dir"], "frames.npz")) | |
| st = np.concatenate([fr["eef_pos"][k], fr["eef_axis_angle"][k], fr["gripper_qpos"][k]]) | |
| worst_num = max(worst_num, float(np.abs(item["observation.state"].numpy() - st).max()), | |
| float(np.abs(item["action"].numpy() - fr["action"][k]).max())) | |
| assert int(item["annotation.attempt_index"]) == int(fr["attempt"][k]) | |
| for key, cam in (("observation.images.image", "agentview"), ("observation.images.wrist_image", "wrist")): | |
| ref = frame_at(os.path.join(e["dir"], f"{cam}.mp4"), k).astype(float) | |
| img = item[key].permute(1, 2, 0).numpy() * 255.0 | |
| worst_img = max(worst_img, float(np.abs(img - ref).mean())) | |
| if (sample_i + 1) % 25 == 0: | |
| print(f"checked {sample_i + 1}/{n_samples}", flush=True) | |
| rep.update(new_samples=n_samples-n_old, old_samples=n_old, every_new_episode_sampled=True, samples=n_samples, max_state_action_abs_err=worst_num, max_mean_abs_pixel_err=round(worst_img, 3)) | |
| assert worst_num < 1e-5 and worst_img < 6.0, rep | |
| print(json.dumps(rep)) | |
| with open(os.path.join(work, "validation_report.json"), "w") as f: | |
| json.dump(dict(rep, status="pass"), f, indent=1) | |