-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Roboflow format: Disable roboflow_tfrecord when tf is not install…
…ed (#1130) <!-- 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). - [ ] 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
264 additions
and
72 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
80 changes: 80 additions & 0 deletions
80
src/datumaro/plugins/data_formats/roboflow/base_tfrecord.py
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,80 @@ | ||
# Copyright (C) 2023 Intel Corporation | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import os | ||
import re | ||
from typing import Optional | ||
|
||
from datumaro.components.importer import ImportContext, Importer | ||
from datumaro.components.lazy_plugin import extra_deps | ||
from datumaro.plugins.data_formats.tf_detection_api.base import TfDetectionApiBase | ||
from datumaro.plugins.data_formats.tf_detection_api.format import TfrecordImporterType | ||
from datumaro.util.tf_util import has_feature | ||
from datumaro.util.tf_util import import_tf as _import_tf | ||
|
||
tf = _import_tf() | ||
|
||
|
||
@extra_deps("tensorflow") | ||
class RoboflowTfrecordImporter(Importer): | ||
@classmethod | ||
def find_sources(cls, path): | ||
sources = cls._find_sources_recursive( | ||
path=path, | ||
ext=".tfrecord", | ||
extractor_name="roboflow_tfrecord", | ||
) | ||
if len(sources) == 0: | ||
return [] | ||
|
||
undesired_feature = { | ||
"image/source_id": tf.io.FixedLenFeature([], tf.string), | ||
} | ||
|
||
subsets = {} | ||
for source in sources: | ||
if has_feature(path=source["url"], feature=undesired_feature): | ||
continue | ||
subset_name = os.path.dirname(source["url"]).split(os.sep)[-1] | ||
subsets[subset_name] = source["url"] | ||
|
||
sources = [ | ||
{ | ||
"url": url, | ||
"format": "roboflow_tfrecord", | ||
"options": { | ||
"subset": subset, | ||
}, | ||
} | ||
for subset, url in subsets.items() | ||
] | ||
|
||
return sources | ||
|
||
|
||
class RoboflowTfrecordBase(TfDetectionApiBase): | ||
def __init__( | ||
self, | ||
path: str, | ||
*, | ||
subset: Optional[str] = None, | ||
ctx: Optional[ImportContext] = None, | ||
): | ||
super().__init__( | ||
path=path, | ||
subset=subset, | ||
tfrecord_importer_type=TfrecordImporterType.roboflow, | ||
ctx=ctx, | ||
) | ||
|
||
@staticmethod | ||
def _parse_labelmap(text): | ||
entry_pattern = r'name:\s*"([^"]+)"\s*,\s*id:\s*(\d+)' | ||
entry_pattern = re.compile(entry_pattern) | ||
|
||
matches = re.findall(entry_pattern, text) | ||
|
||
labelmap = {name: int(id) for name, id in matches} | ||
|
||
return labelmap |
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
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
10 changes: 10 additions & 0 deletions
10
tests/assets/roboflow_dataset/tfrecord/train/label_map.pbtxt
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,10 @@ | ||
item { | ||
name: 'label_0' | ||
id: 1 | ||
display_name: 'label_0' | ||
} | ||
item { | ||
name: 'label_1' | ||
id: 2 | ||
display_name: 'label_1' | ||
} |
Binary file not shown.
10 changes: 10 additions & 0 deletions
10
tests/assets/roboflow_dataset/tfrecord/val/label_map.pbtxt
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,10 @@ | ||
item { | ||
name: 'label_0' | ||
id: 1 | ||
display_name: 'label_0' | ||
} | ||
item { | ||
name: 'label_1' | ||
id: 2 | ||
display_name: 'label_1' | ||
} |
Binary file not shown.
Oops, something went wrong.