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

Add support for exports using existing COCO IDs #4530

Merged
merged 1 commit into from
Jun 25, 2024
Merged

Conversation

brimoor
Copy link
Contributor

@brimoor brimoor commented Jun 25, 2024

Adds an optional coco_id parameter that can be passed when export()ing in COCO format that preserves existing COCO image IDs that are stored on a field of your dataset.

Note that, as the example below shows, we already support storing COCO IDs upon import by passing the include_id=True parameter.

import fiftyone as fo
import fiftyone.zoo as foz

dataset = foz.load_zoo_dataset("quickstart")
dataset.set_values("coco_id", list(range(100, 300)))

dataset.export(
    export_dir="/tmp/coco-labels.json",
    dataset_type=fo.types.COCODetectionDataset,
    label_field="ground_truth",
    coco_id="coco_id",
)

dataset2 = fo.Dataset.from_dir(
    dataset_dir="/tmp/coco-labels.json",
    dataset_type=fo.types.COCODetectionDataset,
    include_id=True,
)

assert dataset.bounds("coco_id") == (100, 299)
assert dataset2.bounds("coco_id") == (100, 299)

Summary by CodeRabbit

  • New Features
    • Added support for specifying custom COCO IDs for images in the COCO dataset exporter. This allows for more flexible handling and mapping of image IDs during export.

@brimoor brimoor requested review from benjaminpkane and a team June 25, 2024 02:31
@brimoor brimoor self-assigned this Jun 25, 2024
Copy link
Contributor

coderabbitai bot commented Jun 25, 2024

Walkthrough

The COCODetectionDatasetExporter class in fiftyone/utils/coco.py has been enhanced with a new coco_id parameter, allowing users to specify which field contains COCO IDs for each image. This update includes modifications to image ID mapping and export functionalities to accommodate the new parameter, ensuring consistency in handling COCO dataset formats.

Changes

File Change Summary
fiftyone/utils/coco.py Added coco_id parameter and corresponding handling in the COCODetectionDatasetExporter class, including adjustments to image ID mapping and export functionalities to use coco_id.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant COCODetectionDatasetExporter
    participant System
    
    User->>COCODetectionDatasetExporter: Initialize with coco_id parameter
    COCODetectionDatasetExporter->>System: Store coco_id and prepare _image_id_map
    User->>COCODetectionDatasetExporter: Invoke log_collection with dataset
    COCODetectionDatasetExporter->>System: Use coco_id field for image ID mapping
    User->>COCODetectionDatasetExporter: Call export_sample for image export
    COCODetectionDatasetExporter->>System: Retrieve and map image ID using coco_id
    System->>COCODetectionDatasetExporter: Return mapped image ID
    COCODetectionDatasetExporter->>User: Complete image export with updated image ID
Loading

Poem

🐇 In the land of data, a new key did soar,
To map out the images, COCO IDs to the core.
With codes and numbers, structured and neat,
Exporting samples becomes a treat.
Hopping through data, precise and spry,
Fiftyone's exporter now reaches sky high! 🌟


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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: 3

Outside diff range and nitpick comments (6)
fiftyone/utils/coco.py (6)

Line range hint 824-827: Optimize conditional assignment using a ternary operator.

The assignment of file_name based on self.abs_paths can be simplified using a ternary operator for better readability.

-        if self.abs_paths:
-            file_name = out_image_path
-        else:
-            file_name = uuid
+        file_name = out_image_path if self.abs_paths else uuid
Tools
Ruff

833-834: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


836-836: No explicit stacklevel keyword argument found (B028)


Line range hint 970-970: Remove unnecessary inheritance from object.

In Python 3, all classes implicitly inherit from object, so it's redundant to specify it.

- class COCOObject(object):
+ class COCOObject:

Line range hint 1224-1227: Simplify attribute assignment with a ternary operator.

The assignment of attributes can be simplified using a ternary operator to enhance code readability.

-        if extra_attrs:
-            attributes = {f: d.get(f, None) for f in extra_attrs}
-        else:
-            attributes = {}
+        attributes = {f: d.get(f, None) for f in extra_attrs} if extra_attrs else {}

Line range hint 1319-1319: Use format specifiers instead of percent format.

The error message uses percent formatting. It's recommended to use Python's .format() or f-strings for better readability and performance.

