From 0295382a7878ffb34c70e4732c9764f2fecd12dc Mon Sep 17 00:00:00 2001 From: Evgeny Tsykunov Date: Wed, 21 Aug 2024 23:06:57 +0900 Subject: [PATCH] comments --- examples/run_detection.py | 2 +- openvino_xai/explainer/visualizer.py | 16 +++++++++++----- openvino_xai/methods/__init__.py | 3 ++- openvino_xai/methods/black_box/aise.py | 3 ++- .../methods/black_box/test_black_box_method.py | 1 - 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/examples/run_detection.py b/examples/run_detection.py index 8773b03e..f123e2cc 100644 --- a/examples/run_detection.py +++ b/examples/run_detection.py @@ -129,7 +129,7 @@ def explain_black_box(args): # Save saliency maps for visual inspection if args.output is not None: output = Path(args.output) / "detection_black_box" - explanation.save(output, Path(args.image_path).stem) + explanation.save(output, f"{Path(args.image_path).stem}_") def main(argv): diff --git a/openvino_xai/explainer/visualizer.py b/openvino_xai/explainer/visualizer.py index 9bf378b2..aedd8295 100644 --- a/openvino_xai/explainer/visualizer.py +++ b/openvino_xai/explainer/visualizer.py @@ -171,10 +171,16 @@ def _apply_metadata(metadata: Dict[Task, Any], saliency_map_np: np.ndarray, indi box, score, label_index = metadata[Task.DETECTION][target_index] x1, y1, x2, y2 = box cv2.rectangle(saliency_map, (int(x1), int(y1)), (int(x2), int(y2)), color=(255, 0, 0), thickness=2) - box_name = f"{label_index}|{score:.2f}" - org = int(x1), int(y1 - 5) + box_label = f"{label_index}|{score:.2f}" + box_label_loc = int(x1), int(y1 - 5) cv2.putText( - saliency_map, box_name, org=org, fontFace=1, fontScale=1, color=(255, 0, 0), thickness=2 + saliency_map, + box_label, + org=box_label_loc, + fontFace=1, + fontScale=1, + color=(255, 0, 0), + thickness=2, ) return saliency_map_np @@ -241,14 +247,14 @@ def _apply_overlay( def _update_explanation_with_processed_sal_map( explanation: Explanation, saliency_map_np: np.ndarray, - indices: List, + target_indices: List, ) -> Explanation: dict_sal_map: Dict[int | str, np.ndarray] = {} if explanation.layout in ONE_MAP_LAYOUTS: dict_sal_map["per_image_map"] = saliency_map_np[0] saliency_map_np = dict_sal_map elif explanation.layout in MULTIPLE_MAP_LAYOUTS: - for index, sal_map in zip(indices, saliency_map_np): + for index, sal_map in zip(target_indices, saliency_map_np): dict_sal_map[index] = sal_map else: raise ValueError diff --git a/openvino_xai/methods/__init__.py b/openvino_xai/methods/__init__.py index d5a88e25..51963bae 100644 --- a/openvino_xai/methods/__init__.py +++ b/openvino_xai/methods/__init__.py @@ -3,7 +3,7 @@ """ XAI algorithms. """ -from openvino_xai.methods.black_box.aise import AISEClassification +from openvino_xai.methods.black_box.aise import AISEClassification, AISEDetection from openvino_xai.methods.black_box.rise import RISE from openvino_xai.methods.white_box.activation_map import ActivationMap from openvino_xai.methods.white_box.base import WhiteBoxMethod @@ -25,4 +25,5 @@ "DetClassProbabilityMap", "RISE", "AISEClassification", + "AISEDetection", ] diff --git a/openvino_xai/methods/black_box/aise.py b/openvino_xai/methods/black_box/aise.py index 4c144727..6eccca70 100644 --- a/openvino_xai/methods/black_box/aise.py +++ b/openvino_xai/methods/black_box/aise.py @@ -4,7 +4,7 @@ import collections import math from abc import ABC, abstractmethod -from typing import Callable, Dict, List, Mapping, Tuple +from typing import Any, Callable, Dict, List, Mapping, Tuple import numpy as np import openvino.runtime as ov @@ -371,6 +371,7 @@ def generate_saliency_map( # type: ignore self._mask_generator = GaussianPerturbationMask(self.input_size) saliency_maps = {} + self.metadata: Dict[Task, Any] = collections.defaultdict(dict) for target in target_indices: self.target_box = boxes[target] self.target_label = labels[target] diff --git a/tests/unit/methods/black_box/test_black_box_method.py b/tests/unit/methods/black_box/test_black_box_method.py index 60374d93..7f889e18 100644 --- a/tests/unit/methods/black_box/test_black_box_method.py +++ b/tests/unit/methods/black_box/test_black_box_method.py @@ -55,7 +55,6 @@ def preprocess_det_fn(x: np.ndarray) -> np.ndarray: @staticmethod def postprocess_det_fn(x) -> np.ndarray: """Returns boxes, scores, labels.""" - # return x["boxes"][:, :4], x["boxes"][:, 4], x["labels"] return x["boxes"][0][:, :4], x["boxes"][0][:, 4], x["labels"][0]