Nadeem-Shoukath115113/opensarm / tools /plot_spiral_labels.py
Nadeem-Shoukath115113's picture
download
raw
3.47 kB
#!/usr/bin/env python
"""Plot the reward / progress columns of a SPIRAL-labelled dataset.
Reads what is actually written to disk - no model inference - so it shows exactly
what SPIRAL will consume.
python tools/plot_spiral_labels.py --repo-id <ns>/<name> --episodes 5
"""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt # noqa: E402
import numpy as np # noqa: E402
import pyarrow.parquet as pq # noqa: E402
def main() -> None:
ap = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
ap.add_argument("--repo-id", required=True)
ap.add_argument("--root", default=None)
ap.add_argument("--episodes", type=int, default=5)
ap.add_argument("--episode-ids", default=None)
ap.add_argument("--seed", type=int, default=0)
ap.add_argument("--out", default="spiral_labels.png")
args = ap.parse_args()
root = Path(args.root) if args.root else Path.home()/".cache/huggingface/lerobot"/args.repo_id
info = json.loads((root/"meta/info.json").read_text())
fps = float(info["fps"])
eps = [json.loads(l) for l in (root/"meta/episodes.jsonl").read_text().splitlines() if l.strip()]
ids = ([int(x) for x in args.episode_ids.split(",")] if args.episode_ids else
sorted(np.random.default_rng(args.seed).choice(
[e["episode_index"] for e in eps], size=min(args.episodes, len(eps)), replace=False).tolist()))
rows = []
for ep in ids:
t = pq.read_table(root/info["data_path"].format(
episode_chunk=ep//int(info.get("chunks_size", 1000)), episode_index=ep))
r = np.array([x[0] for x in t.column("reward").to_pylist()], dtype=np.float64)
p = np.array([x[0] for x in t.column("progress").to_pylist()], dtype=np.float64)
rows.append((ep, np.arange(len(r))/fps, r, p))
fig, axes = plt.subplots(len(rows), 2, figsize=(11, 2.3*len(rows)))
axes = np.atleast_2d(axes)
for i, (ep, tt, r, p) in enumerate(rows):
a0, a1 = axes[i]
a0.plot(tt, p, color="#20c4c0", lw=2)
a0.set_ylabel(f"ep {ep}\nprogress", fontsize=9, fontweight="bold")
a0.grid(alpha=.3, color="white", lw=1.2); a0.set_facecolor("#eef0f4")
a1.plot(tt, r, color="#f0a832", lw=1.4)
a1.axhline(0, color="#999", lw=0.8, ls="--")
a1.set_ylabel("reward", fontsize=9, fontweight="bold")
a1.grid(alpha=.3, color="white", lw=1.2); a1.set_facecolor("#eef0f4")
for a in (a0, a1):
for s in a.spines.values(): s.set_visible(False)
if i == 0:
a0.set_title("progress column (SARM2 value, paper's r*)", fontsize=10)
a1.set_title("reward column (per-frame delta -> SPIRAL TD)", fontsize=10)
if i == len(rows)-1:
a0.set_xlabel("Time (s)"); a1.set_xlabel("Time (s)")
fig.tight_layout(); fig.savefig(args.out, dpi=140)
allr = np.concatenate([r for _, _, r, _ in rows])
allp = np.concatenate([p for _, _, _, p in rows])
print(f"[out] {Path(args.out).resolve()}")
print(f" reward : mean {allr.mean():.5f} max {allr.max():.5f} negative {100*(allr<0).mean():.1f}%")
print(f" progress: range [{allp.min():.3f}, {allp.max():.3f}]")
for ep, _, r, p in rows:
print(f" ep {ep:>3}: sum(reward) {r.sum():+.4f} | progress {p[0]:+.3f} -> {p[-1]:+.3f}")
if __name__ == "__main__":
main()

Xet Storage Details

Size:
3.47 kB
·
Xet hash:
586efddd312856f66cf983dda5d0ba46aa7fe81815e3f6e68bc4807f1f640fa1

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.