Using LIME to explore Brain Tumor MRI Classification Model

Brooke Smyth
9 min readFeb 1, 2022

You’ve just finished iterating through various neural network models, and have finally achieved amazing accuracy, recall, precision, and F1 scores. There are choruses of ‘Hallelujah’ going off in your mind, because you think you’re finally done! But then you remember the dark reality of the black box, the curse of so many classifiers! And so your own biological neural networks inform you that some time should be spent on trying to interpret your computational one, or any other type of machine learning model for which you would like more clarity.

Luckily, the python library LIME makes this task manageable. LIME is an acronym which stands for Local Interpretable Model-agnostic Explanations. The “Local” refers to the fact that LIME focuses on interpreting the behavior of one data point at a time, not every data point in the dataset. In the case of image classification, this means that it interprets one image at a time. “Interpretable” is pretty self explanatory — it just means that LIME provides you with an explanation of what causes your model to make its decisions. “Model-agnostic” means that LIME works on any kind of black-box classifier.

There are four main steps that LIME follows in order to interpret a model:

  1. Local Instance Picking and Artificial Data Generation
  2. Artificial Data Point Prediction
  3. Calculation of Distance and Weight Assignment
  4. Use of a linear classifier to classify the perturbed images

Local Instance Picking and Artificial Data Generation

Code directions for how to generate the superpixel image can be found in this blog post.

When using LIME for explaining the behavior of an Image Classification model, the first step is to choose a random data point, or image, to focus on. Next, a series of random data points are generated. In an image classification problem, what this looks like is dividing an image up into superpixels, or groups of pixels which share similarities. Then, some of these superpixels are randomly turned off, or made to appear black. This process is called image perturbation. Many different images are created based off of one image by varying which superpixels are turned on and off.

Top left image is original; the other three are perturbed images with some superpixels turned off. Code directions for this image can be found in this blog post.

Artificial Data Point Prediction

After each perturbed image is generated, the model initially used to classify the images will be used to predict a class for each perturbed image, along with each superpixel.

Calculation of Distance and Weight Assignment

Because artificial data points closest to the data instance being explored are more important, due to the local nature of lime, distance must be assessed between each perturbed image and the random image being explored. This is done using cosine similarity, and outputs a value between zero and one. The closer the value is to one, the smaller the angle between the two data points, and the greater the similarity.

Image Link

Use a linear classifier to classify the perturbed images

After calculating the weights for each perturbed image, it is time to fit a linear classifier to the perturbed images (which are broken down into superpixels), the labels for each superpixel of those perturbed images, and the weights. This linear model then determines which superpixels are most influential for classifying the image through ranking the coefficients of each superpixel. For a more in depth walk-through on how LIME works, visit this blog post.

Using LIME on Brain MRIs

Now that we have an idea of how LIME works, let’s actually use it! After completing a project where I used a convolutional neural network to classify a series of brain MRIs as normal or containing a tumor, I decided to use LIME to shed light on what features my model was picking up on in order to classify the images. Before digging into the LIME details, a visual explanation of what brain tumors in MRIs look like will help future explanations make more sense. Below are shown six different T1 or FLAIR MRIs of four different appearances of gliomas, and two different appearances of meningiomas (from a kaggle dataset entitled “Brain Tumor Classification MRI”). It is also important to note that the way fluid and brain matter appear in a scan depends on the method used to take an MRI. The following are the different MRI methods and the way they cause the brain to look:

  • T1: fluid appears black, axonal matter (nerve fibers) is a light grey, and the gray matter (where the neurons are) is a darker gray.
  • FLAIR: similar to T1, where fluid appears black, axonal matter (nerve fibers) is a light grey, and the gray matter (where the neurons are) is a darker gray.
  • T2: fluid is white, axonal fibers are a dark gray, and gray matter is light gray.

The images below are all T1 images.

Images can be found in the kaggle dataset “Brain Tumor Classification MRI”

One of the aspects that makes brain tumor classification difficult is the variety in appearance that exists within and between different tumor types. Sometimes the tumor appears black (fluid filled) with a white ring, other times the tumor is a lacy, garbled white, sometimes a bright mass with smaller bright patches within, and other times the tumor appears large and white. This variety can be one of the factors which causes the model to make mistakes, which will be explored below.

Interpretation of Incorrectly Identified Images

In order to compare the original image with the image output by lime, I created the following function using lime methods:

