yangwang825 commited on
Commit
438c856
1 Parent(s): 74eda62

Upload 2 files

Browse files
Files changed (2) hide show
  1. ESC-50-master.zip +3 -0
  2. esc50 (1).py +230 -0
ESC-50-master.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d32cc4432c656db311b39bd474cdc0e9644d03633ea5457dcbba2d7fa7cb55ad
3
+ size 645694876
esc50 (1).py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import datasets
3
+ import typing as tp
4
+ from pathlib import Path
5
+
6
+ SAMPLE_RATE = 44_100
7
+ EXTENSION = '.wav'
8
+
9
+
10
+ class Esc50Config(datasets.BuilderConfig):
11
+ """BuilderConfig for ESC50."""
12
+
13
+ def __init__(self, features, **kwargs):
14
+ super(Esc50Config, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
15
+ self.features = features
16
+
17
+
18
+ class ESC50(datasets.GeneratorBasedBuilder):
19
+ """
20
+ The ESC50 dataset
21
+ """
22
+
23
+ BUILDER_CONFIGS = [
24
+ Esc50Config(
25
+ features=datasets.Features(
26
+ {
27
+ "file": datasets.Value("string"),
28
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
29
+ "sound": datasets.Value("string"),
30
+ "label": datasets.ClassLabel(names=ESC50_LABELS),
31
+ }
32
+ ),
33
+ name="fold1",
34
+ description='',
35
+ ),
36
+ ]
37
+
38
+ def _info(self):
39
+ return datasets.DatasetInfo(
40
+ description='Environmental sound classification dataset',
41
+ features=self.config.features,
42
+ )
43
+
44
+ def _split_generators(self, dl_manager):
45
+ data_dir = dl_manager.download_and_extract("ESC-50-master.zip")
46
+ return [
47
+ datasets.SplitGenerator(
48
+ name=datasets.Split.TRAIN,
49
+ gen_kwargs={
50
+ "data_dir": data_dir,
51
+ "split": 'train'
52
+ },
53
+ ),
54
+ datasets.SplitGenerator(
55
+ name=datasets.Split.TEST,
56
+ gen_kwargs={
57
+ "data_dir": data_dir,
58
+ "split": 'test'
59
+ },
60
+ ),
61
+ ]
62
+
63
+ def _generate_examples(self, data_dir, split):
64
+ _, _walker = fast_scandir(data_dir, [EXTENSION], recursive=True)
65
+
66
+ test_fold = self.config.name
67
+ train_fold = [f'fold{f}' for f in range(1, 11)]
68
+ train_fold.remove(test_fold)
69
+
70
+ for idx, fileid in enumerate(_walker):
71
+ fold = default_find_fold(fileid)
72
+ if split == 'train' and f'fold{fold}' in train_fold:
73
+ yield idx, {
74
+ 'file': fileid,
75
+ 'audio': fileid,
76
+ 'sound': default_find_classes(fileid),
77
+ 'label': default_find_classes(fileid)
78
+ }
79
+ elif split == 'test' and f'fold{fold}' in test_fold:
80
+ yield idx, {
81
+ 'file': fileid,
82
+ 'audio': fileid,
83
+ 'sound': default_find_classes(fileid),
84
+ 'label': default_find_classes(fileid)
85
+ }
86
+
87
+
88
+ def default_find_classes(audio_path):
89
+ id_ = Path(audio_path).stem.split('-')[-1]
90
+ return ESC50_ID2LABEL.get(int(id_))
91
+
92
+
93
+ def default_find_fold(audio_path):
94
+ fold = Path(audio_path).stem.split('-')[0]
95
+ return int(fold)
96
+
97
+
98
+ def fast_scandir(path: str, exts: tp.List[str], recursive: bool = False):
99
+ # Scan files recursively faster than glob
100
+ # From github.com/drscotthawley/aeiou/blob/main/aeiou/core.py
101
+ subfolders, files = [], []
102
+
103
+ try: # hope to avoid 'permission denied' by this try
104
+ for f in os.scandir(path):
105
+ try: # 'hope to avoid too many levels of symbolic links' error
106
+ if f.is_dir():
107
+ subfolders.append(f.path)
108
+ elif f.is_file():
109
+ if os.path.splitext(f.name)[1].lower() in exts:
110
+ files.append(f.path)
111
+ except Exception:
112
+ pass
113
+ except Exception:
114
+ pass
115
+
116
+ if recursive:
117
+ for path in list(subfolders):
118
+ sf, f = fast_scandir(path, exts, recursive=recursive)
119
+ subfolders.extend(sf)
120
+ files.extend(f) # type: ignore
121
+
122
+ return subfolders, files
123
+
124
+
125
+ ESC50_LABELS = [
126
+ "airplane",
127
+ "breathing",
128
+ "brushing_teeth",
129
+ "can_opening",
130
+ "car_horn",
131
+ "cat",
132
+ "chainsaw",
133
+ "chirping_birds",
134
+ "church_bells",
135
+ "clapping",
136
+ "clock_alarm",
137
+ "clock_tick",
138
+ "coughing",
139
+ "cow",
140
+ "crackling_fire",
141
+ "crickets",
142
+ "crow",
143
+ "crying_baby",
144
+ "dog",
145
+ "door_wood_creaks",
146
+ "door_wood_knock",
147
+ "drinking_sipping",
148
+ "engine",
149
+ "fireworks",
150
+ "footsteps",
151
+ "frog",
152
+ "glass_breaking",
153
+ "hand_saw",
154
+ "helicopter",
155
+ "hen",
156
+ "insects",
157
+ "keyboard_typing",
158
+ "laughing",
159
+ "mouse_click",
160
+ "pig",
161
+ "pouring_water",
162
+ "rain",
163
+ "rooster",
164
+ "sea_waves",
165
+ "sheep",
166
+ "siren",
167
+ "sneezing",
168
+ "snoring",
169
+ "thunderstorm",
170
+ "toilet_flush",
171
+ "train",
172
+ "vacuum_cleaner",
173
+ "washing_machine",
174
+ "water_drops",
175
+ "wind",
176
+ ]
177
+
178
+ ESC50_LABEL2ID = {
179
+ "dog": 0,
180
+ "rooster": 1,
181
+ "pig": 2,
182
+ "cow": 3,
183
+ "frog": 4,
184
+ "cat": 5,
185
+ "hen": 6,
186
+ "insects": 7,
187
+ "sheep": 8,
188
+ "crow": 9,
189
+ "rain": 10,
190
+ "sea_waves": 11,
191
+ "crackling_fire": 12,
192
+ "crickets": 13,
193
+ "chirping_birds": 14,
194
+ "water_drops": 15,
195
+ "wind": 16,
196
+ "pouring_water": 17,
197
+ "toilet_flush": 18,
198
+ "thunderstorm": 19,
199
+ "crying_baby": 20,
200
+ "sneezing": 21,
201
+ "clapping": 22,
202
+ "breathing": 23,
203
+ "coughing": 24,
204
+ "footsteps": 25,
205
+ "laughing": 26,
206
+ "brushing_teeth": 27,
207
+ "snoring": 28,
208
+ "drinking_sipping": 29,
209
+ "door_wood_knock": 30,
210
+ "mouse_click": 31,
211
+ "keyboard_typing": 32,
212
+ "door_wood_creaks": 33,
213
+ "can_opening": 34,
214
+ "washing_machine": 35,
215
+ "vacuum_cleaner": 36,
216
+ "clock_alarm": 37,
217
+ "clock_tick": 38,
218
+ "glass_breaking": 39,
219
+ "helicopter": 40,
220
+ "chainsaw": 41,
221
+ "siren": 42,
222
+ "car_horn": 43,
223
+ "engine": 44,
224
+ "train": 45,
225
+ "church_bells": 46,
226
+ "airplane": 47,
227
+ "fireworks": 48,
228
+ "hand_saw": 49
229
+ }
230
+ ESC50_ID2LABEL = {v:k for k, v in ESC50_LABEL2ID.items()}