keras_explainable.engine package

Submodules

keras_explainable.engine.explaining module

explain(method, model, x, y=None, batch_size=None, verbose='auto', steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False, force=True, **method_params)[source]

Explain the outputs of model with respect to the inputs or an intermediate signal, using an AI explaining method.

Usage:

x = np.random.normal((1, 224, 224, 3))
y = np.asarray([[16, 32]])

model = tf.keras.applications.ResNet50V2(classifier_activation=None)

scores, maps = ke.explain(
    ke.methods.gradient.gradients,
    model, x, y,
    postprocessing=filters.absolute_normalize,
)
Parameters:
  • method (Callable) – An AI explaining function, as the ones contained in methods module.

  • model (tf.keras.Model) – The model whose predictions should be explained.

  • x (Union[np.ndarray, tf.Tensor, tf.data.Dataset]) – the input data for the model.

  • y (Optional[Union[np.ndarray, tf.Tensor]], optional) – the indices in the output tensor that should be explained. If none, an activation map is computed for each unit. Defaults to None.

  • batch_size (Optional[int], optional) – the batch size used by method. Defaults to 32.

  • verbose (Union[str, int], optional) – wether to show a progress bar during the calculation of the explaining maps. Defaults to “auto”.

  • steps (Optional[int], optional) – the number of steps, if x is a tf.data.Dataset of unknown cardinallity. Defaults to None.

  • callbacks (List[Callback], optional) – list of callbacks called during the explaining procedure. Defaults to None.

  • max_queue_size (int, optional) – the queue size when retrieving inputs. Used if x is a generator. Defaults to 10.

  • workers (int, optional) – the number of workers used when retrieving inputs. Defaults to 1.

  • use_multiprocessing (bool, optional) – wether to employ multi-process or multi-threading when retrieving inputs, when x is a generator. Defaults to False.

  • force (bool, optional) – to force the creation of the explaining function. Can be set to False if the same function is always applied to a model, avoiding retracing. Defaults to True.

Besides the parameters described above, any named parameters passed to this function will be collected into methods_params and passed onto the explain_step() and method functions. The most common ones are:

  • indices_batch_dims (int): The dimensions marked as batch when gathering units described by y. Ignore if y is None.

  • indices_axis (int): The axes from which to gather units described by y. Ignore if y is None.

  • spatial_axis (Tuple[int]): The axes containing the positional visual info. We assume inputs to contain 2D images or videos in the shape (B1, B2, …, BN, H, W, 3). For 3D image data, set spatial_axis to (1, 2, 3) or (-4, -3, -2).

  • postprocessing (Callable): A function to process the activation maps before normalization (most commonly adopted being maximum(x, 0) and abs).

Raises:

ValueError – the explaining method produced in an unexpected.

Returns:

logits and explaining maps tensors.

Return type:

Tuple[np.ndarray, np.ndarray]

explain_step(model, method, data, spatial_axis=(-3, -2), postprocessing=None, resizing=True, **params)[source]
Return type:

Tuple[Tensor, Tensor]

make_data_handler(model, x, y, batch_size=None, steps=None, max_queue_size=10, workers=1, use_multiprocessing=False)[source]
make_explain_function(model, method, params, force=False)[source]
partial_explain(method, **default_params)[source]

Wrapper for explaining methods.

Parameters:

method (Callable) – the explaining method being wrapped by explain.

Module contents