-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
422 lines (334 loc) · 13.7 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
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
#!/usr/bin/env python3
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import os
import sys
import math
import time
import shutil
import numpy as np
from dataloader import get_dataloaders
from args import arg_parser
from adaptive_inference import dynamic_evaluate
import models
from op_counter import measure_model
#
from laplace import get_hessian_efficient
import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')
args = arg_parser.parse_args()
if args.gpu:
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
args.grFactor = list(map(int, args.grFactor.split('-')))
args.bnFactor = list(map(int, args.bnFactor.split('-')))
args.nScales = len(args.grFactor)
if args.use_valid:
args.splits = ['train', 'val', 'test']
else:
args.splits = ['train', 'val']
if args.data == 'cifar10':
args.num_classes = 10
elif args.data == 'cifar100':
args.num_classes = 100
elif args.data == 'caltech256':
args.num_classes = 257
else:
args.num_classes = 1000
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torchvision.transforms as transforms
torch.manual_seed(args.seed)
def main():
global args
best_prec1, best_epoch = 0.0, 0
if not os.path.exists(args.save):
os.makedirs(args.save)
if args.data.startswith('cifar'):
IM_SIZE = 32
else:
IM_SIZE = 224
model = getattr(models, args.arch)(args)
n_flops, n_params = measure_model(model, IM_SIZE, IM_SIZE)
args.n_flops = n_flops
torch.save(n_flops, os.path.join(args.save, 'flops.pth'))
del(model)
model = getattr(models, args.arch)(args)
model = torch.nn.DataParallel(model).cuda()
criterion = nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.SGD(model.parameters(), args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
if args.resume:
checkpoint = load_checkpoint(args)
if checkpoint is not None:
args.start_epoch = checkpoint['epoch'] + 1
best_prec1 = checkpoint['best_prec1']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
cudnn.benchmark = True
train_loader, val_loader, test_loader = get_dataloaders(args)
if args.evalmode is not None:
state_dict = torch.load(args.evaluate_from)['state_dict']
model.load_state_dict(state_dict)
if args.evalmode == 'anytime':
validate(test_loader, model, criterion)
else:
dynamic_evaluate(model, test_loader, val_loader, args)
return
scores = ['epoch\tlr\ttrain_loss\tval_loss\ttrain_prec1'
'\tval_prec1\ttrain_prec5\tval_prec5']
if not args.compute_only_laplace:
initial_time = time.time()
for epoch in range(args.start_epoch, args.epochs):
model_filename = 'checkpoint_%03d.pth.tar' % epoch
train_loss, train_prec1, train_prec5, lr = train(train_loader, model, criterion, optimizer, epoch)
val_loss, val_prec1, val_prec5 = validate(val_loader, model, criterion)
scores.append(('{}\t{:.3f}' + '\t{:.4f}' * 6)
.format(epoch, lr, train_loss, val_loss,
train_prec1, val_prec1, train_prec5, val_prec5))
is_best = val_prec1 > best_prec1
if is_best:
best_prec1 = val_prec1
best_epoch = epoch
print('New best validation last_bloc_accuracy {}'.format(best_prec1))
else:
print('Current best validation last_bloc_accuracy {}'.format(best_prec1))
save_checkpoint({
'epoch': epoch,
'arch': args.arch,
'state_dict': model.state_dict(),
'best_prec1': best_prec1,
'optimizer': optimizer.state_dict(),
}, args, is_best, model_filename, scores)
print('Best val_prec1: {:.4f} at epoch {}'.format(best_prec1, best_epoch))
print('Total training time: {}'.format(time.time() - initial_time))
# Load the best model
model_dir = os.path.join(args.save, 'save_models')
best_filename = os.path.join(model_dir, 'model_best_acc.pth.tar')
state_dict = torch.load(best_filename)['state_dict']
model.load_state_dict(state_dict)
### Test the final model
print('********** Final prediction results with the best model **********')
validate(test_loader, model, criterion)
### Test the final model + laplace
print('********** Precalculate Laplace approximation **********')
start_time = time.time()
compute_laplace_efficient(args, model, train_loader)
print('Laplace computation time: {}'.format(time.time() - start_time))
return
def compute_laplace_efficient(args, model, dset_loader):
# compute the laplace approximations
M_W, U, V = get_hessian_efficient(model, dset_loader)
print(f'Saving the hessians...')
M_W, U, V = [M_W[i].detach().cpu().numpy() for i in range(len(M_W))], \
[U[i].detach().cpu().numpy() for i in range(len(U))], \
[V[i].detach().cpu().numpy() for i in range(len(V))]
np.save(os.path.join(args.save, "effL_llla.npy"), [M_W, U, V])
def train(train_loader, model, criterion, optimizer, epoch):
global args
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1, top5 = [], []
for i in range(args.nBlocks):
top1.append(AverageMeter())
top5.append(AverageMeter())
# switch to train mode
model.train()
end = time.time()
running_lr = None
for i, (input, target) in enumerate(train_loader):
total_block_counts = torch.zeros(args.nBlocks)
lr = adjust_learning_rate(optimizer, epoch, args, batch=i,
nBatch=len(train_loader), method=args.lr_type)
if running_lr is None:
running_lr = lr
data_time.update(time.time() - end)
target = target.cuda(device=None)
input_var = torch.autograd.Variable(input)
target_var = torch.autograd.Variable(target)
loss = 0.0
output = model(input_var)
if not isinstance(output, list):
output = [output]
for j in range(len(output)):
loss += criterion(output[j], target_var)
losses.update(loss.item(), input.size(0))
for j in range(len(output)):
prec1, prec5 = accuracy(output[j].data, target, topk=(1, 5))
top1[j].update(prec1.item(), input.size(0))
top5[j].update(prec5.item(), input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.avg:.3f}\t'
'Data {data_time.avg:.3f}\t'
'Loss {loss.val:.2f}\t'
'Acc@1 {top1.val:.1f}\t'
'Acc@5 {top5.val:.1f}'.format(
epoch, i + 1, len(train_loader),
batch_time=batch_time, data_time=data_time,
loss=losses, top1=top1[-1], top5=top5[-1]))
return losses.avg, top1[-1].avg, top5[-1].avg, running_lr
def validate(val_loader, model, criterion):
flops = torch.load(os.path.join(args.save, 'flops.pth'))
flop_weights = torch.Tensor(flops)/flops[-1]
batch_time = AverageMeter()
losses = AverageMeter()
data_time = AverageMeter()
top1, top5= [], []
for i in range(args.nBlocks):
top1.append(AverageMeter())
top5.append(AverageMeter())
n = len(val_loader.sampler)
confs = torch.zeros(args.nBlocks,n)
corrs = torch.zeros(args.nBlocks,n)
model.eval()
end = time.time()
sample_ind = 0
with torch.no_grad():
for i, (input, target) in enumerate(val_loader):
batch_size = input.shape[0]
target = target.cuda(device=None)
input = input.cuda()
input_var = torch.autograd.Variable(input)
target_var = torch.autograd.Variable(target)
data_time.update(time.time() - end)
output = model(input_var)
if not isinstance(output, list):
output = [output]
loss = 0.0
for j in range(len(output)):
loss += criterion(output[j], target_var)
losses.update(loss.item(), input.size(0))
for j in range(len(output)):
prec1, prec5 = accuracy(output[j].data, target, topk=(1, 5))
top1[j].update(prec1.item(), input.size(0))
top5[j].update(prec5.item(), input.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
sample_ind += batch_size
if i % args.print_freq == 0:
print('Epoch: [{0}/{1}]\t'
'Time {batch_time.avg:.3f}\t'
'Data {data_time.avg:.3f}\t'
'Loss {loss.val:.4f}\t'
'Acc@1 {top1.val:.4f}\t'
'Acc@5 {top5.val:.4f}'.format(
i + 1, len(val_loader),
batch_time=batch_time, data_time=data_time,
loss=losses, top1=top1[-1], top5=top5[-1]))
for j in range(args.nBlocks):
print(' * prec@1 {top1.avg:.3f} prec@5 {top5.avg:.3f}'.format(top1=top1[j], top5=top5[j]))
return losses.avg, top1[-1].avg, top5[-1].avg
def save_checkpoint(state, args, is_best, filename, result):
print(args)
result_filename = os.path.join(args.save, 'scores.tsv')
model_dir = os.path.join(args.save, 'save_models')
latest_filename = os.path.join(model_dir, 'latest.txt')
model_filename = os.path.join(model_dir, filename)
best_filename = os.path.join(model_dir, 'model_best_acc.pth.tar')
os.makedirs(args.save, exist_ok=True)
os.makedirs(model_dir, exist_ok=True)
print("=> saving checkpoint '{}'".format(model_filename))
torch.save(state, model_filename)
with open(result_filename, 'w') as f:
print('\n'.join(result), file=f)
with open(latest_filename, 'w') as fout:
fout.write(model_filename)
if is_best:
shutil.copyfile(model_filename, best_filename)
print("=> saved checkpoint '{}'".format(model_filename))
return
def load_checkpoint(args):
model_dir = os.path.join(args.save, 'save_models')
if args.compute_only_laplace:
model_filename = os.path.join(model_dir, str(args.resume))
else:
latest_filename = os.path.join(model_dir, 'latest.txt')
if os.path.exists(latest_filename):
with open(latest_filename, 'r') as fin:
model_filename = fin.readlines()[0].strip()
else:
return None
print("=> loading checkpoint '{}'".format(model_filename))
state = torch.load(model_filename)
print("=> loaded checkpoint '{}'".format(model_filename))
return state
def remove_checkpoints(args, model_filename):
print("=> removing checkpoints")
for epoch in range(args.epochs-1):
filename = 'checkpoint_%03d.pth.tar' % epoch
model_dir = os.path.join(args.save, 'save_models')
model_filename = os.path.join(model_dir, filename)
os.remove(model_filename)
print("=> checkpoints removed")
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def accuracy(output, target, topk=(1,)):
"""Computes the precor@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def adjust_learning_rate(optimizer, epoch, args, batch=None,
nBatch=None, method='multistep'):
if method == 'cosine':
T_total = args.epochs * nBatch
T_cur = (epoch % args.epochs) * nBatch + batch
lr = 0.5 * args.lr * (1 + math.cos(math.pi * T_cur / T_total))
elif method == 'multistep':
if args.data.startswith('cifar'):
lr, decay_rate = args.lr, 0.1
if epoch >= args.epochs * 0.75:
lr *= decay_rate ** 2
elif epoch >= args.epochs * 0.5:
lr *= decay_rate
elif args.data.startswith('caltech'):
lr, decay_rate = args.lr, 0.1
if epoch >= args.epochs * 0.75:
lr *= decay_rate ** 2
elif epoch >= args.epochs * 0.5:
lr *= decay_rate
else:
lr = args.lr * (0.1 ** (epoch // 30))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def Entropy(x):
# Calculates the mean entropy for a batch of output logits
epsilon = 1e-5
p = nn.functional.softmax(x, dim=1)
Ex = torch.mean(-1*torch.sum(p*torch.log(p), dim=1))
return Ex
if __name__ == '__main__':
main()