镜像自地址
https://github.com/MINT-SJTU/Evo-RL.git
已同步 2026-11-20 18:17:06 +00:00
fix: stabilize human-inloop dataset schema
这个提交包含在:
@ -0,0 +1,107 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Copyright 2026 The HuggingFace Inc. team. All rights reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
"""Patch legacy human-in-loop datasets so they can merge with newer rollout datasets."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from lerobot.configs import parser
|
||||||
|
from lerobot.datasets.dataset_tools import add_features
|
||||||
|
from lerobot.datasets.lerobot_dataset import LeRobotDataset
|
||||||
|
from lerobot.scripts.lerobot_record import _ensure_human_inloop_compatible_features
|
||||||
|
from lerobot.utils.constants import ACTION
|
||||||
|
from lerobot.utils.utils import init_logging
|
||||||
|
|
||||||
|
|
||||||
|
MISSING_HIL_FEATURES = (
|
||||||
|
"complementary_info.policy_action",
|
||||||
|
"complementary_info.is_intervention",
|
||||||
|
"complementary_info.state",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class PatchHilDatasetSchemaConfig:
|
||||||
|
repo_id: str
|
||||||
|
root: str | None = None
|
||||||
|
output_repo_id: str | None = None
|
||||||
|
output_dir: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def _build_missing_hil_features(dataset: LeRobotDataset) -> dict[str, tuple[np.ndarray, dict]]:
|
||||||
|
action_feature = dataset.features[ACTION]
|
||||||
|
action_names = action_feature["names"]
|
||||||
|
if action_names is None:
|
||||||
|
action_names = [f"action_{idx}" for idx in range(action_feature["shape"][0])]
|
||||||
|
else:
|
||||||
|
action_names = list(action_names)
|
||||||
|
|
||||||
|
feature_defs: dict[str, dict] = {}
|
||||||
|
_ensure_human_inloop_compatible_features(feature_defs, action_feature_names=action_names)
|
||||||
|
|
||||||
|
num_frames = dataset.num_frames
|
||||||
|
num_action_dims = len(action_names)
|
||||||
|
values_by_feature = {
|
||||||
|
"complementary_info.policy_action": np.zeros((num_frames, num_action_dims), dtype=np.float32),
|
||||||
|
"complementary_info.is_intervention": np.zeros((num_frames, 1), dtype=np.float32),
|
||||||
|
"complementary_info.state": np.zeros((num_frames, 1), dtype=np.float32),
|
||||||
|
}
|
||||||
|
|
||||||
|
missing_features = {}
|
||||||
|
for feature_name in MISSING_HIL_FEATURES:
|
||||||
|
if feature_name not in dataset.features:
|
||||||
|
missing_features[feature_name] = (values_by_feature[feature_name], feature_defs[feature_name])
|
||||||
|
|
||||||
|
return missing_features
|
||||||
|
|
||||||
|
|
||||||
|
@parser.wrap()
|
||||||
|
def patch_hil_dataset_schema(cfg: PatchHilDatasetSchemaConfig) -> LeRobotDataset:
|
||||||
|
init_logging()
|
||||||
|
|
||||||
|
dataset = LeRobotDataset(cfg.repo_id, root=cfg.root)
|
||||||
|
missing_features = _build_missing_hil_features(dataset)
|
||||||
|
if not missing_features:
|
||||||
|
logging.info("Dataset already has a merge-compatible human-in-loop schema. Nothing to patch.")
|
||||||
|
return dataset
|
||||||
|
|
||||||
|
output_repo_id = cfg.output_repo_id or f"{cfg.repo_id}_hil_schema_patched"
|
||||||
|
output_dir = Path(cfg.output_dir) if cfg.output_dir is not None else None
|
||||||
|
if output_dir is None and cfg.root is not None:
|
||||||
|
src_root = Path(cfg.root)
|
||||||
|
output_dir = src_root.parent / f"{src_root.name}_hil_schema_patched"
|
||||||
|
|
||||||
|
logging.info("Adding missing human-in-loop schema features: %s", sorted(missing_features))
|
||||||
|
patched_dataset = add_features(
|
||||||
|
dataset=dataset,
|
||||||
|
features=missing_features,
|
||||||
|
output_dir=output_dir,
|
||||||
|
repo_id=output_repo_id,
|
||||||
|
)
|
||||||
|
logging.info("Patched dataset saved to %s", patched_dataset.root)
|
||||||
|
return patched_dataset
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
patch_hil_dataset_schema()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@ -295,6 +295,29 @@ class RecordConfig:
|
|||||||
return ["policy"]
|
return ["policy"]
|
||||||
|
|
||||||
|
|
||||||
|
def _ensure_human_inloop_compatible_features(
|
||||||
|
dataset_features: dict[str, dict],
|
||||||
|
*,
|
||||||
|
action_feature_names: list[str],
|
||||||
|
) -> None:
|
||||||
|
# Keep human-in-loop datasets schema-stable across teleop-only and policy-assisted phases so they can merge.
|
||||||
|
dataset_features["complementary_info.policy_action"] = {
|
||||||
|
"dtype": "float32",
|
||||||
|
"shape": (len(action_feature_names),),
|
||||||
|
"names": action_feature_names,
|
||||||
|
}
|
||||||
|
dataset_features["complementary_info.is_intervention"] = {
|
||||||
|
"dtype": "float32",
|
||||||
|
"shape": (1,),
|
||||||
|
"names": ["is_intervention"],
|
||||||
|
}
|
||||||
|
dataset_features["complementary_info.state"] = {
|
||||||
|
"dtype": "float32",
|
||||||
|
"shape": (1,),
|
||||||
|
"names": ["state"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@parser.wrap()
|
@parser.wrap()
|
||||||
def record(cfg: RecordConfig) -> LeRobotDataset:
|
def record(cfg: RecordConfig) -> LeRobotDataset:
|
||||||
init_logging()
|
init_logging()
|
||||||
@ -330,24 +353,10 @@ def record(cfg: RecordConfig) -> LeRobotDataset:
|
|||||||
use_videos=cfg.dataset.video,
|
use_videos=cfg.dataset.video,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
if cfg.intervention_state_machine_enabled and cfg.policy is not None and cfg.teleop is not None:
|
if cfg.teleop is not None and cfg.enable_collector_policy_id:
|
||||||
action_names = dataset_features[ACTION]["names"]
|
action_names = dataset_features[ACTION]["names"]
|
||||||
action_names = list(robot.action_features) if action_names is None else list(action_names)
|
action_names = list(robot.action_features) if action_names is None else list(action_names)
|
||||||
dataset_features["complementary_info.policy_action"] = {
|
_ensure_human_inloop_compatible_features(dataset_features, action_feature_names=action_names)
|
||||||
"dtype": "float32",
|
|
||||||
"shape": (len(action_names),),
|
|
||||||
"names": action_names,
|
|
||||||
}
|
|
||||||
dataset_features["complementary_info.is_intervention"] = {
|
|
||||||
"dtype": "float32",
|
|
||||||
"shape": (1,),
|
|
||||||
"names": ["is_intervention"],
|
|
||||||
}
|
|
||||||
dataset_features["complementary_info.state"] = {
|
|
||||||
"dtype": "float32",
|
|
||||||
"shape": (1,),
|
|
||||||
"names": ["state"],
|
|
||||||
}
|
|
||||||
if cfg.enable_collector_policy_id:
|
if cfg.enable_collector_policy_id:
|
||||||
dataset_features["complementary_info.collector_policy_id"] = {
|
dataset_features["complementary_info.collector_policy_id"] = {
|
||||||
"dtype": "string",
|
"dtype": "string",
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import numpy as np
|
|||||||
import pytest
|
import pytest
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
|
from lerobot.datasets.dataset_tools import merge_datasets, remove_feature
|
||||||
from lerobot.datasets.lerobot_dataset import LeRobotDataset
|
from lerobot.datasets.lerobot_dataset import LeRobotDataset
|
||||||
from lerobot.scripts.lerobot_calibrate import CalibrateConfig, calibrate
|
from lerobot.scripts.lerobot_calibrate import CalibrateConfig, calibrate
|
||||||
from lerobot.scripts.lerobot_human_inloop_record import (
|
from lerobot.scripts.lerobot_human_inloop_record import (
|
||||||
@ -31,6 +32,7 @@ from lerobot.scripts.lerobot_human_inloop_record import (
|
|||||||
_slow_reset_all_arms_to_pose,
|
_slow_reset_all_arms_to_pose,
|
||||||
human_inloop_record,
|
human_inloop_record,
|
||||||
)
|
)
|
||||||
|
from lerobot.scripts.lerobot_patch_hil_dataset_schema import PatchHilDatasetSchemaConfig, patch_hil_dataset_schema
|
||||||
from lerobot.scripts.lerobot_record import (
|
from lerobot.scripts.lerobot_record import (
|
||||||
ACPInferenceConfig,
|
ACPInferenceConfig,
|
||||||
DatasetRecordConfig,
|
DatasetRecordConfig,
|
||||||
@ -163,13 +165,86 @@ def test_human_inloop_record_works_without_policy_and_saves_annotations(tmp_path
|
|||||||
assert cfg.intervention_state_machine_enabled is False
|
assert cfg.intervention_state_machine_enabled is False
|
||||||
assert cfg.collector_policy_id_policy == "human"
|
assert cfg.collector_policy_id_policy == "human"
|
||||||
assert "complementary_info.collector_policy_id" in dataset.features
|
assert "complementary_info.collector_policy_id" in dataset.features
|
||||||
|
assert "complementary_info.policy_action" in dataset.features
|
||||||
|
assert "complementary_info.is_intervention" in dataset.features
|
||||||
|
assert "complementary_info.state" in dataset.features
|
||||||
|
|
||||||
reloaded = LeRobotDataset(DUMMY_REPO_ID, root=root)
|
reloaded = LeRobotDataset(DUMMY_REPO_ID, root=root)
|
||||||
assert reloaded[0]["complementary_info.collector_policy_id"] == "human"
|
assert reloaded[0]["complementary_info.collector_policy_id"] == "human"
|
||||||
|
torch.testing.assert_close(
|
||||||
|
reloaded[0]["complementary_info.policy_action"],
|
||||||
|
torch.zeros_like(reloaded[0]["action"]),
|
||||||
|
)
|
||||||
|
assert float(reloaded[0]["complementary_info.is_intervention"]) == 0.0
|
||||||
|
assert float(reloaded[0]["complementary_info.state"]) == 0.0
|
||||||
assert "episode_success" in reloaded.meta.episodes.column_names
|
assert "episode_success" in reloaded.meta.episodes.column_names
|
||||||
assert reloaded.meta.episodes[0]["episode_success"] == "failure"
|
assert reloaded.meta.episodes[0]["episode_success"] == "failure"
|
||||||
|
|
||||||
|
|
||||||
|
def test_patch_hil_dataset_schema_restores_legacy_dataset_mergeability(tmp_path):
|
||||||
|
robot_cfg = MockRobotConfig()
|
||||||
|
teleop_cfg = MockTeleopConfig()
|
||||||
|
current_root = tmp_path / "hil_current"
|
||||||
|
legacy_root = tmp_path / "hil_legacy"
|
||||||
|
patched_root = tmp_path / "hil_patched"
|
||||||
|
merged_root = tmp_path / "hil_merged"
|
||||||
|
dataset_cfg = DatasetRecordConfig(
|
||||||
|
repo_id=DUMMY_REPO_ID,
|
||||||
|
single_task="Dummy task",
|
||||||
|
root=current_root,
|
||||||
|
num_episodes=1,
|
||||||
|
episode_time_s=0.1,
|
||||||
|
reset_time_s=0,
|
||||||
|
push_to_hub=False,
|
||||||
|
)
|
||||||
|
cfg = RecordConfig(
|
||||||
|
robot=robot_cfg,
|
||||||
|
dataset=dataset_cfg,
|
||||||
|
teleop=teleop_cfg,
|
||||||
|
play_sounds=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
current_dataset = human_inloop_record(cfg)
|
||||||
|
legacy_dataset = remove_feature(
|
||||||
|
current_dataset,
|
||||||
|
feature_names=[
|
||||||
|
"complementary_info.policy_action",
|
||||||
|
"complementary_info.is_intervention",
|
||||||
|
"complementary_info.state",
|
||||||
|
],
|
||||||
|
output_dir=legacy_root,
|
||||||
|
repo_id="dummy/repo_legacy",
|
||||||
|
)
|
||||||
|
assert "complementary_info.policy_action" not in legacy_dataset.features
|
||||||
|
|
||||||
|
patched_dataset = patch_hil_dataset_schema(
|
||||||
|
PatchHilDatasetSchemaConfig(
|
||||||
|
repo_id="dummy/repo_legacy",
|
||||||
|
root=str(legacy_root),
|
||||||
|
output_repo_id="dummy/repo_patched",
|
||||||
|
output_dir=str(patched_root),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert "complementary_info.policy_action" in patched_dataset.features
|
||||||
|
assert "complementary_info.is_intervention" in patched_dataset.features
|
||||||
|
assert "complementary_info.state" in patched_dataset.features
|
||||||
|
patched_reloaded = LeRobotDataset("dummy/repo_patched", root=patched_root)
|
||||||
|
torch.testing.assert_close(
|
||||||
|
patched_reloaded[0]["complementary_info.policy_action"],
|
||||||
|
torch.zeros_like(patched_reloaded[0]["action"]),
|
||||||
|
)
|
||||||
|
assert float(patched_reloaded[0]["complementary_info.is_intervention"]) == 0.0
|
||||||
|
assert float(patched_reloaded[0]["complementary_info.state"]) == 0.0
|
||||||
|
|
||||||
|
merged_dataset = merge_datasets(
|
||||||
|
datasets=[current_dataset, patched_dataset],
|
||||||
|
output_repo_id="dummy/repo_merged",
|
||||||
|
output_dir=merged_root,
|
||||||
|
)
|
||||||
|
assert merged_dataset.meta.total_episodes == current_dataset.meta.total_episodes + patched_dataset.meta.total_episodes
|
||||||
|
|
||||||
|
|
||||||
def test_record_loop_sets_leader_manual_control_during_reset():
|
def test_record_loop_sets_leader_manual_control_during_reset():
|
||||||
class MockTeleopWithManualControl(MockTeleop):
|
class MockTeleopWithManualControl(MockTeleop):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
|
|||||||
在新工单中引用
屏蔽一个用户