Class Activation Map (CAM)


By Prof. Seungchul Lee
http://iai.postech.ac.kr/
Industrial AI Lab at POSTECH

Table of Contents

  • Attention

  • Visualizing and Understanding Convolutional Networks

0. Video Lectures

In [2]:
%%html
<center><iframe src="https://www.youtube.com/embed/-e4H5Fqm4-w?rel=0" 
width="560" height="315" frameborder="0" allowfullscreen></iframe></center>
In [1]:
%%html
<center><iframe src="https://www.youtube.com/embed/yaDqxQA7rzA?rel=0" 
width="560" height="315" frameborder="0" allowfullscreen></iframe></center>

1. CNN with a Fully Connected Layer

The conventional CNN can be conceptually divided into two parts. One part is feature extraction and the other is classification. In the feature extraction process, convolution is used to extract the features of the input data so that the classification can be performed well. The classification process classifies which group each input data belongs to by using the extracted features from the input data.

When we visually identify images, we do not look at the whole image; instead, we intuitively focus on the most important parts of the image. CNN learning is similar to the way humans focus. When its weights are optimized, the more important parts are given higher weights. But generally, we are not able to recognize this because the generic CNN goes through a fully connected layer and makes the features extracted by the convolution layer more abstract.



1.1. Issues on CNN (or Deep Learning)

  • Deep learning performs well comparing with any other existing algorithms
  • But works as a black box

    • A classification result is simply returned without knowing how the classification results are derived → little interpretability
  • When we visually identify images, we do not look at the whole image

  • Instead, we intuitively focus on the most important parts of the image
  • When CNN weights are optimized, the more important parts are given higher weights

  • Class activation map (CAM)

    • We can determine which parts of the image the model is focusing on, based on the learned weights
    • Highlighting the importance of the image region to the prediction



2. CAM: CNN with a Global Average Pooling

  • shed light on how it explicitly enables the convolutional neural network to have remarkable localization ability
  • the heatmap is the class activation map, highlighting the importance of the image region to the prediction

The deep learning model is a black box model. When input data is received, a classification result of 1 or 0 is simply returned for the binary classification problem, without knowing how the classification results are derived. Meanwhile, The class activation map (CAM) is capable of interpreting the results of the classification. We can determine which parts of the image the model is focusing on. Through an analysis of which part of the image the model is focusing on, we are able to interpret which part of the image is considered important.

The class activation map (CAM) is a modified convolution layer. It directly highlights the important parts of the spatial grid of an image. As a result, we can see the emphasized parts of the model. The below figure describes the procedure for class activation mapping.



The feature maps of the last convolution layer can be interpreted as a collection of visual spatial locations focused on by the model. The CAM can be obtained by taking a linear sum of the features. They all have different weights and thus can obtain spatial locations according to various input images through a linear combination. For a given image, $f_k(x,y)$ represents the feature map of unit $k$ in the last convolution layer at spatial location $(x,y)$. For a given class $c$, the class score, $S_c$, is expressed as the following equation.


$$S_c = \sum_k \omega_k^c \sum_{x,y} f_k(x,y)= \sum_{x,y} \sum_k \omega_k^c \; f_k(x,y)$$

where $\omega_k^c$ the weight corresponding to class $c$ for unit $k$. The class activation map for class $c$ is denoted as $M_c$.


$$M_c(x,y) = \sum_k \omega_k^c \; f_k(x,y)$$

$M_c$ directly indicates the importance of the feature map at a spatial grid $(x,y)$ of the class $c$. Finally the output of the softmax for class $c$ is,


$$P_c = \frac{\exp\left(S_c\right)}{\sum_c \exp\left(S_c\right)}$$

In case of the CNN, the size of the feature map is reduced by the pooling layer. By simple up-sampling, it is possible to identify attention image regions for each label.

3. CAM with MNIST

In [1]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import cv2
In [2]:
mnist = tf.keras.datasets.mnist

(train_x, train_y), (test_x, test_y) = mnist.load_data()

train_x, test_x = train_x/255.0, test_x/255.0
train_x = train_x.reshape((train_x.shape[0], 28, 28, 1))
test_x = test_x.reshape((test_x.shape[0], 28, 28, 1))
In [3]:
# digits of 7 and 9 will be used

train_idx = np.hstack((np.where(train_y == 7), np.where(train_y == 9)))[0]
test_idx = np.hstack((np.where(test_y == 7), np.where(test_y == 9)))[0]

train_imgs   = train_x[train_idx]
train_labels = train_y[train_idx]
test_imgs    = test_x[test_idx]
test_labels  = test_y[test_idx]

n_train      = train_imgs.shape[0]
n_test       = test_imgs.shape[0]

