Visualize the heatmap - GradCAM - Keras

Steps:

1) Compute the model output and last convolutional layer output for the image.

2) Find probability of the winning class.

3) Compute the gradient of the winning class with resepct to the last convolutional layer.

3) Weighted the gradient with the last convolutional layer. And normalize it for visualization

import tensorflow as tf
import tensorflow.keras.backend as K
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.inception_v3 import preprocess_input, decode_predictions
import numpy as np
import os
import matplotlib.pyplot as plt
import cv2

model = InceptionV3(weights='imagenet')
model.summary()
ORIGINAL = 'cat.png'
DIM = 299
img = image.load_img(ORIGINAL, target_size=(DIM, DIM))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
last_conv_layer = model.get_layer('conv2d_93')
grads = tf.gradients([model.output[:, tf.math.argmax(model.output[0])]], [last_conv_layer.output])[0]
pooled_grads = K.mean(grads, axis=(0, 1, 2))
f = K.function([model.inputs], [model.output, last_conv_layer.output, pooled_grads])
model_out, last_conv_layer, pooled_grads = f(x)
class_out = model_out[:, np.argmax(model_out[0])]
heatmap = np.mean(np.multiply(pooled_grads, last_conv_layer), axis=-1)
heatmap = np.maximum(heatmap, 0)
heatmap /= np.max(heatmap)
heatmap = heatmap[0]
img = cv2.imread(ORIGINAL)
INTENSITY = 0.5
heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
heatmap = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET)
img = heatmap * INTENSITY + img
cv2.imwrite('output.jpg', img)
Figure: Input and Output

Post a Comment

0 Comments