-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallBacks.py
344 lines (262 loc) · 12.4 KB
/
CallBacks.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
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
from keras.callbacks import Callback
from sklearn.metrics import precision_score, recall_score, f1_score
from datasetGenerator import DCASE2018
from Binarizer import Binarizer
import os, sys, time
class CompleteLogger(Callback):
def __init__(self, logPath: str, validation_data: tuple, history_size: int = 10,
fallback: bool = False, fallBackThreshold: int = 5, stopAt: int = 100,
display: bool = True
):
super().__init__()
self.validation_input = validation_data[0]
self.validation_output = validation_data[1]
self.metrics = []
self.trainMetrics = []
self.validationMetrics = []
self.logging = logPath is not None
self.logPath = {"general": logPath}
self.logFile = {}
self.currentEpoch = 0
self.stopAt = stopAt
self.epochStart = 0
self.epochDuration = 0
self.history_size = history_size
self.history = [] # a history of the best models
self.sortedHistory = []
self.f1History = {"train": [], "val": []}
self.fallbackCooldown = self.history_size
self.fallbackTh = fallBackThreshold
self.nbFallback = 0
self.cooldown = 0
self.coolingDown = False
self.nbMaxFallback = 3
self.transferMode = False
self.display = display
self.binarizer = Binarizer()
def toggleTransfer(self):
self.transferMode != self.transferMode
def on_train_begin(self, logs=None):
super().on_train_begin(logs)
self.__initLogFiles()
# Split the complete list of metrics into two separated list. training and validation (left and right)
middle = int(len(self.params["metrics"]) / 2)
self.metrics = self.params["metrics"]
self.trainMetrics = self.params["metrics"][:middle]
self.validationMetrics = self.params["metrics"][middle:]
self.__printHeader()
self.__logGeneralHeader()
def on_train_end(self, logs=None):
super().on_train_end(logs)
if self.logPath is not None:
self.__finishLogFiles()
def on_batch_end(self, batch, logs=None):
self.__printMetrics(logs)
def on_epoch_begin(self, epoch, logs=None):
super().on_epoch_begin(epoch, logs)
self.currentEpoch += 1
# if self.currentEpoch == 35:
# currentLr = K.get_value(self.model.optimizer.lr)
# K.set_value(self.model.optimizer.lr, currentLr / 2)
self.epochStart = time.time()
def on_epoch_end(self, epoch, logs=None):
"""
Add the saving of the f1 measure for each class separately on a file called: metrics_learn
"""
super().on_epoch_end(epoch, logs)
self.epochDuration = time.time() - self.epochStart
self.__computeMetrics()
self.__toHistory(logs)
self.__printMetrics(logs, validation=True, overwrite=False)
self.__logGeneralEpoch(logs)
self.__logClassesEpoch()
self.__fallingBack()
# stop training if stopAt is reached
if self.currentEpoch == self.stopAt:
self.model.stop_training = True
# ==================================================================================================================
# Classes metrics compute
# ==================================================================================================================
def __computeMetrics(self):
# compute the results of the metrics and log them to the files
# calc metrics for each classes separately
prediction = self.model.predict(self.validation_input)
prediction = self.binarizer.binarize(prediction)
self.precision = precision_score(self.validation_output, prediction, average=None)
self.recall = recall_score(self.validation_output, prediction, average=None)
self.f1 = f1_score(self.validation_output, prediction, average=None)
# ==================================================================================================================
# HISTORY AND FALLBACK FUNCTION
# ==================================================================================================================
def __toHistory(self, logs=None):
average_f1 = float(logs[self.validationMetrics[-1]])
# add the current model to the list of history
self.history.append(
{"weights": self.model.get_weights(),
"average f1": average_f1,
"epoch": self.currentEpoch}
)
self.sortedHistory.append(
{"weights": self.model.get_weights(),
"average f1": average_f1,
"epoch": self.currentEpoch}
)
self.f1History["train"].append(float(logs[self.trainMetrics[-1]]))
self.f1History["val"].append(float(logs[self.validationMetrics[-1]]))
# sort the list using the average f1 key
self.sortedHistory = sorted(self.history, key=lambda k: k['average f1'])
# keep only the <history_size> first
self.history = self.history[:self.history_size]
self.sortedHistory = self.sortedHistory[-self.history_size:]
self.f1History["train"] = self.f1History["train"][:self.history_size]
self.f1History["val"] = self.f1History["val"][:self.history_size]
def __cutHistoryTo(self, toCut: int):
self.history = self.history[:toCut]
self.f1History["train"] = self.f1History["train"][:toCut]
self.f1History["val"] = self.f1History["val"][:toCut]
def __fallingBack(self):
# managing cooling down
if self.coolingDown:
self.cooldown += 1
if self.cooldown > self.fallbackCooldown:
self.cooldown = 0
self.coolingDown = False
print("cooling down")
return
# if too much fallingback
if self.nbFallback == self.nbMaxFallback:
return
# if not enough time spent
if self.currentEpoch < self.history_size:
return
curF1Val = self.f1History["val"][-1]
curF1Tra = self.f1History["train"][-1]
diff = curF1Tra - curF1Val
if diff > self.fallbackTh:
betterEpoch = self.history_size - 1
mini = diff
for i in range(self.history_size - 1, -1, -1):
cDiff = self.f1History["train"][i] - self.f1History["val"][i]
if cDiff < mini:
mini = cDiff
betterEpoch = i
print("Overfitting... going back in time %s epochs behind" % (betterEpoch))
print("train, val diff: %.2f" % mini)
self.model.set_weights(self.history[betterEpoch]["weights"])
self.nbFallback += 1
# pruning history
cutoff = self.currentEpoch - (self.history_size - betterEpoch)
self.__cutHistoryTo(cutoff)
# starting cooldown
self.coolingDown = True
# ==================================================================================================================
# LOG FUNCTIONS
# ==================================================================================================================
def __initLogFiles(self):
if not self.logging:
return
dirPath = self.logPath["general"]
dirName = os.path.dirname(dirPath)
fileName = os.path.basename(dirPath)
if not os.path.isdir(dirName):
print("doesn't exist, creating it")
os.makedirs(dirName)
# add one files for each metrics that will be compute for classes
self.logPath["precision"] = dirPath + "_precision.csv"
self.logPath["recall"] = dirPath + "_recall.csv"
self.logPath["f1"] = dirPath + "_f1.csv"
self.logPath["general"] += "_metrics.csv"
# open the files
for key in self.logPath.keys():
if not self.transferMode:
self.logFile[key] = open(self.logPath[key], "w")
else:
self.logFile[key] = open(self.logPath[key], "a")
# write headers
if not self.transferMode:
for key in self.logFile:
if key != "general":
self.__logClassesheader(self.logFile[key])
self.__logGeneralHeader()
def __finishLogFiles(self):
if not self.logging:
return
for key in self.logPath.keys():
self.logFile[key].close()
def __logGeneralHeader(self):
if self.logging:
self.logFile["general"].write("epoch,progress,")
for m in self.metrics:
self.logFile["general"].write("%s," % m)
self.logFile["general"].write("duration\n")
def __logGeneralEpoch(self, logs: dict):
if self.logging:
self.logFile["general"].write("%s,100," % (self.currentEpoch))
# all metrics
for m in self.metrics:
if m != self.validationMetrics[-1]:
self.logFile["general"].write("%s," % str(logs[m])[:6])
else:
self.logFile["general"].write("%s" % str(logs[m])[:6])
self.logFile["general"].write("%s\n" % self.epochDuration)
def __logClassesheader(self, file):
if self.logging:
file.write("epoch,")
for key in DCASE2018.class_correspondance.keys():
if key != list(DCASE2018.class_correspondance.keys())[-1]:
file.write("%s," % key)
else:
file.write("%s\n" % key)
def __logClassesEpoch(self):
def convertToCSV(line: list):
return ",".join(map(str, line))
if self.logging:
self.logFile["precision"].write(str(self.currentEpoch) + "," + convertToCSV(self.precision) + "\n")
self.logFile["recall"].write(str(self.currentEpoch) + "," + convertToCSV(self.recall) + "\n")
self.logFile["f1"].write(str(self.currentEpoch) + "," + convertToCSV(self.f1) + "\n")
# ==================================================================================================================
# DISPLAY FUNCTIONS
# ==================================================================================================================
def __printMetrics(self, logs: dict, validation: bool = False, overwrite: bool = True):
if self.display:
# two first column
print("{:<8}".format(self.currentEpoch), end="")
if "batch" in logs.keys():
percent = int(logs["batch"]) * int(self.params["batch_size"]) / int(self.params["samples"]) * 100
print("%{:<10}".format(str(int(percent))[:3]), end="")
else:
print("%{:<10}".format("100"), end="")
for m in self.trainMetrics:
print("{:<12}".format(str(logs[m])[:6]), end="")
if validation:
print(" | ", end="")
for m in self.validationMetrics:
print("{:<12}".format(str(logs[m])[:6]), end="")
if overwrite:
print("", end="\r")
else:
print(" %.2f" % self.epochDuration, end="")
print("", end="\n")
def __printHeader(self):
if self.display:
# Print the complete header
print("{:<8}".format("epoch"), end="")
print("{:<10}".format("progress"), end="")
# print training metric name
for m in self.trainMetrics:
print("{:<12}".format(m[:10]), end="")
# print validation metric name
print(" | ", end="")
for m in self.validationMetrics:
print("{:<12}".format(m[:10]), end="")
print("")
print("-" * (18 + 12 * len(self.trainMetrics) + 3 + 12 * len(self.validationMetrics)) )
# ==================================================================================================================
# EARLY KILL HANDLER
# ==================================================================================================================
def __exitAndSave(self, signum, frame):
print("SIGTERM OR SIGKILL SIGNAL RECEIVED...")
print("Saving the best model to early_stop_weights.h5")
self.model.set_weights(self.sortedHistory[-1]["weights"])
self.model.save_weights("early_stop_weights.h5py")
sys.exit(2)