Skip to content
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

Segmentation annotations default flow modifications #4990

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

geke-mir
Copy link
Contributor

@geke-mir geke-mir commented Oct 25, 2024

What changes are proposed in this pull request?

Require users to specify a mask_targets argument for new segmentations annotations, otherwise fall back onto the dataset's defaults specified in dataset.mask_targets or dataset.default_mask_targets.

How is this patch tested? If it is not, please explain why.

Manual testing of annotation flow with a test CVAT instance.

Release Notes

Is this a user-facing change that should be mentioned in the release notes?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release
    notes for FiftyOne users.

When requesting semantic segmentations annotations, users must specify mask_targets for fields that do not yet exist. If no targets are specified, the targets will be pulled from dataset.mask_targets or dataset.default_mask_targets if either exist.

What areas of FiftyOne does this PR affect?

  • App: FiftyOne application changes
  • Build: Build and test infrastructure changes
  • Core: Core fiftyone Python library changes
  • Documentation: FiftyOne documentation changes
  • Other

Summary by CodeRabbit

  • New Features

    • Enhanced annotation functionality requiring mask targets for new segmentation fields.
    • Improved error handling for empty samples during annotation.
  • Bug Fixes

    • Refined logic for determining mask targets, ensuring proper retrieval from existing samples or defaults.
  • Documentation

    • Added detailed docstrings for better clarity on function parameters and return values.

Copy link
Contributor

coderabbitai bot commented Oct 25, 2024

Walkthrough

The changes primarily focus on enhancing the fiftyone/utils/annotations.py file, specifically the annotate and _get_mask_targets functions. The annotate function now mandates that the mask_targets parameter must be provided if the corresponding field does not already exist. The _get_mask_targets function has been restructured with a comprehensive docstring, improved logic for retrieving mask targets, and updated type hints for its parameters and return values. These modifications aim to improve the robustness and clarity of the annotation process.

Changes

File Change Summary
fiftyone/utils/annotations.py Updated annotate function to require mask_targets if the field does not exist. Retained error handling for empty samples. Restructured _get_mask_targets with a detailed docstring, refined logic for retrieving mask targets, and added type hints to the function signature.

Poem

In the garden of code, where rabbits do play,
New rules for annotations brighten the day.
With mask targets needed, we hop with delight,
Ensuring our fields are all perfectly right.
So let’s raise our ears, and give a cheer,
For clearer functions that bring us near! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between dd5b472 and 050c702.

📒 Files selected for processing (1)
  • fiftyone/utils/annotations.py (3 hunks)
🔇 Additional comments (1)
fiftyone/utils/annotations.py (1)

849-867: Great improvements to type hints and documentation!

The addition of type hints and comprehensive docstring significantly improves code readability and maintainability. The docstring clearly explains the function's purpose, parameters, and return values.

Comment on lines +873 to +886
# If this is a new field, users must define mask targets
if not existing_field:
raise ValueError(
f"Must specify mask_targets argument or in schema for new segmentations field '{label_field}'"
)

# Attempt to find mask targets, otherwise bail and use a default set
if label_field in samples.mask_targets:
mask_targets = samples.mask_targets[label_field]
elif samples.default_mask_targets != {}:
mask_targets = samples.default_mask_targets
else:
mask_targets = {i: str(i) for i in range(1, 256)}
mask_targets[0] = "background"
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding validation for mask target values.

The logic for handling mask targets is well-structured and aligns with the PR objectives. However, consider adding validation to ensure that:

  1. The mask target values are non-negative integers
  2. The value 0 is either undefined or maps to "background"
  3. There are no duplicate labels in the values

Here's a suggested implementation:

 if mask_targets is None:
     # If this is a new field, users must define mask targets
     if not existing_field:
         raise ValueError(
             f"Must specify mask_targets argument or in schema for new segmentations field '{label_field}'"
         )

     # Attempt to find mask targets, otherwise bail and use a default set
     if label_field in samples.mask_targets:
         mask_targets = samples.mask_targets[label_field]
     elif samples.default_mask_targets != {}:
         mask_targets = samples.default_mask_targets
     else:
         mask_targets = {i: str(i) for i in range(1, 256)}
         mask_targets[0] = "background"

+    # Validate mask targets
+    if not all(isinstance(k, int) and k >= 0 for k in mask_targets.keys()):
+        raise ValueError("Mask target keys must be non-negative integers")
+    
+    if 0 in mask_targets and mask_targets[0] != "background":
+        raise ValueError("Mask target 0 must map to 'background' if defined")
+    
+    if len(set(mask_targets.values())) != len(mask_targets):
+        raise ValueError("Mask target values must be unique")

 classes = [c for v, c in mask_targets.items() if v != 0]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# If this is a new field, users must define mask targets
if not existing_field:
raise ValueError(
f"Must specify mask_targets argument or in schema for new segmentations field '{label_field}'"
)
# Attempt to find mask targets, otherwise bail and use a default set
if label_field in samples.mask_targets:
mask_targets = samples.mask_targets[label_field]
elif samples.default_mask_targets != {}:
mask_targets = samples.default_mask_targets
else:
mask_targets = {i: str(i) for i in range(1, 256)}
mask_targets[0] = "background"
# If this is a new field, users must define mask targets
if not existing_field:
raise ValueError(
f"Must specify mask_targets argument or in schema for new segmentations field '{label_field}'"
)
# Attempt to find mask targets, otherwise bail and use a default set
if label_field in samples.mask_targets:
mask_targets = samples.mask_targets[label_field]
elif samples.default_mask_targets != {}:
mask_targets = samples.default_mask_targets
else:
mask_targets = {i: str(i) for i in range(1, 256)}
mask_targets[0] = "background"
# Validate mask targets
if not all(isinstance(k, int) and k >= 0 for k in mask_targets.keys()):
raise ValueError("Mask target keys must be non-negative integers")
if 0 in mask_targets and mask_targets[0] != "background":
raise ValueError("Mask target 0 must map to 'background' if defined")
if len(set(mask_targets.values())) != len(mask_targets):
raise ValueError("Mask target values must be unique")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant