.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_sphinx_gallery_auto_transparency_xmpl_transparency_lime_image.py: ========================== Using LIME Image Explainer ========================== This example illustrates how to use the LIME image explainer to explain a prediction. This example shows how to use the image LIME implementation -- :class:`fatf.transparency.predictions.surrogate_image_explainers.ImageBlimeyLime` -- to explain a prediction of a black-box probabilistic model. .. rst-class:: sphx-glr-horizontal * .. image:: /sphinx_gallery_auto/transparency/images/sphx_glr_xmpl_transparency_lime_image_001.png :class: sphx-glr-multi-img * .. image:: /sphinx_gallery_auto/transparency/images/sphx_glr_xmpl_transparency_lime_image_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'bottom-left': 0.059825904043669643, 'bottom-right': -0.27639956335921306, 'top-left': 0.4983489307756843, 'top-right': 0.04997774025460273} | .. code-block:: default # Author: Kacper Sokol # License: new BSD from pprint import pprint import matplotlib.pyplot as plt import numpy as np import fatf import fatf.utils.models as fatf_models import fatf.transparency.predictions.surrogate_image_explainers as fatf_exp import fatf.vis.lime as fatf_vis_lime print(__doc__) # Fix random seed fatf.setup_random_seed(42) # Create a simple data set r, g, b, k = [255, 0, 0], [0, 255, 0], [0, 0, 255], [0, 0, 0] X = np.array( [ [[r, g], [b, k]], [[r, b], [g, k]], [[r, k], [b, g]], [[k, g], [b, r]], [[k, b], [g, r]], [[g, k], [b, r]] ], dtype=np.uint8 ) y = np.array([0, 0, 0, 1, 1, 1]) feature_names = { 'Segment #1': 'top-left', 'Segment #2': 'top-right', 'Segment #3': 'bottom-left', 'Segment #4': 'bottom-right' } class_names = {0: 'top-left-red', 1: 'bottom-right-red'} # Create a model that operates of 3-D images class KNNimg(fatf_models.KNN): def fit(self, X, y): X_r_flat = np.array([i[:, :, 0].flatten() for i in X], dtype=np.uint8) super().fit(X_r_flat, y) def predict(self, X): X_r_flat = np.array([i[:, :, 0].flatten() for i in X], dtype=np.uint8) return super().predict(X_r_flat) def predict_proba(self, X): X_r_flat = np.array([i[:, :, 0].flatten() for i in X], dtype=np.uint8) return super().predict_proba(X_r_flat) # Train a model clf = KNNimg(k=1) clf.fit(X, y) # Choose an instance to be explained image = X[0] image_class = 0 # Display the instance plt.axis('off') plt.imshow(image) # Create a LIME image explainer lime = fatf_exp.ImageBlimeyLime( image, clf, ratio=1, kernel_size=1.05, max_dist=10, colour='random') # Explain an instance lime_explanation_ = lime.explain_instance(explained_class=image_class) # Remap the explanation lime_explanation = {feature_names[i]: j for i, j in lime_explanation_.items()} # Display the textual explanation pprint(lime_explanation) # Plot the explanation fatf_vis_lime.plot_lime(lime_explanation) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.216 seconds) .. _sphx_glr_download_sphinx_gallery_auto_transparency_xmpl_transparency_lime_image.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: binder-badge .. image:: https://mybinder.org/badge_logo.svg :target: https://mybinder.org/v2/gh/fat-forensics/fat-forensics-doc/master?filepath=notebooks/sphinx_gallery_auto/transparency/xmpl_transparency_lime_image.ipynb :width: 150 px .. container:: sphx-glr-download :download:`Download Python source code: xmpl_transparency_lime_image.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: xmpl_transparency_lime_image.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_