-            "Unsupported label type '%s'. Supported types are %s"
-            % (type(label), self.label_cls)
+            f"Unsupported label type '{type(label)}'. Supported types are {self.label_cls}"

Line range hint 1332-1335: Simplify conditional assignment using a ternary operator.

The assignment of _id can be simplified using a ternary operator for better readability.

-        if id_attr is not None:
-            _id = label.get_attribute_value(id_attr, None)
-        else:
-            _id = None
+        _id = label.get_attribute_value(id_attr, None) if id_attr is not None else None

Line range hint 1367-1370: Use a ternary operator to simplify label assignment.

The assignment of label based on classes_map can be optimized using a ternary operator.

-        if classes_map:
-            label = classes_map[self.category_id]
-        else:
-            label = str(self.category_id)
+        label = classes_map[self.category_id] if classes_map else str(self.category_id)
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 677467b and fc63ec8.

Files selected for processing (1)
  • fiftyone/utils/coco.py (6 hunks)
Additional context used
Ruff
fiftyone/utils/coco.py

245-246: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


605-605: Ambiguous variable name: l (E741)


824-827: Use ternary operator file_name = out_image_path if self.abs_paths else uuid instead of if-else-block (SIM108)

Replace if-else-block with file_name = out_image_path if self.abs_paths else uuid


833-834: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


836-836: No explicit stacklevel keyword argument found (B028)


868-869: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


881-882: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


884-884: No explicit stacklevel keyword argument found (B028)


939-939: Ambiguous variable name: l (E741)


970-970: Class COCOObject inherits from object (UP004)

Remove object inheritance


1224-1227: Use ternary operator attributes = {f: d.get(f, None) for f in extra_attrs} if extra_attrs else {} instead of if-else-block (SIM108)

Replace if-else-block with attributes = {f: d.get(f, None) for f in extra_attrs} if extra_attrs else {}


1319-1319: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1332-1335: Use ternary operator _id = label.get_attribute_value(id_attr, None) if id_attr is not None else None instead of if-else-block (SIM108)

Replace if-else-block with _id = label.get_attribute_value(id_attr, None) if id_attr is not None else None


1367-1370: Use ternary operator label = classes_map[self.category_id] if classes_map else str(self.category_id) instead of if-else-block (SIM108)

Replace if-else-block with label = classes_map[self.category_id] if classes_map else str(self.category_id)


1562-1563: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1568-1569: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1812-1815: Use ternary operator label_types = [label_types] if etau.is_str(label_types) else list(label_types) instead of if-else-block (SIM108)

Replace if-else-block with label_types = [label_types] if etau.is_str(label_types) else list(label_types)


1817-1817: Ambiguous variable name: l (E741)


1821-1822: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1827-1828: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1838-1839: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1842-1842: Avoid equality comparisons to True; use if include_license: for truth checks (E712)

Replace with include_license


1921-1922: Use a single with statement with multiple contexts instead of nested with statements (SIM117)


1944-1944: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


1978-1979: Use format specifiers instead of percent format (UP031)

Replace with format specifiers


2005-2005: Unnecessary open mode parameters (UP015)

Remove open mode parameters


2006-2006: Ambiguous variable name: l (E741)


2010-2010: Unnecessary open mode parameters (UP015)

Remove open mode parameters


2059-2062: Use ternary operator classes = {classes} if etau.is_str(classes) else set(classes) instead of if-else-block (SIM108)

Replace if-else-block with classes = {classes} if etau.is_str(classes) else set(classes)


2149-2149: Avoid equality comparisons to True; use if extra_attrs: for truth checks (E712)

Replace with extra_attrs


2152-2152: Avoid equality comparisons to False; use if not extra_attrs: for false checks (E712)

Replace with not extra_attrs


2274-2274: Do not use bare except (E722)

Copy link
Contributor

@benjaminpkane benjaminpkane left a comment

Choose a reason for hiding this comment

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

LGTM 🙇

@brimoor brimoor merged commit 6630a55 into develop Jun 25, 2024
12 checks passed
@brimoor brimoor deleted the export-coco-ids branch June 25, 2024 16:21
@benjaminpkane benjaminpkane restored the export-coco-ids branch June 25, 2024 20:35
@benjaminpkane benjaminpkane deleted the export-coco-ids branch June 25, 2024 20:36
@coderabbitai coderabbitai bot mentioned this pull request Oct 3, 2024
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.

2 participants