-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MMDetection COCO format importer (#1213)
<!-- Contributing guide: https://github.com/openvinotoolkit/datumaro/blob/develop/CONTRIBUTING.md --> ### Summary <!-- Resolves #111 and #222. Depends on #1000 (for series of dependent commits). This PR introduces this capability to make the project better in this and that. - Added this feature - Removed that feature - Fixed the problem #1234 --> ### How to test <!-- Describe the testing procedure for reviewers, if changes are not fully covered by unit tests or manual testing can be complicated. --> ### Checklist <!-- Put an 'x' in all the boxes that apply --> - [x] I have added unit tests to cover my changes. - [ ] I have added integration tests to cover my changes. - [x] I have added the description of my changes into [CHANGELOG](https://github.com/openvinotoolkit/datumaro/blob/develop/CHANGELOG.md). - [x] I have updated the [documentation](https://github.com/openvinotoolkit/datumaro/tree/develop/docs) accordingly ### License - [ ] I submit _my code changes_ under the same [MIT License](https://github.com/openvinotoolkit/datumaro/blob/develop/LICENSE) that covers the project. Feel free to contact the maintainers if that's a concern. - [ ] I have updated the license header for each file (see an example below). ```python # Copyright (C) 2023 Intel Corporation # # SPDX-License-Identifier: MIT ```
- Loading branch information
Showing
12 changed files
with
449 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# MMDetection COCO | ||
|
||
## Format specification | ||
|
||
[MMDetection](https://mmdetection.readthedocs.io/en/latest/) is a training framework for object detection and instance segmentation tasks, providing a modular and flexible architecture that supports various state-of-the-art models, datasets, and training techniques. MMDetection has gained popularity in the research community for its comprehensive features and ease of use in developing and benchmarking object detection algorithms. | ||
MMDetection specifies their COCO format [here](https://mmdetection.readthedocs.io/en/latest/user_guides/dataset_prepare.html). | ||
|
||
Most of available tasks or formats are similar to the [original COCO format](./formats/coco), while only the image directories are separated with respect to subsets. | ||
In this document, we just describe the directory structure of MMDetection COCO format as per [here](https://mmdetection.readthedocs.io/en/latest/user_guides/dataset_prepare.html). | ||
MMDetection COCO dataset directory should have the following structure: | ||
|
||
<!--lint disable fenced-code-flag--> | ||
``` | ||
└─ Dataset/ | ||
├── <subset_name>/ | ||
│ ├── <image_name1.ext> | ||
│ ├── <image_name2.ext> | ||
│ └── ... | ||
├── <subset_name>/ | ||
│ ├── <image_name1.ext> | ||
│ ├── <image_name2.ext> | ||
│ └── ... | ||
└── annotations/ | ||
├── instances_<subset_name>.json | ||
└── ... | ||
``` | ||
|
||
### Import using CLI | ||
|
||
``` bash | ||
datum project create | ||
datum project import --format mmdet_coco <path/to/dataset> | ||
``` | ||
|
||
### Import using Python API | ||
|
||
```python | ||
import datumaro as dm | ||
|
||
dataset = dm.Dataset.import_from('<path/to/dataset>', 'mmdet_coco') | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright (C) 2023 Intel Corporation | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import os.path as osp | ||
from glob import glob | ||
from typing import Optional | ||
|
||
from datumaro.components.dataset_base import DEFAULT_SUBSET_NAME | ||
from datumaro.components.format_detection import FormatDetectionConfidence, FormatDetectionContext | ||
from datumaro.components.importer import ImportContext | ||
from datumaro.plugins.data_formats.coco.base import _CocoBase | ||
from datumaro.plugins.data_formats.coco.format import CocoImporterType, CocoTask | ||
from datumaro.plugins.data_formats.coco.importer import CocoImporter | ||
|
||
|
||
class MmdetCocoImporter(CocoImporter): | ||
@classmethod | ||
def detect( | ||
cls, | ||
context: FormatDetectionContext, | ||
) -> FormatDetectionConfidence: | ||
ann_paths = context.require_files("annotations/instances_*.json") | ||
|
||
for ann_path in ann_paths: | ||
subset_name = cls._get_subset_name(ann_path) | ||
|
||
with context.require_any(): | ||
with context.alternative(): | ||
image_files = osp.join(subset_name, "*.jpg") | ||
context.require_file(f"{image_files}") | ||
|
||
return FormatDetectionConfidence.MEDIUM | ||
|
||
def __call__(self, path, stream: bool = False, **extra_params): | ||
subset_paths = glob(osp.join(path, "**", "instances_*.json"), recursive=True) | ||
|
||
sources = [] | ||
for subset_path in subset_paths: | ||
options = dict(extra_params) | ||
options["subset"] = self._get_subset_name(subset_path) | ||
|
||
if stream: | ||
options["stream"] = True | ||
|
||
sources.append({"url": subset_path, "format": "mmdet_coco", "options": options}) | ||
|
||
return sources | ||
|
||
@classmethod | ||
def _get_subset_name(cls, subset_path: str): | ||
parts = osp.splitext(osp.basename(subset_path))[0].split("instances_", maxsplit=1) | ||
subset_name = parts[1] if len(parts) == 2 else DEFAULT_SUBSET_NAME | ||
|
||
return subset_name | ||
|
||
|
||
class MmdetCocoBase(_CocoBase): | ||
""" | ||
Parses Roboflow COCO annotations written in the following format: | ||
https://cocodataset.org/#format-data | ||
""" | ||
|
||
def __init__( | ||
self, | ||
path, | ||
*, | ||
subset: Optional[str] = None, | ||
stream: bool = False, | ||
ctx: Optional[ImportContext] = None, | ||
): | ||
super().__init__( | ||
path, | ||
task=CocoTask.instances, | ||
coco_importer_type=CocoImporterType.mmdet, | ||
subset=subset, | ||
stream=stream, | ||
ctx=ctx, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
tests/assets/coco_dataset/mmdet_coco/annotations/instances_train.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ | ||
"licenses":[ | ||
{ | ||
"name":"", | ||
"id":0, | ||
"url":"" | ||
} | ||
], | ||
"info":{ | ||
"contributor":"", | ||
"date_created":"", | ||
"description":"", | ||
"url":"", | ||
"version":"", | ||
"year":"" | ||
}, | ||
"categories":[ | ||
{ | ||
"id":1, | ||
"name":"a", | ||
"supercategory":"" | ||
}, | ||
{ | ||
"id":2, | ||
"name":"b", | ||
"supercategory":"" | ||
}, | ||
{ | ||
"id":4, | ||
"name":"c", | ||
"supercategory":"" | ||
} | ||
], | ||
"images":[ | ||
{ | ||
"id":5, | ||
"width":10, | ||
"height":5, | ||
"file_name":"a.jpg", | ||
"license":0, | ||
"flickr_url":"", | ||
"coco_url":"", | ||
"date_captured":0 | ||
} | ||
], | ||
"annotations":[ | ||
{ | ||
"id":1, | ||
"image_id":5, | ||
"category_id":2, | ||
"segmentation":[ | ||
|
||
], | ||
"area":3.0, | ||
"bbox":[ | ||
2.0, | ||
2.0, | ||
3.0, | ||
1.0 | ||
], | ||
"iscrowd":0 | ||
} | ||
] | ||
} |
101 changes: 101 additions & 0 deletions
101
tests/assets/coco_dataset/mmdet_coco/annotations/instances_val.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
{ | ||
"licenses":[ | ||
{ | ||
"name":"", | ||
"id":0, | ||
"url":"" | ||
} | ||
], | ||
"info":{ | ||
"contributor":"", | ||
"date_created":"", | ||
"description":"", | ||
"url":"", | ||
"version":"", | ||
"year":"" | ||
}, | ||
"categories":[ | ||
{ | ||
"id":1, | ||
"name":"a", | ||
"supercategory":"" | ||
}, | ||
{ | ||
"id":2, | ||
"name":"b", | ||
"supercategory":"" | ||
}, | ||
{ | ||
"id":4, | ||
"name":"c", | ||
"supercategory":"" | ||
} | ||
], | ||
"images":[ | ||
{ | ||
"id":40, | ||
"width":5, | ||
"height":10, | ||
"file_name":"b.jpg", | ||
"license":0, | ||
"flickr_url":"", | ||
"coco_url":"", | ||
"date_captured":0 | ||
} | ||
], | ||
"annotations":[ | ||
{ | ||
"id":1, | ||
"image_id":40, | ||
"category_id":1, | ||
"segmentation":[ | ||
[ | ||
0.0, | ||
0.0, | ||
1.0, | ||
0.0, | ||
1.0, | ||
2.0, | ||
0.0, | ||
2.0 | ||
] | ||
], | ||
"area":2.0, | ||
"bbox":[ | ||
0.0, | ||
0.0, | ||
1.0, | ||
2.0 | ||
], | ||
"iscrowd":0, | ||
"attributes":{ | ||
"x":1, | ||
"y":"hello" | ||
} | ||
}, | ||
{ | ||
"id":2, | ||
"image_id":40, | ||
"category_id":2, | ||
"segmentation":{ | ||
"counts":[ | ||
0, | ||
20, | ||
30 | ||
], | ||
"size":[ | ||
10, | ||
5 | ||
] | ||
}, | ||
"area":20.0, | ||
"bbox":[ | ||
0.0, | ||
0.0, | ||
1.0, | ||
9.0 | ||
], | ||
"iscrowd":1 | ||
} | ||
] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.