|
7 | 7 | import h5py |
8 | 8 | import json |
9 | 9 | import numpy as np |
10 | | -from sklearn.metrics import average_precision_score |
11 | 10 | import tensorflow as tf |
12 | 11 |
|
13 | 12 | import common |
|
52 | 51 | help='Batch size used during evaluation, adapt based on your memory usage.') |
53 | 52 |
|
54 | 53 |
|
| 54 | +def average_precision_score(y_true, y_score): |
| 55 | + """ Compute average precision (AP) from prediction scores. |
| 56 | +
|
| 57 | + This is a replacement for the scikit-learn version which, while likely more |
| 58 | + correct does not follow the same protocol as used in the default Market-1501 |
| 59 | + evaluation that first introduced this score to the person ReID field. |
| 60 | +
|
| 61 | + Args: |
| 62 | + y_true (array): The binary labels for all data points. |
| 63 | + y_score (array): The predicted scores for each samples for all data |
| 64 | + points. |
| 65 | +
|
| 66 | + Raises: |
| 67 | + ValueError if the length of the labels and scores do not match. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + A float representing the average precision given the predictions. |
| 71 | + """ |
| 72 | + |
| 73 | + if len(y_true) != len(y_score): |
| 74 | + raise ValueError('The length of the labels and predictions must match ' |
| 75 | + 'got lengths y_true:{} and y_score:{}'.format( |
| 76 | + len(y_true), len(y_score))) |
| 77 | + |
| 78 | + y_true_sorted = y_true[np.argsort(-y_score, kind='mergesort')] |
| 79 | + |
| 80 | + tp = np.cumsum(y_true_sorted) |
| 81 | + total_true = np.sum(y_true_sorted) |
| 82 | + recall = tp / total_true |
| 83 | + recall = np.insert(recall, 0, 0.) |
| 84 | + precision = tp / np.arange(1, len(tp) + 1) |
| 85 | + precision = np.insert(precision, 0, 1.) |
| 86 | + ap = np.sum(np.diff(recall) * ((precision[1:] + precision[:-1]) / 2)) |
| 87 | + |
| 88 | + return ap |
| 89 | + |
| 90 | + |
55 | 91 | def main(): |
56 | 92 | # Verify that parameters are set correctly. |
57 | 93 | args = parser.parse_args() |
|
0 commit comments