-
Notifications
You must be signed in to change notification settings - Fork 57
/
evaluate_imagenet.py
140 lines (101 loc) · 4.3 KB
/
evaluate_imagenet.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
import argparse
import json
import os.path as osp
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader
from models.resnet import make_resnet50_base
from datasets.imagenet import ImageNet
from utils import set_gpu, pick_vectors
def test_on_subset(dataset, cnn, n, pred_vectors, all_label,
consider_trains):
top = [1, 2, 5, 10, 20]
hits = torch.zeros(len(top)).cuda()
tot = 0
loader = DataLoader(dataset=dataset, batch_size=32,
shuffle=False, num_workers=2)
for batch_id, batch in enumerate(loader, 1):
data, label = batch
data = data.cuda()
feat = cnn(data) # (batch_size, d)
feat = torch.cat([feat, torch.ones(len(feat)).view(-1, 1).cuda()], dim=1)
fcs = pred_vectors.t()
table = torch.matmul(feat, fcs)
if not consider_trains:
table[:, :n] = -1e18
gth_score = table[:, all_label].repeat(table.shape[1], 1).t()
rks = (table >= gth_score).sum(dim=1)
assert (table[:, all_label] == gth_score[:, all_label]).min() == 1
for i, k in enumerate(top):
hits[i] += (rks <= k).sum().item()
tot += len(data)
return hits, tot
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--cnn')
parser.add_argument('--pred')
parser.add_argument('--test-set')
parser.add_argument('--output', default=None)
parser.add_argument('--gpu', default='0')
parser.add_argument('--keep-ratio', type=float, default=0.1)
parser.add_argument('--consider-trains', action='store_true')
parser.add_argument('--test-train', action='store_true')
args = parser.parse_args()
set_gpu(args.gpu)
test_sets = json.load(open('materials/imagenet-testsets.json', 'r'))
train_wnids = test_sets['train']
test_wnids = test_sets[args.test_set]
print('test set: {}, {} classes, ratio={}'
.format(args.test_set, len(test_wnids), args.keep_ratio))
print('consider train classifiers: {}'.format(args.consider_trains))
pred_file = torch.load(args.pred)
pred_wnids = pred_file['wnids']
pred_vectors = pred_file['pred']
pred_dic = dict(zip(pred_wnids, pred_vectors))
pred_vectors = pick_vectors(pred_dic, train_wnids + test_wnids, is_tensor=True).cuda()
pred_vectors = pred_vectors.cuda()
n = len(train_wnids)
m = len(test_wnids)
cnn = make_resnet50_base()
cnn.load_state_dict(torch.load(args.cnn))
cnn = cnn.cuda()
cnn.eval()
TEST_TRAIN = args.test_train
imagenet_path = 'materials/datasets/imagenet'
dataset = ImageNet(imagenet_path)
dataset.set_keep_ratio(args.keep_ratio)
s_hits = torch.FloatTensor([0, 0, 0, 0, 0]).cuda() # top 1 2 5 10 20
s_tot = 0
results = {}
if TEST_TRAIN:
for i, wnid in enumerate(train_wnids, 1):
subset = dataset.get_subset(wnid)
hits, tot = test_on_subset(subset, cnn, n, pred_vectors, i - 1,
consider_trains=args.consider_trains)
results[wnid] = (hits / tot).tolist()
s_hits += hits
s_tot += tot
print('{}/{}, {}:'.format(i, len(train_wnids), wnid), end=' ')
for i in range(len(hits)):
print('{:.0f}%({:.2f}%)'
.format(hits[i] / tot * 100, s_hits[i] / s_tot * 100), end=' ')
print('x{}({})'.format(tot, s_tot))
else:
for i, wnid in enumerate(test_wnids, 1):
subset = dataset.get_subset(wnid)
hits, tot = test_on_subset(subset, cnn, n, pred_vectors, n + i - 1,
consider_trains=args.consider_trains)
results[wnid] = (hits / tot).tolist()
s_hits += hits
s_tot += tot
print('{}/{}, {}:'.format(i, len(test_wnids), wnid), end=' ')
for i in range(len(hits)):
print('{:.0f}%({:.2f}%)'
.format(hits[i] / tot * 100, s_hits[i] / s_tot * 100), end=' ')
print('x{}({})'.format(tot, s_tot))
print('summary:', end=' ')
for s_hit in s_hits:
print('{:.2f}%'.format(s_hit / s_tot * 100), end=' ')
print('total {}'.format(s_tot))
if args.output is not None:
json.dump(results, open(args.output, 'w'))