-
Notifications
You must be signed in to change notification settings - Fork 9
/
main_keypose.py
524 lines (456 loc) · 19.7 KB
/
main_keypose.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
"""Main script for keypose optimization."""
import os
from pathlib import Path
import random
from typing import Tuple, Optional
import numpy as np
import tap
import torch
import torch.distributed as dist
from torch.nn import functional as F
from datasets import RLBenchDataset
from engine import BaseTrainTester
from utils.utils_without_rlbench import (
load_instructions, count_parameters, get_gripper_loc_bounds
)
from model import Act3D
class Arguments(tap.Tap):
# master_port: str = '29500'
local_rank: int
cameras: Tuple[str, ...] = ("wrist", "left_shoulder", "right_shoulder")
image_size: str = "256,256"
max_episodes_per_task: int = 100
instructions: Optional[Path] = "instructions.pkl"
seed: int = 0
tasks: Tuple[str, ...]
variations: Tuple[int, ...] = (0,)
checkpoint: Optional[Path] = None
accumulate_grad_batches: int = 1
val_freq: int = 500
gripper_loc_bounds: Optional[str] = None
eval_only: int = 0
# Training and validation datasets
dataset: Path
valset: Path
# Logging to base_log_dir/exp_log_dir/run_log_dir
base_log_dir: Path = Path(__file__).parent / "train_logs"
exp_log_dir: str = "exp"
run_log_dir: str = "run"
# Main training parameters
num_workers: int = 1
batch_size: int = 16
batch_size_val: int = 4
cache_size: int = 100
cache_size_val: int = 100
lr: float = 1e-4
train_iters: int = 200_000
max_episode_length: int = 5 # -1 for no limit
# Toggle to switch between our models
model: str = "diffusion" # one of "diffusion", "regression"
# Data augmentations
image_rescale: str = "0.75,1.25" # (min, max), "1.0,1.0" for no rescaling
point_cloud_rotate_yaw_range: float = 0.0 # in degrees, 0.0 for no rot
# Loss
position_prediction_only: int = 0
position_loss: str = "ce" # one of "ce" (our model), "mse" (HiveFormer)
ground_truth_gaussian_spread: float = 0.01
compute_loss_at_all_layers: int = 0
position_loss_coeff: float = 1.0
position_offset_loss_coeff: float = 10000.0
rotation_loss_coeff: float = 10.0
symmetric_rotation_loss: int = 0
gripper_loss_coeff: float = 1.0
label_smoothing: float = 0.0
regress_position_offset: int = 0
# Ghost points
num_sampling_level: int = 3
fine_sampling_ball_diameter: float = 0.16
weight_tying: int = 1
gp_emb_tying: int = 1
num_ghost_points: int = 1000
num_ghost_points_val: int = 10000
use_ground_truth_position_for_sampling_train: int = 1 # considerably speeds up training
use_ground_truth_position_for_sampling_val: int = 0 # for debugging
# Model
backbone: str = "clip" # one of "resnet", "clip"
embedding_dim: int = 60
num_ghost_point_cross_attn_layers: int = 2
num_query_cross_attn_layers: int = 2
num_vis_ins_attn_layers: int = 2
rotation_parametrization: str = "quat_from_query"
use_instruction: int = 0
class TrainTester(BaseTrainTester):
"""Train/test a keypose optimization algorithm."""
def __init__(self, args):
"""Initialize."""
super().__init__(args)
def get_datasets(self):
"""Initialize datasets."""
# Load instruction, based on which we load tasks/variations
instruction = load_instructions(
self.args.instructions,
tasks=self.args.tasks,
variations=self.args.variations
)
if instruction is None:
raise NotImplementedError()
else:
taskvar = [
(task, var)
for task, var_instr in instruction.items()
for var in var_instr.keys()
]
# Initialize datasets with arguments
train_dataset = RLBenchDataset(
root=self.args.dataset,
instructions=instruction,
taskvar=taskvar,
max_episode_length=self.args.max_episode_length,
max_episodes_per_task=self.args.max_episodes_per_task,
cache_size=self.args.cache_size,
num_iters=self.args.train_iters,
cameras=self.args.cameras,
training=True,
gripper_loc_bounds=self.args.gripper_loc_bounds,
image_rescale=tuple(
float(x) for x in self.args.image_rescale.split(",")
),
point_cloud_rotate_yaw_range=self.args.point_cloud_rotate_yaw_range,
return_low_lvl_trajectory=False,
dense_interpolation=False,
interpolation_length=0,
action_dim=8,
predict_short=False
)
test_dataset = RLBenchDataset(
root=self.args.valset,
instructions=instruction,
taskvar=taskvar,
max_episode_length=self.args.max_episode_length,
max_episodes_per_task=self.args.max_episodes_per_task,
cache_size=self.args.cache_size_val,
cameras=self.args.cameras,
training=False,
gripper_loc_bounds=self.args.gripper_loc_bounds,
image_rescale=tuple(
float(x) for x in self.args.image_rescale.split(",")
),
point_cloud_rotate_yaw_range=self.args.point_cloud_rotate_yaw_range,
return_low_lvl_trajectory=False,
dense_interpolation=False,
interpolation_length=0,
action_dim=8,
predict_short=False
)
return train_dataset, test_dataset
def get_model(self):
"""Initialize the model."""
# Initialize model with arguments
args = self.args
_model = Act3D(
backbone=args.backbone,
image_size=tuple(int(x) for x in args.image_size.split(",")),
embedding_dim=args.embedding_dim,
num_ghost_point_cross_attn_layers=args.num_ghost_point_cross_attn_layers,
num_query_cross_attn_layers=args.num_query_cross_attn_layers,
num_vis_ins_attn_layers=args.num_vis_ins_attn_layers,
rotation_parametrization=args.rotation_parametrization,
gripper_loc_bounds=self.args.gripper_loc_bounds,
num_ghost_points=args.num_ghost_points,
num_ghost_points_val=args.num_ghost_points_val,
weight_tying=bool(args.weight_tying),
gp_emb_tying=bool(args.gp_emb_tying),
num_sampling_level=args.num_sampling_level,
fine_sampling_ball_diameter=args.fine_sampling_ball_diameter,
regress_position_offset=bool(args.regress_position_offset),
use_instruction=bool(args.use_instruction)
)
print("Model parameters:", count_parameters(_model))
return _model
def get_criterion(self):
args = self.args
return LossAndMetrics(
position_prediction_only=bool(args.position_prediction_only),
rotation_parametrization=args.rotation_parametrization,
position_loss=args.position_loss,
compute_loss_at_all_layers=bool(args.compute_loss_at_all_layers),
ground_truth_gaussian_spread=args.ground_truth_gaussian_spread,
label_smoothing=args.label_smoothing,
position_loss_coeff=args.position_loss_coeff,
position_offset_loss_coeff=args.position_offset_loss_coeff,
rotation_loss_coeff=args.rotation_loss_coeff,
gripper_loss_coeff=args.gripper_loss_coeff,
symmetric_rotation_loss=bool(args.symmetric_rotation_loss)
)
def train_one_step(self, model, criterion, optimizer, step_id, sample):
"""Run a single training step."""
if step_id % self.args.accumulate_grad_batches == 0:
optimizer.zero_grad()
# Forward pass
out = model(
sample["rgbs"],
sample["pcds"],
sample["instr"],
sample["curr_gripper"],
# Provide ground-truth action to bias ghost point sampling at training time
gt_action=sample["action"] if self.args.use_ground_truth_position_for_sampling_train else None
)
# Backward pass
loss = criterion.compute_loss(out, sample)
loss = sum(list(loss.values()))
loss.backward()
# Update
if step_id % self.args.accumulate_grad_batches == self.args.accumulate_grad_batches - 1:
optimizer.step()
# Log
if dist.get_rank() == 0 and (step_id + 1) % self.args.val_freq == 0:
self.writer.add_scalar("lr", self.args.lr, step_id)
self.writer.add_scalar("train-loss/noise_mse", loss, step_id)
@torch.no_grad()
def evaluate_nsteps(self, model, criterion, loader, step_id, val_iters,
split='val'):
"""Run a given number of evaluation steps."""
values = {}
device = next(model.parameters()).device
model.eval()
for i, sample in enumerate(loader):
if i == val_iters:
break
action = model(
sample["rgbs"],
sample["pcds"],
sample["instr"],
sample["curr_gripper"],
# DO NOT provide ground-truth action to sample ghost points at validation time
gt_action=None
)
losses = criterion.compute_metrics(
action,
sample
)
# Gather global statistics
for n, l in losses.items():
key = f"{split}-losses/{n}"
if key not in values:
values[key] = torch.Tensor([]).to(device)
values[key] = torch.cat([values[key], l.unsqueeze(0)])
# Log all statistics
values = {
k: torch.as_tensor(v).mean().item() for k, v in values.items()
}
if dist.get_rank() == 0:
for key, val in values.items():
self.writer.add_scalar(key, val, step_id)
# Also log to terminal
print(f"Step {step_id}:")
for key, value in values.items():
print(f"{key}: {value:.03f}")
return values.get('val-losses/action_mse', None)
def keypose_collate_fn(batch):
# Unfold multi-step demos to form a longer batch
keys = ["rgbs", "pcds", "curr_gripper", "action", "instr"]
ret_dict = {key: torch.cat([item[key] for item in batch]) for key in keys}
ret_dict["task"] = []
for item in batch:
ret_dict["task"] += item['task']
return ret_dict
class LossAndMetrics:
"""
Each method expects two dictionaries:
- pred: {
'position': (B, 3) gripper position,
'rotation': (B, 4) gripper rotation,
'gripper': (B, 1) whether gripper should open/close (0/1),
'position_pyramid': list of 3 elements, (B, 1, 3) interm gripper pos,
'visible_rgb_mask_pyramid': not used in loss,
'ghost_pcd_masks_pyramid',
'ghost_pcd_pyramid',
'fine_ghost_pcd_offsets',
'task'
}
- sample: {
'frame_id',
'task_id',
'task',
'variation',
'rgbs',
'pcds',
'action': (B, 1, 8),
'padding_mask': (B, 1),
'instr',
'gripper'
}
"""
def __init__(
self,
position_loss,
rotation_parametrization,
ground_truth_gaussian_spread,
position_prediction_only=False,
compute_loss_at_all_layers=False,
label_smoothing=0.0,
position_loss_coeff=1.0,
position_offset_loss_coeff=10000.0,
rotation_loss_coeff=10.0,
gripper_loss_coeff=1.0,
symmetric_rotation_loss=False,
):
assert position_loss in ["mse", "ce", "ce+mse"]
assert rotation_parametrization in [
"quat_from_top_ghost", "quat_from_query",
"6D_from_top_ghost", "6D_from_query"
]
self.position_loss = position_loss
self.rotation_parametrization = rotation_parametrization
self.position_prediction_only = position_prediction_only
self.compute_loss_at_all_layers = compute_loss_at_all_layers
self.ground_truth_gaussian_spread = ground_truth_gaussian_spread
self.label_smoothing = label_smoothing
self.position_loss_coeff = position_loss_coeff
self.position_offset_loss_coeff = position_offset_loss_coeff
self.rotation_loss_coeff = rotation_loss_coeff
self.gripper_loss_coeff = gripper_loss_coeff
self.symmetric_rotation_loss = symmetric_rotation_loss
def compute_loss(self, pred, sample):
device = pred["position"].device
# padding_mask = sample["padding_mask"].to(device)
gt_action = sample["action"].to(device) # [padding_mask]
losses = {}
self._compute_position_loss(pred, gt_action[:, :3], losses)
self._compute_rotation_loss(pred, gt_action[:, 3:7], losses)
losses["gripper"] = F.mse_loss(pred["gripper"], gt_action[:, 7:8])
losses["gripper"] *= self.gripper_loss_coeff
return losses
def _compute_rotation_loss(self, pred, gt_quat, losses):
if "quat" in self.rotation_parametrization:
if self.symmetric_rotation_loss:
gt_quat_ = -gt_quat.clone()
quat_loss = F.mse_loss(pred["rotation"], gt_quat, reduction='none').mean(1)
quat_loss_ = F.mse_loss(pred["rotation"], gt_quat_, reduction='none').mean(1)
select_mask = (quat_loss < quat_loss_).float()
losses['rotation'] = (select_mask * quat_loss + (1 - select_mask) * quat_loss_).mean()
else:
losses["rotation"] = F.mse_loss(pred["rotation"], gt_quat)
losses["rotation"] *= self.rotation_loss_coeff
def _compute_position_loss(self, pred, gt_position, losses):
if self.position_loss == "mse":
# Only used for original HiveFormer
losses["position_mse"] = F.mse_loss(pred["position"], gt_position) * self.position_loss_coeff
elif self.position_loss in ["ce", "ce+mse"]:
# Select a normalized Gaussian ball around the ground-truth
# as a proxy label for a soft cross-entropy loss
l2_pyramid = []
label_pyramid = []
for ghost_pcd_i in pred['ghost_pcd_pyramid']:
l2_i = ((ghost_pcd_i - gt_position.unsqueeze(-1)) ** 2).sum(1).sqrt()
label_i = torch.softmax(-l2_i / self.ground_truth_gaussian_spread, dim=-1).detach()
l2_pyramid.append(l2_i)
label_pyramid.append(label_i)
loss_layers = range(len(pred['ghost_pcd_masks_pyramid'][0])) if self.compute_loss_at_all_layers else [-1]
for j in loss_layers:
for i, ghost_pcd_masks_i in enumerate(pred["ghost_pcd_masks_pyramid"]):
losses[f"position_ce_level{i}"] = F.cross_entropy(
ghost_pcd_masks_i[j], label_pyramid[i],
label_smoothing=self.label_smoothing
).mean() * self.position_loss_coeff / len(pred["ghost_pcd_masks_pyramid"])
# Supervise offset from the ghost point's position to the predicted position
num_sampling_level = len(pred['ghost_pcd_masks_pyramid'])
if pred.get("fine_ghost_pcd_offsets") is not None:
if pred["ghost_pcd_pyramid"][-1].shape[-1] != pred["ghost_pcd_pyramid"][0].shape[-1]:
npts = pred["ghost_pcd_pyramid"][-1].shape[-1] // num_sampling_level
pred_with_offset = (pred["ghost_pcd_pyramid"][-1] + pred["fine_ghost_pcd_offsets"])[:, :, -npts:]
else:
pred_with_offset = (pred["ghost_pcd_pyramid"][-1] + pred["fine_ghost_pcd_offsets"])
losses["position_offset"] = F.mse_loss(
pred_with_offset,
gt_position.unsqueeze(-1).repeat(1, 1, pred_with_offset.shape[-1])
)
losses["position_offset"] *= (self.position_offset_loss_coeff * self.position_loss_coeff)
if self.position_loss == "ce":
# Clear gradient on pred["position"] to avoid a memory leak since we don't
# use it in the loss
pred["position"] = pred["position"].detach()
else:
losses["position_mse"] = (
F.mse_loss(pred["position"], gt_position)
* self.position_loss_coeff
)
def compute_metrics(self, pred, sample):
device = pred["position"].device
dtype = pred["position"].dtype
# padding_mask = sample["padding_mask"].to(device)
outputs = sample["action"].to(device) # [padding_mask]
metrics = {}
tasks = np.array(sample["task"])
final_pos_l2 = ((pred["position"] - outputs[:, :3]) ** 2).sum(1).sqrt()
metrics["mean/pos_l2_final"] = final_pos_l2.to(dtype).mean()
metrics["mean/pos_l2_final<0.01"] = (final_pos_l2 < 0.01).to(dtype).mean()
for i in range(len(pred["position_pyramid"])):
pos_l2_i = ((pred["position_pyramid"][i].squeeze(1) - outputs[:, :3]) ** 2).sum(1).sqrt()
metrics[f"mean/pos_l2_level{i}"] = pos_l2_i.to(dtype).mean()
for task in np.unique(tasks):
task_l2 = final_pos_l2[tasks == task]
metrics[f"{task}/pos_l2_final"] = task_l2.to(dtype).mean()
metrics[f"{task}/pos_l2_final<0.01"] = (task_l2 < 0.01).to(dtype).mean()
# Gripper accuracy
pred_gripper = (pred["gripper"] > 0.5).squeeze(-1)
true_gripper = outputs[:, 7].bool()
acc = pred_gripper == true_gripper
metrics["gripper"] = acc.to(dtype).mean()
# Rotation accuracy
gt_quat = outputs[:, 3:7]
if "quat" in self.rotation_parametrization:
if self.symmetric_rotation_loss:
gt_quat_ = -gt_quat.clone()
l1 = (pred["rotation"] - gt_quat).abs().sum(1)
l1_ = (pred["rotation"] - gt_quat_).abs().sum(1)
select_mask = (l1 < l1_).float()
l1 = (select_mask * l1 + (1 - select_mask) * l1_)
else:
l1 = ((pred["rotation"] - gt_quat).abs().sum(1))
metrics["mean/rot_l1"] = l1.to(dtype).mean()
metrics["mean/rot_l1<0.05"] = (l1 < 0.05).to(dtype).mean()
metrics["mean/rot_l1<0.025"] = (l1 < 0.025).to(dtype).mean()
for task in np.unique(tasks):
task_l1 = l1[tasks == task]
metrics[f"{task}/rot_l1"] = task_l1.to(dtype).mean()
metrics[f"{task}/rot_l1<0.05"] = (task_l1 < 0.05).to(dtype).mean()
metrics[f"{task}/rot_l1<0.025"] = (task_l1 < 0.025).to(dtype).mean()
return metrics
if __name__ == '__main__':
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# Arguments
args = Arguments().parse_args()
print("Arguments:")
print(args)
print("-" * 100)
if args.gripper_loc_bounds is None:
args.gripper_loc_bounds = np.array([[-2, -2, -2], [2, 2, 2]]) * 1.0
else:
args.gripper_loc_bounds = get_gripper_loc_bounds(
args.gripper_loc_bounds,
task=args.tasks[0] if len(args.tasks) == 1 else None,
buffer=0.04
)
log_dir = args.base_log_dir / args.exp_log_dir / args.run_log_dir
args.log_dir = log_dir
log_dir.mkdir(exist_ok=True, parents=True)
print("Logging:", log_dir)
print(
"Available devices (CUDA_VISIBLE_DEVICES):",
os.environ.get("CUDA_VISIBLE_DEVICES")
)
print("Device count", torch.cuda.device_count())
# Seeds
torch.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
# DDP initialization
torch.cuda.set_device(args.local_rank)
torch.distributed.init_process_group(backend='nccl', init_method='env://')
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = True
# Run
train_tester = TrainTester(args)
train_tester.main(collate_fn=keypose_collate_fn)