lime_image ========== .. py:module:: lime_image .. autoapi-nested-parse:: LIME image explainer. Classes ------- .. autoapisummary:: lime_image.LIMEImage Module Contents --------------- .. py:class:: LIMEImage(kernel_width=25, kernel=None, verbose=False, feature_selection='auto', random_state=None, axis_labels=None, preprocess_function=None) Wrapper around the LIME explainer. Lime explainer byMarco Tulio Correia Ribeiro (https://github.com/marcotcr/lime). .. py:attribute:: preprocess_function :value: None .. py:attribute:: axis_labels .. py:attribute:: explainer .. py:method:: 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) Run the LIME explainer. :param model_or_function: The function that runs the model to be explained _or_ the path to a ONNX model on disk. :type model_or_function: callable or str :param input_data: Data to be explained. Must be an "RGB image", i.e. with values in the [0,255] range. :type input_data: np.ndarray :param labels: Indices of classes to be explained :type labels: Iterable(int) :param top_labels: Top labels :param num_features: Number of features :type num_features: int :param num_samples: Number of samples :type num_samples: int :param return_masks: If true, return discretized masks. Otherwise, return LIME scores :type return_masks: bool :param positive_only: Positive only :type positive_only: bool :param hide_rest: Hide rest :type hide_rest: bool :param kwargs: These parameters are passed on Other keyword arguments: see the LIME documentation for LimeImageExplainer.explain_instance and ImageExplanation.get_image_and_mask: - https://lime-ml.readthedocs.io/en/latest/lime.html#lime.lime_image.LimeImageExplainer.explain_instance - https://lime-ml.readthedocs.io/en/latest/lime.html#lime.lime_image.ImageExplanation.get_image_and_mask :returns: An array (np.ndarray) containing the LIME explanations for each class. :rtype: np.ndarray .. py:method:: _prepare_image_data(input_data) Transforms the data to be of the shape and type LIME expects. :param input_data: Data to be explained :type input_data: NumPy-compatible array :returns: transformed input data, preprocessing function to use with utils.get_function() .. py:method:: _get_full_preprocess_function(channel_axis_index, dtype, greyscale=False) 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 :param channel_axis_index: Axis index of the channels in the input data :type channel_axis_index: int :param dtype: Data type of the input data (e.g. np.float32) :type dtype: type :param greyscale: Whether or not the data is greyscale (i.e. one channel) :type greyscale: bool :returns: Function that first ensures the data has the same shape and type as the input data, then runs the users' preprocessing function .. py:method:: _get_explanation_values(label: int, explanation: lime.lime_image.ImageExplanation) -> numpy.array 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. :param label: The class label for the given explanation :param explanation: An Image Explanation generated by LIME :returns: A salience map containing the feature importances from LIME