-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_principal_components_autoencoder.py
356 lines (308 loc) · 11.7 KB
/
train_principal_components_autoencoder.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
####################################################################################################
#
# # Train the articulatory model using the autoencoder
#
####################################################################################################
import argparse
import logging
import mlflow
import numpy as np
import os
import random
import shutil
import tempfile
import torch
import yaml
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 vt_tools import *
from helpers import set_seeds, sequences_from_dict, make_indices_dict
from phoneme_to_articulation.principal_components import run_autoencoder_epoch
from phoneme_to_articulation.principal_components.dataset import PrincipalComponentsAutoencoderDataset2
from phoneme_to_articulation.principal_components.evaluation import run_multiart_autoencoder_test
from phoneme_to_articulation.principal_components.losses import RegularizedLatentsMSELoss2
from phoneme_to_articulation.principal_components.models.autoencoder import MultiArticulatorAutoencoder
from phoneme_to_articulation.principal_components.metrics import MeanP2CPDistance
from settings import BASE_DIR, DATASET_CONFIG, TRAIN, VALID, TEST
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 reconstruction_error(
outputs,
targets,
denorm_fn_dict,
px_space=1,
res=1
):
p2cp_fn = MeanP2CPDistance(reduction="mean")
batch_size, num_articulators, n_features = outputs.shape
outputs = outputs.reshape(batch_size, num_articulators, 2, n_features // 2)
targets = targets.reshape(batch_size, num_articulators, 2, n_features // 2)
p2cps = []
for i, (_, denorm_fn) in enumerate(denorm_fn_dict.items()):
outputs[:, i, :] = denorm_fn(outputs[:, i, :])
targets[:, i, :] = denorm_fn(targets[:, i, :])
p2cp = p2cp_fn(
outputs[:, i, :].permute(0, 2, 1),
targets[:, i, :].permute(0, 2, 1)
)
p2cp_mm = p2cp * px_space * res
p2cps.append(p2cp_mm.item())
return np.mean(p2cps)
def main(
database_name,
datadir,
n_epochs,
batch_size,
patience,
learning_rate,
weight_decay,
train_seq_dict,
valid_seq_dict,
test_seq_dict,
model_params,
alpha,
num_workers=0,
clip_tails=True,
state_dict_fpath=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_encoders_path = os.path.join(RESULTS_DIR, "best_encoders.pt")
best_decoders_path = os.path.join(RESULTS_DIR, "best_decoders.pt")
last_encoders_path = os.path.join(RESULTS_DIR, "last_encoders.pt")
last_decoders_path = os.path.join(RESULTS_DIR, "last_decoders.pt")
save_checkpoint_path = os.path.join(RESULTS_DIR, "checkpoint.pt")
articulators_indices_dict = model_params["indices_dict"]
if isinstance(list(articulators_indices_dict.values())[0], int):
articulators_indices_dict = make_indices_dict(articulators_indices_dict)
model_params["indices_dict"] = articulators_indices_dict
articulators = sorted(articulators_indices_dict.keys())
autoencoder = MultiArticulatorAutoencoder(**model_params)
if state_dict_fpath is not None:
state_dict = torch.load(state_dict_fpath, map_location=device)
autoencoder.load_state_dict(state_dict)
autoencoder.to(device)
print(f"""
MultiArticulatorAutoencoder -- {autoencoder.total_parameters} parameters
""")
mlflow.log_param("num_network_params", model.total_parameters)
gen = torch.Generator(device="cpu")
gen.manual_seed(seed)
dataset_config = DATASET_CONFIG[database_name]
train_sequences = sequences_from_dict(datadir, train_seq_dict)
train_dataset = PrincipalComponentsAutoencoderDataset2(
database_name=database_name,
datadir=datadir,
sequences=train_sequences,
articulators=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,
generator=gen,
)
valid_sequences = sequences_from_dict(datadir, valid_seq_dict)
valid_dataset = PrincipalComponentsAutoencoderDataset2(
database_name=database_name,
datadir=datadir,
sequences=valid_sequences,
articulators=articulators,
clip_tails=clip_tails,
)
valid_dataloader = DataLoader(
valid_dataset,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers,
worker_init_fn=set_seeds,
generator=gen,
)
loss_fn = RegularizedLatentsMSELoss2(
indices_dict=articulators_indices_dict,
alpha=alpha,
)
optimizer = Adam(autoencoder.parameters(), lr=learning_rate, weight_decay=weight_decay)
scheduler = ReduceLROnPlateau(
optimizer,
factor=0.1,
patience=10,
min_lr=learning_rate / 1000,
)
denorm_fn_dict = {
articulator: denorm_fn.inverse
for articulator, denorm_fn
in train_dataset.normalize.items()
}
metrics = {
"p2cp_mm": lambda outputs, targets: reconstruction_error(
outputs, targets,
denorm_fn_dict=denorm_fn_dict,
px_space=dataset_config.PIXEL_SPACING,
res=dataset_config.RES,
)
}
best_metric = np.inf
epochs_since_best = 0
epochs = range(1, n_epochs + 1)
if checkpoint_filepath is not None:
checkpoint = torch.load(checkpoint_filepath, map_location=device)
autoencoder.load_state_dict(checkpoint["model"])
optimizer.load_state_dict(checkpoint["optimizer"])
scheduler.load_state_dict(checkpoint["scheduler"])
epoch = checkpoint["epoch"] + 1
epochs = range(epoch, n_epochs + 1)
best_metric = checkpoint["best_metric"]
epochs_since_best = checkpoint["epochs_since_best"]
logging.info(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_autoencoder_epoch(
phase=TRAIN,
epoch=epoch,
model=autoencoder,
dataloader=train_dataloader,
optimizer=optimizer,
criterion=loss_fn,
device=device,
)
mlflow.log_metrics({
f"train_{metric}": value for metric, value in info_train.items()
}, step=epoch)
info_valid = run_autoencoder_epoch(
phase=VALID,
epoch=epoch,
model=autoencoder,
dataloader=valid_dataloader,
optimizer=optimizer,
criterion=loss_fn,
fn_metrics=metrics,
device=device,
)
mlflow.log_metrics({
f"valid_{metric}": value for metric, value in info_valid.items()
}, step=epoch)
if info_valid["p2cp_mm"] < best_metric:
best_metric = info_valid["p2cp_mm"]
epochs_since_best = 0
torch.save(autoencoder.encoders.state_dict(), best_encoders_path)
torch.save(autoencoder.decoders.state_dict(), best_decoders_path)
mlflow.log_artifact(best_encoders_path)
mlflow.log_artifact(best_decoders_path)
else:
epochs_since_best += 1
torch.save(autoencoder.encoders.state_dict(), last_encoders_path)
torch.save(autoencoder.decoders.state_dict(), last_decoders_path)
mlflow.log_artifact(last_encoders_path)
mlflow.log_artifact(last_decoders_path)
checkpoint = {
"epoch": epoch,
"model": autoencoder.state_dict(),
"optimizer": optimizer.state_dict(),
"scheduler": scheduler.state_dict(),
"best_metric": best_metric,
"epochs_since_best": epochs_since_best,
"best_encoders_path": best_encoders_path,
"best_decoders_path": best_decoders_path,
"last_encoders_path": last_encoders_path,
"last_decoders_path": last_decoders_path,
}
torch.save(checkpoint, save_checkpoint_path)
mlflow.log_artifact(save_checkpoint_path)
print(f"""
Finished training epoch {epoch}
Best metric: {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 = PrincipalComponentsAutoencoderDataset2(
database_name=database_name,
datadir=datadir,
sequences=test_sequences,
articulators=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,
generator=gen,
)
best_autoencoder = MultiArticulatorAutoencoder(
**model_params
)
best_encoders_state_dict = torch.load(best_encoders_path, map_location=device)
best_autoencoder.encoders.load_state_dict(best_encoders_state_dict)
best_decoders_state_dict = torch.load(best_decoders_path, map_location=device)
best_autoencoder.decoders.load_state_dict(best_decoders_state_dict)
best_autoencoder.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)
info_test = run_multiart_autoencoder_test(
epoch=0,
model=best_autoencoder,
dataloader=test_dataloader,
criterion=loss_fn,
dataset_config=dataset_config,
outputs_dir=test_outputs_dir,
plots_dir=RESULTS_DIR,
indices_dict=articulators_indices_dict,
device=device,
)
mlflow.log_metrics({
f"test_{metric}": value for metric, value in info_test.items()
}, step=epoch)
mlflow.log_artifacts(test_outputs_dir, "test_outputs")
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="multiarticulator_autoencoder")
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)