-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurbansound8k_train.py
466 lines (398 loc) · 17.6 KB
/
urbansound8k_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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/python3
"""Recipe for training sound class embeddings (e.g, xvectors) using the UrbanSound8k.
We employ an encoder followed by a sound classifier.
To run this recipe, use the following command:
> python train_class_embeddings.py {hyperparameter_file}
Using your own hyperparameter file or one of the following:
hparams/train_x_vectors.yaml (for standard xvectors)
hparams/train_ecapa_tdnn.yaml (for the ecapa+tdnn system)
Authors
* David Whipps 2021
* Ala Eddine Limame 2021
Based on VoxCeleb By:
* Mirco Ravanelli 2020
* Hwidong Na 2020
* Nauman Dawalatabad 2020
"""
import os
import sys
import torch
import torchaudio
import speechbrain as sb
from hyperpyyaml import load_hyperpyyaml
from speechbrain.utils.distributed import run_on_main
from stats import AttentionStats
from urbansound8k_prepare import prepare_urban_sound_8k
from sklearn.metrics import confusion_matrix
import numpy as np
from utils import create_cm_fig, configure_seed, get_stats_continuous_attn, get_stats_discrete_attn
class UrbanSound8kBrain(sb.core.Brain):
"""Class for sound class embedding training"
"""
def compute_forward(self, batch, stage):
"""Computation pipeline based on a encoder + sound classifier.
Data augmentation and environmental corruption are applied to the
input sound.
"""
batch = batch.to(self.device)
wavs, lens = batch.sig
if stage == sb.Stage.TRAIN:
# Applying the augmentation pipeline
wavs_aug_tot = []
wavs_aug_tot.append(wavs)
for count, augment in enumerate(self.hparams.augment_pipeline):
# Apply augment
wavs_aug = augment(wavs, lens)
# Managing speed change
if wavs_aug.shape[1] > wavs.shape[1]:
wavs_aug = wavs_aug[:, 0 : wavs.shape[1]]
else:
zero_sig = torch.zeros_like(wavs)
zero_sig[:, 0 : wavs_aug.shape[1]] = wavs_aug
wavs_aug = zero_sig
if self.hparams.concat_augment:
wavs_aug_tot.append(wavs_aug)
else:
wavs = wavs_aug
wavs_aug_tot[0] = wavs
wavs = torch.cat(wavs_aug_tot, dim=0)
self.n_augment = len(wavs_aug_tot)
lens = torch.cat([lens] * self.n_augment)
# Feature extraction and normalization
feats = self.modules.compute_features(wavs)
if self.hparams.amp_to_db:
Amp2db = torchaudio.transforms.AmplitudeToDB(
stype="power", top_db=80
) # try "magnitude" Vs "power"? db= 80, 50...
feats = Amp2db(feats)
# Normalization
if self.hparams.normalize:
feats = self.modules.mean_var_norm(feats, lens)
# Embeddings + sound classifier
embeddings = self.modules.embedding_model(feats, lens)
outputs = self.modules.classifier(embeddings)
return outputs, lens
def compute_objectives(self, predictions, batch, stage):
"""Computes the loss using class-id as label.
"""
predictions, lens = predictions
uttid = batch.id
classid, _ = batch.class_string_encoded
# Concatenate labels (due to data augmentation)
if stage == sb.Stage.TRAIN:
classid = torch.cat([classid] * self.n_augment, dim=0)
loss = self.hparams.compute_cost(predictions, classid, lens)
if hasattr(self.hparams.lr_annealing, "on_batch_end"):
self.hparams.lr_annealing.on_batch_end(self.optimizer)
# Append this batch of losses to the loss metric for easy
self.loss_metric.append(
uttid, predictions, classid, lens, reduction="batch"
)
# Confusion matrices
if stage != sb.Stage.TRAIN:
y_true = classid.cpu().detach().numpy().squeeze(-1)
y_pred = predictions.cpu().detach().numpy().argmax(-1).squeeze(-1)
if stage == sb.Stage.VALID:
confusion_matix = confusion_matrix(
y_true,
y_pred,
labels=sorted(self.hparams.label_encoder.ind2lab.keys()),
)
self.valid_confusion_matrix += confusion_matix
if stage == sb.Stage.TEST:
confusion_matix = confusion_matrix(
y_true,
y_pred,
labels=sorted(self.hparams.label_encoder.ind2lab.keys()),
)
self.test_confusion_matrix += confusion_matix
# Compute Accuracy using MetricStats
self.acc_metric.append(
uttid, predict=predictions, target=classid, lengths=lens
)
# Attn stats
wavs, _ = batch.sig
feats = self.modules.compute_features(wavs.to(predictions.device))
batch_lens = (lens * feats.shape[1]).long()
if hasattr(self.modules.embedding_model, 'asp'):
if hasattr(self.modules.embedding_model.asp, 'cont_attn'):
attn_stats = get_stats_continuous_attn(self.modules.embedding_model.asp.cont_attn)
else:
attn_stats = get_stats_discrete_attn(self.modules.embedding_model.asp, batch_lens)
else:
if hasattr(self.modules.embedding_model.attn, 'cont_max_activation'): # continuous case
attn_stats = get_stats_continuous_attn(self.modules.embedding_model.attn)
else:
attn_stats = get_stats_discrete_attn(self.modules.embedding_model.attn, batch_lens)
self.attn_stats.append(uttid, attn_stats=attn_stats)
if stage != sb.Stage.TRAIN:
self.error_metrics.append(uttid, predictions, classid, lens)
return loss
def on_stage_start(self, stage, epoch=None):
"""Gets called at the beginning of each epoch.
Arguments
---------
stage : sb.Stage
One of sb.Stage.TRAIN, sb.Stage.VALID, or sb.Stage.TEST.
epoch : int
The currently-starting epoch. This is passed
`None` during the test stage.
"""
# Set up statistics trackers for this stage
self.loss_metric = sb.utils.metric_stats.MetricStats(
metric=sb.nnet.losses.nll_loss # TODO put in yaml hparams?
)
# Compute Accuracy using MetricStats
# Define function taking (prediction, target, length) for eval
def accuracy_value(predict, target, lengths):
"""Computes Accuracy"""
nbr_correct, nbr_total = sb.utils.Accuracy.Accuracy(
predict, target, lengths
)
acc = torch.tensor([nbr_correct / nbr_total])
return acc
self.acc_metric = sb.utils.metric_stats.MetricStats(
metric=accuracy_value, n_jobs=1
)
self.attn_stats = AttentionStats(metric=None, n_jobs=1)
# Confusion matrices
if stage == sb.Stage.VALID:
self.valid_confusion_matrix = np.zeros(
shape=(self.hparams.out_n_neurons, self.hparams.out_n_neurons),
dtype=int,
)
if stage == sb.Stage.TEST:
self.test_confusion_matrix = np.zeros(
shape=(self.hparams.out_n_neurons, self.hparams.out_n_neurons),
dtype=int,
)
# Set up evaluation-only statistics trackers
if stage != sb.Stage.TRAIN:
self.error_metrics = self.hparams.error_stats()
def on_stage_end(self, stage, stage_loss, epoch=None):
"""Gets called at the end of an epoch.
Arguments
---------
stage : sb.Stage
One of sb.Stage.TRAIN, sb.Stage.VALID, sb.Stage.TEST
stage_loss : float
The average loss for all of the data processed in this stage.
epoch : int
The currently-starting epoch. This is passed
`None` during the test stage.
"""
# Compute/store important stats
if stage == sb.Stage.TRAIN:
self.train_loss = stage_loss
self.train_stats = {
"loss": self.train_loss,
"acc": self.acc_metric.summarize("average"), # "acc": self.train_acc_metric.summarize(),
**self.attn_stats.summarize(),
}
# Summarize Valid statistics from the stage for record-keeping.
elif stage == sb.Stage.VALID:
valid_stats = {
"loss": stage_loss,
"acc": self.acc_metric.summarize("average"), # "acc": self.valid_acc_metric.summarize(),
"error": self.error_metrics.summarize("average"),
**self.attn_stats.summarize(),
}
# Summarize Test statistics from the stage for record-keeping.
else:
test_stats = {
"loss": stage_loss,
"acc": self.acc_metric.summarize("average"), # "acc": self.test_acc_metric.summarize(),
"error": self.error_metrics.summarize("average"),
**self.attn_stats.summarize(),
}
# Perform end-of-iteration things, like annealing, logging, etc.
if stage == sb.Stage.VALID:
old_lr, new_lr = self.hparams.lr_annealing(epoch)
sb.nnet.schedulers.update_learning_rate(self.optimizer, new_lr)
# Tensorboard logging
if self.hparams.use_tensorboard:
self.hparams.tensorboard_train_logger.log_stats(
stats_meta={"Epoch": epoch},
train_stats=self.train_stats,
valid_stats=valid_stats,
)
# Log confusion matrix fig to tensorboard
cm_fig = create_cm_fig(
self.valid_confusion_matrix,
display_labels=list(
self.hparams.label_encoder.ind2lab.values()
),
)
self.hparams.tensorboard_train_logger.writer.add_figure(
"Validation Confusion Matrix", cm_fig, epoch
) # TODO use global_step from writer
# Per class accuracy from Validation confusion matrix
per_class_acc_arr = np.diag(self.valid_confusion_matrix) / np.sum(
self.valid_confusion_matrix, axis=1
)
per_class_acc_arr_str = "\n" + "\n".join(
"{:}: {:.3f}".format(
self.hparams.label_encoder.decode_ndim(class_id), class_acc
)
for class_id, class_acc in enumerate(per_class_acc_arr)
)
# The train_logger writes a summary to stdout and to the logfile.
self.hparams.train_logger.log_stats(
stats_meta={"epoch": epoch, "lr": old_lr},
train_stats=self.train_stats,
valid_stats=valid_stats,
)
# Save the current checkpoint and delete previous checkpoints,
self.checkpointer.save_and_keep_only(
meta=valid_stats, min_keys=["error"]
)
# We also write statistics about test data to stdout and to the logfile.
if stage == sb.Stage.TEST:
# Per class accuracy from Test confusion matrix
per_class_acc_arr = np.diag(self.test_confusion_matrix) / np.sum(
self.test_confusion_matrix, axis=1
)
per_class_acc_arr_str = "\n" + "\n".join(
"{:}: {:.3f}".format(class_id, class_acc)
for class_id, class_acc in enumerate(per_class_acc_arr)
)
self.hparams.train_logger.log_stats(
{
"Epoch loaded": self.hparams.epoch_counter.current,
"\n Per Class Accuracy": per_class_acc_arr_str,
"\n Confusion Matrix": "\n{:}\n".format(
self.test_confusion_matrix
),
},
test_stats=test_stats,
)
def dataio_prep(hparams):
"Creates the datasets and their data processing pipelines."
data_audio_folder = hparams["audio_data_folder"]
config_sample_rate = hparams["sample_rate"]
label_encoder = sb.dataio.encoder.CategoricalEncoder()
# TODO use SB implementation but need to make sure it give the same results as PyTorch
# resampler = sb.processing.speech_augmentation.Resample(orig_freq=latest_file_sr, new_freq=config_sample_rate)
hparams["resampler"] = torchaudio.transforms.Resample(
new_freq=config_sample_rate
)
# 2. Define audio pipeline:
@sb.utils.data_pipeline.takes("wav", "fold")
@sb.utils.data_pipeline.provides("sig")
def audio_pipeline(wav, fold):
"""Load the signal, and pass it and its length to the corruption class.
This is done on the CPU in the `collate_fn`."""
wave_file = data_audio_folder + "/fold{:}/{:}".format(fold, wav)
sig, read_sr = torchaudio.load(wave_file)
# If multi-channels, downmix it to a mono channel
sig = torch.squeeze(sig)
if len(sig.shape) > 1:
sig = torch.mean(sig, dim=0)
# Convert sample rate to required config_sample_rate
if read_sr != config_sample_rate:
# Re-initialize sampler if source file sample rate changed compared to last file
if read_sr != hparams["resampler"].orig_freq:
hparams["resampler"] = torchaudio.transforms.Resample(
orig_freq=read_sr, new_freq=config_sample_rate
)
# Resample audio
sig = hparams["resampler"].forward(sig)
return sig
# 3. Define label pipeline:
@sb.utils.data_pipeline.takes("class_string")
@sb.utils.data_pipeline.provides("class_string", "class_string_encoded")
def label_pipeline(class_string):
yield class_string
class_string_encoded = label_encoder.encode_label_torch(class_string)
yield class_string_encoded
# Define datasets. We also connect the dataset with the data processing
# functions defined above.
datasets = {}
for dataset in ["train", "valid", "test"]:
datasets[dataset] = sb.dataio.dataset.DynamicItemDataset.froxm_json(
json_path=hparams[f"{dataset}_annotation"],
replacements={"data_root": hparams["data_folder"]},
dynamic_items=[audio_pipeline, label_pipeline],
output_keys=["id", "sig", "class_string_encoded"],
)
# Load or compute the label encoder (with multi-GPU DDP support)
# Please, take a look into the lab_enc_file to see the label to index
# mappinng.
lab_enc_file = os.path.join(hparams["save_folder"], "label_encoder.txt")
label_encoder.load_or_create(
path=lab_enc_file,
from_didatasets=[datasets["train"]],
output_key="class_string",
)
return datasets, label_encoder
if __name__ == "__main__":
# This flag enables the inbuilt cudnn auto-tuner
torch.backends.cudnn.benchmark = True
# CLI:
hparams_file, run_opts, overrides = sb.parse_arguments(sys.argv[1:])
# Initialize ddp (useful only for multi-GPU DDP training)
sb.utils.distributed.ddp_init_group(run_opts)
# Load hyperparameters file with command-line overrides
with open(hparams_file) as fin:
hparams = load_hyperpyyaml(fin, overrides)
# Configure seed for everything
configure_seed(hparams["seed"])
# Create experiment directory
sb.create_experiment_directory(
experiment_directory=hparams["output_folder"],
hyperparams_to_save=hparams_file,
overrides=overrides,
)
# Tensorboard logging
if hparams["use_tensorboard"]:
from speechbrain.utils.train_logger import TensorboardLogger
hparams["tensorboard_train_logger"] = TensorboardLogger(
hparams["tensorboard_logs_folder"]
)
run_on_main(
prepare_urban_sound_8k,
kwargs={
"data_folder": hparams["data_folder"],
"audio_data_folder": hparams["audio_data_folder"],
"save_json_train": hparams["train_annotation"],
"save_json_valid": hparams["valid_annotation"],
"save_json_test": hparams["test_annotation"],
"train_fold_nums": hparams["train_fold_nums"],
"valid_fold_nums": hparams["valid_fold_nums"],
"test_fold_nums": hparams["test_fold_nums"],
"skip_manifest_creation": hparams["skip_manifest_creation"],
},
)
# Dataset IO prep: creating Dataset objects and proper encodings for phones
datasets, label_encoder = dataio_prep(hparams)
hparams["label_encoder"] = label_encoder
class_labels = list(label_encoder.ind2lab.values())
print("Class Labels:", class_labels)
urban_sound_8k_brain = UrbanSound8kBrain(
modules=hparams["modules"],
opt_class=hparams["opt_class"],
hparams=hparams,
run_opts=run_opts,
checkpointer=hparams["checkpointer"],
)
print("Modules:")
for module in urban_sound_8k_brain.modules.values():
print(module)
# 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.
urban_sound_8k_brain.fit(
epoch_counter=urban_sound_8k_brain.hparams.epoch_counter,
train_set=datasets["train"],
valid_set=datasets["valid"],
train_loader_kwargs=hparams["dataloader_options"],
valid_loader_kwargs=hparams["dataloader_options"],
)
# Load the best checkpoint for evaluation
test_stats = urban_sound_8k_brain.evaluate(
test_set=datasets["test"],
min_key="error",
progressbar=True,
test_loader_kwargs=hparams["dataloader_options"],
)