File size: 4,617 Bytes
20e025e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc97d9f
5b60d13
20e025e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138b9f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import datasets
import typing as tp
from pathlib import Path

SAMPLE_RATE = 44_100
EXTENSION = '.wav'


class ESC50(datasets.GeneratorBasedBuilder):
    """
    The ESC50 dataset
    """
    def _info(self):
        return datasets.DatasetInfo(
            description='Environmental sound classification dataset',
            features=datasets.Features(
                {
                    # "filepath": datasets.Value("string"), 
                    "audio": datasets.Audio(sampling_rate=SAMPLE_RATE), 
                    "fold": datasets.Value(dtype='int32', id=None), 
                    "label": datasets.ClassLabel(names=ESC50_LABELS), 
                }
            ),
        )

    def _split_generators(self, dl_manager):
        data_dir = dl_manager.download_and_extract("ESC-50-master.zip")
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN, 
                gen_kwargs={
                    "data_dir": data_dir, 
                    "split": 'train'
                }, 
            ), 
        ]

    def _generate_examples(self, data_dir, split):
        _, _walker = fast_scandir(data_dir, [EXTENSION], recursive=True)

        for idx, fileid in enumerate(_walker):
            yield idx, {
                # 'filepath': fileid, 
                'audio': fileid, 
                'fold': default_find_fold(fileid), 
                'label': default_find_classes(fileid)
            }


def default_find_classes(audio_path):
    id_ = Path(audio_path).stem.split('-')[-1]
    return ESC50_ID2LABEL.get(int(id_))


def default_find_fold(audio_path):
    fold = Path(audio_path).stem.split('-')[0]
    return int(fold)


def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
    # Scan files recursively faster than glob
    # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
    subfolders, files = [], []

    try:  # hope to avoid 'permission denied' by this try
        for f in os.scandir(path):
            try:  # 'hope to avoid too many levels of symbolic links' error
                if f.is_dir():
                    subfolders.append(f.path)
                elif f.is_file():
                    if os.path.splitext(f.name)[1].lower() in exts:
                        files.append(f.path)
            except Exception:
                pass
    except Exception:
        pass

    if recursive:
        for path in list(subfolders):
            sf, f = fast_scandir(path, exts, recursive=recursive)
            subfolders.extend(sf)
            files.extend(f)  # type: ignore

    return subfolders, files


ESC50_LABELS = [
    "airplane",
    "breathing",
    "brushing_teeth",
    "can_opening",
    "car_horn",
    "cat",
    "chainsaw",
    "chirping_birds",
    "church_bells",
    "clapping",
    "clock_alarm",
    "clock_tick",
    "coughing",
    "cow",
    "crackling_fire",
    "crickets",
    "crow",
    "crying_baby",
    "dog",
    "door_wood_creaks",
    "door_wood_knock",
    "drinking_sipping",
    "engine",
    "fireworks",
    "footsteps",
    "frog",
    "glass_breaking",
    "hand_saw",
    "helicopter",
    "hen",
    "insects",
    "keyboard_typing",
    "laughing",
    "mouse_click",
    "pig",
    "pouring_water",
    "rain",
    "rooster",
    "sea_waves",
    "sheep",
    "siren",
    "sneezing",
    "snoring",
    "thunderstorm",
    "toilet_flush",
    "train",
    "vacuum_cleaner",
    "washing_machine",
    "water_drops",
    "wind",
]

ESC50_LABEL2ID = {
    "dog": 0,
    "rooster": 1,
    "pig": 2,
    "cow": 3,
    "frog": 4,
    "cat": 5,
    "hen": 6,
    "insects": 7,
    "sheep": 8,
    "crow": 9,
    "rain": 10,
    "sea_waves": 11,
    "crackling_fire": 12,
    "crickets": 13,
    "chirping_birds": 14,
    "water_drops": 15,
    "wind": 16,
    "pouring_water": 17,
    "toilet_flush": 18,
    "thunderstorm": 19,
    "crying_baby": 20,
    "sneezing": 21,
    "clapping": 22,
    "breathing": 23,
    "coughing": 24,
    "footsteps": 25,
    "laughing": 26,
    "brushing_teeth": 27,
    "snoring": 28,
    "drinking_sipping": 29,
    "door_wood_knock": 30,
    "mouse_click": 31,
    "keyboard_typing": 32,
    "door_wood_creaks": 33,
    "can_opening": 34,
    "washing_machine": 35,
    "vacuum_cleaner": 36,
    "clock_alarm": 37,
    "clock_tick": 38,
    "glass_breaking": 39,
    "helicopter": 40,
    "chainsaw": 41,
    "siren": 42,
    "car_horn": 43,
    "engine": 44,
    "train": 45,
    "church_bells": 46,
    "airplane": 47,
    "fireworks": 48,
    "hand_saw": 49
}
ESC50_ID2LABEL = {v:k for k, v in ESC50_LABEL2ID.items()}