This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.py
349 lines (301 loc) · 11 KB
/
main.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
# Copyright (c) Facebook, Inc. and its 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
import shutil
import time
from logging import getLogger
# Import torch and other dependecies
import numpy as np
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.distributed as dist
import torch.optim
from torch.utils.tensorboard import SummaryWriter
from datasets.AVideoDataset import AVideoDataset
from model import load_model
from opt import parse_arguments
from src.sk_utils import cluster
from src.warmup_scheduler import GradualWarmupScheduler
from utils import (
initialize_exp,
restart_from_checkpoint,
fix_random_seeds,
AverageMeter,
init_distributed_mode,
init_signal_handler,
trigger_job_requeue,
get_loss,
warmup_batchnorm
)
logger = getLogger()
# global variables
sk_schedule = None
group = None
global sk_counter
sk_counter = 0
def main():
# parse arguments
global args
parser = parse_arguments()
args = parser.parse_args()
# exp setup: logger, distributed mode and seeds
init_distributed_mode(args)
init_signal_handler()
fix_random_seeds(args.seed)
logger, training_stats = initialize_exp(args, "epoch", "loss")
if args.rank == 0:
writer = SummaryWriter(args.dump_path)
else:
writer = None
# build data
train_dataset = AVideoDataset(
ds_name=args.ds_name,
root_dir=args.root_dir,
mode='train',
path_to_data_dir=args.data_path,
num_frames=args.num_frames,
target_fps=args.target_fps,
sample_rate=args.sample_rate,
num_train_clips=args.num_train_clips,
train_crop_size=args.train_crop_size,
test_crop_size=args.test_crop_size,
num_data_samples=args.num_data_samples,
colorjitter=args.colorjitter,
use_grayscale=args.use_grayscale,
use_gaussian=args.use_gaussian,
temp_jitter=True,
decode_audio=True,
aug_audio=None,
num_sec=args.num_sec_aud,
aud_sample_rate=args.aud_sample_rate,
aud_spec_type=args.aud_spec_type,
use_volume_jittering=args.use_volume_jittering,
use_temporal_jittering=args.use_audio_temp_jittering,
z_normalize=args.z_normalize,
dual_data=args.dual_data
)
sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)
train_loader = torch.utils.data.DataLoader(
train_dataset,
sampler=sampler,
batch_size=args.batch_size,
num_workers=args.workers,
pin_memory=True,
drop_last=True
)
logger.info("Loaded data with {} videos.".format(len(train_dataset)))
# Load model
model = load_model(
vid_base_arch=args.vid_base_arch,
aud_base_arch=args.aud_base_arch,
use_mlp=args.use_mlp,
num_classes=args.mlp_dim,
pretrained=False,
norm_feat=False,
use_max_pool=False,
headcount=args.headcount,
)
# synchronize batch norm layers
if args.sync_bn == "pytorch":
model = nn.SyncBatchNorm.convert_sync_batchnorm(model)
elif args.sync_bn == "apex":
process_group = None
if args.world_size // 8 > 0:
process_group = apex.parallel.create_syncbn_process_group(args.world_size // 8)
model = apex.parallel.convert_syncbn_model(model, process_group=process_group)
# copy model to GPU
model = model.cuda()
if args.rank == 0:
logger.info(model)
logger.info("Building model done.")
# build optimizer
optimizer = torch.optim.SGD(
model.parameters(),
lr=args.base_lr,
momentum=0.9,
weight_decay=args.wd,
)
if args.use_warmup_scheduler:
lr_scheduler = GradualWarmupScheduler(
optimizer,
multiplier=args.world_size,
total_epoch=args.warmup_epochs,
after_scheduler=None
)
else:
lr_scheduler = None
logger.info("Building optimizer done.")
# init mixed precision
if args.use_fp16:
model, optimizer = apex.amp.initialize(model, optimizer, opt_level="O1")
logger.info("Initializing mixed precision done.")
# wrap model
model = nn.parallel.DistributedDataParallel(
model,
device_ids=[args.gpu_to_work_on],
find_unused_parameters=True,
)
# SK-Init
N_dl = len(train_loader)
N = len(train_loader.dataset)
N_distr = N_dl * train_loader.batch_size
selflabels = torch.zeros((N, args.headcount), dtype=torch.long, device='cuda')
global sk_schedule
sk_schedule = (args.epochs * N_dl * (np.linspace(0, 1, args.nopts) ** args.schedulepower)[::-1]).tolist()
# to make sure we don't make it empty
sk_schedule = [(args.epochs + 2) * N_dl] + sk_schedule
logger.info(f'remaining SK opts @ epochs {[np.round(1.0 * t / N_dl, 2) for t in sk_schedule]}')
# optionally resume from a checkpoint
to_restore = {"epoch": 0, 'selflabels': selflabels, 'dist':args.dist}
restart_from_checkpoint(
os.path.join(args.dump_path, "checkpoint.pth.tar"),
run_variables=to_restore,
model=model,
optimizer=optimizer,
amp=apex.amp if args.use_fp16 else None,
)
start_epoch = to_restore["epoch"]
selflabels = to_restore["selflabels"]
args.dist = to_restore["dist"]
# Set CuDNN benhcmark
cudnn.benchmark = True
# Restart schedule correctly
if start_epoch != 0:
include = [(qq / N_dl > start_epoch) for qq in sk_schedule]
# (total number of sk-opts) - (number of sk-opts outstanding)
global sk_counter
sk_counter = len(sk_schedule) - sum(include)
sk_schedule = (np.array(sk_schedule)[include]).tolist()
if lr_scheduler:
[lr_scheduler.step() for _ in range(to_restore['epoch'])]
if start_epoch == 0:
train_loader.sampler.set_epoch(999)
warmup_batchnorm(args, model, train_loader, batches=20, group=group)
for epoch in range(start_epoch, args.epochs):
# train the network for one epoch
logger.info("============ Starting epoch %i ... ============" % epoch)
if writer:
writer.add_scalar('train/epoch', epoch, epoch)
# set sampler
train_loader.sampler.set_epoch(epoch)
# train the network
scores, selflabels = train(
train_loader, model, optimizer, epoch, writer, selflabels)
training_stats.update(scores)
# Update LR scheduler
if lr_scheduler:
lr_scheduler.step()
# save checkpoints
if args.rank == 0:
save_dict = {
"epoch": epoch + 1,
"dist": args.dist,
"model": model.state_dict(),
"optimizer": optimizer.state_dict(),
"selflabels": selflabels
}
if args.use_fp16:
save_dict["amp"] = apex.amp.state_dict()
torch.save(
save_dict,
os.path.join(args.dump_path, "checkpoint.pth.tar"),
)
if epoch % args.checkpoint_freq == 0 or epoch == args.epochs - 1:
shutil.copyfile(
os.path.join(args.dump_path, "checkpoint.pth.tar"),
os.path.join(args.dump_checkpoints, "ckp-" + str(epoch) + ".pth")
)
def train(train_loader, model, optimizer, epoch, writer, selflabels):
global sk_schedule
global sk_counter
# Put model in train mode
model.train()
# Init Logger meters
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
world_size = args.world_size
dataset_bs = train_loader.batch_size
end = time.time()
batches_thusfar = epoch * len(train_loader)
for it, inputs in enumerate(train_loader):
# measure data loading time
data_time.update(time.time() - end)
# ============ Get inputs ... ============
video, audio, _, selected, _ = inputs
video, audio = video.cuda(), audio.cuda()
# ============ Occasional clustering via Sinkhorn-Knopp ... ===========
if batches_thusfar + it >= sk_schedule[-1]:
# optimize labels
with torch.no_grad():
_ = sk_schedule.pop()
selflabels = cluster(
args, selflabels, train_loader.dataset, model, sk_counter,
logger, writer, group,
(batches_thusfar + it) * dataset_bs * world_size
)
# ============ forward passes ... ============
feat_v, feat_a = model(video, audio)
# ============ SeLaVi loss ... ============
if args.headcount == 1:
labels = selflabels[selected, 0]
else:
labels = selflabels[selected, :]
loss_vid = get_loss(feat_v, labels, headcount=args.headcount)
loss_aud = get_loss(feat_a, labels, headcount=args.headcount)
loss = 0.5 * loss_vid + 0.5 * loss_aud
# ============ backward and optim step ... ============
optimizer.zero_grad()
if args.use_fp16:
with apex.amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
optimizer.step()
# ============ misc ... ============
losses.update(loss.item(), inputs[0].size(0))
batch_time.update(time.time() - end)
end = time.time()
iteration = epoch * len(train_loader) + it
if args.rank == 0 and it % 50 == 0:
logger.info(
"Epoch: [{0}][{1}]\t"
"Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t"
"Data {data_time.val:.3f} ({data_time.avg:.3f})\t"
"Loss {loss.val:.4f} ({loss.avg:.4f})\t"
"Lr: {lr:.4f}".format(
epoch,
it,
batch_time=batch_time,
data_time=data_time,
loss=losses,
lr=optimizer.param_groups[0]["lr"],
)
)
# Log onto tensorboard
if writer:
writer.add_scalar(
f'loss/iter', loss.item(), iteration)
writer.add_scalar(
f'lr/iter', optimizer.param_groups[0]["lr"], iteration)
writer.add_scalar(
f'batch_time/iter', batch_time.avg, iteration)
writer.add_scalar(
f'data_time/iter', data_time.avg, iteration)
# ============ signal handling ... ============
if os.environ['SIGNAL_RECEIVED'] == 'True':
if args.rank == 0:
logger.info("Beginning reqeue")
trigger_job_requeue(
os.path.join(args.dump_path, "checkpoint.pth.tar"))
dist.barrier()
torch.cuda.empty_cache()
return (epoch, losses.avg), selflabels
if __name__ == "__main__":
main()