forked from RoboMME/robomme_benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
121 lines (102 loc) · 4.22 KB
/
Copy pathevaluation.py
File metadata and controls
121 lines (102 loc) · 4.22 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
import os
import torch
import numpy
import random
import numpy as np
import cv2
import imageio
from pathlib import Path
from robomme.env_record_wrapper import BenchmarkEnvBuilder
class VideoRecorder:
BORDER_COLOR = (255, 0, 0)
BORDER_THICKNESS = 10
def __init__(self, fps: int = 30):
self.fps = fps
self.frames: list[np.ndarray] = []
@staticmethod
def _to_numpy(t) -> np.ndarray:
return t.cpu().numpy() if isinstance(t, torch.Tensor) else np.asarray(t)
@classmethod
def _make_frame(
cls,
front: np.ndarray | torch.Tensor,
wrist: np.ndarray | torch.Tensor,
is_video_demo: bool = False,
) -> np.ndarray:
frame = np.hstack([cls._to_numpy(front), cls._to_numpy(wrist)]).astype(np.uint8)
if is_video_demo:
h, w = frame.shape[:2]
cv2.rectangle(frame, (0, 0), (w, h), cls.BORDER_COLOR, cls.BORDER_THICKNESS)
return frame
def add_initial_obs(self, obs: dict):
rgb_list = obs["front_rgb_list"]
for i, (front, wrist) in enumerate(zip(rgb_list, obs["wrist_rgb_list"])):
self.frames.append(self._make_frame(front, wrist, is_video_demo=i < len(rgb_list) - 1))
def add_step_obs(self, obs: dict):
self.frames.append(self._make_frame(
obs["front_rgb_list"][-1], obs["wrist_rgb_list"][-1],
))
def save(self, file_path: str):
dir_path = Path(file_path).parent
dir_path.mkdir(parents=True, exist_ok=True)
imageio.mimsave(file_path, self.frames, fps=self.fps)
self.frames = []
class DummyModel:
def __init__(self, seed: int):
self.base_action = np.array(
[0.0, 0.0, 0.0, -np.pi / 2, 0.0, np.pi / 2, np.pi / 4, 1.0],
dtype=np.float32,
)
self.set_model_seed(seed)
def set_model_seed(self, seed: int):
# set model seed will not affect the env seed
# env seed is fixed internally
torch.manual_seed(seed)
numpy.random.seed(seed)
random.seed(seed)
self.seed = seed
def predict(self, *args, **kwargs):
noise = np.random.normal(0, 0.01, self.base_action.shape)
noise[..., -1:] = 0.0 # Preserve gripper action
return self.base_action + noise
TASKS = BenchmarkEnvBuilder.get_task_list()
MODEL_SEED = 7 # 7, 42, 0
dummy_model = DummyModel(seed=MODEL_SEED)
total_success = []
for task in TASKS:
env_builder = BenchmarkEnvBuilder(
env_id=task,
dataset="test",
action_space="joint_angle", # change this to your model's action space
max_steps=1300, # we set 1300 in MME-VLA experiments.
)
episode_count = env_builder.get_episode_num()
for episode in range(episode_count):
env = env_builder.make_env_for_episode(episode)
obs, info = env.reset()
task_goal = info["task_goal"][0] # you can take alternative task goals if you want
print(f"\nTask goal: {task_goal}")
recorder = VideoRecorder()
recorder.add_initial_obs(obs)
current_front_rgb = obs["front_rgb_list"][-1]
current_wrist_rgb = obs["wrist_rgb_list"][-1]
while True:
dummy_action = dummy_model.predict(current_front_rgb, current_wrist_rgb, task_goal)
obs, reward, terminated, truncated, info = env.step(dummy_action)
if info is not None and info.get("status") == "error":
print(f"Error: {info.get('error_message')}") # often IK error when using ee pose
total_success.append(False)
break
if terminated or truncated:
outcome = info.get("status", "unknown")
print(f"Outcome of episode {episode} of task {task}: {outcome}")
total_success.append(outcome == "success")
break
current_front_rgb = obs["front_rgb_list"][-1]
current_wrist_rgb = obs["wrist_rgb_list"][-1]
recorder.add_step_obs(obs)
env.close()
os.makedirs("runs/saved_videos", exist_ok=True)
recorder.save(file_path=f"runs/saved_videos/{task}_ep_{episode}_{outcome}_{task_goal}.mp4")
print(f"Evaluation completed.")
print(f"Success rate: {sum(total_success) / len(total_success)}")