-
Notifications
You must be signed in to change notification settings - Fork 0
/
C1M1util.py
110 lines (90 loc) · 3.53 KB
/
C1M1util.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
import random
import cv2
import matplotlib.pyplot as plt
import numpy as np
from keras import backend as K
from keras.preprocessing import image
from sklearn.metrics import roc_auc_score, roc_curve
from tensorflow.compat.v1.logging import INFO, set_verbosity
random.seed(a=None, version=2)
set_verbosity(INFO)
def get_mean_std_per_batch(image_path, df, H=320, W=320):
sample_data = []
for idx, img in enumerate(df.sample(100)["Image"].values):
# path = image_dir + img
sample_data.append(
np.array(image.load_img(image_path, target_size=(H, W))))
mean = np.mean(sample_data[0])
std = np.std(sample_data[0])
return mean, std
def load_image(img, image_dir, df, preprocess=True, H=320, W=320):
"""Load and preprocess image."""
img_path = image_dir + img
mean, std = get_mean_std_per_batch(img_path, df, H=H, W=W)
x = image.load_img(img_path, target_size=(H, W))
if preprocess:
x -= mean
x /= std
x = np.expand_dims(x, axis=0)
return x
def grad_cam(input_model, image, cls, layer_name, H=320, W=320):
"""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]
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 = cam / cam.max()
return cam
def compute_gradcam(model, img, image_dir, df, labels, selected_labels,
layer_name='bn'):
preprocessed_input = load_image(img, image_dir, df)
predictions = model.predict(preprocessed_input)
print("Loading original image")
plt.figure(figsize=(15, 10))
plt.subplot(151)
plt.title("Original")
plt.axis('off')
plt.imshow(load_image(img, image_dir, df, preprocess=False), cmap='gray')
j = 1
for i in range(len(labels)):
if labels[i] in selected_labels:
print(f"Generating gradcam for class {labels[i]}")
gradcam = grad_cam(model, preprocessed_input, i, layer_name)
plt.subplot(151 + j)
plt.title(f"{labels[i]}: p={predictions[0][i]:.3f}")
plt.axis('off')
plt.imshow(load_image(img, image_dir, df, preprocess=False),
cmap='gray')
plt.imshow(gradcam, cmap='jet', alpha=min(0.5, predictions[0][i]))
j += 1
def get_roc_curve(labels, predicted_vals, generator):
auc_roc_vals = []
for i in range(len(labels)):
try:
gt = generator.labels[:, i]
pred = predicted_vals[:, i]
auc_roc = roc_auc_score(gt, pred)
auc_roc_vals.append(auc_roc)
fpr_rf, tpr_rf, _ = roc_curve(gt, pred)
plt.figure(1, figsize=(10, 10))
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr_rf, tpr_rf,
label=labels[i] + " (" + str(round(auc_roc, 3)) + ")")
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
except:
print(
f"Error in generating ROC curve for {labels[i]}. "
f"Dataset lacks enough examples."
)
plt.show()
return auc_roc_vals