-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain.py
402 lines (336 loc) · 14.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
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
#!/usr/bin/env/python3
"""
Recipe for "direct" (speech -> semantics) SLU with ASR-based transfer learning.
We encode input waveforms into features using a model trained on LibriSpeech,
then feed the features into a seq2seq model to map them to semantics.
(Adapted from the LibriSpeech seq2seq ASR recipe written by Ju-Chieh Chou, Mirco Ravanelli, Abdel Heba, and Peter Plantinga.)
Run using:
> python train.py hparams/train.yaml
Authors
* Loren Lugosch 2020
* Mirco Ravanelli 2020
"""
import sys
import torch
import speechbrain as sb
from hyperpyyaml import load_hyperpyyaml
from speechbrain.utils.distributed import run_on_main
import jsonlines
import ast
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
# Define training procedure
class SLU(sb.Brain):
def compute_forward(self, batch, stage):
"""Forward computations from the waveform batches to the output probabilities."""
batch = batch.to(self.device)
wavs, wav_lens = batch.sig
tokens_bos, tokens_bos_lens = batch.tokens_bos
wavs, wav_lens = wavs.to(self.device), wav_lens.to(self.device)
# Add augmentation if specified
# if stage == sb.Stage.TRAIN:
# if hasattr(self.hparams, "env_corrupt"):
# wavs_noise = self.hparams.env_corrupt(wavs, wav_lens)
# wavs = torch.cat([wavs, wavs_noise], dim=0)
# wav_lens = torch.cat([wav_lens, wav_lens])
# tokens_bos = torch.cat([tokens_bos, tokens_bos], dim=0)
# tokens_bos_lens = torch.cat([tokens_bos_lens, tokens_bos_lens])
# if hasattr(self.hparams, "augmentation"):
# wavs = self.hparams.augmentation(wavs, wav_lens)
# ASR encoder forward pass
# with torch.no_grad():
# ASR_encoder_out = self.hparams.asr_model.encode_batch(
# wavs.detach(), wav_lens
# )
# SLU forward pass
feats = self.hparams.compute_features(wavs)
# print(feats.size())
feats = self.hparams.normalize(feats, wav_lens)
# features = np.squeeze(features.numpy())
# plt.imshow(features)
# plt.savefig("a.png")
# print("feat:", feats.size())
encoder_out = self.hparams.enc(feats.detach())
# print("encoder_out:", encoder_out.size())
# encoder_out = self.hparams.slu_enc(encoder_out_0)
e_in = self.hparams.output_emb(tokens_bos)
# print(encoder_out.size())
h, _ = self.hparams.dec(e_in, encoder_out, wav_lens)
# print("decoder_out:", h.size())
# Output layer for seq2seq log-probabilities
logits = self.hparams.seq_lin(h)
# print("logits:", logits.size())
p_seq = self.hparams.log_softmax(logits)
# print("p_seq:", p_seq.size())
# Compute outputs
if (
stage == sb.Stage.TRAIN
and self.batch_count % show_results_every != 0
):
return p_seq, wav_lens
else:
p_tokens, scores = self.hparams.beam_searcher(encoder_out, wav_lens)
return p_seq, wav_lens, p_tokens
def compute_objectives(self, predictions, batch, stage):
"""Computes the loss (NLL) given predictions and targets."""
if (
stage == sb.Stage.TRAIN
and self.batch_count % show_results_every != 0
):
p_seq, wav_lens = predictions
else:
p_seq, wav_lens, predicted_tokens = predictions
ids = batch.id
tokens_eos, tokens_eos_lens = batch.tokens_eos
tokens, tokens_lens = batch.tokens
# if hasattr(self.hparams, "env_corrupt") and stage == sb.Stage.TRAIN:
# tokens_eos = torch.cat([tokens_eos, tokens_eos], dim=0)
# tokens_eos_lens = torch.cat(
# [tokens_eos_lens, tokens_eos_lens], dim=0
# )
loss_seq = self.hparams.seq_cost(
p_seq, tokens_eos, length=tokens_eos_lens
)
# (No ctc loss)
loss = loss_seq
if (stage != sb.Stage.TRAIN) or (
self.batch_count % show_results_every == 0
):
# Decode token terms to words
predicted_semantics = [
tokenizer.decode_ids(utt_seq).split(" ")
for utt_seq in predicted_tokens
]
target_semantics = [wrd.split(" ") for wrd in batch.semantics]
self.log_outputs(predicted_semantics, target_semantics)
if stage != sb.Stage.TRAIN:
self.wer_metric.append(
ids, predicted_semantics, target_semantics
)
self.cer_metric.append(
ids, predicted_semantics, target_semantics
)
if stage == sb.Stage.TEST:
# write to "predictions.jsonl"
with jsonlines.open(
hparams["output_folder"] + "/predictions.jsonl", mode="a"
) as writer:
for i in range(len(predicted_semantics)):
# print(predicted_semantics[i])
'''only city, no entity'''
# dict = {
# "scenario": "none",
# "action": "none",
# "entities": [],
# }
# listToStr = ' '.join(map(str, predicted_semantics[i]))
# dict['results'] = listToStr
''' entity'''
try:
dict = ast.literal_eval(
" ".join(predicted_semantics[i]).replace(
"|", ","
)
)
except SyntaxError: # need this if the output is not a valid dictionary
dict = {
"scenario": "none",
"action": "none",
"entities": [],
}
dict["file"] = id_to_file[ids[i]]
writer.write(dict)
return loss
def log_outputs(self, predicted_semantics, target_semantics):
""" TODO: log these to a file instead of stdout """
for i in range(len(target_semantics)):
# print(predicted_semantics[i])
# print(target_semantics[i])
print(" ".join(predicted_semantics[i]).replace("|", ","))
print(" ".join(target_semantics[i]).replace("|", ","))
print("")
def fit_batch(self, batch):
"""Train the parameters given a single batch in input"""
predictions = self.compute_forward(batch, sb.Stage.TRAIN)
loss = self.compute_objectives(predictions, batch, sb.Stage.TRAIN)
loss.backward()
if self.check_gradients(loss):
self.optimizer.step()
self.optimizer.zero_grad()
self.batch_count += 1
return loss.detach()
def evaluate_batch(self, batch, stage):
"""Computations needed for validation/test batches"""
predictions = self.compute_forward(batch, stage=stage)
loss = self.compute_objectives(predictions, batch, stage=stage)
return loss.detach()
def on_stage_start(self, stage, epoch):
"""Gets called at the beginning of each epoch"""
self.batch_count = 0
if stage != sb.Stage.TRAIN:
self.cer_metric = self.hparams.cer_computer()
self.wer_metric = self.hparams.error_rate_computer()
def on_stage_end(self, stage, stage_loss, epoch):
"""Gets called at the end of a epoch."""
# Compute/store important stats
stage_stats = {"loss": stage_loss}
if stage == sb.Stage.TRAIN:
self.train_stats = stage_stats
else:
stage_stats["CER"] = self.cer_metric.summarize("error_rate")
stage_stats["WER"] = self.wer_metric.summarize("error_rate")
# Perform end-of-iteration things, like annealing, logging, etc.
if stage == sb.Stage.VALID:
old_lr, new_lr = self.hparams.lr_annealing(stage_stats["WER"])
sb.nnet.schedulers.update_learning_rate(self.optimizer, new_lr)
self.hparams.train_logger.log_stats(
stats_meta={"epoch": epoch, "lr": old_lr},
train_stats=self.train_stats,
valid_stats=stage_stats,
)
self.checkpointer.save_and_keep_only(
meta={"WER": stage_stats["WER"]}, min_keys=["WER"],
)
elif stage == sb.Stage.TEST:
self.hparams.train_logger.log_stats(
stats_meta={"Epoch loaded": self.hparams.epoch_counter.current},
test_stats=stage_stats,
)
with open(self.hparams.wer_file, "w") as w:
self.wer_metric.write_stats(w)
def dataio_prepare(hparams):
"""This function prepares the datasets to be used in the brain class.
It also defines the data processing pipeline through user-defined functions."""
data_folder = hparams["data_folder"]
train_data = sb.dataio.dataset.DynamicItemDataset.from_csv(
csv_path=hparams["csv_train"], replacements={"data_root": data_folder},
)
if hparams["sorting"] == "ascending":
# we sort training data to speed up training and get better results.
train_data = train_data.filtered_sorted(sort_key="duration")
# when sorting do not shuffle in dataloader ! otherwise is pointless
hparams["dataloader_opts"]["shuffle"] = False
elif hparams["sorting"] == "descending":
train_data = train_data.filtered_sorted(
sort_key="duration", reverse=True
)
# when sorting do not shuffle in dataloader ! otherwise is pointless
hparams["dataloader_opts"]["shuffle"] = False
elif hparams["sorting"] == "random":
pass
else:
raise NotImplementedError(
"sorting must be random, ascending or descending"
)
valid_data = sb.dataio.dataset.DynamicItemDataset.from_csv(
csv_path=hparams["csv_valid"], replacements={"data_root": data_folder},
)
valid_data = valid_data.filtered_sorted(sort_key="duration")
test_data = sb.dataio.dataset.DynamicItemDataset.from_csv(
csv_path=hparams["csv_test"], replacements={"data_root": data_folder},
)
test_data = test_data.filtered_sorted(sort_key="duration")
datasets = [train_data, valid_data, test_data]
tokenizer = hparams["tokenizer"]
# 2. Define audio pipeline:
@sb.utils.data_pipeline.takes("wav")
@sb.utils.data_pipeline.provides("sig")
def audio_pipeline(wav):
# sig = sb.dataio.dataio.read_audio(wav)
filename = os.path.join(hparams["data_folder"], wav)
''' Read accnpy '''
signal = np.load(filename[:-4] + '.accnpy')
signal = signal[3,:]
signal = torch.from_numpy(signal).float().to('cpu')
# signal = signal.unsqueeze(0)
# signal = signal.unsqueeze(0)
return signal
sb.dataio.dataset.add_dynamic_item(datasets, audio_pipeline)
# 3. Define text pipeline:
@sb.utils.data_pipeline.takes("semantics")
@sb.utils.data_pipeline.provides(
"semantics", "token_list", "tokens_bos", "tokens_eos", "tokens"
)
def text_pipeline(semantics):
yield semantics
tokens_list = tokenizer.encode_as_ids(semantics)
# print(tokens_list)
# print(tokenizer.encode_as_pieces(semantics))
yield tokens_list
tokens_bos = torch.LongTensor([hparams["bos_index"]] + (tokens_list))
yield tokens_bos
tokens_eos = torch.LongTensor(tokens_list + [hparams["eos_index"]])
yield tokens_eos
tokens = torch.LongTensor(tokens_list)
yield tokens
sb.dataio.dataset.add_dynamic_item(datasets, text_pipeline)
# 4. Set output:
sb.dataio.dataset.set_output_keys(
datasets,
["id", "sig", "semantics", "tokens_bos", "tokens_eos", "tokens"],
)
return train_data, valid_data, test_data, tokenizer
if __name__ == "__main__":
# Load hyperparameters file with command-line overrides
hparams_file, run_opts, overrides = sb.parse_arguments(sys.argv[1:])
# hparams_file --> hparams file; run_opts --> command line
with open(hparams_file) as fin:
hparams = load_hyperpyyaml(fin, overrides)
show_results_every = 200 # plots results every N iterations
# If distributed_launch=True then
# create ddp_group with the right communication protocol
sb.utils.distributed.ddp_init_group(run_opts)
# Create experiment directory
sb.create_experiment_directory(
experiment_directory=hparams["output_folder"],
hyperparams_to_save=hparams_file,
overrides=overrides,
)
# Dataset prep (parsing SLURP)
from prepare import prepare_StealthyIMU # noqa
# multi-gpu (ddp) save data preparation
run_on_main(
prepare_StealthyIMU,
kwargs={
"data_folder": hparams["data_folder"],
"file_name": hparams["file_name"],
"save_folder": hparams["output_folder"],
"train_splits": hparams["train_splits"],
"slu_type": "direct",
"skip_prep": hparams["skip_prep"],
"seed": hparams["seed"],
},
)
# here we create the datasets objects as well as tokenization and encoding
(train_set, valid_set, test_set, tokenizer,) = dataio_prepare(hparams)
# We download and pretrain the tokenizer
run_on_main(hparams["pretrainer"].collect_files)
hparams["pretrainer"].load_collected(device=run_opts["device"])
# Brain class initialization
slu_brain = SLU(
modules=hparams["modules"],
opt_class=hparams["opt_class"],
hparams=hparams,
run_opts=run_opts,
checkpointer=hparams["checkpointer"],
)
# adding objects to trainer:
slu_brain.tokenizer = tokenizer
# Training
slu_brain.fit(
slu_brain.hparams.epoch_counter,
train_set,
valid_set,
train_loader_kwargs=hparams["dataloader_opts"],
valid_loader_kwargs=hparams["dataloader_opts"],
)
# Test
print("Creating id_to_file mapping...")
id_to_file = {}
df = pd.read_csv(hparams["csv_test"])
for i in range(len(df)):
id_to_file[str(df.ID[i])] = df.wav[i].split("/")[-1]
slu_brain.hparams.wer_file = hparams["output_folder"] + "/wer_test_real.txt"
slu_brain.evaluate(test_set, test_loader_kwargs=hparams["dataloader_opts"])