-
Notifications
You must be signed in to change notification settings - Fork 5
/
visualize.py
231 lines (170 loc) · 6.47 KB
/
visualize.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python
"""visualize.py: Implementation of Grad-CAM and Saliency Map."""
__author__ = "David Bertoldi"
__email__ = "[email protected]"
import argparse
import utils
import models
import numpy as np
import tensorflow as tf
from tensorflow import keras
from PIL import Image
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import cv2
from tensorflow.keras.models import Model
from processing import Processing
from mlxtend.plotting import plot_confusion_matrix
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
def parse_arguments():
parser = argparse.ArgumentParser(description='Flower Recognition Visualization')
parser.add_argument('--config', type=str, const='config.yml', default='config.yml', nargs='?', help='Configuration file')
return parser.parse_args()
class GradCAM:
def __init__(self, model, classIdx, layerName=None):
self.model = model
self.classIdx = classIdx
self.layerName = layerName
if self.layerName is None:
self.layerName = self.find_target_layer()
def find_target_layer(self):
for layer in reversed(self.model.layers):
if len(layer.output_shape) == 4:
return layer.name
raise ValueError("Could not find 4D layer. Cannot apply GradCAM.")
def compute_heatmap(self, image, eps=1e-8):
gradModel = Model(
inputs=[self.model.inputs],
outputs=[self.model.get_layer(self.layerName).output, self.model.output])
with tf.GradientTape() as tape:
inputs = tf.cast(image, tf.float32)
(convOutputs, predictions) = gradModel(inputs)
loss = predictions[:, tf.argmax(predictions[0])]
# Get the gradients of the loss w.r.t to the input image
grads = tape.gradient(loss, convOutputs)
castConvOutputs = tf.cast(convOutputs > 0, "float32")
castGrads = tf.cast(grads > 0, "float32")
guidedGrads = castConvOutputs * castGrads * grads
convOutputs = convOutputs[0]
guidedGrads = guidedGrads[0]
# take average across channels
weights = tf.reduce_mean(guidedGrads, axis=(0, 1))
cam = tf.reduce_sum(tf.multiply(weights, convOutputs), axis=-1)
(w, h) = (image.shape[2], image.shape[1])
heatmap = cv2.resize(cam.numpy(), (w, h))
numer = heatmap - np.min(heatmap)
denom = (heatmap.max() - heatmap.min()) + eps
heatmap = numer / denom
heatmap = (heatmap * 255).astype("uint8")
return heatmap
def overlay_heatmap(self, heatmap, image, alpha):
jet_heatmap = np.uint8(cm.jet(heatmap)[..., :3] * 255)
image = np.uint8(image)
output = cv2.addWeighted(image, alpha, jet_heatmap, 1 - alpha, 0)
return (jet_heatmap, output)
class SaliencyMap:
def get_saliency_map(self, model, image, class_idx):
with tf.GradientTape() as tape:
inputs = tf.Variable(image, dtype=float)
tape.watch(inputs)
predictions = model(inputs)
loss = predictions[:, class_idx]
# Get the gradients of the loss w.r.t to the input image.
gradient = tape.gradient(loss, inputs)
gradient = tf.nn.relu(gradient)
# take maximum across channels
gradient = tf.reduce_max(gradient, axis=-1)
# convert to numpy
gradient = gradient.numpy() **0.9
# normaliz between 0 and 1
min_val, max_val = np.min(gradient), np.max(gradient)
smap = (gradient - min_val) / (max_val - min_val + keras.backend.epsilon())
jet_saliency = np.uint8(cm.jet(smap[0])[..., :3] * 255)
return jet_saliency
def plot_cm(cm, zero_diagonal=False, labels=None, cmap=plt.cm.viridis):
"""Plot a confusion matrix."""
n = len(cm)
if zero_diagonal:
for i in range(n):
cm[i][i] = 0
size = int(n / 4.)
fig = plt.figure(figsize=(size, size), dpi=80, )
plt.clf()
ax = fig.add_subplot(111)
ax.set_aspect(1)
if labels is None:
labels = [i+1 for i in range(len(cm))]
x = [i for i in range(len(cm))]
plt.xticks(x, labels, rotation='vertical')
y = [i for i in range(len(cm))]
plt.yticks(y, labels) # , rotation='vertical'
res = ax.imshow(np.array(cm), cmap=cmap, interpolation='nearest')
width, height = cm.shape
plt.colorbar(res)
plt.show()
if __name__ == '__main__':
args = parse_arguments()
config = utils.read_configuration(args.config)
preprocess_input = keras.applications.xception.preprocess_input
decode_predictions = keras.applications.xception.decode_predictions
# Choose model
class_model = models.Efficientnetb4(0.5)
model = class_model.get_model()
model.load_weights('output/checkpoints/{}-loss.h5'.format(type(class_model).__name__.lower()))
preprocessor = class_model.preprocess()
target_size = class_model.size
# Load image
p = Processing(target_size=target_size,
batch_size=1,
config=config,
preprocessor=preprocessor)
test_preprocessed = p.from_folder('./image')
image, label = test_preprocessed.__getitem__(0)
img = image[0]
# Print top 4 predictions
top = 4
predictions = model.predict(image)[0]
indexes = np.argpartition(predictions, -top)[-top:]
indexes = indexes[np.argsort(predictions[indexes])]
for i in indexes:
print('{}({}): {}'.format(utils.LABELS[i], i, predictions[i]))
# Grad-CAM computation
icam = GradCAM(model, np.argmax(predictions), None)
heatmap = icam.compute_heatmap(image)
heatmap = cv2.resize(heatmap, (target_size, target_size))
# Saliency Map computation
smap = SaliencyMap().get_saliency_map(model, tf.expand_dims(img, axis=0), np.argmax(predictions))
(heatmap, output) = icam.overlay_heatmap(heatmap, img, 0.4)
# Plot of Grad-CAM
fig, ax = plt.subplots(1, 3)
fig.set_size_inches(20,20)
ax[0].imshow(heatmap)
ax[1].imshow(np.uint8(img))
ax[2].imshow(output)
plt.show()
# Plot of Saliency Map
fig, axes = plt.subplots(1,2,figsize=(14,5))
axes[0].imshow(np.uint8(img))
i = axes[1].imshow(smap, alpha=1.0, cmap='jet')
fig.colorbar(i)
plt.show()
# Confusion Matrix
p = Processing(target_size=target_size,
batch_size=16,
config=config,
preprocessor=preprocessor)
_, _, test_preprocessed = p.get_dataset()
# confusion matrices
viridis = matplotlib.cm.get_cmap('viridis', 330)
newcolors = viridis(np.linspace(0, 1, 330))
white = np.array([1, 1, 1, 1])
newcolors[:5, :] = white
newcmp = ListedColormap(newcolors)
Y_test_pred = model.predict(test_preprocessed)
y_test_pred = Y_test_pred.argmax(1)
cm = confusion_matrix(test_preprocessed.classes, y_test_pred)
plot_cm(cm, cmap=newcmp)
print(classification_report(y_test_pred, test_preprocessed.classes))