lime_image

LIME image explainer.

Module Contents

Classes

LIMEImage

Wrapper around the LIME explainer.

class lime_image.LIMEImage(kernel_width=25, kernel=None, verbose=False, feature_selection='auto', random_state=None, axis_labels=None, preprocess_function=None)[source]

Wrapper around the LIME explainer.

Lime explainer byMarco Tulio Correia Ribeiro (https://github.com/marcotcr/lime).

explain(model_or_function, input_data, labels, top_labels=None, num_features=10, num_samples=5000, return_masks=True, positive_only=False, hide_rest=True, **kwargs)[source]

Run the LIME explainer.

Parameters:
  • model_or_function (callable or str) – The function that runs the model to be explained _or_ the path to a ONNX model on disk.

  • input_data (np.ndarray) – Data to be explained. Must be an “RGB image”, i.e. with values in the [0,255] range.

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

  • top_labels – Top labels

  • num_features (int) – Number of features

  • num_samples (int) – Number of samples

  • return_masks (bool) – If true, return discretized masks. Otherwise, return LIME scores

  • positive_only (bool) – Positive only

  • hide_rest (bool) – Hide rest

  • kwargs – These parameters are passed on

Other keyword arguments: see the LIME documentation for LimeImageExplainer.explain_instance and ImageExplanation.get_image_and_mask:

Returns:

An array (np.ndarray) containing the LIME explanations for each class.

Return type:

np.ndarray

_prepare_image_data(input_data)[source]

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

Parameters:

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

Returns:

transformed input data, preprocessing function to use with utils.get_function()

_get_full_preprocess_function(channel_axis_index, dtype, greyscale=False)[source]

Creates a full preprocessing function.

Creates a preprocessing function that incorporates both the (optional) user’s preprocessing function, as well as any needed dtype and shape conversions

Parameters:
  • channel_axis_index (int) – Axis index of the channels in the input data

  • dtype (type) – Data type of the input data (e.g. np.float32)

  • greyscale (bool) – Whether or not the data is greyscale (i.e. one channel)

Returns:

Function that first ensures the data has the same shape and type as the input data, then runs the users’ preprocessing function

_get_explanation_values(label: int, explanation: lime.lime_image.ImageExplanation) numpy.array[source]

Get the importance scores from LIME in a salience map.

Leverages the ImageExplanation class from LIME to generate salience maps. These salience maps are constructed using the segmentation masks from the explanation and fills these with the scores from the surrogate model (default for LIME is Ridge regression) used for the explanation.

Parameters:
  • label – The class label for the given explanation

  • explanation – An Image Explanation generated by LIME

Returns:

A salience map containing the feature importances from LIME