forked from CPJKU/dcase2024_task1_baseline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_training.py
514 lines (430 loc) · 22.1 KB
/
run_training.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
import pytorch_lightning as pl
from pytorch_lightning.loggers import WandbLogger
import torch
import torchaudio
from torch.utils.data import DataLoader
import argparse
import torch.nn.functional as F
import transformers
import wandb
import json
from dataset.dcase24 import get_training_set, get_test_set, get_eval_set
from helpers.init import worker_init_fn
from models.baseline import get_model
from helpers.utils import mixstyle
from helpers import nessi
class PLModule(pl.LightningModule):
def __init__(self, config):
super().__init__()
self.config = config # results from argparse, contains all configurations for our experiment
# module for resampling waveforms on the fly
resample = torchaudio.transforms.Resample(
orig_freq=self.config.orig_sample_rate,
new_freq=self.config.sample_rate
)
# module to preprocess waveforms into log mel spectrograms
mel = torchaudio.transforms.MelSpectrogram(
sample_rate=config.sample_rate,
n_fft=config.n_fft,
win_length=config.window_length,
hop_length=config.hop_length,
n_mels=config.n_mels,
f_min=config.f_min,
f_max=config.f_max
)
freqm = torchaudio.transforms.FrequencyMasking(config.freqm, iid_masks=True)
timem = torchaudio.transforms.TimeMasking(config.timem, iid_masks=True)
self.mel = torch.nn.Sequential(
resample,
mel
)
self.mel_augment = torch.nn.Sequential(
freqm,
timem
)
# the baseline model
self.model = get_model(n_classes=config.n_classes,
in_channels=config.in_channels,
base_channels=config.base_channels,
channels_multiplier=config.channels_multiplier,
expansion_rate=config.expansion_rate
)
self.device_ids = ['a', 'b', 'c', 's1', 's2', 's3', 's4', 's5', 's6']
self.label_ids = ['airport', 'bus', 'metro', 'metro_station', 'park', 'public_square', 'shopping_mall',
'street_pedestrian', 'street_traffic', 'tram']
# categorization of devices into 'real', 'seen' and 'unseen'
self.device_groups = {'a': "real", 'b': "real", 'c': "real",
's1': "seen", 's2': "seen", 's3': "seen",
's4': "unseen", 's5': "unseen", 's6': "unseen"}
# pl 2 containers:
self.training_step_outputs = []
self.validation_step_outputs = []
self.test_step_outputs = []
def mel_forward(self, x):
"""
:param x: batch of raw audio signals (waveforms)
:return: log mel spectrogram
"""
x = self.mel(x)
if self.training:
x = self.mel_augment(x)
x = (x + 1e-5).log()
return x
def forward(self, x):
"""
:param x: batch of raw audio signals (waveforms)
:return: final model predictions
"""
x = self.mel_forward(x)
x = self.model(x)
return x
def configure_optimizers(self):
"""
This is the way pytorch lightening requires optimizers and learning rate schedulers to be defined.
The specified items are used automatically in the optimization loop (no need to call optimizer.step() yourself).
:return: optimizer and learning rate scheduler
"""
optimizer = torch.optim.AdamW(self.parameters(), lr=self.config.lr, weight_decay=self.config.weight_decay)
scheduler = transformers.get_cosine_schedule_with_warmup(
optimizer,
num_warmup_steps=self.config.warmup_steps,
num_training_steps=self.trainer.estimated_stepping_batches,
)
lr_scheduler_config = {
"scheduler": scheduler,
"interval": "step",
"frequency": 1
}
return [optimizer], [lr_scheduler_config]
def training_step(self, train_batch, batch_idx):
"""
:param train_batch: contains one batch from train dataloader
:param batch_idx
:return: loss to update model parameters
"""
x, files, labels, devices, cities = train_batch
x = self.mel_forward(x) # we convert the raw audio signals into log mel spectrograms
if self.config.mixstyle_p > 0:
# frequency mixstyle
x = mixstyle(x, self.config.mixstyle_p, self.config.mixstyle_alpha)
y_hat = self.model(x)
samples_loss = F.cross_entropy(y_hat, labels, reduction="none")
loss = samples_loss.mean()
self.log("lr", self.trainer.optimizers[0].param_groups[0]['lr'])
self.log("epoch", self.current_epoch)
self.log("train/loss", loss.detach().cpu())
return loss
def on_train_epoch_end(self):
pass
def validation_step(self, val_batch, batch_idx):
x, files, labels, devices, cities = val_batch
y_hat = self.forward(x)
samples_loss = F.cross_entropy(y_hat, labels, reduction="none")
# for computing accuracy
_, preds = torch.max(y_hat, dim=1)
n_correct_per_sample = (preds == labels)
n_correct = n_correct_per_sample.sum()
dev_names = [d.rsplit("-", 1)[1][:-4] for d in files]
results = {'loss': samples_loss.mean(), "n_correct": n_correct,
"n_pred": torch.as_tensor(len(labels), device=self.device)}
# log metric per device and scene
for d in self.device_ids:
results["devloss." + d] = torch.as_tensor(0., device=self.device)
results["devcnt." + d] = torch.as_tensor(0., device=self.device)
results["devn_correct." + d] = torch.as_tensor(0., device=self.device)
for i, d in enumerate(dev_names):
results["devloss." + d] = results["devloss." + d] + samples_loss[i]
results["devn_correct." + d] = results["devn_correct." + d] + n_correct_per_sample[i]
results["devcnt." + d] = results["devcnt." + d] + 1
for l in self.label_ids:
results["lblloss." + l] = torch.as_tensor(0., device=self.device)
results["lblcnt." + l] = torch.as_tensor(0., device=self.device)
results["lbln_correct." + l] = torch.as_tensor(0., device=self.device)
for i, l in enumerate(labels):
results["lblloss." + self.label_ids[l]] = results["lblloss." + self.label_ids[l]] + samples_loss[i]
results["lbln_correct." + self.label_ids[l]] = \
results["lbln_correct." + self.label_ids[l]] + n_correct_per_sample[i]
results["lblcnt." + self.label_ids[l]] = results["lblcnt." + self.label_ids[l]] + 1
results = {k: v.cpu() for k, v in results.items()}
self.validation_step_outputs.append(results)
def on_validation_epoch_end(self):
# convert a list of dicts to a flattened dict
outputs = {k: [] for k in self.validation_step_outputs[0]}
for step_output in self.validation_step_outputs:
for k in step_output:
outputs[k].append(step_output[k])
for k in outputs:
outputs[k] = torch.stack(outputs[k])
avg_loss = outputs['loss'].mean()
acc = sum(outputs['n_correct']) * 1.0 / sum(outputs['n_pred'])
logs = {'acc': acc, 'loss': avg_loss}
# log metric per device and scene
for d in self.device_ids:
dev_loss = outputs["devloss." + d].sum()
dev_cnt = outputs["devcnt." + d].sum()
dev_corrct = outputs["devn_correct." + d].sum()
logs["loss." + d] = dev_loss / dev_cnt
logs["acc." + d] = dev_corrct / dev_cnt
logs["cnt." + d] = dev_cnt
# device groups
logs["acc." + self.device_groups[d]] = logs.get("acc." + self.device_groups[d], 0.) + dev_corrct
logs["count." + self.device_groups[d]] = logs.get("count." + self.device_groups[d], 0.) + dev_cnt
logs["lloss." + self.device_groups[d]] = logs.get("lloss." + self.device_groups[d], 0.) + dev_loss
for d in set(self.device_groups.values()):
logs["acc." + d] = logs["acc." + d] / logs["count." + d]
logs["lloss." + d] = logs["lloss." + d] / logs["count." + d]
for l in self.label_ids:
lbl_loss = outputs["lblloss." + l].sum()
lbl_cnt = outputs["lblcnt." + l].sum()
lbl_corrct = outputs["lbln_correct." + l].sum()
logs["loss." + l] = lbl_loss / lbl_cnt
logs["acc." + l] = lbl_corrct / lbl_cnt
logs["cnt." + l] = lbl_cnt
logs["macro_avg_acc"] = torch.mean(torch.stack([logs["acc." + l] for l in self.label_ids]))
# prefix with 'val' for logging
self.log_dict({"val/" + k: logs[k] for k in logs})
self.validation_step_outputs.clear()
def test_step(self, test_batch, batch_idx):
x, files, labels, devices, cities = test_batch
# maximum memory allowance for parameters: 128 KB
# baseline has 61148 parameters -> we can afford 16-bit precision
# since 61148 * 16 bit ~ 122 kB
# assure fp16
self.model.half()
x = self.mel_forward(x)
x = x.half()
y_hat = self.model(x)
samples_loss = F.cross_entropy(y_hat, labels, reduction="none")
# for computing accuracy
_, preds = torch.max(y_hat, dim=1)
n_correct_per_sample = (preds == labels)
n_correct = n_correct_per_sample.sum()
dev_names = [d.rsplit("-", 1)[1][:-4] for d in files]
results = {'loss': samples_loss.mean(), "n_correct": n_correct,
"n_pred": torch.as_tensor(len(labels), device=self.device)}
# log metric per device and scene
for d in self.device_ids:
results["devloss." + d] = torch.as_tensor(0., device=self.device)
results["devcnt." + d] = torch.as_tensor(0., device=self.device)
results["devn_correct." + d] = torch.as_tensor(0., device=self.device)
for i, d in enumerate(dev_names):
results["devloss." + d] = results["devloss." + d] + samples_loss[i]
results["devn_correct." + d] = results["devn_correct." + d] + n_correct_per_sample[i]
results["devcnt." + d] = results["devcnt." + d] + 1
for l in self.label_ids:
results["lblloss." + l] = torch.as_tensor(0., device=self.device)
results["lblcnt." + l] = torch.as_tensor(0., device=self.device)
results["lbln_correct." + l] = torch.as_tensor(0., device=self.device)
for i, l in enumerate(labels):
results["lblloss." + self.label_ids[l]] = results["lblloss." + self.label_ids[l]] + samples_loss[i]
results["lbln_correct." + self.label_ids[l]] = \
results["lbln_correct." + self.label_ids[l]] + n_correct_per_sample[i]
results["lblcnt." + self.label_ids[l]] = results["lblcnt." + self.label_ids[l]] + 1
self.test_step_outputs.append(results)
def on_test_epoch_end(self):
# convert a list of dicts to a flattened dict
outputs = {k: [] for k in self.test_step_outputs[0]}
for step_output in self.test_step_outputs:
for k in step_output:
outputs[k].append(step_output[k])
for k in outputs:
outputs[k] = torch.stack(outputs[k])
avg_loss = outputs['loss'].mean()
acc = sum(outputs['n_correct']) * 1.0 / sum(outputs['n_pred'])
logs = {'acc': acc, 'loss': avg_loss}
# log metric per device and scene
for d in self.device_ids:
dev_loss = outputs["devloss." + d].sum()
dev_cnt = outputs["devcnt." + d].sum()
dev_corrct = outputs["devn_correct." + d].sum()
logs["loss." + d] = dev_loss / dev_cnt
logs["acc." + d] = dev_corrct / dev_cnt
logs["cnt." + d] = dev_cnt
# device groups
logs["acc." + self.device_groups[d]] = logs.get("acc." + self.device_groups[d], 0.) + dev_corrct
logs["count." + self.device_groups[d]] = logs.get("count." + self.device_groups[d], 0.) + dev_cnt
logs["lloss." + self.device_groups[d]] = logs.get("lloss." + self.device_groups[d], 0.) + dev_loss
for d in set(self.device_groups.values()):
logs["acc." + d] = logs["acc." + d] / logs["count." + d]
logs["lloss." + d] = logs["lloss." + d] / logs["count." + d]
for l in self.label_ids:
lbl_loss = outputs["lblloss." + l].sum()
lbl_cnt = outputs["lblcnt." + l].sum()
lbl_corrct = outputs["lbln_correct." + l].sum()
logs["loss." + l] = lbl_loss / lbl_cnt
logs["acc." + l] = lbl_corrct / lbl_cnt
logs["cnt." + l] = lbl_cnt
logs["macro_avg_acc"] = torch.mean(torch.stack([logs["acc." + l] for l in self.label_ids]))
# prefix with 'test' for logging
self.log_dict({"test/" + k: logs[k] for k in logs})
self.test_step_outputs.clear()
def predict_step(self, eval_batch, batch_idx, dataloader_idx=0):
x, files = eval_batch
# assure fp16
self.model.half()
x = self.mel_forward(x)
x = x.half()
y_hat = self.model(x)
return files, y_hat
def train(config):
# logging is done using wandb
wandb_logger = WandbLogger(
project=config.project_name,
notes="Baseline System for DCASE'24 Task 1.",
tags=["DCASE24"],
config=config, # this logs all hyperparameters for us
name=config.experiment_name
)
# train dataloader
assert config.subset in {100, 50, 25, 10, 5}, "Specify an integer value in: {100, 50, 25, 10, 5} to use one of " \
"the given subsets."
roll_samples = config.orig_sample_rate * config.roll_sec
train_dl = DataLoader(dataset=get_training_set(config.subset, roll=roll_samples),
worker_init_fn=worker_init_fn,
num_workers=config.num_workers,
batch_size=config.batch_size,
shuffle=True)
test_dl = DataLoader(dataset=get_test_set(),
worker_init_fn=worker_init_fn,
num_workers=config.num_workers,
batch_size=config.batch_size)
# create pytorch lightening module
pl_module = PLModule(config)
# get model complexity from nessi and log results to wandb
sample = next(iter(test_dl))[0][0].unsqueeze(0)
shape = pl_module.mel_forward(sample).size()
macs, params = nessi.get_torch_size(pl_module.model, input_size=shape)
# log MACs and number of parameters for our model
wandb_logger.experiment.config['MACs'] = macs
wandb_logger.experiment.config['Parameters'] = params
# create the pytorch lightening trainer by specifying the number of epochs to train, the logger,
# on which kind of device(s) to train and possible callbacks
trainer = pl.Trainer(max_epochs=config.n_epochs,
logger=wandb_logger,
accelerator='gpu',
devices=1,
precision=config.precision,
callbacks=[pl.callbacks.ModelCheckpoint(save_last=True)])
# start training and validation for the specified number of epochs
trainer.fit(pl_module, train_dl, test_dl)
# final test step
# here: use the validation split
trainer.test(ckpt_path='last', dataloaders=test_dl)
wandb.finish()
def evaluate(config):
import os
from sklearn import preprocessing
import pandas as pd
import torch.nn.functional as F
from dataset.dcase24 import dataset_config
assert config.ckpt_id is not None, "A value for argument 'ckpt_id' must be provided."
ckpt_dir = os.path.join(config.project_name, config.ckpt_id, "checkpoints")
assert os.path.exists(ckpt_dir), f"No such folder: {ckpt_dir}"
ckpt_file = os.path.join(ckpt_dir, "last.ckpt")
assert os.path.exists(ckpt_file), f"No such file: {ckpt_file}. Implement your own mechanism to select" \
f"the desired checkpoint."
# create folder to store predictions
os.makedirs("predictions", exist_ok=True)
out_dir = os.path.join("predictions", config.ckpt_id)
os.makedirs(out_dir, exist_ok=True)
# load lightning module from checkpoint
pl_module = PLModule.load_from_checkpoint(ckpt_file, config=config)
trainer = pl.Trainer(logger=False,
accelerator='gpu',
devices=1,
precision=config.precision)
# evaluate lightning module on development-test split
test_dl = DataLoader(dataset=get_test_set(),
worker_init_fn=worker_init_fn,
num_workers=config.num_workers,
batch_size=config.batch_size)
# get model complexity from nessi
sample = next(iter(test_dl))[0][0].unsqueeze(0).to(pl_module.device)
shape = pl_module.mel_forward(sample).size()
macs, params = nessi.get_torch_size(pl_module.model, input_size=shape)
print(f"Model Complexity: MACs: {macs}, Params: {params}")
assert macs <= nessi.MAX_MACS, "The model exceeds the MACs limit and must not be submitted to the challenge!"
assert params <= nessi.MAX_PARAMS_MEMORY, \
"The model exceeds the parameter limit and must not be submitted to the challenge!"
allowed_precision = int(nessi.MAX_PARAMS_MEMORY / params * 8)
print(f"ATTENTION: According to the number of model parameters and the memory limits that apply in the challenge,"
f" you are allowed to use at max the following precision for model parameters: {allowed_precision} bit.")
# obtain and store details on model for reporting in the technical report
info = {}
info['MACs'] = macs
info['Params'] = params
res = trainer.test(pl_module, test_dl)
info['test'] = res
# generate predictions on evaluation set
eval_dl = DataLoader(dataset=get_eval_set(),
worker_init_fn=worker_init_fn,
num_workers=config.num_workers,
batch_size=config.batch_size)
predictions = trainer.predict(pl_module, dataloaders=eval_dl)
# all filenames
all_files = [item[len("audio/"):] for files, _ in predictions for item in files]
# all predictions
all_predictions = torch.cat([torch.as_tensor(p) for _, p in predictions], 0)
all_predictions = F.softmax(all_predictions, dim=1)
# write eval set predictions to csv file
df = pd.read_csv(dataset_config['meta_csv'], sep="\t")
le = preprocessing.LabelEncoder()
le.fit_transform(df[['scene_label']].values.reshape(-1))
class_names = le.classes_
df = {'filename': all_files}
scene_labels = [class_names[i] for i in torch.argmax(all_predictions, dim=1)]
df['scene_label'] = scene_labels
for i, label in enumerate(class_names):
df[label] = all_predictions[:, i]
df = pd.DataFrame(df)
# save eval set predictions, model state_dict and info to output folder
df.to_csv(os.path.join(out_dir, 'output.csv'), sep='\t', index=False)
torch.save(pl_module.model.state_dict(), os.path.join(out_dir, "model_state_dict.pt"))
with open(os.path.join(out_dir, "info.json"), "w") as json_file:
json.dump(info, json_file)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='DCASE 24 argument parser')
# general
parser.add_argument('--project_name', type=str, default="DCASE24_Task1")
parser.add_argument('--experiment_name', type=str, default="Baseline")
parser.add_argument('--num_workers', type=int, default=8) # number of workers for dataloaders
parser.add_argument('--precision', type=str, default="32")
# evaluation
parser.add_argument('--evaluate', action='store_true') # predictions on eval set
parser.add_argument('--ckpt_id', type=str, default=None) # for loading trained model, corresponds to wandb id
# dataset
# subset in {100, 50, 25, 10, 5}
parser.add_argument('--orig_sample_rate', type=int, default=44100)
parser.add_argument('--subset', type=int, default=100)
# model
parser.add_argument('--n_classes', type=int, default=10) # classification model with 'n_classes' output neurons
parser.add_argument('--in_channels', type=int, default=1)
# adapt the complexity of the neural network (3 main dimensions to scale the baseline)
parser.add_argument('--base_channels', type=int, default=32)
parser.add_argument('--channels_multiplier', type=float, default=1.8)
parser.add_argument('--expansion_rate', type=float, default=2.1)
# training
parser.add_argument('--n_epochs', type=int, default=150)
parser.add_argument('--batch_size', type=int, default=256)
parser.add_argument('--mixstyle_p', type=float, default=0.4) # frequency mixstyle
parser.add_argument('--mixstyle_alpha', type=float, default=0.3)
parser.add_argument('--weight_decay', type=float, default=0.0001)
parser.add_argument('--roll_sec', type=int, default=0.1) # roll waveform over time
# peak learning rate (in cosinge schedule)
parser.add_argument('--lr', type=float, default=0.005)
parser.add_argument('--warmup_steps', type=int, default=2000)
# preprocessing
parser.add_argument('--sample_rate', type=int, default=32000)
parser.add_argument('--window_length', type=int, default=3072) # in samples (corresponds to 96 ms)
parser.add_argument('--hop_length', type=int, default=500) # in samples (corresponds to ~16 ms)
parser.add_argument('--n_fft', type=int, default=4096) # length (points) of fft, e.g. 4096 point FFT
parser.add_argument('--n_mels', type=int, default=256) # number of mel bins
parser.add_argument('--freqm', type=int, default=48) # mask up to 'freqm' spectrogram bins
parser.add_argument('--timem', type=int, default=0) # mask up to 'timem' spectrogram frames
parser.add_argument('--f_min', type=int, default=0) # mel bins are created for freqs. between 'f_min' and 'f_max'
parser.add_argument('--f_max', type=int, default=None)
args = parser.parse_args()
if args.evaluate:
evaluate(args)
else:
train(args)