From 998ba7f3c124a59176c30f6e95953efbf29bab5f Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Wed, 19 Apr 2023 07:49:40 +0900 Subject: [PATCH 1/6] Add configuration to segmentation ir --- .../segmentation/adapters/mmseg/task.py | 1 - .../configs/base/configuration.py | 2 +- otx/algorithms/segmentation/task.py | 7 ++++ otx/algorithms/segmentation/utils/__init__.py | 5 +++ otx/algorithms/segmentation/utils/metadata.py | 37 +++++++++++++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 otx/algorithms/segmentation/utils/metadata.py diff --git a/otx/algorithms/segmentation/adapters/mmseg/task.py b/otx/algorithms/segmentation/adapters/mmseg/task.py index bb56499618b..fa357728542 100644 --- a/otx/algorithms/segmentation/adapters/mmseg/task.py +++ b/otx/algorithms/segmentation/adapters/mmseg/task.py @@ -57,7 +57,6 @@ 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..1d14e1887a1 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,11 @@ 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._task_type, 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..7924799032d 100644 --- a/otx/algorithms/segmentation/utils/__init__.py +++ b/otx/algorithms/segmentation/utils/__init__.py @@ -13,3 +13,8 @@ # 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. + + +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..fb97293f7d1 --- /dev/null +++ b/otx/algorithms/segmentation/utils/metadata.py @@ -0,0 +1,37 @@ +"""Utils for hadnling metadata of segmentation models.""" + +# 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. + + +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 \ No newline at end of file From ecbd29abd78762228137bc5e37c5289fa75528e4 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Thu, 20 Apr 2023 03:53:34 +0900 Subject: [PATCH 2/6] Add tests for ir metadata --- otx/algorithms/segmentation/task.py | 2 +- .../cli/classification/test_classification.py | 2 +- tests/integration/cli/detection/test_detection.py | 2 +- .../cli/semantic_segmentation/test_segmentation.py | 2 +- tests/test_suite/run_test_command.py | 9 ++++++++- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/otx/algorithms/segmentation/task.py b/otx/algorithms/segmentation/task.py index 1d14e1887a1..0c1477ba663 100644 --- a/otx/algorithms/segmentation/task.py +++ b/otx/algorithms/segmentation/task.py @@ -231,7 +231,7 @@ def export( onnx_file = outputs.get("onnx") ir_extra_data = get_seg_model_api_configuration( - self._task_environment.label_schema, self._task_type, self._hyperparams + self._task_environment.label_schema, self._hyperparams ) embed_ir_model_data(xml_file, ir_extra_data) 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) From 972a0d3033a55e39595ec8c8d636e410c4a9dfe8 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Thu, 20 Apr 2023 03:59:23 +0900 Subject: [PATCH 3/6] update license --- otx/algorithms/segmentation/utils/__init__.py | 15 ++------------- otx/algorithms/segmentation/utils/metadata.py | 15 ++------------- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/otx/algorithms/segmentation/utils/__init__.py b/otx/algorithms/segmentation/utils/__init__.py index 7924799032d..82b29880450 100644 --- a/otx/algorithms/segmentation/utils/__init__.py +++ b/otx/algorithms/segmentation/utils/__init__.py @@ -1,18 +1,7 @@ """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 diff --git a/otx/algorithms/segmentation/utils/metadata.py b/otx/algorithms/segmentation/utils/metadata.py index fb97293f7d1..4d9b5a62c46 100644 --- a/otx/algorithms/segmentation/utils/metadata.py +++ b/otx/algorithms/segmentation/utils/metadata.py @@ -1,18 +1,7 @@ """Utils for hadnling metadata of segmentation models.""" -# 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 mmcv.utils import ConfigDict From 97b6a3a212252428cc5200f67a48e9abb2ac64e6 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Thu, 20 Apr 2023 04:19:36 +0900 Subject: [PATCH 4/6] Fix linters --- otx/algorithms/segmentation/adapters/mmseg/task.py | 1 - otx/algorithms/segmentation/task.py | 4 +--- otx/algorithms/segmentation/utils/metadata.py | 3 ++- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/otx/algorithms/segmentation/adapters/mmseg/task.py b/otx/algorithms/segmentation/adapters/mmseg/task.py index fa357728542..f5801b1814f 100644 --- a/otx/algorithms/segmentation/adapters/mmseg/task.py +++ b/otx/algorithms/segmentation/adapters/mmseg/task.py @@ -56,7 +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.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/task.py b/otx/algorithms/segmentation/task.py index 0c1477ba663..82dd39b2799 100644 --- a/otx/algorithms/segmentation/task.py +++ b/otx/algorithms/segmentation/task.py @@ -230,9 +230,7 @@ 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 - ) + 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: diff --git a/otx/algorithms/segmentation/utils/metadata.py b/otx/algorithms/segmentation/utils/metadata.py index 4d9b5a62c46..ec20a028ae4 100644 --- a/otx/algorithms/segmentation/utils/metadata.py +++ b/otx/algorithms/segmentation/utils/metadata.py @@ -5,6 +5,7 @@ from mmcv.utils import ConfigDict + from otx.api.entities.label_schema import LabelSchemaEntity @@ -23,4 +24,4 @@ def get_seg_model_api_configuration(label_schema: LabelSchemaEntity, hyperparams omz_config[("model_info", "labels")] = all_labels - return omz_config \ No newline at end of file + return omz_config From 182f0c0936a2125f1e8874049b3493fb532ce4e8 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Thu, 20 Apr 2023 04:24:20 +0900 Subject: [PATCH 5/6] pd changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 24147f86cf9f7fafa0c9e258ea0fe1510e4334a7 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Thu, 20 Apr 2023 05:56:00 +0900 Subject: [PATCH 6/6] Fix segmentation unit test --- tests/unit/algorithms/segmentation/test_task.py | 1 + 1 file changed, 1 insertion(+) 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()