diff --git a/CHANGELOG.md b/CHANGELOG.md index c3f15499516..550e293dff0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 (). ### Bug fixes diff --git a/otx/algorithms/segmentation/adapters/mmseg/task.py b/otx/algorithms/segmentation/adapters/mmseg/task.py index bb56499618b..f5801b1814f 100644 --- a/otx/algorithms/segmentation/adapters/mmseg/task.py +++ b/otx/algorithms/segmentation/adapters/mmseg/task.py @@ -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 diff --git a/otx/algorithms/segmentation/configs/base/configuration.py b/otx/algorithms/segmentation/configs/base/configuration.py index 1d795b896fb..1c59ff5d841 100644 --- a/otx/algorithms/segmentation/configs/base/configuration.py +++ b/otx/algorithms/segmentation/configs/base/configuration.py @@ -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 diff --git a/otx/algorithms/segmentation/task.py b/otx/algorithms/segmentation/task.py index c0cb52c39c2..82dd39b2799 100644 --- a/otx/algorithms/segmentation/task.py +++ b/otx/algorithms/segmentation/task.py @@ -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 @@ -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: diff --git a/otx/algorithms/segmentation/utils/__init__.py b/otx/algorithms/segmentation/utils/__init__.py index 2f72021874d..82b29880450 100644 --- a/otx/algorithms/segmentation/utils/__init__.py +++ b/otx/algorithms/segmentation/utils/__init__.py @@ -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"] diff --git a/otx/algorithms/segmentation/utils/metadata.py b/otx/algorithms/segmentation/utils/metadata.py new file mode 100644 index 00000000000..ec20a028ae4 --- /dev/null +++ b/otx/algorithms/segmentation/utils/metadata.py @@ -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(" ", "_") + " " + all_labels = all_labels.strip() + + omz_config[("model_info", "labels")] = all_labels + + return omz_config diff --git a/tests/integration/cli/classification/test_classification.py b/tests/integration/cli/classification/test_classification.py index c39d77e2ced..213073f6df0 100644 --- a/tests/integration/cli/classification/test_classification.py +++ b/tests/integration/cli/classification/test_classification.py @@ -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) diff --git a/tests/integration/cli/detection/test_detection.py b/tests/integration/cli/detection/test_detection.py index 1ba16910b06..49df29b21d2 100644 --- a/tests/integration/cli/detection/test_detection.py +++ b/tests/integration/cli/detection/test_detection.py @@ -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) diff --git a/tests/integration/cli/semantic_segmentation/test_segmentation.py b/tests/integration/cli/semantic_segmentation/test_segmentation.py index c386a316c05..e4edee8e923 100644 --- a/tests/integration/cli/semantic_segmentation/test_segmentation.py +++ b/tests/integration/cli/semantic_segmentation/test_segmentation.py @@ -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) diff --git a/tests/test_suite/run_test_command.py b/tests/test_suite/run_test_command.py index 0a2cc301e8d..bdf9c1efe10 100644 --- a/tests/test_suite/run_test_command.py +++ b/tests/test_suite/run_test_command.py @@ -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 = [ @@ -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) diff --git a/tests/unit/algorithms/segmentation/test_task.py b/tests/unit/algorithms/segmentation/test_task.py index b2eb5bd4344..b9628dba447 100644 --- a/tests/unit/algorithms/segmentation/test_task.py +++ b/tests/unit/algorithms/segmentation/test_task.py @@ -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()