Views
No views yet
1import numpy as np
2from transformers import AutoModel
3np.set_printoptions(suppress=True)
4if __name__ == "__main__":
5 tokenizer = AutoModel.from_pretrained("ZibinDong/ActionCodec-Base-RVQft", trust_remote_code=True)
6 q99 = np.array([0.9375, 0.91071427, 0.9375, 0.20357142, 0.26357144, 0.375, 1.0])
7 q01 = np.array([-0.87857145, -0.87589288, -0.9375, -0.15107143, -0.20678571, -0.27964285, 0.0])
8 # an example action from physical-intelligence/libero
9 action = np.array(
10 [
11 [0.3268, 0.2089, -0.3295, 0.0000, -0.0868, -0.0611, 1.0000],
12 [0.3696, 0.1955, -0.2866, 0.0000, -0.0793, -0.0643, 1.0000],
13 [0.3857, 0.1929, -0.2759, 0.0000, -0.0782, -0.0654, 1.0000],
14 [0.3964, 0.2089, -0.2786, 0.0000, -0.0761, -0.0654, 1.0000],
15 [0.3321, 0.1741, -0.3268, 0.0000, -0.0793, -0.0686, 1.0000],
16 [0.2250, 0.0964, -0.4232, 0.0000, -0.0932, -0.0761, 1.0000],
17 [0.0723, 0.0000, -0.5625, 0.0000, -0.1339, -0.0879, 1.0000],
18 [0.0536, 0.0000, -0.5652, 0.0000, -0.1521, -0.0921, 1.0000],
19 [0.0750, 0.0000, -0.5464, 0.0000, -0.1511, -0.0964, 1.0000],
20 [0.0723, 0.0000, -0.5411, 0.0000, -0.1414, -0.0986, 1.0000],
21 [0.0402, 0.0000, -0.5196, 0.0000, -0.1350, -0.1007, 1.0000],
22 [0.0080, 0.0000, -0.4795, 0.0000, -0.1189, -0.1018, 1.0000],
23 [0.0000, 0.0000, -0.4527, 0.0000, -0.0986, -0.1018, 1.0000],
24 [0.0000, 0.0000, -0.4313, 0.0000, -0.0846, -0.1018, 1.0000],
25 [-0.0455, -0.0268, -0.3509, 0.0000, -0.0568, -0.1018, 1.0000],
26 [-0.0964, -0.0482, -0.3321, 0.0000, -0.0439, -0.1039, 1.0000],
27 [-0.1768, -0.0562, -0.3402, 0.0000, -0.0300, -0.1050, 1.0000],
28 [-0.2438, -0.0429, -0.3187, 0.0000, -0.0193, -0.0996, 1.0000],
29 [-0.3054, -0.0054, -0.2893, 0.0000, -0.0139, -0.0932, 1.0000],
30 [-0.3509, 0.0000, -0.2598, 0.0000, -0.0054, -0.0879, 1.0000],
31 ],
32 )[None]
33 # normalization
34 normalized_action = np.copy(action)
35 normalized_action[..., :-1] = normalized_action[..., :-1] / np.maximum(np.abs(q99), np.abs(q01))[..., :-1]
36 normalized_action[..., -1] = normalized_action[..., -1] * 2.0 - 1.0 # scale to [-1, 1]
37 normalized_action = normalized_action.clip(-1.0, 1.0)
38 # tokenization
39 tokens = tokenizer.encode(normalized_action) # numpy (b, n, d) -> list of ints
40 print(tokens)
41 # decoding
42 decoded_action, padding_mask = tokenizer.decode(tokens) # list of ints -> numpy (b, n, d)
43 # calculate reconstruction error
44 mse_error = np.mean((normalized_action - decoded_action) ** 2)
45 l1_error = np.mean(np.abs(normalized_action - decoded_action))
46 print(f"Reconstruction MSE error: {mse_error:.6f}")
47 print(f"Reconstruction L1 error: {l1_error:.6f}")