Warning: result not cached.
(self)
| 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 | |
| 571 | # I realize this is weird, but all it does is find the nearest precision(x) for a given x in x_range. |
| 572 | # Basically, if the closest recall we have to 0.01 is 0.009 this sets precision(0.01) = precision(0.009). |
| 573 | # I approximate the integral this way, because that's how COCOEval does it. |
| 574 | indices = np.searchsorted(recalls, x_range, side='left') |
| 575 | for bar_idx, precision_idx in enumerate(indices): |
| 576 | if precision_idx < len(precisions): |
| 577 | y_range[bar_idx] = precisions[precision_idx] |
| 578 | |
| 579 | # Finally compute the riemann sum to get our integral. |
| 580 | # avg([precision(x) for x in 0:0.01:1]) |
| 581 | return sum(y_range) / len(y_range) |
| 582 | |
| 583 | def badhash(x): |
| 584 | """ |