Skip to content

feat: add split_components to semantic maps - #3032

Open
Borda wants to merge 3 commits into
developfrom
feat/semantic-split-components
Open

Borda wants to merge 3 commits into
developfrom
feat/semantic-split-components

Conversation

@Borda

@Borda Borda commented Nov 19, 2026

Copy link
Copy Markdown
Member

Changes:

  • Detections.from_semantic_segmentation takes split_components (default False) and passes it to process_semantic_segmentation, whose docstrings describe the option
  • New helper _split_class_regions labels each class with cv2.connectedComponentsWithStats(..., connectivity=8) and returns one box and mask per patch, sorted by the top-left corner of its box
  • process_semantic_segmentation loops over those regions, so each patch gets its own mask, box and mean confidence, and the class name is built per entry after the loop
  • Tests in TestDetectionsFromSemanticSegmentation cover patch boxes, mask partition, diagonal joining, default behaviour, names, confidence, compact masks, kept background and a background-only map
  • Changelog entry under Unreleased

Impact:

  • A class made of separate patches, such as water in flood imagery, can load as one detection per patch, so boxes and areas no longer span the land between patches
  • Default split_components=False returns the same entries as before, one per class id

Limitations:

  • Connectivity is fixed at 8 and there is no minimum-area filter for small patches
  • The changelog entry has no pull request link yet

Changes:
- `Detections.from_semantic_segmentation` takes `split_components` (default `False`) and passes it to `process_semantic_segmentation`, whose docstrings describe the option
- New helper `_split_class_regions` labels each class with `cv2.connectedComponentsWithStats(..., connectivity=8)` and returns one box and mask per patch, sorted by the top-left corner of its box
- `process_semantic_segmentation` loops over those regions, so each patch gets its own mask, box and mean confidence, and the class name is built per entry after the loop
- Tests in `TestDetectionsFromSemanticSegmentation` cover patch boxes, mask partition, diagonal joining, default behaviour, names, confidence, compact masks, kept background and a background-only map
- Changelog entry under Unreleased

Impact:
- A class made of separate patches, such as water in flood imagery, can load as one detection per patch, so boxes and areas no longer span the land between patches
- Default `split_components=False` returns the same entries as before, one per class id

Limitations:
- Connectivity is fixed at 8 and there is no minimum-area filter for small patches
- The changelog entry has no pull request link yet

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: Codex <codex@openai.com>
@Borda
Borda requested a balanced review from Copilot November 19, 2026 18:25
@Borda
Borda requested a review from SkalskiP as a code owner November 19, 2026 18:25
@Borda Borda added the enhancement New feature or request label Nov 19, 2026
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95%. Comparing base (cdb0fa0) to head (ed671f6).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #3032   +/-   ##
=======================================
  Coverage       95%     95%           
=======================================
  Files           84      84           
  Lines        15408   15421   +13     
=======================================
+ Hits         14677   14690   +13     
  Misses         731     731           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

🟡 Changes recommended

Compact component splitting retains a full-image boolean mask for every component before encoding, creating temporary allocations that can exhaust memory even when compact output is requested.

1 open issue
What changed in this PR

Adds optional connected-component splitting to Supervision’s semantic-segmentation loader, preserving the default of one detection per class.

Changes:

  • Adds split_components=True to produce one detection per 8-connected patch.
  • Computes per-patch boxes, masks and confidence scores, with aligned class names and deterministic ordering.
  • Adds regression tests and an Unreleased changelog entry.
File Description
tests/​detection/​test_core.py Covers patch splitting, metadata, compact masks, defaults and background handling.
src/​supervision/​detection/​utils/​internal.py Extracts connected regions and assembles aligned per-patch detection data.
src/​supervision/​detection/​core.py Exposes, documents and forwards the new option.
docs/​changelog.md Announces component splitting and its unchanged default behavior.

Review effort: Balanced

for label in range(1, count):
left, top, width, height = (int(value) for value in stats[label, :4])
box = [left, top, left + width - 1, top + height - 1]
regions.append((box, labels == label))
@Borda Borda changed the title feat: add split_components to semantic maps feat: add split_components to semantic maps Nov 19, 2026
Borda and others added 2 commits November 23, 2026 00:26
Changes:
- _split_class_regions is now a generator that sorts patch labels by their box corner first, then builds each patch's full-size mask only when it is requested, filling it from the patch's own box
- A regression test measures peak traced memory of a compact split of 300 patches on a 1500 by 1500 map and requires it to stay under a third of what 300 dense masks need

Impact:
- A compact split no longer keeps a full-size boolean mask for every patch before encoding, so peak memory stays near one mask plus the label image
- Patch order, boxes, masks and confidence are unchanged, as the existing split tests still pass

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Changes:
- The split_components changelog entry ends with a link to its pull request

Impact:
- The entry meets the changelog rule that a functional change links its pull request

---
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request has conflicts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants