File size: 2,881 Bytes
6a08a28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""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()