Download code/dexhand/train/serve.py from Noctis12138/dexhand-train-kit: direct link, hf CLI and curl.
- Browser
- Download file 2.88 kB
-
https://huggingface.co/Noctis12138/dexhand-train-kit/resolve/main/code/dexhand/train/serve.py
- Command line
-
hf download hf://Noctis12138/dexhand-train-kit/code/dexhand/train/serve.py
-
curl -L -o serve.py https://huggingface.co/Noctis12138/dexhand-train-kit/resolve/main/code/dexhand/train/serve.py
2.88 kB
| """Serve a trained dexhand pi0.5 checkpoint over openpi's websocket protocol (openpi venv; no data needed). | |
| python train/serve.py --checkpoint <ckpt>/<step> [--port 8000] [--max-token-len 288] [--horizon 50] [--right-wrist black] | |
| The checkpoint dir must hold params/ and assets/dual_marvin_wuji/norm_stats.json (as saved by training). Request | |
| (one dict, unbatched): observation/image (head, HxWx3 uint8), observation/left_wrist_image, | |
| observation/right_wrist_image, observation/state (54,), prompt (sub-task instruction). Reply: actions (T, 54) absolute | |
| joint targets in the data order (arm 7 + hand 20 finger-major, per side), via the training config's output transforms | |
| (de-interleave, unnormalise, arm deltas -> absolute). | |
| """ | |
| import argparse | |
| import logging | |
| import pathlib | |
| import sys | |
| sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) | |
| class BlackRightWrist: | |
| """Policy wrapper: the right wrist image is replaced by zeros before inference.""" | |
| def __init__(self, policy): | |
| self._policy = policy | |
| def infer(self, obs): | |
| import numpy as np | |
| obs = dict(obs) | |
| obs["observation/right_wrist_image"] = np.zeros_like(np.asarray(obs["observation/right_wrist_image"])) | |
| return self._policy.infer(obs) | |
| def __getattr__(self, name): | |
| return getattr(self._policy, name) | |
| def main(): | |
| p = argparse.ArgumentParser() | |
| p.add_argument("--checkpoint", required=True) | |
| p.add_argument("--port", type=int, default=8000) | |
| p.add_argument("--horizon", type=int, default=50) | |
| p.add_argument("--max-token-len", type=int, default=288) # as trained (run.py; mid-train v1 log) | |
| p.add_argument("--num-steps", type=int, default=10, help="flow-matching denoising steps") | |
| p.add_argument("--right-wrist", default="keep", choices=["keep", "black"], | |
| help="black: replace the right wrist image with zeros (models trained before 2026-10-26 saw a " | |
| "right wrist camera on the BACK of the hand, filming the ceiling; the real one looks at the table)") | |
| a = p.parse_args() | |
| logging.basicConfig(level=logging.INFO, force=True) | |
| import dexhand_openpi as dx | |
| from openpi.policies import policy_config | |
| from openpi.serving import websocket_policy_server | |
| cfg = dx.get_config("unused", T=a.horizon, max_token_len=a.max_token_len) | |
| policy = policy_config.create_trained_policy(cfg, a.checkpoint, sample_kwargs={"num_steps": a.num_steps}) | |
| if a.right_wrist == "black": | |
| policy = BlackRightWrist(policy) | |
| meta = {"checkpoint": str(a.checkpoint), "horizon": a.horizon, "action_dim": 54, "right_wrist": a.right_wrist} | |
| logging.info("serving %s on port %d", a.checkpoint, a.port) | |
| websocket_policy_server.WebsocketPolicyServer(policy=policy, host="0.0.0.0", port=a.port, metadata=meta).serve_forever() | |
| if __name__ == "__main__": | |
| main() | |