forked from intrinsic-dev/aic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunACT.py
More file actions
320 lines (270 loc) · 11.1 KB
/
Copy pathRunACT.py
File metadata and controls
320 lines (270 loc) · 11.1 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#
# Copyright (C) 2026 Intrinsic Innovation LLC
#
# 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.
#
import os
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
import time
import json
import torch
import numpy as np
import cv2
import draccus
from pathlib import Path
from typing import Callable, Dict, Any, List
from rclpy.node import Node
from geometry_msgs.msg import Twist, Vector3
from aic_model.policy import (
GetObservationCallback,
MoveRobotCallback,
Policy,
SendFeedbackCallback,
)
from aic_model_interfaces.msg import Observation
from aic_task_interfaces.msg import Task
from aic_control_interfaces.msg import (
MotionUpdate,
TrajectoryGenerationMode,
)
from geometry_msgs.msg import Wrench
# LeRobot & Safetensors
from lerobot.policies.act.modeling_act import ACTPolicy
from lerobot.policies.act.configuration_act import ACTConfig
from safetensors.torch import load_file
from huggingface_hub import snapshot_download
class RunACT(Policy):
def __init__(self, parent_node: Node):
super().__init__(parent_node)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# -------------------------------------------------------------------------
# 1. Configuration & Weights Loading
# -------------------------------------------------------------------------
repo_id = "grkw/aic_act_policy"
# Path to your checkpoint folder
policy_path = Path(
snapshot_download(
repo_id=repo_id,
allow_patterns=["config.json", "model.safetensors", "*.safetensors"],
)
)
# Load Config Manually (Fixes 'Draccus' error by removing unknown 'type' field)
with open(policy_path / "config.json", "r") as f:
config_dict = json.load(f)
if "type" in config_dict:
del config_dict["type"]
config = draccus.decode(ACTConfig, config_dict)
# Load Policy Architecture & Weights
self.policy = ACTPolicy(config)
model_weights_path = policy_path / "model.safetensors"
self.policy.load_state_dict(load_file(model_weights_path))
self.policy.eval()
self.policy.to(self.device)
self.get_logger().info(f"ACT Policy loaded on {self.device} from {policy_path}")
# -------------------------------------------------------------------------
# 2. Normalization Stats Loading
# -------------------------------------------------------------------------
stats_path = (
policy_path / "policy_preprocessor_step_3_normalizer_processor.safetensors"
)
stats = load_file(stats_path)
# Helper to extract and shape stats for broadcasting
def get_stat(key, shape):
return stats[key].to(self.device).view(*shape)
# Image Stats (1, 3, 1, 1) for broadcasting against (Batch, Channel, Height, Width)
self.img_stats = {
"left": {
"mean": get_stat("observation.images.left_camera.mean", (1, 3, 1, 1)),
"std": get_stat("observation.images.left_camera.std", (1, 3, 1, 1)),
},
"center": {
"mean": get_stat("observation.images.center_camera.mean", (1, 3, 1, 1)),
"std": get_stat("observation.images.center_camera.std", (1, 3, 1, 1)),
},
"right": {
"mean": get_stat("observation.images.right_camera.mean", (1, 3, 1, 1)),
"std": get_stat("observation.images.right_camera.std", (1, 3, 1, 1)),
},
}
print(f"Image stats: {self.img_stats}")
# Robot State Stats (1, 26)
self.state_mean = get_stat("observation.state.mean", (1, -1))
self.state_std = get_stat("observation.state.std", (1, -1))
print(f"Robot state mean: {self.state_mean}")
print(f"Robot state std: {self.state_std}")
# Action Stats (1, 7) - Used for Un-normalization
self.action_mean = get_stat("action.mean", (1, -1))
self.action_std = get_stat("action.std", (1, -1))
print(f"Action mean: {self.action_mean}")
print(f"Action std: {self.action_std}")
# Config
self.image_scaling = 0.25 # Must match AICRobotAICControllerConfig
self.get_logger().info("Normalization statistics loaded successfully.")
@staticmethod
def _img_to_tensor(
raw_img,
device: torch.device,
scale: float,
mean: torch.Tensor,
std: torch.Tensor,
) -> torch.Tensor:
"""Converts ROS Image -> Resized -> Permuted -> Normalized Tensor."""
# 1. Bytes to Numpy (H, W, C)
img_np = np.frombuffer(raw_img.data, dtype=np.uint8).reshape(
raw_img.height, raw_img.width, 3
)
# 2. Resize
if scale != 1.0:
img_np = cv2.resize(
img_np, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA
)
# 3. To Tensor -> Permute (HWC -> CHW) -> Float -> Div(255) -> Batch Dim
tensor = (
torch.from_numpy(img_np)
.permute(2, 0, 1)
.float()
.div(255.0)
.unsqueeze(0)
.to(device)
)
# 4. Normalize (Apply Mean/Std)
# Formula: (x - mean) / std
return (tensor - mean) / std
def prepare_observations(self, obs_msg: Observation) -> Dict[str, torch.Tensor]:
"""Convert ROS Observation message into dictionary of normalized tensors."""
# --- Process Cameras ---
obs = {
"observation.images.left_camera": self._img_to_tensor(
obs_msg.left_image,
self.device,
self.image_scaling,
self.img_stats["left"]["mean"],
self.img_stats["left"]["std"],
),
"observation.images.center_camera": self._img_to_tensor(
obs_msg.center_image,
self.device,
self.image_scaling,
self.img_stats["center"]["mean"],
self.img_stats["center"]["std"],
),
"observation.images.right_camera": self._img_to_tensor(
obs_msg.right_image,
self.device,
self.image_scaling,
self.img_stats["right"]["mean"],
self.img_stats["right"]["std"],
),
}
# --- Process Robot State ---
# Construct flat state vector (26 dims) matching training order
tcp_pose = obs_msg.controller_state.tcp_pose
tcp_vel = obs_msg.controller_state.tcp_velocity
state_np = np.array(
[
# TCP Position (3)
tcp_pose.position.x,
tcp_pose.position.y,
tcp_pose.position.z,
# TCP Orientation (4)
tcp_pose.orientation.x,
tcp_pose.orientation.y,
tcp_pose.orientation.z,
tcp_pose.orientation.w,
# TCP Linear Vel (3)
tcp_vel.linear.x,
tcp_vel.linear.y,
tcp_vel.linear.z,
# TCP Angular Vel (3)
tcp_vel.angular.x,
tcp_vel.angular.y,
tcp_vel.angular.z,
# TCP Error (6)
*obs_msg.controller_state.tcp_error,
# Joint Positions (7)
*obs_msg.joint_states.position[:7],
],
dtype=np.float32,
)
# Normalize State
raw_state_tensor = (
torch.from_numpy(state_np).float().unsqueeze(0).to(self.device)
)
obs["observation.state"] = (raw_state_tensor - self.state_mean) / self.state_std
return obs
def insert_cable(
self,
task: Task,
get_observation: GetObservationCallback,
move_robot: MoveRobotCallback,
send_feedback: SendFeedbackCallback,
**kwargs,
):
self.policy.reset()
self.get_logger().info(f"RunACT.insert_cable() enter. Task: {task}")
start_time = time.time()
# Run inference for 30 seconds
while time.time() - start_time < 30.0:
loop_start = time.time()
# 1. Get & Process Observation
observation_msg = get_observation()
if observation_msg is None:
self.get_logger().info("No observation received.")
continue
obs_tensors = self.prepare_observations(observation_msg)
# 2. Model Inference
with torch.inference_mode():
# returns shape [1, 7] (first action of chunk)
normalized_action = self.policy.select_action(obs_tensors)
# 3. Un-normalize Action
# Formula: (norm * std) + mean
raw_action_tensor = (normalized_action * self.action_std) + self.action_mean
# 4. Extract and Command
# raw_action_tensor is [1, 7], taking [0] gives vector of 7
action = raw_action_tensor[0].cpu().numpy()
self.get_logger().info(f"Action: {action}")
twist = Twist(
linear=Vector3(
x=float(action[0]), y=float(action[1]), z=float(action[2])
),
angular=Vector3(
x=float(action[3]), y=float(action[4]), z=float(action[5])
),
)
motion_update = self.set_cartesian_twist_target(twist)
move_robot(motion_update=motion_update)
send_feedback("in progress...")
# Maintain control rate (approx 4Hz loop = 0.25s sleep)
elapsed = time.time() - loop_start
time.sleep(max(0, 0.25 - elapsed))
self.get_logger().info("RunACT.insert_cable() exiting...")
return True
def set_cartesian_twist_target(self, twist: Twist, frame_id: str = "base_link"):
motion_update_msg = MotionUpdate()
motion_update_msg.velocity = twist
motion_update_msg.header.frame_id = frame_id
motion_update_msg.header.stamp = self.get_clock().now().to_msg()
motion_update_msg.target_stiffness = np.diag(
[100.0, 100.0, 100.0, 50.0, 50.0, 50.0]
).flatten()
motion_update_msg.target_damping = np.diag(
[40.0, 40.0, 40.0, 15.0, 15.0, 15.0]
).flatten()
motion_update_msg.feedforward_wrench_at_tip = Wrench(
force=Vector3(x=0.0, y=0.0, z=0.0), torque=Vector3(x=0.0, y=0.0, z=0.0)
)
motion_update_msg.wrench_feedback_gains_at_tip = [0.5, 0.5, 0.5, 0.0, 0.0, 0.0]
motion_update_msg.trajectory_generation_mode.mode = (
TrajectoryGenerationMode.MODE_VELOCITY
)
return motion_update_msg