-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mafa_resize.py
More file actions
executable file
·409 lines (358 loc) · 16.2 KB
/
test_mafa_resize.py
File metadata and controls
executable file
·409 lines (358 loc) · 16.2 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
from __future__ import print_function
import os
import argparse
import torch
import torch.backends.cudnn as cudnn
import numpy as np
from data import cfg_mnet, cfg_re50
from layers.functions.prior_box import PriorBox
from utils.nms.py_cpu_nms import py_cpu_nms
import cv2
from models.retinaface import RetinaFace
from utils.box_utils import decode, decode_landm
from utils.timer import Timer
import random
from utils.box_utils import matrix_iof
from data.data_augment_1 import _crop
import math
import pdb
parser = argparse.ArgumentParser(description='Retinaface')
parser.add_argument('-m', '--trained_model', default='./weights/Resnet50_Final.pth',
type=str, help='Trained state_dict file path to open')
parser.add_argument('--network', default='resnet50', help='Backbone network mobile0.25 or resnet50')
parser.add_argument('--origin_size', default=False, type=str, help='Whether use origin image size to evaluate')
parser.add_argument('--save_folder', default='./wider_evaluate/', type=str, help='Dir to save txt results')
parser.add_argument('--cpu', action="store_true", default=False, help='Use cpu inference')
parser.add_argument('--dataset_folder', default='./data/widerface/val/images/', type=str, help='dataset path')
parser.add_argument('--confidence_threshold', default=0.02, type=float, help='confidence_threshold')
parser.add_argument('--top_k', default=5000, type=int, help='top_k')
parser.add_argument('--nms_threshold', default=0.4, type=float, help='nms_threshold')
parser.add_argument('--keep_top_k', default=750, type=int, help='keep_top_k')
parser.add_argument('-s', '--save_image', action="store_true", default=False, help='show detection results')
parser.add_argument('--vis_thres', default=0.5, type=float, help='visualization_threshold')
parser.add_argument('--path_test', type=str, help='Path Test')
args = parser.parse_args()
print(args)
def resize_subtract_mean(image, rgb_mean):
# interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_NEAREST, cv2.INTER_LANCZOS4]
# interp_method = interp_methods[random.randrange(5)]
# # image = cv2.resize(image, (640, 360), interpolation=interp_method) # (w, h)
# image = cv2.resize(image, (insize, insize), interpolation=interp_method)
image = image.astype(np.float32)
image -= rgb_mean
return image.transpose(2, 0, 1)
# return image
def check_keys(model, pretrained_state_dict):
ckpt_keys = set(pretrained_state_dict.keys())
model_keys = set(model.state_dict().keys())
used_pretrained_keys = model_keys & ckpt_keys
unused_pretrained_keys = ckpt_keys - model_keys
missing_keys = model_keys - ckpt_keys
print('Missing keys:{}'.format(len(missing_keys)))
print('Unused checkpoint keys:{}'.format(len(unused_pretrained_keys)))
print('Used keys:{}'.format(len(used_pretrained_keys)))
assert len(used_pretrained_keys) > 0, 'load NONE from pretrained checkpoint'
return True
def remove_prefix(state_dict, prefix):
''' Old style model is stored with all names of parameters sharing common prefix 'module.' '''
print('remove prefix \'{}\''.format(prefix))
f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
return {f(key): value for key, value in state_dict.items()}
def load_model(model, pretrained_path, load_to_cpu):
print('Loading pretrained model from {}'.format(pretrained_path))
if load_to_cpu:
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage)
else:
device = torch.cuda.current_device()
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage.cuda(device))
if "state_dict" in pretrained_dict.keys():
pretrained_dict = remove_prefix(pretrained_dict['state_dict'], 'module.')
else:
pretrained_dict = remove_prefix(pretrained_dict, 'module.')
check_keys(model, pretrained_dict)
model.load_state_dict(pretrained_dict, strict=False)
return model
def compute_overlap(a, b):
"""
Parameters
----------
a: (N, 4) ndarray of float
b: (K, 4) ndar0ray of float
Returns
-------
overlaps: (N, K) ndarray of overlap between boxes and query_boxes
"""
area = (b[:, 2] - b[:, 0]) * (b[:, 3] - b[:, 1])
iw = np.minimum(np.expand_dims(a[:, 2], axis=1), b[:, 2]) - np.maximum(np.expand_dims(a[:, 0], 1), b[:, 0])
ih = np.minimum(np.expand_dims(a[:, 3], axis=1), b[:, 3]) - np.maximum(np.expand_dims(a[:, 1], 1), b[:, 1])
iw = np.maximum(iw, 0)
ih = np.maximum(ih, 0)
ua = np.expand_dims((a[:, 2] - a[:, 0]) * (a[:, 3] - a[:, 1]), axis=1) + area - iw * ih
ua = np.maximum(ua, np.finfo(float).eps)
intersection = iw * ih
return intersection / ua
def _compute_ap(recall, precision):
""" Compute the average precision, given the recall and precision curves.
Code originally from https://github.com/rbgirshick/py-faster-rcnn.
# Arguments
recall: The recall curve (list).
precision: The precision curve (list).
# Returns
The average precision as computed in py-faster-rcnn.
"""
# correct AP calculation
# first append sentinel values at the end
mrec = np.concatenate(([0.], recall, [1.]))
mpre = np.concatenate(([0.], precision, [0.]))
# compute the precision envelope
for i in range(mpre.size - 1, 0, -1):
mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])
# to calculate area under PR curve, look for points
# where X axis (recall) changes value
i = np.where(mrec[1:] != mrec[:-1])[0]
# and sum (\Delta recall) * prec
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
return ap
def _get_detections(testset_folder, generator):
# testing begin
# rgb_mean = (104, 117, 123)
rgb_mean = (0,0,0)
num_images = len(generator.keys())
_t = {'forward_pass': Timer(), 'misc': Timer()}
all_detections = {}
all_bounding_boxes = {}
for i, (image_path, labels) in enumerate(generator.items()):
img_name = image_path.split("/")[-1]
img_path = str(testset_folder + image_path)
img_raw = cv2.imread(img_path)
img = cv2.cvtColor(img_raw, cv2.COLOR_BGR2RGB)
# img = np.float32(img_raw)
bounding_boxes = np.zeros((0,4))
landmarks = np.zeros((0,10))
true_labels = np.zeros((0,1))
for idx, t_label in enumerate(labels):
label = t_label.split(" ")
bounding_box = np.zeros((1, 4))
landmark = np.zeros((1,10))
true_label = np.zeros((1,1))
# bbox
bounding_box[0, 0] = max(int(label[0]), 0) # x1
bounding_box[0, 1] = max(int(label[1]), 0) # y1
bounding_box[0, 2] = int(label[0]) + int(label[2]) # x2
bounding_box[0, 3] = int(label[1]) + int(label[3]) # y2
# landmarks
landmark[0, 0] = float(label[4]) # l0_x
landmark[0, 1] = float(label[5]) # l0_y
landmark[0, 2] = float(label[7]) # l1_x
landmark[0, 3] = float(label[8]) # l1_y
landmark[0, 4] = float(label[10]) # l2_x
landmark[0, 5] = float(label[11]) # l2_y
landmark[0, 6] = float(label[13]) # l3_x
landmark[0, 7] = float(label[14]) # l3_y
landmark[0, 8] = float(label[16]) # l4_x
landmark[0, 9] = float(label[17]) # l4_y
if (landmark[0, 0]<0):
true_label[0, 0] = -1
else:
true_label[0,0] = 1
bounding_boxes = np.append(bounding_boxes, bounding_box, axis=0)
landmarks = np.append(landmarks, landmark, axis=0)
true_labels = np.append(true_labels, true_label, axis=0)
img, bounding_boxes, true_labels, landmarks, pad_image_flag = _crop(img, bounding_boxes, true_labels, landmarks)
# img = pad_to_rectangle(img, rgb_mean, pad_image_flag)
img_show = img
new_height, new_width, _ = img.shape
all_bounding_boxes[img_name] = bounding_boxes
im_height, im_width, _ = img.shape
# print(im_height, im_width)
scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
img = resize_subtract_mean(img, rgb_mean)
img = torch.from_numpy(img).unsqueeze(0)
img = img.to(device)
scale = scale.to(device)
_t['forward_pass'].tic()
loc, conf, landms = net(img) # forward pass
_t['forward_pass'].toc()
_t['misc'].tic()
priorbox = PriorBox(cfg, image_size=(im_height, im_width))
priors = priorbox.forward()
priors = priors.to(device)
prior_data = priors.data
boxes = decode(loc.data.squeeze(0), prior_data, cfg['variance'])
######
resize = 1
boxes = boxes * scale / resize
######
boxes = boxes.cpu().numpy()
scores = conf.squeeze(0).data.cpu().numpy()[:, 1]
landms = decode_landm(landms.data.squeeze(0), prior_data, cfg['variance'])
scale1 = torch.Tensor([img.shape[3], img.shape[2], img.shape[3], img.shape[2],
img.shape[3], img.shape[2], img.shape[3], img.shape[2],
img.shape[3], img.shape[2]])
scale1 = scale1.to(device)
# landms = landms * scale1 / resize
landms = landms.cpu().numpy()
# ignore low scores
inds = np.where(scores > args.confidence_threshold)[0]
boxes = boxes[inds]
landms = landms[inds]
scores = scores[inds]
# keep top-K before NMS
order = scores.argsort()[::-1]
# order = scores.argsort()[::-1][:args.top_k]
boxes = boxes[order]
landms = landms[order]
scores = scores[order]
# do NMS
dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
keep = py_cpu_nms(dets, args.nms_threshold)
# keep = nms(dets, args.nms_threshold,force_cpu=args.cpu)
dets = dets[keep, :]
landms = landms[keep]
# keep top-K faster NMS
# dets = dets[:args.keep_top_k, :]
# landms = landms[:args.keep_top_k, :]
dets = np.concatenate((dets, landms), axis=1)
_t['misc'].toc()
box_detect = []
for box in dets:
tmp = [box[0], box[1], box[2], box[3], box[4]] # x1y1 x2y2
box_detect.append(tmp)
box_detect = np.array(box_detect)
all_detections[img_name] = box_detect
# --------------------------------------------------------------------
print('im_detect: {:d}/{:d} forward_pass_time: {:.4f}s misc: {:.4f}s'.format(i + 1, num_images, _t['forward_pass'].average_time, _t['misc'].average_time))
# save image
if args.save_image:
###### ground-truth ########
for b in bounding_boxes:
cv2.rectangle(img_show, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 0), 2)
###### prediction #######
for b in dets:
if b[4] < args.vis_thres:
continue
text = "{:.4f}".format(b[4])
b = list(map(int, b))
cv2.rectangle(img_show, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 2)
cx = b[0]
cy = b[1] + 12
cv2.putText(img_raw, text, (cx, cy),
cv2.FONT_HERSHEY_DUPLEX, 0.5, (255, 255, 255))
# landms
cv2.circle(img_show, (b[5], b[6]), 1, (0, 0, 255), 4)
cv2.circle(img_show, (b[7], b[8]), 1, (0, 255, 255), 4)
cv2.circle(img_show, (b[9], b[10]), 1, (255, 0, 255), 4)
cv2.circle(img_show, (b[11], b[12]), 1, (0, 255, 0), 4)
cv2.circle(img_show, (b[13], b[14]), 1, (255, 0, 0), 4)
# save image
if not os.path.exists("./results/"):
os.makedirs("./results/")
name = "./results/" + img_name
cv2.imwrite(name, img_show)
return all_detections, all_bounding_boxes
def _get_annotations(generator, bounding_boxes):
""" Get the ground truth annotations from the generator.
The result is a list of lists such that the size is:
all_detections[num_images][num_classes] = annotations[num_detections, 5]
# Arguments
generator : The generator used to retrieve ground truth annotations.
# Returns
A list of lists containing the annotations for each image in the generator.
"""
num_classes = 1
num_images = len(generator.keys())
# all_annotations = [[None for i in range(num_classes] for j in range(num_images)]
all_annotations = {}
for key, value in generator.items():
image_name = key.split("/")[-1]
tmp = []
boxes = bounding_boxes[image_name]
for box in boxes:
x1 = int(box[0])
y1 = int(box[1])
w = int(box[2])
h = int(box[3])
tmp1 = [x1, y1, int(x1+w), int(y1+h)] # x1y1 , x2y2
tmp.append(tmp1)
# print(tmp)
tmp = np.array(tmp).astype(int)
all_annotations[image_name] = tmp
return all_annotations
if __name__ == '__main__':
torch.set_grad_enabled(False)
cfg = None
if args.network == "mobile0.25":
cfg = cfg_mnet
elif args.network == "resnet50":
cfg = cfg_re50
# net and model
net = RetinaFace(cfg=cfg, phase = 'test')
net = load_model(net, args.trained_model, args.cpu)
net.eval()
print('Finished loading model!')
# print(net)
cudnn.benchmark = True
device = torch.device("cpu" if args.cpu else "cuda")
net = net.to(device)
pytorch_total_params = sum(p.numel() for p in net.parameters())
print("pytorch_total_params", pytorch_total_params)
# testing dataset
testset_folder = args.dataset_folder
with open(args.path_test) as f:
lines = f.readlines()
_fp_bbox_map = {}
for line in lines:
line = line.strip()
if line.startswith('#'):
name = line[1:].strip()
_fp_bbox_map[name] = []
continue
_fp_bbox_map[name].append(line)
all_detections, all_annotations = _get_detections(testset_folder, _fp_bbox_map)
# all_annotations = _get_annotations(_fp_bbox_map, all_bounding_boxes)
average_precisions = {}
iou_threshold = 0.5
score_threshold = 0.05
max_detections = 100
# for label in range(generator.num_classes()):
generator = _fp_bbox_map
false_positives = np.zeros((0,))
true_positives = np.zeros((0,))
scores = np.zeros((0,))
num_annotations = 0.0
for key, _ in generator.items():
image_name = key.split("/")[-1]
detections = all_detections[image_name]
annotations = all_annotations[image_name]
num_annotations += annotations.shape[0]
detected_annotations = []
for d in detections:
scores = np.append(scores, d[4])
if annotations.shape[0] == 0:
false_positives = np.append(false_positives, 1)
true_positives = np.append(true_positives, 0)
continue
overlaps = compute_overlap(np.expand_dims(d, axis=0), annotations)
assigned_annotation = np.argmax(overlaps, axis=1)
max_overlap = overlaps[0, assigned_annotation]
if max_overlap >= iou_threshold and assigned_annotation not in detected_annotations:
false_positives = np.append(false_positives, 0)
true_positives = np.append(true_positives, 1)
detected_annotations.append(assigned_annotation)
else:
false_positives = np.append(false_positives, 1)
true_positives = np.append(true_positives, 0)
# sort by score
indices = np.argsort(-scores)
false_positives = false_positives[indices]
true_positives = true_positives[indices]
# compute false positives and true positives
false_positives = np.cumsum(false_positives)
true_positives = np.cumsum(true_positives)
# compute recall and precision
recall = true_positives / num_annotations
precision = true_positives / np.maximum(true_positives + false_positives, np.finfo(np.float64).eps)
# compute average precision
average_precision = _compute_ap(recall, precision)
print('\nmAP:')
print(average_precision)