File size: 3,076 Bytes
af3fa80 | 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 | """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)
|