Mean average precision (mAP) is the most meaningful metric for object detectors, instance, and semantic segmentors. It incorporates the trade-off between precision and recall, and in doing so, takes into account both types of errors, false positives (FP) and false negatives (FN). This property makes mAP applicable for most use cases.
As the mAP is based on precision and recall, it also relies on the confusion matrix. For object detectors, instance, and semantic segmentors, the confusion matrix is computed by checking if the class of the prediction fits the class from the ground truth and checking if the IoU is above a certain threshold, most of the time above 0.5.
Average Precision (AP) is computed for one class
Don't let the term Average Precision fool you. It's not simply an average of a few precision values, but the result if you average out the Precision-Recall Curve for one class.
The Precision-Recall Curvedepicts the conflict between precision and recall. Typically,
models with high precision and low recall produce very confident predictions but miss a part of the instances.
models with low precision and high recall can find most objects, but the predictions are false positives to a certain degree, and confidence decreases.
It is constructed by computing the precision and recall values for different confidence thresholds. I.e., if the confidence threshold is at 0.9, only predictions where the classifier has a confidence of over 90% are counted as positive predictions. The precision will be pretty high. Vice versa, if the threshold is at 0.1, much more predictions will be included in the positive ones and recall will be large.
The Average Precision is the area under the curve. Perfect models would have an AP of 1.0.
Mean Average Precision (mAP) is computed for many classes
Once you know how to calculate the AP, computing the mAP is easy:
Often, you see the mAP cited as [email protected] or [email protected]. This notation depicts which IoU threshold has been used to calculate the confusion matrix. This is important to note because the IoU threshold can influence the mAP substantially: a low IoU will boost your mAP.
As mentioned above, more often than not, 0.5 is used as an IoU threshold.
Sometimes, for example, for the COCO dataset, the mAP is benchmarked by averaging it out over IoUs from [.5,.95] in .05 steps.
Object detection results on COCO
Research Paper by Yi Lin et al.
import numpy as np
def apk(actual, predicted, k=10):#Computes the average precision at k.
#This function computes the average prescision at k between two lists of items.
if not actual:
return score / min(len(actual), k)
def mapk(actual, predicted, k=10):#Computes the mean average precision at k.
#This function computes the mean average prescision at k between two lists of lists of items.
return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)])
!pip install tensorflow==1.15 #Make sure you have updated the Tensorflow version for tf.metrics.average_precision_at_k to work