-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
687 lines (592 loc) · 24 KB
/
trainer.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
import os
import random
import warnings
from pathlib import Path
import numpy as np
import pandas as pd
import pytorch_lightning as pl
import sed_scores_eval
import torch
import torchmetrics
import yaml
from torchaudio.transforms import AmplitudeToDB, MelSpectrogram
from desed_task.data_augm import mixup
from desed_task.evaluation.evaluation_measures import (
compute_per_intersection_macro_f1,
compute_psds_from_operating_points,
compute_psds_from_scores,
)
from desed_task.utils.scaler import TorchScaler
from encoder import ManyHotEncoder
from model import CRNN
from utils import (
batched_decode_preds,
classes_labels,
log_sedeval_metrics,
)
class SED(pl.LightningModule):
"""Pytorch lightning module for the SED task.
Args:
hparams: dict, the dictionary to be used for the current experiment.
encoder: ManyHotEncoder object, object to encode and decode labels.
sed: torch.Module, the model to be trained.
opt: torch.optimizer.Optimizer object, the optimizer to be used.
train_data: torch.utils.data.Dataset subclass object, the training data to be used.
valid_data: torch.utils.data.Dataset subclass object, the validation data to be used.
test_data: torch.utils.data.Dataset subclass object, the test data to be used.
train_sampler: torch.utils.data.Sampler subclass object, the sampler to be used in the training dataloader.
scheduler: BaseScheduler subclass object, the scheduler to be used.
This is used to apply ramp-up during training for example.
fast_dev_run: bool, whether to launch a run with only one batch for each set, this is for development purpose,
to test the code runs.
"""
def __init__(
self,
hparams,
encoder,
sed,
opt=None,
train_data=None,
valid_data=None,
test_data=None,
train_sampler=None,
scheduler=None,
fast_dev_run=False,
evaluation=False,
):
super(SED, self).__init__()
self.hparams.update(hparams)
self.encoder = encoder
self.sed = sed
self.opt = opt
self.train_data = train_data
self.valid_data = valid_data
self.test_data = test_data
self.train_sampler = train_sampler
self.scheduler = scheduler
self.fast_dev_run = fast_dev_run
self.evaluation = evaluation
if self.fast_dev_run:
self.num_workers = 1
else:
self.num_workers = self.hparams["training"]["num_workers"]
feat_params = self.hparams["feats"]
self.mel_spec = MelSpectrogram(
sample_rate=feat_params["sample_rate"],
n_fft=feat_params["n_window"],
win_length=feat_params["n_window"],
hop_length=feat_params["hop_length"],
f_min=feat_params["f_min"],
f_max=feat_params["f_max"],
n_mels=feat_params["n_mels"],
window_fn=torch.hamming_window,
wkwargs={"periodic": False},
power=1,
center=False,
)
# * instantiating loss fn and scaler
self.loss_fn = torch.nn.BCELoss()
self.get_weak_f1_seg_macro = (
torchmetrics.classification.f_beta.MultilabelF1Score(
len(self.encoder.labels),
average="macro",
)
)
self.scaler = self._init_scaler()
# * buffer for event based scores which we compute using sed-eval
self.val_buffer_strong = {
k: pd.DataFrame() for k in self.hparams["training"]["val_thresholds"]
}
self.val_buffer_test = {
k: pd.DataFrame() for k in self.hparams["training"]["val_thresholds"]
}
self.val_scores_postprocessed_buffer_strong = {}
test_n_thresholds = self.hparams["training"]["n_test_thresholds"]
test_thresholds = np.arange(
1 / (test_n_thresholds * 2), 1, 1 / test_n_thresholds
)
self.test_psds_buffer = {k: pd.DataFrame() for k in test_thresholds}
self.decoded_05_buffer = pd.DataFrame()
self.test_scores_raw_buffer = {}
self.test_scores_postprocessed_buffer = {}
_exp_dir = None
@property
def exp_dir(self):
if self._exp_dir is None:
try:
self._exp_dir = self.logger.log_dir
if self._exp_dir is None:
self._exp_dir = self.hparams["log_dir"]
except Exception:
self._exp_dir = self.hparams["log_dir"]
return self._exp_dir
def lr_scheduler_step(self, scheduler, optimizer_idx, metric):
scheduler.step()
def on_train_start(self) -> None:
if not self.fast_dev_run:
to_ignore = [
".*Trying to infer the `batch_size` from an ambiguous collection.*",
".*invalid value encountered in divide*",
".*mean of empty slice*",
".*self.log*",
]
for message in to_ignore:
warnings.filterwarnings("ignore", message)
def _init_scaler(self):
"""Scaler inizialization function. It can be either a dataset or instance scaler.
Raises:
NotImplementedError: in case of not Implemented scaler
Returns:
TorchScaler: returns the scaler
"""
if self.hparams["scaler"]["statistic"] == "instance":
scaler = TorchScaler(
"instance",
self.hparams["scaler"]["normtype"],
self.hparams["scaler"]["dims"],
)
return scaler
elif self.hparams["scaler"]["statistic"] == "dataset":
scaler = TorchScaler(
"dataset",
self.hparams["scaler"]["normtype"],
self.hparams["scaler"]["dims"],
)
else:
raise NotImplementedError
if self.hparams["scaler"]["savepath"] is not None:
if os.path.exists(self.hparams["scaler"]["savepath"]):
scaler = torch.load(self.hparams["scaler"]["savepath"])
print(
"Loaded Scaler from previous checkpoint from {}".format(
self.hparams["scaler"]["savepath"]
)
)
return scaler
self.train_loader = self.train_dataloader()
scaler.fit(
self.train_loader,
transform_func=lambda x: self.take_log(self.mel_spec(x[0])),
)
if self.hparams["scaler"]["savepath"] is not None:
torch.save(scaler, self.hparams["scaler"]["savepath"])
print(
"Saving Scaler from previous checkpoint at {}".format(
self.hparams["scaler"]["savepath"]
)
)
return scaler
def take_log(self, mels):
"""Apply the log transformation to mel spectrograms.
Args:
mels: torch.Tensor, mel spectrograms for which to apply log.
Returns:
Tensor: logarithmic mel spectrogram of the mel spectrogram given as input
"""
amp_to_db = AmplitudeToDB(stype="amplitude")
amp_to_db.amin = 1e-5 # amin= 1e-5 as in librosa
return amp_to_db(mels).clamp(min=-50, max=80)
def training_step(self, batch, batch_indx):
"""Apply the training for one batch (a step). Used during trainer.fit
Args:
batch: torch.Tensor, batch input tensor
batch_indx: torch.Tensor, 1D tensor of indexes to know which data are present in each batch.
Returns:
torch.Tensor, the loss to take into account.
"""
indx_strong, indx_weak = self.hparams["training"]["batch_size"]
audio, labels, _ = batch
features = self.mel_spec(audio)
batch_num = features.shape[0]
# deriving masks for each dataset
strong_mask = torch.zeros(batch_num).to(features).bool()
weak_mask = torch.zeros(batch_num).to(features).bool()
strong_mask[:indx_strong] = 1
weak_mask[indx_strong : indx_weak + indx_strong] = 1
labels_weak = (torch.sum(labels[weak_mask], -1) > 0).float()
mixup_type = self.hparams["training"].get("mixup")
if mixup_type is not None and 0.5 > random.random():
features[weak_mask], labels_weak = mixup(
features[weak_mask], labels_weak, mixup_label_type=mixup_type
)
features[strong_mask], labels[strong_mask] = mixup(
features[strong_mask], labels[strong_mask], mixup_label_type=mixup_type
)
strong_preds, weak_preds = self.sed(self.scaler(self.take_log(features)))
loss_strong = self.loss_fn(strong_preds[strong_mask], labels[strong_mask])
# supervised loss on weakly labelled
loss_weak = self.loss_fn(weak_preds[weak_mask], labels_weak)
# total supervised loss
total_loss = loss_strong + loss_weak
self.log("train/loss_strong", loss_strong, prog_bar=True, sync_dist=True)
self.log("train/loss_weak", loss_weak, prog_bar=True, sync_dist=True)
self.log("train/total_loss", total_loss, prog_bar=True, sync_dist=True)
self.log(
"train/step",
self.scheduler["scheduler"].step_num,
prog_bar=True,
sync_dist=True,
)
self.log("train/lr", self.opt.param_groups[-1]["lr"], sync_dist=True)
return total_loss
def validation_step(self, batch, batch_indx):
"""Apply validation to a batch (step). Used during trainer.fit
Args:
batch: torch.Tensor, input batch tensor
batch_indx: torch.Tensor, 1D tensor of indexes to know which data are present in each batch.
Returns:
"""
audio, labels, _, filenames = batch
features = self.mel_spec(audio)
strong_preds, weak_preds = self.sed(self.scaler(self.take_log(features)))
weak_mask = (
torch.tensor(
[
str(Path(x).parent)
== str(Path(self.hparams["data"]["weak_folder"]))
for x in filenames
]
)
.to(audio)
.bool()
)
strong_mask = (
torch.tensor(
[
str(Path(x).parent)
== str(Path(self.hparams["data"]["synth_val_folder"]))
for x in filenames
]
)
.to(audio)
.bool()
)
if torch.any(weak_mask):
labels_weak = (torch.sum(labels[weak_mask], -1) >= 1).float()
loss_weak = self.loss_fn(weak_preds[weak_mask], labels_weak)
self.log("val/weak/loss_weak", loss_weak, prog_bar=True)
self.get_weak_f1_seg_macro(weak_preds[weak_mask], labels_weak)
if torch.any(strong_mask):
loss_strong = self.loss_fn(strong_preds[strong_mask], labels[strong_mask])
self.log("val/strong/loss_strong", loss_strong, prog_bar=True)
filenames_strong = [
x
for x in filenames
if Path(x).parent == Path(self.hparams["data"]["synth_val_folder"])
]
(
scores_raw_strong,
scores_postprocessed_strong,
decoded_strong,
) = batched_decode_preds(
strong_preds[strong_mask],
filenames_strong,
self.encoder,
median_filter=self.hparams["training"]["median_window"],
thresholds=list(self.val_buffer_strong.keys()),
)
self.val_scores_postprocessed_buffer_strong.update(
scores_postprocessed_strong
)
for th in self.val_buffer_strong.keys():
self.val_buffer_strong[th] = pd.concat(
[self.val_buffer_strong[th], decoded_strong[th]], ignore_index=True
)
# total supervised loss
if torch.any(strong_mask) and torch.any(weak_mask):
total_loss = loss_strong + loss_weak
self.log("val/total_loss", total_loss, prog_bar=True, sync_dist=True)
return
def validation_epoch_end(self, outputs):
"""Function applied at the end of all the validation steps of the epoch.
Args:
outputs: torch.Tensor, the concatenation of everything returned by validation_step.
Returns:
torch.Tensor, the objective metric to be used to choose the best model from for example.
"""
weak_f1_macro = self.get_weak_f1_seg_macro.compute()
# * strong val dataset
ground_truth = sed_scores_eval.io.read_ground_truth_events(
self.hparams["data"]["synth_val_tsv"]
)
audio_durations = sed_scores_eval.io.read_audio_durations(
self.hparams["data"]["synth_val_dur"]
)
if self.fast_dev_run:
ground_truth = {
audio_id: ground_truth[audio_id]
for audio_id in self.val_scores_postprocessed_buffer_strong
}
audio_durations = {
audio_id: audio_durations[audio_id]
for audio_id in self.val_scores_postprocessed_buffer_strong
}
else:
# * drop audios without events
ground_truth = {
audio_id: gt for audio_id, gt in ground_truth.items() if len(gt) > 0
}
audio_durations = {
audio_id: audio_durations[audio_id] for audio_id in ground_truth.keys()
}
psds1_sed_scores_eval = compute_psds_from_scores(
self.val_scores_postprocessed_buffer_strong,
ground_truth,
audio_durations,
dtc_threshold=0.7,
gtc_threshold=0.7,
cttc_threshold=None,
alpha_ct=0,
alpha_st=1,
# save_dir=os.path.join(save_dir, "", "scenario1"),
)
intersection_f1_macro = compute_per_intersection_macro_f1(
self.val_buffer_strong,
self.hparams["data"]["synth_val_tsv"],
self.hparams["data"]["synth_val_dur"],
)
sed_eval_metrics = log_sedeval_metrics(
self.val_buffer_strong[0.5],
self.hparams["data"]["synth_val_tsv"],
)
strong_event_macro = sed_eval_metrics[0]
strong_segment_macro = sed_eval_metrics[2]
obj_metric_strong_type = self.hparams["training"].get("obj_metric_strong_type")
if obj_metric_strong_type is None:
strong_metric = psds1_sed_scores_eval
elif obj_metric_strong_type == "event":
strong_metric = strong_event_macro
elif obj_metric_strong_type == "intersection":
strong_metric = intersection_f1_macro
elif obj_metric_strong_type == "psds":
strong_metric = psds1_sed_scores_eval
else:
raise NotImplementedError(
f"obj_metric_strong_type: {obj_metric_strong_type} not implemented."
)
obj_metric = torch.tensor(weak_f1_macro.item() + strong_metric)
self.log("val/obj_metric", obj_metric, prog_bar=True, sync_dist=True)
self.log("val/weak/macro_F1", weak_f1_macro, prog_bar=True)
self.log(
"val/strong/psds1_sed_scores_eval",
psds1_sed_scores_eval,
prog_bar=True,
sync_dist=True,
)
self.log(
"val/strong/intersection_f1_macro",
intersection_f1_macro,
prog_bar=True,
sync_dist=True,
)
self.log(
"val/strong/event_f1_macro",
strong_event_macro,
prog_bar=True,
sync_dist=True,
)
self.log(
"val/strong/segment_f1_macro",
strong_segment_macro,
prog_bar=True,
sync_dist=True,
)
# * free the buffers
self.val_buffer_strong = {
k: pd.DataFrame() for k in self.hparams["training"]["val_thresholds"]
}
self.val_scores_postprocessed_buffer_strong = {}
self.get_weak_f1_seg_macro.reset()
return obj_metric
def on_save_checkpoint(self, checkpoint):
checkpoint["sed"] = self.sed.state_dict()
return checkpoint
def test_step(self, batch, batch_indx):
"""Apply Test to a batch (step), used only when (trainer.test is called)
Args:
batch: torch.Tensor, input batch tensor
batch_indx: torch.Tensor, 1D tensor of indexes to know which data are present in each batch.
Returns:
"""
audio, labels, _, filenames = batch
features = self.mel_spec(audio)
preds, _ = self.sed(self.scaler(self.take_log(features)))
if not self.evaluation:
loss = self.loss_fn(preds, labels)
self.log("test/loss", loss)
# * compute psds (Polyphonic Sound Detection Score)
(
scores_raw_strong,
scores_postprocessed_strong,
decoded_strong,
) = batched_decode_preds(
preds,
filenames,
self.encoder,
median_filter=self.hparams["training"]["median_window"],
thresholds=list(self.test_psds_buffer.keys()) + [0.5],
)
self.test_scores_raw_buffer.update(scores_raw_strong)
self.test_scores_postprocessed_buffer.update(scores_postprocessed_strong)
for th in self.test_psds_buffer.keys():
self.test_psds_buffer[th] = pd.concat(
[self.test_psds_buffer[th], decoded_strong[th]], ignore_index=True
)
self.decoded_05_buffer = pd.concat(
[self.decoded_05_buffer, decoded_strong[0.5]]
)
def on_test_epoch_end(self):
save_dir = os.path.join(self.exp_dir, "metrics_test")
# * if evaluation is True, we only save the scores
if self.evaluation:
save_dir_raw = os.path.join(save_dir, "_scores", "raw")
sed_scores_eval.io.write_sed_scores(
self.test_scores_raw_buffer, save_dir_raw
)
print(f"\nRaw scores for saved in: {save_dir_raw}")
save_dir_postprocessed = os.path.join(save_dir, "_scores", "postprocessed")
sed_scores_eval.io.write_sed_scores(
self.test_scores_postprocessed_buffer, save_dir_postprocessed
)
print(f"\nPostprocessed scores for saved in: {save_dir_postprocessed}")
else:
# * calculate the metrics and save them
ground_truth = sed_scores_eval.io.read_ground_truth_events(
self.hparams["data"]["test_tsv"]
)
audio_durations = sed_scores_eval.io.read_audio_durations(
self.hparams["data"]["test_dur"]
)
if self.fast_dev_run:
ground_truth = {
audio_id: ground_truth[audio_id]
for audio_id in self.test_scores_postprocessed_buffer
}
audio_durations = {
audio_id: audio_durations[audio_id]
for audio_id in self.test_scores_postprocessed_buffer
}
else:
# drop audios without events
ground_truth = {
audio_id: gt for audio_id, gt in ground_truth.items() if len(gt) > 0
}
audio_durations = {
audio_id: audio_durations[audio_id]
for audio_id in ground_truth.keys()
}
psds1_psds_eval = compute_psds_from_operating_points(
self.test_psds_buffer,
self.hparams["data"]["test_tsv"],
self.hparams["data"]["test_dur"],
dtc_threshold=0.7,
gtc_threshold=0.7,
alpha_ct=0,
alpha_st=1,
save_dir=os.path.join(save_dir, "", "scenario1"),
)
psds1_sed_scores_eval = compute_psds_from_scores(
self.test_scores_postprocessed_buffer,
ground_truth,
audio_durations,
dtc_threshold=0.7,
gtc_threshold=0.7,
cttc_threshold=None,
alpha_ct=0,
alpha_st=1,
save_dir=os.path.join(save_dir, "", "scenario1"),
)
psds2_psds_eval = compute_psds_from_operating_points(
self.test_psds_buffer,
self.hparams["data"]["test_tsv"],
self.hparams["data"]["test_dur"],
dtc_threshold=0.1,
gtc_threshold=0.1,
cttc_threshold=0.3,
alpha_ct=0.5,
alpha_st=1,
save_dir=os.path.join(save_dir, "", "scenario2"),
)
psds2_sed_scores_eval = compute_psds_from_scores(
self.test_scores_postprocessed_buffer,
ground_truth,
audio_durations,
dtc_threshold=0.1,
gtc_threshold=0.1,
cttc_threshold=0.3,
alpha_ct=0.5,
alpha_st=1,
save_dir=os.path.join(save_dir, "", "scenario2"),
)
sed_eval_metrics = log_sedeval_metrics(
self.decoded_05_buffer,
self.hparams["data"]["test_tsv"],
os.path.join(save_dir, ""),
)
event_macro = sed_eval_metrics[0]
segment_macro = sed_eval_metrics[2]
# strong dataset
intersection_f1_macro = compute_per_intersection_macro_f1(
{"0.5": self.decoded_05_buffer},
self.hparams["data"]["test_tsv"],
self.hparams["data"]["test_dur"],
)
results = {
"test/psds1_psds_eval": psds1_psds_eval,
"test/psds1_sed_scores_eval": psds1_sed_scores_eval,
"test/psds2_psds_eval": psds2_psds_eval,
"test/psds2_sed_scores_eval": psds2_sed_scores_eval,
"test/segment_f1_macro": segment_macro,
"test/event_f1_macro": event_macro,
"test/intersection_f1_macro": intersection_f1_macro,
}
if self.logger is not None:
self.logger.log_metrics(results)
self.logger.log_hyperparams(self.hparams, results)
for key in results.keys():
self.log(key, results[key], prog_bar=True, logger=True, sync_dist=True)
def configure_optimizers(self):
return [self.opt], [self.scheduler]
def train_dataloader(self):
self.train_loader = torch.utils.data.DataLoader(
self.train_data,
batch_sampler=self.train_sampler,
num_workers=self.num_workers,
)
return self.train_loader
def val_dataloader(self):
self.val_loader = torch.utils.data.DataLoader(
self.valid_data,
batch_size=self.hparams["training"]["batch_size_val"],
num_workers=self.num_workers,
shuffle=False,
drop_last=False,
)
return self.val_loader
def test_dataloader(self):
self.test_loader = torch.utils.data.DataLoader(
self.test_data,
batch_size=self.hparams["training"]["batch_size_val"],
num_workers=self.num_workers,
shuffle=False,
drop_last=False,
)
return self.test_loader
def forward(self, x):
features = self.mel_spec(x)
features = features.unsqueeze(0)
preds, _ = self.sed(self.scaler(self.take_log(features)))
return preds
if __name__ == "__main__":
with open("params.yaml", "r") as f:
config = yaml.safe_load(f)
encoder = ManyHotEncoder(
list(classes_labels.keys()),
audio_len=config["data"]["audio_max_len"],
frame_len=config["feats"]["n_filters"],
frame_hop=config["feats"]["hop_length"],
net_pooling=config["data"]["net_subsample"],
fs=config["data"]["fs"],
)
sed = SED(config, encoder=encoder, sed=CRNN(**config["net"]))
print(sed.state_dict().keys())