[db7631]: / Interpretability / IG-with-TF2-cv2_basic.py

Download this file

411 lines (292 with data), 11.3 kB

  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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python
# coding: utf-8
# ## Load the model and weights
# In[1]:
### Import packages
import os
from tensorflow.keras.models import model_from_json
from tensorflow.keras.preprocessing import image
from tensorflow.keras import backend as K
import tensorflow as tf
import numpy as np
import pickle
import cv2
print("TF version: ", tf.__version__)
print("cv2 version: ", cv2.__version__)
### File Constants
# Architecture
arch = "Resnet" # NAS | Resnet
# Mode
mode = "Axial" # Sag | Axial
# some images are in .JPG some in .jpg
IMG_EXT = "JPG" # or "jpg" or "png" or "JPG"!
# classifier dir
# CLASSIFIER_ROOT_DIR = "/hdd2/kaiyuan/SpineAI_classifier_postRSNA"
CLASSIFIER_ROOT_DIR = "./Resnet_Best_Classifiers_Jun2020/"
# 9 weights for avg and std
VERSION = "v_3_C"
if mode == "Axial":
# weights
best_center_weight = "Axial_center_resnetscale150V3_150x150bat128_6LDropout_Date0618-1158_Ep22_ValAcc0.856_ValLoss10.78.h5"
best_center_path = "Axial_Center_BestWeights_NewTop3_Jun2020/"
CENTER_MODEL_WEIGHT = os.path.join(
CLASSIFIER_ROOT_DIR,
best_center_path,
VERSION,
best_center_weight
)
print(os.path.exists(CENTER_MODEL_WEIGHT))
print(CENTER_MODEL_WEIGHT, "exists")
print(os.path.getsize(CENTER_MODEL_WEIGHT), "byte")
# load model
TRAINED_JSON = os.path.join(
CLASSIFIER_ROOT_DIR,
best_center_path,
"6conv-model.json"
)
# Instantiate a model from JSON
json_file = open(TRAINED_JSON, 'r')
model_json = json_file.read()
json_file.close()
center_model = model_from_json(model_json)
center_model.load_weights(CENTER_MODEL_WEIGHT)
print("Loaded center_model from disk")
# ## Setup and dependencies
# In[2]:
import matplotlib.pylab as plt
import numpy as np
import tensorflow as tf
# import tensorflow_hub as hub
import numpy as np
import tensorflow as tf
from tensorflow import keras
# Display
from IPython.display import Image
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# model summary check
model = center_model
model.summary()
# ## Load image with keras and tf
# In[3]:
img_path = "./1.3.6.1.4.1.5962.99.1.2380920017.862678823.1535684277457.21756.0.jpg"
# dimensions of our images.
img_width, img_height = 150, 150
img_size = (img_width, img_height)
# labels
grading = np.array(['normal', 'mild', 'moderate', 'severe'])
img_url = {
'TestImg': img_path,
}
"""
# original tf2 read_image()
def read_image(file_name):
image = tf.io.read_file(file_name)
image = tf.image.decode_jpeg(image, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize_with_pad(image, target_height=224, target_width=224)
return image
"""
# new read_image with cv2 resize and interpolation
def read_image(file_name):
"""our pipeline"""
img_orig = keras.preprocessing.image.load_img(file_name)
print("-keras load_img: ", img_orig)
img = keras.preprocessing.image.img_to_array(img_orig)
print("-keras img_to_array: ", img.shape, img.dtype, img[0][0])
# change from INTER_CUBIC to INTER_LINEAR
img = cv2.resize(img, img_size, interpolation=cv2.INTER_LINEAR)
print("-cv2 resize: ", img.shape, img.dtype, img[0][0])
x = 1/255.0 * img
print("-normalize: ", x.shape, x.dtype, x[0][0])
image = tf.image.convert_image_dtype(x, tf.float32)
print("-convert_image_dtype: ", image.shape, image.dtype, image[0][0])
return image
img_paths = {name: url for (name, url) in img_url.items()}
img_name_tensors = {name: read_image(img_path) for (name, img_path) in img_paths.items()}
# In[4]:
plt.figure(figsize=(8, 8))
for n, (name, img_tensors) in enumerate(img_name_tensors.items()):
ax = plt.subplot(1, 2, n+1)
ax.imshow(img_tensors)
ax.set_title(name)
ax.axis('off')
plt.tight_layout()
# ## Classify Images
# In[6]:
def top_k_predictions(img, k=3):
# print(img)
# x = np.expand_dims(img, axis=0)
# image_batch = np.vstack([x])
image_batch = tf.expand_dims(img, 0)
print("image_batch shape: ", image_batch.shape)
predictions = model(image_batch)
print("predictions: ", predictions)
probs = predictions
print("probs: ", probs)
top_probs, top_idxs = tf.math.top_k(input=probs, k=k)
# not using imagenet_labels
top_labels = grading[tuple(top_idxs)]
print("tuple(top_idxs): ", tuple(top_idxs))
print("top_labels: ", top_labels)
return top_labels, top_probs[0]
for (name, img_tensor) in img_name_tensors.items():
plt.imshow(img_tensor)
plt.title(name, fontweight='bold')
plt.axis('off')
plt.show()
pred_label, pred_prob = top_k_predictions(img_tensor)
for label, prob in zip(pred_label, pred_prob):
print(f'{label}: {prob:0.1%}')
# ## Using IG
# ### Baseline
# In[7]:
baseline = tf.zeros(shape=(150,150,3))
plt.imshow(baseline)
plt.title("Baseline")
plt.axis('off')
plt.show()
m_steps=50
alphas = tf.linspace(start=0.0, stop=1.0, num=m_steps+1) # Generate m_steps intervals for integral_approximation() below.
def interpolate_images(baseline,
image,
alphas):
alphas_x = alphas[:, tf.newaxis, tf.newaxis, tf.newaxis]
baseline_x = tf.expand_dims(baseline, axis=0)
input_x = tf.expand_dims(image, axis=0)
delta = input_x - baseline_x
images = baseline_x + alphas_x * delta
return images
interpolated_images = interpolate_images(
baseline=baseline,
image=img_name_tensors['TestImg'],
alphas=alphas)
fig = plt.figure(figsize=(20, 20))
i = 0
for alpha, image in zip(alphas[0::10], interpolated_images[0::10]):
i += 1
plt.subplot(1, len(alphas[0::10]), i)
plt.title(f'alpha: {alpha:.1f}')
plt.imshow(image)
plt.axis('off')
plt.tight_layout();
# ### Compute Gradients
# In[8]:
target_class_idx = 2 # 2 is moderate for the TestImg
# In[9]:
def compute_gradients(images, target_class_idx):
with tf.GradientTape() as tape:
tape.watch(images)
logits = model(images)
# probs = tf.nn.softmax(logits, axis=-1)[:, target_class_idx]
return tape.gradient(logits, images)
path_gradients = compute_gradients(
images=interpolated_images,
target_class_idx=target_class_idx)
# m_steps = 50, so the path_gradients.shape should be (50+1,..)
print("path_gradients.shape", path_gradients.shape)
# Visualize the gradient saturation
pred = model(interpolated_images)
pred_proba = pred[:, target_class_idx]
plt.figure(figsize=(10, 4))
ax1 = plt.subplot(1, 2, 1)
ax1.plot(alphas, pred_proba)
ax1.set_title('Target class predicted probability over alpha')
ax1.set_ylabel('model p(target class)')
ax1.set_xlabel('alpha')
ax1.set_ylim([0, 1])
ax2 = plt.subplot(1, 2, 2)
# Average across interpolation steps
average_grads = tf.reduce_mean(path_gradients, axis=[1, 2, 3])
# Normalize gradients to 0 to 1 scale. E.g. (x - min(x))/(max(x)-min(x))
average_grads_norm = (average_grads-tf.math.reduce_min(average_grads))/(tf.math.reduce_max(average_grads)-tf.reduce_min(average_grads))
ax2.plot(alphas, average_grads_norm)
ax2.set_title('Average pixel gradients (normalized) over alpha')
ax2.set_ylabel('Average pixel gradients')
ax2.set_xlabel('alpha')
ax2.set_ylim([0, 1]);
# ### Accumulate gradients (integral approximation)
# In[10]:
def integral_approximation(gradients):
# riemann_trapezoidal
grads = (gradients[:-1] + gradients[1:]) / tf.constant(2.0)
integrated_gradients = tf.math.reduce_mean(grads, axis=0)
return integrated_gradients
ig = integral_approximation(
gradients=path_gradients)
print("shape of IG: ", ig.shape)
@tf.function
def integrated_gradients(baseline,
image,
target_class_idx,
m_steps=50,
batch_size=32):
# 1. Generate alphas.
alphas = tf.linspace(start=0.0, stop=1.0, num=m_steps+1)
# Initialize TensorArray outside loop to collect gradients.
gradient_batches = tf.TensorArray(tf.float32, size=m_steps+1)
# Iterate alphas range and batch computation for speed, memory efficiency, and scaling to larger m_steps.
for alpha in tf.range(0, len(alphas), batch_size):
from_ = alpha
to = tf.minimum(from_ + batch_size, len(alphas))
alpha_batch = alphas[from_:to]
# 2. Generate interpolated inputs between baseline and input.
interpolated_path_input_batch = interpolate_images(baseline=baseline,
image=image,
alphas=alpha_batch)
# 3. Compute gradients between model outputs and interpolated inputs.
gradient_batch = compute_gradients(images=interpolated_path_input_batch,
target_class_idx=target_class_idx)
# Write batch indices and gradients to extend TensorArray.
gradient_batches = gradient_batches.scatter(tf.range(from_, to), gradient_batch)
# Stack path gradients together row-wise into single tensor.
total_gradients = gradient_batches.stack()
# 4. Integral approximation through averaging gradients.
avg_gradients = integral_approximation(gradients=total_gradients)
# 5. Scale integrated gradients with respect to input.
integrated_gradients = (image - baseline) * avg_gradients
return integrated_gradients
ig_attributions = integrated_gradients(baseline=baseline,
image=img_name_tensors['TestImg'],
target_class_idx=target_class_idx,
m_steps=240)
print("IG feature attribution shape: ", ig_attributions.shape)
# ## Visualize Attributions
# In[11]:
def plot_img_attributions(baseline,
image,
target_class_idx,
m_steps=50,
cmap=None,
overlay_alpha=0.4):
attributions = integrated_gradients(baseline=baseline,
image=image,
target_class_idx=target_class_idx,
m_steps=m_steps)
# Sum of the attributions across color channels for visualization.
# The attribution mask shape is a grayscale image with height and width
# equal to the original image.
attribution_mask = tf.reduce_sum(tf.math.abs(attributions), axis=-1)
fig, axs = plt.subplots(nrows=2, ncols=2, squeeze=False, figsize=(8, 8))
axs[0, 0].set_title('Baseline image')
axs[0, 0].imshow(baseline)
axs[0, 0].axis('off')
axs[0, 1].set_title('Original image')
axs[0, 1].imshow(image)
axs[0, 1].axis('off')
axs[1, 0].set_title('Attribution mask')
axs[1, 0].imshow(attribution_mask, cmap=cmap)
axs[1, 0].axis('off')
axs[1, 1].set_title('Overlay')
axs[1, 1].imshow(attribution_mask, cmap=cmap)
axs[1, 1].imshow(image, alpha=overlay_alpha)
axs[1, 1].axis('off')
plt.tight_layout()
plt.savefig("IG-TF2-sample.jpg")
return fig
_ = plot_img_attributions(image=img_name_tensors['TestImg'],
baseline=baseline,
target_class_idx=target_class_idx,
m_steps=240,
cmap=plt.cm.inferno,
overlay_alpha=0.4)