text stringlengths 1 93.6k |
|---|
top1 = AverageMeter('Acc@1', ':6.2f')
|
top5 = AverageMeter('Acc@5', ':6.2f')
|
progress = ProgressMeter(
|
len(val_loader),
|
[batch_time, losses, top1, top5],
|
prefix='Test: ')
|
# switch to evaluate mode
|
model.eval()
|
with torch.no_grad():
|
end = time.time()
|
for i, (images, target) in enumerate(val_loader):
|
if args.gpu is not None:
|
images = images.cuda(args.gpu, non_blocking=True)
|
if torch.cuda.is_available():
|
target = target.cuda(args.gpu, non_blocking=True)
|
# compute output
|
output = model(images)
|
loss = criterion(output, target)
|
# measure accuracy and record loss
|
acc1, acc5 = accuracy(output, target, topk=(1, 5))
|
losses.update(loss.item(), images.size(0))
|
top1.update(acc1[0], images.size(0))
|
top5.update(acc5[0], images.size(0))
|
# measure elapsed time
|
batch_time.update(time.time() - end)
|
end = time.time()
|
if i % args.print_freq == 0:
|
progress.display(i)
|
# TODO: this should also be done with the ProgressMeter
|
print(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'
|
.format(top1=top1, top5=top5))
|
return top1.avg
|
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
|
torch.save(state, filename)
|
if is_best:
|
shutil.copyfile(filename, 'model_best.pth.tar')
|
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__)
|
class ProgressMeter(object):
|
def __init__(self, num_batches, meters, prefix=""):
|
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
|
self.meters = meters
|
self.prefix = prefix
|
def display(self, batch):
|
entries = [self.prefix + self.batch_fmtstr.format(batch)]
|
entries += [str(meter) for meter in self.meters]
|
print('\t'.join(entries))
|
def _get_batch_fmtstr(self, num_batches):
|
num_digits = len(str(num_batches // 1))
|
fmt = '{:' + str(num_digits) + 'd}'
|
return '[' + fmt + '/' + fmt.format(num_batches) + ']'
|
def adjust_learning_rate(optimizer, epoch, args):
|
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
|
lr = args.lr * (0.1 ** (epoch // 30))
|
for param_group in optimizer.param_groups:
|
param_group['lr'] = lr
|
def accuracy(output, target, topk=(1,)):
|
"""Computes the accuracy over the k top predictions for the specified values of k"""
|
with torch.no_grad():
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.