Skip to content

Commit

Permalink
fix bbox bug and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
negvet committed Aug 20, 2024
1 parent c02021f commit b7879b8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
13 changes: 10 additions & 3 deletions openvino_xai/methods/black_box/aise.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,15 @@ def generate_saliency_map( # type: ignore

saliency_maps = {}
for target in target_indices:
self.kernel_params_hist = collections.defaultdict(list)
self.pred_score_hist = collections.defaultdict(list)

self.target_box = boxes[target]
self.target_label = labels[target]

if self.target_box[0] == self.target_box[2] or self.target_box[1] == self.target_box[3]:
continue

self.kernel_params_hist = collections.defaultdict(list)
self.pred_score_hist = collections.defaultdict(list)

self._process_box()
saliency_map_per_target = self._run_synchronous_explanation()
if scale_output:
Expand Down Expand Up @@ -427,6 +430,10 @@ def _process_box(self, padding_coef: float = 0.5) -> None:
x_to = min(target_box_scaled[2] + box_width * padding_coef, 1.0)
y_from = max(target_box_scaled[1] - box_height * padding_coef, 0.0)
y_to = min(target_box_scaled[3] + box_height * padding_coef, 1.0)

if x_from < x_to or y_from < y_to:
raise ValueError("Bounding box data is incorrect.")

self.bounds = Bounds([x_from, y_from], [x_to, y_to])

def _get_loss(self, data_perturbed: np.array) -> float:
Expand Down
29 changes: 27 additions & 2 deletions tests/unit/explanation/test_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import numpy as np
import pytest

from openvino_xai.common.parameters import Task
from openvino_xai.common.utils import get_min_max, scaling
from openvino_xai.explainer.explanation import Explanation
from openvino_xai.explainer.visualizer import Visualizer, colormap, overlay, resize

SALIENCY_MAPS = [
(np.random.rand(1, 5, 5) * 255).astype(np.uint8),
(np.random.rand(1, 2, 5, 5) * 255).astype(np.uint8),
{
0: (np.random.rand(5, 5) * 255).astype(np.uint8),
1: (np.random.rand(5, 5) * 255).astype(np.uint8),
},
]

EXPLAIN_ALL_CLASSES = [
Expand Down Expand Up @@ -98,7 +103,7 @@ class TestVisualizer:
@pytest.mark.parametrize("colormap", [True, False])
@pytest.mark.parametrize("overlay", [True, False])
@pytest.mark.parametrize("overlay_weight", [0.5, 0.3])
def test_Visualizer(
def test_visualizer(
self,
saliency_maps,
explain_all_classes,
Expand Down Expand Up @@ -142,7 +147,7 @@ def test_Visualizer(
for map_ in explanation.saliency_map.values():
assert map_.shape[:2] == original_input_image.shape[:2]

if saliency_maps.ndim == 3 and not overlay:
if isinstance(saliency_maps, np.ndarray) and saliency_maps.ndim == 3 and not overlay:
explanation = Explanation(saliency_maps, targets=-1)
visualizer = Visualizer()
explanation_output_size = visualizer(
Expand All @@ -157,3 +162,23 @@ def test_Visualizer(
maps_data = explanation.saliency_map
maps_size = explanation_output_size.saliency_map
assert np.all(maps_data["per_image_map"] == maps_size["per_image_map"])

if isinstance(saliency_maps, dict):
metadata = {
Task.DETECTION: {
0: ([5, 0, 7, 4], 0.5, 0),
1: ([2, 5, 9, 7], 0.5, 0),
}
}
explanation = Explanation(saliency_maps, targets=-1, metadata=metadata)
visualizer = Visualizer()
explanation_output_size = visualizer(
explanation=explanation,
original_input_image=original_input_image,
output_size=(20, 20),
scaling=scaling,
resize=resize,
colormap=colormap,
overlay=overlay,
overlay_weight=overlay_weight,
)
15 changes: 13 additions & 2 deletions tests/unit/methods/black_box/test_black_box_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,23 @@ def test_run(self, target_indices, fxt_data_root: Path):
assert saliency_map[ref_target].shape == (416, 416)
assert (saliency_map[ref_target] >= 0).all() and (saliency_map[ref_target] <= 255).all()

tmp = saliency_map[0]

actual_sal_vals = saliency_map[0][150, 240:250].astype(np.int16)
ref_sal_vals = np.array([152, 168, 184, 199, 213, 225, 235, 243, 247, 249], dtype=np.uint8)
assert np.all(np.abs(actual_sal_vals - ref_sal_vals) <= 1)

def test_target_none(self, fxt_data_root: Path):
model = self.get_det_model(fxt_data_root)

aise_method = AISEDetection(model, self.postprocess_det_fn, self.preprocess_det_fn)
saliency_map = aise_method.generate_saliency_map(
data=self.image,
target_indices=None,
preset=Preset.SPEED,
num_iterations_per_kernel=1,
divisors=[5],
)
assert len(saliency_map) == 56

def test_preset(self, fxt_data_root: Path):
model = self.get_det_model(fxt_data_root)
method = AISEDetection(model, self.postprocess_det_fn, self.preprocess_det_fn)
Expand Down

0 comments on commit b7879b8

Please sign in to comment.