Handling Overlapping Annotations in Mask2Former by A Small Trick #38054
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request: Add Support for Handling Overlapping Annotations in Mask2Former
Problem
The current image processing pipeline for Mask2Former doesn't handle overlapping annotations correctly. When annotations overlap, the processing order is arbitrary, causing larger objects to sometimes be overwritten by smaller ones. This can lead to information loss and incorrect segmentation masks.
Solution
This PR introduces a new function,
convert_segmentation_map_to_binary_masks_sorted
, that addresses this issue by processing object instances in descending order of their area. This ensures that smaller objects consistently appear on top of larger objects, preserving all annotation information in overlapping regions.The implementation draws inspiration from the approach used in the
coco2masks
function, which sorts annotations by area before processing them. This enhancement is particularly beneficial for datasets with significant object overlaps.Changes
A new function
convert_segmentation_map_to_binary_masks_sorted
has been added with the following features:sort_by_area
parameter (default:True
).This new function can serve as a direct replacement for the existing conversion function, providing improved handling of overlapping regions.
Testing
Performance Impact
While the area calculation introduces a minor computational overhead, its impact is negligible within the overall processing pipeline. This step occurs during preprocessing rather than during model inference.