Skip to content

Fix inconsistent force_masks across dataset formats#265

Merged
Borda merged 14 commits intoroboflow:developfrom
kirilllzaitsev:fix/inconsistent_force_masks
Feb 20, 2026
Merged

Fix inconsistent force_masks across dataset formats#265
Borda merged 14 commits intoroboflow:developfrom
kirilllzaitsev:fix/inconsistent_force_masks

Conversation

@kirilllzaitsev
Copy link
Copy Markdown
Contributor

Description

load_coco_annotations used force_masks as a flag that indicates if masks should be parsed or not. On the contrary, YOLO and VOC enforce parsing masks if this flag is set, or parse masks in case they are present in annotations. Current change homogenizes the behavior, setting it to the second option.

Type of change

Please delete options that are not relevant.

  • [+] Bug fix (non-breaking change which fixes an issue)

How has this change been tested, please provide a testcase or example of how you tested the change?

Does not impact anything.

Any specific deployment considerations

For example, documentation changes, usability, usage/costs, secrets, etc.

Docs

  • Docs updated? What were the changes:

@SkalskiP SkalskiP requested a review from artyaltanzaya August 7, 2023 10:17
@SkalskiP SkalskiP added enhancement New feature or request API:datasets Dataset API version 0.14.0 Feature to be added in `0.14.0` release labels Aug 7, 2023
@SkalskiP SkalskiP added this to the version: 0.14.0 milestone Aug 7, 2023
@SkalskiP
Copy link
Copy Markdown
Collaborator

SkalskiP commented Aug 7, 2023

Hi, @kirilllzaitsev! 👋🏻 I marked this PR as a 0.14.0 release. I'm super busy with wrapping work on a few 0.13.0 features as we plan to release tomorrow. Please be patient 🙏🏻

@hardikdava and @artyaltanzaya could you take a look at the PR in the meantime?

@SkalskiP
Copy link
Copy Markdown
Collaborator

SkalskiP commented Aug 7, 2023

@capjamesg @yeldarby I think you should know we are working on those changes. They might influence Autodistill.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Aug 11, 2023

CLA assistant check
All committers have signed the CLA.

@SkalskiP
Copy link
Copy Markdown
Collaborator

Hi, @kirilllzaitsev 👋🏻! Sorry for not being overly active on this PR. I was a bit busy. Any chance you could provide Google Colab to test the updated version? 🙏🏻

@kirilllzaitsev
Copy link
Copy Markdown
Contributor Author

@SkalskiP sure, I will prepare it a bit later today

@SkalskiP
Copy link
Copy Markdown
Collaborator

Awesome! Thanks a lot @kirilllzaitsev 🙏🏻

Comment thread supervision/dataset/formats/coco.py Outdated
Comment thread supervision/dataset/formats/pascal_voc.py Outdated
Copy link
Copy Markdown

@artyaltanzaya artyaltanzaya left a comment

Choose a reason for hiding this comment

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

thanks for the contribution! added minor suggestions for clarity.

@kirilllzaitsev
Copy link
Copy Markdown
Contributor Author

@SkalskiP here is the colab link. Current behavior in COCO and VOC differs if the force_masks is True, but there are no masks: VOC proceeds with an empty annotation.mask, while COCO errors out with a KeyError (at this point, image_annotation["segmentation"]).

I would handle the case of force_masks=True and no masks with a warning and no error. What are your thoughts?

@SkalskiP
Copy link
Copy Markdown
Collaborator

Hi, @kirilllzaitsev 👋🏻! Will you have time to resolve conflicts in this branch today?

@kirilllzaitsev
Copy link
Copy Markdown
Contributor Author

Hey @SkalskiP , sure, done

Comment thread supervision/dataset/formats/pascal_voc.py Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 2, 2026

Codecov Report

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

Additional details and impacted files
@@          Coverage Diff           @@
##           develop   #265   +/-   ##
======================================
  Coverage       73%    74%           
======================================
  Files           61     61           
  Lines         7390   7395    +5     
======================================
+ Hits          5422   5447   +25     
+ Misses        1968   1948   -20     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to make force_masks behave consistently across dataset formats by enabling masks when the flag is set or when masks are present in the annotations, instead of using force_masks as a strict on/off switch for parsing masks in COCO.

Changes:

  • In pascal_voc.py, introduces a _with_mask helper and updates detections_from_xml_obj to use it when deciding whether to include masks.
  • In coco.py, updates load_coco_annotations to compute with_masks based on force_masks or the presence of a segmentation field and passes this into coco_annotations_to_detections, adding a _with_mask helper for COCO annotations.

Reviewed changes

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

File Description
supervision/dataset/formats/pascal_voc.py Refactors mask detection into a _with_mask helper and adjusts detections_from_xml_obj mask-flag logic to align with other formats.
supervision/dataset/formats/coco.py Changes load_coco_annotations to infer with_masks from force_masks plus annotation content via a new _with_mask helper and passes the result into detection conversion.
Comments suppressed due to low confidence (2)

supervision/dataset/formats/pascal_voc.py:245

  • The with_masks flag is overwritten on each object inside the loop and then only used once when constructing Detections, so if an earlier object has a polygon and a later object does not, previously computed masks will be discarded because with_masks reflects only the last object processed. To make mask handling consistent and avoid silently dropping masks, consider computing with_masks based on whether any object in the image has a polygon (or on force_masks) outside the loop, or otherwise ensuring it accumulates mask presence across objects rather than just mirroring the last one.
        with_masks = force_masks or _with_mask(obj)

        for polygon in obj.findall("polygon"):
            polygon = parse_polygon_points(polygon)
            # https://github.com/roboflow/supervision/issues/144

supervision/dataset/formats/pascal_voc.py:245

  • detections_from_xml_obj has mask-handling logic controlled by force_masks and _with_mask, but the existing tests in test/dataset/formats/test_pascal_voc.py::test_detections_from_xml_obj only cover bounding boxes and never exercise objects with <polygon> elements or force_masks=True. Adding tests that cover polygon masks and the force_masks flag would help ensure that future changes to this code (including the new _with_mask helper) behave as intended.
        with_masks = force_masks or _with_mask(obj)

        for polygon in obj.findall("polygon"):
            polygon = parse_polygon_points(polygon)
            # https://github.com/roboflow/supervision/issues/144

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread supervision/dataset/formats/coco.py
Comment thread supervision/dataset/formats/coco.py Outdated
Borda and others added 3 commits February 2, 2026 14:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/supervision/dataset/formats/coco.py
Comment thread src/supervision/dataset/formats/coco.py
Comment thread src/supervision/dataset/formats/pascal_voc.py
@Borda Borda mentioned this pull request Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API:datasets Dataset API bug Something isn't working enhancement New feature or request version 0.14.0 Feature to be added in `0.14.0` release

Projects

Status: Current Release: Done

Development

Successfully merging this pull request may close these issues.

6 participants