Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
negvet committed Aug 21, 2024
1 parent 23714d4 commit 0295382
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/run_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
16 changes: 11 additions & 5 deletions openvino_xai/explainer/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion openvino_xai/methods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,4 +25,5 @@
"DetClassProbabilityMap",
"RISE",
"AISEClassification",
"AISEDetection",
]
3 changes: 2 additions & 1 deletion openvino_xai/methods/black_box/aise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion tests/unit/methods/black_box/test_black_box_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down

0 comments on commit 0295382

Please sign in to comment.