Upload transform_batch.py with huggingface_hub
Browse files- transform_batch.py +82 -20
transform_batch.py
CHANGED
|
@@ -11,6 +11,7 @@ import json
|
|
| 11 |
import os
|
| 12 |
import ssl
|
| 13 |
import sys
|
|
|
|
| 14 |
import urllib.request
|
| 15 |
from concurrent.futures import ProcessPoolExecutor, as_completed
|
| 16 |
from pathlib import Path
|
|
@@ -48,18 +49,66 @@ STAGE_KEYS = [k for k, _ in STAGES]
|
|
| 48 |
MARKERS = [("[M6]", "output"), ("[M5]", "qa"), ("[M4]", "smooth"),
|
| 49 |
("[M3.5]", "collision"), ("[M3]", "ik"), ("[M2]", "base"), ("[M1]", "retarget")]
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
KP_COLS = [f"observation.state.{h}{k}" for h in ("right", "left")
|
| 52 |
for k in ("ThumbTip", "IndexFingerTip", "MiddleFingerTip", "Hand")]
|
| 53 |
|
| 54 |
|
| 55 |
-
def _ensure(cache: Path, rel: str) -> Path:
|
|
|
|
|
|
|
|
|
|
| 56 |
local = cache / REPO.replace("/", "__") / rel
|
| 57 |
-
if
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
def _episode_range(cache: Path, subset: str, ep: int) -> dict:
|
|
@@ -129,10 +178,21 @@ class Report:
|
|
| 129 |
s["status"] = "done"
|
| 130 |
c["status"] = "done"
|
| 131 |
c["output"] = res.get("output")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
c["metrics"] = {
|
| 133 |
-
"ik_R_cm":
|
| 134 |
-
"
|
| 135 |
-
|
|
|
|
| 136 |
"n_frames": res.get("n_frames_out", c["n_frames"]),
|
| 137 |
}
|
| 138 |
|
|
@@ -184,21 +244,23 @@ class _Tee(io.TextIOBase):
|
|
| 184 |
self.orig.flush()
|
| 185 |
|
| 186 |
|
| 187 |
-
def _prewarm(cache: Path, clips: list) -> None:
|
| 188 |
-
"""Download each clip's episodes-meta + data parquet
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
| 192 |
try:
|
| 193 |
subset, ep_s = clip["clip_id"].split("#")
|
| 194 |
r = _episode_range(cache, subset, int(ep_s))
|
| 195 |
-
key = f"{subset}/{r['data/chunk_index']}/{r['data/file_index']}"
|
| 196 |
-
if key in seen:
|
| 197 |
-
continue
|
| 198 |
-
seen.add(key)
|
| 199 |
_ensure(cache, f"{PREFIX}{subset}/data/chunk-{r['data/chunk_index']:03d}/file-{r['data/file_index']:03d}.parquet")
|
| 200 |
except Exception:
|
| 201 |
-
pass
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
|
| 204 |
def _run_one(payload: dict) -> dict:
|
|
|
|
| 11 |
import os
|
| 12 |
import ssl
|
| 13 |
import sys
|
| 14 |
+
import time
|
| 15 |
import urllib.request
|
| 16 |
from concurrent.futures import ProcessPoolExecutor, as_completed
|
| 17 |
from pathlib import Path
|
|
|
|
| 49 |
MARKERS = [("[M6]", "output"), ("[M5]", "qa"), ("[M4]", "smooth"),
|
| 50 |
("[M3.5]", "collision"), ("[M3]", "ik"), ("[M2]", "base"), ("[M1]", "retarget")]
|
| 51 |
|
| 52 |
+
|
| 53 |
+
# ── QA verdict: fold FAITHFULNESS (tracking / orientation / arm-collision) into the per-clip
|
| 54 |
+
# PASS/WARN/FAIL alongside the motion-quality verdict. Was motion-only, which missed clips that
|
| 55 |
+
# move smoothly but don't actually follow the human demo. Thresholds are env-overridable so the
|
| 56 |
+
# UI can tune them per run (see config.py for the same defaults). higher value = worse.
|
| 57 |
+
def _envf(key: str, default: float) -> float:
|
| 58 |
+
try:
|
| 59 |
+
return float(os.environ.get(key, default))
|
| 60 |
+
except (TypeError, ValueError):
|
| 61 |
+
return default
|
| 62 |
+
|
| 63 |
+
_QA_IK_WARN = _envf("QA_IK_CM_WARN", 3.0)
|
| 64 |
+
_QA_IK_FAIL = _envf("QA_IK_CM_FAIL", 6.0)
|
| 65 |
+
_QA_ORI_WARN = _envf("QA_ORI_DEG_WARN", 15.0)
|
| 66 |
+
_QA_ORI_FAIL = _envf("QA_ORI_DEG_FAIL", 30.0)
|
| 67 |
+
_QA_ARMS_STRICT = os.environ.get("QA_ARMS_STRICT", "0") == "1"
|
| 68 |
+
_QA_RANK = {"PASS": 0, "WARN": 1, "FAIL": 2, "?": 0}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _grade_high(v: float, warn: float, fail: float) -> str:
|
| 72 |
+
"""Grade a metric where higher is worse against warn/fail bounds."""
|
| 73 |
+
if v is None:
|
| 74 |
+
return "PASS"
|
| 75 |
+
if v >= fail:
|
| 76 |
+
return "FAIL"
|
| 77 |
+
if v >= warn:
|
| 78 |
+
return "WARN"
|
| 79 |
+
return "PASS"
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def _worst(*verdicts: str) -> str:
|
| 83 |
+
return max(verdicts, key=lambda v: _QA_RANK.get(v, 0))
|
| 84 |
+
|
| 85 |
KP_COLS = [f"observation.state.{h}{k}" for h in ("right", "left")
|
| 86 |
for k in ("ThumbTip", "IndexFingerTip", "MiddleFingerTip", "Hand")]
|
| 87 |
|
| 88 |
|
| 89 |
+
def _ensure(cache: Path, rel: str, timeout: int = 30, retries: int = 4) -> Path:
|
| 90 |
+
"""Download a repo file to cache, ONCE. Bounded per-request timeout + retries + atomic write so a
|
| 91 |
+
single stalled host connection can't hang the whole run forever (urlopen has no default timeout —
|
| 92 |
+
that infinite block was what froze prewarm at 0% CPU)."""
|
| 93 |
local = cache / REPO.replace("/", "__") / rel
|
| 94 |
+
if local.exists() and local.stat().st_size > 0:
|
| 95 |
+
return local
|
| 96 |
+
local.parent.mkdir(parents=True, exist_ok=True)
|
| 97 |
+
url = RESOLVE % (REPO, rel)
|
| 98 |
+
last = None
|
| 99 |
+
for attempt in range(retries):
|
| 100 |
+
try:
|
| 101 |
+
req = urllib.request.Request(url, headers={"User-Agent": "fd-studio"})
|
| 102 |
+
with urllib.request.urlopen(req, timeout=timeout, context=_SSL_CTX) as r:
|
| 103 |
+
data = r.read()
|
| 104 |
+
tmp = local.with_name(local.name + ".part")
|
| 105 |
+
tmp.write_bytes(data)
|
| 106 |
+
tmp.replace(local) # atomic — a killed/partial download never looks complete
|
| 107 |
+
return local
|
| 108 |
+
except Exception as e:
|
| 109 |
+
last = e
|
| 110 |
+
time.sleep(min(5.0, 1.0 * (attempt + 1)))
|
| 111 |
+
raise RuntimeError(f"download failed after {retries} tries: {rel}: {last}")
|
| 112 |
|
| 113 |
|
| 114 |
def _episode_range(cache: Path, subset: str, ep: int) -> dict:
|
|
|
|
| 178 |
s["status"] = "done"
|
| 179 |
c["status"] = "done"
|
| 180 |
c["output"] = res.get("output")
|
| 181 |
+
ik_R = round(res.get("ik_R_cm", 0), 2)
|
| 182 |
+
ik_L = round(res.get("ik_L_cm", 0), 2)
|
| 183 |
+
ori_R = round(res.get("ori_R_deg", 0), 1)
|
| 184 |
+
ori_L = round(res.get("ori_L_deg", 0), 1)
|
| 185 |
+
motion = res.get("qa_verdict", "?") # m5_qa motion-quality verdict
|
| 186 |
+
# grade faithfulness on the worse of the two arms, then combine (worst wins)
|
| 187 |
+
track_g = _grade_high(max(ik_R, ik_L), _QA_IK_WARN, _QA_IK_FAIL)
|
| 188 |
+
ori_g = _grade_high(max(ori_R, ori_L), _QA_ORI_WARN, _QA_ORI_FAIL)
|
| 189 |
+
arms_g = "PASS" if collision == "clean" else ("FAIL" if _QA_ARMS_STRICT else "WARN")
|
| 190 |
+
verdict = _worst(motion, track_g, ori_g, arms_g)
|
| 191 |
c["metrics"] = {
|
| 192 |
+
"ik_R_cm": ik_R, "ik_L_cm": ik_L, "ori_R_deg": ori_R, "ori_L_deg": ori_L,
|
| 193 |
+
"collision": collision, "qa": verdict, "qa_motion": motion, "dof": 14,
|
| 194 |
+
# per-component grades so the UI can show WHAT drove the verdict + re-grade live
|
| 195 |
+
"qa_components": {"tracking": track_g, "orientation": ori_g, "arms": arms_g, "motion": motion},
|
| 196 |
"n_frames": res.get("n_frames_out", c["n_frames"]),
|
| 197 |
}
|
| 198 |
|
|
|
|
| 244 |
self.orig.flush()
|
| 245 |
|
| 246 |
|
| 247 |
+
def _prewarm(cache: Path, clips: list, workers: int = 24) -> None:
|
| 248 |
+
"""Download each clip's episodes-meta + data parquet up front so the retarget workers only read
|
| 249 |
+
the cache. Runs in PARALLEL with bounded per-file timeouts — the old sequential + no-timeout
|
| 250 |
+
version froze the whole run for good if a single host connection stalled (0% CPU, flat disk).
|
| 251 |
+
Existence-check + atomic write make concurrent fetches of a shared file safe/idempotent."""
|
| 252 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 253 |
+
|
| 254 |
+
def _warm(clip):
|
| 255 |
try:
|
| 256 |
subset, ep_s = clip["clip_id"].split("#")
|
| 257 |
r = _episode_range(cache, subset, int(ep_s))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
_ensure(cache, f"{PREFIX}{subset}/data/chunk-{r['data/chunk_index']:03d}/file-{r['data/file_index']:03d}.parquet")
|
| 259 |
except Exception:
|
| 260 |
+
pass # a clip that can't prefetch is retried lazily in its worker — never blocks prewarm
|
| 261 |
+
|
| 262 |
+
with ThreadPoolExecutor(max_workers=min(workers, max(1, len(clips)))) as ex:
|
| 263 |
+
list(ex.map(_warm, clips))
|
| 264 |
|
| 265 |
|
| 266 |
def _run_one(payload: dict) -> dict:
|