-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
946 lines (769 loc) · 30.6 KB
/
utils.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
from abc import abstractmethod, ABC
import random
import itertools
import pickle
from typing import List, Dict, Optional, Tuple, Literal, TypedDict, Union, Any, Sequence
from pathlib import Path
import math
import json
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt
import torch
from torch import nn
import torch.nn.functional as F
from PIL import Image
from scipy.spatial.transform import Rotation as R
import einops
from rlbench.environment import Environment
from rlbench import ObservationConfig, CameraConfig
from rlbench.backend.observation import Observation
from rlbench.task_environment import TaskEnvironment
from rlbench.action_modes.action_mode import MoveArmThenGripper
from rlbench.action_modes.gripper_action_modes import Discrete
from rlbench.action_modes.arm_action_modes import EndEffectorPoseViaPlanning, JointVelocity, JointTorque, EndEffectorPoseViaIK
from rlbench.backend.exceptions import InvalidActionError
from rlbench.demo import Demo
from pyrep.errors import IKError, ConfigurationPathError
from pyrep.const import RenderMode
Camera = Literal["wrist", "left_shoulder", "right_shoulder", "overhead", "front"]
Instructions = Dict[str, Dict[int, torch.Tensor]]
class Sample(TypedDict):
frame_id: torch.Tensor
task: Union[List[str], str]
variation: Union[List[int], int]
rgbs: torch.Tensor
pcds: torch.Tensor
action: torch.Tensor
padding_mask: torch.Tensor
instr: torch.Tensor
gripper: torch.Tensor
def load_episodes() -> Dict[str, Any]:
with open(Path(__file__).parent / "episodes.json") as fid:
return json.load(fid)
def get_max_episode_length(tasks: Tuple[str, ...], variations: Tuple[int, ...]) -> int:
max_episode_length = 0
max_eps_dict = load_episodes()["max_episode_length"]
for task, var in itertools.product(tasks, variations):
if max_eps_dict[task] > max_episode_length:
max_episode_length = max_eps_dict[task]
return max_episode_length
def task_file_to_task_class(task_file):
import importlib
name = task_file.replace(".py", "")
class_name = "".join([w[0].upper() + w[1:] for w in name.split("_")])
mod = importlib.import_module("rlbench.tasks.%s" % name)
mod = importlib.reload(mod)
task_class = getattr(mod, class_name)
return task_class
class Output(TypedDict):
position: torch.Tensor
rotation: torch.Tensor
gripper: torch.Tensor
attention: torch.Tensor
task: Optional[torch.Tensor]
class MotionPlannerError(Exception):
"""When the motion planner is not able to execute an action"""
class Mover:
def __init__(self, task: TaskEnvironment, disabled: bool = False, max_tries: int = 1):
self._task = task
self._last_action: Optional[np.ndarray] = None
self._step_id = 0
self._max_tries = max_tries
self._disabled = disabled
def __call__(self, action: np.ndarray):
if self._disabled:
return self._task.step(action)
target = action.copy()
if self._last_action is not None:
action[7] = self._last_action[7].copy()
images = []
try_id = 0
obs = None
terminate = None
reward = 0
for try_id in range(self._max_tries):
obs, reward, terminate, other_obs = self._task.step(action)
if other_obs == []:
other_obs = [obs]
for o in other_obs:
images.append(
{
k.split("_")[0]: getattr(o, k)
for k in o.__dict__.keys()
if "_rgb" in k and getattr(o, k) is not None
}
)
pos = obs.gripper_pose[:3]
rot = obs.gripper_pose[3:7]
gripper = obs.gripper_open
dist_pos = np.sqrt(np.square(target[:3] - pos).sum())
dist_rot = np.sqrt(np.square(target[3:7] - rot).sum())
criteria = (dist_pos < 5e-2,)
if all(criteria) or reward == 1:
break
print(
f"Too far away (pos: {dist_pos:.3f}, rot: {dist_rot:.3f}, step: {self._step_id})... Retrying..."
)
# we execute the gripper action after re-tries
action = target
if (
not reward
and self._last_action is not None
and action[7] != self._last_action[7]
):
obs, reward, terminate, other_obs = self._task.step(action)
if other_obs == []:
other_obs = [obs]
for o in other_obs:
images.append(
{
k.split("_")[0]: getattr(o, k)
for k in o.__dict__.keys()
if "_rgb" in k and getattr(o, k) is not None
}
)
if try_id == self._max_tries:
print(f"Failure after {self._max_tries} tries")
self._step_id += 1
self._last_action = action.copy()
return obs, reward, terminate, images
class Actioner:
def __init__(
self,
model: nn.Module,
instructions: Dict,
apply_cameras=("left_shoulder", "right_shoulder", "wrist"),
):
self._model = model
self._apply_cameras = apply_cameras
self._instructions = instructions
self._actions: Dict = {}
self._instr: Optional[torch.Tensor] = None
self._task: Optional[str] = None
self._model.eval()
def load_episode(
self, task_str: str, variation: int, demo_id: int, demo: Union[Demo, int]
):
self._task = task_str
instructions = list(self._instructions[task_str][variation])
self._instr = random.choice(instructions).unsqueeze(0)
self._actions = {}
def get_action_from_demo(self, demo: Demo):
"""
Fetch the desired state and action based on the provided demo.
:param demo: fetch each demo and save key-point observations
:param normalise_rgb: normalise rgb to (-1, 1)
:return: a list of obs and action
"""
key_frame = keypoint_discovery(demo)
action_ls = []
for f in key_frame:
obs = demo[f]
action_np = np.concatenate([obs.gripper_pose, [obs.gripper_open]])
action = torch.from_numpy(action_np)
action_ls.append(action.unsqueeze(0))
return action_ls
def predict(
self, step_id: int, rgbs: torch.Tensor, pcds: torch.Tensor, gripper: torch.Tensor
) -> Dict[str, Any]:
padding_mask = torch.ones_like(rgbs[:, :, 0, 0, 0, 0]).bool()
output: Dict[str, Any] = {"action": None, "attention": {}}
if self._instr is None:
raise ValueError()
self._instr = self._instr.to(rgbs.device)
pred = self._model(
rgbs,
pcds,
padding_mask,
self._instr,
gripper,
)
output["action"] = self._model.compute_action(pred) # type: ignore
output["attention"] = pred["attention"]
return output
@property
def device(self):
return next(self._model.parameters()).device
class DT_Actioner:
def __init__(
self,
model: nn.Module,
instructions: Dict,
apply_cameras=("left_shoulder", "right_shoulder", "wrist"),
):
self._model = model
self._apply_cameras = apply_cameras
self._instructions = instructions
self._actions: Dict = {}
self._instr: Optional[torch.Tensor] = None
self._task: Optional[str] = None
self._model.eval()
def load_episode(
self, task_str: str, variation: int, demo_id: int, demo: Union[Demo, int]
):
self._task = task_str
instructions = list(self._instructions[task_str][variation])
self._instr = random.choice(instructions).unsqueeze(0)
self._actions = {}
def get_action_from_demo(self, demo: Demo):
"""
Fetch the desired state and action based on the provided demo.
:param demo: fetch each demo and save key-point observations
:param normalise_rgb: normalise rgb to (-1, 1)
:return: a list of obs and action
"""
key_frame = keypoint_discovery(demo)
action_ls = []
for f in key_frame:
obs = demo[f]
action_np = np.concatenate([obs.gripper_pose, [obs.gripper_open]])
action = torch.from_numpy(action_np)
action_ls.append(action.unsqueeze(0))
return action_ls
def predict(
self, step_id: int, rgbs: torch.Tensor, pcds: torch.Tensor, gripper: torch.Tensor
) -> Dict[str, Any]:
padding_mask = torch.ones_like(rgbs[:, :, 0, 0, 0, 0]).bool()
output: Dict[str, Any] = {"action": None, "attention": {}}
if self._instr is None:
raise ValueError()
self._instr = self._instr.to(rgbs.device)
pred = self._model(
rgbs,
pcds,
padding_mask,
self._instr,
gripper,
)
output["action"] = self._model.compute_action(pred) # type: ignore
output["attention"] = pred["attention"]
return output
@property
def device(self):
return next(self._model.parameters()).device
def obs_to_attn(obs, camera: str) -> Tuple[int, int]:
extrinsics_44 = torch.from_numpy(obs.misc[f"{camera}_camera_extrinsics"]).float()
extrinsics_44 = torch.linalg.inv(extrinsics_44)
intrinsics_33 = torch.from_numpy(obs.misc[f"{camera}_camera_intrinsics"]).float()
intrinsics_34 = F.pad(intrinsics_33, (0, 1, 0, 0))
gripper_pos_3 = torch.from_numpy(obs.gripper_pose[:3]).float()
gripper_pos_41 = F.pad(gripper_pos_3, (0, 1), value=1).unsqueeze(1)
points_cam_41 = extrinsics_44 @ gripper_pos_41
proj_31 = intrinsics_34 @ points_cam_41
proj_3 = proj_31.float().squeeze(1)
u = int((proj_3[0] / proj_3[2]).round())
v = int((proj_3[1] / proj_3[2]).round())
return u, v
# --------------------------------------------------------------------------------
# RLBench Environment & Related Functions
# --------------------------------------------------------------------------------
class RLBenchEnv:
def __init__(
self,
data_path,
apply_rgb=False,
apply_depth=False,
apply_pc=False,
headless=False,
static_positions=False,
apply_cameras=("left_shoulder", "right_shoulder", "wrist", "front"),
act='IK',
):
# setup required inputs
self.data_path = data_path
self.apply_rgb = apply_rgb
self.apply_depth = apply_depth
self.apply_pc = apply_pc
self.apply_cameras = apply_cameras
# setup RLBench environments
self.obs_config = self.create_obs_config(
apply_rgb, apply_depth, apply_pc, apply_cameras
)
if act == 'IK':
action_mode=EndEffectorPoseViaPlanning()
elif act == 'joint_v':
action_mode = JointVelocity()
self.action_mode = MoveArmThenGripper(
arm_action_mode=action_mode,
gripper_action_mode=Discrete(),
)
self.env = Environment(
self.action_mode, str(data_path), self.obs_config, headless=headless, static_positions=static_positions
)
def get_obs_action(self, obs):
"""
Fetch the desired state and action based on the provided demo.
:param obs: incoming obs
:return: required observation and action list
"""
# fetch state
state_dict = {"rgb": [], "depth": [], "pc": []}
for cam in self.apply_cameras:
if self.apply_rgb:
rgb = getattr(obs, "{}_rgb".format(cam))
state_dict["rgb"] += [rgb]
if self.apply_depth:
depth = getattr(obs, "{}_depth".format(cam))
state_dict["depth"] += [depth]
if self.apply_pc:
pc = getattr(obs, "{}_point_cloud".format(cam))
state_dict["pc"] += [pc]
# fetch action
action = np.concatenate([obs.gripper_pose, [obs.gripper_open]])
return state_dict, torch.from_numpy(action).float()
def get_rgb_pos_action(self, obs):
# fetch pose
state_dict = {"rgb": [], "pose": [], "pc": []}
# state_dict['pose'] = torch.from_numpy(obs.gripper_pose).float()
for cam in self.apply_cameras:
if self.apply_rgb:
rgb = getattr(obs, "{}_rgb".format(cam))
state_dict["rgb"] += [rgb]
# fetch action
#import pdb
#pdb.set_trace()
action = np.concatenate([obs.gripper_pose, [obs.gripper_open]])
state_dict['pose'] = torch.from_numpy(action).float()
return state_dict, torch.from_numpy(action).float()
def get_rgb_pcd_gripper_from_obs(
self, obs: Observation
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""
Return rgb, pcd, and gripper from a given observation
:param obs: an Observation from the env
:return: rgb, pcd, gripper
"""
state_dict, gripper = self.get_obs_action(obs)
state = transform(state_dict, augmentation=False)
state = einops.rearrange(
state,
"(m n ch) h w -> n m ch h w",
ch=3,
n=len(self.apply_cameras),
m=2,
)
rgb = state[:, 0].unsqueeze(0) # 1, N, C, H, W
pcd = state[:, 1].unsqueeze(0) # 1, N, C, H, W
gripper = gripper.unsqueeze(0) # 1, D
attns = torch.Tensor([])
for cam in self.apply_cameras:
u, v = obs_to_attn(obs, cam)
attn = torch.zeros((1, 1, 1, 128, 128))
if not (u < 0 or u > 127 or v < 0 or v > 127):
attn[0, 0, 0, v, u] = 1
attns = torch.cat([attns, attn], 1)
rgb = torch.cat([rgb, attns], 2)
return rgb, pcd, gripper
def get_obs_action_from_demo(self, demo: Demo):
"""
Fetch the desired state and action based on the provided demo.
:param demo: fetch each demo and save key-point observations
:param normalise_rgb: normalise rgb to (-1, 1)
:return: a list of obs and action
"""
key_frame = keypoint_discovery(demo)
key_frame.insert(0, 0)
state_ls = []
action_ls = []
for f in key_frame:
state, action = self.get_obs_action(demo._observations[f])
state = transform(state, augmentation=False)
state_ls.append(state.unsqueeze(0))
action_ls.append(action.unsqueeze(0))
return state_ls, action_ls
def get_demo(self, task_name, variation, episode_index):
"""
Fetch a demo from the saved environment.
:param task_name: fetch task name
:param variation: fetch variation id
:param episode_index: fetch episode index: 0 ~ 99
:return: desired demo
"""
demos = self.env.get_demos(
task_name=task_name,
variation_number=variation,
amount=1,
from_episode_number=episode_index,
random_selection=False,
)
return demos
def evaluate(
self,
task_str: str,
max_episodes: int,
variation: int,
num_demos: int,
log_dir: Optional[Path],
actioner: Actioner,
offset: int = 0,
max_tries: int = 1,
demos: Optional[List[Demo]] = None,
save_attn: bool = False,
):
"""
Evaluate the policy network on the desired demo or test environments
:param task_type: type of task to evaluate
:param max_episodes: maximum episodes to finish a task
:param num_demos: number of test demos for evaluation
:param model: the policy network
:param demos: whether to use the saved demos
:return: success rate
"""
self.env.launch()
task_type = task_file_to_task_class(task_str)
task = self.env.get_task(task_type)
task.set_variation(variation) # type: ignore
device = actioner.device
success_rate = 0.0
if demos is None:
fetch_list = [i for i in range(num_demos)]
else:
fetch_list = demos
fetch_list = fetch_list[offset:]
with torch.no_grad():
for demo_id, demo in enumerate(tqdm(fetch_list)):
images = []
rgbs = torch.Tensor([]).to(device)
pcds = torch.Tensor([]).to(device)
grippers = torch.Tensor([]).to(device)
# reset a new demo or a defined demo in the demo list
if demos is None:
_, obs = task.reset()
else:
print("Resetting to demo")
print(demo)
_, obs = task.reset_to_demo(demo)
actioner.load_episode(task_str, variation, demo_id, demo)
images.append(
{cam: getattr(obs, f"{cam}_rgb") for cam in self.apply_cameras}
)
move = Mover(task, max_tries=max_tries)
reward = None
for step_id in range(max_episodes):
# fetch the current observation, and predict one action
rgb, pcd, gripper = self.get_rgb_pcd_gripper_from_obs(obs)
rgb = rgb.to(device)
pcd = pcd.to(device)
gripper = gripper.to(device)
rgbs = torch.cat([rgbs, rgb.unsqueeze(1)], dim=1)
pcds = torch.cat([pcds, pcd.unsqueeze(1)], dim=1)
grippers = torch.cat([grippers, gripper.unsqueeze(1)], dim=1)
output = actioner.predict(step_id, rgbs, pcds, grippers)
action = output["action"]
if action is None:
break
# update the observation based on the predicted action
try:
action_np = action[-1].detach().cpu().numpy()
obs, reward, terminate, step_images = move(action_np)
images += step_images
if reward == 1:
success_rate += 1 / num_demos
break
if terminate:
print("The episode has terminated!")
except (IKError, ConfigurationPathError, InvalidActionError) as e:
print(task_type, demo, step_id, success_rate, e)
reward = 0
break
print(
task_str,
"Reward",
reward,
"Variation",
variation,
"Step",
demo_id,
"SR: %.2f" % (success_rate * 100),
)
self.env.shutdown()
return success_rate
def dt_evaluate(
self,
task_str: str,
max_episodes: int,
variation: int,
num_demos: int,
log_dir: Optional[Path],
actioner: Actioner,
offset: int = 0,
max_tries: int = 1,
demos: Optional[List[Demo]] = None,
save_attn: bool = False,
):
"""
Evaluate the policy network on the desired demo or test environments
:param task_type: type of task to evaluate
:param max_episodes: maximum episodes to finish a task
:param num_demos: number of test demos for evaluation
:param model: the policy network
:param demos: whether to use the saved demos
:return: success rate
"""
self.env.launch()
task_type = task_file_to_task_class(task_str)
task = self.env.get_task(task_type)
task.set_variation(variation) # type: ignore
device = actioner.device
success_rate = 0.0
if demos is None:
fetch_list = [i for i in range(num_demos)]
else:
fetch_list = demos
fetch_list = fetch_list[offset:]
with torch.no_grad():
for demo_id, demo in enumerate(tqdm(fetch_list)):
images = []
rgbs = torch.Tensor([]).to(device)
pcds = torch.Tensor([]).to(device)
grippers = torch.Tensor([]).to(device)
# reset a new demo or a defined demo in the demo list
if demos is None:
_, obs = task.reset()
else:
print("Resetting to demo")
print(demo)
_, obs = task.reset_to_demo(demo)
actioner.load_episode(task_str, variation, demo_id, demo)
images.append(
{cam: getattr(obs, f"{cam}_rgb") for cam in self.apply_cameras}
)
move = Mover(task, max_tries=max_tries)
reward = None
for step_id in range(max_episodes):
# fetch the current observation, and predict one action
rgb, pcd, gripper = self.get_rgb_pcd_gripper_from_obs(obs)
rgb = rgb.to(device)
pcd = pcd.to(device)
gripper = gripper.to(device)
rgbs = torch.cat([rgbs, rgb.unsqueeze(1)], dim=1)
pcds = torch.cat([pcds, pcd.unsqueeze(1)], dim=1)
grippers = torch.cat([grippers, gripper.unsqueeze(1)], dim=1)
output = actioner.predict(step_id, rgbs, pcds, grippers)
action = output["action"]
if action is None:
break
# update the observation based on the predicted action
try:
action_np = action[-1].detach().cpu().numpy()
obs, reward, terminate, step_images = move(action_np)
images += step_images
if reward == 1:
success_rate += 1 / num_demos
break
if terminate:
print("The episode has terminated!")
except (IKError, ConfigurationPathError, InvalidActionError) as e:
print(task_type, demo, step_id, success_rate, e)
reward = 0
break
print(
task_str,
"Reward",
reward,
"Variation",
variation,
"Step",
demo_id,
"SR: %.2f" % (success_rate * 100),
)
self.env.shutdown()
return success_rate
def create_obs_config(
self, apply_rgb, apply_depth, apply_pc, apply_cameras, **kwargs
):
"""
Set up observation config for RLBench environment.
:param apply_rgb: Applying RGB as inputs.
:param apply_depth: Applying Depth as inputs.
:param apply_pc: Applying Point Cloud as inputs.
:param apply_cameras: Desired cameras.
:return: observation config
"""
unused_cams = CameraConfig()
unused_cams.set_all(False)
used_cams = CameraConfig(
rgb=apply_rgb,
point_cloud=apply_pc,
depth=apply_depth,
mask=False,
render_mode=RenderMode.OPENGL,
**kwargs,
)
camera_names = apply_cameras
kwargs = {}
for n in camera_names:
kwargs[n] = used_cams
obs_config = ObservationConfig(
front_camera=kwargs.get("front", unused_cams),
left_shoulder_camera=kwargs.get("left_shoulder", unused_cams),
right_shoulder_camera=kwargs.get("right_shoulder", unused_cams),
wrist_camera=kwargs.get("wrist", unused_cams),
overhead_camera=kwargs.get("overhead", unused_cams),
joint_forces=False,
joint_positions=False,
joint_velocities=True,
task_low_dim_state=False,
gripper_touch_forces=False,
gripper_pose=True,
gripper_open=True,
gripper_matrix=True,
gripper_joint_positions=True,
)
return obs_config
# Identify way-point in each RLBench Demo
def _is_stopped(demo, i, obs, stopped_buffer):
next_is_not_final = i == (len(demo) - 2)
gripper_state_no_change = i < (len(demo) - 2) and (
obs.gripper_open == demo[i + 1].gripper_open
and obs.gripper_open == demo[i - 1].gripper_open
and demo[i - 2].gripper_open == demo[i - 1].gripper_open
)
small_delta = np.allclose(obs.joint_velocities, 0, atol=0.1)
stopped = (
stopped_buffer <= 0
and small_delta
and (not next_is_not_final)
and gripper_state_no_change
)
return stopped
def keypoint_discovery(demo: Demo) -> List[int]:
episode_keypoints = []
prev_gripper_open = demo[0].gripper_open
stopped_buffer = 0
for i, obs in enumerate(demo):
stopped = _is_stopped(demo, i, obs, stopped_buffer)
stopped_buffer = 4 if stopped else stopped_buffer - 1
# If change in gripper, or end of episode.
last = i == (len(demo) - 1)
if i != 0 and (obs.gripper_open != prev_gripper_open or last or stopped):
episode_keypoints.append(i)
prev_gripper_open = obs.gripper_open
if (
len(episode_keypoints) > 1
and (episode_keypoints[-1] - 1) == episode_keypoints[-2]
):
episode_keypoints.pop(-2)
# HACK for tower3 task
return episode_keypoints
# --------------------------------------------------------------------------------
# General Functions
# --------------------------------------------------------------------------------
def transform(obs_dict, scale_size=(0.75, 1.25), augmentation=False):
apply_depth = len(obs_dict.get("depth", [])) > 0
apply_pc = len(obs_dict["pc"]) > 0
num_cams = len(obs_dict["rgb"])
obs_rgb = []
obs_depth = []
obs_pc = []
for i in range(num_cams):
rgb = torch.tensor(obs_dict["rgb"][i]).float().permute(2, 0, 1)
depth = (
torch.tensor(obs_dict["depth"][i]).float().permute(2, 0, 1)
if apply_depth
else None
)
pc = (
torch.tensor(obs_dict["pc"][i]).float().permute(2, 0, 1) if apply_pc else None
)
if augmentation:
raise NotImplementedError() # Deprecated
# normalise to [-1, 1]
rgb = rgb / 255.0
rgb = 2 * (rgb - 0.5)
obs_rgb += [rgb.float()]
if depth is not None:
obs_depth += [depth.float()]
if pc is not None:
obs_pc += [pc.float()]
obs = obs_rgb + obs_depth + obs_pc
return torch.cat(obs, dim=0)
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def norm_tensor(tensor: torch.Tensor) -> torch.Tensor:
return tensor / torch.linalg.norm(tensor, ord=2, dim=-1, keepdim=True)
def compute_rotation_metrics(
pred: torch.Tensor,
true: torch.Tensor,
reduction: str = "mean",
) -> Dict[str, torch.Tensor]:
pred = norm_tensor(pred)
acc = (pred - true).abs().max(1).values < 0.05
acc = acc.to(pred.dtype)
if reduction == "mean":
acc = acc.mean()
return {"rotation": acc}
def compute_rotation_loss(logit: torch.Tensor, rot: torch.Tensor):
dtype = logit.dtype
rot_ = -rot.clone()
loss = F.mse_loss(logit, rot, reduction="none").to(dtype)
loss = loss.mean(1)
loss_ = F.mse_loss(logit, rot_, reduction="none").to(dtype)
loss_ = loss_.mean(1)
select_mask = (loss < loss_).float()
sym_loss = 4 * (select_mask * loss + (1 - select_mask) * loss_)
return {"rotation": sym_loss.mean()}
def load_instructions(
instructions: Optional[Path],
tasks: Optional[Sequence[str]] = None,
variations: Optional[Sequence[int]] = None,
) -> Optional[Instructions]:
if instructions is not None:
with open(instructions, "rb") as fid:
data: Instructions = pickle.load(fid)
if tasks is not None:
data = {task: var_instr for task, var_instr in data.items() if task in tasks}
if variations is not None:
data = {
task: {
var: instr for var, instr in var_instr.items() if var in variations
}
for task, var_instr in data.items()
}
return data
return None
def normalise_quat(x: torch.Tensor):
return x / x.square().sum(dim=-1).sqrt().unsqueeze(-1)
class LossAndMetrics:
def __init__(
self,
):
task_file = Path(__file__).parent / "tasks.csv"
with open(task_file) as fid:
self.tasks = [t.strip() for t in fid.readlines()]
def compute_loss(
self, pred: Dict[str, torch.Tensor], sample: Sample
) -> Dict[str, torch.Tensor]:
device = pred["position"].device
padding_mask = sample["padding_mask"].to(device)
outputs = sample["action"].to(device)[padding_mask]
losses = {}
losses["position"] = F.mse_loss(pred["position"], outputs[:, :3]) * 3
losses.update(compute_rotation_loss(pred["rotation"], outputs[:, 3:7]))
losses["gripper"] = F.mse_loss(pred["gripper"], outputs[:, 7:8])
if pred["task"] is not None:
task = torch.Tensor([self.tasks.index(t) for t in sample["task"]])
task = task.to(device).long()
losses["task"] = F.cross_entropy(pred["task"], task)
return losses
def compute_metrics(
self, pred: Dict[str, torch.Tensor], sample: Sample
) -> Dict[str, torch.Tensor]:
device = pred["position"].device
dtype = pred["position"].dtype
padding_mask = sample["padding_mask"].to(device)
outputs = sample["action"].to(device)[padding_mask]
metrics = {}
acc = ((pred["position"] - outputs[:, :3]) ** 2).sum(1).sqrt() < 0.01
metrics["position"] = acc.to(dtype).mean()
pred_gripper = (pred["gripper"] > 0.5).squeeze(-1)
true_gripper = outputs[:, 7].bool()
acc = pred_gripper == true_gripper
metrics["gripper"] = acc.to(dtype).mean()
metrics.update(compute_rotation_metrics(pred["rotation"], outputs[:, 3:7]))
task = torch.Tensor([self.tasks.index(t) for t in sample["task"]])
task = task.to(device).long()
acc = task == pred["task"].argmax(1)
metrics["task"] = acc.to(dtype).mean()
return metrics