Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Sep 24, 2024
1 parent bfeac6c commit 05ac349
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def __init__(
prepare_model=prepare_model,
)
self.bounds = Bounds([0.0, 0.0], [1.0, 1.0])
self.num_iterations_per_kernel: int | None = None
self.kernel_widths: List[float] | np.ndarray | None = None

def generate_saliency_map( # type: ignore
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def __init__(
)
self.deletion = False
self.predictions = {}
self.num_iterations_per_kernel: int | None = None
self.divisors: List[float] | np.ndarray | None = None

def generate_saliency_map( # type: ignore
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def __init__(
super().__init__(
model=model, postprocess_fn=postprocess_fn, preprocess_fn=preprocess_fn, device_name=device_name
)
self.num_masks: int | None = None
self.num_cells: int | None = None

if prepare_model:
self.prepare_model()
Expand Down Expand Up @@ -84,13 +86,11 @@ def generate_saliency_map(
"""
data_preprocessed = self.preprocess_fn(data)

num_masks, num_cells = self._preset_parameters(preset, num_masks, num_cells)
self.num_masks, self.num_cells = self._preset_parameters(preset, num_masks, num_cells)

saliency_maps = self._run_synchronous_explanation(
data_preprocessed,
target_indices,
num_masks,
num_cells,
prob,
seed,
)
Expand Down Expand Up @@ -134,8 +134,6 @@ def _run_synchronous_explanation(
self,
data_preprocessed: np.ndarray,
target_classes: List[int] | None,
num_masks: int,
num_cells: int,
prob: float,
seed: int,
) -> np.ndarray:
Expand All @@ -152,8 +150,8 @@ def _run_synchronous_explanation(
rand_generator = np.random.default_rng(seed=seed)

saliency_maps = np.zeros((num_targets, input_size[0], input_size[1]))
for _ in tqdm(range(0, num_masks), desc="Explaining in synchronous mode"):
mask = self._generate_mask(input_size, num_cells, prob, rand_generator)
for _ in tqdm(range(0, self.num_masks), desc="Explaining in synchronous mode"):
mask = self._generate_mask(input_size, self.num_cells, prob, rand_generator)
# Add channel dimensions for masks
if is_bhwc_layout(data_preprocessed):
masked = np.expand_dims(mask, 2) * data_preprocessed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ <h1>Source code for openvino_xai.methods.black_box.aise.classification</h1><div
<span class="n">prepare_model</span><span class="o">=</span><span class="n">prepare_model</span><span class="p">,</span>
<span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">bounds</span> <span class="o">=</span> <span class="n">Bounds</span><span class="p">([</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">],</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">])</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_iterations_per_kernel</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">kernel_widths</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">]</span> <span class="o">|</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="AISEClassification.generate_saliency_map">
<a class="viewcode-back" href="../../../../../api-manual.html#openvino_xai.methods.AISEClassification.generate_saliency_map">[docs]</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ <h1>Source code for openvino_xai.methods.black_box.aise.detection</h1><div class
<span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">deletion</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">predictions</span> <span class="o">=</span> <span class="p">{}</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_iterations_per_kernel</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">divisors</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">]</span> <span class="o">|</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span>

<div class="viewcode-block" id="AISEDetection.generate_saliency_map">
<a class="viewcode-back" href="../../../../../api-manual.html#openvino_xai.methods.AISEDetection.generate_saliency_map">[docs]</a>
Expand Down
12 changes: 5 additions & 7 deletions releases/1.1.0/_modules/openvino_xai/methods/black_box/rise.html
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ <h1>Source code for openvino_xai.methods.black_box.rise</h1><div class="highligh
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
<span class="n">model</span><span class="o">=</span><span class="n">model</span><span class="p">,</span> <span class="n">postprocess_fn</span><span class="o">=</span><span class="n">postprocess_fn</span><span class="p">,</span> <span class="n">preprocess_fn</span><span class="o">=</span><span class="n">preprocess_fn</span><span class="p">,</span> <span class="n">device_name</span><span class="o">=</span><span class="n">device_name</span>
<span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_masks</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_cells</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="kc">None</span> <span class="o">=</span> <span class="kc">None</span>

<span class="k">if</span> <span class="n">prepare_model</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">prepare_model</span><span class="p">()</span>
Expand Down Expand Up @@ -485,13 +487,11 @@ <h1>Source code for openvino_xai.methods.black_box.rise</h1><div class="highligh
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">data_preprocessed</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">preprocess_fn</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

<span class="n">num_masks</span><span class="p">,</span> <span class="n">num_cells</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_preset_parameters</span><span class="p">(</span><span class="n">preset</span><span class="p">,</span> <span class="n">num_masks</span><span class="p">,</span> <span class="n">num_cells</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">num_masks</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_cells</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_preset_parameters</span><span class="p">(</span><span class="n">preset</span><span class="p">,</span> <span class="n">num_masks</span><span class="p">,</span> <span class="n">num_cells</span><span class="p">)</span>

<span class="n">saliency_maps</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run_synchronous_explanation</span><span class="p">(</span>
<span class="n">data_preprocessed</span><span class="p">,</span>
<span class="n">target_indices</span><span class="p">,</span>
<span class="n">num_masks</span><span class="p">,</span>
<span class="n">num_cells</span><span class="p">,</span>
<span class="n">prob</span><span class="p">,</span>
<span class="n">seed</span><span class="p">,</span>
<span class="p">)</span>
Expand Down Expand Up @@ -536,8 +536,6 @@ <h1>Source code for openvino_xai.methods.black_box.rise</h1><div class="highligh
<span class="bp">self</span><span class="p">,</span>
<span class="n">data_preprocessed</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span>
<span class="n">target_classes</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">|</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">num_masks</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
<span class="n">num_cells</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
<span class="n">prob</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
<span class="n">seed</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
Expand All @@ -554,8 +552,8 @@ <h1>Source code for openvino_xai.methods.black_box.rise</h1><div class="highligh
<span class="n">rand_generator</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">default_rng</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="n">seed</span><span class="p">)</span>

<span class="n">saliency_maps</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">num_targets</span><span class="p">,</span> <span class="n">input_size</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">input_size</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
<span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="n">tqdm</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">num_masks</span><span class="p">),</span> <span class="n">desc</span><span class="o">=</span><span class="s2">&quot;Explaining in synchronous mode&quot;</span><span class="p">):</span>
<span class="n">mask</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_generate_mask</span><span class="p">(</span><span class="n">input_size</span><span class="p">,</span> <span class="n">num_cells</span><span class="p">,</span> <span class="n">prob</span><span class="p">,</span> <span class="n">rand_generator</span><span class="p">)</span>
<span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="n">tqdm</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_masks</span><span class="p">),</span> <span class="n">desc</span><span class="o">=</span><span class="s2">&quot;Explaining in synchronous mode&quot;</span><span class="p">):</span>
<span class="n">mask</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_generate_mask</span><span class="p">(</span><span class="n">input_size</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">num_cells</span><span class="p">,</span> <span class="n">prob</span><span class="p">,</span> <span class="n">rand_generator</span><span class="p">)</span>
<span class="c1"># Add channel dimensions for masks</span>
<span class="k">if</span> <span class="n">is_bhwc_layout</span><span class="p">(</span><span class="n">data_preprocessed</span><span class="p">):</span>
<span class="n">masked</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">mask</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="n">data_preprocessed</span>
Expand Down

0 comments on commit 05ac349

Please sign in to comment.