forked from DCASE-REPO/DESED_task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_dcase_subset.py
548 lines (418 loc) · 17.3 KB
/
generate_dcase_subset.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
# Script to generate the different version of the DESED dataset used in the paper for the DCASE Workshop.
import jams
import scaper
import json
import os
import glob
import argparse
import yaml
import shutil
import desed
def create_folder(folder, exist_ok=True, delete_if_exists=False):
"""Create folder (and parent folders) if not exists.
Args:
folder (str): path of folder to create.
exist_ok (bool): if set to True (default), the FileExistsError is not raised.
delete_if_exists: bool, True if you want to delete the folder if already exists.
Returns:
None
"""
if not folder == "":
if delete_if_exists:
if os.path.exists(folder):
shutil.rmtree(folder)
os.mkdir(folder)
os.makedirs(folder, exist_ok=exist_ok)
def jam_initialization(annotations):
"""The function initializes the jam file, setting the annotations given as input
Args:
annotations (dict): annotation of jam file
Returns:
JAMS: jam file initialized
"""
# Jams instances
jam = jams.JAMS()
# file_metadata
jam.file_metadata.duration = annotations["duration"]
return jam
def get_sandbox(annotations, background_path, foreground_path):
"""The function gets the sandbox from the annotation and generate the new sandbox for the jam file to create
updating the proper data, as the path to the background and foreground soundbank folders.
Args:
annotations (dict): annotations of the jam file
background_path (str): path to background soundbank folder
foreground_path (str): path to foreground soundbank folder
Returns:
dict: sandbox of the jam file
"""
# getting the sandbox
sandbox = annotations["sandbox"]
duration = annotations["duration"]
# setting the background and foreground folder path
sandbox["scaper"]["fg_path"] = foreground_path
sandbox["scaper"]["bg_path"] = background_path
sandbox_tmp = {"scaper": {}}
sandbox_meta = {
"duration": duration,
"original_duration": duration,
"fg_path": sandbox["scaper"]["fg_path"],
"bg_path": sandbox["scaper"]["bg_path"],
"protected_labels": [],
}
sandbox_tmp["scaper"].update(sandbox_meta)
sandbox_tmp = {"scaper": sandbox["scaper"].copy()}
del sandbox_tmp["scaper"]["fg_spec"]
del sandbox_tmp["scaper"]["bg_spec"]
return sandbox_tmp
def jam_annotation(annotations, background_path, foreground_path):
"""The function sets the annotations for the jam file
Args:
annotations (dict): annotations
background_path (str): path to the background soundbank folder
foreground_path (str): path to foreground soundbank folder
Returns:
dict: annotations of the jam file
"""
# get the necessary annotations and adding them to the annotations to be added to the jam file
namespace = annotations["namespace"]
time = annotations["time"]
duration = annotations["duration"]
sandbox = get_sandbox(annotations, background_path, foreground_path)
ann = jams.Annotation(
namespace=namespace, sandbox=sandbox, time=time, duration=duration
)
return ann
def get_jam_annotations(annotations, background_path, foreground_path):
"""The function initialize a jam file, setting the annotations given as input and return the JAMS file and the annotations
Args:
Args:
annotations (dict): annotations
background_path (str): path to the background soundbank folder
foreground_path (str): path to foreground soundbank folder
Returns:
JAMS: jam file initialized
Annotations: jam annotations
"""
jam = jam_initialization(annotations)
ann = jam_annotation(annotations, background_path, foreground_path)
return jam, ann
def generate_audio(jam, anns, background_path, foreground_path, out_file_path):
"""The function generates an audio clip from a jam file given as input
Args
jam (JAMS): jam file
anns (Annotations): annotations of the jam file
background_path (str): path to background soundbank folder
foreground_path (str): path to foreeground soundbank folder
out_file_path (str): path to output file
Returns:
None
"""
jam.annotations.append(anns)
jam.save(out_file_path)
# generate the audio clip
scaper.generate_from_jams(
out_file_path,
audio_outfile=out_file_path.replace(".jams", ".wav"),
fg_path=foreground_path,
bg_path=background_path,
jams_outfile=out_file_path,
save_isolated_events=False,
txt_path=out_file_path.replace(".jams", ".txt"),
)
def change_snr(annotations, ann_snr, db_to_decrease, target_labels):
"""The function changes (decreases) the SNR of all the non-target events by db_to_decrease given as input
Args:
annotations (Annotations): annotations of the jam file
ann_snr (Annotations): annotations of the jam file with the new SNR
db_to_decrease (int): db to decrease for no-target events
target_labels (list): list of target labels to consider
Returns:
None
"""
number_of_events = len(annotations["data"])
for event in range(number_of_events):
tmp_ann = annotations["data"][event]
if tmp_ann["value"]["role"] == "background":
ann_snr.append(
time=tmp_ann["time"],
duration=tmp_ann["duration"],
value=tmp_ann["value"],
confidence=tmp_ann["confidence"],
)
elif tmp_ann["value"]["role"] == "foreground":
if tmp_ann["value"]["label"] in target_labels:
ann_snr.append(
time=tmp_ann["time"],
duration=tmp_ann["duration"],
value=tmp_ann["value"],
confidence=tmp_ann["confidence"],
)
else:
tmp_ann["value"]["snr"] = tmp_ann["value"]["snr"] - db_to_decrease
ann_snr.append(
time=tmp_ann["time"],
duration=tmp_ann["duration"],
value=tmp_ann["value"],
confidence=tmp_ann["confidence"],
)
def collect_target_events(annotations, ann_target, target_labels):
"""The function collects only the target events present in the annotations and return the number of event collected
Args:
annotations (dict): annotations from jam file
ann_target (dict): only target events annotations
target_labels (list): list of target events
Returns:
int: number of target events present in the jam file
"""
number_of_events = len(annotations["data"])
for event in range(number_of_events):
tmp_ann = annotations["data"][event]
if tmp_ann["value"]["role"] == "background":
ann_target.append(
time=tmp_ann["time"],
duration=tmp_ann["duration"],
value=tmp_ann["value"],
confidence=tmp_ann["confidence"],
)
elif tmp_ann["value"]["role"] == "foreground":
if tmp_ann["value"]["label"] in target_labels:
ann_target.append(
time=tmp_ann["time"],
duration=tmp_ann["duration"],
value=tmp_ann["value"],
confidence=tmp_ann["confidence"],
)
return len(ann_target["data"])
def collect_nontarget_events(annotations, ann_ntarget, target_labels):
"""The function collects only the non-target events present in the annotations and return the number
of non-target events collected
Args:
annotations (dict): annotations from jam file
ann_target (dict): only no-target events annotations
target_labels (list): list of target events
Returns:
int: number of no-target events present in the jam file
"""
number_of_events = len(annotations["data"])
for event in range(number_of_events):
tmp_ann = annotations["data"][event]
if tmp_ann["value"]["role"] == "background":
ann_ntarget.append(
time=tmp_ann["time"],
duration=tmp_ann["duration"],
value=tmp_ann["value"],
confidence=tmp_ann["confidence"],
)
elif tmp_ann["value"]["role"] == "foreground":
if tmp_ann["value"]["label"] not in target_labels:
ann_ntarget.append(
time=tmp_ann["time"],
duration=tmp_ann["duration"],
value=tmp_ann["value"],
confidence=tmp_ann["confidence"],
)
return len(ann_ntarget["data"])
class Subset:
"""Subset class modeling a subsets"""
def __init__(
self,
synth_files_folder,
output_folder_ext,
background_folder,
foreground_folder,
target_labels,
):
"""Initialization of subset class
Args:
synth_files_folder (str): synthetic data folder path
output_folder_ext (str): extension to add to the output folder path
background_folder (str): background soundbank folder path
foreground_folder (str): foreground soundbank folder path
target_labels (list): list of target events
Return:
None
"""
self.synth_files_folder = synth_files_folder
self.jams_files = glob.glob(os.path.join(self.synth_files_folder, "*.jams"))
self.output_folder = self.synth_files_folder + output_folder_ext
self.background_folder = background_folder
self.foreground_folder = foreground_folder
self.target_labels = target_labels
create_folder(self.output_folder, exist_ok=True)
def generate_target_nontarget_files(self, only_target):
"""
The function generate a version of the subset containing only target or non-target events depending on the only_target flag
Args:
only_target (boolean): True if the subset to generate contains only target events, False if the subset to generate contains only non-target events
Returns:
None
"""
self.only_target = only_target
for file in self.jams_files:
n_file = (file.split("/"))[-1].split(".")[0]
with open(file, "r") as f:
contents = json.loads(f.read())
annotations = contents["annotations"][0]
out_file_path = os.path.join(self.output_folder, n_file + ".jams")
if only_target:
self.generate_target_nontarget_file_from_jam(annotations, out_file_path)
else:
self.generate_target_nontarget_file_from_jam(annotations, out_file_path)
def generate_target_nontarget_file_from_jam(self, annotations, out_file_path):
"""The function generates audio clip containing only target or non-target events depending on the jam file specification
Args:
annotations (Annotations): annotations of the jam file
out_file_path (str): output file path
"""
jam, ann = get_jam_annotations(
annotations, self.background_folder, self.foreground_folder
)
if self.only_target:
collect_target_events(annotations, ann, target_labels)
generate_audio(
jam, ann, self.background_folder, self.foreground_folder, out_file_path
)
else:
if collect_nontarget_events(annotations, ann, target_labels) > 1:
generate_audio(
jam,
ann,
self.background_folder,
self.foreground_folder,
out_file_path,
)
def change_snr_from_jam_file(self, annotations, db_to_decrease, out_file_path):
"""The function initializes the jam file and the annotation and change SNR for the non-targets events
Args:
annotations (Annotations): annotations of the file
db_to_decrease (int): db to decrease from the file
out_file_path (str): path to the output file
Return:
None
"""
jam, ann = get_jam_annotations(
annotations, self.background_folder, self.foreground_folder
)
change_snr(annotations, ann, db_to_decrease, self.target_labels)
# generate audio
generate_audio(
jam, ann, self.background_folder, self.foreground_folder, out_file_path
)
def decrease_snr(
self,
db_to_decrease,
):
"""The functions process all the files to decrease the SNR of non-target events contained in the audio clips
Args:
db_to_decrease (int): db to decrease from the file
Return:
None
"""
for file in self.jams_files:
n_file = (file.split("/"))[-1].split(".")[0]
with open(file, "r") as f:
contents = json.loads(f.read())
annotations = contents["annotations"][0]
out_file_path = os.path.join(self.output_folder, n_file + ".jams")
self.change_snr_from_jam_file(annotations, db_to_decrease, out_file_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser("Generating synthetic audio files")
parser.add_argument(
"--conf_file",
default="./confs/sed_dataset.yaml",
help="The configuration file with all the experiment parameters.",
)
parser.add_argument(
"--all",
action="store_true",
default=False,
help="""Generation of the target versions of ths train, validation and evaluation dataset,
non-target events version of the evaluation set and SNR versions of the train,
validation subset of the DESED dataset.""",
)
parser.add_argument(
"--tg",
action="store_true",
default=False,
help="""Generation of the target versions of ths train, validation and/or evaluation dataset.
If only one dataset need to be generated, this should be set on the configuration file.
""",
)
parser.add_argument(
"--ntg",
action="store_true",
default=False,
help="""Generation of the non-target versions of the train, validation and/or evaluation dataset.
If only one dataset need to be generated, this should be set on the configuration file.
""",
)
parser.add_argument(
"--snr",
action="store_true",
default=False,
help="""Generation of the different SNR versions of the dataset.
If only one dataset need to be generated, this should be set on the configuration file.
dB to decreased can be set on the configuration file.
""",
)
args = parser.parse_args()
with open(args.conf_file, "r") as f:
configs = yaml.safe_load(f)
# get the path to the differnt set of data
path_to_folders = configs["data"]
# get the parameters
snr = configs["params"]["snr"]
target_set = configs["params"]["target_set"]
nontarget_set = configs["params"]["nontarget_set"]
snr_set = configs["params"]["snr_set"]
target_labels = configs["params"]["target_labels"]
##############################
##### generation of data #####
##############################
if args.all:
args.tg = True
args.ntg = True
args.snr = True
if args.tg:
for split in target_set:
folder_ext = "_target"
subset = Subset(
configs["data"][f"synth_{split}"],
folder_ext,
configs["data"][f"background_{split}"],
configs["data"][f"foreground_{split}"],
target_labels,
)
print(f"Generating subset {split}, only target files.")
# generate the only-target subset
subset.generate_target_nontarget_files(only_target=True)
print(f"Target {split} subset generated.\n")
if args.ntg:
for split in nontarget_set:
folder_ext = "_nontarget"
subset = Subset(
configs["data"][f"synth_{split}"],
folder_ext,
configs["data"][f"background_{split}"],
configs["data"][f"foreground_{split}"],
target_labels,
)
print(f"Generating subset {split}, only non-target files.")
# generate the non-target subset
subset.generate_target_nontarget_files(only_target=False)
print(f"Non target {split} subset generated.\n")
if args.snr:
for db_to_decrease in snr:
for split in snr_set:
folder_ext = "_" + str(db_to_decrease) + "SNR"
subset = Subset(
configs["data"][f"synth_{split}"],
folder_ext,
configs["data"][f"background_{split}"],
configs["data"][f"foreground_{split}"],
target_labels,
)
print(f"Generating subset {split}, SNR {db_to_decrease}.")
# generate the different SNR versions of the subset
subset.decrease_snr(db_to_decrease)
print(f"Subset generated for SNR {db_to_decrease} for {split}.\n")