-
Notifications
You must be signed in to change notification settings - Fork 10
/
cifar_advanced.py
261 lines (215 loc) · 8.74 KB
/
cifar_advanced.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
import os
import argparse
from pathlib import Path
import torch
import torch.distributed as dist
from torch.nn import SyncBatchNorm
from torch.utils.data import DataLoader, DistributedSampler
from torch.nn.parallel import DistributedDataParallel
from torchvision import transforms
from torchvision.datasets import CIFAR10
import timm
import argus
from argus.utils import deep_to, deep_detach, deep_chunk
from argus.metrics import Metric
from argus.callbacks import (
MonitorCheckpoint,
EarlyStopping,
CosineAnnealingLR,
LoggingToCSV,
LoggingToFile
)
torch.backends.cudnn.benchmark = True
CIFAR_DATA_DIR = Path('./cifar_data')
EXPERIMENT_DIR = Path('./cifar_advanced')
def get_linear_scaled_lr(base_lr, batch_size, base_batch_size=128):
return base_lr * (batch_size / base_batch_size)
def get_data_loaders(batch_size, distributed, local_rank):
mean = (0.4914, 0.4822, 0.4465)
std = (0.2023, 0.1994, 0.2010)
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean, std),
])
test_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean, std),
])
train_dataset = CIFAR10(root=CIFAR_DATA_DIR, train=True,
transform=train_transform, download=True)
val_dataset = CIFAR10(root=CIFAR_DATA_DIR, train=False,
transform=test_transform, download=False)
train_sampler = None
val_sampler = None
if distributed:
train_sampler = DistributedSampler(train_dataset,
num_replicas=dist.get_world_size(),
rank=local_rank,
shuffle=True)
val_sampler = DistributedSampler(val_dataset,
num_replicas=dist.get_world_size(),
rank=local_rank,
shuffle=False)
train_loader = DataLoader(train_dataset, num_workers=2, drop_last=True,
batch_size=batch_size, sampler=train_sampler,
shuffle=train_sampler is None)
val_loader = DataLoader(val_dataset, num_workers=2, shuffle=False,
batch_size=batch_size * 2, sampler=val_sampler)
return train_loader, val_loader
class CategoricalAccuracy(Metric):
"""You don't need to write a distributed metric if you don't
want to validate in parallel. You can use regular metrics if you
are not using DistributedSampler for validation data loader.
"""
name = 'dist_accuracy'
better = 'max'
def __init__(self, distributed=False, world_size=1):
self.distributed = distributed
self.world_size = world_size
def reset(self):
self.correct = 0
self.count = 0
def update(self, step_output: dict):
pred = step_output['prediction']
trg = step_output['target']
indices = torch.max(pred, dim=1)[1]
correct = torch.eq(indices, trg).view(-1)
correct_sum = torch.sum(correct)
if self.distributed:
reduce_correct_sum = correct_sum.clone()
dist.all_reduce(reduce_correct_sum, op=dist.ReduceOp.SUM)
correct_sum = reduce_correct_sum
torch.cuda.synchronize()
self.correct += correct_sum.item()
self.count += correct.shape[0] * self.world_size
def compute(self):
if self.count == 0:
raise Exception('Must be at least one example for computation')
return self.correct / self.count
def initialize_amp(model,
opt_level='O1',
keep_batchnorm_fp32=None,
loss_scale='dynamic'):
from apex import amp
model.nn_module, model.optimizer = amp.initialize(
model.nn_module, model.optimizer,
opt_level=opt_level,
keep_batchnorm_fp32=keep_batchnorm_fp32,
loss_scale=loss_scale
)
model.amp = amp
class CifarModel(argus.Model):
nn_module = timm.create_model
def __init__(self, params):
super().__init__(params)
self.amp = None
if 'iter_size' not in self.params:
self.params['iter_size'] = 1
self.iter_size = self.params['iter_size']
def train_step(self, batch, state) -> dict:
self.train()
self.optimizer.zero_grad()
# Gradient accumulation
for i, chunk_batch in enumerate(deep_chunk(batch, self.iter_size)):
input, target = deep_to(chunk_batch, self.device, non_blocking=True)
prediction = self.nn_module(input)
loss = self.loss(prediction, target)
if self.amp is not None:
delay_unscale = i != (self.iter_size - 1)
# Mixed precision
with self.amp.scale_loss(loss, self.optimizer,
delay_unscale=delay_unscale) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
self.optimizer.step()
torch.cuda.synchronize()
prediction = deep_detach(prediction)
target = deep_detach(target)
prediction = self.prediction_transform(prediction)
return {
'prediction': prediction,
'target': target,
'loss': loss.item()
}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type=int, default=256,
help='input batch size for training (default: 256)')
parser.add_argument('--epochs', type=int, default=200,
help='number of epochs to train (default: 200)')
parser.add_argument('--lr', type=float, default=0.001,
help='learning rate (default: 0.001)')
parser.add_argument('--iter_size', type=int, default=1,
help='gradient accumulation step (default: 1)')
parser.add_argument('--amp', action='store_true',
help='use Apex mixed precision')
parser.add_argument("--local_rank", default=0, type=int)
args = parser.parse_args()
args.distributed = False
if 'WORLD_SIZE' in os.environ:
args.distributed = int(os.environ['WORLD_SIZE']) > 1
if args.distributed:
torch.cuda.set_device(args.local_rank)
dist.init_process_group(backend='nccl', init_method='env://')
if args.distributed:
world_size = dist.get_world_size()
args.world_batch_size = args.batch_size * world_size
else:
world_size = 1
args.world_batch_size = args.batch_size
print("World batch size:", args.world_batch_size)
train_loader, val_loader = get_data_loaders(args.batch_size,
args.distributed,
args.local_rank)
params = {
'nn_module': {
'model_name': 'tf_efficientnet_b0_ns',
'pretrained': True,
'num_classes': 10,
'drop_rate': 0.2,
'drop_path_rate': 0.2,
},
'optimizer': ('AdamW', {
'lr': get_linear_scaled_lr(args.lr, args.world_batch_size)
}),
'loss': 'CrossEntropyLoss',
'device': 'cuda',
'iter_size': args.iter_size
}
model = CifarModel(params)
if args.amp:
initialize_amp(model)
if args.distributed:
model.nn_module = SyncBatchNorm.convert_sync_batchnorm(model.nn_module)
model.nn_module = DistributedDataParallel(model.nn_module.to(args.local_rank),
device_ids=[args.local_rank],
output_device=args.local_rank)
if args.local_rank:
model.logger.disabled = True
else:
model.set_device('cuda')
callbacks = []
if args.local_rank == 0:
callbacks += [
MonitorCheckpoint(dir_path=EXPERIMENT_DIR,
monitor='val_dist_accuracy', max_saves=3),
LoggingToCSV(EXPERIMENT_DIR / 'log.csv'),
LoggingToFile(EXPERIMENT_DIR / 'log.txt')
]
callbacks += [
EarlyStopping(monitor='val_dist_accuracy', patience=30),
CosineAnnealingLR(args.epochs),
]
if args.distributed:
@argus.callbacks.on_epoch_complete
def schedule_sampler(state):
state.data_loader.sampler.set_epoch(state.epoch + 1)
callbacks += [schedule_sampler]
model.fit(train_loader,
val_loader=val_loader,
num_epochs=args.epochs,
metrics=[CategoricalAccuracy(args.distributed, world_size)],
callbacks=callbacks)