yangwang825 commited on
Commit
9211727
1 Parent(s): 0b55947

Upload 2 files

Browse files
Files changed (2) hide show
  1. _fsd2019.py +82 -0
  2. fsd2019.py +124 -0
_fsd2019.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ CLASSES = [
2
+ "Accelerating_and_revving_and_vroom",
3
+ "Accordion",
4
+ "Acoustic_guitar",
5
+ "Applause",
6
+ "Bark",
7
+ "Bass_drum",
8
+ "Bass_guitar",
9
+ "Bathtub_(filling_or_washing)",
10
+ "Bicycle_bell",
11
+ "Burping_and_eructation",
12
+ "Bus",
13
+ "Buzz",
14
+ "Car_passing_by",
15
+ "Cheering",
16
+ "Chewing_and_mastication",
17
+ "Child_speech_and_kid_speaking",
18
+ "Chink_and_clink",
19
+ "Chirp_and_tweet",
20
+ "Church_bell",
21
+ "Clapping",
22
+ "Computer_keyboard",
23
+ "Crackle",
24
+ "Cricket",
25
+ "Crowd",
26
+ "Cupboard_open_or_close",
27
+ "Cutlery_and_silverware",
28
+ "Dishes_and_pots_and_pans",
29
+ "Drawer_open_or_close",
30
+ "Drip",
31
+ "Electric_guitar",
32
+ "Fart",
33
+ "Female_singing",
34
+ "Female_speech_and_woman_speaking",
35
+ "Fill_(with_liquid)",
36
+ "Finger_snapping",
37
+ "Frying_(food)",
38
+ "Gasp",
39
+ "Glockenspiel",
40
+ "Gong",
41
+ "Gurgling",
42
+ "Harmonica",
43
+ "Hi-hat",
44
+ "Hiss",
45
+ "Keys_jangling",
46
+ "Knock",
47
+ "Male_singing",
48
+ "Male_speech_and_man_speaking",
49
+ "Marimba_and_xylophone",
50
+ "Mechanical_fan",
51
+ "Meow",
52
+ "Microwave_oven",
53
+ "Motorcycle",
54
+ "Printer",
55
+ "Purr",
56
+ "Race_car_and_auto_racing",
57
+ "Raindrop",
58
+ "Run",
59
+ "Scissors",
60
+ "Screaming",
61
+ "Shatter",
62
+ "Sigh",
63
+ "Sink_(filling_or_washing)",
64
+ "Skateboard",
65
+ "Slam",
66
+ "Sneeze",
67
+ "Squeak",
68
+ "Stream",
69
+ "Strum",
70
+ "Tap",
71
+ "Tick-tock",
72
+ "Toilet_flush",
73
+ "Traffic_noise_and_roadway_noise",
74
+ "Trickle_and_dribble",
75
+ "Walk_and_footsteps",
76
+ "Water_tap_and_faucet",
77
+ "Waves_and_surf",
78
+ "Whispering",
79
+ "Writing",
80
+ "Yell",
81
+ "Zipper_(clothing)",
82
+ ]
fsd2019.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """FSDKaggle2019 sound classification dataset."""
4
+
5
+
6
+ import os
7
+ import textwrap
8
+ import datasets
9
+ import itertools
10
+ import pandas as pd
11
+ import typing as tp
12
+ from pathlib import Path
13
+ from copy import deepcopy
14
+
15
+ from ._fsd2019 import CLASSES
16
+
17
+ SAMPLE_RATE = 44_100
18
+
19
+ _TRAIN_CURATED_URL = "https://zenodo.org/records/3612637/files/FSDKaggle2019.audio_train_curated.zip"
20
+ _TEST_URL = "https://zenodo.org/records/3612637/files/FSDKaggle2019.audio_test.zip"
21
+ _METADATA_URL = "https://zenodo.org/records/3612637/files/FSDKaggle2019.meta.zip"
22
+
23
+
24
+ class FSDKaggle2019Config(datasets.BuilderConfig):
25
+ """BuilderConfig for FSDKaggle2019."""
26
+
27
+ def __init__(self, features, **kwargs):
28
+ super(FSDKaggle2019Config, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
29
+ self.features = features
30
+
31
+
32
+ class FSDKaggle2019(datasets.GeneratorBasedBuilder):
33
+
34
+ BUILDER_CONFIGS = [
35
+ FSDKaggle2019Config(
36
+ features=datasets.Features(
37
+ {
38
+ "file": datasets.Value("string"),
39
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
40
+ "sound": datasets.Sequence(datasets.Value("string")),
41
+ "label": datasets.Sequence(datasets.features.ClassLabel(names=CLASSES)),
42
+ }
43
+ ),
44
+ name="curated",
45
+ description="",
46
+ ),
47
+ ]
48
+
49
+ def _info(self):
50
+ return datasets.DatasetInfo(
51
+ description="",
52
+ features=self.config.features,
53
+ supervised_keys=None,
54
+ homepage="",
55
+ citation="",
56
+ task_templates=None,
57
+ )
58
+
59
+ def _split_generators(self, dl_manager):
60
+ """Returns SplitGenerators."""
61
+ train_archive_path = dl_manager.download_and_extract(_TRAIN_CURATED_URL)
62
+ test_archive_path = dl_manager.download_and_extract(_TEST_URL)
63
+ metadata_archive_path = dl_manager.download_and_extract(_METADATA_URL)
64
+
65
+ train_df = pd.read_csv(os.path.join(metadata_archive_path, "FSDKaggle2019.meta", "train_curated_post_competition.csv"))
66
+ test_df = pd.read_csv(os.path.join(metadata_archive_path, "FSDKaggle2019.meta", "test_post_competition.csv"))
67
+
68
+ return [
69
+ datasets.SplitGenerator(
70
+ name=datasets.Split.TRAIN, gen_kwargs={"archive_path": train_archive_path, "split": "train", "metadata": train_df}
71
+ ),
72
+ datasets.SplitGenerator(
73
+ name=datasets.Split.TEST, gen_kwargs={"archive_path": test_archive_path, "split": "test", "metadata": test_df}
74
+ ),
75
+ ]
76
+
77
+ def _generate_examples(self, archive_path, split=None, metadata=None):
78
+ extensions = ['.wav']
79
+ _, _walker = fast_scandir(archive_path, extensions, recursive=True)
80
+
81
+ metadata_df = deepcopy(metadata)
82
+
83
+ def default_find_classes(audio_path):
84
+ fileid = Path(audio_path).name
85
+ ids = metadata_df.query(f'fname=="{fileid}"')['labels'].values.tolist()
86
+ ids = str(ids[0]).split(',')
87
+ # assert False, f"{ids}"
88
+ return ids
89
+
90
+ for guid, audio_path in enumerate(_walker):
91
+ yield guid, {
92
+ "id": str(guid),
93
+ "file": audio_path,
94
+ "audio": audio_path,
95
+ "audio": default_find_classes(audio_path),
96
+ "label": default_find_classes(audio_path),
97
+ }
98
+
99
+
100
+ def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
101
+ # Scan files recursively faster than glob
102
+ # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
103
+ subfolders, files = [], []
104
+
105
+ try: # hope to avoid 'permission denied' by this try
106
+ for f in os.scandir(path):
107
+ try: # 'hope to avoid too many levels of symbolic links' error
108
+ if f.is_dir():
109
+ subfolders.append(f.path)
110
+ elif f.is_file():
111
+ if os.path.splitext(f.name)[1].lower() in exts:
112
+ files.append(f.path)
113
+ except Exception:
114
+ pass
115
+ except Exception:
116
+ pass
117
+
118
+ if recursive:
119
+ for path in list(subfolders):
120
+ sf, f = fast_scandir(path, exts, recursive=recursive)
121
+ subfolders.extend(sf)
122
+ files.extend(f) # type: ignore
123
+
124
+ return subfolders, files