BIue-Jay commited on
Commit
f4e12e1
·
verified ·
1 Parent(s): df5be8d

Remove stale duplicates of code/fastwam/tools/*.py

Browse files
code/fastwam/hb_fastwam_aug.py DELETED
@@ -1,73 +0,0 @@
1
- """2026-11-16: Humanoid Benchmark augmentation v2 (project CLAUDE.md) inside the native FastWAM data pipeline.
2
- - Visual: hb_aug_v2.augment on every training sample's frames before the two cameras are concatenated; frame 0 is the
3
- current observation (sensor / optics effects only there), the later frames are the future video targets (colour,
4
- per-camera geometry, light patches and white balance shared with frame 0, as the spec requires).
5
- - Proprio: hb_aug_v2.perturb_joints on the raw body joints (G1 29, H2 31 = body 29 + head 2) before normalization;
6
- hands and gravity untouched; supervision labels untouched. Normalization statistics are computed on the raw data.
7
- FastWAM's own image augmentation is only a resize (no colour jitter / crop), so nothing native is stacked.
8
- Installed in the main process before the dataloaders fork; HB_FW_JOINTS sets the number of perturbed joints."""
9
- import os
10
- import sys
11
- import time
12
-
13
- import numpy as np
14
- import torch
15
-
16
- sys.path.insert(0, os.environ.get("HB_AUG_DIR", ""))
17
- import hb_aug_v2 as A # noqa: E402
18
-
19
- NJ = int(os.environ.get("HB_FW_JOINTS", "29"))
20
- _seen = {"img": False, "state": False}
21
-
22
-
23
- def _rng(idx=0):
24
- return np.random.default_rng([os.getpid(), int(idx) & 0xFFFFFFFF, time.time_ns() & 0xFFFFFFFF])
25
-
26
-
27
- def _train(proc):
28
- v = getattr(proc, "is_train", False)
29
- return bool(v() if callable(v) else v)
30
-
31
-
32
- def install():
33
- from fastwam.datasets.lerobot.base_lerobot_dataset import BaseLerobotDataset
34
- from fastwam.datasets.lerobot.processors.fastwam_processor import FastWAMProcessor
35
-
36
- pre0 = FastWAMProcessor.preprocess
37
-
38
- def preprocess(self, data):
39
- if _train(self) and isinstance(data.get("state"), dict) and "default" in data["state"]:
40
- st = data["state"]["default"]
41
- q = st[..., :NJ].detach().cpu().numpy()
42
- st = st.clone()
43
- st[..., :NJ] = torch.from_numpy(A.perturb_joints(q, _rng())).to(st.dtype)
44
- data["state"]["default"] = st
45
- if not _seen["state"]:
46
- _seen["state"] = True
47
- print(f"HB_FW_AUG proprio perturb_joints on state[..., :{NJ}] shape {tuple(st.shape)}", flush=True)
48
- return pre0(self, data)
49
-
50
- get0 = BaseLerobotDataset.__getitem__
51
-
52
- def getitem(self, idx):
53
- s = get0(self, idx)
54
- if self.processor is not None and _train(self.processor) and "pixel_values" in s:
55
- pv = s["pixel_values"]
56
- five = pv.ndim == 5
57
- x = (pv if five else pv[None]).permute(1, 0, 2, 3, 4).float() # [T, V, C, H, W]
58
- hi = float(x.max())
59
- scale = 255.0 if hi > 1.5 else 1.0
60
- mask = torch.zeros(x.shape[0], dtype=torch.bool)
61
- mask[0] = True
62
- y, p = A.augment((x / scale).clamp(0, 1), _rng(idx), mask)
63
- y = (y * scale).permute(1, 0, 2, 3, 4)
64
- s["pixel_values"] = (y if five else y[0]).to(pv.dtype)
65
- if not _seen["img"]:
66
- _seen["img"] = True
67
- print(f"HB_FW_AUG visual {A.VERSION} on pixel_values {tuple(pv.shape)} dtype {pv.dtype} max {hi:.3f} "
68
- f"(T={x.shape[0]}, obs frame 0) boost={p['boost']}", flush=True)
69
- return s
70
-
71
- FastWAMProcessor.preprocess = preprocess
72
- BaseLerobotDataset.__getitem__ = getitem
73
- print(f"HB_FW_AUG installed: {A.VERSION}, joints {NJ}", flush=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
code/fastwam/hb_fastwam_convert.py DELETED
@@ -1,121 +0,0 @@
1
- """2026-11-16: LeRobot v2.1 copy of one of our datasets for FastWAM on OUR benchmark interface (CLAUDE.md section 4):
2
- g1 : state44 = observation.state[0:29] (29 body joints) + measured Inspire 6 + 6 + projected gravity 3;
3
- action76 = action.motion_token 64 + measured Inspire 6 + 6 (same-row labels, as GR00T / Psi-Zero / Delta Flow)
4
- h2 : state78 = observation.state[0:75] + projected gravity 3; action75 = action.body_target 31 + action.hands 44
5
- h2p: as h2 with action82 = action75 + action.planner 7 (A14 v2)
6
- HumanWAM's fastwam_sonic_data.py expects a 43-D G1 state and takes state[0:22] + state[29:36]; on our 41-D state that
7
- would put left-hand values into the right arm, so it is not used. Source files are never modified; media are symlinked.
8
- --episodes N keeps the first N episodes (B7: first 100 of 107, as for every other model).
9
- Usage: hb_fastwam_convert.py <profile> <source data dir> <fresh output dir> [--episodes N]"""
10
- import copy, json, shutil, sys, tempfile
11
- from pathlib import Path
12
- import numpy as np
13
- import pyarrow as pa
14
- import pyarrow.parquet as pq
15
-
16
- G1 = {"observation.state": 41, "action.motion_token": 64, "observation.inspire_left_hand_state": 6,
17
- "observation.inspire_right_hand_state": 6, "observation.projected_gravity": 3}
18
- H2 = {"observation.state": 75, "observation.projected_gravity": 3, "action.body_target": 31, "action.hands": 44}
19
- H2P = dict(H2, **{"action.planner": 7})
20
- SPEC = {"g1": (G1, ("ego_view", "chest")), "h2": (H2, ("head", "chest")), "h2p": (H2P, ("head", "chest"))}
21
-
22
-
23
- def stats(v):
24
- v = np.asarray(v, dtype=np.float64)
25
- return {"min": v.min(0).tolist(), "max": v.max(0).tolist(), "mean": v.mean(0).tolist(), "std": v.std(0).tolist(), "count": [len(v)]}
26
-
27
-
28
- def vec(table, key, values):
29
- arr = pa.array(values.tolist(), type=pa.list_(pa.float32(), values.shape[1]))
30
- i = table.schema.get_field_index(key)
31
- return table.append_column(key, arr) if i < 0 else table.set_column(i, key, arr)
32
-
33
-
34
- def map_rows(profile, c):
35
- if profile == "g1":
36
- L, R = c["observation.inspire_left_hand_state"], c["observation.inspire_right_hand_state"]
37
- dev = float(np.abs(c["observation.state"][:, 29:41] - np.concatenate([L, R], 1)).max())
38
- assert dev < 1e-4, f"observation.state[29:41] differs from the Inspire columns by {dev}"
39
- return (np.concatenate([c["action.motion_token"], L, R], 1),
40
- np.concatenate([c["observation.state"][:, :29], L, R, c["observation.projected_gravity"]], 1))
41
- a = [c["action.body_target"], c["action.hands"]] + ([c["action.planner"]] if profile == "h2p" else [])
42
- return np.concatenate(a, 1), np.concatenate([c["observation.state"], c["observation.projected_gravity"]], 1)
43
-
44
-
45
- def main(profile, src, out, limit=None):
46
- raw, cams = SPEC[profile]
47
- src, out = Path(src).resolve(strict=True), Path(out).resolve()
48
- if out.exists():
49
- raise SystemExit(f"{out} exists")
50
- info = json.loads((src / "meta/info.json").read_text())
51
- assert info.get("codebase_version") == "v2.1", info.get("codebase_version")
52
- for k, w in raw.items():
53
- assert info["features"][k]["shape"] == [w], (k, info["features"][k]["shape"])
54
- for c in cams:
55
- assert info["features"]["observation.images." + c]["dtype"] in ("video", "image"), c
56
- eps = [json.loads(l) for l in (src / "meta/episodes.jsonl").read_text().splitlines() if l.strip()]
57
- assert [e["episode_index"] for e in eps] == list(range(len(eps)))
58
- if limit is not None:
59
- eps = eps[:limit]
60
- old = {r["episode_index"]: r for r in (json.loads(l) for l in (src / "meta/episodes_stats.jsonl").read_text().splitlines() if l.strip())}
61
- out.parent.mkdir(parents=True, exist_ok=True)
62
- stg = Path(tempfile.mkdtemp(prefix=".hbfw-", dir=out.parent))
63
- shutil.copytree(src / "meta", stg / "meta")
64
- for n in ("videos", "images"):
65
- if (src / n).exists():
66
- (stg / n).symlink_to(src / n, target_is_directory=True)
67
- new, total = [], 0
68
- for e in eps:
69
- i = e["episode_index"]
70
- rel = Path(info["data_path"].format(episode_chunk=i // info["chunks_size"], episode_index=i))
71
- t = pq.read_table(src / rel)
72
- c = {k: np.asarray(t[k].to_pylist(), dtype=np.float32) for k in raw}
73
- act, st = map_rows(profile, c)
74
- assert len(act) == e["length"] and np.isfinite(act).all() and np.isfinite(st).all()
75
- total += len(act)
76
- t = vec(vec(t, "action", act), "observation.state", st)
77
- md = dict(t.schema.metadata or {})
78
- if b"huggingface" in md:
79
- hf = json.loads(md[b"huggingface"])
80
- for k, w in (("action", act.shape[1]), ("observation.state", st.shape[1])):
81
- hf["info"]["features"][k] = {"feature": {"dtype": "float32", "_type": "Value"}, "length": w, "_type": "Sequence"}
82
- md[b"huggingface"] = json.dumps(hf).encode()
83
- t = t.replace_schema_metadata(md)
84
- (stg / rel).parent.mkdir(parents=True, exist_ok=True)
85
- pq.write_table(t, stg / rel)
86
- r = copy.deepcopy(old[i])
87
- r["stats"]["action"], r["stats"]["observation.state"] = stats(act), stats(st)
88
- new.append(r)
89
- if limit is None:
90
- assert total == info["total_frames"], (total, info["total_frames"])
91
- else:
92
- info["total_episodes"], info["total_frames"] = len(eps), total
93
- info["total_videos"] = len(eps) * len(cams) if "total_videos" in info else info.get("total_videos")
94
- info["splits"] = {"train": f"0:{len(eps)}"}
95
- (stg / "meta/episodes.jsonl").write_text("".join(json.dumps(e) + "\n" for e in eps))
96
- info["features"]["action"] = {"dtype": "float32", "shape": [int(act.shape[1])], "names": None}
97
- info["features"]["observation.state"] = {"dtype": "float32", "shape": [int(st.shape[1])], "names": None}
98
- drop = [k for k, v in info["features"].items() if v["dtype"] in ("video", "image") and k not in ["observation.images." + c for c in cams]]
99
- for k in drop: # 2-camera rule: unused cameras are not downloaded, FastWAM checks every listed video file
100
- del info["features"][k]
101
- for r in new:
102
- r["stats"].pop(k, None)
103
- if drop and info.get("total_videos"):
104
- info["total_videos"] = len(eps) * len(cams)
105
- (stg / "meta/info.json").write_text(json.dumps(info, indent=2) + "\n")
106
- (stg / "meta/episodes_stats.jsonl").write_text("".join(json.dumps(r) + "\n" for r in new))
107
- for s in (stg / "meta").glob("*stats*.json"):
108
- s.unlink()
109
- (stg / "meta/hb_fastwam_interface.json").write_text(json.dumps(dict(
110
- profile=profile, source=str(src), cameras=list(cams), state_dim=int(st.shape[1]), action_dim=int(act.shape[1]),
111
- episodes=len(eps), frames=total, fps=info["fps"], first_episodes_only=limit), indent=2) + "\n")
112
- stg.rename(out)
113
- print("HBFW_CONVERT_OK", profile, out, "episodes", len(eps), "frames", total, "state", st.shape[1], "action", act.shape[1], flush=True)
114
-
115
-
116
- if __name__ == "__main__":
117
- a = sys.argv[1:]
118
- lim = None
119
- if "--episodes" in a:
120
- j = a.index("--episodes"); lim = int(a[j + 1]); del a[j:j + 2]
121
- main(a[0], a[1], a[2], lim)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
code/fastwam/hb_fastwam_train.py DELETED
@@ -1,98 +0,0 @@
1
- """2026-11-16: native FastWAM Gaussian-FM training on our benchmark interface, with the HB augmentation v2.
2
- Profiles (HB_FW_PROFILE): g1 = state44 / action76 / cameras ego_view + chest (HumanWAM fastwam_sonic recipe);
3
- h2 = state78 / action75 / head + chest; h2p = state78 / action82 (+ planner 7) / head + chest. Fixed recipe of HumanWAM
4
- fastwam_sonic_training.compose_sonic_config: 17 raw samples = 16 actions + 5 video frames (stride 4), 224 x 448 side by
5
- side, min/max statistics, absolute labels, action scheduler shift 1.0, original Wan files.
6
- Arguments: key=value Hydra overrides (JSON values parsed). Env: FASTWAM_SRC, DIFFSYNTH_MODEL_BASE_PATH, HB_AUG_DIR.
7
- HB_MODEL_YAML=<path> writes the resolved model config; HB_COMPOSE_ONLY=1 prints the config; HB_DATA_SAMPLE=1 reads one
8
- augmented training sample on CPU; HB_FW_AUG=0 disables the augmentation (benchmark only)."""
9
- import json, os, sys
10
- from pathlib import Path
11
-
12
- ROOT = Path(os.environ["FASTWAM_SRC"]).resolve()
13
- sys.path.insert(0, str(ROOT / "src"))
14
- sys.path.insert(0, str(Path(__file__).resolve().parent))
15
- TASK = "libero_uncond_2cam224_1e-4"
16
- PROFILES = {"g1": (("ego_view", "chest"), 44, 76, 29), "h2": (("head", "chest"), 78, 75, 31),
17
- "h2p": (("head", "chest"), 78, 82, 31)}
18
- CAMS, SDIM, ADIM, NJ = PROFILES[os.environ.get("HB_FW_PROFILE", "g1")]
19
- os.environ.setdefault("HB_FW_JOINTS", str(NJ))
20
- SHAPE_META = {
21
- "images": [{"key": n, "raw_shape": [3, 224, 224], "shape": [3, 224, 224]} for n in CAMS],
22
- "action": [{"key": "default", "raw_shape": ADIM, "shape": ADIM}],
23
- "state": [{"key": "default", "raw_shape": SDIM, "shape": SDIM}],
24
- }
25
- FIXED = {
26
- "data.train.shape_meta": SHAPE_META, "data.train.num_frames": 17, "data.train.global_sample_stride": 1,
27
- "data.train.action_video_freq_ratio": 4, "data.train.video_size": [224, 448], "data.train.concat_multi_camera": "horizontal",
28
- "data.train.processor.action_output_dim": ADIM, "data.train.processor.proprio_output_dim": SDIM,
29
- "data.train.processor.delta_action_dim_mask": {"default": [False] * ADIM}, "data.train.processor.action_state_transforms": None,
30
- "data.train.processor.use_stepwise_action_norm": False, "data.train.processor.norm_default_mode": "min/max",
31
- "data.train.processor.norm_exception_mode": None, "model.redirect_common_files": False,
32
- "model.action_scheduler.train_shift": 1.0, "model.action_scheduler.infer_shift": 1.0,
33
- }
34
-
35
-
36
- def build(overrides):
37
- from hydra import compose, initialize_config_dir
38
- from omegaconf import OmegaConf
39
- from fastwam.utils.config_resolvers import register_default_resolvers
40
- register_default_resolvers()
41
- with initialize_config_dir(config_dir=str(ROOT / "configs"), version_base="1.3"):
42
- cfg = compose(config_name="train", overrides=["task=" + TASK])
43
- for k, v in {**FIXED, **overrides}.items():
44
- OmegaConf.update(cfg, k, v, merge=False)
45
- for k, v in FIXED.items():
46
- a = OmegaConf.select(cfg, k)
47
- if OmegaConf.is_config(a):
48
- a = OmegaConf.to_container(a, resolve=True)
49
- assert a == v, (k, a, v)
50
- return cfg
51
-
52
-
53
- def parse(argv):
54
- out = {}
55
- for a in argv:
56
- k, v = a.split("=", 1)
57
- try:
58
- out[k] = json.loads(v)
59
- except ValueError:
60
- out[k] = v
61
- return out
62
-
63
-
64
- if __name__ == "__main__":
65
- from omegaconf import OmegaConf
66
- cfg = build(parse(sys.argv[1:]))
67
- if os.environ.get("HB_MODEL_YAML"):
68
- Path(os.environ["HB_MODEL_YAML"]).write_text(OmegaConf.to_yaml(cfg.model, resolve=True))
69
- print("HBFW_MODEL_YAML", os.environ["HB_MODEL_YAML"], flush=True)
70
- raise SystemExit(0)
71
- if os.environ.get("HB_COMPOSE_ONLY"):
72
- print(OmegaConf.to_yaml(cfg, resolve=True))
73
- raise SystemExit(0)
74
- import fastwam.runtime as native
75
- assert Path(native.__file__).resolve() == ROOT / "src/fastwam/runtime.py", native.__file__
76
- if os.environ.get("HB_FW_AUG", "1") != "0":
77
- import hb_fastwam_aug
78
- hb_fastwam_aug.install()
79
- print("HBFW_RECIPE", json.dumps(dict(profile=os.environ.get("HB_FW_PROFILE", "g1"), cameras=CAMS, state_dim=SDIM,
80
- action_dim=ADIM, perturbed_joints=int(os.environ["HB_FW_JOINTS"]),
81
- aug=os.environ.get("HB_FW_AUG", "1") != "0",
82
- batch_size=cfg.batch_size, max_steps=cfg.max_steps, lr=cfg.learning_rate)), flush=True)
83
- if os.environ.get("HB_DATA_SAMPLE"): # CPU check: build the dataset and read augmented samples, no model
84
- from omegaconf import open_dict
85
- from fastwam.utils import misc
86
- misc.register_work_dir(os.environ.get("HB_SAMPLE_WORKDIR", "/tmp/hbfw-sample"))
87
- tr, _ = native.build_datasets(cfg.data)
88
- s = tr[len(tr) // 2]
89
-
90
- def desc(x):
91
- if isinstance(x, dict):
92
- return {k: desc(v) for k, v in x.items()}
93
- if hasattr(x, "shape"):
94
- return [tuple(x.shape), str(x.dtype), round(float(x.float().min()), 3), round(float(x.float().max()), 3)]
95
- return type(x).__name__
96
- print("HBFW_SAMPLE", len(tr), json.dumps(desc(s), default=str), flush=True)
97
- raise SystemExit(0)
98
- native.run_training(cfg)