Parvulus / action_utils.py
aasivas's picture
Upload folder using huggingface_hub
67c4f10 verified
Raw History Blame Contribute Delete
1.17 kB
"""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_