-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·179 lines (149 loc) · 5.17 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
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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Copyright © Idiap Research Institute <[email protected]>
#
# SPDX-FileContributor: S. Pavankumar Dubagunta <[email protected]>
# SPDX-FileContributor: Mathew Magimai Doss <[email protected]>
# SPDX-FileContributor: Olivier Bornet <[email protected]>
# SPDX-FileContributor: Olivier Canévet <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-only
"""Provide a command line interface for training a model."""
import argparse
from pathlib import Path
import keras
import numpy as np
from keras.optimizers import SGD
from .model_architecture import model_architecture
from .rawdataset import RawDataset
def train(args):
"""Train the model by running `min-epoch` at a constant LR before reducing it."""
tr_dir = args.train_feature_dir
cv_dir = args.validation_feature_dir
exp = args.output_dir
arch = args.arch
verbose = args.verbose
model_filename = Path(exp) / "cnn.keras"
# Learning parameters
learning = {
"rate": args.learning_rate,
"minEpoch": args.min_epoch,
"lrScale": args.learning_scale,
"batchSize": args.batch_size,
"spliceSize": args.splice_size,
# Threshold on validation loss reduction between
# successive epochs, below which learning rate is scaled.
"minValError": 0.002,
"minLr": 1e-7,
} # The final learning rate below which the training stops.
# Number of times the learning rate has to be scaled.
learning["lrScaleCount"] = int(
np.ceil(
np.log(learning["minLr"] / learning["rate"]) / np.log(learning["lrScale"]),
),
)
Path(exp).mkdir(exist_ok=True, parents=True)
logger = keras.callbacks.CSVLogger(
Path(exp) / "log.dat",
separator=" ",
append=True,
)
cvGen = RawDataset(cv_dir, learning["batchSize"], learning["spliceSize"])
trGen = RawDataset(tr_dir, learning["batchSize"], learning["spliceSize"])
s = SGD(
learning_rate=learning["rate"],
weight_decay=0,
momentum=0.5,
nesterov=False,
)
# Initialise model
np.random.seed(512)
m = model_architecture(arch, trGen.inputFeatDim, trGen.outputFeatDim)
# Initial training for "minEpoch-1" epochs
loss = (
"binary_crossentropy"
if trGen.outputFeatDim == 1
else "sparse_categorical_crossentropy"
)
m.compile(loss=loss, optimizer=s, metrics=["accuracy"])
print(f"Learning rate: {learning['rate']:g}")
output = m.fit(
trGen,
validation_data=cvGen,
epochs=learning["minEpoch"] - 1,
verbose=verbose,
shuffle=False,
callbacks=[logger],
)
h = [output]
m.save(model_filename, overwrite=True)
valErrorDiff = 1 + learning["minValError"] # Initialise
# Continue training till validation loss stagnates
while learning["lrScaleCount"]:
print(f"Learning rate: {learning['rate']:g}")
output = m.fit(
trGen,
validation_data=cvGen,
epochs=1,
verbose=verbose,
shuffle=False,
callbacks=[logger],
)
h.append(output)
m.save(model_filename, overwrite=True)
# Check validation error and reduce learning rate if required
valErrorDiff = h[-2].history["val_loss"][-1] - h[-1].history["val_loss"][-1]
if valErrorDiff < learning["minValError"]:
learning["rate"] *= learning["lrScale"]
learning["lrScaleCount"] -= 1
m.optimizer.learning_rate = learning["rate"]
def main():
"""Train and validate the model.
Set the KERAS_BACKEND environment variable to torch or tensorflow.
"""
parser = argparse.ArgumentParser(prog="rsclf-train", description=main.__doc__)
# fmt: off
parser.add_argument(
"--train-feature-dir", required=True,
help="Path to the directory containing the features for training",
)
parser.add_argument(
"--validation-feature-dir", required=True,
help="Path to the directory containing the features for validation",
)
parser.add_argument(
"--arch", choices=["seg", "subseg"], default="seg",
help="Model architecture name",
)
parser.add_argument(
"--splice-size", type=int, default=25,
help="Slice size for feature context",
)
parser.add_argument(
"--learning-rate", type=float, default=0.1,
help="Initial learning rate",
)
parser.add_argument(
"--learning-scale", type=float, default=0.5,
help="Factor by which to reduce the learning rate",
)
parser.add_argument(
"--batch-size", type=int, default=256,
help="Batch size",
)
parser.add_argument(
"--min-epoch", type=int, default=5,
help="Minimum epochs to run before reducing learning rate",
)
parser.add_argument(
"--output-dir", default="output-results",
help="Output directory",
)
parser.add_argument(
"--verbose", type=int, default=0,
help="Keras verbose level for fit and predict",
)
# fmt: on
args = parser.parse_args()
train(args)
if __name__ == "__main__":
main()