-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainingcallbacks.py
68 lines (49 loc) · 2.2 KB
/
trainingcallbacks.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
import keras
import glob
import os
import numpy as np
from keras.models import Model
import matplotlib.cm as cm
import numpy as np
import numpy.ma as ma
import pylab as pl
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt
import keras
import glob
import os
import matplotlib.pyplot as plt
from sklearn import preprocessing
from keras import callbacks
from keras.callbacks import ModelCheckpoint
import matplotlib.pyplot as pyplt
import matplotlib
import matplotlib.pyplot as plt
class DirectoryHouseKeepingCallback(keras.callbacks.Callback):
def __init__(self, filepath):
self.learnedweightpath = filepath
def on_epoch_end(self, epoch, logs={}):
weightList = glob.glob(os.path.join(self.learnedweightpath, "*.hdf5"))
weightList.sort(key=os.path.getmtime)
if len(weightList) > 60:
os.remove(weightList[0])
def prepare_callbacks(configuration, fold_weights_path, data_loader):
callback_list = []
# remote_callback = callbacks.RemoteMonitor(root='http://localhost:9000')
# callback_list.append(remote_callback)
# early stopping
early_stopping_callback = callbacks.EarlyStopping(monitor=configuration.STOPPING_CRITERION,
patience=configuration.WAIT_COUNT,
verbose=0,
mode='auto')
callback_list.append(early_stopping_callback)
# save weights at the end of epoch
weights_file_name_format = 'weights.epoch{epoch:02d}-val_loss{val_loss:.2f}-val_acc{val_acc:.4f}.hdf5'
checkpoint_callback = ModelCheckpoint(os.path.join(fold_weights_path, weights_file_name_format),
monitor='val_loss', verbose=0,
save_best_only=False, mode='auto')
callback_list.append(checkpoint_callback)
# free space of stored weights of early epochs
directory_house_keeping_callback = DirectoryHouseKeepingCallback(fold_weights_path)
callback_list.append(directory_house_keeping_callback)
return callback_list