Download inference.py from locksleyu/locksleyfox-walker: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/locksleyu/locksleyfox-walker/resolve/main/inference.py
- Command line
-
hf download hf://locksleyu/locksleyfox-walker/inference.py
-
curl -L -o inference.py https://huggingface.co/locksleyu/locksleyfox-walker/resolve/main/inference.py
1.49 kB
| """Run the LocksleyFox s22rl3 walking policy with numpy and safetensors (no PyTorch, JAX or Brax). | |
| pip install numpy safetensors | |
| python inference.py # checks the network against test_vectors.npz (actions from the original Brax policy) | |
| The policy runs at 50 Hz (every 20 ms). See README.md for the 46 inputs and 12 outputs. | |
| """ | |
| import numpy as np | |
| from safetensors.numpy import load_file | |
| W = load_file("policy.safetensors") | |
| def policy(obs): | |
| """obs: (..., 46) float32 -> actions (..., 12) in [-1, 1] (deterministic: tanh of the Gaussian mean).""" | |
| x = (obs - W["obs_mean"]) / W["obs_std"] | |
| for i in range(4): | |
| x = x @ W[f"layer{i}.weight"] + W[f"layer{i}.bias"] | |
| if i < 3: | |
| x = x / (1.0 + np.exp(-x)) # swish | |
| return np.tanh(x[..., :12]) # outputs 12-23 are the exploration std, not used when deployed | |
| STAND = np.array([0.0, 0.6435, -1.287] * 4, np.float32) # abad, hip, knee per leg (FL, FR, RL, RR), rad | |
| SCALE = np.array([0.25, 0.4, 0.25] * 4, np.float32) # rad per unit action | |
| def joint_targets(action): | |
| """Position targets for a PD loop (robot: kp 49.4 N.m/rad, kd 1.24 N.m.s/rad). Abad joints are locked on this robot.""" | |
| return STAND + SCALE * np.clip(action, -1, 1) | |
| if __name__ == "__main__": | |
| t = np.load("test_vectors.npz") | |
| err = np.abs(policy(t["obs"]) - t["action"]).max() | |
| print("max difference from the original policy: %.2e" % err) | |
| assert err < 1e-5 | |