Skip to content

Performance: Vectorize inner loop in _match_predictions using NumPy masking #569

Description

@Vo1denz

Problem

_match_predictions in perceptionmetrics/utils/detection_metrics.py computes
the IoU matrix in one fast NumPy call, but then reads it element by element
inside a Python inner loop over GT boxes. With N predictions and M GT boxes,
this results in N×M Python iterations per image, which becomes a significant
bottleneck at scale.

Fix

  • Replaced the inner loop with NumPy boolean masking + np.argmax
  • Added confidence-score-based sorting of predictions before matching
    (standard mAP practice, high-confidence predictions get priority)
  • Kept used_mask as a NumPy boolean array in sync with the used set
    for fast masking

Testing

All 9 detection metric tests pass. Full suite: 44 passed, 2 failed
(pre-existing Open3D compatibility failures unrelated to this change).

I'd be happy to open a PR if this approach looks good to you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions