FiveHundredShots / scripts /fivehundredshots.py
catachiii's picture
Initial release: 500 tennis strokes for the Unitree G1
390e994 verified
Raw History Blame Contribute Delete
4.6 kB
"""Loader for the FiveHundredShots dataset (needs only numpy).
A clip is a float32 array of shape (T, 281) sampled at 50 Hz, world frame z-up:
columns field shape notes
0:3 root_pos (T, 3) pelvis position [m]
3:7 root_quat (T, 4) pelvis orientation (w, x, y, z)
7:36 joint_pos (T, 29) [rad], order = JOINT_NAMES
36:39 root_lin_vel (T, 3) world frame [m/s]
39:42 root_ang_vel (T, 3) world frame [rad/s]
42:71 joint_vel (T, 29) [rad/s], order = JOINT_NAMES
71:281 body_pos/quat (T, 30, 7) per body: position (3) + quaternion wxyz (4),
world frame, order = BODY_NAMES
python scripts/fivehundredshots.py # dataset summary
python scripts/fivehundredshots.py shot_0000 # one clip
"""
from __future__ import annotations
import json
import sys
from dataclasses import dataclass
from pathlib import Path
import numpy as np
ROOT = Path(__file__).resolve().parent.parent
FPS = 50
FRAME_DIM = 281
# Breadth-first joint order (the one Isaac Lab reports for the G1); MuJoCo orders joints differently.
JOINT_NAMES = [
"left_hip_pitch_joint", "right_hip_pitch_joint", "waist_yaw_joint",
"left_hip_roll_joint", "right_hip_roll_joint", "waist_roll_joint",
"left_hip_yaw_joint", "right_hip_yaw_joint", "waist_pitch_joint",
"left_knee_joint", "right_knee_joint",
"left_shoulder_pitch_joint", "right_shoulder_pitch_joint",
"left_ankle_pitch_joint", "right_ankle_pitch_joint",
"left_shoulder_roll_joint", "right_shoulder_roll_joint",
"left_ankle_roll_joint", "right_ankle_roll_joint",
"left_shoulder_yaw_joint", "right_shoulder_yaw_joint",
"left_elbow_joint", "right_elbow_joint",
"left_wrist_roll_joint", "right_wrist_roll_joint",
"left_wrist_pitch_joint", "right_wrist_pitch_joint",
"left_wrist_yaw_joint", "right_wrist_yaw_joint",
]
BODY_NAMES = [
"pelvis", "left_hip_pitch_link", "right_hip_pitch_link", "waist_yaw_link",
"left_hip_roll_link", "right_hip_roll_link", "waist_roll_link",
"left_hip_yaw_link", "right_hip_yaw_link", "torso_link",
"left_knee_link", "right_knee_link",
"left_shoulder_pitch_link", "right_shoulder_pitch_link",
"left_ankle_pitch_link", "right_ankle_pitch_link",
"left_shoulder_roll_link", "right_shoulder_roll_link",
"left_ankle_roll_link", "right_ankle_roll_link",
"left_shoulder_yaw_link", "right_shoulder_yaw_link",
"left_elbow_link", "right_elbow_link",
"left_wrist_roll_link", "right_wrist_roll_link",
"left_wrist_pitch_link", "right_wrist_pitch_link",
"left_wrist_yaw_link", "right_wrist_yaw_link",
]
@dataclass(frozen=True)
class Clip:
root_pos: np.ndarray # (T, 3)
root_quat: np.ndarray # (T, 4) wxyz
joint_pos: np.ndarray # (T, 29)
root_lin_vel: np.ndarray # (T, 3) world
root_ang_vel: np.ndarray # (T, 3) world
joint_vel: np.ndarray # (T, 29)
body_pos: np.ndarray # (T, 30, 3) world
body_quat: np.ndarray # (T, 30, 4) wxyz, world
def __len__(self) -> int:
return len(self.root_pos)
def load_clip(path) -> Clip:
"""Split a (T, 281) clip into named arrays (views of one array, no copies)."""
raw = np.load(path)
if raw.ndim != 2 or raw.shape[1] != FRAME_DIM:
raise ValueError(f"{path}: expected shape (T, {FRAME_DIM}), got {raw.shape}")
body = raw[:, 71:].reshape(len(raw), len(BODY_NAMES), 7)
return Clip(raw[:, 0:3], raw[:, 3:7], raw[:, 7:36], raw[:, 36:39], raw[:, 39:42], raw[:, 42:71],
body[..., :3], body[..., 3:])
def load_metadata(root=ROOT) -> list[dict]:
"""One dict per shot, in file order (see the dataset card for the fields)."""
with open(Path(root) / "metadata.jsonl") as f:
return [json.loads(line) for line in f]
def main():
shots = load_metadata()
if len(sys.argv) > 1:
shot = next((s for s in shots if s["id"] == sys.argv[1]), None)
if shot is None:
raise SystemExit(f"unknown shot id: {sys.argv[1]}")
clip = load_clip(ROOT / shot["file"])
print(json.dumps(shot, indent=1))
for name, value in vars(clip).items():
print(f"{name:13s} {value.shape}")
return
frames = sum(s["n_frames"] for s in shots)
strokes = {k: sum(s["stroke"] == k for s in shots) for k in ("forehand", "backhand")}
print(f"{len(shots)} shots, {frames} frames ({frames / FPS / 60:.1f} min at {FPS} Hz), {strokes}")
if __name__ == "__main__":
main()