-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsgg_eval.py
638 lines (530 loc) · 28.2 KB
/
sgg_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
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
import logging
import os
import torch
import numpy as np
import json
from tqdm import tqdm
from functools import reduce
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from maskrcnn_benchmark.data import get_dataset_statistics
from maskrcnn_benchmark.structures.bounding_box import BoxList
from maskrcnn_benchmark.structures.boxlist_ops import boxlist_iou
from maskrcnn_benchmark.utils.miscellaneous import intersect_2d, argsort_desc, bbox_overlaps
from abc import ABC, abstractmethod
class SceneGraphEvaluation(ABC):
def __init__(self, result_dict):
super().__init__()
self.result_dict = result_dict
@abstractmethod
def register_container(self, mode):
print("Register Result Container")
pass
@abstractmethod
def generate_print_string(self, mode):
print("Generate Print String")
pass
"""
Traditional Recall, implement based on:
https://github.com/rowanz/neural-motifs
"""
class SGRecall(SceneGraphEvaluation):
def __init__(self, result_dict):
super(SGRecall, self).__init__(result_dict)
self.l21_thr=None
def register_container(self, mode):
self.result_dict[mode + '_recall'] = {20: [], 50: [], 100: []}
def generate_print_string(self, mode):
result_str = 'SGG eval: '
for k, v in self.result_dict[mode + '_recall'].items():
result_str += ' R @ %d: %.4f; ' % (k, np.mean(v))
result_str += ' for mode=%s, type=Recall(Main).' % mode
result_str += '\n'
return result_str
def calculate_recall(self, global_container, local_container, mode):
pred_rel_inds = local_container['pred_rel_inds']
rel_scores = local_container['rel_scores']
gt_rels = local_container['gt_rels']
gt_classes = local_container['gt_classes']
gt_boxes = local_container['gt_boxes']
pred_classes = local_container['pred_classes']
pred_boxes = local_container['pred_boxes']
obj_scores = local_container['obj_scores']
iou_thres = global_container['iou_thres']
pred_rels = np.column_stack((pred_rel_inds, 1+rel_scores[:,1:].argmax(1)))
pred_scores = rel_scores[:,1:].max(1)
if self.l21_thr is not None:
pred_rels, pred_scores = rel_nms(pred_boxes, pred_classes, pred_rel_inds, rel_scores, 0.5, self.l21_thr)
pred_rels = np.column_stack((pred_rel_inds, pred_rels))
sort_idx = np.argsort(-(pred_scores*obj_scores[pred_rel_inds[:,0]]*obj_scores[pred_rel_inds[:,0]]))
pred_rels = pred_rels[sort_idx]
pred_scores = pred_scores[sort_idx]
gt_triplets, gt_triplet_boxes, _ = _triplet(gt_rels, gt_classes, gt_boxes)
local_container['gt_triplets'] = gt_triplets
local_container['gt_triplet_boxes'] = gt_triplet_boxes
pred_triplets, pred_triplet_boxes, pred_triplet_scores = _triplet(
pred_rels, pred_classes, pred_boxes, pred_scores, obj_scores)
# Compute recall. It's most efficient to match once and then do recall after
pred_to_gt = _compute_pred_matches(
gt_triplets,
pred_triplets,
gt_triplet_boxes,
pred_triplet_boxes,
iou_thres,
phrdet=mode=='phrdet',
)
local_container['pred_to_gt'] = pred_to_gt
for k in self.result_dict[mode + '_recall']:
# the following code are copied from Neural-MOTIFS
match = reduce(np.union1d, pred_to_gt[:k])
rec_i = float(len(match)) / float(gt_rels.shape[0])
self.result_dict[mode + '_recall'][k].append(rec_i)
return local_container
"""
No Graph Constraint Recall, implement based on:
https://github.com/rowanz/neural-motifs
"""
class SGNoGraphConstraintRecall(SceneGraphEvaluation):
def __init__(self, result_dict):
super(SGNoGraphConstraintRecall, self).__init__(result_dict)
def register_container(self, mode):
self.result_dict[mode + '_recall_nogc'] = {20: [], 50: [], 100: []}
def generate_print_string(self, mode):
result_str = 'SGG eval: '
for k, v in self.result_dict[mode + '_recall_nogc'].items():
result_str += ' ng-R @ %d: %.4f; ' % (k, np.mean(v))
result_str += ' for mode=%s, type=No Graph Constraint Recall(Main).' % mode
result_str += '\n'
return result_str
def calculate_recall(self, global_container, local_container, mode):
obj_scores = local_container['obj_scores']
pred_rel_inds = local_container['pred_rel_inds']
rel_scores = local_container['rel_scores']
pred_boxes = local_container['pred_boxes']
pred_classes = local_container['pred_classes']
gt_rels = local_container['gt_rels']
obj_scores_per_rel = obj_scores[pred_rel_inds].prod(1)
nogc_overall_scores = obj_scores_per_rel[:,None] * rel_scores[:,1:]
nogc_score_inds = argsort_desc(nogc_overall_scores)[:100]
nogc_pred_rels = np.column_stack((pred_rel_inds[nogc_score_inds[:,0]], nogc_score_inds[:,1]+1))
nogc_pred_scores = rel_scores[nogc_score_inds[:,0], nogc_score_inds[:,1]+1]
nogc_pred_triplets, nogc_pred_triplet_boxes, _ = _triplet(
nogc_pred_rels, pred_classes, pred_boxes, nogc_pred_scores, obj_scores
)
# No Graph Constraint
gt_triplets = local_container['gt_triplets']
gt_triplet_boxes = local_container['gt_triplet_boxes']
iou_thres = global_container['iou_thres']
nogc_pred_to_gt = _compute_pred_matches(
gt_triplets,
nogc_pred_triplets,
gt_triplet_boxes,
nogc_pred_triplet_boxes,
iou_thres,
phrdet=mode=='phrdet',
)
local_container['nogc_pred_to_gt'] = nogc_pred_to_gt
for k in self.result_dict[mode + '_recall_nogc']:
match = reduce(np.union1d, nogc_pred_to_gt[:k])
rec_i = float(len(match)) / float(gt_rels.shape[0])
self.result_dict[mode + '_recall_nogc'][k].append(rec_i)
return local_container
"""
Zero Shot Scene Graph
Only calculate the triplet that not occurred in the training set
"""
class SGZeroShotRecall(SceneGraphEvaluation):
def __init__(self, result_dict):
super(SGZeroShotRecall, self).__init__(result_dict)
def register_container(self, mode):
self.result_dict[mode + '_zeroshot_recall'] = {20: [], 50: [], 100: []}
def generate_print_string(self, mode):
result_str = 'SGG eval: '
for k, v in self.result_dict[mode + '_zeroshot_recall'].items():
result_str += ' zR @ %d: %.4f; ' % (k, np.mean(v))
result_str += ' for mode=%s, type=Zero Shot Recall.' % mode
result_str += '\n'
return result_str
def prepare_zeroshot(self, global_container, local_container):
gt_rels = local_container['gt_rels']
gt_classes = local_container['gt_classes']
zeroshot_triplets = global_container['zeroshot_triplet']
sub_id, ob_id, pred_label = gt_rels[:, 0], gt_rels[:, 1], gt_rels[:, 2]
gt_triplets = np.column_stack((gt_classes[sub_id], gt_classes[ob_id], pred_label)) # num_rel, 3
self.zeroshot_idx = np.where( intersect_2d(gt_triplets, zeroshot_triplets).sum(-1) > 0 )[0].tolist()
def calculate_recall(self, global_container, local_container, mode):
pred_to_gt = local_container['pred_to_gt']
for k in self.result_dict[mode + '_zeroshot_recall']:
# Zero Shot Recall
match = reduce(np.union1d, pred_to_gt[:k])
if len(self.zeroshot_idx) > 0:
if not isinstance(match, (list, tuple)):
match_list = match.tolist()
else:
match_list = match
zeroshot_match = len(self.zeroshot_idx) + len(match_list) - len(set(self.zeroshot_idx + match_list))
zero_rec_i = float(zeroshot_match) / float(len(self.zeroshot_idx))
self.result_dict[mode + '_zeroshot_recall'][k].append(zero_rec_i)
"""
No Graph Constraint Mean Recall
"""
class SGNGZeroShotRecall(SceneGraphEvaluation):
def __init__(self, result_dict):
super(SGNGZeroShotRecall, self).__init__(result_dict)
def register_container(self, mode):
self.result_dict[mode + '_ng_zeroshot_recall'] = {20: [], 50: [], 100: []}
def generate_print_string(self, mode):
result_str = 'SGG eval: '
for k, v in self.result_dict[mode + '_ng_zeroshot_recall'].items():
result_str += 'ng-zR @ %d: %.4f; ' % (k, np.mean(v))
result_str += ' for mode=%s, type=No Graph Constraint Zero Shot Recall.' % mode
result_str += '\n'
return result_str
def prepare_zeroshot(self, global_container, local_container):
gt_rels = local_container['gt_rels']
gt_classes = local_container['gt_classes']
zeroshot_triplets = global_container['zeroshot_triplet']
sub_id, ob_id, pred_label = gt_rels[:, 0], gt_rels[:, 1], gt_rels[:, 2]
gt_triplets = np.column_stack((gt_classes[sub_id], gt_classes[ob_id], pred_label)) # num_rel, 3
self.zeroshot_idx = np.where( intersect_2d(gt_triplets, zeroshot_triplets).sum(-1) > 0 )[0].tolist()
def calculate_recall(self, global_container, local_container, mode):
pred_to_gt = local_container['nogc_pred_to_gt']
for k in self.result_dict[mode + '_ng_zeroshot_recall']:
# Zero Shot Recall
match = reduce(np.union1d, pred_to_gt[:k])
if len(self.zeroshot_idx) > 0:
if not isinstance(match, (list, tuple)):
match_list = match.tolist()
else:
match_list = match
zeroshot_match = len(self.zeroshot_idx) + len(match_list) - len(set(self.zeroshot_idx + match_list))
zero_rec_i = float(zeroshot_match) / float(len(self.zeroshot_idx))
self.result_dict[mode + '_ng_zeroshot_recall'][k].append(zero_rec_i)
"""
Give Ground Truth Object-Subject Pairs
Calculate Recall for SG-Cls and Pred-Cls
Only used in https://github.com/NVIDIA/ContrastiveLosses4VRD for sgcls and predcls
"""
class SGPairAccuracy(SceneGraphEvaluation):
def __init__(self, result_dict):
super(SGPairAccuracy, self).__init__(result_dict)
def register_container(self, mode):
self.result_dict[mode + '_accuracy_hit'] = {20: [], 50: [], 100: []}
self.result_dict[mode + '_accuracy_count'] = {20: [], 50: [], 100: []}
def generate_print_string(self, mode):
result_str = 'SGG eval: '
for k, v in self.result_dict[mode + '_accuracy_hit'].items():
a_hit = np.mean(v)
a_count = np.mean(self.result_dict[mode + '_accuracy_count'][k])
result_str += ' A @ %d: %.4f; ' % (k, a_hit/a_count)
result_str += ' for mode=%s, type=TopK Accuracy.' % mode
result_str += '\n'
return result_str
def prepare_gtpair(self, local_container):
pred_pair_idx = local_container['pred_rel_inds'][:, 0] * 1024 + local_container['pred_rel_inds'][:, 1]
gt_pair_idx = local_container['gt_rels'][:, 0] * 1024 + local_container['gt_rels'][:, 1]
self.pred_pair_in_gt = (pred_pair_idx[:, None] == gt_pair_idx[None, :]).sum(-1) > 0
def calculate_recall(self, global_container, local_container, mode):
pred_to_gt = local_container['pred_to_gt']
gt_rels = local_container['gt_rels']
for k in self.result_dict[mode + '_accuracy_hit']:
# to calculate accuracy, only consider those gt pairs
# This metric is used by "Graphical Contrastive Losses for Scene Graph Parsing"
# for sgcls and predcls
if mode != 'sgdet':
gt_pair_pred_to_gt = []
for p, flag in zip(pred_to_gt, self.pred_pair_in_gt):
if flag:
gt_pair_pred_to_gt.append(p)
if len(gt_pair_pred_to_gt) > 0:
gt_pair_match = reduce(np.union1d, gt_pair_pred_to_gt[:k])
else:
gt_pair_match = []
self.result_dict[mode + '_accuracy_hit'][k].append(float(len(gt_pair_match)))
self.result_dict[mode + '_accuracy_count'][k].append(float(gt_rels.shape[0]))
"""
Mean Recall: Proposed in:
https://arxiv.org/pdf/1812.01880.pdf CVPR, 2019
"""
class SGMeanRecall(SceneGraphEvaluation):
def __init__(self, result_dict, num_rel, ind_to_predicates, print_detail=False):
super(SGMeanRecall, self).__init__(result_dict)
self.num_rel = num_rel
self.print_detail = print_detail
self.rel_name_list = ind_to_predicates[1:] # remove __background__
def register_container(self, mode):
#self.result_dict[mode + '_recall_hit'] = {20: [0]*self.num_rel, 50: [0]*self.num_rel, 100: [0]*self.num_rel}
#self.result_dict[mode + '_recall_count'] = {20: [0]*self.num_rel, 50: [0]*self.num_rel, 100: [0]*self.num_rel}
self.result_dict[mode + '_mean_recall'] = {20: 0.0, 50: 0.0, 100: 0.0}
self.result_dict[mode + '_mean_recall_collect'] = {20: [[] for i in range(self.num_rel)], 50: [[] for i in range(self.num_rel)], 100: [[] for i in range(self.num_rel)]}
self.result_dict[mode + '_mean_recall_list'] = {20: [], 50: [], 100: []}
def generate_print_string(self, mode):
result_str = 'SGG eval: '
for k, v in self.result_dict[mode + '_mean_recall'].items():
result_str += ' mR @ %d: %.4f; ' % (k, float(v))
result_str += ' for mode=%s, type=Mean Recall.' % mode
result_str += '\n'
if self.print_detail:
result_str += '----------------------- Details ------------------------\n'
for n, r in zip(self.rel_name_list, self.result_dict[mode + '_mean_recall_list'][100]):
result_str += '({}:{:.4f}) '.format(str(n), r)
result_str += '\n'
result_str += '--------------------------------------------------------\n'
return result_str
def collect_mean_recall_items(self, global_container, local_container, mode):
pred_to_gt = local_container['pred_to_gt']
gt_rels = local_container['gt_rels']
for k in self.result_dict[mode + '_mean_recall_collect']:
# the following code are copied from Neural-MOTIFS
match = reduce(np.union1d, pred_to_gt[:k])
# NOTE: by kaihua, calculate Mean Recall for each category independently
# this metric is proposed by: CVPR 2019 oral paper "Learning to Compose Dynamic Tree Structures for Visual Contexts"
recall_hit = [0] * self.num_rel
recall_count = [0] * self.num_rel
for idx in range(gt_rels.shape[0]):
local_label = gt_rels[idx,2]
recall_count[int(local_label)] += 1
recall_count[0] += 1
for idx in range(len(match)):
local_label = gt_rels[int(match[idx]),2]
recall_hit[int(local_label)] += 1
recall_hit[0] += 1
for n in range(self.num_rel):
if recall_count[n] > 0:
self.result_dict[mode + '_mean_recall_collect'][k][n].append(float(recall_hit[n] / recall_count[n]))
def calculate_mean_recall(self, mode):
for k, v in self.result_dict[mode + '_mean_recall'].items():
sum_recall = 0
num_rel_no_bg = self.num_rel - 1
for idx in range(num_rel_no_bg):
if len(self.result_dict[mode + '_mean_recall_collect'][k][idx+1]) == 0:
tmp_recall = 0.0
else:
tmp_recall = np.mean(self.result_dict[mode + '_mean_recall_collect'][k][idx+1])
self.result_dict[mode + '_mean_recall_list'][k].append(tmp_recall)
sum_recall += tmp_recall
self.result_dict[mode + '_mean_recall'][k] = sum_recall / float(num_rel_no_bg)
return
"""
Mean Recall: Proposed in:
https://arxiv.org/pdf/1812.01880.pdf CVPR, 2019
"""
class SGMeanMicroRecall(SceneGraphEvaluation):
def __init__(self, result_dict, num_rel, ind_to_predicates, print_detail=False):
super(SGMeanMicroRecall, self).__init__(result_dict)
self.num_rel = num_rel
self.print_detail = print_detail
self.rel_name_list = ind_to_predicates[1:] # remove __background__
def register_container(self, mode):
#self.result_dict[mode + '_recall_hit'] = {20: [0]*self.num_rel, 50: [0]*self.num_rel, 100: [0]*self.num_rel}
#self.result_dict[mode + '_recall_count'] = {20: [0]*self.num_rel, 50: [0]*self.num_rel, 100: [0]*self.num_rel}
self.result_dict[mode + '_mean_micro_recall'] = {20: 0.0, 50: 0.0, 100: 0.0}
self.result_dict[mode + '_mean_micro_recall_collect_hit'] = {20: [0.0 for i in range(self.num_rel)], 50: [0.0 for i in range(self.num_rel)], 100: [0.0 for i in range(self.num_rel)]}
self.result_dict[mode + '_mean_micro_recall_collect_gt'] = {20: [0.0 for i in range(self.num_rel)], 50: [0.0 for i in range(self.num_rel)], 100: [0.0 for i in range(self.num_rel)]}
self.result_dict[mode + '_mean_micro_recall_list'] = {20: [], 50: [], 100: []}
def generate_print_string(self, mode):
result_str = 'SGG eval: '
for k, v in self.result_dict[mode + '_mean_micro_recall'].items():
result_str += ' mR @ %d: %.4f; ' % (k, float(v))
result_str += ' for mode=%s, type=Mean Micro Recall.' % mode
result_str += '\n'
if self.print_detail:
result_str += '----------------------- Details ------------------------\n'
for n, r in zip(self.rel_name_list, self.result_dict[mode + '_mean_micro_recall_list'][100]):
result_str += '({}:{:.4f}) '.format(str(n), r)
result_str += '\n'
result_str += '--------------------------------------------------------\n'
return result_str
def collect_mean_recall_items(self, global_container, local_container, mode):
pred_to_gt = local_container['pred_to_gt']
gt_rels = local_container['gt_rels']
for k in self.result_dict[mode + '_mean_micro_recall_collect_hit']:
# the following code are copied from Neural-MOTIFS
match = reduce(np.union1d, pred_to_gt[:k])
# NOTE: by kaihua, calculate Mean Recall for each category independently
# this metric is proposed by: CVPR 2019 oral paper "Learning to Compose Dynamic Tree Structures for Visual Contexts"
recall_hit = self.result_dict[mode + '_mean_micro_recall_collect_hit'][k]
recall_count = self.result_dict[mode + '_mean_micro_recall_collect_gt'][k]
for idx in range(gt_rels.shape[0]):
local_label = gt_rels[idx,2]
recall_count[int(local_label)] += 1
recall_count[0] += 1
for idx in range(len(match)):
local_label = gt_rels[int(match[idx]),2]
recall_hit[int(local_label)] += 1
recall_hit[0] += 1
def calculate_mean_recall(self, mode):
for k, v in self.result_dict[mode + '_mean_micro_recall'].items():
sum_recall = 0
num_rel_no_bg = self.num_rel - 1
for idx in range(num_rel_no_bg):
if self.result_dict[mode + '_mean_micro_recall_collect_hit'][k][idx+1] == 0.0:
tmp_recall = 0.0
else:
tmp_recall = self.result_dict[mode + '_mean_micro_recall_collect_hit'][k][idx+1]/self.result_dict[mode + '_mean_micro_recall_collect_gt'][k][idx+1]
self.result_dict[mode + '_mean_micro_recall_list'][k].append(tmp_recall)
sum_recall += tmp_recall
self.result_dict[mode + '_mean_micro_recall'][k] = sum_recall / float(num_rel_no_bg)
return
"""
No Graph Constraint Mean Recall
"""
class SGNGMeanRecall(SceneGraphEvaluation):
def __init__(self, result_dict, num_rel, ind_to_predicates, print_detail=False):
super(SGNGMeanRecall, self).__init__(result_dict)
self.num_rel = num_rel
self.print_detail = print_detail
self.rel_name_list = ind_to_predicates[1:] # remove __background__
def register_container(self, mode):
self.result_dict[mode + '_ng_mean_recall'] = {20: 0.0, 50: 0.0, 100: 0.0}
self.result_dict[mode + '_ng_mean_recall_collect'] = {20: [[] for i in range(self.num_rel)], 50: [[] for i in range(self.num_rel)], 100: [[] for i in range(self.num_rel)]}
self.result_dict[mode + '_ng_mean_recall_list'] = {20: [], 50: [], 100: []}
def generate_print_string(self, mode):
result_str = 'SGG eval: '
for k, v in self.result_dict[mode + '_ng_mean_recall'].items():
result_str += 'ng-mR @ %d: %.4f; ' % (k, float(v))
result_str += ' for mode=%s, type=No Graph Constraint Mean Recall.' % mode
result_str += '\n'
if self.print_detail:
result_str += '----------------------- Details ------------------------\n'
for n, r in zip(self.rel_name_list, self.result_dict[mode + '_ng_mean_recall_list'][100]):
result_str += '({}:{:.4f}) '.format(str(n), r)
result_str += '\n'
result_str += '--------------------------------------------------------\n'
return result_str
def collect_mean_recall_items(self, global_container, local_container, mode):
pred_to_gt = local_container['nogc_pred_to_gt']
gt_rels = local_container['gt_rels']
for k in self.result_dict[mode + '_ng_mean_recall_collect']:
# the following code are copied from Neural-MOTIFS
match = reduce(np.union1d, pred_to_gt[:k])
# NOTE: by kaihua, calculate Mean Recall for each category independently
# this metric is proposed by: CVPR 2019 oral paper "Learning to Compose Dynamic Tree Structures for Visual Contexts"
recall_hit = [0] * self.num_rel
recall_count = [0] * self.num_rel
for idx in range(gt_rels.shape[0]):
local_label = gt_rels[idx,2]
recall_count[int(local_label)] += 1
recall_count[0] += 1
for idx in range(len(match)):
local_label = gt_rels[int(match[idx]),2]
recall_hit[int(local_label)] += 1
recall_hit[0] += 1
for n in range(self.num_rel):
if recall_count[n] > 0:
self.result_dict[mode + '_ng_mean_recall_collect'][k][n].append(float(recall_hit[n] / recall_count[n]))
def calculate_mean_recall(self, mode):
for k, v in self.result_dict[mode + '_ng_mean_recall'].items():
sum_recall = 0
num_rel_no_bg = self.num_rel - 1
for idx in range(num_rel_no_bg):
if len(self.result_dict[mode + '_ng_mean_recall_collect'][k][idx+1]) == 0:
tmp_recall = 0.0
else:
tmp_recall = np.mean(self.result_dict[mode + '_ng_mean_recall_collect'][k][idx+1])
self.result_dict[mode + '_ng_mean_recall_list'][k].append(tmp_recall)
sum_recall += tmp_recall
self.result_dict[mode + '_ng_mean_recall'][k] = sum_recall / float(num_rel_no_bg)
return
"""
Accumulate Recall:
calculate recall on the whole dataset instead of each image
"""
class SGAccumulateRecall(SceneGraphEvaluation):
def __init__(self, result_dict):
super(SGAccumulateRecall, self).__init__(result_dict)
def register_container(self, mode):
self.result_dict[mode + '_accumulate_recall'] = {20: 0.0, 50: 0.0, 100: 0.0}
def generate_print_string(self, mode):
result_str = 'SGG eval: '
for k, v in self.result_dict[mode + '_accumulate_recall'].items():
result_str += ' aR @ %d: %.4f; ' % (k, float(v))
result_str += ' for mode=%s, type=Accumulate Recall.' % mode
result_str += '\n'
return result_str
def calculate_accumulate(self, mode):
for k, v in self.result_dict[mode + '_accumulate_recall'].items():
self.result_dict[mode + '_accumulate_recall'][k] = float(self.result_dict[mode + '_recall_hit'][k][0]) / float(self.result_dict[mode + '_recall_count'][k][0] + 1e-10)
return
def _triplet(relations, classes, boxes, predicate_scores=None, class_scores=None):
"""
format relations of (sub_id, ob_id, pred_label) into triplets of (sub_label, pred_label, ob_label)
Parameters:
relations (#rel, 3) : (sub_id, ob_id, pred_label)
classes (#objs, ) : class labels of objects
boxes (#objs, 4)
predicate_scores (#rel, ) : scores for each predicate
class_scores (#objs, ) : scores for each object
Returns:
triplets (#rel, 3) : (sub_label, pred_label, ob_label)
triplets_boxes (#rel, 8) array of boxes for the parts
triplets_scores (#rel, 3) : (sub_score, pred_score, ob_score)
"""
sub_id, ob_id, pred_label = relations[:, 0], relations[:, 1], relations[:, 2]
triplets = np.column_stack((classes[sub_id], pred_label, classes[ob_id]))
triplet_boxes = np.column_stack((boxes[sub_id], boxes[ob_id]))
triplet_scores = None
if predicate_scores is not None and class_scores is not None:
triplet_scores = np.column_stack((
class_scores[sub_id], predicate_scores, class_scores[ob_id],
))
return triplets, triplet_boxes, triplet_scores
def _compute_pred_matches(gt_triplets, pred_triplets,
gt_boxes, pred_boxes, iou_thres, phrdet=False):
"""
Given a set of predicted triplets, return the list of matching GT's for each of the
given predictions
Return:
pred_to_gt [List of List]
"""
# This performs a matrix multiplication-esque thing between the two arrays
# Instead of summing, we want the equality, so we reduce in that way
# The rows correspond to GT triplets, columns to pred triplets
keeps = intersect_2d(gt_triplets, pred_triplets)
gt_has_match = keeps.any(1)
pred_to_gt = [[] for x in range(pred_boxes.shape[0])]
for gt_ind, gt_box, keep_inds in zip(np.where(gt_has_match)[0],
gt_boxes[gt_has_match],
keeps[gt_has_match],
):
boxes = pred_boxes[keep_inds]
if phrdet:
# Evaluate where the union box > 0.5
gt_box_union = gt_box.reshape((2, 4))
gt_box_union = np.concatenate((gt_box_union.min(0)[:2], gt_box_union.max(0)[2:]), 0)
box_union = boxes.reshape((-1, 2, 4))
box_union = np.concatenate((box_union.min(1)[:,:2], box_union.max(1)[:,2:]), 1)
inds = bbox_overlaps(gt_box_union[None], box_union)[0] >= iou_thres
else:
sub_iou = bbox_overlaps(gt_box[None,:4], boxes[:, :4])[0]
obj_iou = bbox_overlaps(gt_box[None,4:], boxes[:, 4:])[0]
inds = (sub_iou >= iou_thres) & (obj_iou >= iou_thres)
for i in np.where(keep_inds)[0][inds]:
pred_to_gt[i].append(int(gt_ind))
return pred_to_gt
"""
Based on the per-class NMS: Proposed in:
https://arxiv.org/abs/1711.06640 CVPR, 2018
"""
def rel_nms(pred_boxes, pred_classes, pred_rel_inds, rel_scores, nms_thresh=0.5, l21_thr=0.7):
ious = bbox_overlaps(pred_boxes, pred_boxes)
sub_ious = ious[pred_rel_inds[:,0]][:,pred_rel_inds[:,0]]
obj_ious = ious[pred_rel_inds[:,1]][:,pred_rel_inds[:,1]]
rel_ious = np.minimum(sub_ious, obj_ious)
sub_labels = pred_classes[pred_rel_inds[:,0]]
obj_labels = pred_classes[pred_rel_inds[:,1]]
l21 = np.sqrt((np.power(rel_scores[:,None,:],2.0)+np.power(rel_scores[None,:,:],2.0))).sum(axis=-1)
is_overlap = (rel_ious>=nms_thresh)&(sub_labels[:,None]==sub_labels[None,:])&(obj_labels[:,None]==obj_labels[None,:])&(l21 > l21_thr)
is_overlap = is_overlap[:,:,None].repeat(rel_scores.shape[1],axis=2)
rel_scores_cp = rel_scores.copy()
rel_scores_cp[:,0]=0.
pred_rels = np.zeros(rel_scores_cp.shape[0], dtype=np.int64)
for i in range(rel_scores_cp.shape[0]):
box_ind, cls_ind = np.unravel_index(rel_scores_cp.argmax(), rel_scores_cp.shape)
if float(pred_rels[int(box_ind)]) > 0:
pass
else:
pred_rels[int(box_ind)]=int(cls_ind)
rel_scores_cp[is_overlap[box_ind,:,cls_ind],cls_ind]=0.0
rel_scores_cp[box_ind] = -1.
return pred_rels, rel_scores[np.arange(pred_rels.shape[0],dtype=np.int64), pred_rels]