forked from CCC-123/Hydraplus_Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
153 lines (123 loc) · 3.28 KB
/
test.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
149
150
151
152
153
import os
import torch
import torch.utils.data as data
from PIL import Image
import matplotlib.pyplot as plt
import torch.utils
import torchvision
import torch.nn as nn
from torch.autograd import Variable
import scipy.io as scio
import torchvision.transforms as transforms
import argparse
import dataload
import Hydraplus
import Incep
import AF_1
import AF_2
import AF_3
import time
parser = argparse.ArgumentParser()
parser.add_argument('-m',help = "choose model",choices = ['AF1','AF2','AF3','HP','MNet'],required = True)
parser.add_argument('-p',help = 'wight file path',required = True)
args = parser.parse_args()
mytransform = transforms.Compose([
transforms.Resize((299,299)), #FIXME:resize
transforms.ToTensor(), # mmb,
]
)
# torch.utils.data.DataLoader
set = dataload.myImageFloder(root = "./data/PA-100K/release_data/release_data",
label = "./data/PA-100K/annotation/annotation.mat",
transform = mytransform,
mode = 'test' )
imgLoader = torch.utils.data.DataLoader(
set,
batch_size= 1, shuffle= True, num_workers= 2)
print len(set)
mat = scio.loadmat("./data/PA-100K/annotation/annotation.mat")
att = mat["attributes"]
count = 0
classes = []
for c in att:
classes.append(c[0][0])
count = count + 1
path = args.p
if args.m == 'AF1':
net = AF_1.AF1()
if args.m == 'AF2':
net = AF_2.AF2()
if args.m == 'AF3':
net = AF_3.AF3()
if args.m == 'HP':
net = Hydraplus.HP()
if args.m == 'MNet':
net = Incep.Inception3()
net.load_state_dict(torch.load(path))
net.eval()
net.cuda()
dataiter = iter(imgLoader)
count = 0
TP = [0.0] * 26
P = [0.0] * 26
TN = [0.0] * 26
N = [0.0] * 26
Acc = 0.0
Prec = 0.0
Rec = 0.0
while count < 10000:
images,labels = dataiter.next()
inputs, labels = Variable(images,volatile = True).cuda(), Variable(labels).cuda()
#a = time.time()
outputs = net(inputs)
#b = time.time()
#print(b-a)
Yandf = 0.1
Yorf = 0.1
Y = 0.1
f = 0.1
i = 0
for item in outputs[0]:
if item.data[0] > 0 :
f = f + 1
Yorf = Yorf + 1
if labels[0][i].data[0] == 1:
TP[i] = TP[i] + 1
P[i] = P[i] + 1
Y = Y + 1
Yandf = Yandf + 1
else :
N[i] = N[i] + 1
else :
if labels[0][i].data[0] == 0 :
TN[i] = TN[i] + 1
N[i] = N[i] + 1
else:
P[i] = P[i] + 1
Yorf = Yorf + 1
Y = Y + 1
i = i + 1
Acc = Acc +Yandf/Yorf
Prec = Prec + Yandf/f
Rec = Rec + Yandf/Y
if count % 1000 == 0:
print(count)
count = count + 1
Accuracy = 0
print(TP)
print(TN)
print(P)
print(N)
for l in range(26):
print( "%s : %f" %(classes[l],(TP[l]/P[l] + TN[l]/N[l])/2))
Accuracy = TP[l]/P[l] + TN[l]/N[l] + Accuracy
meanAccuracy = Accuracy / 52
print("path: %s mA: %f"%(path,meanAccuracy))
Acc = Acc/10000
Prec = Prec/10000
Rec = Rec/10000
F1 = 2 * Prec * Rec / (Prec + Rec)
print("ACC: %f"%(Acc))
print("Prec: %f"%(Prec))
print("Rec: %f"%(Rec))
print("F1: %f"%(F1))