-
Notifications
You must be signed in to change notification settings - Fork 0
/
inception_resnetv2_eval.py
90 lines (78 loc) · 2.88 KB
/
inception_resnetv2_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
import datetime
import numpy as np
import torch
from model.inception_resnet_v2 import Inception_ResNetv2
from torch.utils.data import DataLoader
from datasets import ImageNet
model = Inception_ResNetv2()
model = model.cuda()
model.eval()
model.load_state_dict(torch.load('checkpoints/inceptionresnetv2.pth'))
dataset = ImageNet('data/ILSVRC2012_img_val', 'data/imagenet_classes.txt', 'data/imagenet_2012_validation_synset_labels.txt')
def topk_accuracy():
val_loader = DataLoader(dataset=dataset,
batch_size=25,
shuffle=False,
num_workers=4)
tp_1, tp_5 = 0, 0
for i, data in enumerate(val_loader):
input, label = data
input, label = input.cuda(), label.cuda()
pred = model(input)
_, pred = torch.topk(pred, 5, dim=1)
correct = pred.eq(label.view(-1, 1).expand_as(pred)).cpu().numpy()
tp_1 += correct[:, 0].sum()
tp_5 += correct.sum()
print(i, "top1: ", tp_1, "top5:", tp_5)
print("Top1 accuracy: ", tp_1 / 50000)
print("Top5 accuracy: ", tp_5 / 50000)
def each_cls_topk_accuracy():
batch_size = 25
val_loader = DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=False,
num_workers=4)
count = [0] * 1000
tp_1 = [0] * 1000
tp_5 =[0] * 1000
for i, data in enumerate(val_loader):
input, label = data
input, label = input.cuda(), label.cuda()
pred = model(input)
_, pred = torch.topk(pred, 5, dim=1)
correct = pred.eq(label.view(-1, 1).expand_as(pred)).cpu().numpy()
for j in range(batch_size):
tp_1[label.cpu()[j]] += correct[j, 0]
tp_5[label.cpu()[j]] += correct[j].sum()
count[label.cpu()[j]] += 1
print("batch %d"%i)
print(count)
print(tp_1)
print(tp_5)
accuracy_1 = np.array(tp_1) / np.array(count)
accuracy_5 = np.array(tp_5) / np.array(count)
print(sorted(accuracy_1))
print(sorted(accuracy_5))
print(accuracy_1.argsort())
print(accuracy_5.argsort())
def inference_time():
val_loader = DataLoader(dataset=dataset,
batch_size=1,
shuffle=False,
num_workers=1)
starttime = datetime.datetime.now()
for i, data in enumerate(val_loader):
input, label = data
input, label = input.cuda(), label.cuda()
pred = model(input)
_, pred = torch.topk(pred, 1, dim=1)
print(i, label, pred)
endtime = datetime.datetime.now()
total_time = (endtime - starttime).seconds
single_time = total_time / 50000
print("Total time is %f s"%total_time)
print("Single average time is %f s" %single_time)
if __name__ == '__main__':
# each_cls_topk_accuracy()
inference_time()
# topk_accuracy()