def ImageLabelsLime2(img, img_idx, exp_num, lab_num, true_label, pred_label):'''
img = numpy array of image you want to explore with LIME
img_idx = the index of the image you want to explore with LIME (assuming that the index numbers of the incorrectly labelled images are in a list)exp_num = the instantiated instance of LimeExplainer that you will use to explain this imagelab_num = the number of labels you want for the "top_labels" argument of the ".explain_instance" methodtrue_label = the true label for the particular index of the image you are exploring (should come from a list)pred_label = the predicted label for the particular index of the image you are exploring (should come from a list)'''image = imgexplanation_num = exp_num.explain_instance(
val_img[img_idx].astype('double'),
best_model.predict,
top_labels=lab_num,
hide_color=0,
num_samples=1000)
temp_num, mask_num = explanation_num.get_image_and_mask(
explanation_num.top_labels[1],
positive_only=False,
num_features=10,
hide_rest=False)
lime_graph = mark_boundaries(temp_num / 2 + 0.5, mask_num)plt.subplot(1, 2, 1)
plt.imshow(image)
plt.subplot(1, 2, 2)
plt.imshow(lime_graph)
plt.tight_layout()
plt.show()
print(f'Correct Label: {true_label}')
print(f'Predicted Label: {pred_label}')

The function prints out the original image to the left, and the LIME analyzed picture to the right. It shows an image with the ten most influential superpixels, where the green segments represent the top superpixels that increased the probability of belonging to the predicted class that was ultimately chosen by the model, and the red parts are the top superpixels which decreased the probability of belonging to the chosen predicted class.

First Image

the number zero corresponds to the “no tumor” label, and the number 1 corresponds the “tumor” label

In this image, LIME has identified the large, round region of the base of the pons as a region with a high probability of causing the image to be misclassified as containing a tumor. Oftentimes tumors appear in MRIs as large white masses, so perhaps this is what the model is picking up on, and why it incorrectly labels this image.

Second Image

The parts of the image that LIME labels as contributing to high and low probabilities of being labelled as the predicted class appear to be fairly random in this MRI. From what LIME shows us, the model was not able to locate the actual tumor in a region contributing to a high probability that this image will be classified as having a tumor, so that is probably why the image was misclassified.

Fourth Image

In this image, the LIME identifies the eye region as contributing to a high probability that the MRI is misclassified as containing a tumor. This is probably because tumors can also appear in MRIs as a large mass with a white ring and dark center, which is exactly what the eyes look like.

Fifth Image

In this image, LIME identifies the cerebellum and a large region at the bottom of the brain with lots of black/white contrast as contributing to a high probability that the image is misclassified as containing a tumor. Because tumors can contain regions with high black/white contrast in an MRI, and because neural networks are trained to pick up on edges, perhaps this is what the model is picking up on and what causes it to misclassify this image.

Sixth Image

This image is a T2 MRI, where fluid appears white, axonal material appears dark grey, and grey matter appears a lighter gray. Here, LIME predicted the eyes as having a high probabilty of contributing to the image being misclassified as containing a tumor. Oftentimes tumors appear in MRIs as large white masses, so perhaps this is what the model is picking up on, and why it incorrectly labels this image.

Correctly Labelled Images

Now I will explore a few LIME analyses of images which were correctly identified.

In this image, LIME correctly identifies the tumor as contributing to a high probability that the image will be classified as containing a tumor. This tumor has a white outline and gray/black inside, and it is interesting to note that the eye region is also being identified as contributing positively to the tumor prediction. It appears so similar to the tumor, as it is a dark region surrounded by a white circular structure. LIME is showing us that the model has been trained to pick up on this kind of structure.

This image is another example of LIME showing us that the model is doing a good job at picking up on tumors with a white circular surrounding and a black, fluid filled center.

This image is interesting because the model correctly classified this MRI as containing a tumor, but LIME shows that the model was not picking up the actual tumor as being a region which increased the probability of the MRI being classified as containing a tumor. So, the model was right for the wrong reasons.

The tumor in this image is the same type as in the last ( a meningioma) and it is clear that the model correctly identified the tumor region.

This is another example of a meningioma where the model was able to correctly identify the superpixels which made up the tumor, and was correctly classified.

Conclusions

The biggest insight that LIME revealed is how much the eye area really confused the neural network! Whether the MRI was a T1 or T2 scan, the network recognized the eye region as a highly probable tumor containing region. LIME also revealed that the network is able to do a pretty good job at recognizing a variety of tumor types. For a more in-depth analysis, visit my Google Colab notebook on GitHub.

--

--