import collections import logging import math import time from dataclasses import dataclass from pathlib import Path from typing import Any, Dict, Optional, Sequence from collections import deque import numpy as np import cv2 as cv import json_numpy from deployment.model_server.tools.websocket_policy_client import WebsocketClientPolicy from starVLA.model.tools import read_mode_config try: from examples.SimplerEnv.eval_files.adaptive_ensemble import AdaptiveEnsembler except ImportError: AdaptiveEnsembler = None class ModelClient: def __init__( self, policy_ckpt_path, unnorm_key: Optional[str] = None, policy_setup: str = "robotwin", horizon: int = 0, action_ensemble=False, action_ensemble_horizon: Optional[int] = 3, image_size: list[int] = [224, 224], use_ddim: bool = True, num_ddim_steps: int = 10, adaptive_ensemble_alpha=0.1, host="127.0.0.1", port=5694, ) -> None: self.client = WebsocketClientPolicy(host, port) self.policy_setup = policy_setup self.unnorm_key = unnorm_key print(f"*** policy_setup: {policy_setup}, unnorm_key: {unnorm_key} ***") self.use_ddim = use_ddim self.num_ddim_steps = num_ddim_steps self.image_size = image_size self.horizon = horizon self.action_ensemble = action_ensemble and (AdaptiveEnsembler is not None) self.adaptive_ensemble_alpha = adaptive_ensemble_alpha self.action_ensemble_horizon = action_ensemble_horizon self.task_description = None self.image_history = deque(maxlen=self.horizon) if self.action_ensemble: self.action_ensembler = AdaptiveEnsembler(self.action_ensemble_horizon, self.adaptive_ensemble_alpha) else: self.action_ensembler = None self.num_image_history = 0 self.action_norm_stats = self.get_action_stats(self.unnorm_key, policy_ckpt_path=policy_ckpt_path) self.action_chunk_size = self.get_action_chunk_size(policy_ckpt_path=policy_ckpt_path) self.state_norm_stats = self.get_state_stats(self.unnorm_key, policy_ckpt_path=policy_ckpt_path) self.raw_actions = None def reset(self, task_description: str) -> None: self.task_description = task_description self.image_history.clear() if self.action_ensemble: self.action_ensembler.reset() self.num_image_history = 0 self.raw_actions = None def step( self, example: dict, step: int = 0, ) -> np.ndarray: state = example.get("state", None) # if state is not None: # state = self.normalize_state(state, self.state_norm_stats) # state = state[[0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 6, 13]] # example["state"] = state.reshape(1, -1) task_description = example.get("lang", None) images = example["image"] if example is not None: if task_description != self.task_description: self.reset(task_description) images = [self._resize_image(image) for image in images] example["image"] = images vla_input = { "examples": [example], "do_sample": False, "use_ddim": self.use_ddim, "num_ddim_steps": self.num_ddim_steps, } action_chunk_size = self.action_chunk_size if step % action_chunk_size == 0 or self.raw_actions is None: response = self.client.predict_action(vla_input) try: normalized_actions = response["data"]["normalized_actions"] # B, chunk, D except KeyError: print(f"Response data: {response}") raise KeyError(f"Key 'normalized_actions' not found in response data: {response['data'].keys()}") normalized_actions = normalized_actions[0] self.raw_actions = self.unnormalize_actions( normalized_actions=normalized_actions, action_norm_stats=self.action_norm_stats ) action_idx = step % action_chunk_size if action_idx >= len(self.raw_actions): pass current_action = self.raw_actions[action_idx] current_action = current_action[[0, 1, 2, 3, 4, 5, 12, 6, 7, 8, 9, 10, 11, 13]] return current_action @staticmethod def normalize_state(state: dict[str, np.ndarray], state_norm_stats: Dict[str, np.ndarray]) -> dict[str, np.ndarray]: """ Normalize the state """ mask = [True, True, True, True, True, True, True, True, True, True, True, True, False, False] mask = np.array(mask, dtype=bool) state_high, state_low = np.array(state_norm_stats["max"]), np.array(state_norm_stats["min"]) normalized_state = np.where( mask, (state - state_low) / (state_high - state_low) * 2 - 1, state, ) normalized_state = np.where(~mask, (normalized_state > 0.5).astype(normalized_state.dtype), normalized_state) return normalized_state @staticmethod def unnormalize_actions(normalized_actions: np.ndarray, action_norm_stats: Dict[str, np.ndarray]) -> np.ndarray: mask = action_norm_stats.get("mask", np.ones_like(action_norm_stats["min"], dtype=bool)) action_high, action_low = np.array(action_norm_stats["max"]), np.array(action_norm_stats["min"]) normalized_actions = np.clip(normalized_actions, -1, 1) actions = np.where( mask, 0.5 * (normalized_actions + 1) * (action_high - action_low) + action_low, normalized_actions, ) return actions @staticmethod def get_action_stats(unnorm_key: str, policy_ckpt_path) -> dict: policy_ckpt_path = Path(policy_ckpt_path) model_config, norm_stats = read_mode_config(policy_ckpt_path) unnorm_key = ModelClient._check_unnorm_key(norm_stats, unnorm_key) return norm_stats[unnorm_key]["action"] @staticmethod def get_state_stats(unnorm_key: str, policy_ckpt_path) -> dict: policy_ckpt_path = Path(policy_ckpt_path) model_config, norm_stats = read_mode_config(policy_ckpt_path) unnorm_key = ModelClient._check_unnorm_key(norm_stats, unnorm_key) return norm_stats[unnorm_key]["state"] @staticmethod def get_action_chunk_size(policy_ckpt_path): model_config, _ = read_mode_config(policy_ckpt_path) return model_config["framework"]["action_model"]["future_action_window_size"] + 1 def _resize_image(self, image: np.ndarray) -> np.ndarray: image = cv.resize(image, tuple(self.image_size), interpolation=cv.INTER_AREA) return image @staticmethod def _check_unnorm_key(norm_stats, unnorm_key): if unnorm_key is None: if len(norm_stats) == 1: unnorm_key = next(iter(norm_stats.keys())) else: unnorm_key = next(iter(norm_stats.keys())) if unnorm_key not in norm_stats: unnorm_key = next(iter(norm_stats.keys())) return unnorm_key def get_model(usr_args): policy_ckpt_path = usr_args.get("policy_ckpt_path") host = usr_args.get("host", "127.0.0.1") port = usr_args.get("port", 5694) unnorm_key = usr_args.get("unnorm_key", None) if policy_ckpt_path is None: raise ValueError("policy_ckpt_path must be provided in config") return ModelClient( policy_ckpt_path=policy_ckpt_path, host=host, port=port, unnorm_key=unnorm_key, ) def reset_model(model): model.reset(task_description="") def eval(TASK_ENV, model, observation): # Get instruction instruction = TASK_ENV.get_instruction() # Prepare images head_img = observation["observation"]["head_camera"]["rgb"] left_img = observation["observation"]["left_camera"]["rgb"] right_img = observation["observation"]["right_camera"]["rgb"] # Order: [head, left, right] to match training order images = [head_img, left_img, right_img] state = observation["joint_action"]["vector"] example = { "lang": str(instruction), "image": images, # "state": state, } action = model.step(example, step=TASK_ENV.take_action_cnt) # Execute action TASK_ENV.take_action(action)