print ("The number of train images: {}, shape: {}".format(n_train, train_imgs.shape))
print ("The number of test images: {}, shape: {}".format(n_test, test_imgs.shape))
The number of train images: 12214, shape: (12214, 28, 28, 1)
The number of test images: 2037, shape: (2037, 28, 28, 1)
In [4]:
# binary classification
# label 7 to 0 
# label 9 to 1

train_y = (train_labels == 9).astype(int)
test_y = (test_labels == 9).astype(int)
In [5]:
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, 
                           (3,3), 
                           activation = 'relu',
                           padding = 'SAME',
                           input_shape = (28, 28, 1)),
    tf.keras.layers.MaxPool2D((2,2)),
    tf.keras.layers.Conv2D(64, 
                           (3,3), 
                           activation = 'relu',
                           padding = 'SAME',
                           input_shape = (14, 14, 32)),
    tf.keras.layers.MaxPool2D((2,2)),
    tf.keras.layers.GlobalAveragePooling2D(),
    tf.keras.layers.Dense(2, activation = 'softmax', use_bias = False)
])
In [6]:
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 28, 28, 32)        320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 14, 14, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 64)          0         
_________________________________________________________________
global_average_pooling2d (Gl (None, 64)                0         
_________________________________________________________________
dense (Dense)                (None, 2)                 128       
=================================================================
Total params: 18,944
Trainable params: 18,944
Non-trainable params: 0
_________________________________________________________________



In [7]:
model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy', 
              metrics=['accuracy'])
In [8]:
model.fit(train_imgs, train_y, epochs = 10)
Epoch 1/10
382/382 [==============================] - 3s 9ms/step - loss: 0.4439 - accuracy: 0.8121
Epoch 2/10
382/382 [==============================] - 3s 8ms/step - loss: 0.2070 - accuracy: 0.9309
Epoch 3/10
382/382 [==============================] - 3s 9ms/step - loss: 0.1523 - accuracy: 0.9489
Epoch 4/10
382/382 [==============================] - 3s 8ms/step - loss: 0.1334 - accuracy: 0.9549
Epoch 5/10
382/382 [==============================] - 3s 8ms/step - loss: 0.1106 - accuracy: 0.9637
Epoch 6/10
382/382 [==============================] - 3s 8ms/step - loss: 0.1032 - accuracy: 0.9647
Epoch 7/10
382/382 [==============================] - 3s 8ms/step - loss: 0.0914 - accuracy: 0.9686
Epoch 8/10
382/382 [==============================] - 3s 8ms/step - loss: 0.0889 - accuracy: 0.9687
Epoch 9/10
382/382 [==============================] - 3s 8ms/step - loss: 0.0806 - accuracy: 0.9728
Epoch 10/10
382/382 [==============================] - 3s 8ms/step - loss: 0.0829 - accuracy: 0.9709
Out[8]:
<tensorflow.python.keras.callbacks.History at 0x7f88d3c2c2e8>
In [9]:
# accuracy test
test_loss, test_acc = model.evaluate(test_imgs,  test_y)

print('loss = {}, Accuracy = {} %'.format(round(test_loss,8), round(test_acc*100)))
64/64 [==============================] - 0s 4ms/step - loss: 0.0740 - accuracy: 0.9745
loss = 0.07399434, Accuracy = 97 %
In [10]:
# get max pooling layer and fully connected layer 
conv_layer = model.get_layer(index = 3)
fc_layer = np.asarray(model.get_weights())[4]

# Class activation map 
activation_map = tf.matmul(conv_layer.output, fc_layer)
CAM_model = tf.keras.Model(inputs = model.inputs, outputs = activation_map)
In [11]:
test_img = test_imgs[np.random.choice(test_imgs.shape[0], 1)]

pred = np.argmax(model.predict(test_img), axis = 1)

CAM = CAM_model.predict(test_img)
attention = CAM[:,:,:,pred]
attention = np.abs(np.reshape(attention,(7,7)))

large_test_x = cv2.resize(test_img.reshape(28,28), (28*5, 28*5))
large_attention = cv2.resize(attention, (28*5, 28*5), interpolation = cv2.INTER_CUBIC)

plt.figure(figsize = (10,10))
plt.subplot(2,2,1)
plt.imshow(large_test_x, 'gray')
plt.axis('off')
plt.subplot(2,2,2)
plt.imshow(large_attention, 'jet', alpha = 0.5)
plt.axis('off')
plt.subplot(2,2,4)
plt.imshow(large_test_x, 'gray')
plt.imshow(large_attention, 'jet', alpha = 0.5)
plt.axis('off')
plt.show()
In [12]:
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')