Sub topic 8
Mean Average Precision Across Classes
Prerequisites and the main idea
This chapter assumes that you know how to calculate average precision (AP) for one object class. You should also know how detections are matched to ground-truth boxes using an Intersection over Union (IoU) threshold.
A detector usually predicts several classes, such as person, car, and bicycle. Each class has its own ranked detections, ground-truth boxes, precision-recall curve, and AP value. Mean average precision (mAP) combines those per-class AP values into one score.
The word mean is important. Standard mAP gives every class equal weight, regardless of how many objects the class contains.
Per-class AP
Suppose a detector has these AP values at IoU 0.50:
| Class | AP at IoU 0.50 |
|---|---|
| person | 0.90 |
| car | 0.70 |
| bicycle | 0.50 |
The mAP is the arithmetic mean:
mAP_{50} = \frac{0.90 + 0.70 + 0.50}{3} = 0.70A class with many examples contributes no more than a class with few examples. This is called a macro average. It measures how well the detector performs across classes, rather than how well it performs on the average object.
This property helps expose weak performance on rare classes. A detector could perform very well on person and car while failing on bicycle. The bicycle AP still affects the final mAP by the same amount as every other class.
A simple mAP calculation
The following program calculates mAP from already-computed per-class AP values. It also reports the lowest and highest class scores, which helps explain the overall result.
def mean_average_precision(ap_by_class):
"""Return macro mAP and per-class summary values.
ap_by_class maps class names to AP values in the range [0, 1].
"""
if not ap_by_class:
raise ValueError("At least one class is required")
values = list(ap_by_class.values())
if any(not 0.0 <= ap <= 1.0 for ap in values):
raise ValueError("Every AP must be between 0 and 1")
mean_ap = sum(values) / len(values)
return {
"map": mean_ap,
"best_class": max(ap_by_class, key=ap_by_class.get),
"worst_class": min(ap_by_class, key=ap_by_class.get),
}
ap50 = {
"person": 0.90,
"car": 0.70,
"bicycle": 0.50,
}
summary = mean_average_precision(ap50)
print(f"mAP50: {summary['map']:.3f}")
print(f"Best class: {summary['best_class']}")
print(f"Worst class: {summary['worst_class']}")The output is:
mAP50: 0.700
Best class: person
Worst class: bicycleThe class names and AP values must come from the same evaluation dataset and protocol. Mixing AP values from different datasets, IoU thresholds, or matching rules produces a meaningless average.
Averaging across IoU thresholds
A detector can receive an AP value for each IoU threshold. For example, mAP50 uses IoU threshold 0.50. A stricter threshold such as 0.75 requires more accurate box placement.
COCO-style evaluation averages AP over ten thresholds:
0.50,\ 0.55,\ 0.60,\ \ldots,\ 0.95For each threshold, calculate AP for every class. Then average all of those values:
mAP_{50:95} =
\frac{1}{10C}
\sum_{t=1}^{10}
\sum_{c=1}^{C} AP_{c,t}Here, \(C\) is the number of evaluated classes. The notation mAP50:95 is often written as mAP@[.50:.95].
def map_over_thresholds(ap_by_threshold):
"""Average AP over classes and IoU thresholds.
Each dictionary value contains AP values for the same set of classes.
"""
if not ap_by_threshold:
raise ValueError("At least one threshold is required")
class_sets = [set(scores) for scores in ap_by_threshold.values()]
if any(classes != class_sets[0] for classes in class_sets[1:]):
raise ValueError("Every threshold must contain the same classes")
all_ap = [
ap
for scores in ap_by_threshold.values()
for ap in scores.values()
]
return sum(all_ap) / len(all_ap)
ap = {
0.50: {"person": 0.90, "car": 0.70, "bicycle": 0.50},
0.75: {"person": 0.70, "car": 0.55, "bicycle": 0.30},
}
print(f"mAP@[.50:.75]: {map_over_thresholds(ap):.3f}")This example returns 0.525. The stricter threshold lowers the score because the predicted boxes must align more closely with the ground truth.
Class imbalance and missing classes
Macro mAP is different from an object-weighted average. Suppose a dataset contains 10,000 person boxes and 20 bicycle boxes. A macro average still gives both classes equal weight. This prevents the common class from hiding poor rare-class performance.
However, a rare class can have an unstable AP. A few incorrect or missed examples may change its precision-recall curve substantially. Report per-class AP alongside mAP so that users can see this variation.
Do not silently include a class with no ground-truth instances. Its AP is undefined for that evaluation split. Either remove that class from the evaluated class set or report it explicitly as absent. If a benchmark defines a fixed class set, follow that benchmark's rule.
Exercise
A detector has AP values of 0.95 for a common class and 0.35 for a rare class. What is the macro mAP, and why might an object-weighted score tell a different story?
Show answer
The macro mAP is (0.95 + 0.35) / 2 = 0.65. Macro mAP gives both classes equal weight, while an object-weighted score gives much more influence to the common class.