-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEvaluate_JointDetector.py
1536 lines (1340 loc) · 86.7 KB
/
Evaluate_JointDetector.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os, json, glob, shutil, argparse, pdb, sys, cv2, copy, datetime, pickle, operator, math
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
from keras_frcnn.coco import COCO
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
import numpy as np
np.set_printoptions(precision=6, threshold=np.inf, edgeitems=10, linewidth=260, suppress=True)
from keras_frcnn import config
cfg = config.Config()
MINOVERLAP = 0.50 # TODO:default value (defined in the PASCAL VOC2012 challenge)最小交并比MINOVERLAP = 0.5
Hf = 3985 #TODO:4125
cfg.evaluate_subset = 'Reasonable'
# cfg.evaluate_subset = 'All'
# evalute_new = 0 #根据得分阈值score_threshold,再次计算MR-FPPI曲线,评估测距结果
evalute_new = 1 #显示MR-2时的检测结果
idx_Dis_max = 8
idx_step = 100
max_boxes = 300
IoU_threshold_rpn = 0.70
score_threshold_rpn = 0.50
IoU_threshold_cls = 0.50
score_threshold_cls = 0.001
subset = 'val'
#subset = 'test'
if subset == 'val':
cfg.img_dir = cfg.val_img_dir
elif subset == 'test':
cfg.img_dir = cfg.test_img_dir
Detection_results_dir = "./results_NIRPed/dt_results_%s_B%d_%s" % (subset, max_boxes, str(score_threshold_cls)[2:])
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NpEncoder, self).default(obj)
def error(msg): # throw error and exit 抛出错误并退出
print(msg)
sys.exit(0) # 干净利落地退出系统
""" ground-truth 真实标记框 Load each of the ground-truth files into a temporary '.json' file. 将每个真实标记框文件加载到一个临时的“.json”文件中。
Create a list of all the class names present in the ground-truth (gt_classes).创建一个包含真实标记框(gt_类)中所有类名的列表。"""
def Temp_gt_Generation(TEMP_FILES_Path):
cocoGt = COCO(cfg.annos_file)
imgIds = sorted(cocoGt.getImgIds()) # imgIds=[100013, 100024, 100063, 100065, 100074, 100084, 100143, 100154, 100159, 100164,...]
gt_counter_per_class = {}
ele = [0]*12
for class_name in ['Ped', 'Peo', 'Bic', 'Mot','Ign','bg']:
gt_counter_per_class[class_name] = {'All': copy.deepcopy(ele), 'short': copy.deepcopy(ele), 'normal_height': copy.deepcopy(ele), 'tall': copy.deepcopy(ele),
'Sun': copy.deepcopy(ele), 'Rain': copy.deepcopy(ele), 'Spate': copy.deepcopy(ele)}
num_test_imgs = {'All': 0, 'Sun': 0, 'Rain': 0, 'Spate': 0}
for i in range(len(imgIds)):
anno_ids = cocoGt.getAnnIds(imgIds=imgIds[i]) # anno_id=1037542#anno_ids= [7000000, 7000001]
annos = cocoGt.loadAnns(ids=anno_ids)
# annos=[{'occluded': False, 'Dif': False, 'bbox': [349, 227, 20, 41], 'id': 7000000, 'category_id': 1, 'image_id': 7000000, 'pose_id': 1, 'tracking_id': 7000000, 'ignore': 0, 'area': 820, 'truncated': False},
# {'occluded': False, 'Dif': False, 'bbox': [645, 239, 21, 40], 'id': 7000001, 'category_id': 1, 'image_id': 7000000, 'pose_id': 1, 'tracking_id': 7000001, 'ignore': 0, 'area': 840, 'truncated': False}]
# Show the annotation in its image
image = cocoGt.loadImgs(ids=imgIds[i])[0]
# image={'height': 640, 'width': 1024, 'daytime': 'night', 'file_name': '58c58285bc26013700140940.png', 'id': 1096678,'recordings_id': 15.0, 'timestamp': 1598649939}
img_name = image['file_name']
img_id = img_name.split('.', 1)[0]
if img_id[:12] in ['Data20181219', 'Data20181220', 'Data20190113']:
image['weathers_id'] = 0
num_test_imgs['All'] += 1
if image['weathers_id'] == 0:
Wea = 'Sun'
elif image['weathers_id'] == 2:
Wea = 'Spate'
else:
Wea = 'Rain'
num_test_imgs[Wea] += 1
# img_path = os.path.join('E:\\Datasets\\NIRPed2021\\NIRPed\\images\\{}\\{}'.format(subset, img_name))
# img_path = os.path.join('.\\data\\NIRPed\\images\\{}\\{}'.format(subset, img_name))
# img_path = os.path.join('.\\data\\miniNIRPed\\images\\{}\\{}'.format(subset, img_name))
img_path = os.path.join(cfg.img_dir, img_name)
if img_path == None:
print('Notion:{} is not exist.'.format(img_path))
continue
groundTruth_boxes = []
try:
for anno in annos:
cat = cocoGt.loadCats(ids=anno['category_id'])[0] # cat={'name': 'pedestrian', 'id': 1}
class_name = cat['name']
if class_name in ['Pedestrian', 'pedestrian', 'Ped', 'ped']:
class_name = 'Ped'
bbox = anno['bbox']
height_gt = bbox[3]
distance = float(anno['Dis'])
if distance <= 0 or distance == 100:
distance = round(Hf /height_gt, 1) # TODO: 将像素高度直接估算为距离。
if distance > 110:
distance = 110
# distance = cfg.Dis_threshold
box = [bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]]
# if flag_mr_height == 1:
dis_gt = distance
dis_gt_index = int(dis_gt//10)
dis_from_height_gt = Hf / height_gt
error_rate_dis_from_height_gt = 100 * (dis_from_height_gt - dis_gt) / dis_gt
if error_rate_dis_from_height_gt >= 10:
height = 'tall'
elif error_rate_dis_from_height_gt < -20:
height = 'short'
else:
height = 'normal_height'
if class_name in ['Ped', 'Peo', 'Bic', 'Mot']:
vis_box = anno['vis_box']
aera_box = float(bbox[2]) * float(bbox[3])
if 100 < aera_box < 1280 * 720 * 0.9:
vis_aera = float(vis_box[2]) * float(vis_box[3])
Occlusion_coefficient = 1 - vis_aera / aera_box
if 0 <= Occlusion_coefficient < 1:
anno['Occ_Coe'] = round(Occlusion_coefficient, 2)
else:
# print('Wrong visable box yeild to Occlusion_coefficient = {}, and change it to Occlusion_coefficient=0'.format(Occlusion_coefficient))
anno['Occ_Coe'] = 0
if cfg.evaluate_subset == 'Reasonable':
if anno['Occ_Coe'] < cfg.Occ_threshold and not anno['Dif']:
gt_counter_per_class[class_name]['All'][dis_gt_index] += 1
gt_counter_per_class[class_name][height][dis_gt_index] += 1
gt_counter_per_class[class_name][Wea][dis_gt_index] += 1
elif cfg.evaluate_subset == 'All':
gt_counter_per_class[class_name]['All'][dis_gt_index] += 1
gt_counter_per_class[class_name][height][dis_gt_index] += 1
gt_counter_per_class[class_name][Wea][dis_gt_index] += 1
groundTruth_boxes.append({'class_name': class_name, 'bbox': box, 'Dis': dis_gt, 'used': False,
'Dif': anno['Dif'], 'Occ_Coe': anno['Occ_Coe'], 'Age': anno['Age'],
'area': anno['area'], 'Height': height, 'Wea': Wea})
else:
gt_counter_per_class[class_name]['All'][dis_gt_index] += 1
groundTruth_boxes.append({'class_name': class_name, 'bbox': box, 'Dis': dis_gt, 'used': False,
'Dif': anno['Dif'], 'Occ_Coe': anno['Occ_Coe'], 'Age': anno['Age'],
'area': anno['area'], 'Height': height, 'Wea': Wea})
gt_counter_per_class[class_name][height][dis_gt_index] += 1
gt_counter_per_class[class_name][Wea][dis_gt_index] += 1
if len(groundTruth_boxes) == 0:
groundTruth_boxes.append({'class_name': 'Bg_Img', 'Height': 'normal_height', 'Wea': Wea})
except Exception as e:
s = sys.exc_info()
print("Exception: Error '%s' happened on line %d with image:'%s'" % (s[1], s[2].tb_lineno, img_id))
pdb.set_trace()
gt_TEMP_FILES_Path = os.path.join(TEMP_FILES_Path, img_id + '_gt.json')
if not os.path.exists(gt_TEMP_FILES_Path):
outfile = open(gt_TEMP_FILES_Path, 'w')
json.dump(groundTruth_boxes, outfile)
outfile.close()
# get a list with the ground-truth files 获取包含真实标记框文件的列表
gt_files_list = glob.glob(TEMP_FILES_Path + '/*.json')
# gt_files_list = ['E:\\Daixb\\Evaluation_mAP_MissRate-FPPI\\MissRate-FPPI_results\\NIRPed_WeightsResnet50NIR1RGB64_1024_128_2o5L0.18\\.temp_files\\Data20181219200348_020000_gt.json', ……]
gt_files_list.sort()
num_test_imgs['short'] = num_test_imgs['All']
num_test_imgs['normal_height'] = num_test_imgs['All']
num_test_imgs['tall'] = num_test_imgs['All']
for attr in num_test_imgs:
print('NIRPed:num_test_imgs_%s=%d' % (attr, num_test_imgs[attr]))
for class_name in ['Ped']:
for attr in gt_counter_per_class[class_name]:
print('class_name:%s,attribute:%s'% (class_name, attr))
print(gt_counter_per_class[class_name][attr])
return gt_files_list, gt_counter_per_class, num_test_imgs
# TODO: *** 有序显示标记。*** 有序显示标记。*** 有序显示标记。*** 有序显示标记。*** 有序显示标记。*** 有序显示标记。*** 有序显示标记。
colors = {'Slice': ['white', 'white'], 'GT': ['red', 'black'], 'DT': ['white', 'white'], 'TP': ['lime', 'lime'], 'FP': ['magenta', 'magenta'], 'miss_Ped': ['red', 'red'], 'AER_bad': ['red', 'red'],
'Peo': ['orchid', 'black'], 'Ped': ['orangered', 'black'], 'Bic': ['hotpink', 'black'], 'Mot': ['fuchsia', 'black'], 'Ign': ['limegreen', 'black'], 'bg': ['seagreen', 'black']}
# color_white = 'white'
# color_white = 'lightgrey'
color_white = 'dimgray'
color_Obj = colors['Ped']
color_Ped = colors['Ped']
color_bg = colors['bg']
# color_GT = colors['GT']
# color_TP = colors['TP']
# color_FP = colors['FP']
# color_AER_bad = colors['AER_bad']
# color_FN = colors['miss_Ped']
color_GT = colors['Slice']
color_TP = colors['Slice']
color_FP = colors['Slice']
color_AER_bad = colors['Slice']
color_FN = colors['Slice']
size_font_slice = 28
width_line_slice = 1
font_slice = ImageFont.truetype('arial.ttf', size=30)
# font_image = ImageFont.truetype('arial.ttf', size=32)
font_image = ImageFont.truetype('arial.ttf', size=50)
# width_line_image = 1
width_line_image = 2
size_font_highlight = 2
edge_kept = 2
edge_top = 0
def draw_tags_orderly(draw,label,Doted_text,color_cls,left,right,top,bottom,h_box,edge_kept=5):
label_size = draw.textsize(label, font_image)
if bottom < cfg.im_rows_show - 10 - edge_kept - label_size[1]:
bottom_text_boundary = bottom + 20
else:
bottom_text_boundary = bottom - 20
if h_box < cfg.im_rows_show / 4 and top - edge_kept - label_size[1] > 0:
text_origin = np.array([int(min(max(0.5 * (left + right) - 0.5 * label_size[0], 0), cfg.im_cols_show - label_size[0])), int(edge_kept)])
else:
text_origin = np.array([int(min(max(0.5 * (left + right) - 0.5 * label_size[0], 0), cfg.im_cols_show - label_size[0])), int(cfg.im_rows_show - edge_kept - label_size[1])])
x = text_origin[0]
y = text_origin[1]
y_modified = y
dy = label_size[1] // 5
y_range = int((cfg.im_rows_show - 2 * edge_kept) / dy)
for tp_all in range(0, y_range):
flagT = 1
if tp_all > 0:
if y_modified < top - label_size[1] - 10:
y_modified = y_modified + dy
elif y_modified >= bottom_text_boundary:
y_modified = y_modified - dy
if top - label_size[1] - 10 <= y_modified < bottom_text_boundary:
if np.random.randint(0, 2) == 0:
y_modified = edge_kept
else:
y_modified = cfg.im_rows_show - edge_kept - label_size[1]
if Doted_text != []:
for Dot_xy in Doted_text:
dis_x = np.abs(Dot_xy[0] - x)
dis_y = np.abs(Dot_xy[1] - y_modified)
if x < Dot_xy[0] and y_modified < Dot_xy[1]:
if dis_x < label_size[0] + 2 and dis_y < label_size[1] + 2:
flagT = 0
break
elif x < Dot_xy[0] and y_modified >= Dot_xy[1]:
if dis_x < label_size[0] + 2 and dis_y < Dot_xy[3] + 2:
flagT = 0
break
elif x >= Dot_xy[0] and y_modified < Dot_xy[1]:
if dis_x < Dot_xy[2] + 2 and dis_y < label_size[1] + 2:
flagT = 0
break
elif x >= Dot_xy[0] and y_modified >= Dot_xy[1]:
if dis_x < Dot_xy[2] + 2 and dis_y < Dot_xy[3] + 2:
flagT = 0
break
if flagT == 1:
try:
text_origin = np.array([x, y_modified])
text_origin_rectangle = np.array([x-1, y_modified])
draw.rectangle([tuple(text_origin_rectangle), tuple(text_origin_rectangle + label_size)], outline=color_cls[0], fill=color_white, width=width_line_image) #TODO:fill=(255, 255, 255) 将改成:fill=color_white
draw.text(text_origin, label, fill=color_cls[1], font=font_image)
Doted_text.append([text_origin[0], text_origin[1], label_size[0], label_size[1]])
except:
print('error in draw.rectangle()')
pdb.set_trace()
break
if flagT == 0:
text_origin = np.array([x, bottom_text_boundary])
text_origin_rectangle = np.array([x-1, bottom_text_boundary])
draw.rectangle([tuple(text_origin_rectangle), tuple(text_origin_rectangle + label_size)], outline=color_cls[0], fill=(255, 255, 255), width=width_line_image)
draw.text(text_origin, label, fill=color_cls[1], font=font_image)
Doted_text.append([text_origin[0], text_origin[1], label_size[0], label_size[1]])
if top > text_origin[1] + label_size[1]:
draw.line((int(0.5 * (left + right)), top, int(0.5 * (left + right)), text_origin[1] + label_size[1]), fill=color_cls[0], width=width_line_image)
if bottom < text_origin[1]:
draw.line((int(0.5 * (left + right)), bottom, int(0.5 * (left + right)), text_origin[1]), fill=color_cls[0], width=width_line_image)
# pdb.set_trace()
# draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=(255, 255, 255, 20))
# draw.text(text_origin, label, fill=color_Ped[1], font=font_image)
# draw.rectangle([left, top, right, bottom], outline=color_cls[0], width=width_line_image) # 淡红色单框:缩放到resized图上标记框
return draw, Doted_text
#TODO:*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.
def MR_FPPI_Plot(MR_FPPI_dis_dic, detectResults_Path, max_or_seg):# TODO: ***图像显示MissRate_FPPI曲线。
font_size = 24
font = {'family': 'Times New Roman', 'weight': 'normal', 'size': font_size}
colors = ['green', 'lime', 'blue', 'purple', 'hotpink', 'orangered', 'fuchsia', 'red', 'black']
FPPI_0_2 = np.logspace(-2, 0, 9)
FPPI_0_4 = np.logspace(-4, 0, 9)
for attr in MR_FPPI_dis_dic:
fig, ax = plt.subplots(1, 1, figsize=(8, 6)) # fig = plt.figure(facecolor='w')
for dis_index in range(0, idx_Dis_max):
dis_idx = idx_Dis_max-1 - dis_index
MR_FPPI_array = np.array(MR_FPPI_dis_dic[attr][dis_idx])
MissRate_list = []
for i, x in enumerate(FPPI_0_4):
idx = np.argmin(np.abs(MR_FPPI_array[:, 0]-x))
MissRate_list.append(MR_FPPI_array[idx, 1])
# print('MissRate_list_4 of MR_FPPI[%s][%s]='% (attr, dis_index))
# print(MissRate_list)
LAMR_4 = pow(np.prod(MissRate_list), 1 / 9)
MissRate_list = []
for i, x in enumerate(FPPI_0_2):
idx = np.argmin(np.abs(MR_FPPI_array[:, 0]-x))
MissRate_list.append(MR_FPPI_array[idx, 1])
# print('MissRate_list_2 of MR_FPPI[%s][%s]=' % (attr, dis_index))
# print(MissRate_list)
LAMR_2 = pow(np.prod(MissRate_list), 1 / 9)
if max_or_seg == 'max':
dis_txt = 'd<%dm' % (dis_idx * 10 + 10)
elif max_or_seg == 'seg':
dis_txt = '%d≤d<%dm' % (dis_idx * 10, dis_idx * 10 + 10)
# pdb.set_trace()
if dis_idx * 10 + 10 == cfg.Dis_threshold:
if max_or_seg == 'max' and cfg.evaluate_subset == 'Reasonable':
plt.loglog(MR_FPPI_array[:, 0], MR_FPPI_array[:, 1], label='%.2f%%(%.2f%%) %s\n[reasonable]' % (LAMR_2*100, LAMR_4*100, dis_txt), color=colors[dis_idx], linestyle='-', linewidth=2)
else:
plt.loglog(MR_FPPI_array[:, 0], MR_FPPI_array[:, 1], label='%.2f%%(%.2f%%) %s' % (LAMR_2 * 100, LAMR_4 * 100, dis_txt), color=colors[dis_idx], linestyle='-', linewidth=2)
else:
plt.loglog(MR_FPPI_array[:, 0], MR_FPPI_array[:, 1], label='%.2f%%(%.2f%%) %s' % (LAMR_2*100, LAMR_4*100, dis_txt), color=colors[dis_idx], linestyle='-', linewidth=1)
plt.grid(color='b', linestyle='--', linewidth=0.5, alpha=0.3)
plt.xlabel('false positives per image', font)
plt.ylabel('miss rate ({})'.format(attr), font)
# plt.xlim(0.00004, 0.2)
plt.xlim(0.00003, 10) # T35
plt.ylim(0.001, 1)
plt.yticks([0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.14, 0.2, 0.3, 0.4, 0.5, 0.64, 0.8, 1],
['.001', '.002', '.005', '.01', '.02', '.05', '.10', '.14', '.20', '.30', '.40', '.50', '.64', '.80', '1'])
plt.tick_params(labelsize=font_size-10) # 刻度字体大小13
ax.spines['top'].set_visible(False) # 顶边界不可见
ax.spines['right'].set_visible(False) # 右边界不可见
legend = ax.legend(loc="lower left", fontsize=font_size-9)
if max_or_seg == 'seg':
legend = ax.legend(loc="upper right", fontsize=font_size-9)
frame = legend.get_frame()
frame.set_alpha(1)
frame.set_edgecolor('none') # 设置图例legend背景透明
frame.set_facecolor('none') # 设置图例legend背景透明
plt.grid(color='b', linestyle='--', linewidth=0.5, alpha=0.3)
plt.grid(True)
fig.subplots_adjust(left=0.12, right=0.97, top=0.98, bottom=0.12)
# plt.show()
fig.savefig(os.path.join(os.path.dirname(detectResults_Path), 'Miss_RateVsFPPI_loglog_dis_{}_{}_{}.png'.format(max_or_seg, cfg.evaluate_subset, attr)), bbox_inches='tight')
return None
#TODO:*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.
#TODO:*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.*** 图像显示MissRate_FPPI.
def PR_Plot(PR_dis_dic, detectResults_Path, max_or_seg):# TODO: ***图像显示PR曲线。
font_size = 24
font = {'family': 'Times New Roman', 'weight': 'normal', 'size': font_size}
colors = ['green', 'lime', 'blue', 'purple', 'hotpink', 'orangered', 'fuchsia', 'red', 'black']
r_list = np.linspace(0, 1, 11)
# pdb.set_trace()
for attr in PR_dis_dic:
fig, ax = plt.subplots(1, 1, figsize=(8, 6)) # fig = plt.figure(facecolor='w')
for dis_index in range(0, idx_Dis_max):
dis_idx = idx_Dis_max-1 - dis_index
PR_array = np.array(PR_dis_dic[attr][dis_idx])
if PR_array.size == 0:
if max_or_seg == 'seg':
print('Did not detect any pedestrian in distance segment [%d,%d)]' % (dis_idx*10, (dis_idx+1)*10))
elif max_or_seg == 'max':
print('Did not detect any pedestrian in distance range [3,%d)]' % ((dis_idx + 1) * 10))
continue
pr_list = []
for ri in r_list:
index_ri = np.argmin(np.abs(PR_array[:, 1] - ri))
pr_list.append(np.max(PR_array[index_ri:, 0]))
AP = np.mean(pr_list)
if max_or_seg == 'max':
dis_txt = 'd<%dm' % (dis_idx * 10 + 10)
elif max_or_seg == 'seg':
dis_txt = '%d≤d<%dm' % (dis_idx * 10, dis_idx * 10 + 10)
if dis_idx * 10 + 10 == cfg.Dis_threshold:
if max_or_seg == 'max' and cfg.evaluate_subset == 'Reasonable':
plt.plot(PR_array[:, 0], PR_array[:, 1], label='AP=%.2f%% %s[reasonable]' % (100*AP, dis_txt), color=colors[dis_idx], linestyle='-', linewidth=2)
else:
plt.plot(PR_array[:, 0], PR_array[:, 1], label='AP=%.2f%% %s' % (100*AP, dis_txt), color=colors[dis_idx], linestyle='-', linewidth=2)
else:
plt.plot(PR_array[:, 0], PR_array[:, 1], label='AP=%.2f%% %s' % (100*AP, dis_txt), color=colors[dis_idx], linestyle='-', linewidth=1)
plt.grid(color='b', linestyle='--', linewidth=0.5, alpha=0.3)
plt.xlabel('recall', font)
plt.ylabel('precision (%s)' % (attr), font)
plt.xlim(0.4, 1) # T35
plt.ylim(0, 1)
plt.tick_params(labelsize=font_size-10) # 刻度字体大小13
ax.spines['top'].set_visible(False) # 顶边界不可见
ax.spines['right'].set_visible(False) # 右边界不可见
legend = ax.legend(loc="lower left", fontsize=font_size-9)
if max_or_seg == 'seg':
legend = ax.legend(loc="upper right", fontsize=font_size-9)
frame = legend.get_frame()
frame.set_alpha(1)
frame.set_edgecolor('none') # 设置图例legend背景透明
frame.set_facecolor('none') # 设置图例legend背景透明
plt.grid(color='b', linestyle='--', linewidth=0.5, alpha=0.3)
plt.grid(True)
fig.subplots_adjust(left=0.12, right=0.97, top=0.98, bottom=0.12)
plt.show()
fig.savefig(os.path.join(os.path.dirname(detectResults_Path), 'PR_dis_{}_{}_{}.png'.format(max_or_seg, cfg.evaluate_subset, attr)), bbox_inches='tight')
return None
#TODO:******后处理测距误差******后处理测距误差******后处理测距误差******后处理测距误差******后处理测距误差******后处理测距误差******
def post_processing(DE_conf, abs_flag = False):
DE_max_dic = copy.deepcopy(DE_conf)
DE_seg_dic = copy.deepcopy(DE_conf)
for attr in DE_conf:
Results_all_dis = []
for key_dis, v_dis in DE_conf[attr].items():
Results_all_dis += v_dis
num_dis = len(v_dis)
if abs_flag:
v_dis = np.abs(v_dis)
if num_dis > 0:
mean = round(np.mean(v_dis), 2)
sigma = round(np.std(v_dis), 2)
else:
mean = 0
sigma = 0
DE_seg_dic[attr][key_dis] = [mean, sigma, num_dis]
num_dis_all = len(Results_all_dis)
if abs_flag:
Results_all_dis = np.abs(Results_all_dis)
if num_dis_all > 0:
mean_all = round(np.mean(Results_all_dis), 2)
sigma_all = round(np.std(Results_all_dis), 2)
else:
mean_all = 0
sigma_all = 0
DE_max_dic[attr][key_dis] = [mean_all, sigma_all, num_dis_all]
return DE_seg_dic, DE_max_dic
#TODO: ******计算MissRate_FPPI和PR曲线的函数******计算MissRate_FPPI和PR曲线的函数******计算MissRate_FPPI和PR曲线的函数******
def MR_FPPI_PR_Calculate(detectResults_Path):
""" Create a '.temp_files/' and 'results/' directory 创建“.temp_files/”和“results/”目录"""
TEMP_FILES_DIR = os.path.dirname(detectResults_Path)
TEMP_FILES_Path = os.path.join(TEMP_FILES_DIR, '.temp_files')
if not os.path.exists(TEMP_FILES_Path): # if it doesn't exist already如果'.temp_files'文件夹不存在与当前目录
os.makedirs(TEMP_FILES_Path) #在程序当前目录下创建一个新的文件夹TEMP_FILES_Path = '.temp_files'
gt_files_list, gt_counter_per_class, num_test_imgs = Temp_gt_Generation(TEMP_FILES_Path)
gt_classes = list(gt_counter_per_class.keys()) # 获取标记框分类计数的键值——类别,并转化成list=['Ped', 'Peo', 'Bic', 'Mot', 'Ign', 'bg']
for class_index, class_name in enumerate(gt_classes): #gt_classes = ['pedestrian', 'motorbikedriver', 'ignore', 'bicycledriver'] ['bg', 'Peo', 'Ped', 'Mot', 'Ign', 'Bic']
"""Load detection-result of that class 加载该类的检测结果"""
if class_name not in ['Ped', 'ped', 'Pedestrian', 'pedestrian']:
continue
dt_data = json.load(open(detectResults_Path)) #加载json文件中的数据
num_dt_boxes = len(dt_data) # 网络检测到的当前类的总数量(包括正正例tp和假正例FP):num_dt_boxes = 1639
ele = np.zeros((num_dt_boxes, 12))
tp_dic = {'All':copy.deepcopy(ele), 'Sun':copy.deepcopy(ele), 'Rain':copy.deepcopy(ele), 'Spate':copy.deepcopy(ele),
'short':copy.deepcopy(ele), 'normal_height':copy.deepcopy(ele), 'tall':copy.deepcopy(ele)}
fp_dic = copy.deepcopy(tp_dic)
MR_FPPI_dis_max_dic = {'All':{}, 'Sun':{}, 'Rain':{}, 'Spate':{}, 'short':{}, 'normal_height':{}, 'tall':{}}
MR_FPPI_dis_seg_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
PR_dis_max_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
PR_dis_seg_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
AE_dtp_max_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
AE_dtp_seg_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
AE_DE_max_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
AE_DE_seg_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
AER_dtp_max_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
AER_dtp_seg_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
AER_DE_max_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
AER_DE_seg_dic = copy.deepcopy(MR_FPPI_dis_max_dic)
annos_AER_bad = {}
annos_FN = {}
annos_FP = {}
for attr in MR_FPPI_dis_max_dic:
for dis_index in range(0, idx_Dis_max):
MR_FPPI_dis_max_dic[attr][dis_index] = []
MR_FPPI_dis_seg_dic[attr][dis_index] = []
PR_dis_max_dic[attr][dis_index] = []
PR_dis_seg_dic[attr][dis_index] = []
AE_dtp_max_dic[attr][dis_index] = []
AE_dtp_seg_dic[attr][dis_index] = []
AE_DE_max_dic[attr][dis_index] = []
AE_DE_seg_dic[attr][dis_index] = []
AER_dtp_max_dic[attr][dis_index] = []
AER_dtp_seg_dic[attr][dis_index] = []
AER_DE_max_dic[attr][dis_index] = []
AER_DE_seg_dic[attr][dis_index] = []
"""#*************************************************************************对当前类所有检测框与真实标记框进行匹配,并做好TP/FP记录。"""
min_overlap = MINOVERLAP # 最小交并比设置为:MINOVERLAP = 0.5
gt_file_used = []
ignored_dt_count = [0]*12 #TODO: 记录被忽略检测框的数量。
# TODO: 按检测框得分高低开始评估MR-FPPI和AP。
# idx = 0, box_detection = {'class_name': 'Ped', 'confidence': '1.0', 'file_id': 'Data20200624201506_971695N850F12', 'bbox': [1040.0, 202.0, 1168.0, 517.0], 'Dis': 14.78}
for idx, box_detection in enumerate(dt_data):
img_id = box_detection['file_id']
# assign box_detection-result to ground truth object if any
# open ground-truth with that img_id
gt_file = os.path.join(TEMP_FILES_Path, img_id + '_gt.json') # gt_file = '.temp_files/Data20200624201506_971695N850F12_gt.json'
try:
gt_data_temp = json.load(open(gt_file))
except:
print("\033[5;31;47m\tImage:{}, is not in ground-truth. Please check that ground-truth and detect results are matched.\033[0m".format(img_id))
continue
ovmax = -1 # 最大交并比初值设为-1
a_percentage_max = -1 # 最大交并比初值设为-1
gt_matched = -1 # 当前第idx预测框没有匹配上GT
# load detected object bounding-box
bbdtF = [float(x) for x in box_detection['bbox']] # 检测框信息:bbdtF = [1040.0, 202.0, 1168.0, 517.0]
cx_dt = 0.5*(bbdtF[0]+bbdtF[2])
cy_dt = 0.5*(bbdtF[1]+bbdtF[3])
width_dt = bbdtF[2] - bbdtF[0]
height_dt = bbdtF[3] - bbdtF[1]
dis_dt = min(110, box_detection['Dis'])
dis_dt_index = int(dis_dt//10)
in_boxes = False
""" Assign detection-result to ground-truth objects 将检测结果与标记对象进行匹配。"""
for obj in gt_data_temp:
'''gt_data_temp=[{"class_name": "Ped", "bbox": [39, 191, 153, 525], "Dis": 13.4, "used": false, "Dif": false, "Occ_Coe": 0.0, "Age": "Adult", "area": 38076},
{"class_name": "Ped", "bbox": [434, 208, 569, 511], "Dis": 13.0, "used": false, "Dif": false, "Occ_Coe": 0.0, "Age": "Adult", "area": 40905},
{"class_name": "Ped", "bbox": [196, 217, 346, 517], "Dis": 13.2, "used": false, "Dif": false, "Occ_Coe": 0.0, "Age": "Adult", "area": 45000}]'''
# look for a class_name match obj = {'class_name': 'Ped', 'bbox': '466 313 518 433 38', 'used': False, 'Dif': True}
if obj['class_name'] != 'Bg_Img':
bbgtF = [float(x) for x in obj['bbox']] # 真实标记框信息:bbgtF = [466.0, 313.0, 518.0, 433.0, 38.0]
if (bbgtF[0]-2 <= cx_dt <= bbgtF[2]+2) and (bbgtF[1]-2 <= cy_dt <= bbgtF[3]+2):
in_boxes = True
bi = [max(bbdtF[0], bbgtF[0]), max(bbdtF[1], bbgtF[1]), min(bbdtF[2], bbgtF[2]), min(bbdtF[3], bbgtF[3])] # 用于计算检测框和真实标记框的交集面积
iw = bi[2] - bi[0] + 1 # 计算交集宽
ih = bi[3] - bi[1] + 1 # 计算交集高
if iw > 0 and ih > 0: # 如果相交,才开始计算交并比 compute overlap (IoU) = area of intersection / area of union
if obj['class_name'] in ['Ped', 'ped', 'Pedestrian', 'pedestrian']:
ua = (width_dt + 1) * (height_dt + 1) + (bbgtF[2] - bbgtF[0] + 1) * (bbgtF[3] - bbgtF[1] + 1) - iw * ih # 计算并集面积
ov = iw * ih / ua # 计算交并比
if ov > ovmax:
ovmax = ov
gt_matched = obj # 用gt_match记录与当前检测框bb匹配的真实标记框gt_match = {'class_name': 'Bic', 'bbox': [1035, 229, 1156, 526],
# 'Dis': 15.4, 'used': False, 'Dif': False, 'Occ_Coe': 0.0, 'Age': 'Adult', 'area': 35937}
else:
a_percentage = iw * ih / ((width_dt + 1) * (height_dt + 1))
if a_percentage > a_percentage_max:
a_percentage_max = a_percentage
gt_matched = obj
try: # gt_match={'class_name': 'Ped', 'bbox': '98 309 212 584 16.3', 'used': False, 'scale_dis': 'near'}gt_match=-1将出错不能进行。
if not in_boxes: #TODO:{'class_name': 'Ped', 'confidence': '1.0', 'file_id': 'Data20190113195620_050000', 'bbox': [1040.0, 0.0, 1312.0, 652.0], 'Dis': 20520.62}
gt_matched = gt_data_temp[0]
fp_dic['All'][idx, dis_dt_index] = 1
fp_dic[gt_matched['Wea']][idx, dis_dt_index] = 1
fp_dic[gt_matched['Height']][idx, dis_dt_index] = 1
else: #TODO:对匹配上扎推的人群、骑单车、摩托车的人、背景画、或者人类都难以识别的行人进行忽略,即:既不判定为TP又不判定为FP。
if gt_matched['class_name'] in ['Ped', 'ped', 'Pedestrian', 'pedestrian']:
if ovmax >= min_overlap: # 最小交并比设置为:min_overlap = MINOVERLAP = 0.5
dis_gt = gt_matched['Dis']
dis_gt_index = int(dis_gt // 10)
bbgtF = [float(x) for x in gt_matched['bbox']]
if cfg.evaluate_subset == 'Reasonable':
if not (gt_matched['Dif'] or float(gt_matched['Occ_Coe']) > cfg.Occ_threshold or dis_gt >= cfg.Dis_threshold):
if not bool(gt_matched['used']): # 换成布尔值
tp_dic['All'][idx, dis_gt_index] = 1
tp_dic[gt_matched['Wea']][idx, dis_gt_index] = 1
tp_dic[gt_matched['Height']][idx, dis_gt_index] = 1
# TODO:正确检测目标的距离误差求解
dis_dtp = round(Hf / height_dt, 2) # TODO: 将预测框像素高度直接估算为距离。
AE_dtp_instance = abs(dis_dtp - dis_gt) # TODO:AE=absolute_error
AE_dtp_seg_dic['All'][dis_gt_index].append(AE_dtp_instance)
AE_dtp_seg_dic[gt_matched['Wea']][dis_gt_index].append(AE_dtp_instance)
AE_dtp_seg_dic[gt_matched['Height']][dis_gt_index].append(AE_dtp_instance)
AE_DE_instance = abs(dis_dt - dis_gt) # TODO:AE=absolute_error
AE_DE_seg_dic['All'][dis_gt_index].append(AE_DE_instance)
AE_DE_seg_dic[gt_matched['Wea']][dis_gt_index].append(AE_DE_instance)
AE_DE_seg_dic[gt_matched['Height']][dis_gt_index].append(AE_DE_instance)
AER_dtp_instance = round(100 * AE_dtp_instance / dis_gt, 3) # TODO:AE=error
AER_dtp_seg_dic['All'][dis_gt_index].append(AER_dtp_instance)
AER_dtp_seg_dic[gt_matched['Wea']][dis_gt_index].append(AER_dtp_instance)
AER_dtp_seg_dic[gt_matched['Height']][dis_gt_index].append(AER_dtp_instance)
AER_DE_instance = round(100 * AE_DE_instance / dis_gt, 3) # TODO:AER = error_rate
AER_DE_seg_dic['All'][dis_gt_index].append(AER_DE_instance)
AER_DE_seg_dic[gt_matched['Wea']][dis_gt_index].append(AER_DE_instance)
AER_DE_seg_dic[gt_matched['Height']][dis_gt_index].append(AER_DE_instance)
if (abs(AER_DE_instance) > 15):
if img_id not in annos_AER_bad:
annos_AER_bad[img_id] = []
annos_AER_bad[img_id].append({'class_name': gt_matched['class_name'],
'BB_gt': gt_matched['bbox'], 'Dis_gt': dis_gt,
'BB_dt': bbdtF,
'Dis_dt': dis_dt, 'Wea': gt_matched['Wea'],
'Height': gt_matched['Height']})
# TODO:正确检测目标的距离误差求解
gt_matched['used'] = True # 真实标记框已被检测到,标记为已使用,以后的预测框不再考虑此真实标记框。
with open(gt_file, 'w') as f: # gt_file = '.temp_files/Tongxy13m161R850_gt.json'
f.write(json.dumps(gt_data_temp)) # 更新gt_data
f.close()
if img_id not in gt_file_used:
gt_file_used.append(img_id)
else:# false positive (multiple box_detection)
fp_dic['All'][idx, dis_gt_index] = 1
fp_dic[gt_matched['Wea']][idx, dis_gt_index] = 1
fp_dic[gt_matched['Height']][idx, dis_gt_index] = 1
else: #TODO: 忽略此检测框,不计入误报。
ignored_dt_count[dis_gt_index] += 1
elif cfg.evaluate_subset == 'All':
if dis_dt < cfg.Dis_threshold:
if not bool(gt_matched['used']): # 换成布尔值
tp_dic['All'][idx, dis_gt_index] = 1
tp_dic[gt_matched['Wea']][idx, dis_gt_index] = 1
tp_dic[gt_matched['Height']][idx, dis_gt_index] = 1
# TODO:正确检测目标的距离误差求解
dis_dtp = round(Hf / height_dt, 2) # TODO: 将预测框像素高度直接估算为距离。
AE_dtp_instance = abs(dis_dtp - dis_gt) # TODO:AE=absolute_error
AE_dtp_seg_dic['All'][dis_gt_index].append(AE_dtp_instance)
AE_dtp_seg_dic[gt_matched['Wea']][dis_gt_index].append(AE_dtp_instance)
AE_dtp_seg_dic[gt_matched['Height']][dis_gt_index].append(AE_dtp_instance)
AE_DE_instance = abs(dis_dt - dis_gt) # TODO:AE=absolute_error
AE_DE_seg_dic['All'][dis_gt_index].append(AE_DE_instance)
AE_DE_seg_dic[gt_matched['Wea']][dis_gt_index].append(AE_DE_instance)
AE_DE_seg_dic[gt_matched['Height']][dis_gt_index].append(AE_DE_instance)
AER_dtp_instance = round(100 * AE_dtp_instance / dis_gt, 3) # TODO:AE=error
AER_dtp_seg_dic['All'][dis_gt_index].append(AER_dtp_instance)
AER_dtp_seg_dic[gt_matched['Wea']][dis_gt_index].append(AER_dtp_instance)
AER_dtp_seg_dic[gt_matched['Height']][dis_gt_index].append(AER_dtp_instance)
AER_DE_instance = round(100 * AE_DE_instance / dis_gt, 3) # TODO:AER = error_rate
AER_DE_seg_dic['All'][dis_gt_index].append(AER_DE_instance)
AER_DE_seg_dic[gt_matched['Wea']][dis_gt_index].append(AER_DE_instance)
AER_DE_seg_dic[gt_matched['Height']][dis_gt_index].append(AER_DE_instance)
if (abs(AER_DE_instance) > 15):
if img_id not in annos_AER_bad:
annos_AER_bad[img_id] = []
annos_AER_bad[img_id].append({'class_name': gt_matched['class_name'],
'BB_gt': gt_matched['bbox'], 'Dis_gt': dis_gt,
'BB_dt': bbdtF,
'Dis_dt': dis_dt, 'Wea': gt_matched['Wea'],
'Height': gt_matched['Height']})
gt_matched['used'] = True # 真实标记框已被检测到,标记为已使用,以后的预测框不再考虑此真实标记框。
with open(gt_file, 'w') as f: # gt_file = '.temp_files/Tongxy13m161R850_gt.json'
f.write(json.dumps(gt_data_temp)) # 更新gt_data
f.close()
if img_id not in gt_file_used:
gt_file_used.append(img_id)
else:# TODO:第idx个检测框匹配上当前类的标记框,且交并比不小于min_overlap=0.5,但此标记框在之前已被检测框匹配到了,判定为真正例FP
fp_dic['All'][idx, dis_gt_index] = 1
fp_dic[gt_matched['Wea']][idx, dis_gt_index] = 1
fp_dic[gt_matched['Height']][idx, dis_gt_index] = 1
else: #TODO: 忽略此检测框,不计入误报。
ignored_dt_count[dis_gt_index] += 1
elif ovmax > 0: # 如果ovmax < min_overlap: # false positive
if cfg.evaluate_subset == 'Reasonable':
if not (gt_matched['Dif'] or float(gt_matched['Occ_Coe']) > cfg.Occ_threshold or dis_dt >= cfg.Dis_threshold):
fp_dic['All'][idx, dis_dt_index] = 1
fp_dic[gt_matched['Wea']][idx, dis_dt_index] = 1
fp_dic[gt_matched['Height']][idx, dis_dt_index] = 1
else: #TODO: 忽略此检测框,不计入误报。
ignored_dt_count[dis_dt_index] += 1
elif cfg.evaluate_subset == 'All':
if dis_dt < cfg.Dis_threshold:
fp_dic['All'][idx, dis_dt_index] = 1
fp_dic[gt_matched['Wea']][idx, dis_dt_index] = 1
fp_dic[gt_matched['Height']][idx, dis_dt_index] = 1
else: #TODO: 忽略此检测框,不计入误报。
ignored_dt_count[dis_dt_index] += 1
else: # elif gt_matched['class_name'] in ['Peo', 'Bic', 'Mot', 'Ign', 'bg', 'Bg', 'BG']:
if a_percentage_max >= min_overlap: #TODO:匹配上任何忽略区域,将被忽略。
ignored_dt_count[dis_dt_index] += 1
#print('Exception: {} in img_id:{}'.format(e, img_id))
else: #TODO:没有匹配上目标,也没有匹配上任何忽略区域,检测框判定为假正例fp。
fp_dic['All'][idx, dis_dt_index] = 1
fp_dic[gt_matched['Wea']][idx, dis_dt_index] = 1
fp_dic[gt_matched['Height']][idx, dis_dt_index] = 1
except Exception as e: #TODO:对没有匹配上任何目标的检测框判定为假正例fp。
# errormessage = '{}'.format(e)
s = sys.exc_info()
print("Exception: Error '%s' happened on line %d with image:'%s'" % (s[1], s[2].tb_lineno, img_id))
pdb.set_trace()
#fp_all[idx, dis_dt_index] = 1
if idx % idx_step != 0: #每个100个检测结果,计算一次MR_FPPI和PR
continue
flag_break = 0
for attr in MR_FPPI_dis_max_dic:
for dis_index in range(0, idx_Dis_max):
num_tp = tp_dic[attr][:idx+1, dis_index:dis_index+1].sum() # 找出不同距离段的TP的位置序列,#正确检测到的行人统计。tp_all = np.zeros((num_dt_boxes, 1))
num_fp = fp_dic[attr][:idx+1, dis_index:dis_index+1].sum() # 找出不同距离段的TP的位置序列,#错误检测到的行人统计。fp_all = np.zeros((num_dt_boxes, 1))
num_gt_boxes = sum(gt_counter_per_class[class_name][attr][dis_index:dis_index+1])
FPPI_seg = round(num_fp/num_test_imgs[attr], 6)
if num_gt_boxes == 0:
MissRate_seg=0
else:
MissRate_seg = round((num_gt_boxes - num_tp) / num_gt_boxes, 6)
if num_tp+num_fp != 0: #防止num_tp=0.0; num_fp=0.0
re_seg = 1 - MissRate_seg
pr_seg = round(num_tp/(num_tp+num_fp), 6)
PR_dis_seg_dic[attr][dis_index].append([pr_seg, re_seg])
MR_FPPI_dis_seg_dic[attr][dis_index].append([FPPI_seg, MissRate_seg])
num_tp = tp_dic[attr][:idx+1, :dis_index+1].sum() # 找出不同距离段的TP的位置序列,#正确检测到的行人统计。tp_all = np.zeros((num_dt_boxes, 1))
num_fp = fp_dic[attr][:idx+1, :dis_index+1].sum() # 找出不同距离段的TP的位置序列,#错误检测到的行人统计。fp_all = np.zeros((num_dt_boxes, 1))
num_gt_boxes = sum(gt_counter_per_class[class_name][attr][:dis_index+1])
FPPI_max = round(num_fp / num_test_imgs[attr], 6)
if num_gt_boxes == 0:
MissRate_max = 0
else:
MissRate_max = round((num_gt_boxes - num_tp) / num_gt_boxes, 6)
MR_FPPI_dis_max_dic[attr][dis_index].append([FPPI_max, MissRate_max])
if (num_tp + num_fp) != 0: #防止num_tp=0.0; num_fp=0.0
re_max = 1-MissRate_max
pr_max = round(num_tp / (num_tp+num_fp), 6)
PR_dis_max_dic[attr][dis_index].append([pr_max, re_max])
if (attr == 'All') and (dis_index*10+10 == cfg.Dis_threshold):
score_threshold = float(box_detection['confidence'])
if idx % 200 == 0:
print('idx=%d/%d;d<=%d;score_threshold=\033[30;41m %.4f\033[0m:[FPPI, MissRate]=[\033[30;42m %.6f\033[0m, \033[30;41m %.4f\033[0m]; ignored_dt_count=%d' % (idx, num_dt_boxes, dis_index * 10 + 10, score_threshold, FPPI_max, MissRate_max, sum(ignored_dt_count)))
# print(ignored_dt_count)
if FPPI_max > 10:
flag_break = 1 #TODO: 提前终止计算标识。
if flag_break == 1:
break
# TODO:***将判定为假正例fp的检测框保存下来,为后续可视化做准备。***将判定为假正例fp的检测框保存下来,为后续可视化做准备。***将判定为假正例fp的检测框保存下来,为后续可视化做准备。
if max(fp_dic['All'][idx, :int(cfg.Dis_threshold // 10)]) == 1:
if img_id not in annos_FP:
annos_FP[img_id] = []
annos_FP[img_id].append({'class_name': box_detection['class_name'], 'confidence': box_detection['confidence'],
'BB_dt': box_detection['bbox'], 'Dis_dt': box_detection['Dis'], 'Wea':gt_matched['Wea'],
'Height':gt_matched['Height']})
for gt_file in gt_files_list:
img_id = os.path.basename(gt_file)
img_id = img_id.split('_gt', 1)[0]
try:
gt_data_temp = json.load(open(gt_file))
except:
print("\033[5;31;47m\tImage:{}, is not in ground-truth. Please check that ground-truth and detect results are matched.\033[0m".format(img_id))
continue
for anno in gt_data_temp:
if anno['class_name'] not in ['Ped', 'ped', 'Pedestrian', 'pedestrian']:
continue
if anno['used']:
continue
if cfg.evaluate_subset == 'Reasonable':
if anno['Dif'] or (anno['Occ_Coe'] > cfg.Occ_threshold) or (anno['Dis'] > cfg.Dis_threshold): #TODO:检查此步是否执行到位????
continue
elif cfg.evaluate_subset == 'All':
if anno['Dis'] > cfg.Dis_threshold: #TODO:检查此步是否执行到位????
continue
if img_id not in annos_FN:
annos_FN[img_id] = []
annos_FN[img_id].append(anno)
AE_dtp_seg_dic, AE_dtp_max_dic = post_processing(AE_dtp_seg_dic)
AE_DE_seg_dic, AE_DE_max_dic = post_processing(AE_DE_seg_dic)
AER_dtp_seg_dic, AER_dtp_max_dic = post_processing(AER_dtp_seg_dic)
AER_DE_seg_dic, AER_DE_max_dic = post_processing(AER_DE_seg_dic)
# TODO:***将判定为假正例fp的检测框保存下来,为后续可视化做准备。***将判定为假正例fp的检测框保存下来,为后续可视化做准备。***将判定为假正例fp的检测框保存下来,为后续可视化做准备。
print('\n\nMR_FPPI_dis_max_dic:')
MR_FPPI_Plot(MR_FPPI_dis_max_dic, detectResults_Path, 'max') #TODO:max_or_seg
print('\n\nPR_dis_max_dic:')
PR_Plot(PR_dis_max_dic, detectResults_Path, 'max') #TODO:max_or_seg
print('\n\nMR_FPPI_dis_seg_dic:')
MR_FPPI_Plot(MR_FPPI_dis_seg_dic, detectResults_Path, 'seg') #TODO:max_or_seg
print('\n\nPR_dis_seg_dic:')
PR_Plot(PR_dis_seg_dic, detectResults_Path, 'seg') # TODO:max_or_seg
shutil.rmtree(TEMP_FILES_Path) # 删除临时文件夹及其下所有文件。
return MR_FPPI_dis_max_dic, MR_FPPI_dis_seg_dic,PR_dis_max_dic,PR_dis_seg_dic, AE_dtp_seg_dic, AE_dtp_max_dic,\
AE_DE_seg_dic, AE_DE_max_dic,AER_dtp_seg_dic, AER_dtp_max_dic,AER_DE_seg_dic, AER_DE_max_dic,\
annos_AER_bad, annos_FN, annos_FP
#TODO:*** 图像显示检测结果.***图像显示检测结果.***图像显示检测结果.***图像显示检测结果.***图像显示检测结果.***图像显示检测结果.***图像显示检测结果.
def visulize_FP_FN_bad_ER(imgs_data, dt_data_plot, imgs_dt_show_dir, annos_FP_Dis, annos_AER_bad_Dis, annos_miss_Ped_Dis, score_threshold):
txt_output_path = os.path.join(imgs_dt_show_dir, 'annos_GT_txt_FP_AER_bad_miss_Ped.txt')
annos_GT_txt = open(txt_output_path, 'w')
annos_GT_txt.write('path, x1, y1, x2, y2, Dis, Age, Dif, Sce, Wea, Occlusion, Ignore, pose, truncation, ID, cls\n')
#TODO: ***将标记写入记事本。 ***将标记写入记事本。 ***将标记写入记事本。 ***将标记写入记事本。 ***将标记写入记事本。
num_Bg_Img = 700000
cocoGt = COCO(cfg.annos_file)
images = imgs_data['images']
scenes = imgs_data['scenes']
weathers = imgs_data['weathers']
path_img_dt_FP_region_dir = os.path.join(imgs_dt_show_dir, 'visualize_FP')
if not os.path.exists(path_img_dt_FP_region_dir):
os.makedirs(path_img_dt_FP_region_dir)
path_img_dt_FP_region_dir_Bg = os.path.join(path_img_dt_FP_region_dir, 'visualize_FP_Bg')
if not os.path.exists(path_img_dt_FP_region_dir_Bg):
os.makedirs(path_img_dt_FP_region_dir_Bg)
path_img_dt_FP_region_dir_FP = os.path.join(path_img_dt_FP_region_dir, 'visualize_FP_FP')
if not os.path.exists(path_img_dt_FP_region_dir_FP):
os.makedirs(path_img_dt_FP_region_dir_FP)
path_img_dt_FP_region_dir_TP = os.path.join(path_img_dt_FP_region_dir, 'visualize_FP_TP')
if not os.path.exists(path_img_dt_FP_region_dir_TP):
os.makedirs(path_img_dt_FP_region_dir_TP)
path_img_dt_AER_bad_region_dir = os.path.join(imgs_dt_show_dir, 'visualize_AER_bad')
if not os.path.exists(path_img_dt_AER_bad_region_dir):
os.makedirs(path_img_dt_AER_bad_region_dir)
path_img_dt_AER_bad_region_dir_plus = os.path.join(path_img_dt_AER_bad_region_dir, 'visualize_AER_bad_Plus')
if not os.path.exists(path_img_dt_AER_bad_region_dir_plus):
os.makedirs(path_img_dt_AER_bad_region_dir_plus)
path_img_dt_AER_bad_region_dir_minus = os.path.join(path_img_dt_AER_bad_region_dir, 'visualize_AER_bad_minus')
if not os.path.exists(path_img_dt_AER_bad_region_dir_minus):
os.makedirs(path_img_dt_AER_bad_region_dir_minus)
path_img_dt_FN_region_dir = os.path.join(imgs_dt_show_dir, 'visualize_FN')
if not os.path.exists(path_img_dt_FN_region_dir):
os.makedirs(path_img_dt_FN_region_dir)
path_img_dt_FN_region_dir_Bg = os.path.join(path_img_dt_FN_region_dir, 'visualize_FN_Bg')
if not os.path.exists(path_img_dt_FN_region_dir_Bg):
os.makedirs(path_img_dt_FN_region_dir_Bg)
path_img_dt_FN_region_dir_FP = os.path.join(path_img_dt_FN_region_dir, 'visualize_FN_FP')
if not os.path.exists(path_img_dt_FN_region_dir_FP):
os.makedirs(path_img_dt_FN_region_dir_FP)
# img_data={'id': 100000, 'file_name': 'Data20181219200348_010000.png', 'height': 720, 'width': 1280,
# 'daytime': 'night', 'scenes_id': 2, 'weathers_id': 1, 'seasons_id': 0, 'recordings_id': 0, 'imageset': 'train'}
num_test_imgs = len(images)
for img_idx, img_data in enumerate(images):
img_name = img_data['file_name']
image_id = img_name.split('.', 1)[0]
print('\033[1;30;43m Process:%d / %d \033[0m:%s' % (img_idx, num_test_imgs, image_id))
try:
# img_path = os.path.join('E:\\Datasets\\NIRPed2021\\NIRPed\\images\\{}\\{}'.format(subset, img_name))
# img_path = '.\\data\\miniNIRPed\\images\\{}\\{}'.format(subset, img_name)
img_path = os.path.join(cfg.img_dir, img_name)
except:
continue
anno_ids = cocoGt.getAnnIds(imgIds=img_data['id']) # anno_id=1037542
annos_GT = cocoGt.loadAnns(ids=anno_ids)
'''annos_GT =[{'occluded': None, 'difficult': None, 'bbox': [453, 207, 30, 54], 'id': 1000007, 'category_id': 4, 'image_id': 1000043, 'pose_id': 5, 'tracking_id': 1000000, 'ignore': 1, 'area': 1620, 'truncated': False},
{'occluded': None, 'difficult': None, 'bbox': [514, 233, 95, 40], 'id': 1000010, 'category_id': 4, 'image_id': 1000043, 'pose_id': 5, 'tracking_id': 1000001, 'ignore': 1, 'area': 3800, 'truncated': False}]
'''
Wea = weathers[img_data['weathers_id']]['name']
Sce = scenes[img_data['scenes_id']]['name']
img_dt_results_path = os.path.join(imgs_dt_show_dir, image_id + '.png')
if os.path.exists(img_dt_results_path):
print('Notion:Detect results have shown for image {} .'.format(img_dt_results_path))
continue
dt_boxes = [box for box in dt_data_plot if box['file_id'] == image_id]
# dt_boxes = [{'class_name': 'Ped', 'confidence': '0.999998', 'file_id': 'Data20181219200348_020000', 'bbox': [448.0, 202.0, 576.0, 517.0], 'Dis': 13.91},
# {'class_name': 'Ped', 'confidence': '0.999998', 'file_id': 'Data20181219200348_020000', 'bbox': [448.0, 202.0, 576.0, 517.0], 'Dis': 13.91},
# {'class_name': 'Ped', 'confidence': '0.999997', 'file_id': 'Data20181219200348_020000', 'bbox': [32.0, 179.0, 160.0, 539.0], 'Dis': 12.74},
# {'class_name': 'Ped', 'confidence': '0.999997', 'file_id': 'Data20181219200348_020000', 'bbox': [32.0, 179.0, 160.0, 539.0], 'Dis': 12.74},
# {'class_name': 'Ped', 'confidence': '0.999964', 'file_id': 'Data20181219200348_020000', 'bbox': [208.0, 202.0, 336.0, 494.0], 'Dis': 15.45},
# {'class_name': 'Ped', 'confidence': '0.999964', 'file_id': 'Data20181219200348_020000', 'bbox': [208.0, 202.0, 336.0, 494.0], 'Dis': 15.45}]
if dt_boxes == []:
print('Notion:image {} detect no object. Continue!'.format(img_path))
#continue
try:
img = cv2.imread(img_path)
except:
print('Notion:do not find image: {}.'.format(img_path))
# TODO: ***将背景图片写入记事本。 ***将背景图片写入记事本。 ***将背景图片写入记事本。 ***将背景图片写入记事本。 ***将背景图片写入记事本。 ***将背景图片写入记事本。
if annos_GT == []:
annos_GT_txt.write(img_dt_results_path + ',' + '0,0,0,0' + ',' + '100' + ',' + '0,0,0,0' + ',' + 'Neu' + ',' + 'Esay' + ',' + Sce + ',' + Wea + ',' + str(num_Bg_Img) + ',' + 'Bg_Img' + '\n')
image_with_boxes =Image.fromarray(np.uint8(img))
draw = ImageDraw.Draw(image_with_boxes)
'''gt_data_temp=[{"class_name": "Ped", "bbox": [39, 191, 153, 525], "Dis": 13.4, "used": false, "Dif": false, "Occ_Coe": 0.0, "Age": "Adult", "area": 38076},
{"class_name": "Ped", "bbox": [434, 208, 569, 511], "Dis": 13.0, "used": false, "Dif": false, "Occ_Coe": 0.0, "Age": "Adult", "area": 40905},
{"class_name": "Ped", "bbox": [196, 217, 346, 517], "Dis": 13.2, "used": false, "Dif": false, "Occ_Coe": 0.0, "Age": "Adult", "area": 45000}]'''
# TODO: ***切片FP 检测结果。***切片FP 检测结果。***切片FP 检测结果。***切片FP 检测结果。***切片FP 检测结果。***切片FP 检测结果。***切片FP 检测结果。
# TODO: ***切片FP 检测结果。***切片FP 检测结果。***切片FP 检测结果。***切片FP 检测结果。***切片FP 检测结果。***切片FP 检测结果。***切片FP 检测结果。
if image_id in annos_FP_Dis:
annos_FP = annos_FP_Dis[image_id]
for index, anno in enumerate(annos_FP): #anno={'class_name': 'Ped', 'confidence': '0.999982', 'BB_dt': [240.0, 224.0, 288.0, 404.0], 'Dis_dt': 25.35}
b = anno['BB_dt']
left, top, right, bottom = int(b[0]), int(b[1]), int(b[2]), int(b[3])
#TODO:***切片显示FP。***切片显示FP。***切片显示FP。***切片显示FP。***切片显示FP。***切片显示FP。
ovmax = 0
'''annos_GT=[{'id': 10036185, 'category_id': 1, 'image_id': 107911, 'pose_id': 0, 'tracking_id': 100000, 'bbox': [294, 200, 68, 225], 'Dis': 19.2, 'vis_box': [294, 200, 68, 225],
'Occ_Coe': 0.0, 'Dif': False, 'Ign': 0, 'area': 15300, 'Tru': 0, 'Age': 'Adult'}, {'id': 10036186, 'category_id': 1, 'image_id': 107911, 'pose_id': 0, 'tracking_id': 100000,
'bbox': [343, 214, 61, 196], 'Dis': 21.1, 'vis_box': [343, 214, 61, 196], 'Occ_Coe': 0.0, 'Dif': False, 'Ign': 0, 'area': 11956, 'Tru': 0, 'Age': 'Adult'},
{'id': 10036187, 'category_id': 1, 'image_id': 107911, 'pose_id': 0, 'tracking_id': 100000, 'bbox': [255, 219, 49, 178], 'Dis': 23.3, 'vis_box': [255, 219, 49, 178],
'Occ_Coe': 0.0, 'Dif': False, 'Ign': 0, 'area': 8722, 'Tru': 0, 'Age': 'Adult'}, {'id': 10036188, 'category_id': 1, 'image_id': 107911, 'pose_id': 0, 'tracking_id': 100000,
'bbox': [491, 218, 59, 173], 'Dis': 25.0, 'vis_box': [491, 218, 59, 173], 'Occ_Coe': 0.0, 'Dif': False, 'Ign': 0, 'area': 10207, 'Tru': 0, 'Age': 'Adult'},
{'id': 10036189, 'category_id': 1, 'image_id': 107911, 'pose_id': 0, 'tracking_id': 100000, 'bbox': [619, 220, 78, 163], 'Dis': 27.0, 'vis_box': [619, 220, 78, 163],
'Occ_Coe': 0.0, 'Dif': False, 'Ign': 0, 'area': 12714, 'Tru': 0, 'Age': 'Adult'}]'''
for gt_box in annos_GT: # TODO: ***以标记框高度排序。
cat = cocoGt.loadCats(ids=gt_box['category_id'])[0] # cat={'name': 'pedestrian', 'id': 1}
class_name = cat['name']
if class_name not in ['Ped', 'ped', 'Pedestrian', 'pedestrian']:
continue
b_gt = gt_box['bbox']
x1, y1, x2, y2 = b_gt[0], b_gt[1], b_gt[0] + b_gt[2], b_gt[1] + b_gt[3]
bi = [max(left, x1), max(top, y1), min(right, x2), min(bottom, y2)] # 用于计算检测框和真实标记框的交集面积
iw = bi[2] - bi[0] + 1 # 计算交集宽
ih = bi[3] - bi[1] + 1 # 计算交集高
if iw > 0 and ih > 0: # 如果相交,才开始计算交并比 compute overlap (IoU) = area of intersection / area of union
ua = (right - left + 1) * (bottom - top + 1) + (x2 - x1 + 1) * (y2 - y1 + 1) - iw * ih # 计算并集面积
ov = iw * ih / ua # 计算交并比
if 0 < ov <= 1:
if ov > ovmax:
ovmax = ov
gt_match_box = gt_box # 用gt_match记录与当前检测框bb匹配的真实标记框gt_match = {'class_name': 'Bic', 'bbox': [1035, 229, 1156, 526],
else:
print('error overlap = %.1f%%' % ov * 100)
if ovmax >= 0.5:
if (gt_match_box['Dif'] or float(gt_match_box['Occ_Coe']) > cfg.Occ_threshold or float(gt_match_box['Dis']) >= cfg.Dis_threshold):
continue
if ovmax > 0.1:
dis_gt = gt_match_box['Dis']
b_gt = gt_match_box['bbox']
w = b_gt[2]
h = b_gt[3]
x1, y1, x2, y2 = b_gt[0], b_gt[1], b_gt[0] + w, b_gt[1] + h
w_edge = int(0.2 * w)
h_edge = int(0.12 * h)
'''annos_GT =[{'occluded': None, 'difficult': None, 'bbox': [453, 207, 30, 54], 'id': 1000007, 'category_id': 4, 'image_id': 1000043, 'pose_id': 5, 'tracking_id': 1000000, 'ignore': 1, 'area': 1620, 'truncated': False},
{'occluded': None, 'difficult': None, 'bbox': [514, 233, 95, 40], 'id': 1000010, 'category_id': 4, 'image_id': 1000043, 'pose_id': 5, 'tracking_id': 1000001, 'ignore': 1, 'area': 3800, 'truncated': False}]
'''
dt_match_box = -1
if ovmax >= 0.5:
ovmax2 = MINOVERLAP
# pdb.set_trace()
for dt_box in dt_boxes:
class_name_dt = dt_box['class_name']
if class_name_dt not in ['Ped', 'ped', 'Pedestrian', 'pedestrian']:
continue
b2 = dt_box['bbox']
if abs(b2[0]-left+b2[1]-top+b2[2]-right+b2[3]-bottom) < 10:
continue
left2, top2, right2, bottom2 = b2[0], b2[1], b2[2], b2[3]