yangwang825 commited on
Commit
39620da
·
verified ·
1 Parent(s): 9b3e6b8

Create mswc.py

Browse files
Files changed (1) hide show
  1. mswc.py +166 -0
mswc.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+
3
+ """MSWC keyword spotting classification dataset."""
4
+
5
+
6
+ import os
7
+ import textwrap
8
+ import datasets
9
+ import itertools
10
+ import typing as tp
11
+ from pathlib import Path
12
+
13
+ from ._mswc import (
14
+ TRAIN_ENG, VALIDATION_ENG, TEST_ENG,
15
+ TRAIN_SPA, VALIDATION_SPA, TEST_SPA,
16
+ TRAIN_IND, VALIDATION_IND, TEST_IND,
17
+ )
18
+
19
+ FOLDER_IN_ARCHIVE = "genres"
20
+ SAMPLE_RATE = 16_000
21
+
22
+ _ENG_FILENAME = 'eng-kw-archive.tar.gz'
23
+ _SPA_FILENAME = 'spa-kw-archive.tar.gz'
24
+ _IND_FILENAME = 'ind-kw-archive.tar.gz'
25
+
26
+ CLASS_ENG = set([fileid.split('_')[0] for fileid in TRAIN_ENG])
27
+ CLASS_SPA = set([fileid.split('_')[0] for fileid in TRAIN_SPA])
28
+ CLASS_IND = set([fileid.split('_')[0] for fileid in TRAIN_IND])
29
+
30
+
31
+ class MswcConfig(datasets.BuilderConfig):
32
+ """BuilderConfig for MSWC."""
33
+
34
+ def __init__(self, features, **kwargs):
35
+ super(MswcConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs)
36
+ self.features = features
37
+
38
+
39
+ class MSWC(datasets.GeneratorBasedBuilder):
40
+
41
+ BUILDER_CONFIGS = [
42
+ MswcConfig(
43
+ features=datasets.Features(
44
+ {
45
+ "file": datasets.Value("string"),
46
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
47
+ "keyword": datasets.Value("string"),
48
+ "label": datasets.ClassLabel(names=CLASS_ENG),
49
+ }
50
+ ),
51
+ name="english",
52
+ description=textwrap.dedent(
53
+ """\
54
+ Keyword spotting classifies each audio for its keywords as a multi-class
55
+ classification, where keywords are in the same pre-defined set for both training and testing.
56
+ The evaluation metric is accuracy (ACC).
57
+ """
58
+ ),
59
+ ),
60
+ MswcConfig(
61
+ features=datasets.Features(
62
+ {
63
+ "file": datasets.Value("string"),
64
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
65
+ "keyword": datasets.Value("string"),
66
+ "label": datasets.ClassLabel(names=CLASS_SPA),
67
+ }
68
+ ),
69
+ name="spanish",
70
+ description=textwrap.dedent(
71
+ """\
72
+ Keyword spotting classifies each audio for its keywords as a multi-class
73
+ classification, where keywords are in the same pre-defined set for both training and testing.
74
+ The evaluation metric is accuracy (ACC).
75
+ """
76
+ ),
77
+ ),
78
+ MswcConfig(
79
+ features=datasets.Features(
80
+ {
81
+ "file": datasets.Value("string"),
82
+ "audio": datasets.Audio(sampling_rate=SAMPLE_RATE),
83
+ "keyword": datasets.Value("string"),
84
+ "label": datasets.ClassLabel(names=CLASS_IND),
85
+ }
86
+ ),
87
+ name="indian",
88
+ description=textwrap.dedent(
89
+ """\
90
+ Keyword spotting classifies each audio for its keywords as a multi-class
91
+ classification, where keywords are in the same pre-defined set for both training and testing.
92
+ The evaluation metric is accuracy (ACC).
93
+ """
94
+ ),
95
+ ),
96
+ ]
97
+
98
+ def _info(self):
99
+ return datasets.DatasetInfo(
100
+ description="",
101
+ features=self.config.features,
102
+ supervised_keys=None,
103
+ homepage="",
104
+ citation="",
105
+ task_templates=None,
106
+ )
107
+
108
+ def _split_generators(self, dl_manager):
109
+ """Returns SplitGenerators."""
110
+
111
+ if self.config.name == "english":
112
+ archive_path = dl_manager.extract(_ENG_FILENAME)
113
+ elif self.config.name == "spanish":
114
+ archive_path = dl_manager.extract(_SPA_FILENAME)
115
+ elif self.config.name == "indian":
116
+ archive_path = dl_manager.extract(_IND_FILENAME)
117
+
118
+ return [
119
+ datasets.SplitGenerator(
120
+ name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"}
121
+ ),
122
+ datasets.SplitGenerator(
123
+ name=datasets.Split.VALIDATION, gen_kwargs={"archive_path": archive_path, "split": "validation"}
124
+ ),
125
+ datasets.SplitGenerator(
126
+ name=datasets.Split.TEST, gen_kwargs={"archive_path": archive_path, "split": "test"}
127
+ ),
128
+ ]
129
+
130
+ def _generate_examples(self, archive_path, split=None):
131
+ if self.config.name == 'english':
132
+ extensions = ['.wav']
133
+ _, _walker = fast_scandir(archive_path, extensions, recursive=True)
134
+ if subset == 'train':
135
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in TRAIN_ENG]
136
+ elif subset == 'validation':
137
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in VALIDATION_ENG]
138
+ elif subset == 'test':
139
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in TEST_ENG]
140
+ elif self.config.name == 'spanish':
141
+ extensions = ['.wav']
142
+ _, _walker = fast_scandir(archive_path, extensions, recursive=True)
143
+ if subset == 'train':
144
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in TRAIN_ENG]
145
+ elif subset == 'validation':
146
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in VALIDATION_ENG]
147
+ elif subset == 'test':
148
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in TEST_ENG]
149
+ elif self.config.name == 'indian':
150
+ extensions = ['.wav']
151
+ _, _walker = fast_scandir(archive_path, extensions, recursive=True)
152
+ if subset == 'train':
153
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in TRAIN_ENG]
154
+ elif subset == 'validation':
155
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in VALIDATION_ENG]
156
+ elif subset == 'test':
157
+ _walker = [fileid for fileid in _walker if Path(fileid).stem in TEST_ENG]
158
+
159
+ for guid, audio_path in enumerate(_walker):
160
+ yield guid, {
161
+ "id": str(guid),
162
+ "file": audio_path,
163
+ "audio": audio_path,
164
+ "keyword": Path(audio_path).stem.split('_')[0],
165
+ "label": Path(audio_path).stem.split('_')[0],
166
+ }