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 configuration to segmentation IR #2029

Merged
merged 6 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file.

### Enhancements

-
- Make semantic segmentation OpenVINO models compatible with ModelAPI (<https://github.com/openvinotoolkit/training_extensions/pull/2029>).

### Bug fixes

Expand Down
2 changes: 0 additions & 2 deletions otx/algorithms/segmentation/adapters/mmseg/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
from otx.algorithms.segmentation.adapters.mmseg.utils.builder import build_segmentor
from otx.algorithms.segmentation.adapters.mmseg.utils.exporter import SegmentationExporter
from otx.algorithms.segmentation.task import OTXSegmentationTask

# from otx.algorithms.segmentation.utils import get_det_model_api_configuration
from otx.api.configuration import cfg_helper
from otx.api.configuration.helper.utils import ids_to_strings
from otx.api.entities.datasets import DatasetEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
class SegmentationConfig(BaseConfig):
"""Configurations of OTX Segmentation."""

header = string_attribute("Configuration for an object detection task of MPA")
header = string_attribute("Configuration for an object semantic segmentation task of OTX")
description = header

@attrs
Expand Down
5 changes: 5 additions & 0 deletions otx/algorithms/segmentation/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
InferenceProgressCallback,
TrainingProgressCallback,
)
from otx.algorithms.common.utils.ir import embed_ir_model_data
from otx.algorithms.common.utils.logger import get_logger
from otx.algorithms.segmentation.adapters.openvino.model_wrappers.blur import (
get_activation_map,
)
from otx.algorithms.segmentation.configs.base import SegmentationConfig
from otx.algorithms.segmentation.utils.metadata import get_seg_model_api_configuration
from otx.api.configuration import cfg_helper
from otx.api.configuration.helper.utils import ids_to_strings
from otx.api.entities.datasets import DatasetEntity
Expand Down Expand Up @@ -228,6 +230,9 @@ def export(
xml_file = outputs.get("xml")
onnx_file = outputs.get("onnx")

ir_extra_data = get_seg_model_api_configuration(self._task_environment.label_schema, self._hyperparams)
embed_ir_model_data(xml_file, ir_extra_data)

if xml_file is None or bin_file is None or onnx_file is None:
raise RuntimeError("invalid status of exporting. bin and xml or onnx should not be None")
with open(bin_file, "rb") as f:
Expand Down
20 changes: 7 additions & 13 deletions otx/algorithms/segmentation/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
"""Collection of utils for task implementation in Segmentation Task."""

# Copyright (C) 2022 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.
# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


from .metadata import get_seg_model_api_configuration

__all__ = ["get_seg_model_api_configuration"]
27 changes: 27 additions & 0 deletions otx/algorithms/segmentation/utils/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Utils for hadnling metadata of segmentation models."""

# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


from mmcv.utils import ConfigDict

from otx.api.entities.label_schema import LabelSchemaEntity


def get_seg_model_api_configuration(label_schema: LabelSchemaEntity, hyperparams: ConfigDict):
"""Get ModelAPI config."""
omz_config = {}
omz_config[("model_info", "model_type")] = "segmentation"

omz_config[("model_info", "soft_threshold")] = hyperparams.postprocessing.soft_threshold
omz_config[("model_info", "blur_strength")] = hyperparams.postprocessing.blur_strength

all_labels = ""
for lbl in label_schema.get_labels(include_empty=False):
all_labels += lbl.name.replace(" ", "_") + " "
sovrasov marked this conversation as resolved.
Show resolved Hide resolved
all_labels = all_labels.strip()

omz_config[("model_info", "labels")] = all_labels

return omz_config
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_otx_resume(self, template, tmp_dir_path):
@pytest.mark.parametrize("dump_features", [True, False])
def test_otx_export(self, template, tmp_dir_path, dump_features):
tmp_dir_path = tmp_dir_path / "multi_class_cls"
otx_export_testing(template, tmp_dir_path, dump_features)
otx_export_testing(template, tmp_dir_path, dump_features, check_ir_meta=True)

@e2e_pytest_component
@pytest.mark.parametrize("template", templates, ids=templates_ids)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/cli/detection/test_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_otx_resume(self, template, tmp_dir_path):
@pytest.mark.parametrize("dump_features", [True, False])
def test_otx_export(self, template, tmp_dir_path, dump_features):
tmp_dir_path = tmp_dir_path / "detection"
otx_export_testing(template, tmp_dir_path, dump_features)
otx_export_testing(template, tmp_dir_path, dump_features, check_ir_meta=True)

@e2e_pytest_component
@pytest.mark.parametrize("template", templates, ids=templates_ids)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_otx_resume(self, template, tmp_dir_path):
@pytest.mark.parametrize("dump_features", [True, False])
def test_otx_export(self, template, tmp_dir_path, dump_features):
tmp_dir_path = tmp_dir_path / "segmentation"
otx_export_testing(template, tmp_dir_path, dump_features)
otx_export_testing(template, tmp_dir_path, dump_features, check_ir_meta=True)

@e2e_pytest_component
@pytest.mark.parametrize("template", templates, ids=templates_ids)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_suite/run_test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def otx_hpo_testing(template, root, otx_dir, args):
assert os.path.exists(f"{template_work_dir}/hpo_trained_{template.model_template_id}/models/label_schema.json")


def otx_export_testing(template, root, dump_features=False, half_precision=False):
def otx_export_testing(template, root, dump_features=False, half_precision=False, check_ir_meta=False):
template_work_dir = get_template_dir(template, root)
save_path = f"{template_work_dir}/exported_{template.model_template_id}"
command_line = [
Expand Down Expand Up @@ -246,6 +246,13 @@ def otx_export_testing(template, root, dump_features=False, half_precision=False
xml_model = stream.read()
assert "FP16" in xml_model

if check_ir_meta:
with open(path_to_xml, encoding="utf-8") as stream:
xml_model = stream.read()
assert "model_info" in xml_model
assert "model_type" in xml_model
assert "labels" in xml_model


def otx_eval_testing(template, root, otx_dir, args):
template_work_dir = get_template_dir(template, root)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/algorithms/segmentation/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,6 @@ class _MockResultEntity:
def test_export(self, otx_model, mocker):
mocker_open = mocker.patch("builtins.open")
mocker_open.__enter__.return_value = True
mocker.patch("otx.algorithms.segmentation.task.embed_ir_model_data", return_value=None)
self.seg_task.export(ExportType.OPENVINO, otx_model)
mocker_open.assert_called()