File size: 15,325 Bytes
3bb05c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c24f3e
3bb05c0
 
 
 
 
 
 
 
 
 
 
 
1188314
3bb05c0
9c24f3e
a165712
 
9c24f3e
a165712
 
3bb05c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: Address all TODOs and remove all explanatory comments
"""TODO: Add a description here."""


import csv
import json
import os

import datasets

_CITATION = """\
@misc{cooper2021generalization,
    title={Generalization Ability of MOS Prediction Networks}, 
    author={Erica Cooper and Wen-Chin Huang and Tomoki Toda and Junichi Yamagishi},
    year={2021},
    eprint={2110.02635},
    archivePrefix={arXiv},
    primaryClass={eess.AS}
}
"""

# TODO: Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
This dataset is for internal use only. For voicemos challenge
"""

# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = "https://codalab.lisn.upsaclay.fr/competitions/695"

# TODO: Add the licence for the dataset here if you can find it
_LICENSE = "INTERNAL"


class BvccDataset(datasets.GeneratorBasedBuilder):
    """BVCC dataset for voicemos challenge 2022"""

    VERSION = datasets.Version("1.1.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="main_track",
            version=VERSION,
            description="main track dataset by wavfiles",
        ),
        datasets.BuilderConfig(
            name="main_track_listeners",
            version=VERSION,
            description="main track dataset by listener rating",
        ),
        datasets.BuilderConfig(
            name="ood_track", version=VERSION, description="Out of domain dataset"
        ),
        datasets.BuilderConfig(
            name="ood_track_unlabeled",
            version=VERSION,
            description="Out of domain dataset unlabeled",
        ),
        datasets.BuilderConfig(
            name="ood_track_listeners",
            version=VERSION,
            description="ood track dataset by listener rating",
        ),
    ]

    DEFAULT_CONFIG_NAME = "main_track"  # It's not mandatory to have a default configuration. Just use one if it make sense.

    def _info(self):
        # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
        if (
            self.config.name == "main_track"
        ):  # This is the name of the configuration selected in BUILDER_CONFIGS above
            features = datasets.Features(
                {
                    "path": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=16_000),
                    "sysID": datasets.Value("string"),
                    "uttID": datasets.Value("string"),
                    "averaged rating": datasets.Value("float32"),
                    # These are the features of your dataset like images, labels ...
                }
            )
        elif self.config.name == "main_track_listeners":
            # sysID,uttID,rating,ignore,listenerinfo
            # {}_AGERANGE_LISTENERID_GENDER_[ignore]_[ignore]_HEARINGIMPAIRMENT
            features = datasets.Features(
                {
                    "path": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=16_000),
                    "sysID": datasets.Value("string"),
                    "uttID": datasets.Value("string"),
                    "rating": datasets.Value("int8"),
                    "age range": datasets.Value("string"),
                    "listener id": datasets.Value("string"),
                    "gender": datasets.Value("string"),
                    "hearing impairment": datasets.Value("string"),
                }
            )
        elif (
            self.config.name == "ood_track"
        ):  # This is the name of the configuration selected in BUILDER_CONFIGS above
            features = datasets.Features(
                {
                    "path": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=16_000),
                    "sysID": datasets.Value("string"),
                    "uttID": datasets.Value("string"),
                    "averaged rating": datasets.Value("float32"),
                    # These are the features of your dataset like images, labels ...
                }
            )
        elif self.config.name == "ood_track_listeners":
            # sysID,uttID,rating,ignore,listenerinfo
            # {}_AGERANGE_LISTENERID_GENDER_[ignore]_[ignore]_HEARINGIMPAIRMENT
            features = datasets.Features(
                {
                    "path": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=16_000),
                    "sysID": datasets.Value("string"),
                    "uttID": datasets.Value("string"),
                    "rating": datasets.Value("int8"),
                    "age range": datasets.Value("string"),
                    "listener id": datasets.Value("string"),
                    "gender": datasets.Value("string"),
                    "hearing impairment": datasets.Value("string"),
                }
            )
        elif self.config.name == "ood_track_unlabeled":
            # sysID,uttID,rating,ignore,listenerinfo
            # {}_AGERANGE_LISTENERID_GENDER_[ignore]_[ignore]_HEARINGIMPAIRMENT
            features = datasets.Features(
                {
                    "path": datasets.Value("string"),
                    "audio": datasets.Audio(sampling_rate=16_000),
                    "sysID": datasets.Value("string"),
                    "uttID": datasets.Value("string"),
                }
            )
        else:
            raise ValueError(f"invalid config name {self.config.name}")
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
        # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name

        # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
        # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
        # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
        data_dir = self.config.data_dir
        if "listeners" in self.config.name:
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "filepath": os.path.join(data_dir, "DATA/sets/TRAINSET"),
                        "split": "train",
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "filepath": os.path.join(data_dir, "DATA/sets/DEVSET"),
                        "split": "dev",
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "filepath": os.path.join(data_dir, "DATA/sets/TESTSET"),
                        "split": "test",
                    },
                ),
            ]
        elif "unlabeled" in self.config.name:
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "filepath": os.path.join(
                            data_dir, "DATA/sets/unlabeled_mos_list.txt"
                        ),
                        "split": "train",
                    },
                ),
            ]
        else:
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "filepath": os.path.join(
                            data_dir, "DATA/sets/train_mos_list.txt"
                        ),
                        "split": "train",
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "filepath": os.path.join(
                            data_dir, "DATA/sets/val_mos_list.txt"
                        ),
                        "split": "dev",
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    # These kwargs will be passed to _generate_examples
                    gen_kwargs={
                        "filepath": os.path.join(data_dir, "DATA/sets/test_mos_list.txt"),
                        "split": "test",
                    },
                ),
            ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepath, split):
        # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
        # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
        with open(filepath, encoding="utf-8") as f:
            for key, row in enumerate(f.readlines()):
                data = row.strip().split(",")
                print(data)
                if self.config.name == "main_track":
                    sysID, uttID = data[0].split("-")
                    uttID = uttID.replace(".wav", "")
                    if len(data) > 1:
                        score = data[1]
                    else:
                        score = 999
                    # Yields examples as (key, example) tuples
                    path = os.path.join(self.config.data_dir, "DATA/wav/", data[0])
                    yield key, {
                        "path": path,
                        "audio": path,
                        "sysID": sysID,
                        "uttID": uttID,
                        "averaged rating": score,
                    }
                elif self.config.name == "main_track_listeners":
                    if len(data) > 1:
                        rating = data[1]
                        sysID, path, rating, _, listenerinfo = data
                        _, age, listenrID, gender, _, _, hearingImpairement = (
                            listenerinfo.split("_")
                        )
                    else:
                        sysID, uttID = data[0].split("-")
                        uttID = uttID.replace(".wav", "")
                        rating = 999
                        age = 999
                        listenrID = 999
                        gender = 999
                        path = data[0]
                    uttID = path.split("-")[-1]
                    uttID = uttID.replace(".wav", "")
                    path = os.path.join(self.config.data_dir, "DATA/wav/", path)
                    yield key, {
                        "path": path,
                        "audio": path,
                        "sysID": sysID,
                        "uttID": uttID,
                        "rating": rating,
                        "age range": age,
                        "listener id": listenrID,
                        "gender": gender,
                        "hearing impairment": hearingImpairement,
                    }
                if self.config.name == "ood_track":
                    sysID, uttID = data[0].split("-")
                    uttID = uttID.replace(".wav", "")
                    if len(data) > 1:
                        score = data[1]
                    else:
                        score = 999
                    # Yields examples as (key, example) tuples
                    path = os.path.join(self.config.data_dir, "DATA/wav/", data[0])
                    yield key, {
                        "path": path,
                        "audio": path,
                        "sysID": sysID,
                        "uttID": uttID,
                        "averaged rating": score,
                    }
                elif self.config.name == "ood_track_listeners":
                    if len(data) > 1:
                        rating = data[1]
                        sysID, path, rating, _, listenerinfo = data
                        _, age, listenrID, gender, _, _, hearingImpairement = (
                            listenerinfo.split("_")
                        )
                    else:
                        sysID, uttID = data[0].split("-")
                        uttID = uttID.replace(".wav", "")
                        path = data[0]
                        rating = 999
                        age = 999
                        listenrID = 999
                        gender = 999
                    uttID = path.split("-")[-1]
                    uttID = uttID.replace(".wav", "")
                    path = os.path.join(self.config.data_dir, "DATA/wav/", path)
                    yield key, {
                        "path": path,
                        "audio": path,
                        "sysID": sysID,
                        "uttID": uttID,
                        "rating": rating,
                        "age range": age,
                        "listener id": listenrID,
                        "gender": gender,
                        "hearing impairment": hearingImpairement,
                    }
                if self.config.name == "ood_track_unlabeled":
                    sysID, uttID = data[0].strip().split("-")
                    uttID = uttID.replace(".wav", "")
                    # Yields examples as (key, example) tuples
                    path = os.path.join(
                        self.config.data_dir, "DATA/wav/", data[0].strip()
                    )
                    yield key, {
                        "path": path,
                        "audio": path,
                        "sysID": sysID,
                        "uttID": uttID,
                    }