2025-07-03 18:35:14 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from dataclasses import dataclass, field
|
2025-07-03 16:35:37 +00:00
|
|
|
from typing import Any, Mapping
|
2025-07-03 18:35:14 +02:00
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import torch
|
|
|
|
|
from torch import Tensor
|
|
|
|
|
|
|
|
|
|
from lerobot.datasets.lerobot_dataset import LeRobotDataset
|
|
|
|
|
from lerobot.processor.pipeline import EnvTransition, ProcessorStepRegistry, TransitionIndex
|
|
|
|
|
|
|
|
|
|
|
2025-07-03 16:35:37 +00:00
|
|
|
def _convert_stats_to_tensors(stats: dict[str, dict[str, Any]]) -> dict[str, dict[str, Tensor]]:
|
2025-07-03 18:35:14 +02:00
|
|
|
"""Convert numpy arrays and other types to torch tensors."""
|
2025-07-03 16:35:37 +00:00
|
|
|
tensor_stats: dict[str, dict[str, Tensor]] = {}
|
2025-07-03 18:35:14 +02:00
|
|
|
for key, sub in stats.items():
|
|
|
|
|
tensor_stats[key] = {}
|
|
|
|
|
for stat_name, value in sub.items():
|
|
|
|
|
if isinstance(value, np.ndarray):
|
|
|
|
|
tensor_val = torch.from_numpy(value.astype(np.float32))
|
|
|
|
|
elif isinstance(value, torch.Tensor):
|
|
|
|
|
tensor_val = value.to(dtype=torch.float32)
|
|
|
|
|
elif isinstance(value, (int, float, list, tuple)):
|
|
|
|
|
tensor_val = torch.tensor(value, dtype=torch.float32)
|
|
|
|
|
else:
|
|
|
|
|
raise TypeError(f"Unsupported type for stats['{key}']['{stat_name}']: {type(value)}")
|
|
|
|
|
tensor_stats[key][stat_name] = tensor_val
|
|
|
|
|
return tensor_stats
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2025-07-04 12:09:40 +02:00
|
|
|
@ProcessorStepRegistry.register(name="normalizer_processor")
|
|
|
|
|
class NormalizerProcessor:
|
2025-07-04 12:55:35 +02:00
|
|
|
"""Normalizes observations and actions in a single processor step.
|
2025-07-04 12:09:40 +02:00
|
|
|
|
2025-07-04 12:55:35 +02:00
|
|
|
This processor handles normalization of both observation and action tensors
|
|
|
|
|
using either mean/std normalization or min/max scaling to a [-1, 1] range.
|
2025-07-04 12:09:40 +02:00
|
|
|
|
2025-07-04 12:55:35 +02:00
|
|
|
For each tensor key in the stats dictionary, the processor will:
|
|
|
|
|
- Use mean/std normalization if those statistics are provided: (x - mean) / std
|
|
|
|
|
- Use min/max scaling if those statistics are provided: 2 * (x - min) / (max - min) - 1
|
2025-07-04 12:09:40 +02:00
|
|
|
|
2025-07-04 12:55:35 +02:00
|
|
|
The processor can be configured to normalize only specific keys by setting
|
|
|
|
|
the normalize_keys parameter.
|
2025-07-03 18:35:14 +02:00
|
|
|
"""
|
|
|
|
|
|
2025-07-03 16:35:37 +00:00
|
|
|
stats: dict[str, dict[str, Any]]
|
2025-07-03 18:35:14 +02:00
|
|
|
normalize_keys: set[str] | None = None
|
|
|
|
|
eps: float = 1e-8
|
|
|
|
|
|
2025-07-03 16:35:37 +00:00
|
|
|
_tensor_stats: dict[str, dict[str, Tensor]] = field(default_factory=dict, init=False, repr=False)
|
2025-07-03 18:35:14 +02:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def from_lerobot_dataset(
|
|
|
|
|
cls,
|
|
|
|
|
dataset: LeRobotDataset,
|
|
|
|
|
*,
|
|
|
|
|
normalize_keys: set[str] | None = None,
|
|
|
|
|
eps: float = 1e-8,
|
2025-07-04 12:09:40 +02:00
|
|
|
) -> NormalizerProcessor:
|
|
|
|
|
return cls(stats=dataset.meta.stats, normalize_keys=normalize_keys, eps=eps)
|
2025-07-03 18:35:14 +02:00
|
|
|
|
|
|
|
|
def __post_init__(self):
|
|
|
|
|
self._tensor_stats = _convert_stats_to_tensors(self.stats)
|
|
|
|
|
|
2025-07-04 12:09:40 +02:00
|
|
|
def _normalize_obs(self, observation):
|
2025-07-03 18:35:14 +02:00
|
|
|
if observation is None:
|
2025-07-04 12:09:40 +02:00
|
|
|
return None
|
2025-07-03 18:35:14 +02:00
|
|
|
|
|
|
|
|
keys_to_norm = (
|
2025-07-04 12:09:40 +02:00
|
|
|
self.normalize_keys
|
|
|
|
|
if self.normalize_keys is not None
|
|
|
|
|
else {k for k in self._tensor_stats if k != "action"}
|
2025-07-03 18:35:14 +02:00
|
|
|
)
|
2025-07-04 12:09:40 +02:00
|
|
|
processed = dict(observation)
|
2025-07-03 18:35:14 +02:00
|
|
|
for key in keys_to_norm:
|
2025-07-04 12:09:40 +02:00
|
|
|
if key not in processed or key not in self._tensor_stats:
|
2025-07-03 18:35:14 +02:00
|
|
|
continue
|
|
|
|
|
|
2025-07-04 12:09:40 +02:00
|
|
|
orig_val = processed[key]
|
|
|
|
|
tensor = (
|
|
|
|
|
orig_val.to(dtype=torch.float32)
|
|
|
|
|
if isinstance(orig_val, torch.Tensor)
|
|
|
|
|
else torch.as_tensor(orig_val, dtype=torch.float32)
|
|
|
|
|
)
|
|
|
|
|
stats = {k: v.to(tensor.device) for k, v in self._tensor_stats[key].items()}
|
2025-07-03 18:35:14 +02:00
|
|
|
|
|
|
|
|
if "mean" in stats and "std" in stats:
|
|
|
|
|
mean, std = stats["mean"], stats["std"]
|
2025-07-04 12:09:40 +02:00
|
|
|
processed[key] = (tensor - mean) / (std + self.eps)
|
2025-07-03 18:35:14 +02:00
|
|
|
elif "min" in stats and "max" in stats:
|
|
|
|
|
min_val, max_val = stats["min"], stats["max"]
|
2025-07-04 12:09:40 +02:00
|
|
|
processed[key] = 2 * (tensor - min_val) / (max_val - min_val + self.eps) - 1
|
|
|
|
|
return processed
|
|
|
|
|
|
|
|
|
|
def _normalize_action(self, action):
|
|
|
|
|
if action is None or "action" not in self._tensor_stats:
|
|
|
|
|
return action
|
|
|
|
|
|
|
|
|
|
tensor = (
|
|
|
|
|
action.to(dtype=torch.float32)
|
|
|
|
|
if isinstance(action, torch.Tensor)
|
|
|
|
|
else torch.as_tensor(action, dtype=torch.float32)
|
|
|
|
|
)
|
|
|
|
|
stats = {k: v.to(tensor.device) for k, v in self._tensor_stats["action"].items()}
|
|
|
|
|
if "mean" in stats and "std" in stats:
|
|
|
|
|
mean, std = stats["mean"], stats["std"]
|
|
|
|
|
return (tensor - mean) / (std + self.eps)
|
|
|
|
|
if "min" in stats and "max" in stats:
|
|
|
|
|
min_val, max_val = stats["min"], stats["max"]
|
|
|
|
|
return 2 * (tensor - min_val) / (max_val - min_val + self.eps) - 1
|
|
|
|
|
raise ValueError("Action stats must contain either ('mean','std') or ('min','max')")
|
2025-07-03 18:35:14 +02:00
|
|
|
|
2025-07-04 12:09:40 +02:00
|
|
|
def __call__(self, transition: EnvTransition) -> EnvTransition:
|
|
|
|
|
observation = self._normalize_obs(transition[TransitionIndex.OBSERVATION])
|
|
|
|
|
action = self._normalize_action(transition[TransitionIndex.ACTION])
|
2025-07-03 18:35:14 +02:00
|
|
|
return (
|
2025-07-04 12:09:40 +02:00
|
|
|
observation,
|
|
|
|
|
action,
|
2025-07-03 18:35:14 +02:00
|
|
|
transition[TransitionIndex.REWARD],
|
|
|
|
|
transition[TransitionIndex.DONE],
|
|
|
|
|
transition[TransitionIndex.TRUNCATED],
|
|
|
|
|
transition[TransitionIndex.INFO],
|
|
|
|
|
transition[TransitionIndex.COMPLEMENTARY_DATA],
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-03 16:35:37 +00:00
|
|
|
def get_config(self) -> dict[str, Any]:
|
2025-07-04 12:09:40 +02:00
|
|
|
return {"normalize_keys": list(self.normalize_keys) if self.normalize_keys else None, "eps": self.eps}
|
2025-07-03 18:35:14 +02:00
|
|
|
|
2025-07-03 16:35:37 +00:00
|
|
|
def state_dict(self) -> dict[str, Tensor]:
|
2025-07-04 12:09:40 +02:00
|
|
|
flat = {}
|
2025-07-03 18:35:14 +02:00
|
|
|
for key, sub in self._tensor_stats.items():
|
|
|
|
|
for stat_name, tensor in sub.items():
|
2025-07-04 12:09:40 +02:00
|
|
|
flat[f"{key}.{stat_name}"] = tensor
|
|
|
|
|
return flat
|
2025-07-03 18:35:14 +02:00
|
|
|
|
|
|
|
|
def load_state_dict(self, state: Mapping[str, Tensor]) -> None:
|
|
|
|
|
self._tensor_stats.clear()
|
|
|
|
|
for flat_key, tensor in state.items():
|
2025-07-04 10:53:40 +02:00
|
|
|
key, stat_name = flat_key.rsplit(".", 1)
|
2025-07-04 12:09:40 +02:00
|
|
|
self._tensor_stats.setdefault(key, {})[stat_name] = tensor
|
2025-07-03 18:35:14 +02:00
|
|
|
|
2025-07-04 12:09:40 +02:00
|
|
|
def reset(self):
|
2025-07-03 18:35:14 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2025-07-04 12:09:40 +02:00
|
|
|
@ProcessorStepRegistry.register(name="unnormalizer_processor")
|
|
|
|
|
class UnnormalizerProcessor:
|
|
|
|
|
"""Inverse normalisation for observations and actions.
|
2025-07-03 18:35:14 +02:00
|
|
|
|
2025-07-04 12:09:40 +02:00
|
|
|
Exactly mirrors :class:`NormalizerProcessor` but applies the inverse
|
|
|
|
|
transform.
|
2025-07-03 18:35:14 +02:00
|
|
|
"""
|
|
|
|
|
|
2025-07-03 16:35:37 +00:00
|
|
|
stats: dict[str, dict[str, Any]]
|
2025-07-04 12:09:40 +02:00
|
|
|
unnormalize_keys: set[str] | None = None
|
2025-07-03 18:35:14 +02:00
|
|
|
eps: float = 1e-8
|
|
|
|
|
|
2025-07-03 16:35:37 +00:00
|
|
|
_tensor_stats: dict[str, dict[str, Tensor]] = field(default_factory=dict, init=False, repr=False)
|
2025-07-03 18:35:14 +02:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def from_lerobot_dataset(
|
|
|
|
|
cls,
|
|
|
|
|
dataset: LeRobotDataset,
|
|
|
|
|
*,
|
2025-07-04 12:09:40 +02:00
|
|
|
unnormalize_keys: set[str] | None = None,
|
2025-07-03 18:35:14 +02:00
|
|
|
eps: float = 1e-8,
|
2025-07-04 12:09:40 +02:00
|
|
|
) -> UnnormalizerProcessor:
|
|
|
|
|
return cls(stats=dataset.meta.stats, unnormalize_keys=unnormalize_keys, eps=eps)
|
2025-07-03 18:35:14 +02:00
|
|
|
|
|
|
|
|
def __post_init__(self):
|
|
|
|
|
self._tensor_stats = _convert_stats_to_tensors(self.stats)
|
|
|
|
|
|
2025-07-04 12:09:40 +02:00
|
|
|
def _unnormalize_obs(self, observation):
|
|
|
|
|
if observation is None:
|
|
|
|
|
return None
|
|
|
|
|
keys = (
|
|
|
|
|
self.unnormalize_keys
|
|
|
|
|
if self.unnormalize_keys is not None
|
|
|
|
|
else {k for k in self._tensor_stats if k != "action"}
|
|
|
|
|
)
|
|
|
|
|
processed = dict(observation)
|
|
|
|
|
for key in keys:
|
|
|
|
|
if key not in processed or key not in self._tensor_stats:
|
|
|
|
|
continue
|
|
|
|
|
orig_val = processed[key]
|
|
|
|
|
tensor = (
|
|
|
|
|
orig_val.to(dtype=torch.float32)
|
|
|
|
|
if isinstance(orig_val, torch.Tensor)
|
|
|
|
|
else torch.as_tensor(orig_val, dtype=torch.float32)
|
2025-07-03 18:35:14 +02:00
|
|
|
)
|
2025-07-04 12:09:40 +02:00
|
|
|
stats = {k: v.to(tensor.device) for k, v in self._tensor_stats[key].items()}
|
2025-07-03 18:35:14 +02:00
|
|
|
if "mean" in stats and "std" in stats:
|
|
|
|
|
mean, std = stats["mean"], stats["std"]
|
2025-07-04 12:09:40 +02:00
|
|
|
processed[key] = tensor * std + mean
|
2025-07-03 18:35:14 +02:00
|
|
|
elif "min" in stats and "max" in stats:
|
|
|
|
|
min_val, max_val = stats["min"], stats["max"]
|
2025-07-04 12:09:40 +02:00
|
|
|
processed[key] = (tensor + 1) / 2 * (max_val - min_val) + min_val
|
|
|
|
|
return processed
|
|
|
|
|
|
|
|
|
|
def _unnormalize_action(self, action):
|
|
|
|
|
if action is None or "action" not in self._tensor_stats:
|
|
|
|
|
return action
|
|
|
|
|
tensor = (
|
|
|
|
|
action.to(dtype=torch.float32)
|
|
|
|
|
if isinstance(action, torch.Tensor)
|
|
|
|
|
else torch.as_tensor(action, dtype=torch.float32)
|
|
|
|
|
)
|
|
|
|
|
stats = {k: v.to(tensor.device) for k, v in self._tensor_stats["action"].items()}
|
|
|
|
|
if "mean" in stats and "std" in stats:
|
|
|
|
|
mean, std = stats["mean"], stats["std"]
|
|
|
|
|
return tensor * std + mean
|
|
|
|
|
if "min" in stats and "max" in stats:
|
|
|
|
|
min_val, max_val = stats["min"], stats["max"]
|
|
|
|
|
return (tensor + 1) / 2 * (max_val - min_val) + min_val
|
|
|
|
|
raise ValueError("Action stats must contain either ('mean','std') or ('min','max')")
|
2025-07-03 18:35:14 +02:00
|
|
|
|
2025-07-04 12:09:40 +02:00
|
|
|
def __call__(self, transition: EnvTransition) -> EnvTransition:
|
|
|
|
|
observation = self._unnormalize_obs(transition[TransitionIndex.OBSERVATION])
|
|
|
|
|
action = self._unnormalize_action(transition[TransitionIndex.ACTION])
|
2025-07-03 18:35:14 +02:00
|
|
|
return (
|
|
|
|
|
observation,
|
|
|
|
|
action,
|
|
|
|
|
transition[TransitionIndex.REWARD],
|
|
|
|
|
transition[TransitionIndex.DONE],
|
|
|
|
|
transition[TransitionIndex.TRUNCATED],
|
|
|
|
|
transition[TransitionIndex.INFO],
|
|
|
|
|
transition[TransitionIndex.COMPLEMENTARY_DATA],
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-03 16:35:37 +00:00
|
|
|
def get_config(self) -> dict[str, Any]:
|
2025-07-03 18:35:14 +02:00
|
|
|
return {
|
2025-07-04 12:09:40 +02:00
|
|
|
"unnormalize_keys": list(self.unnormalize_keys) if self.unnormalize_keys else None,
|
2025-07-03 18:35:14 +02:00
|
|
|
"eps": self.eps,
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-03 16:35:37 +00:00
|
|
|
def state_dict(self) -> dict[str, Tensor]:
|
2025-07-04 12:09:40 +02:00
|
|
|
flat = {}
|
2025-07-03 18:35:14 +02:00
|
|
|
for key, sub in self._tensor_stats.items():
|
|
|
|
|
for stat_name, tensor in sub.items():
|
2025-07-04 12:09:40 +02:00
|
|
|
flat[f"{key}.{stat_name}"] = tensor
|
|
|
|
|
return flat
|
2025-07-03 18:35:14 +02:00
|
|
|
|
|
|
|
|
def load_state_dict(self, state: Mapping[str, Tensor]) -> None:
|
|
|
|
|
self._tensor_stats.clear()
|
|
|
|
|
for flat_key, tensor in state.items():
|
2025-07-04 10:53:40 +02:00
|
|
|
key, stat_name = flat_key.rsplit(".", 1)
|
2025-07-04 12:09:40 +02:00
|
|
|
self._tensor_stats.setdefault(key, {})[stat_name] = tensor
|
2025-07-03 18:35:14 +02:00
|
|
|
|
2025-07-04 12:09:40 +02:00
|
|
|
def reset(self):
|
2025-07-03 18:35:14 +02:00
|
|
|
pass
|