-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
160 lines (135 loc) · 5.57 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
import random
import numpy as np
import pandas as pd
np.set_printoptions(edgeitems=25, linewidth=100000)
pd.options.display.float_format = '{:,.2f}'.format
pd.options.display.max_rows = 10000000
# ML
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.models import load_model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.regularizers import l2
from tensorflow.keras.optimizers.schedules import ExponentialDecay
from tensorflow.keras.callbacks import (Callback, ModelCheckpoint, EarlyStopping,
ReduceLROnPlateau, LambdaCallback, LearningRateScheduler)
from tensorflow.keras.layers import (GlobalAveragePooling2D, Activation, ZeroPadding2D, ReLU,
AveragePooling2D, Add, Conv2D, MaxPool2D, BatchNormalization,
Input, Flatten, Dense, Dropout)
from tensorflow.keras.initializers import VarianceScaling, RandomUniform, HeNormal
# tensorflow add-ons
import tensorflow_addons as tfa
# tensorflow probability
import tensorflow_probability as tfp
tfd = tfp.distributions
# sklearn metrics and plotting
from sklearn.metrics import (roc_curve, roc_auc_score, auc, log_loss)
## Plotting
import seaborn as sns
import matplotlib.pyplot as plt
# Fixing global random seed for reproducibility
np.random.seed(0)
random.seed(0)
tf.random.set_seed(0)
def analyse_model_prediction(data, true_labels,
model, image_num,
run_ensemble=False,
NUM_CLASSES=NUM_CLASSES):
if run_ensemble:
ensemble_size = 200
else:
ensemble_size = 1
image = data[image_num]
true_label = true_labels[image_num]
predicted_probabilities = np.empty(shape=(ensemble_size, NUM_CLASSES))
for i in range(ensemble_size):
predicted_probabilities[i] = model(image[np.newaxis, :]).numpy().mean()[0]
model_prediction = model(image[np.newaxis, :])
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 2),
gridspec_kw={'width_ratios': [2, 4]})
# Show the image and the true label
ax1.imshow(image[...][0])
ax1.axis('off')
if true_label[0] == 1:
ax1.set_title('True label: Spiral')
elif true_label[1] == 1:
ax1.set_title('True label: Elliptical')
elif true_label[2] == 1:
ax1.set_title('True label: Merger')
# Show a 95% prediction interval of model predicted probabilities
pct_2p5 = np.array([np.percentile(predicted_probabilities[:, i], 2.5) for i in range(NUM_CLASSES)])
pct_97p5 = np.array([np.percentile(predicted_probabilities[:, i], 97.5) for i in range(NUM_CLASSES)])
bar = ax2.bar(np.arange(NUM_CLASSES), color='red')
if true_label[0] == 1:
bar[0].set_color('green')
elif true_label[1] == 1:
bar[1].set_color('green')
elif true_label[2] == 1:
bar[2].set_color('green')
ax2.bar(np.arange(NUM_CLASSES), color='white', linewidth=1, edgecolor='white')
ax2.set_xticks(np.arange(NUM_CLASSES))
ax2.set_ylim([0, 1])
ax2.set_ylabel('Probability')
ax2.set_title('Example Model Estimated Probabilities')
plt.savefig(f'{str(model)}_estimated_probabilities_examples.png', dpi=300)
plt.show()
def plot_acc_loss(history, model, model_name=None):
"""Plot model accuracy and loss training history."""
if model_name is None:
model_name = model.name
else:
model_name = model_name
acc = history['accuracy']
val_acc = history['val_accuracy']
loss = history['loss']
val_loss = history['val_loss']
epochs = list(range(len(loss)))
# Plot accuracy
plt.figure(figsize=(6, 4))
plt.plot(epochs, acc, 'navy', label='Accuracy')
plt.plot(epochs, val_acc, 'deepskyblue', label="Validation Accuracy")
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.title(f"{model_name} Accuracy Training History")
plt.legend(loc='best')
plt.tight_layout()
plt.savefig(f"Plots/{model_name} Accuracy Training History", dpi=300)
plt.show()
# Plot loss
plt.figure(figsize=(6, 4))
plt.plot(epochs, loss, 'red', label='Loss')
plt.plot(epochs, val_loss, 'lightsalmon', label="Validation Loss")
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title(f"{model_name} Loss Training History")
plt.legend(loc='best')
plt.tight_layout()
plt.savefig(f"Plots/{model_name} Loss Training History", dpi=300)
plt.show()
def plot_roc(model, y_pred, Y_test, NUM_CLASSES, model_name=None):
"""Plot ROC curves & AUC for a categorical classifier."""
if model_name is None:
model_name = model.name
else:
model_name = model_name
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(NUM_CLASSES):
fpr[i], tpr[i], thresholds = roc_curve(Y_test[:, i], y_pred[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
plt.figure(figsize=(6, 5), tight_layout=True)
colors = ['blue', 'red', 'green', 'brown', 'purple', 'pink', 'orange', 'black', 'yellow', 'cyan']
for i, color, lbl in zip(range(NUM_CLASSES), colors, class_names):
plt.plot(fpr[i], tpr[i], color = color, lw = 1.5,
label='ROC Curve of class {0} (area = {1:0.3f})'.format(lbl, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=2)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate (1 - Specificity)')
plt.ylabel('True Positive Rate (Selectivity)')
plt.legend(loc="lower right")
plt.title(f'{model_name} Test ROC Curves')
plt.tight_layout()
plt.savefig(f'Plots/{model_name} ROC', dpi=300)
plt.show()