File size: 1,169 Bytes
67c4f10 | 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 | """Action normalization utilities for the 7-DoF LIBERO action space:
(dx, dy, dz, drot_x, drot_y, drot_z, gripper).
"""
import json
from pathlib import Path
from typing import Tuple
import torch
DEFAULT_SCALE_PATH = Path(__file__).resolve().parent / "action_scale.json"
def load_action_scale(path: str | Path = DEFAULT_SCALE_PATH) -> Tuple[torch.Tensor, torch.Tensor]:
"""Loads per-dimension min/max normalization limits."""
path = Path(path)
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
action_min = torch.tensor(data["min"], dtype=torch.float32)
action_max = torch.tensor(data["max"], dtype=torch.float32)
return action_min, action_max
def normalize_action(raw: torch.Tensor, min_: torch.Tensor, max_: torch.Tensor) -> torch.Tensor:
"""Normalizes raw actions to [-1, 1]."""
span = (max_ - min_).clamp(min=1e-8)
return 2.0 * (raw - min_) / span - 1.0
def denormalize_action(scaled: torch.Tensor, min_: torch.Tensor, max_: torch.Tensor) -> torch.Tensor:
"""Denormalizes actions from [-1, 1] back to physical robot units."""
span = max_ - min_
return (scaled + 1.0) / 2.0 * span + min_
|