From a9a7cd5dcf4e43cc2f3b571472adf997df14d89f Mon Sep 17 00:00:00 2001 From: junxnone Date: Sat, 25 May 2024 00:12:04 +0800 Subject: [PATCH 1/4] fix winclip openvino inference output without mask Signed-off-by: junxnone --- src/anomalib/models/image/winclip/lightning_model.py | 2 +- src/anomalib/models/image/winclip/torch_model.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/anomalib/models/image/winclip/lightning_model.py b/src/anomalib/models/image/winclip/lightning_model.py index 0d86697faf..16f2b9d4bf 100644 --- a/src/anomalib/models/image/winclip/lightning_model.py +++ b/src/anomalib/models/image/winclip/lightning_model.py @@ -125,7 +125,7 @@ def configure_optimizers() -> None: def validation_step(self, batch: dict[str, str | torch.Tensor], *args, **kwargs) -> dict: """Validation Step of WinCLIP.""" del args, kwargs # These variables are not used. - batch["pred_scores"], batch["anomaly_maps"] = self.model(batch["image"]) + batch["anomaly_maps"], batch["pred_scores"] = self.model(batch["image"]) return batch @property diff --git a/src/anomalib/models/image/winclip/torch_model.py b/src/anomalib/models/image/winclip/torch_model.py index 5c69853db6..f39fe371a7 100644 --- a/src/anomalib/models/image/winclip/torch_model.py +++ b/src/anomalib/models/image/winclip/torch_model.py @@ -250,7 +250,7 @@ def forward(self, batch: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: size=batch.shape[-2:], mode="bilinear", ) - return image_scores, pixel_scores.squeeze(1) + return pixel_scores.squeeze(1), image_scores def _compute_zero_shot_scores( self, From 7ce79a62a2201312e64369735e8d07b97014ef54 Mon Sep 17 00:00:00 2001 From: junxnone Date: Thu, 30 May 2024 11:39:20 +0800 Subject: [PATCH 2/4] Revert "fix for winclip openvino inference without mask" This reverts commit 2a9691fed6065ee4061707eadc4b80df79a51902. --- src/anomalib/models/image/winclip/lightning_model.py | 2 +- src/anomalib/models/image/winclip/torch_model.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/anomalib/models/image/winclip/lightning_model.py b/src/anomalib/models/image/winclip/lightning_model.py index 16f2b9d4bf..0d86697faf 100644 --- a/src/anomalib/models/image/winclip/lightning_model.py +++ b/src/anomalib/models/image/winclip/lightning_model.py @@ -125,7 +125,7 @@ def configure_optimizers() -> None: def validation_step(self, batch: dict[str, str | torch.Tensor], *args, **kwargs) -> dict: """Validation Step of WinCLIP.""" del args, kwargs # These variables are not used. - batch["anomaly_maps"], batch["pred_scores"] = self.model(batch["image"]) + batch["pred_scores"], batch["anomaly_maps"] = self.model(batch["image"]) return batch @property diff --git a/src/anomalib/models/image/winclip/torch_model.py b/src/anomalib/models/image/winclip/torch_model.py index f39fe371a7..5c69853db6 100644 --- a/src/anomalib/models/image/winclip/torch_model.py +++ b/src/anomalib/models/image/winclip/torch_model.py @@ -250,7 +250,7 @@ def forward(self, batch: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: size=batch.shape[-2:], mode="bilinear", ) - return pixel_scores.squeeze(1), image_scores + return image_scores, pixel_scores.squeeze(1) def _compute_zero_shot_scores( self, From b63cb5febfc5e91778fb5ead2c4239a6448b7d64 Mon Sep 17 00:00:00 2001 From: junxnone Date: Thu, 30 May 2024 11:52:21 +0800 Subject: [PATCH 3/4] fix openvino inferencer results parser Signed-off-by: junxnone --- .../deploy/inferencers/openvino_inferencer.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/anomalib/deploy/inferencers/openvino_inferencer.py b/src/anomalib/deploy/inferencers/openvino_inferencer.py index 91970a0adf..3321a96dcc 100644 --- a/src/anomalib/deploy/inferencers/openvino_inferencer.py +++ b/src/anomalib/deploy/inferencers/openvino_inferencer.py @@ -30,7 +30,6 @@ else: logger.warning("OpenVINO is not installed. Please install OpenVINO to use OpenVINOInferencer.") - class OpenVINOInferencer(Inferencer): """OpenVINO implementation for the inference. @@ -147,7 +146,7 @@ def load_model(self, path: str | Path | tuple[bytes, bytes]) -> tuple[Any, Any, compile_model = core.compile_model(model=model, device_name=self.device, config=self.config) input_blob = compile_model.input(0) - output_blob = compile_model.output(0) + output_blob = compile_model.outputs return input_blob, output_blob, compile_model @@ -254,7 +253,7 @@ def post_process(self, predictions: np.ndarray, metadata: dict | DictConfig | No if metadata is None: metadata = self.metadata - predictions = predictions[self.output_blob] + predictions = [predictions[i] for i in self.output_blob] # Initialize the result variables. anomaly_map: np.ndarray | None = None @@ -263,13 +262,17 @@ def post_process(self, predictions: np.ndarray, metadata: dict | DictConfig | No # If predictions returns a single value, this means that the task is # classification, and the value is the classification prediction score. - if len(predictions.shape) == 1: + if len(predictions) == 1: task = TaskType.CLASSIFICATION - pred_score = predictions + pred_score = predictions[0] else: - task = TaskType.SEGMENTATION - anomaly_map = predictions.squeeze() - pred_score = anomaly_map.reshape(-1).max() + task = TaskType.SEGMENTATION if self.task is None else self.task + for item in predictions: + if len(item.shape) == 1: + pred_score = item[0] + else: + anomaly_map = item.squeeze() + # Common practice in anomaly detection is to assign anomalous # label to the prediction if the prediction score is greater From 52e5364b432a8cecb2d478a058de78bfc2f6ab6b Mon Sep 17 00:00:00 2001 From: junxnone Date: Fri, 31 May 2024 12:01:37 +0800 Subject: [PATCH 4/4] fix format Signed-off-by: junxnone --- src/anomalib/deploy/inferencers/openvino_inferencer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/anomalib/deploy/inferencers/openvino_inferencer.py b/src/anomalib/deploy/inferencers/openvino_inferencer.py index 3321a96dcc..6211797a81 100644 --- a/src/anomalib/deploy/inferencers/openvino_inferencer.py +++ b/src/anomalib/deploy/inferencers/openvino_inferencer.py @@ -30,6 +30,7 @@ else: logger.warning("OpenVINO is not installed. Please install OpenVINO to use OpenVINOInferencer.") + class OpenVINOInferencer(Inferencer): """OpenVINO implementation for the inference. @@ -273,7 +274,6 @@ def post_process(self, predictions: np.ndarray, metadata: dict | DictConfig | No else: anomaly_map = item.squeeze() - # Common practice in anomaly detection is to assign anomalous # label to the prediction if the prediction score is greater # than the image threshold.