-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
95 lines (74 loc) · 2.59 KB
/
train.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
"""
Train model which uses facial geometry as input
```
python train.py [model]
```
"""
import argparse
from collections import Counter
from keras import losses
from keras import callbacks
from model import architecture, loader, evaluate, preprocessing
def get_model(name, input_shape):
""""
Retrieve model architecture depending on cmd args
"""
if name == 'mlp':
return architecture.mlp(input_shape)
if name == 'mlp_fv':
return architecture.mlp_fisher(input_shape)
if name == 'cnn_rnn':
return architecture.cnn_rnn(input_shape)
raise ValueError('Model {} is not defined'.format(name))
def get_class_weights(y):
"""
Determine class weights based on frequency distribution of labels
:param y:
:return:
"""
counter = Counter(y)
majority = max(counter.values())
return {cls: float(majority/count) for cls, count in counter.items()}
def train(args):
x, y = loader.load()
if args.model == 'mlp_fv':
x = preprocessing.to_fisher(x)
nb_samples, nb_landmarks, l = x.shape
input_shape = (nb_landmarks, l)
else:
nb_samples, nb_frames, nb_landmarks, _ = x.shape
input_shape = (nb_frames, nb_landmarks, 3)
# x = loader.compact_frames(x, window_size=5, step_size=2)
x_train, y_train, x_val, y_val = loader.split_data(x, y)
model = get_model(args.model, input_shape=input_shape)
print("Input shape: {}".format(x.shape))
print(model.summary())
model.compile(optimizer='adam',
loss=losses.binary_crossentropy,
metrics=['accuracy'])
checkpointer = callbacks.ModelCheckpoint(filepath="data/weights.hdf5", verbose=1, save_best_only=True)
early_stopping = callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=2)
class_weights = get_class_weights(y_train)
model.fit(x_train, y_train,
epochs=50,
batch_size=8,
validation_data=(x_val, y_val),
callbacks=[
checkpointer,
early_stopping,
],
class_weight=class_weights,
)
# Load best model
model.load_weights('data/weights.hdf5')
# Print evaluation matrix
train_score = model.evaluate(x_train, y_train)
val_score = model.evaluate(x_val, y_val)
print(model.metrics_names, train_score, val_score)
evaluate.evaluate(model, x_train, y_train)
evaluate.evaluate(model, x_val, y_val)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('model')
args = parser.parse_args()
train(args)