-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.py
217 lines (175 loc) · 5.69 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
Copyright: Tianming Qiu
28th May 2020
"""
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
from lib import dataload
from lib.AF import AF
from lib.MNet import MNet
from lib.Hydraplus import HP
import pdb
import pickle
from att_vis import att_plot
def parse_args():
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)
parser.add_argument('-att',
help='attention results',
choices=['no_att', # no attention test
'img_save', # save attention results as img
'img_show', # plot attention
'pkl_save'], # save attention results as pickle file
default='no_att')
args = parser.parse_args()
return args
def model_pred(data_input, model_name, img_name, network, att_mode):
if att_mode == 'no_att':
outputs = network(data_input)
else:
if model_name == 'HP':
att1, att2, att3, outputs = network(data_input)
out_dict = {
"filename": img_name[0],
'AF1': att1[0].detach().numpy(),
'AF2': att2[0].detach().numpy(),
'AF3': att3[0].detach().numpy()
}
elif 'AF' in model_name:
outputs, att = network(data_input)
out_dict = {
"filename": img_name[0],
model_name: att[0].detach().numpy()
}
if att_mode == 'pkl_save':
pickle.dump(
out_dict,
open('result/att_output_' + model_name + '.pkl', 'ab') # append
)
else:
att_plot(model_name, out_dict, att_mode)
return outputs
def main():
args = parse_args()
mytransform = transforms.Compose([
transforms.Resize((299, 299)),
transforms.ToTensor()
]
)
# torch.utils.data.DataLoader
test_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(
test_set,
batch_size=1, shuffle=True, num_workers=2)
print('image number in test set: {}'.format(len(test_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.att == 'no_att':
if 'AF' in args.m:
net = AF(af_name=args.m)
elif args.m == 'HP':
net = HP()
elif args.m == 'MNet':
net = MNet()
else:
if 'AF' in args.m:
net = AF(att_out=True, af_name=args.m)
elif args.m == 'HP':
net = HP(att_out=True)
net.load_state_dict(torch.load(path))
print("para_load_done")
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
if args.att == 'pkl_save':
# overwrite the previous pickle file
pkl_file = 'result/att_output_' + args.m + '.pkl'
if os.path.exists(pkl_file):
os.remove(pkl_file)
while count < 10: # todo: full test set: len(test_set)
images, labels, filename = dataiter.next()
inputs, labels = Variable(images, volatile=True).cuda(), Variable(labels).cuda()
outputs = model_pred(data_input=inputs,
model_name=args.m,
img_name=filename,
network=net,
att_mode=args.att)
Yandf = 0.1
Yorf = 0.1
Y = 0.1
f = 0.1
i = 0
for item in outputs[0]:
if item.data.item() > 0:
f = f + 1
Yorf = Yorf + 1
if labels[0][i].data.item() == 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.item() == 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 % 1 == 0:
print('test on {}th img'.format(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)
if __name__ == '__main__':
main()