-
Notifications
You must be signed in to change notification settings - Fork 66
/
train_sed.py
376 lines (334 loc) · 12.2 KB
/
train_sed.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
import argparse
import os
import random
import warnings
import numpy as np
import pandas as pd
import pytorch_lightning as pl
import torch
import yaml
from desed_task.dataio import ConcatDatasetBatchSampler
from desed_task.dataio.datasets import (StronglyAnnotatedSet, UnlabeledSet,
WeakSet)
from desed_task.nnet.CRNN import CRNN
from desed_task.utils.encoder import ManyHotEncoder
from desed_task.utils.schedulers import ExponentialWarmup
from local.classes_dict import classes_labels
from local.resample_folder import resample_folder
from local.sed_trainer import SEDTask4
from local.utils import generate_tsv_wav_durations
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.loggers import TensorBoardLogger
def resample_data_generate_durations(config_data, test_only=False, evaluation=False):
if not test_only:
dsets = [
"synth_folder",
"synth_val_folder",
"strong_folder",
"weak_folder",
"unlabeled_folder",
"test_folder",
]
elif test_only:
dsets = ["test_folder"]
else:
dsets = ["eval_folder"]
for dset in dsets:
computed = resample_folder(
config_data[dset + "_44k"], config_data[dset], target_fs=config_data["fs"]
)
if not evaluation:
for base_set in ["synth_val", "test"]:
if not os.path.exists(config_data[base_set + "_dur"]) or computed:
generate_tsv_wav_durations(
config_data[base_set + "_folder"], config_data[base_set + "_dur"]
)
def single_run(
config,
log_dir,
gpus,
strong_real=False,
checkpoint_resume=None,
test_state_dict=None,
fast_dev_run=False,
evaluation=False,
):
"""
Running sound event detection baselin
Args:
config (dict): the dictionary of configuration params
log_dir (str): path to log directory
gpus (int): number of gpus to use
checkpoint_resume (str, optional): path to checkpoint to resume from. Defaults to "".
test_state_dict (dict, optional): if not None, no training is involved. This dictionary is the state_dict
to be loaded to test the model.
fast_dev_run (bool, optional): whether to use a run with only one batch at train and validation, useful
for development purposes.
"""
config.update({"log_dir": log_dir})
##### data prep test ##########
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"],
)
if not evaluation:
devtest_df = pd.read_csv(config["data"]["test_tsv"], sep="\t")
devtest_dataset = StronglyAnnotatedSet(
config["data"]["test_folder"],
devtest_df,
encoder,
return_filename=True,
pad_to=config["data"]["audio_max_len"],
)
else:
devtest_dataset = UnlabeledSet(
config["data"]["eval_folder"], encoder, pad_to=None, return_filename=True
)
test_dataset = devtest_dataset
##### model definition ############
sed_student = CRNN(**config["net"])
if test_state_dict is None:
##### data prep train valid ##########
synth_df = pd.read_csv(config["data"]["synth_tsv"], sep="\t")
synth_set = StronglyAnnotatedSet(
config["data"]["synth_folder"],
synth_df,
encoder,
pad_to=config["data"]["audio_max_len"],
)
if strong_real:
strong_df = pd.read_csv(config["data"]["strong_tsv"], sep="\t")
strong_set = StronglyAnnotatedSet(
config["data"]["strong_folder"],
strong_df,
encoder,
pad_to=config["data"]["audio_max_len"],
)
weak_df = pd.read_csv(config["data"]["weak_tsv"], sep="\t")
train_weak_df = weak_df.sample(
frac=config["training"]["weak_split"],
random_state=config["training"]["seed"],
)
valid_weak_df = weak_df.drop(train_weak_df.index).reset_index(drop=True)
train_weak_df = train_weak_df.reset_index(drop=True)
weak_set = WeakSet(
config["data"]["weak_folder"],
train_weak_df,
encoder,
pad_to=config["data"]["audio_max_len"],
)
unlabeled_set = UnlabeledSet(
config["data"]["unlabeled_folder"],
encoder,
pad_to=config["data"]["audio_max_len"],
)
synth_df_val = pd.read_csv(config["data"]["synth_val_tsv"], sep="\t")
synth_val = StronglyAnnotatedSet(
config["data"]["synth_val_folder"],
synth_df_val,
encoder,
return_filename=True,
pad_to=config["data"]["audio_max_len"],
)
weak_val = WeakSet(
config["data"]["weak_folder"],
valid_weak_df,
encoder,
pad_to=config["data"]["audio_max_len"],
return_filename=True,
)
if strong_real:
strong_full_set = torch.utils.data.ConcatDataset([strong_set, synth_set])
tot_train_data = [strong_full_set, weak_set, unlabeled_set]
else:
tot_train_data = [synth_set, weak_set, unlabeled_set]
train_dataset = torch.utils.data.ConcatDataset(tot_train_data)
batch_sizes = config["training"]["batch_size"]
samplers = [torch.utils.data.RandomSampler(x) for x in tot_train_data]
batch_sampler = ConcatDatasetBatchSampler(samplers, batch_sizes)
valid_dataset = torch.utils.data.ConcatDataset([synth_val, weak_val])
##### training params and optimizers ############
epoch_len = min(
[
len(tot_train_data[indx])
// (
config["training"]["batch_size"][indx]
* config["training"]["accumulate_batches"]
)
for indx in range(len(tot_train_data))
]
)
opt = torch.optim.Adam(sed_student.parameters(), 1e-3, betas=(0.9, 0.999))
exp_steps = config["training"]["n_epochs_warmup"] * epoch_len
exp_scheduler = {
"scheduler": ExponentialWarmup(opt, config["opt"]["lr"], exp_steps),
"interval": "step",
}
logger = TensorBoardLogger(
os.path.dirname(config["log_dir"]),
config["log_dir"].split("/")[-1],
)
print(f"experiment dir: {logger.log_dir}")
callbacks = [
EarlyStopping(
monitor="val/obj_metric",
patience=config["training"]["early_stop_patience"],
verbose=True,
mode="max",
),
ModelCheckpoint(
logger.log_dir,
monitor="val/obj_metric",
save_top_k=1,
mode="max",
save_last=True,
),
]
else:
train_dataset = None
valid_dataset = None
batch_sampler = None
opt = None
exp_scheduler = None
logger = True
callbacks = None
desed_training = SEDTask4(
config,
encoder=encoder,
sed_student=sed_student,
opt=opt,
train_data=train_dataset,
valid_data=valid_dataset,
test_data=test_dataset,
train_sampler=batch_sampler,
scheduler=exp_scheduler,
fast_dev_run=fast_dev_run,
evaluation=evaluation,
)
# Not using the fast_dev_run of Trainer because creates a DummyLogger so cannot check problems with the Logger
if fast_dev_run:
flush_logs_every_n_steps = 1
log_every_n_steps = 1
limit_train_batches = 2
limit_val_batches = 2
limit_test_batches = 2
n_epochs = 3
else:
flush_logs_every_n_steps = 100
log_every_n_steps = 40
limit_train_batches = 1.0
limit_val_batches = 1.0
limit_test_batches = 1.0
n_epochs = config["training"]["n_epochs"]
if len(gpus.split(",")) > 1:
raise NotImplementedError("Multiple GPUs are currently not supported")
trainer = pl.Trainer(
precision=config["training"]["precision"],
max_epochs=n_epochs,
callbacks=callbacks,
gpus=gpus,
strategy=config["training"].get("backend"),
accumulate_grad_batches=config["training"]["accumulate_batches"],
logger=logger,
resume_from_checkpoint=checkpoint_resume,
gradient_clip_val=config["training"]["gradient_clip"],
check_val_every_n_epoch=config["training"]["validation_interval"],
num_sanity_val_steps=0,
log_every_n_steps=log_every_n_steps,
flush_logs_every_n_steps=flush_logs_every_n_steps,
limit_train_batches=limit_train_batches,
limit_val_batches=limit_val_batches,
limit_test_batches=limit_test_batches,
)
if test_state_dict is None:
# start tracking energy consumption
trainer.fit(desed_training)
best_path = trainer.checkpoint_callback.best_model_path
print(f"best model: {best_path}")
test_state_dict = torch.load(best_path)["state_dict"]
desed_training.load_state_dict(test_state_dict)
trainer.test(desed_training)
if __name__ == "__main__":
parser = argparse.ArgumentParser("Training a SED system for DESED Task")
parser.add_argument(
"--conf_file",
default="./confs/default.yaml",
help="The configuration file with all the experiment parameters.",
)
parser.add_argument(
"--log_dir",
default="./exp/2022_baseline",
help="Directory where to save tensorboard logs, saved models, etc.",
)
parser.add_argument(
"--strong_real",
action="store_true",
default=False,
help="The strong annotations coming from Audioset will be included in the training phase.",
)
parser.add_argument(
"--resume_from_checkpoint",
default=None,
help="Allow the training to be resumed, take as input a previously saved model (.ckpt).",
)
parser.add_argument(
"--test_from_checkpoint", default=None, help="Test the model specified"
)
parser.add_argument(
"--gpus",
default="1",
help="The number of GPUs to train on, or the gpu to use, default='0', "
"so uses one GPU",
)
parser.add_argument(
"--fast_dev_run",
action="store_true",
default=False,
help="Use this option to make a 'fake' run which is useful for development and debugging. "
"It uses very few batches and epochs so it won't give any meaningful result.",
)
parser.add_argument(
"--eval_from_checkpoint", default=None, help="Evaluate the model specified"
)
args = parser.parse_args()
with open(args.conf_file, "r") as f:
configs = yaml.safe_load(f)
evaluation = False
test_from_checkpoint = args.test_from_checkpoint
if args.eval_from_checkpoint is not None:
test_from_checkpoint = args.eval_from_checkpoint
evaluation = True
test_model_state_dict = None
if test_from_checkpoint is not None:
checkpoint = torch.load(test_from_checkpoint)
configs_ckpt = checkpoint["hyper_parameters"]
configs_ckpt["data"] = configs["data"]
print(
f"loaded model: {test_from_checkpoint} \n"
f"at epoch: {checkpoint['epoch']}"
)
test_model_state_dict = checkpoint["state_dict"]
if evaluation:
configs["training"]["batch_size_val"] = 1
seed = configs["training"]["seed"]
if seed:
torch.random.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
pl.seed_everything(seed)
test_only = test_from_checkpoint is not None
resample_data_generate_durations(configs["data"], test_only, evaluation)
single_run(
configs,
args.log_dir,
args.gpus,
args.strong_real,
args.resume_from_checkpoint,
test_model_state_dict,
args.fast_dev_run,
evaluation,
)