Skip to content

Add OBB support to ConfusionMatrix via MetricTarget#2247

Open
Khanz9664 wants to merge 6 commits into
roboflow:developfrom
Khanz9664:feat/obb-confusion-matrix
Open

Add OBB support to ConfusionMatrix via MetricTarget#2247
Khanz9664 wants to merge 6 commits into
roboflow:developfrom
Khanz9664:feat/obb-confusion-matrix

Conversation

@Khanz9664
Copy link
Copy Markdown

Before submitting
  • Self-reviewed the code
  • Updated documentation, following Google-style docstrings
  • Added docs entry for autogeneration (not applicable – no new public functions/classes introduced)
  • Added/updated tests
  • All tests pass locally

Description

Adds Oriented Bounding Box (OBB) support to ConfusionMatrix by aligning it with the existing MetricTarget pattern used across other metrics.

Currently, ConfusionMatrix computes IoU using box_iou_batch, meaning OBB detections are effectively evaluated using derived axis-aligned bounding boxes (AABB). This PR enables correct OBB evaluation through oriented_box_iou_batch.

The implementation preserves full backward compatibility for existing AABB workflows and introduces explicit handling for unsupported MetricTarget.MASKS.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 🧪 Test update
  • 📝 Documentation update
  • 🔨 Refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🔧 Chore (dependencies, configs, etc.)

Motivation and Context

ConfusionMatrix currently lacks OBB support, even though other metrics (e.g., Precision, Recall, F1 Score) already support MetricTarget.ORIENTED_BOUNDING_BOXES.

As a result, users working with OBB detections may assume IoU is computed using rotated boxes, while it is actually computed using derived AABB coordinates via box_iou_batch.

This PR aligns ConfusionMatrix with the existing metric architecture and ensures correct OBB evaluation.

Closes #1760

Changes Made

  • Added metric_target support to ConfusionMatrix
  • Extended detections_to_tensor to support OBB tensor extraction from xyxyxyxy
  • Added OBB-aware tensor validation for (N, 9) target tensors and (N, 10) prediction tensors
  • Reused oriented_box_iou_batch for OBB IoU computation
  • Added explicit handling for unsupported MetricTarget.MASKS
  • Preserved backward compatibility for existing AABB workflows
  • Updated docstrings for tensor format expectations and supported metric targets
  • Added regression and OBB-specific tests

Testing

  • I have tested this code locally
  • I have added unit tests that prove my fix is effective or that my feature works
  • All new and existing tests pass

Tests executed locally:

pytest tests/metrics/test_detection.py
pytest tests/metrics/
pre-commit run --all-files

Results:

  • 118 metrics tests passing
  • Ruff, mypy, formatting, and codespell passing
  • Existing AABB behavior remains unchanged

Google Colab (optional)

Not applicable.

Screenshots/Videos (optional)

Not applicable.

Additional Notes

This implementation intentionally reuses the existing oriented_box_iou_batch utility and follows the established MetricTarget pattern used by other metrics to minimize architectural changes and preserve consistency.

@Khanz9664 Khanz9664 requested a review from SkalskiP as a code owner May 11, 2026 17:12
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 11, 2026

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

❌ Patch coverage is 97.22222% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78%. Comparing base (7dce606) to head (cb071d3).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2247   +/-   ##
=======================================
  Coverage       78%     78%           
=======================================
  Files           66      66           
  Lines         8347    8370   +23     
=======================================
+ Hits          6475    6510   +35     
+ Misses        1872    1860   -12     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds OBB support to ConfusionMatrix by threading a metric_target parameter through detections_to_tensor, validate_input_tensors, ConfusionMatrix.from_detections/from_tensors/evaluate_detection_batch/benchmark. When MetricTarget.ORIENTED_BOUNDING_BOXES is selected, IoU is computed via oriented_box_iou_batch over xyxyxyxy data; MetricTarget.MASKS is explicitly rejected.

Changes:

  • Extend detections_to_tensor to emit (N,9)/(N,10) OBB tensors from data["xyxyxyxy"] and reject MASKS.
  • Update validate_input_tensors and evaluate_detection_batch to use OBB-aware column layout and dispatch to oriented_box_iou_batch.
  • Plumb metric_target through ConfusionMatrix dataclass, from_detections, from_tensors, and benchmark; update docstrings.
  • Add OBB tensor-extraction parametrizations and an OBB rotation-sensitivity end-to-end test.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/supervision/metrics/detection.py Adds OBB-aware tensor extraction, validation, IoU dispatch and a new metric_target field plumbed through the public APIs.
tests/metrics/test_detection.py Adds OBB cases for detections_to_tensor, validation tests for OBB shapes, and a from_detections OBB rotation-sensitivity test.

Borda and others added 4 commits May 13, 2026 22:04
…idation

- Dispatch to oriented_box_iou_batch when metric_target=ORIENTED_BOUNDING_BOXES
- Raise ValueError (not NotImplementedError) for MetricTarget.MASKS on all entry points (from_detections, from_tensors, evaluate_detection_batch) via _assert_supported_target helper
- Validate OBB array shape before reshape; clear error message on malformed data
- Add regression test for roboflow#1760 (orthogonal thin bars: AABB IoU=1.0 but OBB IoU≈0.053)
- Add parametrized tests: from_tensors OBB end-to-end, MASKS rejection, multiclass OBB, metric_target persistence
- Update docstrings: detections_to_tensor shape table, from_detections non-trivial example, metric_target param docs across public API
- Add changelog entry for PR roboflow#2247 under 0.28.0

---
Co-authored-by: Claude Code <noreply@anthropic.com>
- Refactor ConfusionMatrix test cases for MASKS metric target into reusable helper functions (_call_confusion_matrix_from_detections_masks, _call_confusion_matrix_from_tensors_masks, _call_confusion_matrix_evaluate_detection_batch_masks).
- Update test parametrization to use extracted helper functions for improved clarity and maintainability.
- Simplify docstring examples to Python REPL (pycon) output format for consistency.
Borda
Borda previously approved these changes May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OBB support for ConfusionMatrix

4 participants