-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradcam_orig.py
195 lines (159 loc) · 6.74 KB
/
gradcam_orig.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import sys
import os
import numpy as np
import cv2
from matplotlib import pyplot as plt
from keras import backend as K
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
import tensorflow as tf
from tensorflow.python.framework import ops
# Define model here ---------------------------------------------------
def build_model():
"""Function returning keras model instance.
Model can be
- Trained here
- Loaded with load_model
- Loaded from keras.applications
"""
return VGG16(include_top=True, weights='imagenet')
H, W = 224, 224 # Input shape, defined by the model (model.input_shape)
# ---------------------------------------------------------------------
def load_image(path, preprocess=True):
"""Load and preprocess image."""
x = image.load_img(path, target_size=(H, W))
if preprocess:
x = image.img_to_array(x)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
return x
def deprocess_image(x):
"""Same normalization as in:
https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py
"""
# normalize tensor: center on 0., ensure std is 0.25
x = x.copy()
x -= x.mean()
x /= (x.std() + K.epsilon())
x *= 0.25
# clip to [0, 1]
x += 0.5
x = np.clip(x, 0, 1)
# convert to RGB array
x *= 255
if K.image_data_format() == 'channels_first':
x = x.transpose((1, 2, 0))
x = np.clip(x, 0, 255).astype('uint8')
return x
def normalize(x):
"""Utility function to normalize a tensor by its L2 norm"""
return (x + 1e-10) / (K.sqrt(K.mean(K.square(x))) + 1e-10)
def build_guided_model():
"""Function returning modified model.
Changes gradient function for all ReLu activations
according to Guided Backpropagation.
"""
if "GuidedBackProp" not in ops._gradient_registry._registry:
@ops.RegisterGradient("GuidedBackProp")
def _GuidedBackProp(op, grad):
dtype = op.inputs[0].dtype
return grad * tf.cast(grad > 0., dtype) * \
tf.cast(op.inputs[0] > 0., dtype)
g = tf.get_default_graph()
with g.gradient_override_map({'Relu': 'GuidedBackProp'}):
new_model = build_model()
return new_model
def guided_backprop(input_model, images, layer_name):
"""Guided Backpropagation method for visualizing input saliency."""
input_imgs = input_model.input
layer_output = input_model.get_layer(layer_name).output
grads = K.gradients(layer_output, input_imgs)[0]
backprop_fn = K.function([input_imgs, K.learning_phase()], [grads])
grads_val = backprop_fn([images, 0])[0]
return grads_val
def grad_cam(input_model, image, cls, layer_name):
"""GradCAM method for visualizing input saliency."""
y_c = input_model.output[0, cls]
conv_output = input_model.get_layer(layer_name).output
grads = K.gradients(y_c, conv_output)[0]
# Normalize if necessary
# grads = normalize(grads)
gradient_function = K.function([input_model.input], [conv_output, grads])
output, grads_val = gradient_function([image])
output, grads_val = output[0, :], grads_val[0, :, :, :]
weights = np.mean(grads_val, axis=(0, 1))
cam = np.dot(output, weights)
# Process CAM
cam = cv2.resize(cam, (W, H), cv2.INTER_LINEAR)
cam = np.maximum(cam, 0)
cam_max = cam.max()
if cam_max != 0:
cam = cam / cam_max
return cam
def grad_cam_batch(input_model, images, classes, layer_name):
"""GradCAM method for visualizing input saliency.
Same as grad_cam but processes multiple images in one run."""
loss = tf.gather_nd(input_model.output, np.dstack([range(images.shape[0]), classes])[0])
layer_output = input_model.get_layer(layer_name).output
grads = K.gradients(loss, layer_output)[0]
gradient_fn = K.function([input_model.input, K.learning_phase()], [layer_output, grads])
conv_output, grads_val = gradient_fn([images, 0])
weights = np.mean(grads_val, axis=(1, 2))
cams = np.einsum('ijkl,il->ijk', conv_output, weights)
# Process CAMs
new_cams = np.empty((images.shape[0], W, H))
for i in range(new_cams.shape[0]):
cam_i = cams[i] - cams[i].mean()
cam_i = (cam_i + 1e-10) / (np.linalg.norm(cam_i, 2) + 1e-10)
new_cams[i] = cv2.resize(cam_i, (H, W), cv2.INTER_LINEAR)
new_cams[i] = np.maximum(new_cams[i], 0)
new_cams[i] = new_cams[i] / new_cams[i].max()
return new_cams
def compute_saliency(model, guided_model, img_path, layer_name='block5_conv3', cls=-1, visualize=True, save=True):
"""Compute saliency using all three approaches.
-layer_name: layer to compute gradients;
-cls: class number to localize (-1 for most probable class).
"""
preprocessed_input = load_image(img_path)
predictions = model.predict(preprocessed_input)
top_n = 5
top = decode_predictions(predictions, top=top_n)[0]
classes = np.argsort(predictions[0])[-top_n:][::-1]
print('Model prediction:')
for c, p in zip(classes, top):
print('\t{:15s}\t({})\twith probability {:.3f}'.format(p[1], c, p[2]))
if cls == -1:
cls = np.argmax(predictions)
class_name = decode_predictions(np.eye(1, 1000, cls))[0][0][1]
print("Explanation for '{}'".format(class_name))
gradcam = grad_cam(model, preprocessed_input, cls, layer_name)
gb = guided_backprop(guided_model, preprocessed_input, layer_name)
guided_gradcam = gb * gradcam[..., np.newaxis]
if save:
jetcam = cv2.applyColorMap(np.uint8(255 * gradcam), cv2.COLORMAP_JET)
jetcam = (np.float32(jetcam) + load_image(img_path, preprocess=False)) / 2
cv2.imwrite('gradcam.jpg', np.uint8(jetcam))
cv2.imwrite('guided_backprop.jpg', deprocess_image(gb[0]))
cv2.imwrite('guided_gradcam.jpg', deprocess_image(guided_gradcam[0]))
if visualize:
plt.figure(figsize=(15, 10))
plt.subplot(131)
plt.title('GradCAM')
plt.axis('off')
plt.imshow(load_image(img_path, preprocess=False))
plt.imshow(gradcam, cmap='jet', alpha=0.5)
plt.subplot(132)
plt.title('Guided Backprop')
plt.axis('off')
plt.imshow(np.flip(deprocess_image(gb[0]), -1))
plt.subplot(133)
plt.title('Guided GradCAM')
plt.axis('off')
plt.imshow(np.flip(deprocess_image(guided_gradcam[0]), -1))
plt.show()
return gradcam, gb, guided_gradcam
if __name__ == '__main__':
model = build_model()
guided_model = build_guided_model()
gradcam, gb, guided_gradcam = compute_saliency(model, guided_model, layer_name='block5_conv3',
img_path=sys.argv[1], cls=-1, visualize=True, save=True)