File size: 13,616 Bytes
2d3265e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | from dotenv import load_dotenv
assert load_dotenv(), "Failed to load .env file. Make sure it exists and is properly formatted."
import dataclasses
import enum
import logging
import pathlib
import time
import os
import numpy as np
from openpi_client import websocket_client_policy as _websocket_client_policy
import polars as pl
import rich
import tqdm
import tyro
logger = logging.getLogger(__name__)
class EnvMode(enum.Enum):
"""Supported environments."""
ALOHA = "aloha"
ALOHA_SIM = "aloha_sim"
DROID = "droid"
LIBERO = "libero"
HFM = "hfm"
@dataclasses.dataclass
class Checkpoint:
"""Load a policy from a trained checkpoint."""
# Training config name (e.g., "pi0_aloha_sim").
config: str
# Checkpoint directory (e.g., "checkpoints/pi0_aloha_sim/exp/10000").
dir: str
@dataclasses.dataclass
class Args:
"""Command line arguments."""
# Host and port to connect to the server.
host: str = "0.0.0.0"
# Port to connect to the server. If None, the server will use the default port.
port: int | None = 8000
# API key to use for the server.
api_key: str | None = None
# # Number of steps to run the policy for.
# num_steps: int = 20
# Path to save the timings to a parquet file. (e.g., timing.parquet)
timing_file: pathlib.Path | None = None
# Environment to run the policy in.
env: EnvMode = EnvMode.HFM
task: str = "Pick_toys_into_box_and_lift_and_turn_and_put_on_the_chair_new_target_yaw"
# policy: Checkpoint = dataclasses.field(default_factory=Checkpoint)
class TimingRecorder:
"""Records timing measurements for different keys."""
def __init__(self) -> None:
self._timings: dict[str, list[float]] = {}
def record(self, key: str, time_ms: float) -> None:
"""Record a timing measurement for the given key."""
if key not in self._timings:
self._timings[key] = []
self._timings[key].append(time_ms)
def get_stats(self, key: str) -> dict[str, float]:
"""Get statistics for the given key."""
times = self._timings[key]
return {
"mean": float(np.mean(times)),
"std": float(np.std(times)),
"p25": float(np.quantile(times, 0.25)),
"p50": float(np.quantile(times, 0.50)),
"p75": float(np.quantile(times, 0.75)),
"p90": float(np.quantile(times, 0.90)),
"p95": float(np.quantile(times, 0.95)),
"p99": float(np.quantile(times, 0.99)),
}
def print_all_stats(self) -> None:
"""Print statistics for all keys in a concise format."""
table = rich.table.Table(
title="[bold blue]Timing Statistics[/bold blue]",
show_header=True,
header_style="bold white",
border_style="blue",
title_justify="center",
)
# Add metric column with custom styling
table.add_column("Metric", style="cyan", justify="left", no_wrap=True)
# Add statistical columns with consistent styling
stat_columns = [
("Mean", "yellow", "mean"),
("Std", "yellow", "std"),
("P25", "magenta", "p25"),
("P50", "magenta", "p50"),
("P75", "magenta", "p75"),
("P90", "magenta", "p90"),
("P95", "magenta", "p95"),
("P99", "magenta", "p99"),
]
for name, style, _ in stat_columns:
table.add_column(name, justify="right", style=style, no_wrap=True)
# Add rows for each metric with formatted values
for key in sorted(self._timings.keys()):
stats = self.get_stats(key)
values = [f"{stats[key]:.1f}" for _, _, key in stat_columns]
table.add_row(key, *values)
# Print with custom console settings
console = rich.console.Console(width=None, highlight=True)
console.print(table)
def write_parquet(self, path: pathlib.Path) -> None:
"""Save the timings to a parquet file."""
logger.info(f"Writing timings to {path}")
frame = pl.DataFrame(self._timings)
path.parent.mkdir(parents=True, exist_ok=True)
frame.write_parquet(path)
import torch
from PIL import Image
def pt_to_pil(x, normalizee=False):
s, b = (0.5, 0.5) if normalizee else (1.0, 0.0)
return Image.fromarray(
(((x.float() * s + b).clamp(0, 1))*255.0).permute(1,2,0).cpu().numpy().astype(np.uint8)
)
def obs_fn(sample, prompt):
return {
"observation/image": pt_to_pil(sample["observation.images.egocentric"]),
# "observation/arm_joints": sample["observation.arm_joints"].numpy(),
# "observation/hand_joints": sample["observation.hand_joints"].numpy(),
# "observation/leg_joints": sample["observation.leg_joints"].numpy(),
# "observation/torso_rpy": sample["observation.prev_rpy"].numpy(),
# "observation/base_height": sample["observation.prev_height"].numpy(),
"states": sample["states"].numpy()[:28],
"prompt": f"g1/{prompt}" #"g1/Remove_the_cap_turn_on_the_faucet_and_fill_the_bottle_with_water",
}
def main(args: Args) -> None:
from lerobot.common.datasets.lerobot_dataset import HF_LEROBOT_HOME
from lerobot.common.datasets.lerobot_dataset import LeRobotDataset
import lerobot.common.datasets.lerobot_dataset as lerobot_dataset
# ds = LeRobotDataset("pick_n_squat", root="/hfm/pick_n_squat")
root_dir = f"{os.environ['DATA_HOME']}/simple/G1WholebodyBendPick-v0-psi0"
action_horizon = 30
action_sequence_keys = ("action",)
meta = lerobot_dataset.LeRobotDatasetMetadata(root_dir)
delta_timestamps={
key: [t / meta.fps for t in range(action_horizon)] for key in action_sequence_keys
}
dataset = LeRobotDataset(root_dir, delta_timestamps=delta_timestamps)
episode_idx = 0
from_idx:int = dataset.episode_data_index["from"][episode_idx].item()
to_idx:int = dataset.episode_data_index["to"][episode_idx].item()
import dataclasses
import enum
import logging
import socket
import numpy as np
import tyro
import numpy as np
import einops
from openpi.policies import policy as _policy
from openpi.policies import policy_config as _policy_config
from openpi.serving import websocket_policy_server
from openpi.training import config as _config
# @dataclasses.dataclass
# class Args2:
# """Arguments for the serve_policy script."""
# # Environment to serve the policy for. This is only used when serving default policies.
# env: EnvMode = EnvMode.HFM
# # If provided, will be used in case the "prompt" key is not present in the data, or if the model doesn't have a default
# # prompt.
# default_prompt: str | None = "g1/Remove_the_cap_turn_on_the_faucet_and_fill_the_bottle_with_water"
# # Port to serve the policy on.
# port: int = 8000
# # Record the policy's behavior for debugging.
# record: bool = False
# # Specifies how to load the policy. If not provided, the default policy for the environment will be used.
# policy: Checkpoint = dataclasses.field(default_factory=Checkpoint)
# args2 = Args2(
# policy = Checkpoint(
# config="pi05_finetune_pick_n_squat",
# dir="/hfm/cache/openpi/checkpoints/pi05_finetune_pick_n_squat/pick_n_squat/20000"
# )
# )
# policy = _policy_config.create_trained_policy(
# _config.get_config(args2.policy.config), args2.policy.dir, default_prompt=args2.default_prompt
# )
policy = _websocket_client_policy.WebsocketClientPolicy(
host=args.host,
port=args.port,
api_key=args.api_key,
)
logger.info(f"Server metadata: {policy.get_server_metadata()}")
timing_recorder = TimingRecorder()
l1_losses = []
for i in tqdm.tqdm(range(from_idx, to_idx + 1, 10)):
sample = dataset[i]
# print(sample.keys())
# print(type(sample["action"])) # torch.Tensor, action
inference_start = time.time()
# print(f"============={i}=============")
obs = obs_fn(sample, prompt=args.task)
# print(obs["observation/image"].save(f"debug_obs_{i}.png"))
obs["observation/image"] = np.array(obs["observation/image"], dtype=np.uint8)
result = policy.infer(obs)
pred_actions = result["actions"]
# print(f"actions.shape {pred_actions.shape}")
n_action_dim = pred_actions.shape[-1]
gt_action = sample["action"][:, :n_action_dim].numpy() # FIXME
# print(f"gt_action.shape:{gt_action.shape}")
l1_loss = np.abs(gt_action - pred_actions)
timing_recorder.record("client_infer_ms", 1000 * (time.time() - inference_start))
# print(l1_loss)
# print("="*20)
l1_losses.append(l1_loss)
print("==== Final L1 Loss ====")
l1_losses = np.array(l1_losses)
print(f"L1 Loss shape: {l1_losses.shape}")
np.save("hfm_pick_n_squat_l1_loss.npy", l1_losses)
# print(f"Mean L1 Loss: {np.mean(l1_losses)}")
# print(f"Std L1 Loss: {np.std(l1_losses)}"
plot_error(l1_losses)
def plot_error(l1_losses=None):
import numpy as np
import matplotlib.pyplot as plt
if l1_losses is None:
l1_losses = np.load("hfm_pick_n_squat_l1_loss copy.npy")
print(f"L1 Loss shape: {l1_losses.shape}")
print(f"Mean L1 Loss: {np.mean(l1_losses)}")
if True:
errors_roll = np.rad2deg(l1_losses[:, 0, 28])
errors_pitch = np.rad2deg(l1_losses[:, 0, 29])
errors_yaw = np.rad2deg(l1_losses[:, 0, 30])
# Plot
plt.figure(figsize=(8, 4))
# plt.plot(np.rad2deg(errors_roll), marker='o')
plt.plot(errors_roll, label="Roll (0,0)")
plt.plot(errors_pitch, label="Pitch (0,1)")
plt.plot(errors_yaw, label="Yaw (0,2)")
plt.xlabel("Index")
plt.ylabel("L1 Loss")
plt.title("L1 Loss for Roll, Pitch, Yaw (l1_losses[:, 0, *]) (Degree) ")
plt.grid(True)
plt.legend()
# plt.show()
plt.savefig("l1_loss_plot_rpy.png", dpi=300, bbox_inches='tight')
plt.close()
if True:
errors_height = l1_losses[:, 0, 31]
# Plot
plt.figure(figsize=(8, 4))
plt.plot(errors_height, marker='o')
plt.xlabel("Index")
plt.ylabel("L1 Loss")
plt.title("L1 Loss for height (Meter)")
plt.grid(True)
plt.legend()
# plt.show()
plt.savefig("l1_loss_plot_height.png", dpi=300, bbox_inches='tight')
plt.close()
if True:
errors_vx = l1_losses[:, 0, 32]
# Plot
plt.figure(figsize=(8, 4))
plt.plot(errors_vx, marker='o')
plt.xlabel("Index")
plt.ylabel("L1 Loss")
plt.title("L1 Loss for Vx (m/s)")
plt.grid(True)
plt.legend()
# plt.show()
plt.savefig("l1_loss_plot_vx.png", dpi=300, bbox_inches='tight')
plt.close()
if True:
errors_vy = l1_losses[:, 0, 33]
# Plot
plt.figure(figsize=(8, 4))
plt.plot(errors_vy, marker='o')
plt.xlabel("Index")
plt.ylabel("L1 Loss")
plt.title("L1 Loss for Vy (m/s)")
plt.grid(True)
plt.legend()
# plt.show()
plt.savefig("l1_loss_plot_vy.png", dpi=300, bbox_inches='tight')
plt.close()
if True:
errors_vyaw = l1_losses[:, 0, 34]
# Plot
plt.figure(figsize=(8, 4))
plt.plot(errors_vyaw, marker='o')
plt.xlabel("Index")
plt.ylabel("L1 Loss")
plt.title("L1 Loss for Vyaw (m/s)")
plt.grid(True)
plt.legend()
# plt.show()
plt.savefig("l1_loss_plot_vyaw.png", dpi=300, bbox_inches='tight')
plt.close()
if True:
errors_target_yaw = np.rad2deg(l1_losses[:, 0, 35])
# Plot
plt.figure(figsize=(8, 4))
plt.plot(errors_target_yaw, marker='o')
plt.xlabel("Index")
plt.ylabel("L1 Loss")
plt.title("L1 Loss for Target Yaw (Degree)")
plt.grid(True)
plt.legend()
# plt.show()
plt.savefig("l1_loss_plot_target_yaw.png", dpi=300, bbox_inches='tight')
plt.close()
if True:
errors_arm = np.rad2deg(l1_losses[:, 0, 14:28])
plt.figure(figsize=(8, 4))
# Plot all 14 curves
plt.figure(figsize=(10, 5))
for i in range(errors_arm.shape[1]):
plt.plot(errors_arm[:, i], label=f"Arm Joint {i}")
plt.xlabel("Index")
plt.ylabel("L1 Loss")
plt.title("Arm Joint L1 Loss (joints 0–13) (Degree)")
plt.grid(True)
plt.legend(ncol=2, fontsize=8)
# plt.show()
plt.savefig("l1_loss_plot_arm.png", dpi=300, bbox_inches='tight')
plt.close()
if True:
errors_hand = np.rad2deg(l1_losses[:, 0, 0:14])
plt.figure(figsize=(8, 4))
# Plot all 14 curves
plt.figure(figsize=(10, 5))
for i in range(errors_hand.shape[1]):
plt.plot(errors_hand[:, i], label=f"Hand Joint {i}")
plt.xlabel("Index")
plt.ylabel("L1 Loss")
plt.title("Hand Joint L1 Loss (joints 0–13) (Degree)")
plt.grid(True)
plt.legend(ncol=2, fontsize=8)
# plt.show()
plt.savefig("l1_loss_plot_hand.png", dpi=300, bbox_inches='tight')
plt.close()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main(tyro.cli(Args))
# plot_error()
|