fix: train ACT on causal RM65 actions
This commit is contained in:
parent
67e9dc0ec8
commit
a916ec3517
@ -11,7 +11,7 @@ from typing import Any
|
||||
|
||||
import numpy as np
|
||||
|
||||
from .rm65_controller import RM65PickPlaceRuntime, _write_preview_video
|
||||
from .rm65_controller import ACTION_ALIGNMENT, CONTROL_HOLD_STEPS, RM65PickPlaceRuntime, _write_preview_video
|
||||
|
||||
|
||||
def load_policy(model_dir: Path, act_root: Path, max_timesteps: int):
|
||||
@ -90,7 +90,7 @@ def infer(
|
||||
for index in range(7):
|
||||
action[index] = np.clip(action[index], runtime.ctrl_ranges[index, 0], runtime.ctrl_ranges[index, 1])
|
||||
runtime.data.ctrl[:7] = action
|
||||
for _ in range(10):
|
||||
for _ in range(CONTROL_HOLD_STEPS):
|
||||
runtime.mujoco.mj_step(runtime.model, runtime.data)
|
||||
touching = runtime.finger_contacts()
|
||||
bilateral_steps += int(len(touching) == 2)
|
||||
@ -138,6 +138,8 @@ def infer(
|
||||
"episodes": results,
|
||||
"success_rate": success_rate,
|
||||
"minimum_success_rate": minimum_success_rate,
|
||||
"action_alignment": ACTION_ALIGNMENT,
|
||||
"control_hold_steps": CONTROL_HOLD_STEPS,
|
||||
"action_std": action_std,
|
||||
"actions_nonconstant": nonconstant,
|
||||
"pass": success_rate >= minimum_success_rate and nonconstant,
|
||||
|
||||
@ -1069,6 +1069,8 @@ def _run_discoverse_act_training(config: PlatformConfig, store: PlatformStore, t
|
||||
"state_dim": dataset_info["state_dim"],
|
||||
"action_dim": dataset_info["action_dim"],
|
||||
"image_color_order": "RGB (OpenCV-decoded BGR is converted by the generated ACT augmentor)",
|
||||
"action_alignment": dataset_info["action_alignment"],
|
||||
"control_hold_steps": dataset_info["control_hold_steps"],
|
||||
"config_path": str(config_path),
|
||||
"runtime_compatibility": "HDF5 ACT training; AIRBOT MCAP/hardware codecs remain optional and fail explicitly when requested",
|
||||
},
|
||||
@ -2032,6 +2034,8 @@ def _inspect_act_dataset(dataset_path: Path) -> dict[str, Any]:
|
||||
cameras: list[str] = []
|
||||
state_dim = 0
|
||||
action_dim = 0
|
||||
action_alignment = ""
|
||||
control_hold_steps = 0
|
||||
for path in episodes:
|
||||
with h5py.File(path, "r") as root:
|
||||
if "/observations/qpos" not in root or "/action" not in root or "/observations/images" not in root:
|
||||
@ -2039,12 +2043,24 @@ def _inspect_act_dataset(dataset_path: Path) -> dict[str, Any]:
|
||||
current_cameras = sorted(str(name) for name in root["/observations/images"].keys())
|
||||
current_state_dim = int(root["/observations/qpos"].shape[1])
|
||||
current_action_dim = int(root["/action"].shape[1])
|
||||
current_alignment = str(root.attrs.get("action_alignment", ""))
|
||||
current_hold_steps = int(root.attrs.get("control_hold_steps", 0))
|
||||
if current_alignment != "observation_t_to_action_t" or current_hold_steps <= 0:
|
||||
raise ValueError(f"ACT HDF5 不是因果 observation_t -> action_t 数据: {path.name}")
|
||||
if not cameras:
|
||||
cameras = current_cameras
|
||||
state_dim = current_state_dim
|
||||
action_dim = current_action_dim
|
||||
if current_cameras != cameras or current_state_dim != state_dim or current_action_dim != action_dim:
|
||||
raise ValueError("ACT 数据集的相机、状态维度或动作维度不一致")
|
||||
action_alignment = current_alignment
|
||||
control_hold_steps = current_hold_steps
|
||||
if (
|
||||
current_cameras != cameras
|
||||
or current_state_dim != state_dim
|
||||
or current_action_dim != action_dim
|
||||
or current_alignment != action_alignment
|
||||
or current_hold_steps != control_hold_steps
|
||||
):
|
||||
raise ValueError("ACT 数据集的相机、状态维度、动作维度或控制周期不一致")
|
||||
length = int(root["/action"].shape[0])
|
||||
if length < 2:
|
||||
raise ValueError(f"ACT episode 过短: {path.name}")
|
||||
@ -2055,6 +2071,8 @@ def _inspect_act_dataset(dataset_path: Path) -> dict[str, Any]:
|
||||
"camera_names": cameras,
|
||||
"state_dim": state_dim,
|
||||
"action_dim": action_dim,
|
||||
"action_alignment": action_alignment,
|
||||
"control_hold_steps": control_hold_steps,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -14,6 +14,10 @@ from typing import Any
|
||||
import numpy as np
|
||||
|
||||
|
||||
CONTROL_HOLD_STEPS = 35
|
||||
ACTION_ALIGNMENT = "observation_t_to_action_t"
|
||||
|
||||
|
||||
@dataclass
|
||||
class EpisodeResult:
|
||||
success: bool
|
||||
@ -117,17 +121,18 @@ class RM65PickPlaceRuntime:
|
||||
action = np.empty(7, dtype=np.float32)
|
||||
action[:6] = start_ctrl * (1 - alpha) + target_q * alpha
|
||||
action[6] = gripper
|
||||
self.renderer.update_scene(self.data, camera="overview")
|
||||
frame = self.renderer.render().copy()
|
||||
observation = np.concatenate([self.data.qpos[self.qpos_adrs], [self.data.qpos[self.gripper_qadr]]]).astype(np.float32)
|
||||
cube_before = self.data.qpos[self.cube_qadr : self.cube_qadr + 3].astype(float).copy()
|
||||
self.data.ctrl[:7] = action
|
||||
# The RM65 model has conservative actuator force limits; each policy
|
||||
# command is held long enough for the physical joints to settle.
|
||||
for _ in range(35):
|
||||
for _ in range(CONTROL_HOLD_STEPS):
|
||||
mujoco.mj_step(self.model, self.data)
|
||||
touching = self.finger_contacts()
|
||||
bilateral_contact_steps += int(len(touching) == 2)
|
||||
max_cube_z = max(max_cube_z, float(self.data.qpos[self.cube_qadr + 2]))
|
||||
self.renderer.update_scene(self.data, camera="overview")
|
||||
frame = self.renderer.render().copy()
|
||||
observation = np.concatenate([self.data.qpos[self.qpos_adrs], [self.data.qpos[self.gripper_qadr]]]).astype(np.float32)
|
||||
observations.append(observation)
|
||||
actions.append(action.copy())
|
||||
contacts.append(np.array([int(geom in touching) for geom in sorted(self.finger_geoms)], dtype=np.uint8))
|
||||
@ -139,6 +144,8 @@ class RM65PickPlaceRuntime:
|
||||
"phase": phase,
|
||||
"qpos": observation.tolist(),
|
||||
"action": action.tolist(),
|
||||
"action_alignment": ACTION_ALIGNMENT,
|
||||
"cube_pos_before": cube_before.tolist(),
|
||||
"cube_pos": self.data.qpos[self.cube_qadr : self.cube_qadr + 3].astype(float).tolist(),
|
||||
"gripper_pos": self.data.site_xpos[self.site_id].astype(float).tolist(),
|
||||
"bilateral_contact": len(touching) == 2,
|
||||
@ -256,6 +263,8 @@ def collect(scene_path: Path, out_dir: Path, *, required_successes: int, max_att
|
||||
"scene": str(scene_path),
|
||||
"camera_names": ["overview"],
|
||||
"image_encoding": {"codec": "jpeg", "semantic_color_order": "RGB", "opencv_decode_color_order": "BGR"},
|
||||
"action_alignment": ACTION_ALIGNMENT,
|
||||
"control_hold_steps": CONTROL_HOLD_STEPS,
|
||||
"state_dim": 7,
|
||||
"action_dim": 7,
|
||||
"required_successes": required_successes,
|
||||
@ -289,6 +298,8 @@ def _write_hdf5_episode(path: Path, result: EpisodeResult) -> None:
|
||||
root.attrs["success"] = bool(result.success)
|
||||
root.attrs["image_color_order"] = "RGB"
|
||||
root.attrs["opencv_decode_color_order"] = "BGR"
|
||||
root.attrs["action_alignment"] = ACTION_ALIGNMENT
|
||||
root.attrs["control_hold_steps"] = CONTROL_HOLD_STEPS
|
||||
root.create_dataset("/observations/qpos", data=observations, dtype="f4")
|
||||
root.create_dataset("/action", data=actions, dtype="f4")
|
||||
root.create_dataset("/observations/contact", data=contacts, dtype="u1")
|
||||
|
||||
@ -12,8 +12,8 @@ import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from real2sim_platform.config import PlatformConfig
|
||||
from real2sim_platform.adapters import _act_task_config_source, index_artifacts_for_task
|
||||
from real2sim_platform.rm65_controller import _write_preview_video, collect
|
||||
from real2sim_platform.adapters import _act_task_config_source, _inspect_act_dataset, index_artifacts_for_task
|
||||
from real2sim_platform.rm65_controller import ACTION_ALIGNMENT, CONTROL_HOLD_STEPS, _write_preview_video, collect
|
||||
from real2sim_platform.runtime_gate import _motrixsim_assets_probe, runtime_key
|
||||
from real2sim_platform.server import create_app
|
||||
from real2sim_platform.source_revision import read_source_revision
|
||||
@ -328,6 +328,22 @@ def test_generated_act_config_converts_opencv_bgr_to_rgb(tmp_path: Path) -> None
|
||||
assert "return image[:, [2, 1, 0], ...]" in source
|
||||
|
||||
|
||||
def test_act_training_rejects_noncausal_hdf5_and_records_control_cycle(tmp_path: Path) -> None:
|
||||
episode = tmp_path / "episode_0.hdf5"
|
||||
with h5py.File(episode, "w") as root:
|
||||
root.create_dataset("/observations/qpos", data=np.zeros((2, 7), dtype=np.float32))
|
||||
root.create_dataset("/action", data=np.zeros((2, 7), dtype=np.float32))
|
||||
root.create_dataset("/observations/images/overview", data=np.zeros((2, 4), dtype=np.uint8))
|
||||
with pytest.raises(ValueError, match="不是因果"):
|
||||
_inspect_act_dataset(tmp_path)
|
||||
with h5py.File(episode, "r+") as root:
|
||||
root.attrs["action_alignment"] = ACTION_ALIGNMENT
|
||||
root.attrs["control_hold_steps"] = CONTROL_HOLD_STEPS
|
||||
inspected = _inspect_act_dataset(tmp_path)
|
||||
assert inspected["action_alignment"] == ACTION_ALIGNMENT
|
||||
assert inspected["control_hold_steps"] == CONTROL_HOLD_STEPS
|
||||
|
||||
|
||||
def test_runtime_gate_rejects_unresolved_motrixsim_lfs_pointer(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
root = tmp_path / "motrixsim-docs"
|
||||
required = [
|
||||
@ -483,4 +499,6 @@ def test_real_rm65_physical_collection_writes_act_hdf5(tmp_path: Path) -> None:
|
||||
assert bool(root.attrs["success"]) is True
|
||||
assert root.attrs["image_color_order"] == "RGB"
|
||||
assert root.attrs["opencv_decode_color_order"] == "BGR"
|
||||
assert root.attrs["action_alignment"] == ACTION_ALIGNMENT
|
||||
assert root.attrs["control_hold_steps"] == CONTROL_HOLD_STEPS
|
||||
assert (tmp_path / "collection/episode_0.mp4").stat().st_size > 0
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user