-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_cifar.py
193 lines (171 loc) · 7.26 KB
/
train_cifar.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
from argparse import ArgumentParser
from typing import List
import time
import numpy as np
from tqdm import tqdm
import torch as ch
from torch.cuda.amp import GradScaler, autocast
from torch.nn import CrossEntropyLoss
from torch.optim import SGD, lr_scheduler
import torchvision
from fastargs import get_current_config, Param, Section
from fastargs.decorators import param
from ffcv.fields.decoders import IntDecoder, SimpleRGBImageDecoder
from ffcv.loader import Loader, OrderOption
from ffcv.pipeline.operation import Operation
from ffcv.transforms import RandomHorizontalFlip, Cutout, \
RandomTranslate, Convert, ToDevice, ToTensor, ToTorchImage
from ffcv.transforms.common import Squeeze
Section('training', 'Hyperparameters').params(
lr=Param(float, 'The learning rate to use', default=0.5),
epochs=Param(int, 'Number of epochs to run for', default=24),
lr_peak_epoch=Param(int, 'Peak epoch for cyclic lr', default=5),
batch_size=Param(int, 'Batch size', default=512),
momentum=Param(float, 'Momentum for SGD', default=0.9),
weight_decay=Param(float, 'l2 weight decay', default=5e-4),
label_smoothing=Param(float, 'Value of label smoothing', default=0.1),
num_workers=Param(int, 'The number of workers', default=4),
lr_tta=Param(bool, 'Test time augmentation by averaging with horizontally flipped version', default=True)
)
Section('data', 'data related stuff').params(
train_dataset=Param(str, '.dat file to use for training',
default='/mnt/cfs/datasets/cifar_ffcv/cifar_train.beton'),
val_dataset=Param(str, '.dat file to use for validation',
default='/mnt/cfs/datasets/cifar_ffcv/cifar_val.beton'),
)
@param('data.train_dataset')
@param('data.val_dataset')
@param('training.batch_size')
@param('training.num_workers')
def make_dataloaders(train_dataset=None, val_dataset=None, batch_size=None, num_workers=None, mask=None):
paths = {
'train': train_dataset,
'test': val_dataset
}
start_time = time.time()
CIFAR_MEAN = [125.307, 122.961, 113.8575]
CIFAR_STD = [51.5865, 50.847, 51.255]
loaders = {}
for name in ['train', 'test']:
label_pipeline: List[Operation] = [IntDecoder(), ToTensor(), ToDevice('cuda:0'), Squeeze()]
image_pipeline: List[Operation] = [SimpleRGBImageDecoder()]
if name == 'train':
image_pipeline.extend([
RandomHorizontalFlip(),
RandomTranslate(padding=2, fill=tuple(map(int, CIFAR_MEAN))),
Cutout(4, tuple(map(int, CIFAR_MEAN))),
])
image_pipeline.extend([
ToTensor(),
ToDevice('cuda:0', non_blocking=True),
ToTorchImage(),
Convert(ch.float16),
torchvision.transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
])
ordering = OrderOption.RANDOM if name == 'train' else OrderOption.SEQUENTIAL
loaders[name] = Loader(paths[name], indices=(mask if name == 'train' else None),
batch_size=batch_size, num_workers=num_workers,
order=ordering, drop_last=(name == 'train'),
pipelines={'image': image_pipeline, 'label': label_pipeline})
return loaders
# Model (from KakaoBrain: https://github.com/wbaek/torchskeleton)
class Mul(ch.nn.Module):
def __init__(self, weight):
super(Mul, self).__init__()
self.weight = weight
def forward(self, x): return x * self.weight
class Flatten(ch.nn.Module):
def forward(self, x): return x.view(x.size(0), -1)
class Residual(ch.nn.Module):
def __init__(self, module):
super(Residual, self).__init__()
self.module = module
def forward(self, x): return x + self.module(x)
def conv_bn(channels_in, channels_out, kernel_size=3, stride=1, padding=1, groups=1):
return ch.nn.Sequential(
ch.nn.Conv2d(channels_in, channels_out, kernel_size=kernel_size,
stride=stride, padding=padding, groups=groups, bias=False),
ch.nn.BatchNorm2d(channels_out),
ch.nn.ReLU(inplace=True)
)
def construct_model():
num_class = 10
model = ch.nn.Sequential(
conv_bn(3, 64, kernel_size=3, stride=1, padding=1),
conv_bn(64, 128, kernel_size=5, stride=2, padding=2),
Residual(ch.nn.Sequential(conv_bn(128, 128), conv_bn(128, 128))),
conv_bn(128, 256, kernel_size=3, stride=1, padding=1),
ch.nn.MaxPool2d(2),
Residual(ch.nn.Sequential(conv_bn(256, 256), conv_bn(256, 256))),
conv_bn(256, 128, kernel_size=3, stride=1, padding=0),
ch.nn.AdaptiveMaxPool2d((1, 1)),
Flatten(),
ch.nn.Linear(128, num_class, bias=False),
Mul(0.2)
)
model = model.to(memory_format=ch.channels_last).cuda()
return model
@param('training.lr')
@param('training.epochs')
@param('training.momentum')
@param('training.weight_decay')
@param('training.label_smoothing')
@param('training.lr_peak_epoch')
def train(model, loaders, lr=None, epochs=None, label_smoothing=None,
momentum=None, weight_decay=None, lr_peak_epoch=None):
opt = SGD(model.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay)
iters_per_epoch = len(loaders['train'])
# Cyclic LR with single triangle
lr_schedule = np.interp(np.arange((epochs+1) * iters_per_epoch),
[0, lr_peak_epoch * iters_per_epoch, epochs * iters_per_epoch],
[0, 1, 0])
scheduler = lr_scheduler.LambdaLR(opt, lr_schedule.__getitem__)
scaler = GradScaler()
loss_fn = CrossEntropyLoss(label_smoothing=label_smoothing)
for _ in range(epochs):
for ims, labs in tqdm(loaders['train']):
opt.zero_grad(set_to_none=True)
with autocast():
out = model(ims)
loss = loss_fn(out, labs)
scaler.scale(loss).backward()
scaler.step(opt)
scaler.update()
scheduler.step()
@param('training.lr_tta')
def evaluate(model, loaders, lr_tta=False):
model.eval()
with ch.no_grad():
all_margins = []
for ims, labs in tqdm(loaders['test']):
with autocast():
out = model(ims)
if lr_tta:
out += model(ch.fliplr(ims))
out /= 2
class_logits = out[ch.arange(out.shape[0]), labs].clone()
out[ch.arange(out.shape[0]), labs] = -1000
next_classes = out.argmax(1)
class_logits -= out[ch.arange(out.shape[0]), next_classes]
all_margins.append(class_logits.cpu())
all_margins = ch.cat(all_margins)
print('Average margin:', all_margins.mean())
return all_margins.numpy()
def main(index, logdir):
config = get_current_config()
parser = ArgumentParser(description='Fast CIFAR-10 training')
config.augment_argparse(parser)
# Also loads from args.config_path if provided
config.collect_argparse_args(parser)
config.validate(mode='stderr')
config.summary()
mask = (np.random.rand(50_000) > 0.5)
loaders = make_dataloaders(mask=np.nonzero(mask)[0])
model = construct_model()
train(model, loaders)
res = evaluate(model, loaders)
print(mask.shape, res.shape)
return {
'masks': mask,
'margins': res
}