DetectionMetricsFactory ignores predictions for unrepresented ground truth classes, skipping False Positives in mAP

Open Beginner friendly
#684 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 6 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
87/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Start in perceptionmetrics/utils/detection_metrics.py, at the zero-ground-truth guard in compute_metrics() (around lines 174-177) and the matching guard in compute_coco_map() (around lines 268-271). Change both so a class with predictions but no ground truth records AP, Precision and Recall as 0.0, TP 0, FP len(detections), FN 0, and appends 0.0 to ap_values. Add a test in tests/test_detection_metrics.py that feeds predictions for an absent class and asserts the metrics and that mAP includes the zero.

Written by the indexing model from the issue text.

Description

DetectionMetricsFactory ignores predictions for unrepresented ground truth classes, skipping False Positives in mAP

Description

In perceptionmetrics/utils/detection_metrics.py, DetectionMetricsFactory.compute_metrics() (lines 174-177) and compute_coco_map() (lines 268-271) iterate over accumulated class detections:

for label, detections in self.results.items():
    # Skip classes with no ground truth instances
    if self.gt_counts.get(int(label), 0) == 0:
        continue

When a model outputs predictions for a class that has zero ground truth instances in the evaluated dataset:

  1. In update(), all such predictions are correctly flagged as False Positives (score, 0) under self.results[p_label].
  2. However, because self.gt_counts.get(int(label), 0) == 0, compute_metrics() skips the class completely.
  3. As a result, compute_ap() is never executed for this class, Precision is reported as NaN (or omitted) instead of 0.0, and the 0.0 AP is excluded from ap_values.
  4. In compute_coco_map(), the identical if self.gt_counts.get(int(label), 0) == 0: continue check skips the class at every IoU threshold.

This means a model that hallucinates dozens of phantom bounding boxes for classes not present in the dataset receives no penalty in its mAP calculation.

Expected Behavior

If predictions exist for a class with zero ground truth boxes, those predictions are False Positives. The class should have Precision = 0.0, Recall = 0.0, AP = 0.0, and should penalize the overall mAP by including 0.0 in the mean AP.

Proposed Solution

In compute_metrics() and compute_coco_map(), handle classes with predictions but zero ground truth instances by recording AP = 0.0, Precision = 0.0, Recall = 0.0, TP = 0, FP = len(detections), FN = 0, and including 0.0 in ap_values:

if self.gt_counts.get(int(label), 0) == 0:
    if len(detections) > 0:
        metrics[label] = {
            "AP": 0.0,
            "Precision": 0.0,
            "Recall": 0.0,
            "TP": 0,
            "FP": len(detections),
            "FN": 0,
        }
        ap_values.append(0.0)
    continue

I can open a PR with this correction and a test case in tests/test_detection_metrics.py.

Dominant language
Python
Stars
116
Forks
117
Avg merge
2d 17h
Merged PRs (30d)
6

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JdeRobot/PerceptionMetrics

All issues in JdeRobot/PerceptionMetrics

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.