From bb86c9389ec9e932a39b9172ea25250da93bdaab Mon Sep 17 00:00:00 2001 From: Ashwin Vaidya Date: Wed, 23 Aug 2023 16:23:49 +0200 Subject: [PATCH 1/2] bug fix for legacy openvino models --- src/otx/algorithms/anomaly/tasks/openvino.py | 40 +++++++++++++++++--- src/otx/cli/utils/io.py | 4 ++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/otx/algorithms/anomaly/tasks/openvino.py b/src/otx/algorithms/anomaly/tasks/openvino.py index cc65ef74294..3457cee2529 100644 --- a/src/otx/algorithms/anomaly/tasks/openvino.py +++ b/src/otx/algorithms/anomaly/tasks/openvino.py @@ -26,6 +26,7 @@ import numpy as np import openvino.runtime as ov from addict import Dict as ADDict +from anomalib.data.utils.transform import get_transforms from anomalib.deploy import OpenVINOInferencer from nncf.common.quantization.structs import QuantizationPreset from omegaconf import OmegaConf @@ -216,16 +217,45 @@ def get_metadata(self) -> Dict: """Get Meta Data.""" metadata = {} if self.task_environment.model is not None: - metadata = json.loads(self.task_environment.model.get_data("metadata").decode()) - metadata["image_threshold"] = np.array(metadata["image_threshold"], dtype=np.float32).item() - metadata["pixel_threshold"] = np.array(metadata["pixel_threshold"], dtype=np.float32).item() - metadata["min"] = np.array(metadata["min"], dtype=np.float32).item() - metadata["max"] = np.array(metadata["max"], dtype=np.float32).item() + try: + metadata = json.loads(self.task_environment.model.get_data("metadata").decode()) + self._populate_metadata(metadata) + except Exception: + # model is from version 1.2.x + metadata = self._populate_metadata_legacy(self.task_environment.model) else: raise ValueError("Cannot access meta-data. self.task_environment.model is empty.") return metadata + def _populate_metadata_legacy(self, model: ModelEntity) -> Dict[str, Any]: + """Populates metadata for models for version 1.2.x.""" + image_threshold = np.frombuffer(model.get_data("image_threshold"), dtype=np.float32) + pixel_threshold = np.frombuffer(model.get_data("pixel_threshold"), dtype=np.float32) + min_value = np.frombuffer(model.get_data("min"), dtype=np.float32) + max_value = np.frombuffer(model.get_data("max"), dtype=np.float32) + transform = get_transforms( + config=self.config.dataset.transform_config.train, + image_size=tuple(self.config.dataset.image_size), + to_tensor=True, + ) + metadata = { + "transform": transform.to_dict(), + "image_threshold": image_threshold, + "pixel_threshold": pixel_threshold, + "min": min_value, + "max": max_value, + "task": str(self.task_type).lower().split("_")[-1], + } + return metadata + + def _populate_metadata(self, metadata: Dict[str, Any]): + """Populates metadata for models from version 1.4 onwards.""" + metadata["image_threshold"] = np.array(metadata["image_threshold"], dtype=np.float32).item() + metadata["pixel_threshold"] = np.array(metadata["pixel_threshold"], dtype=np.float32).item() + metadata["min"] = np.array(metadata["min"], dtype=np.float32).item() + metadata["max"] = np.array(metadata["max"], dtype=np.float32).item() + def evaluate(self, output_resultset: ResultSetEntity, evaluation_metric: Optional[str] = None): """Evaluate the performance of the model. diff --git a/src/otx/cli/utils/io.py b/src/otx/cli/utils/io.py index 73941eb1a6c..3770fb279bf 100644 --- a/src/otx/cli/utils/io.py +++ b/src/otx/cli/utils/io.py @@ -51,6 +51,10 @@ "visual_prompting_image_encoder.bin", "visual_prompting_decoder.xml", "visual_prompting_decoder.bin", + "image_threshold", # NOTE: used for compatibility with with OTX 1.2.x. Remove when all Geti projects are upgraded. + "pixel_threshold", # NOTE: used for compatibility with with OTX 1.2.x. Remove when all Geti projects are upgraded. + "min", # NOTE: used for compatibility with with OTX 1.2.x. Remove when all Geti projects are upgraded. + "max", # NOTE: used for compatibility with with OTX 1.2.x. Remove when all Geti projects are upgraded. ) From 8617057690d9f95719a61f5731b6a1a1afaf9f98 Mon Sep 17 00:00:00 2001 From: Ashwin Vaidya Date: Mon, 29 Jan 2024 14:23:03 +0100 Subject: [PATCH 2/2] Set reverse_input_channels to True --- src/otx/algorithms/anomaly/tasks/inference.py | 2 +- src/otx/algorithms/anomaly/tasks/openvino.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/otx/algorithms/anomaly/tasks/inference.py b/src/otx/algorithms/anomaly/tasks/inference.py index aac3d525b5e..c59b76e9366 100644 --- a/src/otx/algorithms/anomaly/tasks/inference.py +++ b/src/otx/algorithms/anomaly/tasks/inference.py @@ -357,7 +357,7 @@ def _add_metadata_to_ir(self, model_file: str, export_type: ExportType) -> None: if "min" in metadata and "max" in metadata: extra_model_data[("model_info", "normalization_scale")] = metadata["max"] - metadata["min"] - extra_model_data[("model_info", "reverse_input_channels")] = False + extra_model_data[("model_info", "reverse_input_channels")] = True # convert BGR to RGB extra_model_data[("model_info", "model_type")] = "AnomalyDetection" labels = [] diff --git a/src/otx/algorithms/anomaly/tasks/openvino.py b/src/otx/algorithms/anomaly/tasks/openvino.py index d96516a5752..be681277369 100644 --- a/src/otx/algorithms/anomaly/tasks/openvino.py +++ b/src/otx/algorithms/anomaly/tasks/openvino.py @@ -453,7 +453,7 @@ def _metadata_in_ir_format(self) -> Dict[Tuple[str, str], Union[str, int, float, if "min" in metadata and "max" in metadata: extra_model_data[("model_info", "normalization_scale")] = metadata["max"] - metadata["min"] - extra_model_data[("model_info", "reverse_input_channels")] = False + extra_model_data[("model_info", "reverse_input_channels")] = True # convert BGR to RGB extra_model_data[("model_info", "model_type")] = "AnomalyDetection" extra_model_data[("model_info", "labels")] = "Normal Anomaly" return extra_model_data