Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keras-team
GitHub Repository: keras-team/keras-io
Path: blob/master/examples/vision/grad_cam.py
8146 views
1
"""
2
Title: Grad-CAM class activation visualization
3
Author: [fchollet](https://twitter.com/fchollet)
4
Date created: 2020/04/26
5
Last modified: 2021/03/07
6
Description: How to obtain a class activation heatmap for an image classification model.
7
Accelerator: GPU
8
"""
9
10
"""
11
Adapted from Deep Learning with Python (2017).
12
13
## Setup
14
"""
15
16
import os
17
18
os.environ["KERAS_BACKEND"] = "tensorflow"
19
20
import numpy as np
21
import tensorflow as tf
22
import keras
23
24
# Display
25
from IPython.display import Image, display
26
import matplotlib as mpl
27
import matplotlib.pyplot as plt
28
29
"""
30
## Configurable parameters
31
32
You can change these to another model.
33
34
To get the values for `last_conv_layer_name` use `model.summary()`
35
to see the names of all layers in the model.
36
"""
37
38
model_builder = keras.applications.xception.Xception
39
img_size = (299, 299)
40
preprocess_input = keras.applications.xception.preprocess_input
41
decode_predictions = keras.applications.xception.decode_predictions
42
43
last_conv_layer_name = "block14_sepconv2_act"
44
45
# The local path to our target image
46
img_path = keras.utils.get_file(
47
"african_elephant.jpg", "https://i.imgur.com/Bvro0YD.png"
48
)
49
50
display(Image(img_path))
51
52
53
"""
54
## The Grad-CAM algorithm
55
"""
56
57
58
def get_img_array(img_path, size):
59
# `img` is a PIL image of size 299x299
60
img = keras.utils.load_img(img_path, target_size=size)
61
# `array` is a float32 Numpy array of shape (299, 299, 3)
62
array = keras.utils.img_to_array(img)
63
# We add a dimension to transform our array into a "batch"
64
# of size (1, 299, 299, 3)
65
array = np.expand_dims(array, axis=0)
66
return array
67
68
69
def make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None):
70
# First, we create a model that maps the input image to the activations
71
# of the last conv layer as well as the output predictions
72
grad_model = keras.models.Model(
73
model.inputs, [model.get_layer(last_conv_layer_name).output, model.output]
74
)
75
76
# Then, we compute the gradient of the top predicted class for our input image
77
# with respect to the activations of the last conv layer
78
with tf.GradientTape() as tape:
79
last_conv_layer_output, preds = grad_model(img_array)
80
if pred_index is None:
81
pred_index = tf.argmax(preds[0])
82
class_channel = preds[:, pred_index]
83
84
# This is the gradient of the output neuron (top predicted or chosen)
85
# with regard to the output feature map of the last conv layer
86
grads = tape.gradient(class_channel, last_conv_layer_output)
87
88
# This is a vector where each entry is the mean intensity of the gradient
89
# over a specific feature map channel
90
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
91
92
# We multiply each channel in the feature map array
93
# by "how important this channel is" with regard to the top predicted class
94
# then sum all the channels to obtain the heatmap class activation
95
last_conv_layer_output = last_conv_layer_output[0]
96
heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis]
97
heatmap = tf.squeeze(heatmap)
98
99
# For visualization purpose, we will also normalize the heatmap between 0 & 1
100
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
101
return heatmap.numpy()
102
103
104
"""
105
## Let's test-drive it
106
"""
107
108
# Prepare image
109
img_array = preprocess_input(get_img_array(img_path, size=img_size))
110
111
# Make model
112
model = model_builder(weights="imagenet")
113
114
# Remove last layer's softmax
115
model.layers[-1].activation = None
116
117
# Print what the top predicted class is
118
preds = model.predict(img_array)
119
print("Predicted:", decode_predictions(preds, top=1)[0])
120
121
# Generate class activation heatmap
122
heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name)
123
124
# Display heatmap
125
plt.matshow(heatmap)
126
plt.show()
127
128
129
"""
130
## Create a superimposed visualization
131
"""
132
133
134
def save_and_display_gradcam(img_path, heatmap, cam_path="cam.jpg", alpha=0.4):
135
# Load the original image
136
img = keras.utils.load_img(img_path)
137
img = keras.utils.img_to_array(img)
138
139
# Rescale heatmap to a range 0-255
140
heatmap = np.uint8(255 * heatmap)
141
142
# Use jet colormap to colorize heatmap
143
jet = mpl.colormaps["jet"]
144
145
# Use RGB values of the colormap
146
jet_colors = jet(np.arange(256))[:, :3]
147
jet_heatmap = jet_colors[heatmap]
148
149
# Create an image with RGB colorized heatmap
150
jet_heatmap = keras.utils.array_to_img(jet_heatmap)
151
jet_heatmap = jet_heatmap.resize((img.shape[1], img.shape[0]))
152
jet_heatmap = keras.utils.img_to_array(jet_heatmap)
153
154
# Superimpose the heatmap on original image
155
superimposed_img = jet_heatmap * alpha + img
156
superimposed_img = keras.utils.array_to_img(superimposed_img)
157
158
# Save the superimposed image
159
superimposed_img.save(cam_path)
160
161
# Display Grad CAM
162
display(Image(cam_path))
163
164
165
save_and_display_gradcam(img_path, heatmap)
166
167
"""
168
## Let's try another image
169
170
We will see how the grad cam explains the model's outputs for a multi-label image. Let's
171
try an image with a cat and a dog together, and see how the grad cam behaves.
172
"""
173
174
img_path = keras.utils.get_file(
175
"cat_and_dog.jpg",
176
"https://storage.googleapis.com/petbacker/images/blog/2017/dog-and-cat-cover.jpg",
177
)
178
179
display(Image(img_path))
180
181
# Prepare image
182
img_array = preprocess_input(get_img_array(img_path, size=img_size))
183
184
# Print what the two top predicted classes are
185
preds = model.predict(img_array)
186
print("Predicted:", decode_predictions(preds, top=2)[0])
187
188
"""
189
We generate class activation heatmap for "chow," the class index is 260
190
"""
191
192
heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=260)
193
194
save_and_display_gradcam(img_path, heatmap)
195
196
"""
197
We generate class activation heatmap for "egyptian cat," the class index is 285
198
"""
199
200
heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=285)
201
202
save_and_display_gradcam(img_path, heatmap)
203
204
"""
205
## Relevant Chapters from Deep Learning with Python
206
- [Chapter 10: Interpreting what ConvNets learn](https://deeplearningwithpython.io/chapters/chapter10_interpreting-what-convnets-learn)
207
"""
208
209