-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtvve_stage1.py
More file actions
792 lines (711 loc) · 37.7 KB
/
Copy pathtvve_stage1.py
File metadata and controls
792 lines (711 loc) · 37.7 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
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
from collections import defaultdict
from omegaconf import DictConfig
import torch.nn.functional as F
from torch import nn
import torch
import numpy as np
import clip
from torch.cuda.amp import autocast, GradScaler
from torch.nn.parallel.distributed import DistributedDataParallel
from torch.optim.lr_scheduler import CosineAnnealingLR
from utils.optim import Lamb, GradualWarmupScheduler
from utils.structure import ActResult
import utils.math3d as math3d
from utils.clip import clip_encode_text
from pathlib import Path
from datetime import datetime
from preprocess import CubePointCloudRenderer, preprocess_images_in_batch, \
flatten_img_pc_to_points, clamp_pc_in_bound, place_pc_in_cube, generate_heatmap_from_screen_pts, \
apply_se3_augmentation, transform_pc, grid_sample_from_heatmap, add_uniform_noise, preprocess_mem_obs_in_batch
from taskmoe_mvt import TaskMoEMVT
from utils.taskmoe import TaskMoE
from arp import AutoRegressivePolicy, TokenType, LayerType, ModelConfig
class PolicyNetwork(nn.Module):
def __init__(self, model_cfg, env_cfg, render_device):
super().__init__()
self._num_rotation_classes = model_cfg.num_rotation_classes
self._rotation_resolution = 360 / self._num_rotation_classes
self._image_resolution = [env_cfg.image_size, env_cfg.image_size]
self._transform_augmentation = model_cfg.transform_augmentation
self._place_with_mean = model_cfg.place_with_mean
self._transform_augmentation_xyz = torch.from_numpy(np.array(model_cfg.transform_augmentation_xyz))
self._transform_augmentation_rpy = model_cfg.transform_augmentation_rpy
self._transform_augmentation_rot_resolution = self._rotation_resolution
self.gt_hm_sigma = model_cfg.gt_hm_sigma
self.add_rgc_loss = model_cfg.add_rgc_loss
self.amp = model_cfg.amp
self.scene_bounds = env_cfg.scene_bounds
self.cameras = env_cfg.cameras
self.move_pc_in_bound = model_cfg.move_pc_in_bound
self.rotation_aug = model_cfg.rotation_aug # 2
self.stage2_zoom_scale = model_cfg.stage2_zoom_scale # st_sca
self.stage2_waypoint_label_noise = model_cfg.stage2_waypoint_label_noise # st_wpt_loc_aug
self.point_augment_noise = model_cfg.point_augment_noise # img_aug_2
self.num_all_rot = self._num_rotation_classes * 3
self.proprio_dim = model_cfg.proprio_dim
self.img_size = model_cfg.img_size
self.img_patch_size = model_cfg.img_patch_size
self.renderer = CubePointCloudRenderer(render_device, (model_cfg.img_size, model_cfg.img_size), with_depth=model_cfg.add_depth, cameras=model_cfg.mvt_cameras)
self.num_cameras = len(model_cfg.mvt_cameras)
if model_cfg.render_with_cpp:
assert model_cfg.mvt_cameras == ['top', 'left', 'front']
self.render_with_cpp = True
from point_renderer.rvt_renderer import RVTBoxRenderer
self.cpp_renderer = RVTBoxRenderer(device=render_device,
img_size=(model_cfg.img_size, model_cfg.img_size),
three_views=True,
with_depth=model_cfg.add_depth)
else:
self.render_with_cpp = False
self.mvt1 = TaskMoEMVT(model_cfg, renderer=self.renderer)
self.mvt2 = TaskMoEMVT(model_cfg, renderer=self.renderer)
self.spatial_logits_buffer = []
def sample_callback(lst_of_spatial_logits):
assert len(lst_of_spatial_logits) == 1
self.spatial_logits_buffer.append(lst_of_spatial_logits[0])
bs = len(lst_of_spatial_logits[0])
dev = lst_of_spatial_logits[0].device
return torch.zeros(bs, 1, 2, device=dev) # dummy output
self.sample_callback = sample_callback
# produce each xyz for stage 1
# then use xyz feature as a condition, to produce each xyz for stage 2
# then produce rot and grip separately
arp_cfg = ModelConfig(
n_embd=128,
embd_pdrop = 0.1,
max_seq_len = 6 + 6 + 3 + 2,
max_chunk_size = 2, # grip and collision
layer_norm_every_block=False,
tokens=[
TokenType.make(name='prompt-features', dim=1,
embedding='discrete', is_control=True,
embedding_kwargs={'embed_from': "prompt-features"}),
TokenType.make(
name='stage1-screen-pts', dim=2, is_continuous=True, dict_sizes=[self.img_size, self.img_size],
embedding="zero", predictor="upsample_from_2d_attn",
predictor_kwargs={'attn_with': 'visual-featmap', 'upscale_ratio': self.img_patch_size, 'label_name': 'smooth-heatmap'}),
TokenType.make(
name='stage2-screen-pts', dim=2, is_continuous=True, dict_sizes=[self.img_size, self.img_size],
embedding="zero", predictor="upsample_from_2d_attn",
predictor_kwargs={'attn_with': 'visual-featmap', 'upscale_ratio': self.img_patch_size, 'label_name': 'smooth-heatmap'}),
] + [
TokenType.make(name=f'rot-{c}', dim=1, is_continuous=False, dict_sizes=[self._num_rotation_classes], embedding='position_1d', predictor='class', predictor_kwargs={'label_name': f'rot-{c}'}) for c in ['x', 'y', 'z']
] + [
TokenType.make(name='grip', dim=1, is_continuous=False, dict_sizes=[2], embedding='discrete', predictor='class'),
TokenType.make(name='collision', dim=1, is_continuous=False, dict_sizes=[2], embedding='discrete', predictor='class')
],
layers=[
LayerType.make(n_head=8, AdaLN=True, condition_on='visual-tokens', name='cross')
] * 4 + [
LayerType.make(n_head=8, name='self')
] * 6
)
self.policy = AutoRegressivePolicy(arp_cfg)
# gripper state only depends on xyz, but not rotation
self.block_attn_directions = [(n, f'rot-{c}') for c in ['x', 'y', 'z'] for n in ['grip', 'collision']]
self.cfg = model_cfg
self.save_eval_rgb = getattr(model_cfg, "save_eval_rgb", True)
eval_rgb_dir = getattr(model_cfg, "eval_rgb_dump_dir", "rlb/outputs/eval_input_images")
self.eval_rgb_base_dir = Path(eval_rgb_dir)
self._eval_rgb_save_cfg = None
def _collect_taskmoe_metrics(self):
metrics = {}
for prefix, module in (("mvt1", getattr(self, "mvt1", None)), ("mvt2", getattr(self, "mvt2", None))):
if module is None:
continue
aggregates = defaultdict(list)
for _, submodule in module.named_modules():
if isinstance(submodule, TaskMoE):
stats = getattr(submodule, "last_step_metrics", None)
if not stats:
continue
for key, value in stats.items():
if torch.is_tensor(value):
val = float(value.detach().mean().item())
else:
val = float(value)
aggregates[key].append(val)
for key, values in aggregates.items():
metrics[f"taskmoe/{prefix}/{key}"] = float(sum(values) / max(len(values), 1))
return metrics
def _get_eval_rgb_save_cfg(self):
if self._eval_rgb_save_cfg is None:
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
run_dir = self.eval_rgb_base_dir / timestamp
run_dir.mkdir(parents=True, exist_ok=True)
self._eval_rgb_save_cfg = {
"enabled": True,
"output_dir": run_dir,
"counter": 0,
}
return self._eval_rgb_save_cfg
def multi_view_coordinate_sampler(self, lst_of_spatial_logits):
hm_logits = torch.cat([a for a in lst_of_spatial_logits], dim=1)
hm = F.softmax(hm_logits.flatten(2), dim=2)
bs = len(hm_logits)
hm = hm.view(bs, 3, 224, 224)
pred_pt = [self.renderer.get_most_likely_point_3d(hm[i : i + 1]) for i in range(bs)]
spatial_point = torch.cat(pred_pt, 0) # bs, 3
screen_points = self.renderer.points3d_to_screen2d(spatial_point[:, None, :])
screen_points = screen_points[:, 0]
return spatial_point, screen_points
def to_tk_reg_ids(self, token_name_regs):
result = []
for v in token_name_regs:
r = [self.token_name_2_ids[v[0]], v[1]]
if len(v) == 3: r.append(v[2])
result.append(r)
return result
def get_gt_rot_grip_collision(
self,
batch_size,
action_rot,
action_grip,
action_ignore_collisions,
device,
):
"""
:param batch_size: int
:param action_rot: np.array of shape (bs, 4), quternion xyzw format
:param action_grip: torch.tensor of shape (bs)
:param action_ignore_collisions: torch.tensor of shape (bs)
:param device:
"""
bs = batch_size
assert action_rot.shape == (bs, 4)
assert action_grip.shape == (bs,), (action_grip, bs)
action_rot_x_one_hot = torch.zeros(
(bs, self._num_rotation_classes), dtype=int, device=device
)
action_rot_y_one_hot = torch.zeros(
(bs, self._num_rotation_classes), dtype=int, device=device
)
action_rot_z_one_hot = torch.zeros(
(bs, self._num_rotation_classes), dtype=int, device=device
)
action_grip_one_hot = torch.zeros((bs, 2), dtype=int, device=device)
action_collision_one_hot = torch.zeros((bs, 2), dtype=int, device=device)
# fill one-hots
for b in range(bs):
gt_rot = action_rot[b]
gt_rot = math3d.quaternion_to_discrete_euler(
gt_rot, self._rotation_resolution
)
action_rot_x_one_hot[b, gt_rot[0]] = 1
action_rot_y_one_hot[b, gt_rot[1]] = 1
action_rot_z_one_hot[b, gt_rot[2]] = 1
# grip
gt_grip = action_grip[b]
action_grip_one_hot[b, gt_grip] = 1
# ignore collision (to one hot, if result = 0, then don't ignore collision)
gt_ignore_collisions = action_ignore_collisions[b, :]
action_collision_one_hot[b, gt_ignore_collisions[0]] = 1
return (
action_rot_x_one_hot,
action_rot_y_one_hot,
action_rot_z_one_hot,
action_grip_one_hot,
action_collision_one_hot,
)
def get_gt_translation_action(
self,
waypoint, # this is groundtruth 3d point
dims,
): # note: will be called separately for stage 1 / 2
bs, nc, h, w = dims
wpt_img = self.renderer.points3d_to_screen2d(waypoint.unsqueeze(1))
assert wpt_img.shape[1] == 1
wpt_img = wpt_img.squeeze(1) # (bs, num_img, 2)
action_trans = generate_heatmap_from_screen_pts(
wpt_img.reshape(-1, 2), #! just the winning points
(h, w),
sigma=self.gt_hm_sigma,
thres_sigma_times=3,
)
action_trans = action_trans.view(bs, nc, h * w).transpose(1, 2).clone()
return action_trans, wpt_img
def heatmap_to_screen_pts(self, hms):
"""
从热图(hms)中计算对应的屏幕坐标点(screen_pts)
参数:
hms: torch.Tensor, 形状为 [batch_size, num_views, height, width]
由 generate_heatmap_from_screen_pts 生成的热图,或经过 softmax 归一化的类似热图
每个热图表示一个视图的概率分布
返回:
screen_pts: torch.Tensor, 形状为 [batch_size, num_views, 2]
每个视图对应的屏幕坐标点 (x, y)
"""
device = hms.device
batch_size, num_views, height, width = hms.shape
# 创建网格坐标系统
# x_grid: 宽度方向的坐标 (0 到 width-1),形状 [width] -> 扩展为 [1, 1, height, width]
# y_grid: 高度方向的坐标 (0 到 height-1),形状 [height] -> 扩展为 [1, 1, height, width]
x_grid = torch.arange(0, width, device=device).view(1, 1, 1, width)
y_grid = torch.arange(0, height, device=device).view(1, 1, height, 1)
# 扩展网格以匹配 hms 的维度 [batch_size, num_views, height, width]
# 使用广播机制避免显式复制数据
# 计算 x 坐标:热图 * x_grid 在空间维度求和
# 计算 y 坐标:热图 * y_grid 在空间维度求和
x_coords = torch.sum(hms * x_grid, dim=[2, 3]) # 结果形状: [batch_size, num_views]
y_coords = torch.sum(hms * y_grid, dim=[2, 3]) # 结果形状: [batch_size, num_views]
# 组合 (x, y) 坐标点
screen_pts = torch.stack([x_coords, y_coords], dim=-1) # 形状: [batch_size, num_views, 2]
return screen_pts
def get_gt_translation_action_Dynamic(
self,
waypoint, # this is groundtruth 3d point
dims,
cam_look_at
): # note: will be called separately for stage 1 / 2
bs, nc, h, w = dims
wpt_img = self.renderer.points3d_to_screen2d_Dynamic(waypoint.unsqueeze(1), cam_look_at)
assert wpt_img.shape[1] == 1
wpt_img = wpt_img.squeeze(1) # (bs, num_img, 2)
action_trans = generate_heatmap_from_screen_pts(
wpt_img.reshape(-1, 2), #! just the winning points
(h, w),
sigma=self.gt_hm_sigma,
thres_sigma_times=3,
)
action_trans = action_trans.view(bs, nc, h * w).transpose(1, 2).clone()
return action_trans, wpt_img
def render(self, pc, img_feat, mvt: TaskMoEMVT):
renderer = self.cpp_renderer if self.render_with_cpp else self.renderer
with torch.no_grad():
with autocast(enabled=False):
if mvt.add_corr:
if mvt.norm_corr:
img = []
for _pc, _img_feat in zip(pc, img_feat):
max_pc = 1.0 if len(_pc) == 0 else torch.max(torch.abs(_pc))
img.append(
renderer(_pc, torch.cat((_pc / max_pc, _img_feat), dim=-1)).unsqueeze(0) # [3, 224, 224, 7], 3 -> views, 7 -> feats
)
else:
img = [renderer(_pc, torch.cat((_pc, _img_feat), dim=-1)).unsqueeze(0) for _pc, _img_feat in zip(pc, img_feat)]
else:
img = [renderer(_pc, _img_feat).unsqueeze(0) for _pc, _img_feat in zip(pc, img_feat)]
img = torch.cat(img, 0)
img = img.permute(0, 1, 4, 2, 3) # [1, 3, 7, 224, 224]
if mvt.add_pixel_loc:
bs = img.shape[0]
pixel_loc = mvt.pixel_loc.to(img.device) # extra feature
img = torch.cat(
(img, pixel_loc.unsqueeze(0).repeat(bs, 1, 1, 1, 1)), dim=2
)
return img
def forward(self, observation):
loss_dicts = []
nc, h, w = len(self.cfg.mvt_cameras), self.img_size, self.img_size
dev = observation["lang_goal_embs"].device
if self.training:
action_grip = observation["gripper_action"].int() # (b,) of int
action_ignore_collisions = observation["ignore_collisions"].view(-1, 1).int() # (b, 1) of int
action_gripper_pose = observation["gripper_pose"] # (b, 7)
action_trans_con = action_gripper_pose[:, 0:3] # (b, 3), translation in xyz
action_rot = action_gripper_pose[:, 3:7] # (b, 4), rotation in quaternion xyzw
lang_goal_embs = observation["lang_goal_embs"].float()
proprio = observation["low_dim_state"]
task_id = observation["task_idx"]
save_cfg = None
save_eval_rgb = False
if save_eval_rgb and not self.training:
save_cfg = self._get_eval_rgb_save_cfg()
obs, pcd = preprocess_images_in_batch(observation, self.cameras)
pc, img_feat = flatten_img_pc_to_points(obs, pcd)
with torch.no_grad():
if self._transform_augmentation and self.training:
action_trans_con, action_rot, pc = apply_se3_augmentation( #! where the gt really comes out (for SE3 trans)
pcd=pc,
action_gripper_pose=action_gripper_pose,
bounds=torch.tensor(self.scene_bounds),
trans_aug_range=torch.tensor(self._transform_augmentation_xyz),
rot_aug_range=torch.tensor(self._transform_augmentation_rpy),
)
action_trans_con = torch.tensor(action_trans_con).to(pc.device)
action_rot = torch.tensor(action_rot).to(pc.device)
action_rot = action_rot.cpu().numpy()
for i, _action_rot in enumerate(action_rot):
_action_rot = math3d.normalize_quaternion(_action_rot)
if _action_rot[-1] < 0:
_action_rot = -_action_rot
action_rot[i] = _action_rot
pc, img_feat = clamp_pc_in_bound(pc, img_feat, self.scene_bounds, skip=not self.move_pc_in_bound)
pc_new, rev_trans_stage1, waypoint_stage1 = [], [], []
for i, _pc in enumerate(pc):
a, b = place_pc_in_cube(_pc,
with_mean_or_bounds=self._place_with_mean,
scene_bounds=None if self._place_with_mean else self.scene_bounds,
)
if self.training:
waypoint_stage1.append(place_pc_in_cube(_pc, action_trans_con[i][:3],
with_mean_or_bounds=self._place_with_mean,
scene_bounds=None if self._place_with_mean else self.scene_bounds,
)[0].unsqueeze(0))
pc_new.append(a)
rev_trans_stage1.append(b)
pc = pc_new
bs = len(pc)
if self.training:
waypoint_stage1 = torch.cat(waypoint_stage1, axis=0).clone().detach()
if self.point_augment_noise != 0:
with torch.no_grad():
for x in img_feat:
stdv = self.point_augment_noise * torch.rand(1, device=x.device)
noise = stdv * ((2 * torch.rand(*x.shape, device=x.device)) - 1)
x += noise
img = self.render(pc, img_feat, self.mvt1)
#endregion ###########################
visual_featmap_1, loss1 = self.mvt1(img=img, task_id=task_id, proprio=proprio, lang_emb=lang_goal_embs) # [B, num_cameras, 128, np, np]
if self.training:
smooth_spatial_label_stage1, screen_waypoint_stage1 = self.get_gt_translation_action(waypoint_stage1, dims=(bs, nc, h, w))
stage1_chk_ids = torch.as_tensor([0], device=dev)[None, :]
# the 0, 0 are dummy input
seq = torch.as_tensor([0, 0, self.policy.token_name_2_ids['stage1-screen-pts']], device=dev).reshape(1, 1, 3).repeat(bs, 1, 1)
tmp_loss_dict = defaultdict(list)
for view_id in range(3):
_loss_dict = self.policy.compute_loss(seq, stage1_chk_ids, match_layer='cross',
contexts={
'visual-tokens': visual_featmap_1[:, view_id].flatten(-2, -1).permute(0, 2, 1),
'visual-featmap': visual_featmap_1[:, view_id],
'smooth-heatmap': smooth_spatial_label_stage1[:, :, view_id]
})
for k, v in _loss_dict.items():
tmp_loss_dict[k].append(v)
loss_dicts.append({k: sum(v) / len(v) for k, v in tmp_loss_dict.items()})
else:
prompt_seq = torch.zeros([bs, 0, 3], device=dev, dtype=torch.float32)
future_tk_chk_ids = [dict(chk_id=0, tk_id=self.policy.token_name_2_ids['stage1-screen-pts'])]
assert len(self.spatial_logits_buffer) == 0
for view_id in range(3):
self.policy.generate(prompt_seq, future_tk_chk_ids, match_layer='cross', sample_function=self.sample_callback,
contexts={
'visual-tokens': visual_featmap_1[:, view_id].flatten(-2, -1).permute(0, 2, 1),
'visual-featmap': visual_featmap_1[:, view_id],
})
assert len(self.spatial_logits_buffer) == (view_id + 1)
hms = torch.cat([F.softmax(hm_logits.reshape(bs, -1), dim=1).reshape(bs, 1, 224, 224)
for hm_logits in self.spatial_logits_buffer], dim=1)
pred_pt = [self.renderer.get_most_likely_point_3d(hms[i : i + 1]) for i in range(bs)]
waypoint_stage1 = torch.cat(pred_pt, 0) # bs, 3
self.spatial_logits_buffer.clear()
with torch.no_grad():
if self.training:
waypoint_stage1_noisy = add_uniform_noise(
waypoint_stage1.clone().detach(), 2 * self.stage2_waypoint_label_noise
)
pc, rev_trans_stage2 = transform_pc(pc, loc=waypoint_stage1_noisy, sca=self.stage2_zoom_scale)
waypoint_stage2, _ = transform_pc(waypoint_stage1, loc=waypoint_stage1_noisy, sca=self.stage2_zoom_scale)
else:
pc, rev_trans_stage2 = transform_pc(pc, loc=waypoint_stage1, sca=self.stage2_zoom_scale)
waypoint_stage1_noisy = waypoint_stage1
waypoint_stage2 = None
img = self.render(pc, img_feat, self.mvt2)
visual_featmap_2, loss2 = self.mvt2(img=img, task_id=task_id, proprio=proprio, lang_emb=lang_goal_embs)
if loss1 and loss2:
for i in loss1.keys():
loss_dicts.append({i:loss1[i]+loss2[i]})
if self.training:
(
action_rot_x,
action_rot_y,
action_rot_z,
action_grip, # (bs)
action_collision, # (bs)
) = [v.argmax(-1) for v in self.get_gt_rot_grip_collision(bs, action_rot, action_grip, action_ignore_collisions, device=dev)]
if self.rotation_aug:
rotation_aug = torch.from_numpy(np.random.choice(self.rotation_aug[0], p=self.rotation_aug[1], size=(bs, 3))).to(dev)
action_rot_aug_x = action_rot_x + rotation_aug[:, 0]
action_rot_aug_y = action_rot_y + rotation_aug[:, 1]
action_rot_aug_z = action_rot_z + rotation_aug[:, 2]
else:
action_rot_aug_x = action_rot_x
action_rot_aug_y = action_rot_y
action_rot_aug_z = action_rot_z
action_rot_aug_x %= self._num_rotation_classes
action_rot_aug_y %= self._num_rotation_classes
action_rot_aug_z %= self._num_rotation_classes
smooth_spatial_label_stage2, screen_waypoint_stage2 = self.get_gt_translation_action(waypoint_stage2, dims=(bs, nc, h, w))
stage2_chk_ids = torch.as_tensor([0], device=dev)[None, :]
seq = torch.as_tensor([0, 0, self.policy.token_name_2_ids['stage2-screen-pts']], device=dev).reshape(1, 1, 3).repeat(bs, 1, 1)
tmp_loss_dict = defaultdict(list)
for view_id in range(3):
_loss_dict = self.policy.compute_loss(seq, stage2_chk_ids, match_layer='cross',
contexts={
'visual-tokens': visual_featmap_2[:, view_id].flatten(-2, -1).permute(0, 2, 1),
'visual-featmap': visual_featmap_2[:, view_id],
'smooth-heatmap': smooth_spatial_label_stage2[:, :, view_id]
})
for k, v in _loss_dict.items():
tmp_loss_dict[k].append(v)
loss_dicts.append({k: sum(v) / len(v) for k, v in tmp_loss_dict.items()})
# ------------------------------------------- #
prompt_features = torch.cat([ # [bs, 6, 128]
grid_sample_from_heatmap(screen_waypoint_stage2.reshape(-1, 1, 2) / self.img_patch_size,
visual_featmap_2.flatten(0, 1))[0].reshape(bs, -1, 128),
visual_featmap_2.max(dim=-1)[0].max(dim=-1)[0]], dim=1)
seq = torch.as_tensor([(i, self.policy.token_name_2_ids['prompt-features']) for i in range(6)],
device=dev).reshape(1, 6, 2).repeat(bs, 1, 1)
seq = torch.cat([seq, torch.cat([
torch.cat([
action_rot_aug_x[:, None, None],
action_rot_aug_y[:, None, None],
action_rot_aug_z[:, None, None],
action_grip[:, None, None],
action_collision[:, None, None]], dim=1),
torch.as_tensor([self.policy.token_name_2_ids[k] for k in ['rot-x', 'rot-y', 'rot-z', 'grip', 'collision']],
device=dev)[None, :, None].repeat(bs, 1, 1)], dim=-1)
], dim=1)
chk_ids = torch.as_tensor(list(range(11)), device=dev)[None, :]
loss_dict_gripper = self.policy.compute_loss(seq, chk_ids,
block_attn_directions=self.block_attn_directions,
match_layer='self', contexts={
'prompt-features': prompt_features,
'rot-x': action_rot_x[:, None],
'rot-y': action_rot_y[:, None], 'rot-z': action_rot_z[:, None]
})
loss_dicts.append(loss_dict_gripper)
else:
prompt_seq = torch.zeros([bs, 0, 3], device=dev, dtype=torch.float32)
future_tk_chk_ids = [dict(chk_id=0, tk_id=self.policy.token_name_2_ids['stage2-screen-pts'])]
for view_id in range(3):
self.policy.generate(prompt_seq, future_tk_chk_ids, match_layer='cross', sample_function=self.sample_callback,
contexts={
'visual-tokens': visual_featmap_2[:, view_id].flatten(-2, -1).permute(0, 2, 1),
'visual-featmap': visual_featmap_2[:, view_id],
})
assert len(self.spatial_logits_buffer) == (view_id + 1)
hms = torch.cat([F.softmax(hm_logits.reshape(bs, -1), dim=1).reshape(bs, 1, 224, 224)
for hm_logits in self.spatial_logits_buffer], dim=1)
pred_pt = [self.renderer.get_most_likely_point_3d(hms[i : i + 1]) for i in range(bs)]
waypoint_stage2 = torch.cat(pred_pt, 0) # bs, 3
self.spatial_logits_buffer.clear()
screen_waypoint_stage2 = self.renderer.points3d_to_screen2d(waypoint_stage2[:, None, :])[:, 0]
prompt_features = torch.cat([ # [bs, 6, 128]
grid_sample_from_heatmap(screen_waypoint_stage2.reshape(-1, 1, 2) / self.img_patch_size,
visual_featmap_2.flatten(0, 1))[0].reshape(bs, -1, 128),
visual_featmap_2.max(dim=-1)[0].max(dim=-1)[0]], dim=1)
prompt_seq = torch.as_tensor([(i, self.policy.token_name_2_ids['prompt-features']) for i in range(6)],
device=dev).reshape(1, 6, 2).repeat(bs, 1, 1)
future_tk_chk_ids = [dict(chk_id=chk_id, tk_id=self.policy.token_name_2_ids[tk_name])
for chk_id, tk_name in zip(range(6, 11), ['rot-x', 'rot-y', 'rot-z', 'grip', 'collision'])]
result_seq_stage2 = self.policy.generate(prompt_seq, future_tk_chk_ids, match_layer='self',
sample=False, block_attn_directions=self.block_attn_directions,
contexts={
'prompt-features': prompt_features
})
if self.training:
loss_dict = {}
for d in loss_dicts: loss_dict.update(d)
norm = lambda x: torch.norm(x.flatten(1), dim=1).mean().item()
loss_dict['stat_dict'] = {
'v1_norm': norm(visual_featmap_1.flatten(0, 1)),
'v2_norm': norm(visual_featmap_2.flatten(0, 1)),
}
loss_dict['stat_dict'].update(self._collect_taskmoe_metrics())
return loss_dict
else:
final_waypoint = rev_trans_stage1[0](rev_trans_stage2(waypoint_stage2))
pred_rot = result_seq_stage2[:, 6:9, 0]
pred_rot_quat = math3d.discrete_euler_to_quaternion(pred_rot.cpu().numpy(), self._rotation_resolution)
continuous_action = np.concatenate(
(
final_waypoint[0].cpu().numpy(),
pred_rot_quat[0],
result_seq_stage2[:, 9, 0].cpu().numpy(),
result_seq_stage2[:, 10, 0].cpu().numpy(),
)
)
return continuous_action
class Policy:
def __init__(self, network: PolicyNetwork, model_cfg: DictConfig, log_dir=""):
self._optimizer_type = model_cfg.optimizer_type
self.warmup_steps = model_cfg.warmup_steps
self.lr_cos_dec = model_cfg.lr_cos_dec
self.cos_dec_max_step = model_cfg.cos_dec_max_step
self._resume = model_cfg.resume
self._lr = model_cfg.lr
self._lambda_weight_l2 = model_cfg.lambda_weight_l2
self.amp = model_cfg.amp
self.bnb = model_cfg.bnb
self.add_lang = model_cfg.add_lang
self.proprio_dim = model_cfg.proprio_dim
self.clip_grad_norm = model_cfg.clip_grad_norm
self._network = network
self.log_dir = log_dir
self.scaler = GradScaler(enabled=self.amp)
def build(self, training: bool, device: torch.device = 'cpu'):
self._training = training
self._device = device
if self._training:
if self._optimizer_type == "lamb":
if self.bnb:
import bitsandbytes as bnb
print("Using 8-Bit Optimizer")
self._optimizer = bnb.optim.LAMB(
self._network.parameters(),
lr=self._lr,
weight_decay=self._lambda_weight_l2,
betas=(0.9, 0.999),
)
else:
# From: https://github.com/cybertronai/pytorch-lamb/blob/master/pytorch_lamb/lamb.py
self._optimizer = Lamb(
self._network.parameters(),
lr=self._lr,
weight_decay=self._lambda_weight_l2,
betas=(0.9, 0.999),
adam=False,
)
elif self._optimizer_type == "adam":
self._optimizer = torch.optim.Adam(
self._network.parameters(),
lr=self._lr,
weight_decay=self._lambda_weight_l2,
)
else:
raise Exception("Unknown optimizer")
if self.lr_cos_dec:
after_scheduler = CosineAnnealingLR(
self._optimizer,
T_max=self.cos_dec_max_step,
eta_min=self._lr / 100, # mininum lr
)
else:
after_scheduler = None
self._lr_sched = GradualWarmupScheduler(
self._optimizer,
multiplier=1,
total_epoch=self.warmup_steps,
after_scheduler=after_scheduler,
)
def load_clip(self):
self.clip_model, self.clip_preprocess = clip.load("RN50", device=self._device)
self.clip_model.eval()
def unload_clip(self):
del self.clip_model
del self.clip_preprocess
with torch.cuda.device(self._device):
torch.cuda.empty_cache()
def reset(self, **kwargs):
self._network.renderer.reset()
def eval(self):
self._network.eval()
def train(self):
self._network.train()
def load(self, model_path):
checkpoint = torch.load(model_path, map_location="cpu")
epoch = checkpoint.get("epoch", checkpoint.get("step", None))
model = self._network
if isinstance(model, DistributedDataParallel):
model.module.load_state_dict(checkpoint["model_state"])
else:
model.load_state_dict(checkpoint["model_state"])
try:
self._optimizer.load_state_dict(checkpoint["optimizer_state"])
# for param_group in self._optimizer.param_groups:
# param_group['lr'] = param_group['lr'] * 0.6
except:
print("WARNING: Optimizer state not loaded. KNOW WHAT YOU ARE DOING!!")
try:
self._lr_sched.load_state_dict(checkpoint["lr_sched_state"])
# print("self._lr_sched.after_scheduler.T_max:", self._lr_sched.after_scheduler.T_max)
# self._lr_sched.after_scheduler.T_max = self.cos_dec_max_step
# print("self._lr_sched.after_scheduler.T_max:", self._lr_sched.after_scheduler.T_max)
# self.print_lr(checkpoint)
except:
print("WARNING: No lr_sched_state in checkpoint" "KNOW WHAT YOU ARE DOING!!")
return epoch
def print_lr(self, checkpoint):
print("Checkpoint lr_sched_state:")
print(checkpoint["lr_sched_state"])
print("\nCurrent lr_sched_state:")
print(self._lr_sched.state_dict())
if checkpoint["lr_sched_state"] == self._lr_sched.state_dict():
print("\nThe scheduler states are consistent.")
else:
print("\nThe scheduler states are NOT consistent. Differences:")
import pprint
for key in checkpoint["lr_sched_state"]:
if checkpoint["lr_sched_state"][key] != self._lr_sched.state_dict().get(key):
print(f"Key: {key}")
print("Checkpoint value:")
pprint.pprint(checkpoint["lr_sched_state"][key])
print("Current value:")
pprint.pprint(self._lr_sched.state_dict().get(key))
def save(self, step):
model_path = f"{self.log_dir}/model_{step}.pth"
model = self._network
optimizer = self._optimizer
lr_sched = self._lr_sched
if isinstance(model, DistributedDataParallel):
model_state = model.module.state_dict()
else:
model_state = model.state_dict()
torch.save(
{
"step": step,
"model_state": model_state,
"optimizer_state": optimizer.state_dict(),
"lr_sched_state": lr_sched.state_dict(),
},
model_path
)
def lang_strs2_embs(self, lang_strs):
lang_goal_tokens = torch.tensor(clip.tokenize(lang_strs).numpy(), device=self._device).long()
_, lang_goal_embs = clip_encode_text(self.clip_model, lang_goal_tokens)
return lang_goal_embs.float()
def lang_strs2_tokens(self, lang_strs):
lang_goal_tokens = torch.tensor(clip.tokenize(lang_strs).numpy(), device=self._device).long()
return lang_goal_tokens
def update(
self,
replay_sample: dict,
) -> dict:
assert replay_sample["gripper_pose"].shape[1:] == (7, )
assert replay_sample["lang_goal_embs"].shape[1:] == (77, 512)
assert replay_sample["low_dim_state"].shape[1:] == (self.proprio_dim,)
assert self._network.training
with autocast(enabled=self.amp):
loss_dict = self._network(replay_sample)
stat_dict = loss_dict.pop('stat_dict', {})
total_loss = sum(loss_dict.values())
self._optimizer.zero_grad(set_to_none=True)
self.scaler.scale(total_loss).backward()
if getattr(self, 'clip_grad_norm', None) and self.clip_grad_norm > 0:
self.scaler.unscale_(self._optimizer)
torch.nn.utils.clip_grad_norm_(
self._network.parameters(),
max_norm=self.clip_grad_norm,
norm_type=2
)
self.scaler.step(self._optimizer)
self.scaler.update()
if not self._resume:
self._lr_sched.step()
loss_log = {
**{k: v.item() for k, v in loss_dict.items()},
"lr": self._optimizer.param_groups[0]["lr"],
**stat_dict
}
return loss_log
@torch.no_grad()
def act(
self, step: int, observation: dict
) -> ActResult:
assert observation['left_shoulder_rgb'].size(0) == 1, "Only batch size 1 is supported for evaluation"
if self.add_lang:
lang_goal_tokens = observation.get("lang_goal_tokens", None).long()
_, lang_goal_embs = clip_encode_text(self.clip_model, lang_goal_tokens)
lang_goal_embs = lang_goal_embs.float()
observation['lang_goal_embs'] = lang_goal_embs
else:
lang_goal_embs = (
torch.zeros(observation["lang_goal_embs"].shape)
.float()
.to(self._device)
)
observation['lang_goal_embs'] = lang_goal_embs
assert not self._network.training
continuous_action = self._network(observation)
return ActResult(continuous_action)