-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.py
152 lines (126 loc) · 4.74 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
import numpy as np
import os
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from tensorflow.keras.callbacks import Callback
# learning rate scheduler for classifier
def lr_schedule(epoch):
lrate = 0.001
if epoch > 70:
lrate = 0.0002
elif epoch > 85:
lrate = 0.00001
return lrate
# learning rate scheduler for AutoEncoder
def lr_schedule_ae(epoch):
lrate = 0.00015
if epoch > 40:
lrate = 0.00005
return lrate
def GetCifar10Mean():
return np.array([0.491, 0.482, 0.446], dtype=np.float32).reshape((1,1,3)) # ordering: [R, G, B]
def GetCifar10STD():
return np.array([0.247, 0.243, 0.261], dtype=np.float32).reshape((1,1,3)) # ordering: [R, G, B]
def GetImageNetMean():
return np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape((1,1,3)) # ordering: [R, G, B]
def GetImageNetSTD():
return np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape((1,1,3)) # ordering: [R, G, B]
######## Utils for Autoencoders #########
class SaveOutputImages(Callback):
'''
Save original and reconstructed images after each epoch during training process.
'''
def __init__(self, validation_generator, save_dir):
self.x_test = validation_generator.next()
self.out_dir = save_dir
self.count = 0
def on_epoch_end(self, batch, logs={}):
self.count+=1
decoded_imgs = self.model.predict(self.x_test)
n = 15
plt.figure(figsize=(n*1.5, 2*1.45))
for i in range(n):
# display original
ax = plt.subplot(2, n, i+1)
plt.imshow(self.x_test[i])
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display reconstruction
ax = plt.subplot(2, n, i + n+1)
plt.imshow(decoded_imgs[i])
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.savefig(self.out_dir + "/" + "ae_ep%d.jpg"%(self.count), bbox_inches='tight', dpi=150)
plt.close('all')
def VisualizeAE(original_imgs, decoded_imgs , out_dir, epochs):
n = 15
plt.figure(figsize=(n*1.5, 2*1.45))
for i in range(n):
# display original
ax = plt.subplot(2, n, i+1)
plt.imshow(original_imgs[i])
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display reconstruction
ax = plt.subplot(2, n, i + n+1)
plt.imshow(decoded_imgs[i])
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.savefig(out_dir + "/ae_trained_ep%d.jpg"%(epochs), bbox_inches='tight', dpi=150)
#plt.show()
plt.close()
def PlotTrainValAccuracy(history, out_dir, epochs):
# Plot training & validation accuracy values
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper left')
plt.savefig(out_dir + "/" + "train_val_accuracy_ep%d.jpg"%(epochs), bbox_inches='tight', dpi=150)
#plt.show()
plt.close()
def PlotTrainValLoss(history, out_dir, epochs):
# Plot training & validation loss values
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Val'], loc='upper left')
plt.savefig(out_dir + "/" + "train_val_loss_ep%d.jpg"%(epochs), bbox_inches='tight', dpi=150)
#plt.show()
plt.close()
def CalculateConfusionMatrix(gt_list, pred_list, target_names, img_out_path, accuracy):
print("Calculating confusion matrix ...")
cm = confusion_matrix(y_true=gt_list, y_pred=pred_list)
# normalized confusion matrix
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print(cm)
# Plot the confusion matrix as an image.
plt.matshow(cm, cmap=plt.cm.Blues)
plt.colorbar()
num_classes = len(target_names)
tick_marks = np.arange(num_classes)
plt.xticks(tick_marks, target_names)
plt.yticks(tick_marks, target_names)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix for CIFAR10. Accuracy: %f'%(accuracy))
# Loop over data dimensions and create text annotations.
fmt = '.2f'
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
plt.text(j, i, format(cm[i, j], fmt),
ha="center", va="center",
color="white" if cm[i, j] > thresh else "black")
figure = plt.gcf()
figure.set_size_inches(15, 15)
plt.savefig(img_out_path, bbox_inches='tight', dpi = 200)
#plt.show()
plt.close()