-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathairbench_94_code.py
More file actions
423 lines (361 loc) · 16.3 KB
/
airbench_94_code.py
File metadata and controls
423 lines (361 loc) · 16.3 KB
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
import os
import sys
import uuid
from math import ceil
import torch
from torch import nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as T
# Enable CuDNN Benchmark for speed [cite: 551]
torch.backends.cudnn.benchmark = True
#############################################
# Setup/Hyperparameters [cite: 536-618]
#############################################
hyp = {
'opt': {
'train_epochs': 9.9,
'batch_size': 1024,
'lr': 11.5,
'momentum': 0.85,
'weight_decay': 0.0153,
'bias_scaler': 64.0, # scales up learning rate for BatchNorm biases
'label_smoothing': 0.2,
'whiten_bias_epochs': 3, # epochs to train whitening bias before freezing
},
'aug': {
'flip': True,
'translate': 2,
},
'net': {
'widths': {
'block1': 64,
'block2': 256,
'block3': 256,
},
'batchnorm_momentum': 0.6,
'scaling_factor': 1.0 / 9.0,
'tta_level': 2, # 0=none, 1=mirror, 2=mirror+translate
},
}
#############################################
# Data Constants [cite: 622-626]
#############################################
CIFAR_MEAN = torch.tensor((0.4914, 0.4822, 0.4465))
CIFAR_STD = torch.tensor((0.2470, 0.2435, 0.2616))
#############################################
# Augmentation Helpers [cite: 627-666]
#############################################
def batch_flip_lr(inputs):
flip_mask = (torch.rand(len(inputs), device=inputs.device) < 0.5).view(-1, 1, 1, 1)
return torch.where(flip_mask, inputs.flip(-1), inputs)
def batch_crop(images, crop_size):
r = (images.size(-1) - crop_size) // 2
shifts = torch.randint(-r, r + 1, size=(len(images), 2), device=images.device)
images_out = torch.empty((len(images), 3, crop_size, crop_size), device=images.device, dtype=images.dtype)
# Optimized cropping for small r
if r <= 2:
for sy in range(-r, r + 1):
for sx in range(-r, r + 1):
mask = (shifts[:, 0] == sy) & (shifts[:, 1] == sx)
images_out[mask] = images[mask, :, r+sy:r+sy+crop_size, r+sx:r+sx+crop_size]
else:
images_tmp = torch.empty((len(images), 3, crop_size, crop_size + 2 * r), device=images.device, dtype=images.dtype)
for s in range(-r, r + 1):
mask = (shifts[:, 0] == s)
images_tmp[mask] = images[mask, :, r+s:r+s+crop_size, :]
for s in range(-r, r + 1):
mask = (shifts[:, 1] == s)
images_out[mask] = images_tmp[mask, :, :, r+s:r+s+crop_size]
return images_out
#############################################
# DataLoader [cite: 667-798]
#############################################
class CifarLoader:
def __init__(self, path, train=True, batch_size=500, aug=None, drop_last=None, shuffle=None, gpu=0):
data_path = os.path.join(path, 'train.pt' if train else 'test.pt')
if not os.path.exists(data_path):
dset = torchvision.datasets.CIFAR10(path, download=True, train=train)
images = torch.tensor(dset.data)
labels = torch.tensor(dset.targets)
torch.save({'images': images, 'labels': labels, 'classes': dset.classes}, data_path)
data = torch.load(data_path, map_location=torch.device(gpu))
self.images, self.labels, self.classes = data['images'], data['labels'], data['classes']
# Load as FP16 CHW
self.images = (self.images.half() / 255).permute(0, 3, 1, 2).to(memory_format=torch.channels_last)
self.normalize = T.Normalize(CIFAR_MEAN, CIFAR_STD)
self.proc_images = {}
self.epoch = 0
self.aug = aug or {}
self.batch_size = batch_size
self.drop_last = train if drop_last is None else drop_last
self.shuffle = train if shuffle is None else shuffle
def __len__(self):
return len(self.images) // self.batch_size if self.drop_last else ceil(len(self.images) / self.batch_size)
def __iter__(self):
# Pre-process on first epoch
if self.epoch == 0:
images = self.proc_images['norm'] = self.normalize(self.images)
# Randomly flip all images on the first epoch (Alternating Flip logic)
if self.aug.get('flip', False):
images = self.proc_images['flip'] = batch_flip_lr(images)
# Pre-pad for translation
pad = self.aug.get('translate', 0)
if pad > 0:
self.proc_images['pad'] = F.pad(images, (pad,) * 4, 'reflect')
# Select base images for this epoch
if self.aug.get('translate', 0) > 0:
images = batch_crop(self.proc_images['pad'], self.images.shape[-2])
elif self.aug.get('flip', False):
images = self.proc_images['flip']
else:
images = self.proc_images['norm']
# Alternating Flip: Flip inputs on odd epochs [cite: 133-136]
if self.aug.get('flip', False) and self.epoch % 2 == 1:
images = images.flip(-1)
self.epoch += 1
indices = (torch.randperm if self.shuffle else torch.arange)(len(images), device=images.device)
for i in range(len(self)):
idxs = indices[i * self.batch_size : (i + 1) * self.batch_size]
yield (images[idxs], self.labels[idxs])
#############################################
# Network Components [cite: 799-950]
#############################################
class Flatten(nn.Module):
def forward(self, x): return x.view(x.size(0), -1)
class Mul(nn.Module):
def __init__(self, scale):
super().__init__()
self.scale = scale
def forward(self, x): return x * self.scale
class BatchNorm(nn.BatchNorm2d):
def __init__(self, num_features, momentum, eps=1e-12, weight=False, bias=True):
super().__init__(num_features, eps=eps, momentum=1-momentum)
self.weight.requires_grad = weight
self.bias.requires_grad = bias
class Conv(nn.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size=3, padding='same', bias=False):
super().__init__(in_channels, out_channels, kernel_size=kernel_size, padding=padding, bias=bias)
def reset_parameters(self):
super().reset_parameters()
if self.bias is not None: self.bias.data.zero_()
w = self.weight.data
torch.nn.init.dirac_(w[:w.size(1)])
class ConvGroup(nn.Module):
def __init__(self, channels_in, channels_out, batchnorm_momentum):
super().__init__()
self.conv1 = Conv(channels_in, channels_out)
self.pool = nn.MaxPool2d(2)
self.norm1 = BatchNorm(channels_out, batchnorm_momentum)
self.conv2 = Conv(channels_out, channels_out)
self.norm2 = BatchNorm(channels_out, batchnorm_momentum)
self.activ = nn.GELU()
def forward(self, x):
x = self.conv1(x)
x = self.pool(x)
x = self.norm1(x)
x = self.activ(x)
x = self.conv2(x)
x = self.norm2(x)
x = self.activ(x)
return x
def make_net(widths, batchnorm_momentum):
whiten_kernel_size = 2
whiten_width = 2 * 3 * whiten_kernel_size**2
net = nn.Sequential(
Conv(3, whiten_width, whiten_kernel_size, padding=0, bias=True),
nn.GELU(),
ConvGroup(whiten_width, widths['block1'], batchnorm_momentum),
ConvGroup(widths['block1'], widths['block2'], batchnorm_momentum),
ConvGroup(widths['block2'], widths['block3'], batchnorm_momentum),
nn.MaxPool2d(3),
Flatten(),
nn.Linear(widths['block3'], 10, bias=False),
Mul(hyp['net']['scaling_factor']),
)
net[0].weight.requires_grad = False
net = net.half().cuda()
net = net.to(memory_format=torch.channels_last)
for mod in net.modules():
if isinstance(mod, BatchNorm): mod.float()
return net
#############################################
# Whitening Initialization [cite: 953-982]
#############################################
def get_patches(x, patch_shape):
c, (h, w) = x.shape[1], patch_shape
return x.unfold(2, h, 1).unfold(3, w, 1).transpose(1, 3).reshape(-1, c, h, w).float()
def get_whitening_parameters(patches):
n, c, h, w = patches.shape
patches_flat = patches.view(n, -1)
est_patch_covariance = (patches_flat.T @ patches_flat) / n
eigenvalues, eigenvectors = torch.linalg.eigh(est_patch_covariance, UPLO='U')
return eigenvalues.flip(0).view(-1, 1, 1, 1), eigenvectors.T.reshape(c * h * w, c, h, w).flip(0)
def init_whitening_conv(layer, train_set, eps=5e-4):
patches = get_patches(train_set, patch_shape=layer.weight.data.shape[2:])
eigenvalues, eigenvectors = get_whitening_parameters(patches)
eigenvectors_scaled = eigenvectors / torch.sqrt(eigenvalues + eps)
layer.weight.data[:] = torch.cat((eigenvectors_scaled, -eigenvectors_scaled))
#############################################
# Optimization [cite: 989-1007]
#############################################
class LookaheadState:
def __init__(self, net):
self.net_ema = {k: v.clone() for k, v in net.state_dict().items()}
def update(self, net, decay):
for ema_param, net_param in zip(self.net_ema.values(), net.state_dict().values()):
if net_param.dtype in (torch.half, torch.float):
ema_param.lerp_(net_param, 1 - decay)
net_param.copy_(ema_param)
#############################################
# Logging [cite: 1013-1061]
#############################################
def print_columns(columns_list, is_head=False, is_final_entry=False):
print_string = ""
for col in columns_list:
print_string += "%s " % col
if is_head:
print("-" * len(print_string))
print(print_string)
if is_head or is_final_entry:
print("-" * len(print_string))
logging_columns_list = ['run', 'epoch', 'train_loss', 'train_acc', 'val_acc', 'tta_val_acc', 'total_time_seconds']
def print_training_details(variables, is_final_entry):
formatted = []
for col in logging_columns_list:
var = variables.get(col.strip(), None)
if type(var) in (int, str):
res = str(var)
elif type(var) is float:
res = "{:0.4f}".format(var)
else:
res = ""
formatted.append(res.rjust(len(col)))
print_columns(formatted, is_final_entry=is_final_entry)
#############################################
# Inference [cite: 1066-1127]
#############################################
def infer(model, loader, tta_level=0):
def infer_basic(inputs, net): return net(inputs).clone()
def infer_mirror(inputs, net): return 0.5 * net(inputs) + 0.5 * net(inputs.flip(-1))
def infer_mirror_translate(inputs, net):
logits = infer_mirror(inputs, net)
pad = 1
padded_inputs = F.pad(inputs, (pad,) * 4, 'reflect')
inputs_translate_list = [
padded_inputs[:, :, 0:32, 0:32],
padded_inputs[:, :, 0:32, 2:34],
padded_inputs[:, :, 2:34, 0:32],
padded_inputs[:, :, 2:34, 2:34],
]
logits_translate = torch.stack([infer_mirror(i, net) for i in inputs_translate_list]).mean(0)
return 0.5 * logits + 0.5 * logits_translate
model.eval()
test_images = loader.normalize(loader.images)
infer_fn = [infer_basic, infer_mirror, infer_mirror_translate][tta_level]
with torch.no_grad():
return torch.cat([infer_fn(inputs, model) for inputs in test_images.split(2000)])
def evaluate(model, loader, tta_level=0):
logits = infer(model, loader, tta_level)
return (logits.argmax(1) == loader.labels).float().mean().item()
#############################################
# Main Training Loop [cite: 1133-1380]
#############################################
def main(run):
batch_size = hyp['opt']['batch_size']
epochs = hyp['opt']['train_epochs']
# Optimizer Setup
momentum = hyp['opt']['momentum']
kilostep_scale = 1024 * (1 + 1 / (1 - momentum))
lr = hyp['opt']['lr'] / kilostep_scale
wd = hyp['opt']['weight_decay'] * batch_size / kilostep_scale
lr_biases = hyp['opt']['lr'] * hyp['opt']['bias_scaler'] / kilostep_scale # Paper logic implies scaling
test_loader = CifarLoader('cifar10', train=False, batch_size=2000)
train_loader = CifarLoader('cifar10', train=True, batch_size=batch_size, aug=hyp['aug'])
# Warmup
if run == 'warmup':
train_loader.labels = torch.randint(0, 10, size=(len(train_loader.labels),), device=train_loader.labels.device)
model = make_net(hyp['net']['widths'], hyp['net']['batchnorm_momentum'])
# Param groups
norm_biases = [p for k, p in model.named_parameters() if 'norm' in k and p.requires_grad]
other_params = [p for k, p in model.named_parameters() if 'norm' not in k and p.requires_grad]
param_configs = [
dict(params=norm_biases, lr=lr_biases, weight_decay=wd/lr_biases), # Note: wd scaled by inverse LR for consistent decay
dict(params=other_params, lr=lr, weight_decay=wd/lr)
]
optimizer = torch.optim.SGD(param_configs, momentum=momentum, nesterov=True)
# Scheduler
total_train_steps = ceil(len(train_loader) * epochs)
def triangle(steps, start=0.2, end=0.07, peak=0.23):
xp = torch.tensor([0, int(peak * steps), steps])
fp = torch.tensor([start, 1, end])
x = torch.arange(1 + steps)
m = (fp[1:] - fp[:-1]) / (xp[1:] - xp[:-1])
b = fp[:-1] - (m * xp[:-1])
indices = torch.sum(torch.ge(x[:, None], xp[None, :]), 1) - 1
indices = torch.clamp(indices, 0, len(m) - 1)
return m[indices] * x + b[indices]
lr_schedule = triangle(total_train_steps)
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lambda i: lr_schedule[i])
lookahead_state = LookaheadState(model)
# Timing
starter = torch.cuda.Event(enable_timing=True)
ender = torch.cuda.Event(enable_timing=True)
total_time_seconds = 0.0
# Whitening Init
starter.record()
train_images = train_loader.normalize(train_loader.images[:5000])
init_whitening_conv(model[0], train_images)
ender.record()
torch.cuda.synchronize()
total_time_seconds += 1e-3 * starter.elapsed_time(ender)
# Training
current_steps = 0
alpha_schedule = 0.95**5 * (torch.arange(total_train_steps + 1) / total_train_steps)**3
loss_fn = nn.CrossEntropyLoss(label_smoothing=hyp['opt']['label_smoothing'], reduction='none')
for epoch in range(ceil(epochs)):
model[0].bias.requires_grad = (epoch < hyp['opt']['whiten_bias_epochs'])
starter.record()
model.train()
for inputs, labels in train_loader:
outputs = model(inputs)
loss = loss_fn(outputs, labels).sum()
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
scheduler.step()
current_steps += 1
if current_steps % 5 == 0:
lookahead_state.update(model, decay=alpha_schedule[current_steps].item())
if current_steps >= total_train_steps:
if lookahead_state is not None:
lookahead_state.update(model, decay=1.0)
break
ender.record()
torch.cuda.synchronize()
total_time_seconds += 1e-3 * starter.elapsed_time(ender)
# Eval
train_acc = (outputs.detach().argmax(1) == labels).float().mean().item()
train_loss = loss.item() / batch_size
val_acc = evaluate(model, test_loader, tta_level=0)
print_training_details(locals(), is_final_entry=False)
run = None
# TTA Eval
starter.record()
tta_val_acc = evaluate(model, test_loader, tta_level=hyp['net']['tta_level'])
ender.record()
torch.cuda.synchronize()
total_time_seconds += 1e-3 * starter.elapsed_time(ender)
print_training_details(locals(), is_final_entry=True)
# --- ADDED: SAVE MODEL ---
if run != 'warmup':
save_path = "airbench94.pth"
torch.save(model.state_dict(), save_path)
print(f"💾 Model saved successfully to: {save_path}")
return tta_val_acc
if __name__ == "__main__":
print_columns(logging_columns_list, is_head=True)
main('warmup')
# Run multiple times to match paper methodology, or just once for testing
accs = torch.tensor([main(run) for run in range(1)])
print('Mean: %.4f Std: %.4f' % (accs.mean(), accs.std()))