Skip to content

fix(detection): keep from_inference aligned on partial masks#2362

Merged
Borda merged 3 commits into
roboflow:developfrom
kounelisagis:fix/from-inference-partial-masks
Jun 30, 2026
Merged

fix(detection): keep from_inference aligned on partial masks#2362
Borda merged 3 commits into
roboflow:developfrom
kounelisagis:fix/from-inference-partial-masks

Conversation

@kounelisagis

Copy link
Copy Markdown
Contributor

Bug

process_roboflow_result appends a mask only for predictions that carry one (RLE or polygon), but appends xyxy, confidence and class_id for every prediction. A result that mixes masked and box-only predictions therefore ends up with a mask array shorter than the boxes, and Detections.from_inference raises:

ValueError: mask must be a 3D np.ndarray with shape (2, H, W), but got shape (1, H, W)

This shows up with segmentation results where one polygon is empty/degenerate and falls back to a box, so part of the batch has masks and part does not.

This is the same shape of issue as #2353 (partial tracker_id), in the same function; the mask path was the remaining case.

Fix

Append None for box-only predictions to keep the list aligned, and build the mask array only when every prediction has a mask, otherwise drop masks (with a warning) to preserve alignment with xyxy. Fully-masked and mask-free results are unchanged. Missing entries are detected by identity (mask is None) since None in masks would compare numpy arrays element-wise and raise.

Tests

Added an end-to-end from_inference test for a mixed masked/box-only batch, and updated the existing process_roboflow_result case that previously documented the misalignment as a known limitation.

process_roboflow_result appended a mask only for predictions carrying one
(RLE or polygon), while xyxy/confidence/class_id were appended for every
prediction. A result mixing masked and box-only predictions (e.g. a
segmentation batch where one polygon is empty) produced a mask array shorter
than the boxes, so Detections.from_inference raised a shape-mismatch error.

Append None for box-only predictions and build the mask array only when every
prediction has a mask, otherwise drop masks to preserve alignment, mirroring
the tracker_id handling. Fully-masked and mask-free results are unchanged.
@kounelisagis kounelisagis requested a review from SkalskiP as a code owner June 28, 2026 11:01
@codecov

codecov Bot commented Jun 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83%. Comparing base (15f56de) to head (70ded47).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2362   +/-   ##
=======================================
  Coverage       83%     83%           
=======================================
  Files           69      69           
  Lines         9616    9622    +6     
=======================================
+ Hits          7941    7947    +6     
  Misses        1675    1675           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Borda Borda requested a review from Copilot June 30, 2026 21:03
@Borda Borda changed the title fix(detection): keep from_inference aligned on partial masks fix(detection): keep from_inference aligned on partial masks Jun 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes a robustness issue in the Roboflow/Inference connector path by ensuring mask handling stays aligned with xyxy when a batch mixes masked and box-only predictions. It updates process_roboflow_result to avoid producing misaligned mask arrays that previously caused Detections.from_inference to raise on mixed batches.

Changes:

  • Ensure process_roboflow_result appends None for box-only predictions and drops masks (with a warning) when only a subset of predictions includes mask data.
  • Update the existing unit test that previously documented the misalignment as a known limitation to reflect the new aligned behavior.
  • Add an end-to-end Detections.from_inference regression test covering a mixed masked/box-only batch.

Reviewed changes

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

File Description
src/supervision/detection/utils/internal.py Keeps mask collection aligned by inserting None for box-only detections and dropping masks on partial-mask batches to prevent shape errors.
tests/detection/utils/test_internal.py Updates the expected output for a mixed RLE + box-only batch to confirm masks are dropped (not misaligned).
tests/detection/test_core.py Adds an end-to-end regression test asserting mixed mask presence does not crash and results in mask is None.

…cker_id

- Update `masks` Returns clause to document partial-drop case and corrupt-RLE blast radius (D1+C1)
- Remove stale "known limitation" note from from_inference docstring; describe actual behavior (D2)
- Extract _all_present_or_none() helper; eliminate duplicated partial-drop-warn pattern (S1)

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
@Borda Borda merged commit e1b7a16 into roboflow:develop Jun 30, 2026
23 checks passed
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.

3 participants