-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
executable file
·212 lines (157 loc) · 6.3 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
""" Auxiliary functions for OTTER training and run.
@F. Comitani 2018-2022
"""
import os
import json
import pandas as pd
import tensorflow as tf
import keras
from keras import backend as K
#config = tf.ConfigProto(device_count={"CPU": 8})
#keras.backend.tensorflow_backend.set_session(tf.Session(config=config))
from keras.models import Sequential, model_from_json
from keras.layers import Dense, Dropout, Flatten, Conv1D, MaxPooling1D, BatchNormalization
from keras.optimizers import Adadelta
from keras.callbacks import EarlyStopping, ModelCheckpoint
import matplotlib.pyplot as plt
from metrics import *
def load_json_hparam(filename):
"""Load JSON from a path (directory + filename).
Args:
filename (str): name of the JSON file to load.
"""
with open(filename, 'r') as f:
return json.JSONDecoder().decode(
f.read()
)
def reset_weights(model):
""" Reset model's weights.
Args:
model (keras obj): model whose weights will be reset.
"""
session = K.get_session()
for layer in model.layers:
if hasattr(layer, 'kernel_initializer'):
layer.kernel.initializer.run(session=session)
def low_var_drop(df,thresh=0.99):
""" Remove low variance features from a pandas dataframe.
Args:
df (pandas dataframe): the dataframe to transform.
threhs (float): cumulative variance value threshold,
features beyond this threshold will be removed.
Return:
(pandas dataframe): the transformed dataframe.
"""
vVal=df.var(axis=0).values
cs=pd.Series(vVal).sort_values(ascending=False).cumsum()
remove=cs[cs>cs.values[-1]*thresh].index.values
return df.drop(df.columns[remove],axis=1)
def build(hparam):
"""Build a model according to hyper-space parameters.
Args:
hparam (dict): dictionary containing hyperparameters
values.
Returns:
model (Keras obj): Keras model.
"""
print("Hyperparameters:")
print(hparam)
""" Build model sequentially based on provided hyperparameters. """
model = Sequential()
#First conv layer
if hparam['first_conv'] is not None:
model.add(Conv1D(32,kernel_size=int(hparam['first_conv']),
activation='relu',
input_shape=hparam['input_shape']))
#Hidden conv layers
n_nodes = int(64 * hparam['conv_hid_uni_mult'])
for i in range(hparam['conv_pool_layers']):
if hparam['first_conv'] is None:
model.add(Conv1D(n_nodes, kernel_size=int(hparam['conv_kernel_size']), activation='relu', input_shape=hparam['input_shape']))
else:
model.add(Conv1D(n_nodes, kernel_size=int(hparam['conv_kernel_size']), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size=4, stride=2))
model.add(Flatten())
#Hidden dense layers
for i in range(hparam['dense_layers']):
n_base=int(1024.0/(i*0.5+1))
n_nodes = int(n_base * hparam['dense_hid_uni_mult'])
model.add(Dense(n_nodes, activation='relu'))
model.add(Dropout(0.6))
#Final output layer
model.add(Dense(hparam['num_classes'], activation='sigmoid'))
#Compile the model
model.compile(loss=keras.losses.binary_crossentropy,
optimizer=Adadelta(),
metrics=['accuracy', f1, precision, recall])
return model
def train(model, traindata, valdata=None, epochs = 50, batchsize = 64, patience = 3):
"""Train a model.
Args:
model (Keras obj): a Keras model.
traindata (tuple numpy array): tuple containing training data and labels
valdata (tuple numpy array): tuple containing validation data and labels,
if None do not run validation (default None).
epochs (int): number of epochs (default 50).
batchsize (int): batch size (default 64).
patience (int): number of patience epochs for early stopping (default 3,
ignored if valdata is None)
Returns:
fitting (Keras obj): the fitted model.
"""
reset_weights(model)
K.set_learning_phase(1)
K.set_image_data_format('channels_last')
""" Set early stopper. """
callbacks = None
if valdata is not None:
earlyStop = EarlyStopping(monitor='val_loss', patience=patience, mode='auto', restore_best_weights=True)
callbacks = [earlyStop]
""" Train model. """
fitting=model.fit(traindata[0], traindata[1],
batch_size=batchsize,
epochs=epochs,
shuffle=True,
verbose=1,
validation_data=valdata,
callbacks=callbacks).history
""" Performance scores. """
print("Training Results\n==========================================")
print('Best Loss:', min(fitting['loss']))
print('Best Accuracy:', max(fitting['acc']))
print('Best F1:', max(fitting['f1']))
print('Best Precision:', max(fitting['precision']))
print('Best Recall:', max(fitting['recall']))
if valdata is not None:
print("Validation Results\n==========================================")
print('Best Loss:', min(fitting['val_loss']))
print('Best Accuracy:', max(fitting['val_acc']))
print('Best F1:', max(fitting['val_f1']))
print('Best Precision:', max(fitting['val_precision']))
print('Best Recall:', max(fitting['val_recall']))
return fitting
def plot_history(history, metric, path='./'):
""" Plot the change in a given metric
through both training and validation epochs.
Args:
history (dict): dictionary containing the values
to plot.
metric (str): key for the history dictionary,
the name of the metric to plot.
path (str): path where the png plot will be saved.i
"""
plt.plot(history[metric])
legs=['train']
if 'val_'+metric in history:
plt.plot(history['val_'+metric])
legs.append('val')
plt.title('model '+metric)
plt.ylabel(metric)
plt.xlabel('epoch')
plt.legend(legs, loc='upper left')
plt.tight_layout()
plt.savefig(os.path.join(path, metric+'_training.png'), bbox_inches='tight', dpi=600)
plt.clf()
if __name__ == "__main__":
pass