-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcustom_rlbench_env.py
391 lines (336 loc) · 15.7 KB
/
custom_rlbench_env.py
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from typing import Type, List
import numpy as np
from rlbench import ObservationConfig, ActionMode
from rlbench.backend.exceptions import InvalidActionError
from rlbench.backend.observation import Observation
from rlbench.backend.task import Task
from yarr.agents.agent import ActResult, VideoSummary, TextSummary
from yarr.envs.rlbench_env import RLBenchEnv, MultiTaskRLBenchEnv
from yarr.utils.observation_type import ObservationElement
from yarr.utils.transition import Transition
from yarr.utils.process_str import change_case
from pyrep.const import RenderMode
from pyrep.errors import IKError, ConfigurationPathError
from pyrep.objects import VisionSensor, Dummy
class CustomRLBenchEnv(RLBenchEnv):
def __init__(self,
task_class: Type[Task],
observation_config: ObservationConfig,
action_mode: ActionMode,
episode_length: int,
dataset_root: str = '',
channels_last: bool = False,
reward_scale=100.0,
headless: bool = True,
time_in_state: bool = False,
include_lang_goal_in_obs: bool = False,
record_every_n: int = 20):
super(CustomRLBenchEnv, self).__init__(
task_class, observation_config, action_mode, dataset_root,
channels_last, headless=headless,
include_lang_goal_in_obs=include_lang_goal_in_obs)
self._reward_scale = reward_scale
self._episode_index = 0
self._record_current_episode = False
self._record_cam = None
self._previous_obs, self._previous_obs_dict = None, None
self._recorded_images = []
self._episode_length = episode_length
self._time_in_state = time_in_state
self._record_every_n = record_every_n
self._i = 0
self._error_type_counts = {
'IKError': 0,
'ConfigurationPathError': 0,
'InvalidActionError': 0,
}
self._last_exception = None
@property
def observation_elements(self) -> List[ObservationElement]:
obs_elems = super(CustomRLBenchEnv, self).observation_elements
for oe in obs_elems:
if oe.name == 'low_dim_state':
oe.shape = (oe.shape[0] - 7 * 3 + int(self._time_in_state),) # remove pose and joint velocities as they will not be included
self.low_dim_state_len = oe.shape[0]
return obs_elems
def extract_obs(self, obs: Observation, t=None, prev_action=None):
obs.joint_velocities = None
grip_mat = obs.gripper_matrix
grip_pose = obs.gripper_pose
joint_pos = obs.joint_positions
obs.gripper_pose = None
# obs.gripper_pose = None
obs.gripper_matrix = None
obs.wrist_camera_matrix = None
obs.joint_positions = None
if obs.gripper_joint_positions is not None:
obs.gripper_joint_positions = np.clip(
obs.gripper_joint_positions, 0., 0.04)
obs_dict = super(CustomRLBenchEnv, self).extract_obs(obs)
if self._time_in_state:
time = (1. - ((self._i if t is None else t) / float(
self._episode_length - 1))) * 2. - 1.
obs_dict['low_dim_state'] = np.concatenate(
[obs_dict['low_dim_state'], [time]]).astype(np.float32)
obs.gripper_matrix = grip_mat
# obs.gripper_pose = grip_pose
obs.joint_positions = joint_pos
obs.gripper_pose = grip_pose
# obs_dict['gripper_pose'] = grip_pose
return obs_dict
def launch(self):
super(CustomRLBenchEnv, self).launch()
self._task._scene.register_step_callback(self._my_callback)
if self.eval:
cam_placeholder = Dummy('cam_cinematic_placeholder')
cam_base = Dummy('cam_cinematic_base')
cam_base.rotate([0, 0, np.pi * 0.75])
self._record_cam = VisionSensor.create([320, 180])
self._record_cam.set_explicit_handling(True)
self._record_cam.set_pose(cam_placeholder.get_pose())
self._record_cam.set_render_mode(RenderMode.OPENGL)
def reset(self) -> dict:
self._i = 0
self._previous_obs_dict = super(CustomRLBenchEnv, self).reset()
self._record_current_episode = (
self.eval and self._episode_index % self._record_every_n == 0)
self._episode_index += 1
self._recorded_images.clear()
return self._previous_obs_dict
def register_callback(self, func):
self._task._scene.register_step_callback(func)
def _my_callback(self):
if self._record_current_episode:
self._record_cam.handle_explicitly()
cap = (self._record_cam.capture_rgb() * 255).astype(np.uint8)
self._recorded_images.append(cap)
def _append_final_frame(self, success: bool):
self._record_cam.handle_explicitly()
img = (self._record_cam.capture_rgb() * 255).astype(np.uint8)
self._recorded_images.append(img)
final_frames = np.zeros((10, ) + img.shape[:2] + (3,), dtype=np.uint8)
# Green/red for success/failure
final_frames[:, :, :, 1 if success else 0] = 255
self._recorded_images.extend(list(final_frames))
def step(self, act_result: ActResult) -> Transition:
action = act_result.action
success = False
obs = self._previous_obs_dict # in case action fails.
try:
obs, reward, terminal = self._task.step(action)
if reward >= 1:
success = True
reward *= self._reward_scale
else:
reward = 0.0
obs = self.extract_obs(obs)
self._previous_obs_dict = obs
except (IKError, ConfigurationPathError, InvalidActionError) as e:
terminal = True
reward = 0.0
if isinstance(e, IKError):
self._error_type_counts['IKError'] += 1
elif isinstance(e, ConfigurationPathError):
self._error_type_counts['ConfigurationPathError'] += 1
elif isinstance(e, InvalidActionError):
self._error_type_counts['InvalidActionError'] += 1
self._last_exception = e
summaries = []
self._i += 1
if ((terminal or self._i == self._episode_length) and
self._record_current_episode):
self._append_final_frame(success)
vid = np.array(self._recorded_images).transpose((0, 3, 1, 2))
summaries.append(VideoSummary(
'episode_rollout_' + ('success' if success else 'fail'),
vid, fps=30))
# error summary
error_str = f"Errors - IK : {self._error_type_counts['IKError']}, " \
f"ConfigPath : {self._error_type_counts['ConfigurationPathError']}, " \
f"InvalidAction : {self._error_type_counts['InvalidActionError']}"
if not success and self._last_exception is not None:
error_str += f"\n Last Exception: {self._last_exception}"
self._last_exception = None
summaries.append(TextSummary('errors', f"Success: {success} | " + error_str))
return Transition(obs, reward, terminal, summaries=summaries)
def reset_to_demo(self, i):
self._i = 0
# super(CustomRLBenchEnv, self).reset()
self._task.set_variation(-1)
d, = self._task.get_demos(
1, live_demos=False, random_selection=False, from_episode_number=i)
self._task.set_variation(d.variation_number)
_, obs = self._task.reset_to_demo(d)
self._lang_goal = self._task.get_task_descriptions()[0]
self._previous_obs_dict = self.extract_obs(obs)
self._record_current_episode = (
self.eval and self._episode_index % self._record_every_n == 0)
self._episode_index += 1
self._recorded_images.clear()
return self._previous_obs_dict
class CustomMultiTaskRLBenchEnv(MultiTaskRLBenchEnv):
def __init__(self,
task_classes: List[Type[Task]],
observation_config: ObservationConfig,
action_mode: ActionMode,
episode_length: int,
dataset_root: str = '',
channels_last: bool = False,
reward_scale=100.0,
headless: bool = True,
swap_task_every: int = 1,
time_in_state: bool = False,
include_lang_goal_in_obs: bool = False,
record_every_n: int = 20):
super(CustomMultiTaskRLBenchEnv, self).__init__(
task_classes, observation_config, action_mode, dataset_root,
channels_last, headless=headless, swap_task_every=swap_task_every,
include_lang_goal_in_obs=include_lang_goal_in_obs)
self._reward_scale = reward_scale
self._episode_index = 0
self._record_current_episode = False
self._record_cam = None
self._previous_obs, self._previous_obs_dict = None, None
self._recorded_images = []
self._episode_length = episode_length
self._time_in_state = time_in_state
self._record_every_n = record_every_n
self._i = 0
self._error_type_counts = {
'IKError': 0,
'ConfigurationPathError': 0,
'InvalidActionError': 0,
}
self._last_exception = None
@property
def observation_elements(self) -> List[ObservationElement]:
obs_elems = super(CustomMultiTaskRLBenchEnv, self).observation_elements
for oe in obs_elems:
if oe.name == 'low_dim_state':
oe.shape = (oe.shape[0] - 7 * 3 + int(self._time_in_state),) # remove pose and joint velocities as they will not be included
self.low_dim_state_len = oe.shape[0]
return obs_elems
def extract_obs(self, obs: Observation, t=None, prev_action=None):
obs.joint_velocities = None
grip_mat = obs.gripper_matrix
grip_pose = obs.gripper_pose
joint_pos = obs.joint_positions
obs.gripper_pose = None
# obs.gripper_pose = None
obs.gripper_matrix = None
obs.wrist_camera_matrix = None
obs.joint_positions = None
if obs.gripper_joint_positions is not None:
obs.gripper_joint_positions = np.clip(
obs.gripper_joint_positions, 0., 0.04)
obs_dict = super(CustomMultiTaskRLBenchEnv, self).extract_obs(obs)
if self._time_in_state:
time = (1. - ((self._i if t is None else t) / float(
self._episode_length - 1))) * 2. - 1.
obs_dict['low_dim_state'] = np.concatenate(
[obs_dict['low_dim_state'], [time]]).astype(np.float32)
obs.gripper_matrix = grip_mat
# obs.gripper_pose = grip_pose
obs.joint_positions = joint_pos
obs.gripper_pose = grip_pose
# obs_dict['gripper_pose'] = grip_pose
return obs_dict
def launch(self):
super(CustomMultiTaskRLBenchEnv, self).launch()
self._task._scene.register_step_callback(self._my_callback)
if self.eval:
cam_placeholder = Dummy('cam_cinematic_placeholder')
cam_base = Dummy('cam_cinematic_base')
cam_base.rotate([0, 0, np.pi * 0.75])
self._record_cam = VisionSensor.create([320, 180])
self._record_cam.set_explicit_handling(True)
self._record_cam.set_pose(cam_placeholder.get_pose())
self._record_cam.set_render_mode(RenderMode.OPENGL)
def reset(self) -> dict:
self._i = 0
self._previous_obs_dict = super(CustomMultiTaskRLBenchEnv, self).reset()
self._record_current_episode = (
self.eval and self._episode_index % self._record_every_n == 0)
self._episode_index += 1
self._recorded_images.clear()
return self._previous_obs_dict
def register_callback(self, func):
self._task._scene.register_step_callback(func)
def _my_callback(self):
if self._record_current_episode:
self._record_cam.handle_explicitly()
cap = (self._record_cam.capture_rgb() * 255).astype(np.uint8)
self._recorded_images.append(cap)
def _append_final_frame(self, success: bool):
self._record_cam.handle_explicitly()
img = (self._record_cam.capture_rgb() * 255).astype(np.uint8)
self._recorded_images.append(img)
final_frames = np.zeros((10, ) + img.shape[:2] + (3,), dtype=np.uint8)
# Green/red for success/failure
final_frames[:, :, :, 1 if success else 0] = 255
self._recorded_images.extend(list(final_frames))
def step(self, act_result: ActResult) -> Transition:
action = act_result.action
success = False
obs = self._previous_obs_dict # in case action fails.
try:
obs, reward, terminal = self._task.step(action)
if reward >= 1:
success = True
reward *= self._reward_scale
else:
reward = 0.0
obs = self.extract_obs(obs)
self._previous_obs_dict = obs
except (IKError, ConfigurationPathError, InvalidActionError) as e:
terminal = True
reward = 0.0
if isinstance(e, IKError):
self._error_type_counts['IKError'] += 1
elif isinstance(e, ConfigurationPathError):
self._error_type_counts['ConfigurationPathError'] += 1
elif isinstance(e, InvalidActionError):
self._error_type_counts['InvalidActionError'] += 1
self._last_exception = e
summaries = []
self._i += 1
if ((terminal or self._i == self._episode_length) and
self._record_current_episode):
self._append_final_frame(success)
vid = np.array(self._recorded_images).transpose((0, 3, 1, 2))
task_name = change_case(self._task._task.__class__.__name__)
summaries.append(VideoSummary(
'episode_rollout_' + ('success' if success else 'fail') + f'/{task_name}',
vid, fps=30))
# error summary
error_str = f"Errors - IK : {self._error_type_counts['IKError']}, " \
f"ConfigPath : {self._error_type_counts['ConfigurationPathError']}, " \
f"InvalidAction : {self._error_type_counts['InvalidActionError']}"
if not success and self._last_exception is not None:
error_str += f"\n Last Exception: {self._last_exception}"
self._last_exception = None
summaries.append(TextSummary('errors', f"Success: {success} | " + error_str))
return Transition(obs, reward, terminal, summaries=summaries)
def reset_to_demo(self, i, variation_number=-1):
if self._episodes_this_task == self._swap_task_every:
self._set_new_task()
self._episodes_this_task = 0
self._episodes_this_task += 1
self._i = 0
# super(CustomMultiTaskRLBenchEnv, self).reset()
# if variation_number == -1:
# self._task.sample_variation()
# else:
# self._task.set_variation(variation_number)
self._task.set_variation(-1)
d = self._task.get_demos(
1, live_demos=False, random_selection=False, from_episode_number=i)[0]
self._task.set_variation(d.variation_number)
_, obs = self._task.reset_to_demo(d)
self._lang_goal = self._task.get_task_descriptions()[0]
self._previous_obs_dict = self.extract_obs(obs)
self._record_current_episode = (
self.eval and self._episode_index % self._record_every_n == 0)
self._episode_index += 1
self._recorded_images.clear()
return self._previous_obs_dict