-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·347 lines (320 loc) · 9.49 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python
from dataset import RailDataset
from model import get_model
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
import os
import argparse
import pathlib
import datetime
def train(
trn_img,
val_img,
trn_msk,
val_msk,
aug_prm,
sve_pth="output/",
train_res=(320, 160),
file_ext="png",
lr=0.0001,
bs=1,
epochs=1,
load_echpoint_pth=None,
bg_dir_pth = None,
):
"""
Tran the model, save model and weights.
Training can continue at former checkpoint.
:param trn_img: Path to training image folder.
:param val_img: Path to validation image folder.
:param trn_msk: Path to training masks.
:param val_msk: Path to validation masks.
:param aug_prm: Dict of augmentation parameters.
:param sve_pth: Path to save model and other artifacts.
:param train_res: Resolution to train the network. (width, height)
:param file_ext: Extension for the image/mask data.
:param lr: Learning rate for the adam solver.
:param bs: Batch size for training and validation.
:param epochs: Training epochs to iterate over dataset.
:param load_echpoint_pth: Path to load checkpoint.
:param bg_dir_pth: Directory to images for background augmentation.
:return: None.
"""
# Get time for save path
iso_time = datetime.datetime.now().isoformat(timespec="minutes")
# Root path to save current training
sve_pth = pathlib.Path(sve_pth)
sve_pth = sve_pth.joinpath(f"{iso_time}")
# Path to metrics plot
history_pth = sve_pth.joinpath(sve_pth, "history/")
history_pth.mkdir(parents=True, exist_ok=True)
# Path to epoch checkpoints
checkpoint_pth = sve_pth.joinpath(sve_pth, f"checkpoints/")
checkpoint_pth.mkdir(parents=True, exist_ok=True)
# Path to last model incl. architecture
last_model_pth = checkpoint_pth.joinpath("end_model.h5")
# Path to tensorboard log
tb_log_pth = sve_pth
tb_log_pth.mkdir(parents=True, exist_ok=True)
# Path to training csv history
csv_history_pth = sve_pth.joinpath("history.csv")
# Path to training plot history
plt_history_pth = sve_pth.joinpath("history.svg")
# Paths to store metrics and checkpoints
history_pth2 = history_pth.joinpath("history2.svg")
history_pth3 = history_pth.joinpath("history3.csv")
history_pth = history_pth.joinpath("history.svg")
checkpoint_pth = pathlib.PurePath(
checkpoint_pth, "weights-improvement-{epoch:02d}-{val_accuracy:.2f}.hdf5"
)
# Data generator for training
trn_gen = RailDataset(
trn_img,
trn_msk,
train_res,
file_ext,
file_ext,
batch_size=bs,
tfs_prb=aug_prm,
bg_dir_pth=bg_dir_pth
)
# Data generator for validation
val_gen = RailDataset(
val_img,
val_msk,
train_res,
file_ext,
file_ext,
batch_size=bs,
transforms=False,
)
model = get_model(input_shape=train_res)
# Load former checkpoint if available.
if load_echpoint_pth is not None:
load_echpoint_pth = pathlib.Path(load_echpoint_pth)
model.load_weights(load_echpoint_pth)
# Compile model
loss = tf.keras.losses.BinaryCrossentropy(from_logits=False)
# Lower lerning rate every 5th epoch.
# One step means model optimized to one mini batch aka one iteration.
decay_steps = int(5 * (len(trn_gen)/bs))
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=lr,
decay_steps=int(decay_steps),
decay_rate=0.8,
)
opt = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
model.compile(loss=loss, optimizer=opt, metrics=["accuracy"])
# Training callbacks
cp_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_pth, save_weights_only=False, verbose=1
)
tb_callback = tf.keras.callbacks.TensorBoard(log_dir=tb_log_pth)
callbacks = [cp_callback, tb_callback]
# Train model
history = model.fit_generator(
trn_gen,
validation_data=val_gen,
epochs=epochs,
callbacks=callbacks,
workers=12,
use_multiprocessing=True,
max_queue_size=100,
)
# Save Last model
model.save(last_model_pth)
# Save history
hist_df = pd.DataFrame(history.history)
hist_df.to_csv(csv_history_pth)
# Save history plot
hist_df.plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 1)
plt.xlabel("epoch in 1")
plt.ylabel("metric in 1")
plt.savefig(plt_history_pth)
hist_df[["val_loss", "acc_loss"]].plot(figsize=(8, 5), logy=True)
plt.grid(True)
plt.savefig(history_pth2)
def parse_args(parser: argparse.ArgumentParser):
"""
Parse CLI arguments to control the training.
:param parser: Argument parser Object.
:return: CLI Arguments object.
"""
parser.add_argument(
"train_images",
type=pathlib.Path,
help="Path to the folder containing RGB training images.",
)
parser.add_argument(
"train_masks",
type=pathlib.Path,
help="Path to the folder containing training masks.",
)
parser.add_argument(
"val_images",
type=pathlib.Path,
help="Path to the folder containing RGB validation images.",
)
parser.add_argument(
"val_masks",
type=pathlib.Path,
help="Path to the folder containing validation masks.",
)
parser.add_argument(
"extension",
type=str,
help="Name of the file extension. For example: '-e jpg''.",
)
parser.add_argument(
"background_pth",
type=pathlib.Path,
help=(
"Path to direcotry contatining images used for background switch"
" augmentation. For example: '/path_to/random_images/'."
),
)
parser.add_argument(
"-o",
"--output",
type=pathlib.Path,
help=(
"Path to the folder where to store model path and "
+ "other training artifacts."
),
default="output/",
required=False,
)
parser.add_argument(
"-ep",
"--epochs",
type=int,
help="Training epochs.",
default=10,
required=False,
)
parser.add_argument(
"-l",
"--learning_rate",
type=float,
help="Learning rate for the adam solver.",
default=0.0001,
required=False,
)
parser.add_argument(
"-b",
"--batch_size",
type=int,
help="Batch size of training and validation.",
default=1,
required=False,
)
parser.add_argument(
"-g",
"--gpu",
type=int,
help="Select the GPU id to train on.",
default=0,
required=False,
)
parser.add_argument(
"--height",
type=int,
help="Height of the neural network input layer.",
default=160,
required=False,
)
parser.add_argument(
"-w",
"--width",
type=int,
help="Width of the neural network input layer.",
default=320,
required=False,
)
parser.add_argument(
"--horizontal_flip",
type=float,
help="Probability of flipping image in training set. Default is 0.5.",
default=0.5,
required=False,
)
parser.add_argument(
"--brightness_contrast",
type=float,
help="Probability of applying random brightness contrast on image in training set. Default is 0.2.",
default=0.2,
required=False,
)
parser.add_argument(
"--rotation",
type=float,
help="Probability of applying random rotation on image in training set. Default is 0.9.",
default=0.9,
required=False,
)
parser.add_argument(
"--motion_blur",
type=float,
help="Probability of applying motion blur on image in training set. Default is 0.1.",
default=0.1,
required=False,
)
parser.add_argument(
"--background_swap",
type=float,
help="Probability of applying background swap on image in training set. Default is 0.9.",
default=0.9,
required=False,
)
return parser.parse_args()
def main():
"""
Entry point for training.
"""
# Parse arguments from cli
parser = argparse.ArgumentParser()
args = parse_args(parser)
# Path parameters
trn_img = args.train_images
val_img = args.val_images
trn_msk = args.train_masks
val_msk = args.val_masks
sve_pth = args.output
bg_pth = args.background_pth
# Hardware parameters
gpu = args.gpu
os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu)
physical_devices = tf.config.list_physical_devices("GPU")
tf.config.experimental.set_memory_growth(physical_devices[gpu], True)
# Training parameters
train_res = (args.width, args.height) # Width height
lr = args.learning_rate
bs = args.batch_size
epochs = args.epochs
# Augmentation parameters
aug_prm = {
"HorizontalFlip": args.horizontal_flip,
"RandomBrightnessContrast": args.brightness_contrast,
"Rotate": args.rotation,
"MotionBlur": args.motion_blur,
"BackgroundSwap": args.background_swap,
}
print(aug_prm)
train(
trn_img,
val_img,
trn_msk,
val_msk,
aug_prm,
sve_pth=sve_pth,
epochs=epochs,
train_res=train_res,
lr=lr,
bs=bs,
bg_dir_pth=bg_pth
)
if __name__ == "__main__":
main()