-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_phoneme_to_articulation.py
426 lines (373 loc) · 12.5 KB
/
train_phoneme_to_articulation.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
####################################################################################################
#
# Train the model-free phoneme-to-articulation
#
####################################################################################################
import argparse
import logging
import mlflow
import numpy as np
import os
import pandas as pd
import random
import shutil
import tempfile
import torch
import ujson
import yaml
from collections import OrderedDict
from numpy.random import MT19937
from numpy.random import RandomState, SeedSequence
from torch.optim import Adam
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.utils.data import DataLoader
from tqdm import tqdm
from helpers import set_seeds, sequences_from_dict, make_padding_mask
from phoneme_to_articulation.encoder_decoder.dataset import ArtSpeechDataset, pad_sequence_collate_fn
from phoneme_to_articulation.encoder_decoder.evaluation import run_test
from phoneme_to_articulation.encoder_decoder.metrics import P2CPDistance
from phoneme_to_articulation.encoder_decoder.models import (
ArtSpeech,
SimpleArtSpeech,
)
from phoneme_to_articulation.metrics import EuclideanDistance
from settings import BASE_DIR, TRAIN, VALID, DATASET_CONFIG, BLANK, UNKNOWN
TMPFILES = os.path.join(BASE_DIR, "tmp")
TMP_DIR = tempfile.mkdtemp(dir=TMPFILES)
RESULTS_DIR = os.path.join(TMP_DIR, "results")
if not os.path.exists(RESULTS_DIR):
os.makedirs(RESULTS_DIR)
def run_epoch(
phase,
epoch,
model,
dataloader,
optimizer,
criterion,
fn_metrics=None,
scheduler=None,
device=None,
):
if device is None:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if fn_metrics is None:
fn_metrics = {}
training = phase == TRAIN
if training:
model.train()
else:
model.eval()
losses = []
metrics_values = {metric_name: [] for metric_name in fn_metrics}
progress_bar = tqdm(dataloader, desc=f"Epoch {epoch} - {phase}")
for (
_, # sentence_ids
sentence,
targets,
lengths,
_, # phonemes
_, # references
_, # sentence_frames
_, # voicing
) in progress_bar:
sentence = sentence.to(device)
targets = targets.to(device)
optimizer.zero_grad()
with torch.set_grad_enabled(training):
outputs = model(sentence, lengths)
loss = criterion(outputs, targets)
padding_mask = make_padding_mask(lengths)
bs, max_len, num_articulators, features = loss.shape
loss = loss.view(bs * max_len, num_articulators, features)
loss = loss[padding_mask.view(bs * max_len)].mean()
if training:
loss.backward()
optimizer.step()
if scheduler is not None:
scheduler.step()
for metric_name, fn_metric in fn_metrics.items():
metric_val = fn_metric(outputs, targets, lengths)
metrics_values[metric_name].append(metric_val.item())
losses.append(loss.item())
postfixes = {
"loss": np.mean(losses)
}
postfixes.update({
metric_name: np.mean(metric_vals)
for metric_name, metric_vals in metrics_values.items()
})
progress_bar.set_postfix(OrderedDict(postfixes))
mean_loss = np.mean(losses)
info = {
"loss": mean_loss
}
info.update({
metric_name: np.mean(metric_values)
for metric_name, metric_values in metrics_values.items()
})
return info
def main(
datadir,
database_name,
num_epochs,
batch_size,
patience,
learning_rate,
weight_decay,
train_seq_dict,
valid_seq_dict,
test_seq_dict,
vocab_filepath,
articulators,
model_kwargs=None,
num_workers=0,
clip_tails=True,
state_dict_filepath=None,
checkpoint_filepath=None,
seed=0,
):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
logging.info(f"Running on '{device.type}'")
best_model_path = os.path.join(RESULTS_DIR, "best_model.pt")
last_model_path = os.path.join(RESULTS_DIR, "last_model.pt")
save_checkpoint_path = os.path.join(RESULTS_DIR, "checkpoint.pt")
default_tokens = [BLANK, UNKNOWN]
vocabulary = {token: i for i, token in enumerate(default_tokens)}
with open(vocab_filepath) as f:
tokens = ujson.load(f)
for i, token in enumerate(tokens, start=len(vocabulary)):
vocabulary[token] = i
num_articulators = len(articulators)
model_kwargs = model_kwargs or {}
model = ArtSpeech(
len(vocabulary),
num_articulators,
**model_kwargs,
)
if state_dict_filepath is not None:
state_dict = torch.load(state_dict_filepath, map_location=device)
model.load_state_dict(state_dict)
model.to(device)
print(f"""
ArtSpeech -- {model.total_parameters} parameters
""")
mlflow.log_param("num_network_params", model.total_parameters)
loss_fn = EuclideanDistance(reduction="none")
optimizer = Adam(
model.parameters(),
lr=learning_rate,
weight_decay=weight_decay
)
scheduler = ReduceLROnPlateau(
optimizer,
factor=0.1,
patience=10,
)
gen = torch.Generator(device="cpu")
gen.manual_seed(seed)
train_sequences = sequences_from_dict(datadir, train_seq_dict)
train_dataset = ArtSpeechDataset(
datadir,
database_name,
train_sequences,
vocabulary,
articulators,
clip_tails=clip_tails,
)
train_dataloader = DataLoader(
train_dataset,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers,
worker_init_fn=set_seeds,
collate_fn=pad_sequence_collate_fn,
generator=gen,
)
valid_sequences = sequences_from_dict(datadir, valid_seq_dict)
valid_dataset = ArtSpeechDataset(
datadir,
database_name,
valid_sequences,
vocabulary,
articulators,
clip_tails=clip_tails,
)
valid_dataloader = DataLoader(
valid_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
worker_init_fn=set_seeds,
collate_fn=pad_sequence_collate_fn,
generator=gen,
)
dataset_config = DATASET_CONFIG[database_name]
fn_metrics = {
"p2cp_mean": P2CPDistance(
dataset_config=dataset_config,
)
}
epochs = range(1, num_epochs + 1)
best_metric = np.inf
epochs_since_best = 0
if checkpoint_filepath is not None:
checkpoint = torch.load(checkpoint_filepath, map_location=device)
model.load_state_dict(checkpoint["model"])
optimizer.load_state_dict(checkpoint["optimizer"])
scheduler.load_state_dict(checkpoint["scheduler"])
epoch = checkpoint["epoch"] + 1
epochs = range(epoch, num_epochs + 1)
best_metric = checkpoint["best_metric"]
epochs_since_best = checkpoint["epochs_since_best"]
best_model_path = checkpoint["best_model_path"]
last_model_path = checkpoint["last_model_path"]
print(f"""
Loaded checkpoint -- Launching training from epoch {epoch} with best metric
so far {best_metric} seen {epochs_since_best} epochs ago.
""")
for epoch in epochs:
info_train = run_epoch(
phase=TRAIN,
epoch=epoch,
model=model,
dataloader=train_dataloader,
optimizer=optimizer,
criterion=loss_fn,
device=device,
)
mlflow.log_metrics(
{"train_loss": info_train["loss"]},
step=epoch
)
info_valid = run_epoch(
phase=VALID,
epoch=epoch,
model=model,
dataloader=valid_dataloader,
optimizer=optimizer,
criterion=loss_fn,
device=device,
fn_metrics=fn_metrics,
)
mlflow.log_metrics(
{"valid_loss": info_valid["loss"]},
step=epoch
)
scheduler.step(info_valid["loss"])
if info_valid["p2cp_mean"] < best_metric:
best_metric = info_valid["p2cp_mean"]
torch.save(model.state_dict(), best_model_path)
mlflow.log_artifact(best_model_path)
epochs_since_best = 0
else:
epochs_since_best += 1
torch.save(model.state_dict(), last_model_path)
mlflow.log_artifact(last_model_path)
checkpoint = {
"epoch": epoch,
"model": model.state_dict(),
"optimizer": optimizer.state_dict(),
"scheduler": scheduler.state_dict(),
"best_metric": best_metric,
"epochs_since_best": epochs_since_best,
"best_model_path": best_model_path,
"last_model_path": last_model_path
}
torch.save(checkpoint, save_checkpoint_path)
mlflow.log_artifact(save_checkpoint_path)
print(f"""
Finished training epoch {epoch}
Best metric: {'%0.4f' % best_metric}, Epochs since best: {epochs_since_best}
""")
if epochs_since_best > patience:
break
test_sequences = sequences_from_dict(datadir, test_seq_dict)
test_dataset = ArtSpeechDataset(
datadir,
database_name,
test_sequences,
vocabulary,
articulators,
clip_tails=clip_tails,
)
test_dataloader = DataLoader(
test_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
worker_init_fn=set_seeds,
collate_fn=pad_sequence_collate_fn,
generator=gen,
)
best_model = ArtSpeech(
len(vocabulary),
num_articulators,
**model_kwargs,
)
state_dict = torch.load(best_model_path, map_location=device)
best_model.load_state_dict(state_dict)
best_model.to(device)
test_outputs_dir = os.path.join(RESULTS_DIR, "test_outputs")
if not os.path.exists(test_outputs_dir):
os.makedirs(test_outputs_dir)
test_results = run_test(
epoch=0,
model=best_model,
dataloader=test_dataloader,
criterion=loss_fn,
outputs_dir=test_outputs_dir,
articulators=test_dataset.articulators,
device=device,
regularize_out=True,
)
mlflow.log_artifact(test_outputs_dir)
test_results_filepath = os.path.join(RESULTS_DIR, "test_results.json")
with open(test_results_filepath, "w") as f:
ujson.dump(test_results, f)
mlflow.log_artifact(test_results_filepath)
results_item = {
"loss": test_results["loss"],
}
for articulator in test_dataset.articulators:
results_item[f"p2cp_{articulator}"] = test_results[articulator]["p2cp"]
results_item[f"med_{articulator}"] = test_results[articulator]["med"]
df = pd.DataFrame([results_item])
df_filepath = os.path.join(RESULTS_DIR, "test_results.csv")
df.to_csv(df_filepath, index=False)
mlflow.log_artifact(df_filepath)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", dest="config_filepath")
parser.add_argument("--mlflow", dest="mlflow_tracking_uri", default=None)
parser.add_argument("--experiment", dest="experiment_name", default="phoneme_to_articulation")
parser.add_argument("--run_id", dest="run_id", default=None)
parser.add_argument("--run_name", dest="run_name", default=None)
parser.add_argument("--checkpoint", dest="checkpoint_filepath", default=None)
args = parser.parse_args()
seed = 0
rs = RandomState(MT19937(SeedSequence(seed)))
random.seed(seed)
torch.manual_seed(seed)
if args.mlflow_tracking_uri is not None:
mlflow.set_tracking_uri(args.mlflow_tracking_uri)
with open(args.config_filepath) as f:
cfg = yaml.safe_load(f)
experiment = mlflow.set_experiment(args.experiment_name)
with mlflow.start_run(
run_id=args.run_id,
experiment_id=experiment.experiment_id,
run_name=args.run_name
) as run:
print(f"Experiment ID: {experiment.experiment_id}\nRun ID: {run.info.run_id}")
try:
mlflow.log_artifact(args.config_filepath)
except shutil.SameFileError:
logging.info("Skipping logging config file since it already exists.")
try:
main(
**cfg,
checkpoint_filepath=args.checkpoint_filepath,
seed=seed,
)
finally:
shutil.rmtree(TMP_DIR)