Warning: result not cached.
(self)
| 272 | return len(self.data_points) == 0 and self.num_gt_positives == 0 |
| 273 | |
| 274 | def get_ap(self) -> float: |
| 275 | """ Warning: result not cached. """ |
| 276 | |
| 277 | if self.num_gt_positives == 0: |
| 278 | return 0 |
| 279 | |
| 280 | # Sort descending by score |
| 281 | self.data_points.sort(key=lambda x: -x[0]) |
| 282 | |
| 283 | precisions = [] |
| 284 | recalls = [] |
| 285 | num_true = 0 |
| 286 | num_false = 0 |
| 287 | |
| 288 | # Compute the precision-recall curve. The x axis is recalls and the y axis precisions. |
| 289 | for datum in self.data_points: |
| 290 | # datum[1] is whether the detection a true or false positive |
| 291 | if datum[1]: |
| 292 | num_true += 1 |
| 293 | else: |
| 294 | num_false += 1 |
| 295 | |
| 296 | precision = num_true / (num_true + num_false) |
| 297 | recall = num_true / self.num_gt_positives |
| 298 | |
| 299 | precisions.append(precision) |
| 300 | recalls.append(recall) |
| 301 | |
| 302 | # Smooth the curve by computing [max(precisions[i:]) for i in range(len(precisions))] |
| 303 | # Basically, remove any temporary dips from the curve. |
| 304 | # At least that's what I think, idk. COCOEval did it so I do too. |
| 305 | for i in range(len(precisions)-1, 0, -1): |
| 306 | if precisions[i] > precisions[i-1]: |
| 307 | precisions[i-1] = precisions[i] |
| 308 | |
| 309 | # Compute the integral of precision(recall) d_recall from recall=0->1 using fixed-length riemann summation with 101 bars. |
| 310 | # idx 0 is recall == 0.0 and idx 100 is recall == 1.00 |
| 311 | y_range = [0] * 101 |
| 312 | x_range = np.array([x / 100 for x in range(101)]) |
| 313 | recalls = np.array(recalls) |
| 314 | |
| 315 | # I realize this is weird, but all it does is find the nearest precision(x) for a given x in x_range. |
| 316 | # Basically, if the closest recall we have to 0.01 is 0.009 this sets precision(0.01) = precision(0.009). |
| 317 | # I approximate the integral this way, because that's how COCOEval does it. |
| 318 | indices = np.searchsorted(recalls, x_range, side='left') |
| 319 | for bar_idx, precision_idx in enumerate(indices): |
| 320 | if precision_idx < len(precisions): |
| 321 | y_range[bar_idx] = precisions[precision_idx] |
| 322 | |
| 323 | # Finally compute the riemann sum to get our integral. |
| 324 | # avg([precision(x) for x in 0:0.01:1]) |
| 325 | return sum(y_range) / len(y_range) |
| 326 | |
| 327 | def calc_map(ap_data): |
| 328 | print('Calculating mAP...') |