Fix overlap filter to use segmentation masks when available (#1987)#1994
Fix overlap filter to use segmentation masks when available (#1987)#1994Nishant-ZFYII wants to merge 1 commit intoroboflow:mainfrom
Conversation
|
Good Day, Thanks and Regards. |
| return bool(overlap_mask[cy, cx]) | ||
| return False | ||
| else: | ||
| return bool(np.logical_and(overlap_mask, other_mask).any()) |
There was a problem hiding this comment.
(Claude)
Performance: full-image np.logical_and for "Any Overlap" (minor)
np.logical_and(overlap_mask, other_mask).any() allocates a full (H, W) intermediate boolean
array on every comparison. For large images (e.g., 4K = 3840x2160) with many detections, this can be
costly. A more efficient approach would be to restrict the comparison to the intersection of the two
bounding boxes:
| return bool(np.logical_and(overlap_mask, other_mask).any()) | |
| x1 = max(overlap_bbox[0], other_bbox[0]) | |
| y1 = max(overlap_bbox[1], other_bbox[1]) | |
| x2 = min(overlap_bbox[2], other_bbox[2]) | |
| y2 = min(overlap_bbox[3], other_bbox[3]) | |
| if x1 >= x2 or y1 >= y2: | |
| return False | |
| return bool(np.logical_and(overlap_mask[y1:y2, x1:x2], other_mask[y1:y2, x1:x2]).any()) |
This will also require to pass extra param to this method, so signature will need to receive: overlap_bbox: list[int]
Then, it will also need predictions.xyxy[oi] passed when OverlapBlockV1.masks_overlap is called and some test assertions fixed
Would be nice to use named params when masks_overlap is called (though it's up to you)
|
hi @Nishant-ZFYII, thank you for this contribution! I have ran your PR through Claude and got one suggestion that I think makes sense in terms of performance, please let me know what are your thoughts about this. |
What does this PR do?
This PR fixes the
Overlap Filterworkflow block (roboflow_core/overlap@v1) so that instance segmentation predictions use masks for overlap checks, rather than implicitly falling back to bounding-box-only overlap.Before this change,
OverlapBlockV1.run()only usedpredictions.xyxy, which could produce false positives for segmented objects with irregular shapes (their bounding boxes overlap even when the masks do not).This PR:
sv.DetectionsRelated Issue(s): Fixes #1987
Type of Change
Testing
Test details:
Center OverlapandAny Overlap)Example command run locally:
pytest tests/workflows/unit_tests/core_steps/analytics/test_coords_overlap_v1.py -vChecklist
Additional Context
I’m new to the repo, so please let me know if you’d prefer this behavior behind a new version (e.g.
@v2) or if the current approach (fixing@v1since it already accepts instance segmentation predictions) is aligned with the project’s versioning expectations. Happy to adjust based on maintainer feedback.