-
Notifications
You must be signed in to change notification settings - Fork 7
/
train.py
153 lines (125 loc) · 5.4 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
"""
:author:
Paul Bethge (bethge@zkm.de)
2021
:License:
This package is published under Simplified BSD License.
"""
import os
import shutil
import argparse
from datetime import datetime
from yaml import load
import numpy as np
import tensorflow as tf
from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard, EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.optimizers import Adam, RMSprop, SGD
from tensorflow.keras.losses import CategoricalCrossentropy
from tensorflow.keras.metrics import Precision, Recall, CategoricalAccuracy
from tensorflow.keras.models import load_model
import src.models as models
from src.utils.training_utils import CustomCSVCallback, get_saved_model_function, visualize_results
from src.utils.training_utils import create_dataset_from_set_of_files, tf_normalize
from src.audio.augment import AudioAugmenter
def train(config_path, log_dir):
# Config
config = load(open(config_path, "rb"))
if config is None:
print("Please provide a config.")
train_dir = config["train_dir"]
val_dir = config["val_dir"]
batch_size = config["batch_size"]
languages = config["languages"]
num_epochs = config["num_epochs"]
sample_rate = config["sample_rate"]
audio_length_s = config["audio_length_s"]
augment = config["augment"]
learning_rate = config["learning_rate"]
model_name = config["model"]
model_path = config["model_path"]
# create or load the model
if model_path != "":
model = load_model(model_path)
else:
model_class = getattr(models, model_name)
model = model_class.create_model(config)
optimizer = Adam(lr=learning_rate)
model.compile(optimizer=optimizer,
loss=CategoricalCrossentropy(),
metrics=[Recall(), Precision(), CategoricalAccuracy()])
print(model.summary())
# load the dataset
train_ds = create_dataset_from_set_of_files(
ds_dir=train_dir, languages=languages)
val_ds = create_dataset_from_set_of_files(
ds_dir=val_dir, languages=languages)
# Optional augmentation of the training set
## Note: tf.py_function allows to construct a graph but code is executed in python (may be slow)
if augment:
augmenter = AudioAugmenter(audio_length_s, sample_rate)
# process a single audio array (note: dataset needs to be batched later on)
def process_aug(audio, label):
augmented_audio = augmenter.augment_audio(audio.numpy())
tensor_audio = tf.convert_to_tensor(augmented_audio, dtype=tf.float32)
return tensor_audio, label
aug_wav = lambda x,y: tf.py_function(process_aug, [x, y], [tf.float32, tf.float32])
train_ds = train_ds.map(aug_wav, num_parallel_calls=tf.data.experimental.AUTOTUNE)
# normalize audio and expand by one dimension (as required by feature extraction)
def process(audio, label):
audio = tf_normalize(audio)
audio = tf.expand_dims(audio, axis=-1)
return audio, label
train_ds = train_ds.map(process, num_parallel_calls=tf.data.experimental.AUTOTUNE)
val_ds = val_ds.map(process, num_parallel_calls=tf.data.experimental.AUTOTUNE)
# batch and prefetch data
train_ds = train_ds.batch(batch_size)
val_ds = val_ds.batch(batch_size)
train_ds = train_ds.prefetch(tf.data.experimental.AUTOTUNE)
val_ds = val_ds.prefetch(tf.data.experimental.AUTOTUNE)
# Training Callbacks
tensorboard_callback = TensorBoard(log_dir=log_dir, write_images=True)
csv_logger_callback = CustomCSVCallback(os.path.join(log_dir, "log.csv"))
reduce_on_plateau = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3,
verbose=1, min_lr=0.000001, min_delta=0.001)
checkpoint_filename = os.path.join(log_dir, "trained_models", "model.{epoch:02d}")
model_checkpoint_callback = ModelCheckpoint(checkpoint_filename, save_best_only=True, verbose=1,
monitor="val_categorical_accuracy",
save_weights_only=False)
early_stopping_callback = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1, mode="min")
# comment callbacks that you don't care about
callbacks = [
# tensorboard_callback,
csv_logger_callback,
reduce_on_plateau,
# model_checkpoint_callback,
# early_stopping_callback,
]
# Training
history = model.fit(x=train_ds, epochs=num_epochs,
callbacks=callbacks, validation_data=val_ds)
# TODO Do evaluation on model with best validation accuracy
visualize_results(history, config, log_dir)
best_epoch = np.argmax(history.history["val_categorical_accuracy"])
print("Log files: ", log_dir)
print("Best epoch: ", best_epoch)
checkpoint_filename.replace("{epoch:02d}", "{:02d}".format(best_epoch))
print("Best model at: ", checkpoint_filename)
return model, best_epoch
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--config', default="config_train.yaml",
help="Path to the required config file.")
cli_args = parser.parse_args()
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
# copy models & config for later
log_dir = os.path.join("logs", datetime.now().strftime("%Y-%m-%d-%H-%M-%S"))
print("Logging to {}".format(log_dir))
shutil.copytree("src/models", os.path.join(log_dir, "models"))
shutil.copy(cli_args.config, log_dir)
# train and save the best model as SavedModel
model, best_epoch = train(cli_args.config, log_dir)
saved_model_path = os.path.join(log_dir, "model_" + str(best_epoch))
model.save(saved_model_path, signatures=get_saved_model_function(model))
#TODO visualize the training process and save as png
#TODO convert model to saved model