-
Notifications
You must be signed in to change notification settings - Fork 254
/
eval.py
561 lines (498 loc) · 17.7 KB
/
eval.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import os
# -- FOR DISTRIBUTED TRAINING ENSURE ONLY 1 DEVICE VISIBLE PER PROCESS
try:
# -- WARNING: IF DOING DISTRIBUTED TRAINING ON A NON-SLURM CLUSTER, MAKE
# -- SURE TO UPDATE THIS TO GET LOCAL-RANK ON NODE, OR ENSURE
# -- THAT YOUR JOBS ARE LAUNCHED WITH ONLY 1 DEVICE VISIBLE
# -- TO EACH PROCESS
os.environ['CUDA_VISIBLE_DEVICES'] = os.environ['SLURM_LOCALID']
except Exception:
pass
import logging
import pprint
import numpy as np
import torch
import torch.multiprocessing as mp
import torch.nn.functional as F
from torch.nn.parallel import DistributedDataParallel
import src.models.vision_transformer as vit
from src.models.attentive_pooler import AttentiveClassifier
from src.datasets.data_manager import (
init_data,
)
from src.utils.distributed import (
init_distributed,
AllReduce
)
from src.utils.schedulers import (
WarmupCosineSchedule,
CosineWDSchedule,
)
from src.utils.logging import (
AverageMeter,
CSVLogger
)
from evals.video_classification_frozen.utils import (
make_transforms,
ClipAggregation,
FrameAggregation
)
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
_GLOBAL_SEED = 0
np.random.seed(_GLOBAL_SEED)
torch.manual_seed(_GLOBAL_SEED)
torch.backends.cudnn.benchmark = True
pp = pprint.PrettyPrinter(indent=4)
def main(args_eval, resume_preempt=False):
# ----------------------------------------------------------------------- #
# PASSED IN PARAMS FROM CONFIG FILE
# ----------------------------------------------------------------------- #
# -- PRETRAIN
args_pretrain = args_eval.get('pretrain')
checkpoint_key = args_pretrain.get('checkpoint_key', 'target_encoder')
model_name = args_pretrain.get('model_name', None)
patch_size = args_pretrain.get('patch_size', None)
pretrain_folder = args_pretrain.get('folder', None)
ckp_fname = args_pretrain.get('checkpoint', None)
tag = args_pretrain.get('write_tag', None)
use_sdpa = args_pretrain.get('use_sdpa', True)
use_SiLU = args_pretrain.get('use_silu', False)
tight_SiLU = args_pretrain.get('tight_silu', True)
uniform_power = args_pretrain.get('uniform_power', False)
pretrained_path = os.path.join(pretrain_folder, ckp_fname)
# Optional [for Video model]:
tubelet_size = args_pretrain.get('tubelet_size', 2)
pretrain_frames_per_clip = args_pretrain.get('frames_per_clip', 1)
# -- DATA
args_data = args_eval.get('data')
train_data_path = [args_data.get('dataset_train')]
val_data_path = [args_data.get('dataset_val')]
dataset_type = args_data.get('dataset_type', 'VideoDataset')
num_classes = args_data.get('num_classes')
eval_num_segments = args_data.get('num_segments', 1)
eval_frames_per_clip = args_data.get('frames_per_clip', 16)
eval_frame_step = args_pretrain.get('frame_step', 4)
eval_duration = args_pretrain.get('clip_duration', None)
eval_num_views_per_segment = args_data.get('num_views_per_segment', 1)
# -- OPTIMIZATION
args_opt = args_eval.get('optimization')
resolution = args_opt.get('resolution', 224)
batch_size = args_opt.get('batch_size')
attend_across_segments = args_opt.get('attend_across_segments', False)
num_epochs = args_opt.get('num_epochs')
wd = args_opt.get('weight_decay')
start_lr = args_opt.get('start_lr')
lr = args_opt.get('lr')
final_lr = args_opt.get('final_lr')
warmup = args_opt.get('warmup')
use_bfloat16 = args_opt.get('use_bfloat16')
# -- EXPERIMENT-ID/TAG (optional)
resume_checkpoint = args_eval.get('resume_checkpoint', False) or resume_preempt
eval_tag = args_eval.get('tag', None)
# ----------------------------------------------------------------------- #
try:
mp.set_start_method('spawn')
except Exception:
pass
if not torch.cuda.is_available():
device = torch.device('cpu')
else:
device = torch.device('cuda:0')
torch.cuda.set_device(device)
world_size, rank = init_distributed()
logger.info(f'Initialized (rank/world-size) {rank}/{world_size}')
# -- log/checkpointing paths
folder = os.path.join(pretrain_folder, 'video_classification_frozen/')
if eval_tag is not None:
folder = os.path.join(folder, eval_tag)
if not os.path.exists(folder):
os.makedirs(folder, exist_ok=True)
log_file = os.path.join(folder, f'{tag}_r{rank}.csv')
latest_path = os.path.join(folder, f'{tag}-latest.pth.tar')
# -- make csv_logger
if rank == 0:
csv_logger = CSVLogger(log_file,
('%d', 'epoch'),
('%.5f', 'loss'),
('%.5f', 'acc'))
# Initialize model
# -- pretrained encoder (frozen)
encoder = init_model(
crop_size=resolution,
device=device,
pretrained=pretrained_path,
model_name=model_name,
patch_size=patch_size,
tubelet_size=tubelet_size,
frames_per_clip=pretrain_frames_per_clip,
uniform_power=uniform_power,
checkpoint_key=checkpoint_key,
use_SiLU=use_SiLU,
tight_SiLU=tight_SiLU,
use_sdpa=use_sdpa)
if pretrain_frames_per_clip == 1:
# Process each frame independently and aggregate
encoder = FrameAggregation(encoder).to(device)
else:
# Process each video clip independently and aggregate
encoder = ClipAggregation(
encoder,
tubelet_size=tubelet_size,
attend_across_segments=attend_across_segments
).to(device)
encoder.eval()
for p in encoder.parameters():
p.requires_grad = False
# -- init classifier
classifier = AttentiveClassifier(
embed_dim=encoder.embed_dim,
num_heads=encoder.num_heads,
depth=1,
num_classes=num_classes,
).to(device)
train_loader = make_dataloader(
dataset_type=dataset_type,
root_path=train_data_path,
resolution=resolution,
frames_per_clip=eval_frames_per_clip,
frame_step=eval_frame_step,
eval_duration=eval_duration,
num_segments=eval_num_segments if attend_across_segments else 1,
num_views_per_segment=1,
allow_segment_overlap=True,
batch_size=batch_size,
world_size=world_size,
rank=rank,
training=True)
val_loader = make_dataloader(
dataset_type=dataset_type,
root_path=val_data_path,
resolution=resolution,
frames_per_clip=eval_frames_per_clip,
frame_step=eval_frame_step,
num_segments=eval_num_segments,
eval_duration=eval_duration,
num_views_per_segment=eval_num_views_per_segment,
allow_segment_overlap=True,
batch_size=batch_size,
world_size=world_size,
rank=rank,
training=False)
ipe = len(train_loader)
logger.info(f'Dataloader created... iterations per epoch: {ipe}')
# -- optimizer and scheduler
optimizer, scaler, scheduler, wd_scheduler = init_opt(
classifier=classifier,
wd=wd,
start_lr=start_lr,
ref_lr=lr,
final_lr=final_lr,
iterations_per_epoch=ipe,
warmup=warmup,
num_epochs=num_epochs,
use_bfloat16=use_bfloat16)
classifier = DistributedDataParallel(classifier, static_graph=True)
# -- load training checkpoint
start_epoch = 0
if resume_checkpoint:
classifier, optimizer, scaler, start_epoch = load_checkpoint(
device=device,
r_path=latest_path,
classifier=classifier,
opt=optimizer,
scaler=scaler)
for _ in range(start_epoch*ipe):
scheduler.step()
wd_scheduler.step()
def save_checkpoint(epoch):
save_dict = {
'classifier': classifier.state_dict(),
'opt': optimizer.state_dict(),
'scaler': None if scaler is None else scaler.state_dict(),
'epoch': epoch,
'batch_size': batch_size,
'world_size': world_size,
'lr': lr
}
if rank == 0:
torch.save(save_dict, latest_path)
# TRAIN LOOP
for epoch in range(start_epoch, num_epochs):
logger.info('Epoch %d' % (epoch + 1))
train_acc = run_one_epoch(
device=device,
training=True,
num_temporal_views=eval_num_segments if attend_across_segments else 1,
attend_across_segments=attend_across_segments,
num_spatial_views=1,
encoder=encoder,
classifier=classifier,
scaler=scaler,
optimizer=optimizer,
scheduler=scheduler,
wd_scheduler=wd_scheduler,
data_loader=train_loader,
use_bfloat16=use_bfloat16)
val_acc = run_one_epoch(
device=device,
training=False,
num_temporal_views=eval_num_segments,
attend_across_segments=attend_across_segments,
num_spatial_views=eval_num_views_per_segment,
encoder=encoder,
classifier=classifier,
scaler=scaler,
optimizer=optimizer,
scheduler=scheduler,
wd_scheduler=wd_scheduler,
data_loader=val_loader,
use_bfloat16=use_bfloat16)
logger.info('[%5d] train: %.3f%% test: %.3f%%' % (epoch + 1, train_acc, val_acc))
if rank == 0:
csv_logger.log(epoch + 1, train_acc, val_acc)
save_checkpoint(epoch + 1)
def run_one_epoch(
device,
training,
encoder,
classifier,
scaler,
optimizer,
scheduler,
wd_scheduler,
data_loader,
use_bfloat16,
num_spatial_views,
num_temporal_views,
attend_across_segments,
):
classifier.train(mode=training)
criterion = torch.nn.CrossEntropyLoss()
top1_meter = AverageMeter()
for itr, data in enumerate(data_loader):
if training:
scheduler.step()
wd_scheduler.step()
with torch.cuda.amp.autocast(dtype=torch.float16, enabled=use_bfloat16):
# Load data and put on GPU
clips = [
[dij.to(device, non_blocking=True) for dij in di] # iterate over spatial views of clip
for di in data[0] # iterate over temporal index of clip
]
clip_indices = [d.to(device, non_blocking=True) for d in data[2]]
labels = data[1].to(device)
batch_size = len(labels)
# Forward and prediction
with torch.no_grad():
outputs = encoder(clips, clip_indices)
if not training:
if attend_across_segments:
outputs = [classifier(o) for o in outputs]
else:
outputs = [[classifier(ost) for ost in os] for os in outputs]
if training:
if attend_across_segments:
outputs = [classifier(o) for o in outputs]
else:
outputs = [[classifier(ost) for ost in os] for os in outputs]
# Compute loss
if attend_across_segments:
loss = sum([criterion(o, labels) for o in outputs]) / len(outputs)
else:
loss = sum([sum([criterion(ost, labels) for ost in os]) for os in outputs]) / len(outputs) / len(outputs[0])
with torch.no_grad():
if attend_across_segments:
outputs = sum([F.softmax(o, dim=1) for o in outputs]) / len(outputs)
else:
outputs = sum([sum([F.softmax(ost, dim=1) for ost in os]) for os in outputs]) / len(outputs) / len(outputs[0])
top1_acc = 100. * outputs.max(dim=1).indices.eq(labels).sum() / batch_size
top1_acc = float(AllReduce.apply(top1_acc))
top1_meter.update(top1_acc)
if training:
if use_bfloat16:
scaler.scale(loss).backward()
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(classifier.parameters(), 1.0)
scaler.step(optimizer)
scaler.update()
else:
loss.backward()
torch.nn.utils.clip_grad_norm_(classifier.parameters(), 1.0)
optimizer.step()
optimizer.zero_grad()
if itr % 20 == 0:
logger.info('[%5d] %.3f%% (loss: %.3f) [mem: %.2e]'
% (itr, top1_meter.avg, loss,
torch.cuda.max_memory_allocated() / 1024.**2))
return top1_meter.avg
def load_checkpoint(
device,
r_path,
classifier,
opt,
scaler
):
try:
checkpoint = torch.load(r_path, map_location=torch.device('cpu'))
epoch = checkpoint['epoch']
# -- loading encoder
pretrained_dict = checkpoint['classifier']
msg = classifier.load_state_dict(pretrained_dict)
logger.info(f'loaded pretrained classifier from epoch {epoch} with msg: {msg}')
# -- loading optimizer
opt.load_state_dict(checkpoint['opt'])
if scaler is not None:
scaler.load_state_dict(checkpoint['scaler'])
logger.info(f'loaded optimizers from epoch {epoch}')
logger.info(f'read-path: {r_path}')
del checkpoint
except Exception as e:
logger.info(f'Encountered exception when loading checkpoint {e}')
epoch = 0
return classifier, opt, scaler, epoch
def load_pretrained(
encoder,
pretrained,
checkpoint_key='target_encoder'
):
logger.info(f'Loading pretrained model from {pretrained}')
checkpoint = torch.load(pretrained, map_location='cpu')
try:
pretrained_dict = checkpoint[checkpoint_key]
except Exception:
pretrained_dict = checkpoint['encoder']
pretrained_dict = {k.replace('module.', ''): v for k, v in pretrained_dict.items()}
pretrained_dict = {k.replace('backbone.', ''): v for k, v in pretrained_dict.items()}
for k, v in encoder.state_dict().items():
if k not in pretrained_dict:
logger.info(f'key "{k}" could not be found in loaded state dict')
elif pretrained_dict[k].shape != v.shape:
logger.info(f'key "{k}" is of different shape in model and loaded state dict')
pretrained_dict[k] = v
msg = encoder.load_state_dict(pretrained_dict, strict=False)
print(encoder)
logger.info(f'loaded pretrained model with msg: {msg}')
logger.info(f'loaded pretrained encoder from epoch: {checkpoint["epoch"]}\n path: {pretrained}')
del checkpoint
return encoder
def make_dataloader(
root_path,
batch_size,
world_size,
rank,
dataset_type='VideoDataset',
resolution=224,
frames_per_clip=16,
frame_step=4,
num_segments=8,
eval_duration=None,
num_views_per_segment=1,
allow_segment_overlap=True,
training=False,
num_workers=12,
subset_file=None
):
# Make Video Transforms
transform = make_transforms(
training=training,
num_views_per_clip=num_views_per_segment,
random_horizontal_flip=False,
random_resize_aspect_ratio=(0.75, 4/3),
random_resize_scale=(0.08, 1.0),
reprob=0.25,
auto_augment=True,
motion_shift=False,
crop_size=resolution,
)
data_loader, _ = init_data(
data=dataset_type,
root_path=root_path,
transform=transform,
batch_size=batch_size,
world_size=world_size,
rank=rank,
clip_len=frames_per_clip,
frame_sample_rate=frame_step,
duration=eval_duration,
num_clips=num_segments,
allow_clip_overlap=allow_segment_overlap,
num_workers=num_workers,
copy_data=False,
drop_last=False,
subset_file=subset_file)
return data_loader
def init_model(
device,
pretrained,
model_name,
patch_size=16,
crop_size=224,
# Video specific parameters
frames_per_clip=16,
tubelet_size=2,
use_sdpa=False,
use_SiLU=False,
tight_SiLU=True,
uniform_power=False,
checkpoint_key='target_encoder'
):
encoder = vit.__dict__[model_name](
img_size=crop_size,
patch_size=patch_size,
num_frames=frames_per_clip,
tubelet_size=tubelet_size,
uniform_power=uniform_power,
use_sdpa=use_sdpa,
use_SiLU=use_SiLU,
tight_SiLU=tight_SiLU,
)
encoder.to(device)
encoder = load_pretrained(encoder=encoder, pretrained=pretrained, checkpoint_key=checkpoint_key)
return encoder
def init_opt(
classifier,
iterations_per_epoch,
start_lr,
ref_lr,
warmup,
num_epochs,
wd=1e-6,
final_wd=1e-6,
final_lr=0.0,
use_bfloat16=False
):
param_groups = [
{
'params': (p for n, p in classifier.named_parameters()
if ('bias' not in n) and (len(p.shape) != 1))
}, {
'params': (p for n, p in classifier.named_parameters()
if ('bias' in n) or (len(p.shape) == 1)),
'WD_exclude': True,
'weight_decay': 0
}
]
logger.info('Using AdamW')
optimizer = torch.optim.AdamW(param_groups)
scheduler = WarmupCosineSchedule(
optimizer,
warmup_steps=int(warmup*iterations_per_epoch),
start_lr=start_lr,
ref_lr=ref_lr,
final_lr=final_lr,
T_max=int(num_epochs*iterations_per_epoch))
wd_scheduler = CosineWDSchedule(
optimizer,
ref_wd=wd,
final_wd=final_wd,
T_max=int(num_epochs*iterations_per_epoch))
scaler = torch.cuda.amp.GradScaler() if use_bfloat16 else None
return optimizer, scaler, scheduler, wd_scheduler