Skip to content

Occlusion sensitivity

View colab tutorial | View source

The Occlusion sensitivity method sweep a patch that occludes pixels over the images, and use the variations of the model prediction to deduce critical areas.1

Quote

[...] this method, referred to as Occlusion, replacing one feature \(x_i\) at the time with a baseline and measuring the effect of this perturbation on the target output.

-- Towards better understanding of the gradient-based attribution methods for Deep Neural Networks (2017)2

with \(S_c\) the unormalized class score (layer before softmax) and \(\bar{x}\) a baseline, the Occlusion sensitivity map \(\phi\) is defined as :

\[ \phi_i = S_c(x) - S_c(x_{[x_i = \bar{x}]}) \]

Example

from xplique.attributions import Occlusion

# load images, labels and model
# ...

method = Occlusion(model, patch_size=(10, 10),
                   patch_stride=(2, 2), occlusion_value=0.5)
explanations = method.explain(images, labels)

Notebooks

Occlusion

Used to compute the Occlusion sensitivity method, sweep a patch that occludes pixels over the images and use the variations of the model prediction to deduce critical areas.

__init__(self,
         model: Callable,
         batch_size: Optional[int] = 32,
         operator: Union[xplique.commons.operators_operations.Tasks, str,
         Callable[[keras.src.engine.training.Model, tensorflow.python.framework.tensor.Tensor, tensorflow.python.framework.tensor.Tensor], float], None] = None,
         patch_size: Union[int, Tuple[int, int]] = 3,
         patch_stride: Union[int, Tuple[int, int]] = 3,
         occlusion_value: float = 0.0)

Parameters

  • model : Callable

    • The model from which we want to obtain explanations

  • batch_size : Optional[int] = 32

    • Number of pertubed samples to explain at once.

      Default to 32.

  • operator : Union[xplique.commons.operators_operations.Tasks, str, Callable[[keras.src.engine.training.Model, tensorflow.python.framework.tensor.Tensor, tensorflow.python.framework.tensor.Tensor], float], None] = None

    • Function g to explain, g take 3 parameters (f, x, y) and should return a scalar, with f the model, x the inputs and y the targets. If None, use the standard operator g(f, x, y) = f(x)[y].

  • patch_size : Union[int, Tuple[int, int]] = 3

    • Size of the patches to apply, if integer then assume an hypercube.

  • patch_stride : Union[int, Tuple[int, int]] = 3

    • Stride between two patches, if integer then assume an hypercube.

  • occlusion_value : float = 0.0

    • Value used as occlusion.

explain(self,
        inputs: Union[tf.Dataset, tensorflow.python.framework.tensor.Tensor, numpy.ndarray],
        targets: Union[tensorflow.python.framework.tensor.Tensor, numpy.ndarray, None] = None) -> tensorflow.python.framework.tensor.Tensor

Compute Occlusion sensitivity for a batch of samples.

Parameters

  • inputs : Union[tf.Dataset, tensorflow.python.framework.tensor.Tensor, numpy.ndarray]

    • Dataset, Tensor or Array. Input samples to be explained.

      If Dataset, targets should not be provided (included in Dataset).

      Expected shape among (N, W), (N, T, W), (N, H, W, C).

      More information in the documentation.

  • targets : Union[tensorflow.python.framework.tensor.Tensor, numpy.ndarray, None] = None

    • Tensor or Array. One-hot encoding of the model's output from which an explanation is desired. One encoding per input and only one output at a time. Therefore, the expected shape is (N, output_size).

      More information in the documentation.

Return

  • explanations : tensorflow.python.framework.tensor.Tensor

    • Occlusion sensitivity, same shape as the inputs, except for the channels.


Info

patch_size and patch_stride will define patch to apply to the original input. Thus, a combination of patches will generate pertubed samples of the original input (masked by patches with occlusion_value value). Consequently, the number of pertubed instances of an input depend on those parameters. Too little value of those two arguments on large image might lead to an incredible amount of pertubed samples and increase compuation time. On another hand too huge values might not be accurate enough.