Download scripts/test_transforms.py from simpk/single_pickplace: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/datasets/simpk/single_pickplace/resolve/main/scripts/test_transforms.py
- Command line
-
hf download hf://datasets/simpk/single_pickplace/scripts/test_transforms.py
-
curl -L -o test_transforms.py https://huggingface.co/datasets/simpk/single_pickplace/resolve/main/scripts/test_transforms.py
5.01 kB
| """Verify the G1 Dex3 transform pipeline against real dataset rows. | |
| uv run python scripts/test_transforms.py [DATASET_ROOT] | |
| Prefers the real `openpi.transforms` when importable (run this on the Brev box after | |
| Step 5 for the strongest check); falls back to the faithful stub in scripts/_stub_openpi | |
| so it can also run anywhere with just numpy + pandas + einops. | |
| """ | |
| import pathlib | |
| import sys | |
| import numpy as np | |
| import pandas as pd | |
| HERE = pathlib.Path(__file__).resolve().parent | |
| ROOT = pathlib.Path(sys.argv[1]) if len(sys.argv) > 1 else HERE.parent | |
| sys.path.insert(0, str(ROOT)) # for g1_dex3_policy | |
| try: | |
| from openpi import transforms # noqa: F401 | |
| print("using REAL openpi.transforms") | |
| except ModuleNotFoundError: | |
| sys.path.insert(0, str(HERE / "_stub_openpi")) | |
| from openpi import transforms | |
| print("using STUB openpi.transforms (scripts/_stub_openpi)") | |
| import g1_dex3_policy as P | |
| ok=lambda m:print(f" PASS {m}") | |
| def fail(m): print(f" FAIL {m}"); sys.exit(1) | |
| print("=== 1. delta mask layout ===") | |
| m=transforms.make_bool_mask(7,-7,7,-7) | |
| assert len(m)==28, len(m) | |
| exp=tuple([True]*7+[False]*7+[True]*7+[False]*7) | |
| assert m==exp | |
| groups={'L_arm':m[0:7],'L_hand':m[7:14],'R_arm':m[14:21],'R_hand':m[21:28]} | |
| for g,v in groups.items(): print(f" {g:<7} delta={all(v)}") | |
| assert all(m[0:7]) and not any(m[7:14]) and all(m[14:21]) and not any(m[21:28]) | |
| ok("make_bool_mask(7,-7,7,-7) -> delta on both arms, absolute on both hands") | |
| wrong=transforms.make_bool_mask(14,-14) | |
| print(f" runbook's make_bool_mask(14,-14): R_arm delta={all(wrong[14:21])} (should be True) " | |
| f"-> would leave the right arm ABSOLUTE") | |
| assert not any(wrong[14:21]) | |
| ok("confirmed the runbook mask would put the right arm on absolute actions") | |
| print("\n=== 2. _upper_body accepts 43 and 28, rejects the rest ===") | |
| assert P._upper_body(np.arange(43.),'s').shape==(28,) | |
| assert np.array_equal(P._upper_body(np.arange(43.),'s'), np.arange(15.,43.)) | |
| assert P._upper_body(np.arange(28.),'s').shape==(28,) | |
| assert P._upper_body(np.zeros((50,43)),'a').shape==(50,28) | |
| for bad in (32,43-1,14): | |
| try: P._upper_body(np.zeros(bad),'s'); fail(f"accepted dim {bad}") | |
| except ValueError: pass | |
| ok("43 -> slice 15:43, 28 -> passthrough, anything else raises ValueError") | |
| print("\n=== 3. real data through Inputs -> Delta -> Absolute -> Outputs ===") | |
| df=pd.read_parquet(ROOT/'data/chunk-000/episode_000000.parquet') | |
| state43=np.asarray(df['observation.state'].iloc[100],dtype=np.float64) | |
| acts43=np.stack([np.asarray(v,np.float64) for v in df['action'].iloc[100:150]]) | |
| assert state43.shape==(43,) and acts43.shape==(50,43) | |
| img=np.zeros((3,480,640),dtype=np.float32) | |
| data={"state":state43,"actions":acts43,"prompt":"pick octopus and place inside brown basket", | |
| "images":{"base_0_rgb":img,"left_wrist_0_rgb":img,"right_wrist_0_rgb":img}} | |
| out=P.G1Dex3Inputs(action_dim=32)(dict(data)) | |
| print(f" state {state43.shape} -> {out['state'].shape} actions {acts43.shape} -> {out['actions'].shape}") | |
| assert out['state'].shape==(32,) and out['actions'].shape==(50,32) | |
| assert np.array_equal(out['state'][:28],state43[15:43]) | |
| assert np.all(out['state'][28:]==0) and np.all(out['actions'][:,28:]==0) | |
| for k,v in out['image'].items(): assert v.shape==(480,640,3) and v.dtype==np.uint8, (k,v.shape,v.dtype) | |
| ok("Inputs: 43->28 sliced, padded to 32 with zeros, CHW float -> HWC uint8") | |
| mask=transforms.make_bool_mask(7,-7,7,-7) | |
| d=transforms.DeltaActions(mask)({**out,"actions":out['actions'].copy()}) | |
| delta=d['actions'] | |
| # arms became deltas; hands untouched | |
| assert np.allclose(delta[:,0:7], acts43[:,15:22]-state43[15:22]) | |
| assert np.allclose(delta[:,7:14], acts43[:,22:29]) | |
| assert np.allclose(delta[:,14:21], acts43[:,29:36]-state43[29:36]) | |
| assert np.allclose(delta[:,21:28], acts43[:,36:43]) | |
| print(f" mean |arm| raw={np.abs(acts43[:,15:22]).mean():.4f} -> delta={np.abs(delta[:,0:7]).mean():.4f}" | |
| f" (delta is {np.abs(acts43[:,15:22]).mean()/max(np.abs(delta[:,0:7]).mean(),1e-9):.1f}x smaller)") | |
| ok("DeltaActions: arms differenced against state, hands left absolute, padding untouched") | |
| back=transforms.AbsoluteActions(mask)({**d,"actions":delta.copy()}) | |
| final=P.G1Dex3Outputs()({"actions":back['actions']}) | |
| assert final['actions'].shape==(50,28) | |
| assert np.allclose(final['actions'],acts43[:,15:43]), np.abs(final['actions']-acts43[:,15:43]).max() | |
| ok(f"round trip exact: max err {np.abs(final['actions']-acts43[:,15:43]).max():.2e}") | |
| print("\n=== 4. serve-time path: robot sends 28 dims instead of 43 ===") | |
| o28=P.G1Dex3Inputs(action_dim=32)({**data,"state":state43[15:43],"actions":None} | {"actions":acts43[:,15:43]}) | |
| assert np.array_equal(o28['state'],out['state']) and np.allclose(o28['actions'],out['actions']) | |
| ok("28-dim client state produces an identical model input to the 43-dim path") | |
| print("\n=== 5. left-hand dims are the constant we expect ===") | |
| assert np.all(acts43[:,22:29]==0.0) | |
| ok("left_hand action == 0.0, survives padding/normalisation as a constant") | |
| print("\nALL CHECKS PASSED") | |