File size: 3,315 Bytes
46bb2e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Minimal loader for openroboto-ai/axis-franka-datapool.

Install with: pip install "lerobot>=0.4" torch numpy
"""

from __future__ import annotations

import numpy as np
import torch
from torch.utils.data import Dataset

FINGER_OPEN = 0.04
PI05_DT = 1.0 / 15.0


def _closedness(fingers: torch.Tensor) -> torch.Tensor:
    return torch.clamp(1.0 - fingers.mean(dim=-1) / FINGER_OPEN, 0.0, 1.0)


def to_pi05_droid(state: torch.Tensor, action: torch.Tensor):
    """Convert raw 9D qpos/qpos-target into Pi0.5-Axis's 8D DROID view."""
    if state.shape[-1] != 9 or action.shape[-1] != 9:
        raise ValueError(f"Expected 9D state/action, got {state.shape}/{action.shape}")
    converted_state = torch.cat([state[..., 2:9], _closedness(state[..., :2])[..., None]], dim=-1)
    arm_action = torch.clamp((action[..., 2:9] - state[..., 2:9]) / PI05_DT, -1.0, 1.0)
    converted_action = torch.cat([arm_action, _closedness(action[..., :2])[..., None]], dim=-1)
    return converted_state, converted_action


class AxisFrankaDataset(Dataset):
    """LeRobot-backed dataset with explicit raw-joint or Pi0.5/DROID action view."""

    def __init__(
        self,
        repo_id="openroboto-ai/axis-franka-datapool",
        *,
        revision="v0.1-metadata",
        action_space="joint_position_target",
        sample_hz=30,
        root=None,
    ):
        from lerobot.datasets.lerobot_dataset import LeRobotDataset

        if action_space not in {"joint_position_target", "pi05_droid"}:
            raise ValueError("action_space must be 'joint_position_target' or 'pi05_droid'")
        if sample_hz not in {15, 30}:
            raise ValueError("sample_hz must be 15 or 30")
        self.raw = LeRobotDataset(
            repo_id=repo_id,
            revision=revision,
            root=root,
            download_videos=False,
        )
        self.action_space = action_space
        if sample_hz == 30:
            self.indices = None
        else:
            # AXIS was recorded at 30 Hz.  Taking even frame indices within
            # each episode exactly reproduces the official 2x temporal stride.
            # Use the compact episode table rather than formatting/scanning
            # all 2.8M frame_index values through the torch dataset transform.
            episodes = self.raw.meta.episodes
            starts = np.asarray(episodes["dataset_from_index"], dtype=np.int64)
            stops = np.asarray(episodes["dataset_to_index"], dtype=np.int64)
            self.indices = np.concatenate(
                [np.arange(start, stop, 2, dtype=np.int64) for start, stop in zip(starts, stops)]
            )

    def __len__(self):
        return len(self.raw) if self.indices is None else len(self.indices)

    def __getitem__(self, index):
        raw_index = int(index) if self.indices is None else int(self.indices[index])
        sample = self.raw[raw_index]
        if self.action_space == "joint_position_target":
            return sample
        result = dict(sample)
        result["observation.state"], result["action"] = to_pi05_droid(
            sample["observation.state"], sample["action"]
        )
        return result


def load_dataset(**kwargs):
    """Convenience entry point; keyword arguments are passed to AxisFrankaDataset."""
    return AxisFrankaDataset(**kwargs)