Download scripts/load_clip.py from hahela/kine2go: direct link, hf CLI and curl.
- Browser
- Download file 5.24 kB
-
https://huggingface.co/datasets/hahela/kine2go/resolve/main/scripts/load_clip.py
- Command line
-
hf download hf://datasets/hahela/kine2go/scripts/load_clip.py
-
curl -L -o load_clip.py https://huggingface.co/datasets/hahela/kine2go/resolve/main/scripts/load_clip.py
5.24 kB
| """Lightweight loader for the Kine2Go dataset. | |
| Repository layout: | |
| <dataset_root>/ | |
| ├── README.md | |
| ├── LICENSE | |
| ├── LICENSES/ | |
| ├── metadata.json | |
| ├── scripts/ | |
| └── data/ | |
| └── <clip_name>/ | |
| ├── motion.npy (T, 61) float32 reference trajectory | |
| ├── config.json env / obs / reward / command / policy configs | |
| ├── cfgs.pkl same configs as a pickle (for tooling) | |
| ├── clip.json per-clip metadata (source, license, n_frames, ...) | |
| ├── traj_NNNN/ 20 perturbed PPO rollouts (NNNN = 0000..0019) | |
| │ ├── init_state.pkl | |
| │ ├── traj.pkl list of per-step frame dicts | |
| │ └── traj.mp4 rendered video | |
| └── logs/ | |
| └── model.pt final PPO checkpoint | |
| Quick usage: | |
| from load_clip import Clip, list_clips | |
| for name in list_clips("kine2go"): | |
| clip = Clip("kine2go", name) | |
| print(name, clip.motion.shape, clip.metadata["license"]) | |
| clip = Clip("kine2go", "ai4_dog_canter") | |
| motion = clip.motion # np.ndarray (T, 61) float32 | |
| cfg = clip.config # {env_cfg, obs_cfg, reward_cfg, command_cfg, policy_cfg} | |
| meta = clip.metadata # {name, source, license, fps, n_frames, ...} | |
| frames = clip.rollout(0) # list of per-step dicts (requires torch) | |
| policy = clip.policy_path # Path to data/<clip>/logs/model.pt | |
| Run as a script to print clip summaries: | |
| python scripts/load_clip.py # list every clip | |
| python scripts/load_clip.py ai4_dog_canter # summarize one clip | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| from dataclasses import dataclass | |
| from functools import cached_property | |
| from pathlib import Path | |
| import numpy as np | |
| def list_clips(root: Path | str) -> list[str]: | |
| """Return clip directory names in alphabetical order.""" | |
| return sorted(p.name for p in (Path(root) / "data").iterdir() if p.is_dir()) | |
| class Clip: | |
| """A single Kine2Go clip. Files are loaded lazily on first access.""" | |
| root: Path | |
| name: str | |
| def __post_init__(self) -> None: | |
| self.root = Path(self.root) | |
| if not self.dir.is_dir(): | |
| raise FileNotFoundError(f"clip directory not found: {self.dir}") | |
| def dir(self) -> Path: | |
| return self.root / "data" / self.name | |
| def metadata(self) -> dict: | |
| return json.loads((self.dir / "clip.json").read_text()) | |
| def config(self) -> dict: | |
| return json.loads((self.dir / "config.json").read_text()) | |
| def motion(self) -> np.ndarray: | |
| return np.load(self.dir / "motion.npy") | |
| def n_rollouts(self) -> int: | |
| return sum( | |
| 1 for p in self.dir.iterdir() if p.is_dir() and p.name.startswith("traj_") | |
| ) | |
| def rollout_dir(self, idx: int) -> Path: | |
| return self.dir / f"traj_{idx:04d}" | |
| def rollout(self, idx: int) -> list[dict]: | |
| """Load rollout ``idx`` as a list of per-step frame dicts. Requires ``torch``.""" | |
| try: | |
| import torch | |
| except ImportError as e: | |
| raise ImportError( | |
| "Loading rollouts requires `torch`. Install with: pip install torch" | |
| ) from e | |
| return torch.load( | |
| self.rollout_dir(idx) / "traj.pkl", map_location="cpu", weights_only=False | |
| ) | |
| def rollout_video(self, idx: int) -> Path: | |
| return self.rollout_dir(idx) / "traj.mp4" | |
| def policy_path(self) -> Path: | |
| """Path to the PPO checkpoint in logs/.""" | |
| path = self.dir / "logs" / "model.pt" | |
| if not path.exists(): | |
| raise FileNotFoundError(f"checkpoint not found: {path}") | |
| return path | |
| def _summary(clip: Clip) -> str: | |
| m = clip.metadata | |
| return ( | |
| f"{clip.name}\n" | |
| f" source : {m['source']}\n" | |
| f" license : {m['license']}\n" | |
| f" fps : {m['fps']}\n" | |
| f" n_frames : {m['n_frames']}\n" | |
| f" duration_s : {m['duration_s']}\n" | |
| f" motion : shape {clip.motion.shape}, dtype {clip.motion.dtype}\n" | |
| f" rollouts : {clip.n_rollouts}\n" | |
| f" policy : {clip.policy_path.name}\n" | |
| ) | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Load and summarize Kine2Go clips.") | |
| parser.add_argument( | |
| "--root", | |
| type=Path, | |
| default=Path(__file__).resolve().parent.parent, | |
| help="Dataset root directory (default: parent of this script).", | |
| ) | |
| parser.add_argument( | |
| "clip", | |
| nargs="?", | |
| help="Clip name to summarize. If omitted, lists every clip.", | |
| ) | |
| args = parser.parse_args() | |
| if args.clip: | |
| print(_summary(Clip(args.root, args.clip))) | |
| else: | |
| names = list_clips(args.root) | |
| print(f"{len(names)} clips in {args.root}:") | |
| for n in names: | |
| print(f" {n}") | |
| if __name__ == "__main__": | |
| main() | |