-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcallback.py
79 lines (61 loc) · 2.21 KB
/
callback.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
"""
Keras Callback
"""
import csv
import os
import numpy as np
from keras.callbacks import Callback, EarlyStopping, ModelCheckpoint, \
TensorBoard, CSVLogger
from metric import custom_metric
class CalcScore(Callback):
"""Calculate score on custom metric with Keras callback"""
def __init__(self, filename):
super(CalcScore, self).__init__()
self.file = open(filename, "w")
self.writer = None
def on_epoch_end(self, epoch, logs=None):
# Validation data
x_true = self.validation_data[0]
y_true = self.validation_data[1]
# Convert 3D one-hot label to 2D label
y_true = np.argmax(y_true, axis=2)
# Predict
y_pred = self.model.predict(x_true)
y_pred = np.argmax(y_pred, axis=2)
# Calculate score
scores, _ = custom_metric(y_true, y_pred)
# Display score
print(end="\r")
for metric, score in scores.items():
print("| {0}: {1:.4f} |".format(metric, score), sep="", end="")
# Save score to file
if not self.writer:
fields = ["epoch"] + sorted(scores.keys())
self.writer = csv.DictWriter(self.file, fieldnames=fields)
self.writer.writeheader()
row = scores
row["epoch"] = epoch
self.writer.writerow(row)
self.file.flush()
print("\n")
def on_train_end(self, logs=None):
self.file.close()
self.writer = None
class CustomCallback(object):
def __init__(self, params, path):
# Model path
model_path = os.path.join(path.checkpoint, "{epoch:04d}-{val_loss:.4f}.hdf5")
# Callbacks
self.callbacks = [
ModelCheckpoint(model_path),
TensorBoard(path.tensorboard),
CSVLogger(path.loss_log),
CalcScore(path.score_log)
]
# Early stopping
if params.es_enable:
early_stopping = EarlyStopping(monitor="val_loss",
min_delta=params.es_min_delta,
patience=params.es_patience,
verbose=1)
self.callbacks.append(early_stopping)