Пример #1
0
# ### 5.2 Visualizing Learning with GradCAM
#

# One of the challenges of using deep learning in medicine is that the complex architecture used for neural networks makes them much harder to interpret compared to traditional machine learning models (e.g. linear models).
#
# One of the most common approaches aimed at increasing the interpretability of models for computer vision tasks is to use Class Activation Maps (CAM).
# - Class activation maps are useful for understanding where the model is "looking" when classifying an image.
#
# In this section we will use a [GradCAM's](https://arxiv.org/abs/1610.02391) technique to produce a heatmap highlighting the important regions in the image for predicting the pathological condition.
# - This is done by extracting the gradients of each predicted class, flowing into our model's final convolutional layer. Look at the `util.compute_gradcam` which has been provided for you in `util.py` to see how this is done with the Keras framework.
#
# It is worth mentioning that GradCAM does not provide a full explanation of the reasoning for each classification probability.
# - However, it is still a useful tool for "debugging" our model and augmenting our prediction so that an expert could validate that a prediction is indeed due to the model focusing on the right regions of the image.

# First we will load the small training set and setup to look at the 4 classes with the highest performing AUC measures.

# In[66]:

df = pd.read_csv("nih/train-small.csv")
IMAGE_DIR = "nih/images-small/"

# only show the labels with top 4 AUC
labels_to_show = np.take(labels, np.argsort(auc_rocs)[::-1])[:4]

# Now let's look at a few specific images.

# In[67]:

util.compute_gradcam(model, '00008270_015.png', IMAGE_DIR, df, labels,
                     labels_to_show)
Пример #2
0
# First we will load the small training set and setup to look at the 4 classes with the highest performing AUC measures.

# In[28]:

df = pd.read_csv("nih/train-small.csv")
IMAGE_DIR = "nih/images-small/"

# only show the labels with top 4 AUC
labels_to_show = np.take(labels, np.argsort(auc_rocs)[::-1])[:4]

# Now let's look at a few specific images.

# In[29]:

util.compute_gradcam(model, '00008270_015.png', IMAGE_DIR, df, labels,
                     labels_to_show)

# In[30]:

util.compute_gradcam(model, '00011355_002.png', IMAGE_DIR, df, labels,
                     labels_to_show)

# In[31]:

util.compute_gradcam(model, '00029855_001.png', IMAGE_DIR, df, labels,
                     labels_to_show)

# In[32]:

util.compute_gradcam(model, '00005410_000.png', IMAGE_DIR, df, labels,
                     labels_to_show)
## GradCAM: Visualizing Learning 


layer_name = 'post_bn'

df = df_test
IMAGE_DIR = "Data/out/test/"

# only show the labels with top 4 AUC
labels_to_show = np.take(labels, np.argsort(auc_rocs)[::-1])[:4]

images = ['demd117_CC_Right.png', 'demd122_MLO_Right.png', 
          'demd1501_CC_Right.png', 'demd50449_MLO_Right.png']

for image in images:

    # choose image with positive label
    util.compute_gradcam(model, image, IMAGE_DIR, df, labels,
                         labels_to_show,
                         layer_name= layer_name)


'-----------------DEBUG GRADCAM--------------------------'