|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import torch
|
|
|
|
import torch.backends.cudnn as cudnn
|
|
|
|
import torch.nn as nn
|
|
|
|
import torch.nn.parallel
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
from models import create_model
|
|
|
|
from data import Dataset, create_loader, get_model_meanstd
|
|
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='PyTorch ImageNet Validation')
|
|
|
|
parser.add_argument('data', metavar='DIR',
|
|
|
|
help='path to dataset')
|
|
|
|
parser.add_argument('--model', '-m', metavar='MODEL', default='dpn92',
|
|
|
|
help='model architecture (default: dpn92)')
|
|
|
|
parser.add_argument('-j', '--workers', default=2, type=int, metavar='N',
|
|
|
|
help='number of data loading workers (default: 2)')
|
|
|
|
parser.add_argument('-b', '--batch-size', default=256, type=int,
|
|
|
|
metavar='N', help='mini-batch size (default: 256)')
|
|
|
|
parser.add_argument('--img-size', default=224, type=int,
|
|
|
|
metavar='N', help='Input image dimension')
|
|
|
|
parser.add_argument('--print-freq', '-p', default=10, type=int,
|
|
|
|
metavar='N', help='print frequency (default: 10)')
|
|
|
|
parser.add_argument('--checkpoint', default='', type=str, metavar='PATH',
|
|
|
|
help='path to latest checkpoint (default: none)')
|
|
|
|
parser.add_argument('--pretrained', dest='pretrained', action='store_true',
|
|
|
|
help='use pre-trained model')
|
|
|
|
parser.add_argument('--num-gpu', type=int, default=1,
|
|
|
|
help='Number of GPUS to use')
|
|
|
|
parser.add_argument('--no-test-pool', dest='no_test_pool', action='store_true',
|
|
|
|
help='disable test time pool for DPN models')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
test_time_pool = False
|
|
|
|
if 'dpn' in args.model and args.img_size > 224 and not args.no_test_pool:
|
|
|
|
test_time_pool = True
|
|
|
|
|
|
|
|
# create model
|
|
|
|
num_classes = 1000
|
|
|
|
model = create_model(
|
|
|
|
args.model,
|
|
|
|
num_classes=num_classes,
|
|
|
|
pretrained=args.pretrained,
|
|
|
|
test_time_pool=test_time_pool)
|
|
|
|
|
|
|
|
print('Model %s created, param count: %d' %
|
|
|
|
(args.model, sum([m.numel() for m in model.parameters()])))
|
|
|
|
|
|
|
|
# optionally resume from a checkpoint
|
|
|
|
if args.checkpoint and os.path.isfile(args.checkpoint):
|
|
|
|
print("=> loading checkpoint '{}'".format(args.checkpoint))
|
|
|
|
checkpoint = torch.load(args.checkpoint)
|
|
|
|
if isinstance(checkpoint, dict) and 'state_dict' in checkpoint:
|
|
|
|
new_state_dict = OrderedDict()
|
|
|
|
for k, v in checkpoint['state_dict'].items():
|
|
|
|
if k.startswith('module'):
|
|
|
|
name = k[7:] # remove `module.`
|
|
|
|
else:
|
|
|
|
name = k
|
|
|
|
new_state_dict[name] = v
|
|
|
|
model.load_state_dict(new_state_dict)
|
|
|
|
else:
|
|
|
|
model.load_state_dict(checkpoint)
|
|
|
|
print("=> loaded checkpoint '{}'".format(args.checkpoint))
|
|
|
|
elif not args.pretrained:
|
|
|
|
print("=> no checkpoint found at '{}'".format(args.checkpoint))
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if args.num_gpu > 1:
|
|
|
|
model = torch.nn.DataParallel(model, device_ids=list(range(args.num_gpu))).cuda()
|
|
|
|
else:
|
|
|
|
model = model.cuda()
|
|
|
|
|
|
|
|
# define loss function (criterion) and optimizer
|
|
|
|
criterion = nn.CrossEntropyLoss().cuda()
|
|
|
|
|
|
|
|
cudnn.benchmark = True
|
|
|
|
|
|
|
|
data_mean, data_std = get_model_meanstd(args.model)
|
|
|
|
loader = create_loader(
|
|
|
|
Dataset(args.data),
|
|
|
|
img_size=args.img_size,
|
|
|
|
batch_size=args.batch_size,
|
|
|
|
use_prefetcher=True,
|
|
|
|
mean=data_mean,
|
|
|
|
std=data_std,
|
|
|
|
num_workers=args.workers)
|
|
|
|
|
|
|
|
batch_time = AverageMeter()
|
|
|
|
losses = AverageMeter()
|
|
|
|
top1 = AverageMeter()
|
|
|
|
top5 = AverageMeter()
|
|
|
|
|
|
|
|
# switch to evaluate mode
|
|
|
|
model.eval()
|
|
|
|
end = time.time()
|
|
|
|
with torch.no_grad():
|
|
|
|
for i, (input, target) in enumerate(loader):
|
|
|
|
target = target.cuda()
|
|
|
|
input = input.cuda()
|
|
|
|
|
|
|
|
# compute output
|
|
|
|
output = model(input)
|
|
|
|
loss = criterion(output, target)
|
|
|
|
|
|
|
|
# measure accuracy and record loss
|
|
|
|
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
|
|
|
|
losses.update(loss.item(), input.size(0))
|
|
|
|
top1.update(prec1.item(), input.size(0))
|
|
|
|
top5.update(prec5.item(), input.size(0))
|
|
|
|
|
|
|
|
# measure elapsed time
|
|
|
|
batch_time.update(time.time() - end)
|
|
|
|
end = time.time()
|
|
|
|
|
|
|
|
if i % args.print_freq == 0:
|
|
|
|
print('Test: [{0}/{1}]\t'
|
|
|
|
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
|
|
|
|
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
|
|
|
|
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
|
|
|
|
'Prec@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
|
|
|
|
i, len(loader), batch_time=batch_time, loss=losses,
|
|
|
|
top1=top1, top5=top5))
|
|
|
|
|
|
|
|
print(' * Prec@1 {top1.avg:.3f} ({top1a:.3f}) Prec@5 {top5.avg:.3f} ({top5a:.3f})'.format(
|
|
|
|
top1=top1, top1a=100-top1.avg, top5=top5, top5a=100.-top5.avg))
|
|
|
|
|
|
|
|
|
|
|
|
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 precision@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].view(-1).float().sum(0)
|
|
|
|
res.append(correct_k.mul_(100.0 / batch_size))
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|