| |
| import os |
| import time |
| import torch |
|
|
| from lerobot.policies.smolvla.modeling_smolvla import SmolVLAPolicy |
| from lerobot.cameras.opencv.configuration_opencv import OpenCVCameraConfig |
| from lerobot.policies.utils import build_inference_frame, make_robot_action |
| from lerobot.robots.so101_follower import SO101FollowerConfig, SO101Follower |
| from lerobot.datasets.utils import hw_to_dataset_features |
| from lerobot.policies.factory import make_pre_post_processors |
|
|
| |
| |
| |
| MODEL_ID = "lerobot/smolvla_base" |
| DEVICE = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
| FOLLOWER_PORT = "/dev/ttyACM3" |
| TOP_CAM_INDEX = 7 |
| WRIST_CAM_INDEX = 9 |
|
|
| TASK = "Pick up the red block." |
| ROBOT_TYPE = "so101_follower" |
|
|
| FPS = 10 |
| STEPS_PER_EPISODE = 30 |
|
|
| os.environ.setdefault("TOKENIZERS_PARALLELISM", "false") |
|
|
| |
| |
| |
| print("[INFO] Loading SmolVLA policy") |
| policy = SmolVLAPolicy.from_pretrained(MODEL_ID).to(DEVICE) |
| policy.eval() |
|
|
| |
| |
| |
| print("[INFO] Loading policy preprocess + postprocess") |
| preprocess, postprocess = make_pre_post_processors( |
| policy.config, |
| MODEL_ID, |
| preprocessor_overrides={"device_processor": {"device": str(DEVICE)}}, |
| ) |
|
|
| |
| |
| |
| camera_cfg = { |
| "camera1": OpenCVCameraConfig(index_or_path=TOP_CAM_INDEX, width=640, height=480, fps=30), |
| "camera2": OpenCVCameraConfig(index_or_path=WRIST_CAM_INDEX, width=640, height=480, fps=30), |
| } |
|
|
| robot = SO101Follower( |
| SO101FollowerConfig( |
| port=FOLLOWER_PORT, |
| id="so101_follower_arm", |
| cameras=camera_cfg, |
| ) |
| ) |
| robot.connect() |
|
|
| |
| |
| |
| action_feats = hw_to_dataset_features(robot.action_features, "action") |
| obs_feats = hw_to_dataset_features(robot.observation_features, "observation") |
| ds_features = {**obs_feats, **action_feats} |
|
|
| |
| |
| |
| print("[INFO] Starting evaluation") |
| policy.reset() |
|
|
| try: |
| for step in range(STEPS_PER_EPISODE): |
| obs = robot.get_observation() |
| |
| |
| |
|
|
|
|
| obs_frame = build_inference_frame( |
| observation=obs, |
| ds_features=ds_features, |
| device=DEVICE, |
| task=TASK, |
| robot_type=ROBOT_TYPE, |
| ) |
|
|
| |
| batch = preprocess(obs_frame) |
|
|
| |
| with torch.no_grad(): |
| raw_action = policy.select_action(batch) |
|
|
|
|
| action = postprocess(raw_action) |
|
|
| print("Raw action:", raw_action, "-> Decoded action:", action) |
| |
| |
| import pdb; pdb.set_trace() |
| robot_action = make_robot_action(action.squeeze(0), ds_features) |
| robot.send_action(robot_action) |
|
|
| time.sleep(1.0 / FPS) |
|
|
| except KeyboardInterrupt: |
| print("\n[INFO] Ctrl+C received") |
|
|
| finally: |
| robot.disconnect() |
| print("[INFO] Done") |
|
|