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

[QUESTION] Customising "category_id" values when exporting a COCO-formatted dataset #4162

Closed
astringfield opened this issue Mar 15, 2024 · 2 comments · Fixed by #4354
Closed
Labels
bug Bug fixes

Comments

@astringfield
Copy link

I have a custom COCO-formatted dataset loaded into FiftyOne and I'm trying to export the images and annotations in COCO format:

import fiftyone as fo

custom_coco_dataset_view.export(
        export_dir=str(output_path),
        dataset_type=fo.types.COCODetectionDataset,
        label_field="segmentations",
    )

Currently, the COCO category_id values in the export COCO JSON file start at 0.

Is it possible to modify this behaviour so that the category_id values start at 1?

Examples below of the current export results and the export results I'm hoping to see.

Current export format (categories start at 0)

"categories": [
    {
        "id": 0,
        "name": "apple",
        "supercategory": ""
    },
    {
        "id": 1,
        "name": "orange",
        "supercategory": ""
    },
    {
        "id": 2,
        "name": "banana",
        "supercategory": ""
    },
]

Desired export format (categories start at 1)

"categories": [
    {
        "id": 1,
        "name": "apple",
        "supercategory": ""
    },
    {
        "id": 2,
        "name": "orange",
        "supercategory": ""
    },
    {
        "id": 3,
        "name": "banana",
        "supercategory": ""
    },
]

Appreciate any help - thanks!

@astringfield astringfield added the bug Bug fixes label Mar 15, 2024
@astringfield astringfield changed the title Customising "category_id" values when exporting a COCO-formatted dataset [QUESTION] Customising "category_id" values when exporting a COCO-formatted dataset Mar 15, 2024
@benjaminpkane
Copy link
Contributor

benjaminpkane commented Mar 18, 2024

HI @astringfield. I do not believe this is supported by default. One option is subclass COCODetectionDatasetExporter and override the _parse_classes method. It looks like that is where the class mapping is initialized. (Note the safer solution is to re-implement the entire setup method as that is a public contract)

Contributions are also welcome if you see an opportunity to add category_id configuration out of the box!

@brimoor
Copy link
Contributor

brimoor commented May 7, 2024

#4354 adds support for this!

categories = [
    {'supercategory': 'person', 'id': 1, 'name': 'person'},
    {'supercategory': 'vehicle', 'id': 2, 'name': 'bicycle'},
    ...
    {'supercategory': 'indoor', 'id': 90, 'name': 'toothbrush'}
]

dataset.export(
    export_dir="/path/for/export",
    dataset_type=fo.types.COCODetectionDataset,
    categories=categories,
)

@brimoor brimoor closed this as completed May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug fixes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants