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:
- image
np.ndarray
The image to explain as a three-dimensional array of shape (image_width, image_height, 3) representing an RGB image.
- predict_fn
Callable
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_idx
int
, 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_method
str
, default “slic” The method used to segment the image into superpixels. See
visualime.lime.create_segments()
for available methods.- segmentation_settings
dict
, optional Keyword arguments to pass to the segmentation method. See
visualime.lime.create_segments()
for details.- num_of_samples
int
, default 64 The number of sample images to generate for calculating the explanation.
- p
float
, default 0.33 The probability of a segment to be replaced in a sample.
- segment_selection_method
str
, default “by_weight” The segment selection method. Possible choices are “by_weight” and “forward_selection”.
- num_segments_to_select
int
, 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.
- image
- Returns:
- segment_mask
np.ndarray
A two-dimensional array of shape (image_width, image_height).
- segment_weights
np.ndarray
A one-dimensional array whose length is equal to the number of segments.
- segment_mask
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:
- image
np.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_mask
np.ndarray
The mask generated by
visualime.lime.create_segments()
: An array of shape (image_width, image_height).- segment_weights
np.ndarray
The weights produced by
visualime.lime.weigh_segments()
: A one-dimensional array of length num_of_segments.- positive
str
orint
3-tuple (RGB), optional, default “green” The color for the segments that contribute positively towards the classification. If None, these segments are not colored.
- negative
str
orint
3-tuple (RGB), optional The color for the segments that contribute negatively towards the classification. If None (the default), these segments are not colored.
- opacity
float
, default 0.7 The opacity of the explanation overlay.
- coverage
float
, 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_segments
int
, optional The number of segments to be colored.
- min_num_of_segments
int
, default 0 The minimum number of segments to be colored.
- max_num_of_segments
int
, optional The maximum number of segments to be colored.
- image
- Returns:
PIL.Image
The rendered explanation as a PIL Image object.
Examples
TODO: Add end-to-end example