kernelshap_image

Module Contents

Classes

KERNELSHAPImage

Kernel SHAP implementation based on shap https://github.com/slundberg/shap.

Functions

_create_heatemaps(shap_values_list, image_segments)

Create heatmaps from the shap values and the image segments.

class kernelshap_image.KERNELSHAPImage(axis_labels=None, preprocess_function=None)[source]

Kernel SHAP implementation based on shap https://github.com/slundberg/shap.

static _segment_image(image, n_segments, compactness, sigma, **kwargs)[source]

Create segmentation to explain by segment, not every pixel.

This could help speed-up the calculation when the input size is very large.

This function segments image using k-means clustering in Color-(x,y,z) space. It uses scikit-image.

Parameters:
  • image (np.ndarray) – Input image to be segmented.

  • n_segments (int) – The (approximate) number of labels in the segmented output image

  • compactness (int) – Balances color proximity and space proximity.

  • sigma (float) – Width of Gaussian smoothing kernel

  • kwargs – These keyword parameters are passed on

Returns:

integer mask indicating segment labels with the shape of the input image. the number of segments is less than or equal to n_segments.

Check keyword arguments for the skimage.segmentation.slic function via the following link: https://scikit-image.org/docs/dev/api/skimage.segmentation.html#skimage.segmentation.slic

explain(model_or_function, input_data, labels, nsamples='auto', background=None, n_segments=100, compactness=10.0, sigma=0, **kwargs)[source]

Run the KernelSHAP explainer.

The model will be called with the function of image segmentation.

Parameters:
  • model_or_function (str) – The path to a ONNX model on disk.

  • input_data (np.ndarray) – Data to be explained. It is mandatory to only provide a single example as input. This is because KernelShap is generally used for sample-based interpretability, training a separate interpretable model to explain a model prediction on each individual example. The input dimension must be [batch, height, width, color_channels] or [batch, color_channels, height, width] (see axis_labels)

  • labels (Iterable(int)) – Indices of classes to be explained

  • nsamples ("auto" or int) – Number of times to re-evaluate the model when explaining each prediction. More samples lead to lower variance estimates of the SHAP values. The “auto” setting uses nsamples = 2 * X.shape[1] + 2048

  • background (int) – Background color for the masked image

  • n_segments (int) – The (approximate) number of labels in the segmented output image

  • compactness (int) – Balances color proximity and space proximity. Higher values give more weight to space proximity, making superpixel shapes more square/cubic.

  • sigma (float) – Width of Gaussian smoothing kernel for pre-processing for each dimension of the image. Zero means no smoothing.

  • kwargs – These keyword parameters are passed on

Other keyword arguments: see the documentation of kernel explainer of SHAP

(also in function “shap_values”) via:

https://github.com/slundberg/shap/blob/master/shap/explainers/_kernel.py

and the documentation of image segmentation via:

https://scikit-image.org/docs/dev/api/skimage.segmentation.html#skimage.segmentation.slic

Returns:

Explanation heatmap of Shapley values for each class (np.ndarray).

_prepare_image_data(input_data)[source]

Transforms the data to be of the shape and type KernelSHAP expects.

Parameters:

input_data (NumPy-compatible array) – Data to be explained

Returns:

transformed input data

_mask_image(features, segmentation, image, background=None, channels_axis_index=2, datatype=np.float32)[source]

Define a function that depends on a binary mask representing if an image region is hidden.

Parameters:
  • features (np.ndarray) – A matrix of samples (# samples x # features) on which to explain the model’s output.

  • segmentation (np.ndarray) – Image segmentations generated by the function _segment_image

  • image (np.ndarray) – Image to be explained

  • background (int) – Background color for the masked image

  • channels_axis_index (int) – See the function _prepare_image_data

  • datatype (np.dtype) – Datatype for the returned value

_runner(features)[source]

Define a runner/wrapper to load models and values.

Parameters:

features (np.ndarray) – A matrix of samples (# samples x # features) on which to explain the model’s output.

kernelshap_image._create_heatemaps(shap_values_list, image_segments)[source]

Create heatmaps from the shap values and the image segments.

The final heatmaps has a shape of (n_classes, *image_segments.shape).