-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Merge Multiple Polygons in a COCO Dataset Annotation #1229
base: develop
Are you sure you want to change the base?
feat: Merge Multiple Polygons in a COCO Dataset Annotation #1229
Conversation
If a segmentation list of an annotation in a COCO dataset contains more than 1 nested list, seperate binary masks are created for each nested list (polygon). Then, the first polygon is designated as a parent and subsequent polygons in the list will be merged with the parent using numpy logical or.
The function merge_masks() has been renamed to merge_polygons(). It has been refactored to include explicit function parameters and return value.
@@ -46,6 +47,59 @@ def approximate_mask_with_polygons( | |||
] | |||
|
|||
|
|||
def merge_polygons( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the merge_polygons
name is a bit misleading and not conststant with outhe util names in supervision. I think we should call it polygons_to_mask
.
And of course we already have a function called polygon_to_mask
that does the same but only take one mask.
It seems a bit silly that we would have 2 functions doeng almost the same. I thin kwe should:
- rename
merge_polygons
topolygons_to_mask
. - make sure that
polygons_to_mask
is not usingpolygon_to_mask
internally. - modify places where
polygon_to_mask
was used and usepolygons_to_mask
instead. - mask
polygon_to_mask
as deprecated and recommend usingpolygons_to_mask
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DancinParrot, here is an example of how we mark functions as deprecated. We use deprecated
decorator to do it.
@deprecated(
"`Detections.from_roboflow` is deprecated and will be removed in "
"`supervision-0.22.0`. Use `Detections.from_inference` instead."
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use supervision-0.27.0
as a deprecation version.
Hi @DancinParrot 👋🏻 Thanks a lot for your contribution 🙏🏻 This is a valid change. As I try to keep the code clean, I left a comment proposing a rename to |
Hi @SkalskiP ! Thanks for reviewing and providing feedback on my PR!
Yup, I think that's a great idea. It'd certainly be a more accurate representation of the function's intended purpose. Since multiple lists of vertices within the "segmentation" field (which in itself is a list) of a COCO dataset annotation is perfectly acceptable, the function should be capable of handling both one and multiple lists without relying on yet another function that introduces another unnecessary layer of abstraction. I'll proceed with implementing the suggested changes and deprecating the current Thanks again for your feedback, appreciate it! |
Hi @SkalskiP ! During refactoring, I noticed a very similar function I suppose we could refactor it in such a way but it does seem to be a bit odd: def _polygons_to_masks(
polygons: List[np.ndarray], resolution_wh: Tuple[int, int]
) -> np.ndarray:
return np.array(
[
polygons_to_mask(polygon=[polygon], resolution_wh=resolution_wh)
for polygon in polygons
],
dtype=bool,
) |
Hi @SkalskiP ! Any updates on the above? |
Description
This PR attempts to address issue #1209. Within an appropriately structured COCO dataset, an annotation may contain a segmentation list with multiple nested lists of vertices. This results in supervision terminating prematurely and raising a ValueError.
The solution as discussed in the issue includes the creation of binary masks for all polygons within the segmentation list and the usage of numpy's logical_or() function to merge all masks which is used in the
coco_annotations_to_masks
function.Type of change
Please delete options that are not relevant.
How has this change been tested, please provide a testcase or example of how you tested the change?
The change has been tested using a custom COCO dataset which produces the ValueError mentioned in the issue linked above. You may test out the changes by following the guide on the provided Jupyter Notebook using the following resources:
Any specific deployment considerations
Documentation may require an update.
Docs
docs/datasets/utils.md
with description and types of the function and its parameters. An example and type have also been added for its output.