-
Notifications
You must be signed in to change notification settings - Fork 11
/
main_utils.py
506 lines (447 loc) · 18.7 KB
/
main_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
# ------------------------------------------------------------------------
# BEAUTY DETR
# Copyright (c) 2022 Ayush Jain & Nikolaos Gkanatsios
# Licensed under CC-BY-NC [see LICENSE for details]
# All Rights Reserved
# ------------------------------------------------------------------------
# Parts adapted from Group-Free
# Copyright (c) 2021 Ze Liu. All Rights Reserved.
# Licensed under the MIT License.
# ------------------------------------------------------------------------
"""Shared utilities for all main scripts."""
import argparse
import json
import os
import random
import time
import numpy as np
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel
from models import HungarianMatcher, SetCriterion, compute_hungarian_loss
from utils import get_scheduler, setup_logger
def parse_option():
"""Parse cmd arguments."""
parser = argparse.ArgumentParser()
# Model
parser.add_argument('--num_target', type=int, default=256,
help='Proposal number')
parser.add_argument('--sampling', default='kps', type=str,
help='Query points sampling method (kps, fps)')
# Transformer
parser.add_argument('--num_encoder_layers', default=3, type=int)
parser.add_argument('--num_decoder_layers', default=6, type=int)
parser.add_argument('--self_position_embedding', default='loc_learned',
type=str, help='(none, xyz_learned, loc_learned)')
parser.add_argument('--self_attend', action='store_true')
# Loss
parser.add_argument('--query_points_obj_topk', default=4, type=int)
parser.add_argument('--use_contrastive_align', action='store_true')
parser.add_argument('--use_soft_token_loss', action='store_true')
parser.add_argument('--detect_intermediate', action='store_true')
parser.add_argument('--joint_det', action='store_true')
# Data
parser.add_argument('--batch_size', type=int, default=8,
help='Batch Size during training')
parser.add_argument('--dataset', type=str, default=['sr3d'],
nargs='+', help='list of datasets to train on')
parser.add_argument('--test_dataset', default='sr3d')
parser.add_argument('--data_root', default='./')
parser.add_argument('--use_height', action='store_true',
help='Use height signal in input.')
parser.add_argument('--use_color', action='store_true',
help='Use RGB color in input.')
parser.add_argument('--use_multiview', action='store_true')
parser.add_argument('--butd', action='store_true')
parser.add_argument('--butd_gt', action='store_true')
parser.add_argument('--butd_cls', action='store_true')
parser.add_argument('--augment_det', action='store_true')
parser.add_argument('--num_workers', type=int, default=4)
# Training
parser.add_argument('--start_epoch', type=int, default=1)
parser.add_argument('--max_epoch', type=int, default=400)
parser.add_argument('--optimizer', type=str, default='adamW')
parser.add_argument('--weight_decay', type=float, default=0.0005)
parser.add_argument("--lr", default=1e-3, type=float)
parser.add_argument("--lr_backbone", default=1e-4, type=float)
parser.add_argument("--text_encoder_lr", default=1e-5, type=float)
parser.add_argument('--lr-scheduler', type=str, default='step',
choices=["step", "cosine"])
parser.add_argument('--lr_decay_epochs', type=int, default=[280, 340],
nargs='+', help='when to decay lr, can be a list')
parser.add_argument('--lr_decay_rate', type=float, default=0.1,
help='for step scheduler. decay rate for lr')
parser.add_argument('--clip_norm', default=0.1, type=float,
help='gradient clipping max norm')
parser.add_argument('--bn_momentum', type=float, default=0.1)
parser.add_argument('--syncbn', action='store_true')
parser.add_argument('--warmup-epoch', type=int, default=-1)
parser.add_argument('--warmup-multiplier', type=int, default=100)
# io
parser.add_argument('--checkpoint_path', default=None,
help='Model checkpoint path')
parser.add_argument('--log_dir', default='log',
help='Dump dir to save model checkpoint')
parser.add_argument('--print_freq', type=int, default=10) # batch-wise
parser.add_argument('--save_freq', type=int, default=10) # epoch-wise
parser.add_argument('--val_freq', type=int, default=5) # epoch-wise
# others
parser.add_argument("--local_rank", type=int,
help='local rank for DistributedDataParallel')
parser.add_argument('--ap_iou_thresholds', type=float, default=[0.25, 0.5],
nargs='+', help='A list of AP IoU thresholds')
parser.add_argument("--rng_seed", type=int, default=0, help='manual seed')
parser.add_argument("--debug", action='store_true',
help="try to overfit few samples")
parser.add_argument('--eval', default=False, action='store_true')
parser.add_argument('--eval_train', action='store_true')
parser.add_argument('--pp_checkpoint', default=None)
parser.add_argument('--reduce_lr', action='store_true')
args, _ = parser.parse_known_args()
args.eval = args.eval or args.eval_train
return args
def load_checkpoint(args, model, optimizer, scheduler):
"""Load from checkpoint."""
print("=> loading checkpoint '{}'".format(args.checkpoint_path))
checkpoint = torch.load(args.checkpoint_path, map_location='cpu')
try:
args.start_epoch = int(checkpoint['epoch']) + 1
except Exception:
args.start_epoch = 0
model.load_state_dict(checkpoint['model'], strict=True)
if not args.eval and not args.reduce_lr:
optimizer.load_state_dict(checkpoint['optimizer'])
scheduler.load_state_dict(checkpoint['scheduler'])
print("=> loaded successfully '{}' (epoch {})".format(
args.checkpoint_path, checkpoint['epoch']
))
del checkpoint
torch.cuda.empty_cache()
def save_checkpoint(args, epoch, model, optimizer, scheduler, save_cur=False):
"""Save checkpoint if requested."""
if save_cur or epoch % args.save_freq == 0:
state = {
'config': args,
'save_path': '',
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'epoch': epoch
}
spath = os.path.join(args.log_dir, f'ckpt_epoch_{epoch}.pth')
state['save_path'] = spath
torch.save(state, spath)
print("Saved in {}".format(spath))
else:
print("not saving checkpoint")
class BaseTrainTester:
"""Basic train/test class to be inherited."""
def __init__(self, args):
"""Initialize."""
name = args.log_dir.split('/')[-1]
# Create log dir
args.log_dir = os.path.join(
args.log_dir,
','.join(args.dataset),
f'{int(time.time())}'
)
os.makedirs(args.log_dir, exist_ok=True)
# Create logger
self.logger = setup_logger(
output=args.log_dir, distributed_rank=dist.get_rank(),
name=name
)
# Save config file and initialize tb writer
if dist.get_rank() == 0:
path = os.path.join(args.log_dir, "config.json")
with open(path, 'w') as f:
json.dump(vars(args), f, indent=2)
self.logger.info("Full config saved to {}".format(path))
self.logger.info(str(vars(args)))
@staticmethod
def get_datasets(args):
"""Initialize datasets."""
train_dataset = None
test_dataset = None
return train_dataset, test_dataset
def get_loaders(self, args):
"""Initialize data loaders."""
def seed_worker(worker_id):
worker_seed = torch.initial_seed() % 2**32
np.random.seed(worker_seed)
random.seed(worker_seed)
np.random.seed(np.random.get_state()[1][0] + worker_id)
# Datasets
train_dataset, test_dataset = self.get_datasets(args)
# Samplers and loaders
g = torch.Generator()
g.manual_seed(0)
train_sampler = DistributedSampler(train_dataset)
train_loader = DataLoader(
train_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers,
worker_init_fn=seed_worker,
pin_memory=True,
sampler=train_sampler,
drop_last=True,
generator=g
)
test_sampler = DistributedSampler(test_dataset, shuffle=False)
test_loader = DataLoader(
test_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers,
worker_init_fn=seed_worker,
pin_memory=True,
sampler=test_sampler,
drop_last=False,
generator=g
)
return train_loader, test_loader
@staticmethod
def get_model(args):
"""Initialize the model."""
return None
@staticmethod
def get_criterion(args):
"""Get loss criterion for training."""
matcher = HungarianMatcher(1, 0, 2, args.use_soft_token_loss)
losses = ['boxes', 'labels']
if args.use_contrastive_align:
losses.append('contrastive_align')
set_criterion = SetCriterion(
matcher=matcher,
losses=losses, eos_coef=0.1, temperature=0.07
)
criterion = compute_hungarian_loss
return criterion, set_criterion
@staticmethod
def get_optimizer(args, model):
"""Initialize optimizer."""
param_dicts = [
{
"params": [
p for n, p in model.named_parameters()
if "backbone_net" not in n and "text_encoder" not in n
and p.requires_grad
]
},
{
"params": [
p for n, p in model.named_parameters()
if "backbone_net" in n and p.requires_grad
],
"lr": args.lr_backbone
},
{
"params": [
p for n, p in model.named_parameters()
if "text_encoder" in n and p.requires_grad
],
"lr": args.text_encoder_lr
}
]
optimizer = optim.AdamW(param_dicts,
lr=args.lr,
weight_decay=args.weight_decay)
return optimizer
def main(self, args):
"""Run main training/testing pipeline."""
# Get loaders
train_loader, test_loader = self.get_loaders(args)
n_data = len(train_loader.dataset)
self.logger.info(f"length of training dataset: {n_data}")
n_data = len(test_loader.dataset)
self.logger.info(f"length of testing dataset: {n_data}")
# Get model
model = self.get_model(args)
# Get criterion
criterion, set_criterion = self.get_criterion(args)
# Get optimizer
optimizer = self.get_optimizer(args, model)
# Get scheduler
scheduler = get_scheduler(optimizer, len(train_loader), args)
# Move model to devices
if torch.cuda.is_available():
model = model.cuda()
model = DistributedDataParallel(
model, device_ids=[args.local_rank],
broadcast_buffers=False # , find_unused_parameters=True
)
# Check for a checkpoint
if args.checkpoint_path:
assert os.path.isfile(args.checkpoint_path)
load_checkpoint(args, model, optimizer, scheduler)
# Just eval and end execution
if args.eval:
print("Testing evaluation.....................")
self.evaluate_one_epoch(
args.start_epoch, test_loader,
model, criterion, set_criterion, args
)
return
# Training loop
for epoch in range(args.start_epoch, args.max_epoch + 1):
train_loader.sampler.set_epoch(epoch)
tic = time.time()
self.train_one_epoch(
epoch, train_loader, model,
criterion, set_criterion,
optimizer, scheduler, args
)
self.logger.info(
'epoch {}, total time {:.2f}, '
'lr_base {:.5f}, lr_pointnet {:.5f}'.format(
epoch, (time.time() - tic),
optimizer.param_groups[0]['lr'],
optimizer.param_groups[1]['lr']
)
)
if epoch % args.val_freq == 0:
if dist.get_rank() == 0: # save model
save_checkpoint(args, epoch, model, optimizer, scheduler)
print("Test evaluation.......")
self.evaluate_one_epoch(
epoch, test_loader,
model, criterion, set_criterion, args
)
# Training is over, evaluate
save_checkpoint(args, 'last', model, optimizer, scheduler, True)
saved_path = os.path.join(args.log_dir, 'ckpt_epoch_last.pth')
self.logger.info("Saved in {}".format(saved_path))
self.evaluate_one_epoch(
args.max_epoch, test_loader,
model, criterion, set_criterion, args
)
return saved_path
@staticmethod
def _to_gpu(data_dict):
if torch.cuda.is_available():
for key in data_dict:
if isinstance(data_dict[key], torch.Tensor):
data_dict[key] = data_dict[key].cuda(non_blocking=True)
return data_dict
@staticmethod
def _get_inputs(batch_data):
return {
'point_clouds': batch_data['point_clouds'].float(),
'text': batch_data['utterances']
}
@staticmethod
def _compute_loss(end_points, criterion, set_criterion, args):
loss, end_points = criterion(
end_points, args.num_decoder_layers,
set_criterion,
query_points_obj_topk=args.query_points_obj_topk
)
return loss, end_points
@staticmethod
def _accumulate_stats(stat_dict, end_points):
for key in end_points:
if 'loss' in key or 'acc' in key or 'ratio' in key:
if key not in stat_dict:
stat_dict[key] = 0
if isinstance(end_points[key], (float, int)):
stat_dict[key] += end_points[key]
else:
stat_dict[key] += end_points[key].item()
return stat_dict
def train_one_epoch(self, epoch, train_loader, model,
criterion, set_criterion,
optimizer, scheduler, args):
"""
Run a single epoch.
Some of the args:
model: a nn.Module that returns end_points (dict)
criterion: a function that returns (loss, end_points)
"""
stat_dict = {} # collect statistics
model.train() # set model to training mode
# Loop over batches
for batch_idx, batch_data in enumerate(train_loader):
# Move to GPU
batch_data = self._to_gpu(batch_data)
inputs = self._get_inputs(batch_data)
# Forward pass
end_points = model(inputs)
# Compute loss and gradients, update parameters.
for key in batch_data:
assert (key not in end_points)
end_points[key] = batch_data[key]
loss, end_points = self._compute_loss(
end_points, criterion, set_criterion, args
)
optimizer.zero_grad()
loss.backward()
if args.clip_norm > 0:
grad_total_norm = torch.nn.utils.clip_grad_norm_(
model.parameters(), args.clip_norm
)
stat_dict['grad_norm'] = grad_total_norm
optimizer.step()
scheduler.step()
# Accumulate statistics and print out
stat_dict = self._accumulate_stats(stat_dict, end_points)
if (batch_idx + 1) % args.print_freq == 0:
# Terminal logs
self.logger.info(
f'Train: [{epoch}][{batch_idx + 1}/{len(train_loader)}] '
)
self.logger.info(''.join([
f'{key} {stat_dict[key] / args.print_freq:.4f} \t'
for key in sorted(stat_dict.keys())
if 'loss' in key and 'proposal_' not in key
and 'last_' not in key and 'head_' not in key
]))
for key in sorted(stat_dict.keys()):
stat_dict[key] = 0
@torch.no_grad()
def _main_eval_branch(self, batch_idx, batch_data, test_loader, model,
stat_dict,
criterion, set_criterion, args):
# Move to GPU
batch_data = self._to_gpu(batch_data)
inputs = self._get_inputs(batch_data)
if "train" not in inputs:
inputs.update({"train": False})
else:
inputs["train"] = False
# Forward pass
end_points = model(inputs)
# Compute loss
for key in batch_data:
assert (key not in end_points)
end_points[key] = batch_data[key]
_, end_points = self._compute_loss(
end_points, criterion, set_criterion, args
)
for key in end_points:
if 'pred_size' in key:
end_points[key] = torch.clamp(end_points[key], min=1e-6)
# Accumulate statistics and print out
stat_dict = self._accumulate_stats(stat_dict, end_points)
if (batch_idx + 1) % args.print_freq == 0:
self.logger.info(f'Eval: [{batch_idx + 1}/{len(test_loader)}] ')
self.logger.info(''.join([
f'{key} {stat_dict[key] / (float(batch_idx + 1)):.4f} \t'
for key in sorted(stat_dict.keys())
if 'loss' in key and 'proposal_' not in key
and 'last_' not in key and 'head_' not in key
]))
return stat_dict, end_points
@torch.no_grad()
def evaluate_one_epoch(self, epoch, test_loader,
model, criterion, set_criterion, args):
"""
Eval grounding after a single epoch.
Some of the args:
model: a nn.Module that returns end_points (dict)
criterion: a function that returns (loss, end_points)
"""
return None