-
Notifications
You must be signed in to change notification settings - Fork 59
/
run_finetuning_taskonomy.py
829 lines (716 loc) · 36.5 KB
/
run_finetuning_taskonomy.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
# Copyright (c) EPFL VILAB.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
# --------------------------------------------------------
# Based on timm, DeiT, DINO, MoCo-v3, BEiT, MAE-priv and MAE code bases
# https://github.com/rwightman/pytorch-image-models/tree/master/timm
# https://github.com/facebookresearch/deit
# https://github.com/facebookresearch/dino
# https://github.com/facebookresearch/moco-v3
# https://github.com/microsoft/unilm/tree/master/beit
# https://github.com/BUPT-PRIV/MAE-priv
# https://github.com/facebookresearch/mae
# --------------------------------------------------------
import argparse
import datetime
import json
import math
import os
import sys
import time
import warnings
from functools import partial
from pathlib import Path
from typing import Dict, Iterable, List, Optional, Union
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.nn.functional as F
import yaml
from einops import rearrange
import utils
import utils.data_constants as data_constants
from multimae import multimae
from multimae.input_adapters import PatchedInputAdapter
from multimae.output_adapters import DPTOutputAdapter
from utils import NativeScalerWithGradNormCount as NativeScaler
from utils import create_model
from utils.log_images import log_taskonomy_wandb
from utils.optim_factory import LayerDecayValueAssigner, create_optimizer
from utils.pos_embed import interpolate_pos_embed_multimae
from utils.taskonomy import TaskonomyDataset
def masked_mse_loss(preds, target, mask_valid=None):
if mask_valid is None:
mask_valid = torch.ones_like(preds).bool()
if preds.shape[1] != mask_valid.shape[1]:
mask_valid = mask_valid.repeat_interleave(preds.shape[1], 1)
element_wise_loss = (preds - target)**2
element_wise_loss[~mask_valid] = 0
return element_wise_loss.sum() / mask_valid.sum()
def masked_l1_loss(preds, target, mask_valid=None):
if mask_valid is None:
mask_valid = torch.ones_like(preds).bool()
if preds.shape[1] != mask_valid.shape[1]:
mask_valid = mask_valid.repeat_interleave(preds.shape[1], 1)
element_wise_loss = abs(preds - target)
element_wise_loss[~mask_valid] = 0
return element_wise_loss.sum() / mask_valid.sum()
DOMAIN_CONF = {
'rgb': {
'channels': 3,
'stride_level': 1,
'input_adapter': partial(PatchedInputAdapter, num_channels=3),
'loss': masked_l1_loss,
},
'depth': {
'channels': 1,
'stride_level': 1,
'input_adapter': partial(PatchedInputAdapter, num_channels=1),
'loss': masked_l1_loss,
},
'edge_occlusion': {
'channels': 1,
'stride_level': 1,
'input_adapter': partial(PatchedInputAdapter, num_channels=1),
'loss': masked_l1_loss,
},
'edge_texture': {
'channels': 1,
'stride_level': 1,
'input_adapter': partial(PatchedInputAdapter, num_channels=1),
'loss': masked_l1_loss,
},
'keypoints2d': {
'channels': 1,
'stride_level': 1,
'input_adapter': partial(PatchedInputAdapter, num_channels=1),
'loss': masked_l1_loss,
},
'keypoints3d': {
'channels': 1,
'stride_level': 1,
'input_adapter': partial(PatchedInputAdapter, num_channels=1),
'loss': masked_l1_loss,
},
'normal': {
'channels': 3,
'stride_level': 1,
'input_adapter': partial(PatchedInputAdapter, num_channels=1),
'loss': masked_l1_loss,
},
'principal_curvature': {
'channels': 2,
'stride_level': 1,
'input_adapter': partial(PatchedInputAdapter, num_channels=1),
'loss': masked_l1_loss,
},
'reshading': {
'channels': 1,
'stride_level': 1,
'input_adapter': partial(PatchedInputAdapter, num_channels=1),
'loss': masked_l1_loss,
},
}
def get_args():
config_parser = parser = argparse.ArgumentParser(description='Training Config', add_help=False)
parser.add_argument('-c', '--config', default='', type=str, metavar='FILE',
help='YAML config file specifying default arguments')
parser = argparse.ArgumentParser('MultiMAE Taskonomy fine-tuning script', add_help=False)
parser.add_argument('--batch_size', default=32, type=int, help='Batch size per GPU')
parser.add_argument('--epochs', default=100, type=int)
parser.add_argument('--save_ckpt_freq', default=20, type=int)
# Task parameters
parser.add_argument('--in_domains', default='rgb', type=str,
help='Input domain names, separated by hyphen')
parser.add_argument('--out_domains', default='normal', type=str,
help='Output domain names, separated by hyphen')
parser.add_argument('--decoder_main_tasks', type=str, default='rgb',
help='for convnext & DPT adapters, separate tasks with a hyphen')
parser.add_argument('--standardize_depth', action='store_true')
parser.add_argument('--no_standardize_depth', action='store_false', dest='standardize_depth')
parser.set_defaults(standardize_depth=True)
# Model parameters
parser.add_argument('--model', default='multivit_base', type=str, metavar='MODEL',
help='Name of MultiViT model to train')
parser.add_argument('--num_global_tokens', default=1, type=int,
help='number of global tokens to add to encoder')
parser.add_argument('--patch_size', default=16, type=int,
help='base patch size for image-like modalities')
parser.add_argument('--input_size', default=384, type=int,
help='images input size for backbone')
parser.add_argument('--drop_path_encoder', type=float, default=0.1, metavar='PCT',
help='Drop path rate (default: 0.1)')
parser.add_argument('--output_adapter', type=str, default='dpt',
choices=['dpt'],
help='One of [dpt] (default: dpt)')
parser.add_argument('--decoder_dim', default=768, type=int,
help='Token dimension inside the decoder layers')
parser.add_argument('--decoder_depth', default=2, type=int,
help='Number of self-attention layers after the initial cross attention')
parser.add_argument('--drop_path_decoder', type=float, default=0.0, metavar='PCT',
help='Drop path rate (default: 0.0)')
# Optimizer parameters
parser.add_argument('--opt', default='adamw', type=str, metavar='OPTIMIZER',
help='Optimizer (default: "adamw"')
parser.add_argument('--opt_eps', default=1e-8, type=float, metavar='EPSILON',
help='Optimizer Epsilon (default: 1e-8)')
parser.add_argument('--opt_betas', default=[0.9, 0.999], type=float, nargs='+', metavar='BETA',
help='Optimizer Betas (default: None, use opt default)')
parser.add_argument('--clip_grad', type=float, default=None, metavar='NORM',
help='Clip gradient norm (default: None, no clipping)')
parser.add_argument('--momentum', type=float, default=0.9, metavar='M',
help='SGD momentum (default: 0.9)')
parser.add_argument('--weight_decay', type=float, default=0.05,
help='weight decay (default: 0.05)')
parser.add_argument('--weight_decay_end', type=float, default=None, help="""Final value of the
weight decay. We use a cosine schedule for WD.
(Set the same value with args.weight_decay to keep weight decay no change)""")
parser.add_argument('--decoder_decay', type=float, default=None,
help='decoder weight decay')
parser.add_argument('--lr', type=float, default=3e-4, metavar='LR',
help='learning rate (default: 3e-4)')
parser.add_argument('--warmup_lr', type=float, default=1e-6, metavar='LR',
help='warmup learning rate (default: 1e-6)')
parser.add_argument('--min_lr', type=float, default=0.0, metavar='LR',
help='lower lr bound for cyclic schedulers that hit 0 (0.0)')
parser.add_argument('--layer_decay', type=float, default=0.75,
help='layer-wise lr decay from ELECTRA')
parser.add_argument('--scale_input_lr', action='store_true')
parser.add_argument('--no_scale_input_lr', action='store_false', dest='scale_input_lr')
parser.set_defaults(scale_input_lr=True)
parser.add_argument('--freeze_transformer', action='store_true')
parser.add_argument('--no_freeze_transformer', action='store_false', dest='freeze_transformer')
parser.set_defaults(freeze_transformer=False)
parser.add_argument('--warmup_epochs', type=int, default=1, metavar='N',
help='epochs to warmup LR, if scheduler supports')
parser.add_argument('--warmup_steps', type=int, default=-1, metavar='N',
help='epochs to warmup LR, if scheduler supports')
# Finetuning parameters
parser.add_argument('--finetune', default='', help='finetune from checkpoint')
# Dataset parameters
parser.add_argument('--data_path', default=data_constants.TASKONOMY_PATH, type=str, help='dataset path')
parser.add_argument('--eval_freq', default=1, type=int, help="frequency of evaluation")
parser.add_argument('--max_train_images', default=1000, type=int, help='number of train images')
parser.add_argument('--max_val_images', default=100, type=int, help='number of validation images')
parser.add_argument('--max_test_images', default=54514, type=int, help='number of test images')
parser.add_argument('--output_dir', default='',
help='path where to save, empty for no saving')
parser.add_argument('--device', default='cuda',
help='device to use for training / testing')
parser.add_argument('--seed', default=0, type=int)
parser.add_argument('--resume', default='', help='resume from checkpoint')
parser.add_argument('--auto_resume', action='store_true')
parser.add_argument('--no_auto_resume', action='store_false', dest='auto_resume')
parser.set_defaults(auto_resume=True)
parser.add_argument('--save_ckpt', action='store_true')
parser.add_argument('--no_save_ckpt', action='store_false', dest='save_ckpt')
parser.set_defaults(save_ckpt=True)
parser.add_argument('--start_epoch', default=0, type=int, metavar='N',
help='start epoch')
parser.add_argument('--eval', action='store_true',
help='Perform evaluation only')
parser.add_argument('--test', action='store_true',
help='Perform testing only')
parser.add_argument('--dist_eval', action='store_true', default=False,
help='Enabling distributed evaluation')
parser.add_argument('--no_dist_eval', action='store_false', dest='dist_eval',
help='Disabling distributed evaluation')
parser.set_defaults(dist_eval=False)
parser.add_argument('--num_workers', default=16, type=int)
parser.add_argument('--pin_mem', action='store_true',
help='Pin CPU memory in DataLoader for more efficient (sometimes) transfer to GPU.')
parser.add_argument('--no_pin_mem', action='store_false', dest='pin_mem',
help='')
parser.set_defaults(pin_mem=True)
parser.add_argument('--find_unused_params', action='store_true')
parser.add_argument('--no_find_unused_params', action='store_false', dest='find_unused_params')
parser.set_defaults(find_unused_params=True)
# Wandb logging
parser.add_argument('--log_wandb', default=False, action='store_true',
help='log training and validation metrics to wandb')
parser.add_argument('--wandb_project', default=None, type=str,
help='log training and validation metrics to wandb')
parser.add_argument('--wandb_entity', default=None, type=str,
help='user or team name of wandb')
parser.add_argument('--wandb_run_name', default=None, type=str,
help='run name on wandb')
parser.add_argument('--log_images_wandb', action='store_true')
parser.add_argument('--log_images_freq', default=5, type=int,
help="Frequency of image logging (in epochs)")
parser.add_argument('--show_user_warnings', default=False, action='store_true')
# distributed training parameters
parser.add_argument('--world_size', default=1, type=int,
help='number of distributed processes')
parser.add_argument('--local_rank', default=-1, type=int)
parser.add_argument('--dist_on_itp', action='store_true')
parser.add_argument('--dist_url', default='env://', help='url used to set up distributed training')
# Do we have a config file to parse?
args_config, remaining = config_parser.parse_known_args()
if args_config.config:
with open(args_config.config, 'r') as f:
cfg = yaml.safe_load(f)
parser.set_defaults(**cfg)
# The main arg parser parses the rest of the args, the usual
# defaults will have been overridden if config file specified.
args = parser.parse_args(remaining)
# Cache the args as a text string to save them in the output dir later
# args_text = yaml.safe_dump(args.__dict__, default_flow_style=False)
return args
def main(args):
utils.init_distributed_mode(args)
device = torch.device(args.device)
# fix the seed for reproducibility
seed = args.seed + utils.get_rank()
torch.manual_seed(seed)
np.random.seed(seed)
# random.seed(seed)
cudnn.benchmark = True
if not args.show_user_warnings:
warnings.filterwarnings("ignore", category=UserWarning)
num_tasks = utils.get_world_size()
global_rank = utils.get_rank()
if global_rank == 0 and args.log_wandb:
log_writer = utils.WandbLogger(args)
else:
log_writer = None
args.in_domains = args.in_domains.split('-')
args.out_domains = args.out_domains.split('-')
args.all_domains = list(set(args.in_domains) | set(args.out_domains))
args.all_domains.append('mask_valid')
args.decoder_main_tasks = args.decoder_main_tasks.split('-')
for task in args.decoder_main_tasks:
assert task in args.in_domains, f'Readout task {task} must be in in_domains.'
dataset_train = TaskonomyDataset(data_root=args.data_path, tasks=args.all_domains, split='train',
variant='tiny', image_size=args.input_size, max_images=args.max_train_images
)
dataset_val = TaskonomyDataset(data_root=args.data_path, tasks=args.all_domains, split='val',
variant='tiny', image_size=args.input_size, max_images=args.max_val_images
)
dataset_test = TaskonomyDataset(data_root=args.data_path, tasks=args.all_domains, split='test',
variant='tiny', image_size=args.input_size, max_images=args.max_test_images
)
if True: # args.distributed:
sampler_train = torch.utils.data.DistributedSampler(
dataset_train, num_replicas=num_tasks, rank=global_rank, shuffle=True, drop_last=True,
)
print("Sampler_train = %s" % str(sampler_train))
if args.dist_eval:
if len(dataset_val) % num_tasks != 0 or len(dataset_test) % num_tasks != 0:
print('Warning: Enabling distributed evaluation with an eval dataset not divisible by process number. '
'This will slightly alter validation results as extra duplicate entries are added to achieve '
'equal num of samples per-process.')
sampler_val = torch.utils.data.DistributedSampler(
dataset_val, num_replicas=num_tasks, rank=global_rank, shuffle=False)
sampler_test = torch.utils.data.DistributedSampler(
dataset_test, num_replicas=num_tasks, rank=global_rank, shuffle=False)
else:
sampler_val = torch.utils.data.SequentialSampler(dataset_val)
sampler_test = torch.utils.data.SequentialSampler(dataset_test)
else:
sampler_train = torch.utils.data.RandomSampler(dataset_train)
sampler_val = torch.utils.data.SequentialSampler(dataset_val)
sampler_test = torch.utils.data.SequentialSampler(dataset_test)
data_loader_train = torch.utils.data.DataLoader(
dataset_train, sampler=sampler_train,
batch_size=args.batch_size,
num_workers=args.num_workers,
pin_memory=args.pin_mem,
drop_last=True,
)
data_loader_val = torch.utils.data.DataLoader(
dataset_val, sampler=sampler_val,
batch_size=int(1.5*args.batch_size),
num_workers=args.num_workers,
pin_memory=args.pin_mem,
drop_last=False
)
data_loader_test = torch.utils.data.DataLoader(
dataset_test, sampler=sampler_test,
batch_size=int(1.5*args.batch_size),
num_workers=args.num_workers,
pin_memory=args.pin_mem,
drop_last=False
)
# Model
# Rename depth task
if 'depth_zbuffer' in args.in_domains:
args.in_domains.remove('depth_zbuffer')
args.in_domains.append('depth')
if 'depth_zbuffer' in args.out_domains:
args.out_domains.remove('depth_zbuffer')
args.out_domains.append('depth')
input_adapters = {
domain: DOMAIN_CONF[domain]['input_adapter'](
stride_level=DOMAIN_CONF[domain]['stride_level'],
patch_size_full=args.patch_size,
image_size=args.input_size,
)
for domain in args.in_domains
}
# DPT settings are fixed for ViT-B. Modify them if using a different backbone.
if args.model != 'multivit_base' and args.output_adapter == 'dpt':
raise NotImplementedError('Unsupported backbone: DPT head is fixed for ViT-B.')
adapters_dict = {
'dpt': DPTOutputAdapter,
}
output_adapters = {
domain: adapters_dict[args.output_adapter](
num_classes=DOMAIN_CONF[domain]['channels'],
stride_level=DOMAIN_CONF[domain]['stride_level'],
patch_size=args.patch_size,
main_tasks=args.decoder_main_tasks
)
for domain in args.out_domains
}
model = create_model(
args.model,
input_adapters=input_adapters,
output_adapters=output_adapters,
drop_path_rate=args.drop_path_encoder,
)
if args.finetune:
if args.finetune.startswith('https'):
checkpoint = torch.hub.load_state_dict_from_url(
args.finetune, map_location='cpu')
else:
checkpoint = torch.load(args.finetune, map_location='cpu')
checkpoint_model = checkpoint['model']
# # Remove keys for semantic segmentation
# for k in list(checkpoint_model.keys()):
# if "semseg" in k:
# del checkpoint_model[k]
# Remove output adapters
for k in list(checkpoint_model.keys()):
if "output_adapters" in k:
del checkpoint_model[k]
# Interpolate position embedding
interpolate_pos_embed_multimae(model, checkpoint_model)
# Load pre-trained model
msg = model.load_state_dict(checkpoint_model, strict=False)
print(msg)
# Optionally freeze the encoder
if args.freeze_transformer:
for param in model.encoder.parameters():
param.requires_grad = False
model.to(device)
model_without_ddp = model
n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
print("Model = %s" % str(model_without_ddp))
print('number of params: {} M'.format(n_parameters / 1e6))
tasks_loss_fn = {
domain: DOMAIN_CONF[domain]['loss']
for domain in args.out_domains
}
total_batch_size = args.batch_size * utils.get_world_size()
num_training_steps_per_epoch = len(dataset_train) // total_batch_size
print("LR = %.8f" % args.lr)
print("Batch size = %d" % total_batch_size)
print("Number of training steps = %d" % num_training_steps_per_epoch)
print("Number of training examples per epoch = %d" % (total_batch_size * num_training_steps_per_epoch))
num_layers = model_without_ddp.get_num_layers()
if args.layer_decay < 1.0:
# idx=0: input adapters, idx>0: transformer layers
layer_decay_values = list(args.layer_decay ** (num_layers + 1 - i) for i in range(num_layers + 2))
if not args.scale_input_lr:
layer_decay_values[0] = 1.0
assigner = LayerDecayValueAssigner(layer_decay_values)
else:
assigner = None
if assigner is not None:
print("Assigned values = %s" % str(assigner.values))
skip_weight_decay_list = model.no_weight_decay()
print("Skip weight decay list: ", skip_weight_decay_list)
if args.distributed:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu], find_unused_parameters=args.find_unused_params)
model_without_ddp = model.module
optimizer = create_optimizer(args, model_without_ddp, skip_list=skip_weight_decay_list,
get_num_layer=assigner.get_layer_id if assigner is not None else None,
get_layer_scale=assigner.get_scale if assigner is not None else None)
loss_scaler = NativeScaler()
print("Use step level LR & WD scheduler!")
lr_schedule_values = utils.cosine_scheduler(
args.lr, args.min_lr, args.epochs, num_training_steps_per_epoch,
warmup_epochs=args.warmup_epochs, warmup_steps=args.warmup_steps,
)
if args.weight_decay_end is None:
args.weight_decay_end = args.weight_decay
wd_schedule_values = utils.cosine_scheduler(
args.weight_decay, args.weight_decay_end, args.epochs, num_training_steps_per_epoch)
print("Max WD = %.7f, Min WD = %.7f" % (max(wd_schedule_values), min(wd_schedule_values)))
# Specifies if transformer encoder should only return last layer or all layers for DPT
return_all_layers = args.output_adapter in ['dpt']
utils.auto_load_model(
args=args, model=model, model_without_ddp=model_without_ddp, optimizer=optimizer, loss_scaler=loss_scaler)
if args.eval:
val_stats = evaluate(model=model, tasks_loss_fn=tasks_loss_fn, data_loader=data_loader_val,
device=device, epoch=-1, in_domains=args.in_domains, mode='val',
return_all_layers=return_all_layers, standardize_depth=args.standardize_depth)
print(f"Performance of the network on the {len(dataset_val)} validation images")
print(f"Loss {val_stats['loss']:.3f}")
exit(0)
if args.test:
test_stats = evaluate(model=model, tasks_loss_fn=tasks_loss_fn, data_loader=data_loader_test,
device=device, epoch=-1, in_domains=args.in_domains, mode='test',
return_all_layers=return_all_layers, standardize_depth=args.standardize_depth)
print(f"Performance of the network on the {len(dataset_test)} test images")
print(f"Loss {test_stats['loss']:.3f}")
exit(0)
print(f"Start training for {args.epochs} epochs")
start_time = time.time()
min_val_loss = np.inf
for epoch in range(args.start_epoch, args.epochs):
if args.distributed:
data_loader_train.sampler.set_epoch(epoch)
if log_writer is not None:
log_writer.set_step(epoch * num_training_steps_per_epoch)
train_stats = train_one_epoch(
model=model, tasks_loss_fn=tasks_loss_fn, data_loader=data_loader_train,
optimizer=optimizer, device=device, epoch=epoch, loss_scaler=loss_scaler,
max_norm=args.clip_grad, log_writer=log_writer, start_steps=epoch * num_training_steps_per_epoch,
lr_schedule_values=lr_schedule_values, wd_schedule_values=wd_schedule_values,
in_domains=args.in_domains, return_all_layers=return_all_layers,
standardize_depth=args.standardize_depth
)
if args.output_dir and args.save_ckpt:
if (epoch + 1) % args.save_ckpt_freq == 0 or epoch + 1 == args.epochs:
utils.save_model(
args=args, model=model, model_without_ddp=model_without_ddp, optimizer=optimizer,
loss_scaler=loss_scaler, epoch=epoch)
if epoch % args.eval_freq == 0 or epoch == args.epochs - 1:
log_images = args.log_wandb and args.log_images_wandb and (epoch % args.log_images_freq == 0)
val_stats = evaluate(model=model, tasks_loss_fn=tasks_loss_fn, data_loader=data_loader_val,
device=device, epoch=epoch, in_domains=args.in_domains, log_images=log_images,
mode='val', return_all_layers=return_all_layers, standardize_depth=args.standardize_depth)
if val_stats["loss"] < min_val_loss:
min_val_loss = val_stats["loss"]
if args.output_dir and args.save_ckpt:
utils.save_model(
args=args, model=model, model_without_ddp=model_without_ddp, optimizer=optimizer,
loss_scaler=loss_scaler, epoch="best")
print(f'New best val loss: {min_val_loss:.3f}')
log_stats = {**{f'train/{k}': v for k, v in train_stats.items()},
**{f'val/{k}': v for k, v in val_stats.items()},
'epoch': epoch,
'n_parameters': n_parameters}
else:
log_stats = {**{f'train/{k}': v for k, v in train_stats.items()},
'epoch': epoch,
'n_parameters': n_parameters}
if log_writer is not None:
log_writer.update(log_stats)
if args.output_dir and utils.is_main_process():
with open(os.path.join(args.output_dir, "log.txt"), mode="a", encoding="utf-8") as f:
f.write(json.dumps(log_stats) + "\n")
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
print('Training time {}'.format(total_time_str))
# Test with best checkpoint
print('Loading model with best validation loss')
checkpoint = torch.load(os.path.join(args.output_dir, 'checkpoint-best.pth'), map_location='cpu')
state_dict = {}
for k,v in checkpoint['model'].items():
state_dict[f'module.{k}'] = v
msg = model.load_state_dict(state_dict, strict=False)
print(msg)
print('Testing with best checkpoint')
test_stats = evaluate(model=model, tasks_loss_fn=tasks_loss_fn, data_loader=data_loader_test,
device=device, epoch=checkpoint['epoch'], in_domains=args.in_domains,
log_images=True, return_all_layers=return_all_layers,
mode='test', standardize_depth=args.standardize_depth)
log_stats = {f'test/{k}': v for k, v in test_stats.items()}
if log_writer is not None:
log_writer.set_step(args.epochs * num_training_steps_per_epoch)
log_writer.update(log_stats)
if args.output_dir and utils.is_main_process():
with open(os.path.join(args.output_dir, "log.txt"), mode="a", encoding="utf-8") as f:
f.write(json.dumps(log_stats) + "\n")
def train_one_epoch(model: torch.nn.Module, tasks_loss_fn: Dict[str, torch.nn.Module], data_loader: Iterable,
optimizer: torch.optim.Optimizer, device: torch.device, epoch: int,
loss_scaler, max_norm: float = 0, log_writer=None, start_steps=None,
lr_schedule_values=None, wd_schedule_values=None, in_domains=None,
return_all_layers=False, standardize_depth=True):
model.train()
metric_logger = utils.MetricLogger(delimiter=" ")
metric_logger.add_meter('lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
metric_logger.add_meter('min_lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
header = 'Epoch: [{}]'.format(epoch)
print_freq = 20
for step, x in enumerate(metric_logger.log_every(data_loader, print_freq, header)):
# assign learning rate & weight decay for each step
it = start_steps + step # global training iteration
if lr_schedule_values is not None or wd_schedule_values is not None:
for i, param_group in enumerate(optimizer.param_groups):
if lr_schedule_values is not None:
param_group["lr"] = lr_schedule_values[it] * param_group["lr_scale"]
if wd_schedule_values is not None and param_group["weight_decay"] > 0:
param_group["weight_decay"] = wd_schedule_values[it]
if 'depth_zbuffer' in x:
x['depth'] = x['depth_zbuffer']
del x['depth_zbuffer']
tasks_dict = {
task: tensor.to(device, non_blocking=True)
for task, tensor in x.items()
}
input_dict = {
task: tensor
for task, tensor in tasks_dict.items()
if task in in_domains
}
# Robust depth standardization
if standardize_depth and 'depth' in input_dict:
# Flatten depth and remove bottom and top 10% of non-masked values
nan_depth = input_dict['depth'].clone()
nan_depth[~tasks_dict['mask_valid']] = np.nan
trunc_depth = torch.sort(rearrange(nan_depth, 'b c h w -> b (c h w)'), dim=1)[0]
n_valid = (~torch.isnan(trunc_depth)).sum(dim=1)
from_idxs, to_idxs = (n_valid * 0.1).long(), (n_valid * 0.9).long()
robust_means = torch.stack([
trunc_depth[batch_idx, from_idx:to_idx].mean()
for batch_idx, (from_idx, to_idx) in enumerate(zip(from_idxs, to_idxs))
])
robust_vars = torch.stack([
trunc_depth[batch_idx, from_idx:to_idx].var()
for batch_idx, (from_idx, to_idx) in enumerate(zip(from_idxs, to_idxs))
])
input_dict['depth'] = (input_dict['depth'] - robust_means[:,None,None,None]) / torch.sqrt(robust_vars[:,None,None,None] + 1e-6)
input_dict['depth'][~tasks_dict['mask_valid']] = 0.0
# Mask invalid input values
for task in input_dict:
if task in ['rgb']:
continue
channels = input_dict[task].shape[1]
input_dict[task][~tasks_dict['mask_valid'].repeat_interleave(repeats=channels, dim=1)] = 0.0
# Forward + backward
with torch.cuda.amp.autocast():
preds = model(input_dict, return_all_layers=return_all_layers)
task_losses = {
task: tasks_loss_fn[task](preds[task].float(), tasks_dict[task], mask_valid=tasks_dict['mask_valid'])
for task in preds
}
loss = sum(task_losses.values())
loss_value = loss.item()
task_loss_values = {f'{task}_loss': l.item() for task, l in task_losses.items()}
if not math.isfinite(loss_value):
print("Loss is {}, stopping training".format(loss_value))
sys.exit(1)
optimizer.zero_grad()
# this attribute is added by timm on one optimizer (adahessian)
is_second_order = hasattr(optimizer, 'is_second_order') and optimizer.is_second_order
grad_norm = loss_scaler(loss, optimizer, clip_grad=max_norm,
parameters=model.parameters(), create_graph=is_second_order)
loss_scale_value = loss_scaler.state_dict()["scale"]
torch.cuda.synchronize()
# Metrics and logging
metric_logger.update(loss=loss_value)
metric_logger.update(**task_loss_values)
metric_logger.update(loss_scale=loss_scale_value)
min_lr = 10.
max_lr = 0.
for group in optimizer.param_groups:
min_lr = min(min_lr, group["lr"])
max_lr = max(max_lr, group["lr"])
metric_logger.update(lr=max_lr)
metric_logger.update(min_lr=min_lr)
weight_decay_value = None
for group in optimizer.param_groups:
if group["weight_decay"] > 0:
weight_decay_value = group["weight_decay"]
metric_logger.update(weight_decay=weight_decay_value)
metric_logger.update(grad_norm=grad_norm)
if log_writer is not None:
log_writer.update(
{
'loss': loss_value,
'lr': max_lr,
'weight_decay': weight_decay_value,
'grad_norm': grad_norm,
}
)
log_writer.set_step()
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print("Averaged stats:", metric_logger)
return {'[Epoch] ' + k: meter.global_avg for k, meter in metric_logger.meters.items()}
@torch.no_grad()
def evaluate(model, tasks_loss_fn, data_loader, device, epoch, in_domains, log_images=False,
mode='val', return_all_layers=False, standardize_depth=True):
# Switch to evaluation mode
model.eval()
metric_logger = utils.MetricLogger(delimiter=" ")
if mode == 'val':
header = '(Eval) Epoch: [{}]'.format(epoch)
elif mode == 'test':
header = '(Test) Epoch: [{}]'.format(epoch)
else:
raise ValueError(f'Invalid eval mode {mode}')
print_freq = 20
pred_images = None
gt_images = None
for x in metric_logger.log_every(data_loader, print_freq, header):
if 'depth_zbuffer' in x:
x['depth'] = x['depth_zbuffer']
del x['depth_zbuffer']
tasks_dict = {
task: tensor.to(device, non_blocking=True)
for task, tensor in x.items()
}
input_dict = {
task: tensor
for task, tensor in tasks_dict.items()
if task in in_domains
}
# Robust depth standardization
if standardize_depth and 'depth' in input_dict:
# Flatten depth and remove bottom and top 10% of non-masked values
nan_depth = input_dict['depth'].clone()
nan_depth[~tasks_dict['mask_valid']] = np.nan
trunc_depth = torch.sort(rearrange(nan_depth, 'b c h w -> b (c h w)'), dim=1)[0]
n_valid = (~torch.isnan(trunc_depth)).sum(dim=1)
from_idxs, to_idxs = (n_valid * 0.1).long(), (n_valid * 0.9).long()
robust_means = torch.stack([
trunc_depth[batch_idx, from_idx:to_idx].mean()
for batch_idx, (from_idx, to_idx) in enumerate(zip(from_idxs, to_idxs))
])
robust_vars = torch.stack([
trunc_depth[batch_idx, from_idx:to_idx].var()
for batch_idx, (from_idx, to_idx) in enumerate(zip(from_idxs, to_idxs))
])
input_dict['depth'] = (input_dict['depth'] - robust_means[:,None,None,None]) / torch.sqrt(robust_vars[:,None,None,None] + 1e-6)
input_dict['depth'][~tasks_dict['mask_valid']] = 0.0
# Mask invalid input values
for task in input_dict:
if task in ['rgb']:
continue
channels = input_dict[task].shape[1]
input_dict[task][~tasks_dict['mask_valid'].repeat_interleave(repeats=channels, dim=1)] = 0.0
# Forward + backward
with torch.cuda.amp.autocast():
preds = model(input_dict, return_all_layers=return_all_layers)
task_losses = {
task: tasks_loss_fn[task](preds[task], tasks_dict[task], mask_valid=tasks_dict['mask_valid'])
for task in preds
}
loss = sum(task_losses.values())
loss_value = loss.item()
task_loss_values = {f'{task}_loss': l.item() for task, l in task_losses.items()}
if log_images and pred_images is None and utils.is_main_process():
# Just log images of first batch
pred_images = {task: v.detach().cpu().float() for task, v in preds.items()}
gt_images = {task: v.detach().cpu().float() for task, v in input_dict.items()}
gt_images.update({task: v.detach().cpu().float() for task, v in tasks_dict.items() if task not in gt_images})
metric_logger.update(loss=loss_value)
metric_logger.update(**task_loss_values)
# Do before metrics so that void is not replaced
if log_images and utils.is_main_process():
prefix = 'val/img' if mode == 'val' else 'test/img'
log_taskonomy_wandb(pred_images, gt_images, prefix=prefix, image_count=8)
# gather the stats from all processes
metric_logger.synchronize_between_processes()
print(f'* Loss {metric_logger.loss.global_avg:.3f}')
return {k: meter.global_avg for k, meter in metric_logger.meters.items()}
if __name__ == '__main__':
opts = get_args()
if opts.output_dir:
Path(opts.output_dir).mkdir(parents=True, exist_ok=True)
main(opts)