Text and image explainability with LIME

Explainable AI in Python

Fouad Trad

Machine Learning Engineer

Text-based models

  • Process and interpret written language
  • Example: Sentiment analysis
  • Black box models
  • LimeTextExplainer explains such models
    • Finds how each word impacts prediction

Sentiment analysis model receiving a user review and predicting the corresponding sentiment ('Positive' or 'Negative').

Explainable AI in Python

LIME text explainer

from lime.lime_text import LimeTextExplainer

text_instance = "This product has great features but a poor design."
def model_predict(instance): ... return class_probabilities
explainer = LimeTextExplainer()
exp = explainer.explain_instance(
text_instance,
model_predict
)
exp.as_pyplot_figure()

output_sentiment.png

Explainable AI in Python

Image-based models

  • Highly complex
  • Interpret visual data
  • Example: Food classification
  • LimeImageExplainer explains such models
    • Finds which parts of image impact predictions

Food classification model that receives the image of a pizza and predicts which type of food is in the image.

Explainable AI in Python

LIME image explainer

from lime.lime_image import LimeImageExplainer

explainer = LimeImageExplainer()
explanation = explainer.explain_instance(
image,
model_predict,
num_samples=50
)
temp, _ = explanation.get_image_and_mask(
explanation.top_labels[0],
hide_rest=True
)

Ice cream image.

Explainable AI in Python

LIME image explainer

from lime.lime_image import LimeImageExplainer

explainer = LimeImageExplainer()
explanation = explainer.explain_instance(
  image, 
  model_predict,  
  num_samples=50
)

temp, _ = explanation.get_image_and_mask(
  explanation.top_labels[0], 
  hide_rest=True
)

plt.imshow(temp)

Ice cream image with less relevant parts being obscured with a black color.

Explainable AI in Python

Let's practice!

Explainable AI in Python

Preparing Video For Download...