Views
No views yet
pip install torch numpy matplotlib einops transformersdevice="cpu".1from transformers import AutoProcessor
2import torch
3
4# Initialize the BEAST processor with configuration parameters:
5# - num_dof: degrees of freedom (3 for 3D trajectories like x, y, z)
6# - num_basis: number of B-spline basis functions used for trajectory representation
7# - seq_len: length of the trajectory sequence (number of time steps)
8# - degree_p: degree of the B-spline polynomial (3 = cubic spline)
9# - device: computation device ('cpu' or 'cuda')
10beast = AutoProcessor.from_pretrained(
11 "zhouhongyi/beast",
12 trust_remote_code=True,
13 num_dof = 3,
14 num_basis = 20,
15 seq_len = 50,
16 degree_p = 3,
17 device = 'cpu'
18)
19
20# Create random trajectory data: 10 trajectories, each with 50 time steps, 3 dimensions
21trajectories = torch.randn(10, 50, 3)
22
23# Encode trajectories into discrete tokens
24# update_bounds=True allows the processor to adaptively update quantization bounds
25tokens = beast.encode_discrete(trajectories, update_bounds=True)
26print(f"Encoded tokens shape: {tokens.shape}")
27
28# Decode tokens back to continuous trajectories
29reconstructed_trajectories = beast.decode_discrete(tokens)
30print(f"Reconstructed trajectories shape: {reconstructed_trajectories.shape}")
31
32# Calculate mean squared error to measure reconstruction quality
33mse_loss = torch.mean((trajectories - reconstructed_trajectories) ** 2)
34print(f"MSE Loss: {mse_loss.item()}")
35
36# Visualize the reconstruction error for analysis
37beast.visualize_reconstruction_error_discrete(trajectories)1# Encode to normalized continuous parameters [-1, 1]
2params = beast.encode_continuous(trajectories, update_bounds=True)
3
4# Decode back
5reconstructed = beast.decode_continuous(params)| Parameter | Description | Default |
|---|---|---|
num_dof | Total degrees of freedom (robot joints + gripper) | 7 |
num_basis | Number of B-spline basis functions. Higher values improve reconstruction fidelity but produce more tokens | 10 |
seq_len | Trajectory sequence length (number of timesteps) | 50 |
vocab_size | Discrete vocabulary size (256 = 8-bit tokens) | 256 |
degree_p | B-spline polynomial degree. Higher degrees produce smoother curves (3=cubic, 4=quartic) | 4 |
device | Torch device ("cuda" or "cpu") | "cuda" |
gripper_zero_order | Use piecewise-constant (degree 0) splines for gripper DOFs. Useful for binary gripper states | False |
gripper_dof | Number of gripper DOFs, assumed to be in the end. Only used when gripper_zero_order=True | 1 |
enforce_init_pos | Enforce initial position constraint during decoding | False |
num_basis * num_dofencode_discrete(trajs, update_bounds=True)[batch, seq_len, num_dof][batch, num_basis * num_dof] in range [0, vocab_size-1]update_bounds: Whether to update internal weight bounds from this batchencode_continuous(trajs, update_bounds=True)[batch, seq_len, num_dof][batch, num_basis * num_dof] in range [-1, 1]decode_discrete(tokens, times=None, init_pos=None)[batch, num_basis * num_dof][batch, seq_len, num_dof]times: Custom time points (optional, defaults to seq_len uniform points)init_pos: Initial position constraint (optional)decode_continuous(params, times=None, init_pos=None)[batch, num_basis * num_dof][batch, seq_len, num_dof]compute_reconstruction_error(raw_traj)visualize_reconstruction_error_discrete(raw_traj) / visualize_reconstruction_error_continuous(raw_traj)1@inproceedings{
2zhou2025beast,
3title={{BEAST}: Efficient Tokenization of B-Splines Encoded Action Sequences for Imitation Learning},
4author={Hongyi Zhou and Weiran Liao and Xi Huang and Yucheng Tang and Fabian Otto and Xiaogang Jia and Xinkai Jiang and Simon Hilber and Ge Li and Qian Wang and {\"O}mer Erdin{\c{c}} Ya{\u{g}}murlu and Nils Blank and Moritz Reuss and Rudolf Lioutikov},
5booktitle={The Thirty-ninth Annual Conference on Neural Information Processing Systems},
6year={2025},
7url={https://openreview.net/forum?id=rQCl1sf62w}
8}