forked from denru01/netadapt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.py
148 lines (120 loc) · 4.64 KB
/
eval.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
from argparse import ArgumentParser
import os
import time
import math
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torch.backends.cudnn as cudnn
import pickle
import nets as models
import functions as fns
_NUM_CLASSES = 10
model_names = sorted(name for name in models.__dict__
if name.islower() and not name.startswith("__")
and callable(models.__dict__[name]))
def compute_topk_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))
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
def compute_accuracy(output, target):
output = output.argmax(dim=1)
acc = 0.0
acc = torch.sum(target == output).item()
acc = acc/output.size(0)*100
return acc
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 get_avg(self):
return self.avg
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def eval(test_loader, model, args):
batch_time = AverageMeter()
acc = AverageMeter()
# switch to eval mode
model.eval()
end = time.time()
for i, (images, target) in enumerate(test_loader):
if not args.no_cuda:
images = images.cuda()
target = target.cuda()
output = model(images)
batch_acc = compute_accuracy(output, target)
acc.update(batch_acc, images.size(0))
batch_time.update(time.time() - end)
end = time.time()
# Update statistics
estimated_time_remained = batch_time.get_avg()*(len(test_loader)-i-1)
fns.update_progress(i, len(test_loader),
ESA='{:8.2f}'.format(estimated_time_remained)+'s',
acc='{:4.2f}'.format(float(batch_acc))
)
print()
print('Test accuracy: {:4.2f}% (time = {:8.2f}s)'.format(
float(acc.get_avg()), batch_time.get_avg()*len(test_loader)))
print('===================================================================')
return float(acc.get_avg())
if __name__ == '__main__':
# Parse the input arguments.
arg_parser = ArgumentParser()
arg_parser.add_argument('data', metavar='DIR', help='path to dataset')
arg_parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
help='number of data loading workers (default: 4)')
arg_parser.add_argument('-a', '--arch', metavar='ARCH', default='alexnet',
choices=model_names,
help='model architecture: ' +
' | '.join(model_names) +
' (default: alexnet)')
arg_parser.add_argument('-b', '--batch-size', default=128, type=int,
metavar='N',
help='batch size (default: 128)')
arg_parser.add_argument('--dir', type=str, default='models/', dest='save_dir',
help='path to save models (default: models/')
arg_parser.add_argument('--no-cuda', action='store_true', default=False, dest='no_cuda',
help='disables training on GPU')
args = arg_parser.parse_args()
print(args)
# Data loader
test_dataset = datasets.CIFAR10(root=args.data, train=False, download=True,
transform=transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
]))
test_loader = torch.utils.data.DataLoader(
test_dataset, batch_size=args.batch_size, shuffle=False,
num_workers=args.workers, pin_memory=True)
# Network
model_arch = args.arch
cudnn.benchmark = True
num_classes = _NUM_CLASSES
model = models.__dict__[model_arch](num_classes=num_classes)
if not args.no_cuda:
model = model.cuda()
# Evaluation
filename = os.path.join(args.save_dir)
model = torch.load(filename)
print(model)
best_acc = eval(test_loader, model, args)
print('Testing accuracy:', best_acc)