High-level interface (visualime.explain)

visualime.explain.explain_classification(image: ndarray, predict_fn: Callable[[ndarray], ndarray], label_idx: int | None = None, segmentation_method: Literal['felzenszwalb', 'slic', 'quickshift', 'watershed'] = 'slic', segmentation_settings: Dict[str, Any] | None = None, num_of_samples: int = 64, p: float = 0.33, segment_selection_method: str = 'by_weight', num_segments_to_select: int | None = 0) Tuple[ndarray, ndarray][source]

Explain why the classifier called through predict_fn classifies the image into a particular class using the LIME algorithm.

For more detailed control, we recommend you create your own function, using this function as a template.

Parameters:
imagenp.ndarray

The image to explain as a three-dimensional array of shape (image_width, image_height, 3) representing an RGB image.

predict_fnCallable

A function that takes an input of shape (num_of_samples, image_width, image_height, 3) and returns an array of shape (num_of_samples, num_of_classes).

label_idxint, optional

The index of the label to explain in the output of predict_fn(). If not given, this corresponds to the class that predict_fn() assigns to the image.

segmentation_methodstr, default “slic”

The method used to segment the image into superpixels. See visualime.lime.create_segments() for available methods.

segmentation_settingsdict, optional

Keyword arguments to pass to the segmentation method. See visualime.lime.create_segments() for details.

num_of_samplesint, default 64

The number of sample images to generate for calculating the explanation.

pfloat, default 0.33

The probability of a segment to be replaced in a sample.

segment_selection_methodstr, default “by_weight”

The segment selection method. Possible choices are “by_weight” and “forward_selection”.

num_segments_to_selectint, optional

The number of segments to be considered when fitting the linear model to determine the segment_weights. If not given, half of the generated segments are selected.

Returns:
segment_masknp.ndarray

A two-dimensional array of shape (image_width, image_height).

segment_weightsnp.ndarray

A one-dimensional array whose length is equal to the number of segments.

Examples

TODO: Add end-to-end example

visualime.explain.render_explanation(image: ndarray, segment_mask: ndarray, segment_weights: ndarray, *, positive: Tuple[int, int, int] | str | None = 'green', negative: Tuple[int, int, int] | str | None = None, opacity: float = 0.7, coverage: float | None = 0.2, num_of_segments: int | None = None, min_num_of_segments: int = 0, max_num_of_segments: int | None = None) Image[source]

Render a visual explanation from the segment_mask and segment_weights produced by visualime.explain.explain_classification().

Segments are selected in the order of descending weight until the desired coverage or num_of_segments is reached. Exactly one of these parameters has to be specified when calling the function.

If both ‘positive’ and ‘negative’ colors are specified, the ‘coverage’ or num_of_segments will be distributed evenly between the two classes of segments.

Parameters:
imagenp.ndarray

The image to explain the classification for as a three-dimensional array of shape (image_width, image_height, 3) representing an RGB image.

segment_masknp.ndarray

The mask generated by visualime.lime.create_segments(): An array of shape (image_width, image_height).

segment_weightsnp.ndarray

The weights produced by visualime.lime.weigh_segments(): A one-dimensional array of length num_of_segments.

positivestr or int 3-tuple (RGB), optional, default “green”

The color for the segments that contribute positively towards the classification. If None, these segments are not colored.

negativestr or int 3-tuple (RGB), optional

The color for the segments that contribute negatively towards the classification. If None (the default), these segments are not colored.

opacityfloat, default 0.7

The opacity of the explanation overlay.

coveragefloat, optional, default 0.2

The coverage of each overlay relative to the area of the image. E.g., if set to 0.2 (the default), about 20% of the image are colored.

num_of_segmentsint, optional

The number of segments to be colored.

min_num_of_segmentsint, default 0

The minimum number of segments to be colored.

max_num_of_segmentsint, optional

The maximum number of segments to be colored.

Returns:
PIL.Image

The rendered explanation as a PIL Image object.

Examples

TODO: Add end-to-end example