lerobot/test2.py

62 lines
1.7 KiB
Python
Raw Permalink Normal View History

2024-10-01 21:57:09 +08:00
import time
2024-09-30 21:22:13 +08:00
import torch
from lerobot.common.datasets.factory import make_dataset
from lerobot.common.policies.factory import make_policy
2024-10-01 21:57:09 +08:00
from lerobot.common.robot_devices.utils import busy_wait
2024-09-30 21:22:13 +08:00
from lerobot.common.utils.utils import init_hydra_config, set_global_seed
from tests.utils import DEFAULT_CONFIG_PATH
def main(env_name, policy_name, extra_overrides):
cfg = init_hydra_config(
DEFAULT_CONFIG_PATH,
overrides=[
f"env={env_name}",
f"policy={policy_name}",
2024-10-01 21:57:09 +08:00
"device=mps",
2024-09-30 21:22:13 +08:00
]
+ extra_overrides,
)
set_global_seed(1337)
dataset = make_dataset(cfg)
policy = make_policy(cfg, dataset_stats=dataset.stats)
dataloader = torch.utils.data.DataLoader(
dataset,
num_workers=0,
batch_size=1,
shuffle=False,
)
batch = next(iter(dataloader))
obs = {}
for k in batch:
if k.startswith("observation"):
2024-10-01 21:57:09 +08:00
obs[k] = batch[k].to("mps")
# actions = policy.inference(obs)
fps = 30
2024-10-16 18:10:40 +08:00
for i in range(400):
2024-10-01 21:57:09 +08:00
start_loop_t = time.perf_counter()
2024-10-16 18:10:40 +08:00
next_action = policy.select_action(obs) # noqa: F841
2024-09-30 21:22:13 +08:00
2024-10-01 21:57:09 +08:00
dt_s = time.perf_counter() - start_loop_t
busy_wait(1 / fps - dt_s)
2024-09-30 21:22:13 +08:00
dt_s = time.perf_counter() - start_loop_t
print(
2024-10-16 18:10:40 +08:00
f"{i=}, {dt_s * 1000:5.2f} ({1/ dt_s:3.1f}hz) \t{policy._present_timestamp}\t{policy._present_action_timestamp}"
) # , {next_action.mean().item()}")
2024-10-01 21:57:09 +08:00
# time.sleep(1/30) # frequency at which we receive a new observation (30 Hz = 0.03 s)
# time.sleep(0.5) # frequency at which we receive a new observation (5 Hz = 0.2 s)
2024-09-30 21:22:13 +08:00
if __name__ == "__main__":
2024-10-01 21:57:09 +08:00
main("aloha", "act", ["policy.n_action_steps=100"])