-
Notifications
You must be signed in to change notification settings - Fork 34
/
train_imagenet.py
505 lines (422 loc) · 17.3 KB
/
train_imagenet.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
import torch as ch
from torch.cuda.amp import GradScaler
from torch.cuda.amp import autocast
import torch.nn.functional as F
import torch.distributed as dist
ch.backends.cudnn.benchmark = True
ch.autograd.profiler.emit_nvtx(False)
ch.autograd.profiler.profile(False)
from torchvision import models
import torchmetrics
import numpy as np
from tqdm import tqdm
import os
import time
import json
from uuid import uuid4
from typing import List
from pathlib import Path
from argparse import ArgumentParser
from fastargs import get_current_config
from fastargs.decorators import param
from fastargs import Param, Section
from fastargs.validation import And, OneOf
from ffcv.pipeline.operation import Operation
from ffcv.loader import Loader, OrderOption
from ffcv.transforms import ToTensor, ToDevice, Squeeze, NormalizeImage, \
RandomHorizontalFlip, ToTorchImage
from ffcv.fields.rgb_image import CenterCropRGBImageDecoder, \
RandomResizedCropRGBImageDecoder
from ffcv.fields.basics import IntDecoder
Section('model', 'model details').params(
arch=Param(And(str, OneOf(models.__dir__())), default='resnet18'),
pretrained=Param(int, 'is pretrained? (1/0)', default=0)
)
Section('resolution', 'resolution scheduling').params(
min_res=Param(int, 'the minimum (starting) resolution', default=160),
max_res=Param(int, 'the maximum (starting) resolution', default=160),
end_ramp=Param(int, 'when to stop interpolating resolution', default=0),
start_ramp=Param(int, 'when to start interpolating resolution', default=0)
)
Section('data', 'data related stuff').params(
train_dataset=Param(str, '.dat file to use for training', required=True),
val_dataset=Param(str, '.dat file to use for validation', required=True),
num_workers=Param(int, 'The number of workers', required=True),
in_memory=Param(int, 'does the dataset fit in memory? (1/0)', required=True)
)
Section('lr', 'lr scheduling').params(
step_ratio=Param(float, 'learning rate step ratio', default=0.1),
step_length=Param(int, 'learning rate step length', default=30),
lr_schedule_type=Param(OneOf(['step', 'cyclic']), default='cyclic'),
lr=Param(float, 'learning rate', default=0.5),
lr_peak_epoch=Param(int, 'Epoch at which LR peaks', default=2),
)
Section('logging', 'how to log stuff').params(
folder=Param(str, 'log location', required=True),
log_level=Param(int, '0 if only at end 1 otherwise', default=1)
)
Section('validation', 'Validation parameters stuff').params(
batch_size=Param(int, 'The batch size for validation', default=512),
resolution=Param(int, 'final resized validation image size', default=224),
lr_tta=Param(int, 'should do lr flipping/avging at test time', default=1)
)
Section('training', 'training hyper param stuff').params(
eval_only=Param(int, 'eval only?', default=0),
batch_size=Param(int, 'The batch size', default=512),
optimizer=Param(And(str, OneOf(['sgd'])), 'The optimizer', default='sgd'),
momentum=Param(float, 'SGD momentum', default=0.9),
weight_decay=Param(float, 'weight decay', default=4e-5),
epochs=Param(int, 'number of epochs', default=30),
label_smoothing=Param(float, 'label smoothing parameter', default=0.1),
distributed=Param(int, 'is distributed?', default=0),
use_blurpool=Param(int, 'use blurpool?', default=0)
)
Section('dist', 'distributed training options').params(
world_size=Param(int, 'number gpus', default=1),
address=Param(str, 'address', default='localhost'),
port=Param(str, 'port', default='12355')
)
IMAGENET_MEAN = np.array([0.485, 0.456, 0.406]) * 255
IMAGENET_STD = np.array([0.229, 0.224, 0.225]) * 255
DEFAULT_CROP_RATIO = 224/256
@param('lr.lr')
@param('lr.step_ratio')
@param('lr.step_length')
@param('training.epochs')
def get_step_lr(epoch, lr, step_ratio, step_length, epochs):
if epoch >= epochs:
return 0
num_steps = epoch // step_length
return step_ratio**num_steps * lr
@param('lr.lr')
@param('training.epochs')
@param('lr.lr_peak_epoch')
def get_cyclic_lr(epoch, lr, epochs, lr_peak_epoch):
xs = [0, lr_peak_epoch, epochs]
ys = [1e-4 * lr, lr, 0]
return np.interp([epoch], xs, ys)[0]
class BlurPoolConv2d(ch.nn.Module):
def __init__(self, conv):
super().__init__()
default_filter = ch.tensor([[[[1, 2, 1], [2, 4, 2], [1, 2, 1]]]]) / 16.0
filt = default_filter.repeat(conv.in_channels, 1, 1, 1)
self.conv = conv
self.register_buffer('blur_filter', filt)
def forward(self, x):
blurred = F.conv2d(x, self.blur_filter, stride=1, padding=(1, 1),
groups=self.conv.in_channels, bias=None)
return self.conv.forward(blurred)
class ImageNetTrainer:
@param('training.distributed')
def __init__(self, gpu, distributed):
self.all_params = get_current_config()
self.gpu = gpu
self.uid = str(uuid4())
if distributed:
self.setup_distributed()
self.train_loader = self.create_train_loader()
self.val_loader = self.create_val_loader()
self.model, self.scaler = self.create_model_and_scaler()
self.create_optimizer()
self.initialize_logger()
@param('dist.address')
@param('dist.port')
@param('dist.world_size')
def setup_distributed(self, address, port, world_size):
os.environ['MASTER_ADDR'] = address
os.environ['MASTER_PORT'] = port
dist.init_process_group("nccl", rank=self.gpu, world_size=world_size)
ch.cuda.set_device(self.gpu)
def cleanup_distributed(self):
dist.destroy_process_group()
@param('lr.lr_schedule_type')
def get_lr(self, epoch, lr_schedule_type):
lr_schedules = {
'cyclic': get_cyclic_lr,
'step': get_step_lr
}
return lr_schedules[lr_schedule_type](epoch)
# resolution tools
@param('resolution.min_res')
@param('resolution.max_res')
@param('resolution.end_ramp')
@param('resolution.start_ramp')
def get_resolution(self, epoch, min_res, max_res, end_ramp, start_ramp):
assert min_res <= max_res
if epoch <= start_ramp:
return min_res
if epoch >= end_ramp:
return max_res
# otherwise, linearly interpolate to the nearest multiple of 32
interp = np.interp([epoch], [start_ramp, end_ramp], [min_res, max_res])
final_res = int(np.round(interp[0] / 32)) * 32
return final_res
@param('training.momentum')
@param('training.optimizer')
@param('training.weight_decay')
@param('training.label_smoothing')
def create_optimizer(self, momentum, optimizer, weight_decay,
label_smoothing):
assert optimizer == 'sgd'
# Only do weight decay on non-batchnorm parameters
all_params = list(self.model.named_parameters())
bn_params = [v for k, v in all_params if ('bn' in k)]
other_params = [v for k, v in all_params if not ('bn' in k)]
param_groups = [{
'params': bn_params,
'weight_decay': 0.
}, {
'params': other_params,
'weight_decay': weight_decay
}]
self.optimizer = ch.optim.SGD(param_groups, lr=1, momentum=momentum)
self.loss = ch.nn.CrossEntropyLoss(label_smoothing=label_smoothing)
@param('data.train_dataset')
@param('data.num_workers')
@param('training.batch_size')
@param('training.distributed')
@param('data.in_memory')
def create_train_loader(self, train_dataset, num_workers, batch_size,
distributed, in_memory):
this_device = f'cuda:{self.gpu}'
train_path = Path(train_dataset)
assert train_path.is_file()
res = self.get_resolution(epoch=0)
self.decoder = RandomResizedCropRGBImageDecoder((res, res))
image_pipeline: List[Operation] = [
self.decoder,
RandomHorizontalFlip(),
ToTensor(),
ToDevice(ch.device(this_device), non_blocking=True),
ToTorchImage(),
NormalizeImage(IMAGENET_MEAN, IMAGENET_STD, np.float16)
]
label_pipeline: List[Operation] = [
IntDecoder(),
ToTensor(),
Squeeze(),
ToDevice(ch.device(this_device), non_blocking=True)
]
order = OrderOption.RANDOM if distributed else OrderOption.QUASI_RANDOM
loader = Loader(train_dataset,
batch_size=batch_size,
num_workers=num_workers,
order=order,
os_cache=in_memory,
drop_last=True,
pipelines={
'image': image_pipeline,
'label': label_pipeline
},
distributed=distributed)
return loader
@param('data.val_dataset')
@param('data.num_workers')
@param('validation.batch_size')
@param('validation.resolution')
@param('training.distributed')
def create_val_loader(self, val_dataset, num_workers, batch_size,
resolution, distributed):
this_device = f'cuda:{self.gpu}'
val_path = Path(val_dataset)
assert val_path.is_file()
res_tuple = (resolution, resolution)
cropper = CenterCropRGBImageDecoder(res_tuple, ratio=DEFAULT_CROP_RATIO)
image_pipeline = [
cropper,
ToTensor(),
ToDevice(ch.device(this_device), non_blocking=True),
ToTorchImage(),
NormalizeImage(IMAGENET_MEAN, IMAGENET_STD, np.float16)
]
label_pipeline = [
IntDecoder(),
ToTensor(),
Squeeze(),
ToDevice(ch.device(this_device),
non_blocking=True)
]
loader = Loader(val_dataset,
batch_size=batch_size,
num_workers=num_workers,
order=OrderOption.SEQUENTIAL,
drop_last=False,
pipelines={
'image': image_pipeline,
'label': label_pipeline
},
distributed=distributed)
return loader
@param('training.epochs')
@param('logging.log_level')
def train(self, epochs, log_level):
for epoch in range(epochs):
res = self.get_resolution(epoch)
self.decoder.output_size = (res, res)
train_loss = self.train_loop(epoch)
if log_level > 0:
extra_dict = {
'train_loss': train_loss,
'epoch': epoch
}
self.eval_and_log(extra_dict)
self.eval_and_log({'epoch':epoch})
if self.gpu == 0:
ch.save(self.model.state_dict(), self.log_folder / 'final_weights.pt')
def eval_and_log(self, extra_dict={}):
start_val = time.time()
stats = self.val_loop()
val_time = time.time() - start_val
if self.gpu == 0:
self.log(dict({
'current_lr': self.optimizer.param_groups[0]['lr'],
'top_1': stats['top_1'],
'top_5': stats['top_5'],
'val_time': val_time
}, **extra_dict))
return stats
@param('model.arch')
@param('model.pretrained')
@param('training.distributed')
@param('training.use_blurpool')
def create_model_and_scaler(self, arch, pretrained, distributed, use_blurpool):
scaler = GradScaler()
model = getattr(models, arch)(pretrained=pretrained)
def apply_blurpool(mod: ch.nn.Module):
for (name, child) in mod.named_children():
if isinstance(child, ch.nn.Conv2d) and (np.max(child.stride) > 1 and child.in_channels >= 16):
setattr(mod, name, BlurPoolConv2d(child))
else: apply_blurpool(child)
if use_blurpool: apply_blurpool(model)
model = model.to(memory_format=ch.channels_last)
model = model.to(self.gpu)
if distributed:
model = ch.nn.parallel.DistributedDataParallel(model, device_ids=[self.gpu])
return model, scaler
@param('logging.log_level')
def train_loop(self, epoch, log_level):
model = self.model
model.train()
losses = []
lr_start, lr_end = self.get_lr(epoch), self.get_lr(epoch + 1)
iters = len(self.train_loader)
lrs = np.interp(np.arange(iters), [0, iters], [lr_start, lr_end])
iterator = tqdm(self.train_loader)
for ix, (images, target) in enumerate(iterator):
### Training start
for param_group in self.optimizer.param_groups:
param_group['lr'] = lrs[ix]
self.optimizer.zero_grad(set_to_none=True)
with autocast():
output = self.model(images)
loss_train = self.loss(output, target)
self.scaler.scale(loss_train).backward()
self.scaler.step(self.optimizer)
self.scaler.update()
### Training end
### Logging start
if log_level > 0:
losses.append(loss_train.detach())
group_lrs = []
for _, group in enumerate(self.optimizer.param_groups):
group_lrs.append(f'{group["lr"]:.3f}')
names = ['ep', 'iter', 'shape', 'lrs']
values = [epoch, ix, tuple(images.shape), group_lrs]
if log_level > 1:
names += ['loss']
values += [f'{loss_train.item():.3f}']
msg = ', '.join(f'{n}={v}' for n, v in zip(names, values))
iterator.set_description(msg)
### Logging end
@param('validation.lr_tta')
def val_loop(self, lr_tta):
model = self.model
model.eval()
with ch.no_grad():
with autocast():
for images, target in tqdm(self.val_loader):
output = self.model(images)
if lr_tta:
output += self.model(ch.flip(images, dims=[3]))
for k in ['top_1', 'top_5']:
self.val_meters[k](output, target)
loss_val = self.loss(output, target)
self.val_meters['loss'](loss_val)
stats = {k: m.compute().item() for k, m in self.val_meters.items()}
[meter.reset() for meter in self.val_meters.values()]
return stats
@param('logging.folder')
def initialize_logger(self, folder):
self.val_meters = {
'top_1': torchmetrics.Accuracy(task='multiclass', num_classes=1000, compute_on_step=False).to(self.gpu),
'top_5': torchmetrics.Accuracy(task='multiclass', num_classes=1000, compute_on_step=False, top_k=5).to(self.gpu),
'loss': MeanScalarMetric(compute_on_step=False).to(self.gpu)
}
if self.gpu == 0:
folder = (Path(folder) / str(self.uid)).absolute()
folder.mkdir(parents=True)
self.log_folder = folder
self.start_time = time.time()
print(f'=> Logging in {self.log_folder}')
params = {
'.'.join(k): self.all_params[k] for k in self.all_params.entries.keys()
}
with open(folder / 'params.json', 'w+') as handle:
json.dump(params, handle)
def log(self, content):
print(f'=> Log: {content}')
if self.gpu != 0: return
cur_time = time.time()
with open(self.log_folder / 'log', 'a+') as fd:
fd.write(json.dumps({
'timestamp': cur_time,
'relative_time': cur_time - self.start_time,
**content
}) + '\n')
fd.flush()
@classmethod
@param('training.distributed')
@param('dist.world_size')
def launch_from_args(cls, distributed, world_size):
if distributed:
ch.multiprocessing.spawn(cls._exec_wrapper, nprocs=world_size, join=True)
else:
cls.exec(0)
@classmethod
def _exec_wrapper(cls, *args, **kwargs):
make_config(quiet=True)
cls.exec(*args, **kwargs)
@classmethod
@param('training.distributed')
@param('training.eval_only')
def exec(cls, gpu, distributed, eval_only):
trainer = cls(gpu=gpu)
if eval_only:
trainer.eval_and_log()
else:
trainer.train()
if distributed:
trainer.cleanup_distributed()
# Utils
class MeanScalarMetric(torchmetrics.Metric):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_state('sum', default=ch.tensor(0.), dist_reduce_fx='sum')
self.add_state('count', default=ch.tensor(0), dist_reduce_fx='sum')
def update(self, sample: ch.Tensor):
self.sum += sample.sum()
self.count += sample.numel()
def compute(self):
return self.sum.float() / self.count
# Running
def make_config(quiet=False):
config = get_current_config()
parser = ArgumentParser(description='Fast imagenet training')
config.augment_argparse(parser)
config.collect_argparse_args(parser)
config.validate(mode='stderr')
if not quiet:
config.summary()
if __name__ == "__main__":
make_config()
ImageNetTrainer.launch_from_args()