forked from Gastron/sb-fin-parl-2015-2020-kevat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain-lfmmi-xent-fbank-mtl-outnorm-w2v2.py
348 lines (305 loc) · 13.6 KB
/
train-lfmmi-xent-fbank-mtl-outnorm-w2v2.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
#!/usr/bin/env/python3
"""Finnish Parliament ASR
"""
import os
import sys
import torch
import logging
import speechbrain as sb
from hyperpyyaml import load_hyperpyyaml
from speechbrain.utils.distributed import run_on_main
import webdataset as wds
from glob import glob
import io
import torchaudio
import local
import tqdm
from pychain import ChainGraph, ChainGraphBatch
import simplefst
import pathlib
from concurrent.futures import ThreadPoolExecutor
logger = logging.getLogger(__name__)
# Brain class for speech recognition training
class LFMMIAM(sb.Brain):
def __init__(self, train_fsts={}, threadpool_workers=4, *args, **kwargs):
super().__init__(*args, **kwargs)
self.train_fsts = train_fsts
self.executor = ThreadPoolExecutor(max_workers = threadpool_workers)
def compute_forward(self, batch, stage):
batch = batch.to(self.device)
wavs, wav_lens = batch.wav
# Add augmentation if specified
if stage == sb.Stage.TRAIN:
if hasattr(self.modules, "env_corrupt"):
wavs = self.modules.env_corrupt(wavs, wav_lens)
feats = self.modules.wav2vec2(wavs)
if self.hparams.subsampling == 2:
pass
elif self.hparams.subsampling == 3:
feats = torch.repeat_interleave(feats,2,dim=1)[:,::self.hparams.subsampling,:]
elif self.hparams.subsampling == 4:
feats = feats[:,::2,:]
encoded = self.modules.encoder(feats)
lfmmi_out = self.modules.lfmmi_lin_out(encoded)
xent_out = self.modules.xent_lin_out(encoded)
xent_predictions = self.hparams.log_softmax(xent_out)
return lfmmi_out, xent_predictions
def load_graph(self, uttid):
try:
fstpath, offset = self.train_fsts[uttid]
return ChainGraph(simplefst.StdVectorFst.read_ark(fstpath, offset), log_domain=True)
except:
return None
def compute_objectives(self, predictions, batch, stage):
lfmmi_out, xent_predictions = predictions
# Get the grahps:
if stage == sb.Stage.TRAIN:
futures = []
for uttid in batch.__key__:
futures.append(self.executor.submit(self.load_graph, uttid))
graphs = []
for future in futures:
result = future.result()
graphs.append(result)
if result is None:
raise ValueError("Empty Graph I GUESS")
else:
graphs = batch.graph
num_transitions = list(map(self.hparams.transgetter, graphs))
output_lengths = (lfmmi_out.shape[1] * batch.wav.lengths).int().cpu()
max_num_states = max(map(self.hparams.stategetter, graphs))
numerator_graphs = ChainGraphBatch(
graphs,
max_num_transitions=max(num_transitions),
max_num_states=max_num_states
)
lfmmi_loss = self.hparams.chain_loss(lfmmi_out, output_lengths, numerator_graphs)
xent_loss = sb.nnet.losses.nll_loss(
log_probabilities=xent_predictions,
length=batch.ali.lengths,
targets=batch.ali.data,
label_smoothing=self.hparams.label_smoothing,
)
output_norm_loss = torch.linalg.norm(lfmmi_out,dim=2).mean()
loss = lfmmi_loss + self.hparams.xent_scale * xent_loss + output_norm_loss*self.hparams.outnorm_scale
if stage != sb.Stage.TRAIN:
min_length = min(xent_predictions.shape[1], batch.ali.data.shape[1])
self.accuracy_metric.append(xent_predictions[:,:min_length,:], batch.ali.data[:,:min_length], length=batch.ali.lengths)
return loss
def on_stage_start(self, stage, epoch):
if stage != sb.Stage.TRAIN:
self.accuracy_metric = self.hparams.accuracy_computer()
def on_stage_end(self, stage, stage_loss, epoch):
stage_stats = {"loss": stage_loss}
# Store the train loss until the validation stage.
if stage == sb.Stage.TRAIN:
self.train_stats = stage_stats
# Summarize the statistics from the stage for record-keeping.
else:
stage_stats["accuracy"] = self.accuracy_metric.summarize()
# Perform end-of-iteration things, like annealing, logging, etc.
if stage == sb.Stage.VALID:
# Update learning rate
old_lr_model, new_lr_model = self.hparams.lr_annealing_model(stage_stats["loss"])
sb.nnet.schedulers.update_learning_rate(self.model_optimizer, new_lr_model)
old_lr_w2v, new_lr_w2v = self.hparams.lr_annealing_wav2vec(stage_stats["loss"])
sb.nnet.schedulers.update_learning_rate(self.wav2vec_optimizer, new_lr_w2v)
# The train_logger writes a summary to stdout and to the logfile.
self.hparams.train_logger.log_stats(
stats_meta={"epoch": epoch, "lr_model": old_lr_model, "lr_w2v": old_lr_w2v},
train_stats=self.train_stats,
valid_stats=stage_stats,
)
# Save the current checkpoint and delete previous checkpoints.
self.checkpointer.save_and_keep_only(
meta={"loss": stage_stats["loss"], "xent-accuracy": stage_stats["accuracy"]},
min_keys=["loss"],
num_to_keep=getattr(self.hparams, "ckpts_to_keep", 1)
)
# We also write statistics about test data to stdout and to the logfile.
elif stage == sb.Stage.TEST:
self.hparams.train_logger.log_stats(
stats_meta={"Epoch loaded": self.hparams.epoch_counter.current},
test_stats=stage_stats,
)
def init_optimizers(self):
"Initializes the wav2vec2 optimizer and model optimizer"
if not self.hparams.wav2vec2.freeze:
self.wav2vec_optimizer = self.hparams.wav2vec_opt_class(
self.modules.wav2vec2.parameters()
)
if self.checkpointer is not None:
self.checkpointer.add_recoverable(
"wav2vec_opt", self.wav2vec_optimizer
)
self.model_optimizer = self.hparams.model_opt_class(
self.hparams.model.parameters()
)
if self.checkpointer is not None:
self.checkpointer.add_recoverable("modelopt", self.model_optimizer)
def fit_batch(self, batch):
"""Train the parameters given a single batch in input"""
should_step = self.step % self.hparams.grad_accumulation_factor == 0
outputs = self.compute_forward(batch, sb.Stage.TRAIN)
loss = self.compute_objectives(outputs, batch, sb.Stage.TRAIN)
(loss / self.hparams.grad_accumulation_factor).backward()
if should_step:
if self.check_gradients(loss):
if not self.hparams.wav2vec2.freeze:
self.wav2vec_optimizer.step()
self.model_optimizer.step()
if not self.hparams.wav2vec2.freeze:
self.wav2vec_optimizer.zero_grad()
self.model_optimizer.zero_grad()
return loss.detach()
def on_evaluate_start(self, max_key=None, min_key=None):
super().on_evaluate_start(max_key=max_key, min_key=min_key)
if getattr(self.hparams, "avg_ckpts", 1) > 1:
ckpts = self.checkpointer.find_checkpoints(
max_key=max_key,
min_key=min_key,
max_num_checkpoints=self.hparams.avg_ckpts
)
model_state_dict = sb.utils.checkpoints.average_checkpoints(
ckpts, "model"
)
self.hparams.model.load_state_dict(model_state_dict)
self.checkpointer.save_checkpoint(name=f"AVERAGED-{self.hparams.avg_ckpts}")
def estimate_prior_empirical(self, train_data, loader_kwargs={}, max_key=None, min_key=None):
self.on_evaluate_start(max_key=max_key, min_key=min_key)
self.hparams.train_logger.log_stats(
stats_meta={"Epoch loaded for prior": self.hparams.epoch_counter.current},
)
dataloader = self.make_dataloader(train_data, **loader_kwargs, stage=sb.Stage.TEST)
with torch.no_grad():
prior_floor = 1.0e-15
prior = torch.ones((self.hparams.num_units,)) * prior_floor
for batch in tqdm.tqdm(dataloader):
lfmmi_pred, log_predictions = self.compute_forward(batch, stage=sb.Stage.TEST)
predictions = log_predictions.exp()
lengths = batch.wav.lengths*predictions.shape[1]
mask = sb.dataio.dataio.length_to_mask(lengths).float()
summed_preds = torch.sum(predictions * mask.unsqueeze(-1), dim=(0,1))
prior += summed_preds.detach().cpu()
# Normalize:
prior = prior / prior.sum()
return prior.log()
def numfsts_to_local_tmp(fstdir, tmpdir):
"""Copies the chain numerator FSTs onto a local disk"""
fstdir = pathlib.Path(fstdir)
tmpdir = pathlib.Path(tmpdir)
tmpdir.mkdir(parents=True, exist_ok=True)
sb.utils.superpowers.run_shell(f"rsync --update {fstdir}/num.*.ark {tmpdir}/")
numfsts = {}
for scpfile in fstdir.glob("num.*.scp"):
with open(scpfile) as fin:
for line in fin:
uttid, data = line.strip().split()
arkpath, offset = data.split(":")
newpath = arkpath.replace(str(fstdir), str(tmpdir))
numfsts[uttid] = (newpath, int(offset))
return numfsts
def dataio_prepare(hparams, numfsts):
"""This function prepares the datasets to be used in the brain class.
It also defines the data processing pipeline through user-defined functions.
Arguments
---------
hparams : dict
This dictionary is loaded from the `train.yaml` file, and it includes
all the hyperparameters needed for dataset construction and loading.
Returns
-------
datasets : dict
Dictionary containing "train", "valid", and "test" keys mapping to
WebDataset datasets dataloaders for them.
"""
def load_valid_fst(sample):
uttid = sample["__key__"]
fstpath, offset = numfsts["valid"][uttid]
sample["graph"] = ChainGraph(simplefst.StdVectorFst.read_ark(fstpath, offset), log_domain=True)
return sample
traindata = (
wds.WebDataset(hparams["trainshards"])
.decode()
.rename(wav="audio.pth", ali="ali.pth")
.repeat()
.then(
sb.dataio.iterators.dynamic_bucketed_batch,
**hparams["dynamic_batch_kwargs"]
)
)
validdata = (
wds.WebDataset(hparams["validshards"])
.decode()
.rename(wav="audio.pth", ali="ali.pth")
.map(load_valid_fst, handler=wds.warn_and_continue)
.then(
sb.dataio.iterators.dynamic_bucketed_batch,
drop_end=False,
**hparams["valid_dynamic_batch_kwargs"],
)
)
return {"train": traindata, "valid": validdata}
if __name__ == "__main__":
# Reading command line arguments
hparams_file, run_opts, overrides = sb.parse_arguments(sys.argv[1:])
# Load hyperparameters file with command-line overrides
with open(hparams_file) as fin:
hparams = load_hyperpyyaml(fin, overrides)
# Create experiment directory
sb.create_experiment_directory(
experiment_directory=hparams["output_folder"],
hyperparams_to_save=hparams_file,
overrides=overrides,
)
# Copy numerator FSTs to local drive:
numfsts = {}
numfsts["train"] = numfsts_to_local_tmp(hparams["numfstdir"], hparams["numfsttmpdir"])
numfsts["valid"] = numfsts_to_local_tmp(hparams["valid_numfstdir"], hparams["valid_numfsttmpdir"])
# We can now directly create the datasets for training, valid, and test
datasets = dataio_prepare(hparams, numfsts)
# read valid data into memory:
datasets["valid"] = torch.utils.data.DataLoader(
list(iter(datasets["valid"])),
batch_size=None
)
# Pretrain if defined:
if "pretrainer" in hparams:
if "pretrain_max_key" in hparams:
ckpt = hparams["ckpt_finder"].find_checkpoint(max_key=hparams["pretrain_max_key"])
elif "pretrain_min_key" in hparams:
ckpt = hparams["ckpt_finder"].find_checkpoint(min_key=hparams["pretrain_min_key"])
else:
ckpt = hparams["ckpt_finder"].find_checkpoint()
hparams["pretrainer"].collect_files(ckpt.path)
hparams["pretrainer"].load_collected()
# Trainer initialization
asr_brain = LFMMIAM(
modules=hparams["modules"],
hparams=hparams,
run_opts=run_opts,
checkpointer=hparams["checkpointer"],
train_fsts = numfsts["train"],
)
# The `fit()` method iterates the training loop, calling the methods
# necessary to update the parameters of the model. Since all objects
# with changing state are managed by the Checkpointer, training can be
# stopped at any point, and will be resumed on next call.
asr_brain.fit(
asr_brain.hparams.epoch_counter,
datasets["train"],
datasets["valid"],
train_loader_kwargs = hparams["train_loader_kwargs"]
)
if "prior_file" in hparams:
kwargs = {}
if "test_max_key" in hparams:
kwargs["max_key"] = hparams["test_max_key"]
elif "test_min_key" in hparams:
kwargs["min_key"] = hparams["test_min_key"]
prior = asr_brain.estimate_prior_empirical(
datasets["train"],
loader_kwargs=hparams["prior_loader_kwargs"],
**kwargs
)
torch.save(prior, hparams["prior_file"])