-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
274 lines (240 loc) · 11.6 KB
/
utils.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
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
import astroNN
import os
import cv2
import h5py
from livelossplot import PlotLossesKeras
from astroNN.datasets import load_galaxy10sdss
from astroNN.datasets.galaxy10sdss import galaxy10cls_lookup
import tensorflow_probability as tfp
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
from tensorflow.keras.losses import SparseCategoricalCrossentropy
from tensorflow.keras.optimizers import RMSprop, Adam
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from sklearn import metrics
from math import ceil, floor
import seaborn as sns
from tqdm import tqdm
import numpy as np
import pandas as pd
import random
import json
tfd = tfp.distributions
tfpl = tfp.layers
total_samples = 21768
train_samples = floor(total_samples*0.7)
val_samples = ceil(total_samples*0.15)
test_samples = total_samples - train_samples - val_samples
batch_size = 64
epochs = 50
weights_dir = {
'frequentist' : './checkpoints/freq_logs/best' ,
'bayesian' : './checkpoints/baye_logs/best'
}
ensemble_size = 100
def remove_specific_class(images = None, labels = None, class_label = None):
"""
Removes specific class from the dataset and returns the removed samples as well.
"""
if images is None or labels is None or class_label is None:
raise ValueError("Images, labels and class label must be provided.")
else:
# Get the indices of the class
indices = np.where(labels == class_label)
# Return the removed samples as well
images_removed = np.take(images, indices, axis = 0)
labels_removed = np.take(labels, indices, axis = 0)
# Remove the class from the dataset
images = np.delete(images, indices, axis = 0)
labels = np.delete(labels, indices, axis = 0)
return images, labels, images_removed, labels_removed
def convert_to_categorical(labels = None):
"""
Converts the labels to categorical.
"""
if labels is None:
raise ValueError("Labels and number of classes must be provided.")
else:
one_hot_labels = []
for label in labels:
one_hot_vector = [0,0,0,0,0,0,0,0,0]
if label>5:
label = label - 1
one_hot_vector[int(label)] = 1
one_hot_labels.append(one_hot_vector)
return np.array(one_hot_labels).astype('float32')
def get_ensembled_classification_report(X_test = None, y_test = None, ensemble_size = None, model = None, threshold = None):
"""
Returns the classification report of the ensembled model.
"""
if X_test is None or y_test is None or ensemble_size is None or model is None or threshold is None:
raise ValueError("X_test, y_test, ensemble_size and model must be provided.")
else:
# Get the predictions of the ensemble
predicted_probabilities = np.zeros((X_test.shape[0], 9))
for i in range(ensemble_size):
predicted_probabilities += model.predict(X_test)
predicted_probabilities /= ensemble_size
classified_sample_predictions = []
classified_sample_labels = []
for i in range(X_test.shape[0]):
if np.max(predicted_probabilities[i]) > threshold:
classified_sample_predictions.append(np.argmax(predicted_probabilities[i]))
classified_sample_labels.append(np.argmax(y_test[i]))
samples_classified = len(classified_sample_predictions)/X_test.shape[0]*100
return samples_classified, metrics.classification_report(classified_sample_labels, classified_sample_predictions)
def get_ensembled_entropy_report(X_test = None, y_test = None, ensemble_size = None, model = None, threshold = None):
"""
Returns the entropy report of the ensembled model.
"""
if X_test is None or y_test is None or ensemble_size is None or model is None or threshold is None:
raise ValueError("X_test, y_test, ensemble_size and model must be provided.")
else:
# Get the predictions of the ensemble
mean_predicted_probabilities = np.zeros((X_test.shape[0], 9))
entropies = np.zeros((X_test.shape[0], 1))
for i in range(ensemble_size):
predicted_probabilities = model.predict(X_test)
mean_predicted_probabilities += predicted_probabilities
entropies += np.expand_dims(-np.sum(predicted_probabilities * np.log2(predicted_probabilities), axis = 1), axis = 1)
entropies /= ensemble_size
mean_predicted_probabilities /= ensemble_size
classified_sample_predictions = []
classified_sample_labels = []
for i in range(X_test.shape[0]):
if entropies[i] < threshold:
classified_sample_predictions.append(np.argmax(mean_predicted_probabilities[i]))
classified_sample_labels.append(np.argmax(y_test[i]))
samples_classified = len(classified_sample_predictions)/X_test.shape[0]*100
return samples_classified, metrics.classification_report(classified_sample_labels, classified_sample_predictions)
def plot_sample_with_confidence(sample_index=None, X_test=None, y_test=None, ensemble_size=None, mode=None, style = None, model = None):
# Get the sample image and true label
sample_image = X_test[sample_index]
if mode is None:
true_label = np.argmax(y_test[sample_index])
#if true_label > 5:
# true_label += 1
else:
true_label = y_test[sample_index]
# Initialize an array to store the predicted probabilities for each class
predicted_probabilities = np.zeros((ensemble_size, 9))
# Make predictions using each Bayesian network
for i in range(ensemble_size):
predicted_probabilities[i] = model.predict(sample_image.reshape(1, 69, 69, 3))
pct_2p5 = np.array([np.percentile(predicted_probabilities[:, i], 2.5) for i in range(9)])
pct_97p5 = np.array([np.percentile(predicted_probabilities[:, i], 97.5) for i in range(9)])
bar_height = pct_97p5 - pct_2p5
entropies = -np.expand_dims(np.sum(predicted_probabilities*np.log2(predicted_probabilities), axis =1), axis = 1)
mean_entropy = np.mean(entropies)
# Calculate the mean probabilities for each class
mean_probabilities = np.mean(predicted_probabilities, axis=0)
# Create a bar chart with the mean probabilities and confidence intervals
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 5), gridspec_kw={'width_ratios': [10, 18]})
ax1.imshow(sample_image)
ax1.axis('off')
if mode is None:
plt.rcParams['axes.titlesize'] = 10
ax1.set_title(f'True label: {true_label}')
else:
plt.rcParams['axes.titlesize'] = 10
ax1.set_title(f'Label : OOD')
ax1.text(0.5, -0.1, 'Disk, Edge-On, Boxy Bulge', color='white', fontsize=8,
ha='center', va='center', transform=ax1.transAxes, bbox=dict(facecolor='black', alpha=0.8))
x = np.arange(9)
colours = 'green'
if style == 'bayesian':
bars_bayes = ax2.bar(x, bottom=pct_2p5, height=bar_height, width=0.8, color=colours, alpha=0.5)
else:
#bars_freq = ax2.bar(x, bottom=0, height = mean_probabilities, width = 0.8, color = colours, alpha = 0.5)
scatter_freq = ax2.scatter(x, mean_probabilities, color='red', alpha=0.5)
if style == 'bayesian':
for i, bar in enumerate(bars_bayes):
bar_x = bar.get_x()
bar_width = bar.get_width()
bar_height = bar.get_height()
mean_probability = mean_probabilities[i]
pct_2p5_i = pct_2p5[i]
pct_97p5_i = pct_97p5[i]
if mean_probability > 0.02:
ax2.plot([bar_x, bar_x + bar_width],
[mean_probability, mean_probability],
color='black', linestyle='dashed')
ax2.plot([bar_x, bar_x + bar_width],
[pct_2p5_i, pct_2p5_i],
color = 'red', linestyle = 'solid')
ax2.plot([bar_x, bar_x + bar_width],
[pct_97p5_i, pct_97p5_i],
color = 'blue', linestyle = 'solid')
ax2.text(bar_x + bar_width, mean_probability, f'{mean_probability:.2f}', verticalalignment='center', fontsize = 8)
ax2.text(bar_x-bar_width+0.25, pct_2p5_i,f'{pct_2p5_i:.2f}', verticalalignment='center', fontsize = 8)
ax2.text(bar_x-bar_width+0.25, pct_97p5_i,f'{pct_97p5_i:.2f}', verticalalignment='center', fontsize = 8)
ax2.legend(['Mean Probability', '2.5th percentile', '97.5th percentile'])
ax2.text(0.05, 0.95, f'Mean Entropy: {mean_entropy:.2f}', transform=ax2.transAxes, bbox=dict(facecolor='white', edgecolor='black', boxstyle='round,pad=0.3'))
else:
'''for i, bar in enumerate(bars_freq):
bar_x = bar.get_x()
bar_width = bar.get_width()
bar_height = bar.get_height()
mean_probability = mean_probabilities[i]
if mean_probability>0.005:
ax2.text(bar_x+bar_width/10, mean_probability+0.015, f'{mean_probability:.2f}', verticalalignment='center')'''
for i, prob in enumerate(mean_probabilities):
ax2.text(x[i], prob+0.0075, f'{prob:.2f}', ha='center', va='bottom')
ax2.set_xticks(x, ['0', '1', '2', ' 3', '4', '5', '6', '7', '8'])
ax2.set_ylim([0, 1.05])
ax2.set_xlabel('Classes')
ax2.set_ylabel('Probability')
plt.rcParams['axes.titlesize'] = 10
if style == 'bayesian':
ax2.set_title('Sample Classification with 95% CI(Bayesian)')
else:
ax2.set_title('Sample Classification(Frequentist)')
plt.show()
def create_hdf5_file(images = None, labels = None, mode = None):
"""
Creates HDF5 file from the given images and labels.
"""
if images is None or labels is None or mode is None:
raise ValueError("Images, labels and mode must be provided.")
else:
# Create the HDF5 file
samples = images.shape[0]
labels_dim = labels.shape[1]
image_size = images.shape[1]
channels = images.shape[3]
file = h5py.File(f'{mode}.hdf5', 'w')
images_set, labels_set = file.create_dataset(
'images',
(samples, image_size, image_size, channels),
dtype='f4'
), file.create_dataset(
'labels',
(samples, labels_dim),
dtype='f4'
)
# Write the images and labels to the HDF5 file
images_set[...] = images
labels_set[...] = labels
file.close()
def HDF5ImageGenerator(hdf5_x = None, hdf5_y = None, batch_size = None, mode = 'train'):
sample_count = hdf5_x.shape[0]
while True:
batch_index = 0
batches_list = list(range(int(ceil(float(sample_count) / batch_size))))
if mode == 'train':
shuffle(batches_list)
while batch_index < len(batches_list):
batch_number = batches_list[batch_index]
start = batch_number * batch_size
end = min(start + batch_size, sample_count)
# Load data from disk
x = hdf5_x[start: end]
y = hdf5_y[start: end]
# Augment batch
batch_index += 1
yield x,y