Stores all the information necessary to calculate the AP for one IoU and one class. Note: I type annotated this because why not.
| 511 | |
| 512 | |
| 513 | class APDataObject: |
| 514 | """ |
| 515 | Stores all the information necessary to calculate the AP for one IoU and one class. |
| 516 | Note: I type annotated this because why not. |
| 517 | """ |
| 518 | |
| 519 | def __init__(self): |
| 520 | self.data_points = [] |
| 521 | self.num_gt_positives = 0 |
| 522 | |
| 523 | def push(self, score:float, is_true:bool): |
| 524 | self.data_points.append((score, is_true)) |
| 525 | |
| 526 | def add_gt_positives(self, num_positives:int): |
| 527 | """ Call this once per image. """ |
| 528 | self.num_gt_positives += num_positives |
| 529 | |
| 530 | def is_empty(self) -> bool: |
| 531 | return len(self.data_points) == 0 and self.num_gt_positives == 0 |
| 532 | |
| 533 | def get_ap(self) -> float: |
| 534 | """ Warning: result not cached. """ |
| 535 | |
| 536 | if self.num_gt_positives == 0: |
| 537 | return 0 |
| 538 | |
| 539 | # Sort descending by score |
| 540 | self.data_points.sort(key=lambda x: -x[0]) |
| 541 | |
| 542 | precisions = [] |
| 543 | recalls = [] |
| 544 | num_true = 0 |
| 545 | num_false = 0 |
| 546 | |
| 547 | # Compute the precision-recall curve. The x axis is recalls and the y axis precisions. |
| 548 | for datum in self.data_points: |
| 549 | # datum[1] is whether the detection a true or false positive |
| 550 | if datum[1]: num_true += 1 |
| 551 | else: num_false += 1 |
| 552 | |
| 553 | precision = num_true / (num_true + num_false) |
| 554 | recall = num_true / self.num_gt_positives |
| 555 | |
| 556 | precisions.append(precision) |
| 557 | recalls.append(recall) |
| 558 | |
| 559 | # Smooth the curve by computing [max(precisions[i:]) for i in range(len(precisions))] |
| 560 | # Basically, remove any temporary dips from the curve. |
| 561 | # At least that's what I think, idk. COCOEval did it so I do too. |
| 562 | for i in range(len(precisions)-1, 0, -1): |
| 563 | if precisions[i] > precisions[i-1]: |
| 564 | precisions[i-1] = precisions[i] |
| 565 | |
| 566 | # Compute the integral of precision(recall) d_recall from recall=0->1 using fixed-length riemann summation with 101 bars. |
| 567 | y_range = [0] * 101 # idx 0 is recall == 0.0 and idx 100 is recall == 1.00 |
| 568 | x_range = np.array([x / 100 for x in range(101)]) |
| 569 | recalls = np.array(recalls) |
| 570 |