-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
541 lines (455 loc) · 23.2 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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import torch
import time
import shutil
from torchvision.transforms import transforms
from torch.utils.data import DataLoader
import torch.nn.functional as F
from loss.contrastive import BalSCL
from loss.logitadjust import LogitAdjust
import math
from tensorboardX import SummaryWriter
from dataset.inat import INaturalist
from dataset.imagenet import ImageNetLT
# from models import resnet_big, resnext
from models import resnext
from PIL import Image, ImageFilter, ImageOps
import warnings
import torch.backends.cudnn as cudnn
import random
from randaugment import rand_augment_transform
import torchvision
from utils import GaussianBlur, shot_acc
# from torch.models.tensorboard import SummaryWriter
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', default='imagenet', choices=['inat', 'imagenet'])
parser.add_argument('--data', default='/DATACENTER/raid5/zjg/imagenet', metavar='DIR')
parser.add_argument('--arch', default='resnext50', choices=['resnet50', 'resnext50'])
parser.add_argument('--workers', default=32, type=int)
parser.add_argument('--epochs', default=90, type=int)
parser.add_argument('--temp', default=0.07, type=float, help='scalar temperature for contrastive learning')
parser.add_argument('--start_epoch', default=0, type=int, metavar='N',
help='manual epoch number (useful on restarts)')
parser.add_argument('-b', '--batch-size', default=256, type=int,
metavar='N',
help='mini-batch size (default: 256), this is the total '
'batch size of all GPUs on the current node when '
'using Data Parallel or Distributed Data Parallel')
parser.add_argument('--lr', '--learning-rate', default=0.1, type=float,
metavar='LR', help='initial learning rate', dest='lr')
parser.add_argument('--schedule', default=[160, 180], nargs='*', type=int,
help='learning rate schedule (when to drop lr by 10x)')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum of SGD solver')
parser.add_argument('--wd', '--weight-decay', default=5e-4, type=float,
metavar='W', help='weight decay (default: 1e-4)',
dest='weight_decay')
parser.add_argument('-p', '--print-freq', default=20, type=int,
metavar='N', help='print frequency (default: 20)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true',
help='evaluate model on validation set')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to latest checkpoint (default: none)')
parser.add_argument('--gpu', default=None, type=int,
help='GPU id to use.')
parser.add_argument('--alpha', default=1.0, type=float, help='cross entropy loss weight')
parser.add_argument('--beta', default=0.5, type=float, help='supervised contrastive loss weight')
parser.add_argument('--randaug', default=True, type=bool, help='use RandAugmentation for classification branch')
parser.add_argument('--cl_views', default='sim-sim', type=str, choices=['sim-sim', 'sim-rand', 'rand-rand'],
help='Augmentation strategy for contrastive learning views')
parser.add_argument('--feat_dim', default=1024, type=int, help='feature dimension of mlp head')
parser.add_argument('--warmup_epochs', default=0, type=int,
help='warmup epochs')
parser.add_argument('--root_log', type=str, default='log')
parser.add_argument('--cos', default=True, type=bool,
help='lr decays by cosine scheduler. ')
parser.add_argument('--use_norm', default=True, type=bool,
help='cosine classifier.')
parser.add_argument('--randaug_m', default=10, type=int, help='randaug-m')
parser.add_argument('--randaug_n', default=2, type=int, help='randaug-n')
parser.add_argument('--seed', default=None, type=int, help='seed for initializing training')
parser.add_argument('--reload', default=False, type=bool, help='load supervised model')
parser.add_argument('--num_classes', default=1000, type=int, help='num_classes')
def main():
args = parser.parse_args()
args.store_name = '_'.join(
[args.dataset, args.arch, 'batchsize', str(args.batch_size), 'epochs', str(args.epochs), 'temp', str(args.temp),
'lr', str(args.lr), args.cl_views])
if args.seed is not None:
random.seed(args.seed)
torch.manual_seed(args.seed)
cudnn.deterministic = True
warnings.warn('You have chosen to seed training. '
'This will turn on the CUDNN deterministic setting, '
'which can slow down your training considerably! '
'You may see unexpected behavior when restarting '
'from checkpoints.')
if args.gpu is not None:
warnings.warn('You have chosen a specific GPU. This will completely '
'disable data parallelism.')
ngpus_per_node = torch.cuda.device_count()
main_worker(args.gpu, ngpus_per_node, args)
class GaussianBlur(object):
"""Gaussian blur augmentation from SimCLR: https://arxiv.org/abs/2002.05709"""
def __init__(self, sigma=[.1, 2.]):
self.sigma = sigma
def __call__(self, x):
sigma = random.uniform(self.sigma[0], self.sigma[1])
x = x.filter(ImageFilter.GaussianBlur(radius=sigma))
return x
class Solarize(object):
"""Solarize augmentation from BYOL: https://arxiv.org/abs/2006.07733"""
def __call__(self, x):
return ImageOps.solarize(x)
def main_worker(gpu, ngpus_per_node, args):
args.gpu = gpu
if args.gpu is not None:
print("Use GPU: {} for training".format(args.gpu))
# create model
print("=> creating model '{}'".format(args.arch))
if args.arch == 'resnet50':
model = resnext.BCLModel(name='resnet50', num_classes=args.num_classes, feat_dim=args.feat_dim,
use_norm=args.use_norm)
elif args.arch == 'resnext50':
model = resnext.BCLModel(name='resnext50', num_classes=args.num_classes, feat_dim=args.feat_dim,
use_norm=args.use_norm)
else:
raise NotImplementedError('This model is not supported')
print(model)
if args.gpu is not None:
torch.cuda.set_device(args.gpu)
model = model.cuda(args.gpu)
else:
model = torch.nn.DataParallel(model).cuda()
optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume, map_location='cuda:0')
args.start_epoch = checkpoint['epoch']
best_acc1 = checkpoint['best_acc1']
if args.gpu is not None:
# best_acc1 may be from a checkpoint from a different GPU
best_acc1 = best_acc1.to(args.gpu)
model.load_state_dict(checkpoint['state_dict'], strict=False)
#optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.resume))
cudnn.benchmark = True
txt_train = f'dataset/ImageNet_LT/ImageNet_LT_train.txt' if args.dataset == 'imagenet' \
else f'dataset/iNaturalist18/iNaturalist18_train.txt'
txt_val = f'dataset/ImageNet_LT/ImageNet_LT_val.txt' if args.dataset == 'imagenet' \
else f'dataset/iNaturalist18/iNaturalist18_val.txt'
normalize = transforms.Normalize((0.466, 0.471, 0.380), (0.195, 0.194, 0.192)) if args.dataset == 'inat' \
else transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
rgb_mean = (0.485, 0.456, 0.406)
ra_params = dict(translate_const=int(224 * 0.45), img_mean=tuple([min(255, round(255 * x)) for x in rgb_mean]), )
augmentation_randncls = [
transforms.RandomResizedCrop(224, scale=(0.08, 1.)),
transforms.RandomHorizontalFlip(),
transforms.RandomApply([
transforms.ColorJitter(0.4, 0.4, 0.4, 0.0)
], p=1.0),
rand_augment_transform('rand-n{}-m{}-mstd0.5'.format(args.randaug_n, args.randaug_m), ra_params),
transforms.ToTensor(),
normalize,
]
augmentation_randnclsstack = [
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomApply([
transforms.ColorJitter(0.4, 0.4, 0.4, 0.1)
], p=0.8),
transforms.RandomGrayscale(p=0.2),
rand_augment_transform('rand-n{}-m{}-mstd0.5'.format(args.randaug_n, args.randaug_m), ra_params),
transforms.ToTensor(),
normalize,
]
augmentation_sim = [
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.RandomApply([
transforms.ColorJitter(0.4, 0.4, 0.4, 0.1) # not strengthened
], p=0.8),
transforms.RandomGrayscale(p=0.2),
transforms.ToTensor(),
normalize
]
augmentation_randnclsstack_small = [
transforms.RandomResizedCrop(112),
transforms.RandomHorizontalFlip(),
transforms.RandomApply([
transforms.ColorJitter(0.4, 0.4, 0.2, 0.1)
], p=0.8),
transforms.RandomGrayscale(p=0.2),
#transforms.RandomApply([moco.loader.GaussianBlur([.1, 2.])], p=0.1),
#transforms.RandomApply([moco.loader.Solarize()], p=0.2),
rand_augment_transform('rand-n{}-m{}-mstd0.5'.format(args.randaug_n, args.randaug_m), ra_params),
transforms.ToTensor(),
normalize,
]
if args.cl_views == 'sim-sim':
transform_train = [transforms.Compose(augmentation_randncls), transforms.Compose(augmentation_sim),
transforms.Compose(augmentation_sim), ]
elif args.cl_views == 'sim-rand':
transform_train = [transforms.Compose(augmentation_randncls), transforms.Compose(augmentation_randnclsstack),
transforms.Compose(augmentation_sim), ]
elif args.cl_views == 'rand-rand':
transform_train = [transforms.Compose(augmentation_randncls), transforms.Compose(augmentation_randnclsstack),
transforms.Compose(augmentation_randnclsstack), transforms.Compose(augmentation_randnclsstack_small),]
else:
raise NotImplementedError("This augmentations strategy is not available for contrastive learning branch!")
val_transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize
])
val_dataset = INaturalist(
root=args.data,
txt=txt_val,
transform=val_transform, train=False,
) if args.dataset == 'inat' else ImageNetLT(
root=args.data,
txt=txt_val,
transform=val_transform, train=False)
train_dataset = INaturalist(
root=args.data,
txt=txt_train,
transform=transform_train
) if args.dataset == 'inat' else ImageNetLT(
root=args.data,
txt=txt_train,
transform=transform_train)
cls_num_list = train_dataset.cls_num_list
args.cls_num = len(cls_num_list)
train_sampler = None
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.batch_size, shuffle=(train_sampler is None),
num_workers=args.workers, pin_memory=True)
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=args.batch_size, shuffle=False,
num_workers=args.workers, pin_memory=True)
criterion_ce = LogitAdjust(cls_num_list).cuda(args.gpu)
criterion_scl = BalSCL(cls_num_list, args.temp).cuda(args.gpu)
tf_writer = SummaryWriter(log_dir=os.path.join(args.root_log, args.store_name))
best_acc1 = 0.0
best_many, best_med, best_few = 0.0, 0.0, 0.0
if args.reload:
txt_test = f'dataset/ImageNet_LT/ImageNet_LT_test.txt' if args.dataset == 'imagenet' \
else f'dataset/iNaturalist18/iNaturalist18_val.txt'
test_dataset = INaturalist(
root=args.data,
txt=txt_test,
transform=val_transform, train=False
) if args.dataset == 'inat' else ImageNetLT(
root=args.data,
txt=txt_test,
transform=val_transform, train=False)
test_loader = torch.utils.data.DataLoader(
test_dataset, batch_size=args.batch_size, shuffle=False,
num_workers=args.workers, pin_memory=True)
acc1, many, med, few = validate(train_loader, test_loader, model, criterion_ce, 1, args, tf_writer)
print('Prec@1: {:.3f}, Many Prec@1: {:.3f}, Med Prec@1: {:.3f}, Few Prec@1: {:.3f}'.format(acc1,
many,
med,
few))
return
for epoch in range(args.start_epoch, args.epochs):
adjust_lr(optimizer, epoch, args)
# train for one epoch
train(train_loader, model, criterion_ce, criterion_scl, optimizer, epoch, args, tf_writer)
# evaluate on validation set
acc1, many, med, few = validate(train_loader, val_loader, model, criterion_ce, epoch, args,
tf_writer)
# remember best acc@1 and save checkpoint
is_best = acc1 > best_acc1
best_acc1 = max(acc1, best_acc1)
if is_best:
best_many = many
best_med = med
best_few = few
print('Best Prec@1: {:.3f}, Many Prec@1: {:.3f}, Med Prec@1: {:.3f}, Few Prec@1: {:.3f}'.format(best_acc1,
best_many,
best_med,
best_few))
save_checkpoint(args, {
'epoch': epoch + 1,
'arch': args.arch,
'state_dict': model.state_dict(),
'best_acc1': best_acc1,
'optimizer': optimizer.state_dict(),
}, is_best)
def train(train_loader, model, criterion_ce, criterion_scl, optimizer, epoch, args, tf_writer):
batch_time = AverageMeter('Time', ':6.3f')
ce_loss_all = AverageMeter('CE_Loss', ':.4e')
scl_loss_all = AverageMeter('SCL_Loss', ':.4e')
top1 = AverageMeter('Acc@1', ':6.2f')
model.train()
end = time.time()
for i, data in enumerate(train_loader):
inputs, targets = data #### input[0]:256,3,224,224
for_logit_targets = targets[0]
#print(inputs[1].shape)
images_gather = []
labels_gather = []
for ii in range(1, len(inputs)):
images_gather.append(inputs[ii])
#print(len(images_gather))
for iii in range(1, len(inputs)):
labels_gather.append(targets[iii])
batch_size = targets[0].shape[0] #print('batch_size',batch_size) 256
permute = torch.randperm((len(inputs)-1) * batch_size).cuda()
images_gather = torch.cat(images_gather, dim=0)
images_gather = images_gather[permute, :, :, :]
labels_gather = torch.cat(labels_gather, dim=0) #print("labels_gather",labels_gather)
labels_gather = labels_gather[permute] #print(labels_gather)
targets = labels_gather
# stitched image1
col11 = torch.cat([images_gather[0:batch_size], images_gather[batch_size:2*batch_size]], dim=3)
col21 = torch.cat([images_gather[2*batch_size:3*batch_size], images_gather[3*batch_size:4*batch_size]], dim=3)
images_gather1 = torch.cat([col11, col21], dim=2)
# stitched image2
col12 = torch.cat([images_gather[4*batch_size:5*batch_size], images_gather[5*batch_size:6*batch_size]], dim=3)
col22 = torch.cat([images_gather[6*batch_size:7*batch_size], images_gather[7*batch_size:8*batch_size]], dim=3)
images_gather2 = torch.cat([col12, col22], dim=2)
inputs = torch.cat([inputs[0], images_gather1, images_gather2], dim=0)
inputs, targets = inputs.cuda(), targets.cuda()
for_logit_targets = for_logit_targets.cuda()
feat_mlp1, feat_mlp2, logits, centers1, centers2 = model(inputs, train=True)
centers1 = centers1[:args.cls_num]
centers2 = centers2[:args.cls_num]
#print(centers.shape)
f11, f12, f13, f14, g11, g12, g13, g14 = torch.split(feat_mlp1, [batch_size, batch_size, batch_size, batch_size, batch_size, batch_size, batch_size, batch_size], dim=0)
f21, f22, f23, f24, g21, g22, g23, g24 = torch.split(feat_mlp2, [batch_size, batch_size, batch_size, batch_size, batch_size, batch_size, batch_size, batch_size], dim=0)
features1 = torch.cat([f11.unsqueeze(1), f12.unsqueeze(1), f13.unsqueeze(1), f14.unsqueeze(1), g11.unsqueeze(1), g12.unsqueeze(1), g13.unsqueeze(1), g14.unsqueeze(1)], dim=1)
features2 = torch.cat([f21.unsqueeze(1), f22.unsqueeze(1), f23.unsqueeze(1), f24.unsqueeze(1), g21.unsqueeze(1), g22.unsqueeze(1), g23.unsqueeze(1), g24.unsqueeze(1)], dim=1)
#logits, _, __ = torch.split(logits, [batch_size, batch_size, batch_size], dim=0)
logits = logits
#scl_loss = criterion_scl(centers1, features2, targets) ###为什么只有feature2能用
#print('start calculate scl')
scl_loss1 = criterion_scl(centers1, features2, targets)
scl_loss2 = criterion_scl(centers2, features1, targets)
scl_loss3 = criterion_scl(centers1, features1, targets)
scl_loss4 = criterion_scl(centers2, features2, targets)
scl_loss = scl_loss1+scl_loss4+scl_loss2+scl_loss3
#print('complete calculate scl')
ce_loss = criterion_ce(logits, for_logit_targets)
loss = args.alpha * ce_loss + args.beta * scl_loss
ce_loss_all.update(ce_loss.item(), batch_size)
scl_loss_all.update(scl_loss.item(), batch_size)
acc1 = accuracy(logits, for_logit_targets, topk=(1,))
top1.update(acc1[0].item(), batch_size)
optimizer.zero_grad()
loss.backward()
optimizer.step()
batch_time.update(time.time() - end)
end = time.time()
#print(i)
if i % args.print_freq == 0:
output = ('Epoch: [{0}][{1}/{2}] \t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'CE_Loss {ce_loss.val:.4f} ({ce_loss.avg:.4f})\t'
'SCL_Loss {scl_loss.val:.4f} ({scl_loss.avg:.4f})\t'
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})'.format(
epoch, i, len(train_loader), batch_time=batch_time,
ce_loss=ce_loss_all, scl_loss=scl_loss_all, top1=top1, )) # TODO
print(output)
tf_writer.add_scalar('CE loss/train', ce_loss_all.avg, epoch)
tf_writer.add_scalar('SCL loss/train', scl_loss_all.avg, epoch)
tf_writer.add_scalar('acc/train_top1', top1.avg, epoch)
def validate(train_loader, val_loader, model, criterion_ce, epoch, args, tf_writer=None, flag='val'):
model.eval()
batch_time = AverageMeter('Time', ':6.3f')
ce_loss_all = AverageMeter('CE_Loss', ':.4e')
top1 = AverageMeter('Acc@1', ':6.2f')
total_logits = torch.empty((0, args.cls_num)).cuda()
total_labels = torch.empty(0, dtype=torch.long).cuda()
with torch.no_grad():
end = time.time()
for i, data in enumerate(val_loader):
inputs, targets = data
inputs, targets = inputs.cuda(), targets.cuda()
batch_size = targets.size(0)
logits = model(inputs)
ce_loss = criterion_ce(logits, targets)
total_logits = torch.cat((total_logits, logits))
total_labels = torch.cat((total_labels, targets))
acc1 = accuracy(logits, targets, topk=(1,))
ce_loss_all.update(ce_loss.item(), batch_size)
top1.update(acc1[0].item(), batch_size)
batch_time.update(time.time() - end)
if i % args.print_freq == 0:
output = ('Test: [{0}/{1}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'CE_Loss {ce_loss.val:.4f} ({ce_loss.avg:.4f})\t'
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})'.format(
i, len(val_loader), batch_time=batch_time, ce_loss=ce_loss_all, top1=top1, )) # TODO
print(output)
tf_writer.add_scalar('CE loss/val', ce_loss_all.avg, epoch)
tf_writer.add_scalar('acc/val_top1', top1.avg, epoch)
probs, preds = F.softmax(total_logits.detach(), dim=1).max(dim=1)
many_acc_top1, median_acc_top1, low_acc_top1 = shot_acc(preds, total_labels, train_loader,
acc_per_cls=False)
return top1.avg, many_acc_top1, median_acc_top1, low_acc_top1
def save_checkpoint(args, state, is_best):
filename = os.path.join(args.root_log, args.store_name, 'bcl_ckpt.pth.tar')
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, filename.replace('pth.tar', 'best.pth.tar'))
class TwoCropTransform:
def __init__(self, transform1, transform2):
self.transform1 = transform1
self.transform2 = transform2
def __call__(self, x):
return [self.transform1(x), self.transform2(x), self.transform2(x)]
def adjust_lr(optimizer, epoch, args):
"""Decay the learning rate based on schedule"""
lr = args.lr
if epoch < args.warmup_epochs:
lr = lr / args.warmup_epochs * (epoch + 1)
elif args.cos: # cosine lr schedule
lr *= 0.5 * (1. + math.cos(math.pi * (epoch - args.warmup_epochs + 1) / (args.epochs - args.warmup_epochs + 1)))
else: # stepwise lr schedule
for milestone in args.schedule:
lr *= 0.1 if epoch >= milestone else 1.
for param_group in optimizer.param_groups:
param_group['lr'] = lr
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
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 __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
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)).contiguous()
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
if __name__ == '__main__':
main()