File size: 12,705 Bytes
e7dd21d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8
# Copyright 2022 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
#
# 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.

# Lint as: python3
"""
Librispeech automatic speech recognition dataset for reproducing Reborn UASR results.
Note that the silence in each audio has been removed by performing unsupervised VAD (https://github.com/zhenghuatan/rVADfast).
We only process the 100-hour split from LibriSpeech 'train-clean-100' as the training split.
"""

import os

import datasets


_CITATION = """\
@inproceedings{panayotov2015librispeech,
  title={Librispeech: an ASR corpus based on public domain audio books},
  author={Panayotov, Vassil and Chen, Guoguo and Povey, Daniel and Khudanpur, Sanjeev},
  booktitle={Acoustics, Speech and Signal Processing (ICASSP), 2015 IEEE International Conference on},
  pages={5206--5210},
  year={2015},
  organization={IEEE}
}
@article{tan2020rvad,
  title={rVAD: An unsupervised segment-based robust voice activity detection method},
  author={Tan, Zheng-Hua and Dehak, Najim and others},
  journal={Computer speech \& language},
  volume={59},
  pages={1--21},
  year={2020},
  publisher={Elsevier}
}
@article{tseng2024reborn,
  title={REBORN: Reinforcement-Learned Boundary Segmentation with Iterative Training for Unsupervised ASR},
  author={Tseng, Liang-Hsuan and Hu, En-Pei and Chiang, Cheng-Han and Tseng, Yuan and Lee, Hung-yi and Lee, Lin-shan and Sun, Shao-Hua},
  journal={arXiv preprint arXiv:2402.03988},
  year={2024}
}
"""

_DESCRIPTION = """\
LibriSpeech is a corpus of approximately 1000 hours of read English speech with sampling rate of 16 kHz,
prepared by Vassil Panayotov with the assistance of Daniel Povey. The data is derived from read
audiobooks from the LibriVox project, and has been carefully segmented and aligned

This dataset is the 100-hour subset of LibriSpeech 'train-clean-100' split, with silence removed.
Additionally, all the dev and test sets are included for fair comparison and evaluation if needed.
The dataset is prepared by the Reborn UASR team. 
Arxiv paper link: https://arxiv.org/abs/2402.03988
"""

_URL = "http://www.openslr.org/12"

_DL_URL_FORMAT = "data"


class RebornLibrispeechConfig(datasets.BuilderConfig):
    """BuilderConfig for Reborn-Librispeech."""

    def __init__(self, name, **kwargs):
        """
        Args:
          name: `string`, name of dataset config (=language)
          **kwargs: keyword arguments forwarded to super.
        """
        super(RebornLibrispeechConfig, self).__init__(
            version=datasets.Version("2.12.0", ""), name=name, **kwargs
        )
        # relative path to full data inside a repo (for example `data/train-clean-100`)
        self.data_root_url = _DL_URL_FORMAT


class RebornLibrispeech(datasets.GeneratorBasedBuilder):
    """Multilingual Librispeech dataset."""

    BUILDER_CONFIGS = [
        RebornLibrispeechConfig(name="reborn_ls100hr", description="train-clean-100 LibriSpeech dataset without silence"),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "file": datasets.Value("string"),
                    "audio": datasets.features.Audio(sampling_rate=16_000),
                    "word": datasets.Value("string"),
                    "phoneme": datasets.Value("string"),
                    "speaker_id": datasets.Value("int64"),
                    "chapter_id": datasets.Value("int64"),
                    "id": datasets.Value("string"),
                }
            ),
            supervised_keys=("file", "phone"),
            homepage=_URL,
            citation=_CITATION,
            task_templates=None,
        )

    def _split_generators(self, dl_manager):
        
        metadata = dl_manager.download({
            "train-clean-100": self.config.data_root_url + "/metadata/train-clean-100.tsv",
            "dev-clean": self.config.data_root_url + "/metadata/dev-clean.tsv",
            "dev-clean-small": self.config.data_root_url + "/metadata/dev-clean-small.tsv",
            "dev-other": self.config.data_root_url + "/metadata/dev-other.tsv",
            "test-clean": self.config.data_root_url + "/metadata/test-clean.tsv",
            "test-other": self.config.data_root_url + "/metadata/test-other.tsv",
        })

        all_splits = [
            "train-clean-100",
            "dev-clean",
            "dev-other",
            "test-clean",
            "test-other",
        ]

        # # Download handles.txt files containing ids for limited supervision train sets
        # limited_supervision_9h = dl_manager.download(
        #     [self.config.data_root_url + "/train/limited_supervision/9hr/handles.txt"],
        # )
        # # in our case of 1 hour limited supervision ("train.1h") there are always 6 subfolders like:
        # # "limited_supervision/1h/0/handles.txt", "limited_supervision/1h/1/handles.txt", ...
        # limited_supervision_1h = dl_manager.download([
        #     self.config.data_root_url + f"/train/limited_supervision/1hr/{i}/handles.txt" for i in range(6)
        # ])
        
        # each split contains many .tar.gz archives with its audio files
        # audio_filenames.txt contains the names of these archives
        # audio_filenames_paths = dl_manager.download({
        #     "train": self.config.data_root_url + "/train/audio_filenames.txt",
        #     "dev": self.config.data_root_url + "/dev/audio_filenames.txt",
        #     "test": self.config.data_root_url + "/test/audio_filenames.txt",
        # })

        audio_archives = {}
        for split in all_splits:
            audio_archives[split] = dl_manager.download(
                os.path.join(self.config.data_root_url, f"{split}.tar.gz")
            )

        # (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files:
        local_extracted_archives = dl_manager.extract(audio_archives) if not dl_manager.is_streaming else {}

        train_splits = [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "metadata_fpaths": [metadata["train-clean-100"]],
                    "audio_archives": [dl_manager.iter_archive(audio_archives["train"])],
                    "local_extracted_archives": [local_extracted_archives.get("train")],
                }
            ),
            datasets.SplitGenerator(
                name="train-clean-100",
                gen_kwargs={
                    "metadata_fpaths": [metadata["train-clean-100"]],
                    "audio_archives": [dl_manager.iter_archive(audio_archives["train"])],
                    "local_extracted_archives": [local_extracted_archives.get("train")],
                }
            ),
        ]

        dev_splits = [
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "metadata_fpath": [metadata["dev-clean"], metadata["dev-other"]],
                    "audio_archives": [dl_manager.iter_archive(audio_archives["dev-clean"]), dl_manager.iter_archive(audio_archives["dev-other"])],
                    "local_extracted_archives": [local_extracted_archives.get("dev-clean"), local_extracted_archives.get("dev-other")],
                }
            ),
            datasets.SplitGenerator(
                name="dev-clean",
                gen_kwargs={
                    "metadata_fpaths": [metadata["dev-clean"]],
                    "audio_archives": [dl_manager.iter_archive(audio_archives["dev-clean"])],
                    "local_extracted_archives": [local_extracted_archives.get("dev-clean")],
                },
            ),
            datasets.SplitGenerator(
                name="dev-other",
                gen_kwargs={
                    "metadata_fpaths": [metadata["dev-other"]],
                    "audio_archives": [dl_manager.iter_archive(audio_archives["dev-other"])],
                    "local_extracted_archives": [local_extracted_archives.get("dev-other")],
                },
            ),
            datasets.SplitGenerator(
                name="dev-clean-small",
                gen_kwargs={
                    "metadata_fpaths": [metadata["dev-clean-small"]],
                    "audio_archives": [dl_manager.iter_archive(audio_archives["dev-clean"])],
                    "local_extracted_archives": [local_extracted_archives.get("dev-clean")],
                },
            ),
        ]

        test_splits = [
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "metadata_fpaths": [metadata["test-clean"], metadata["test-other"]],
                    "audio_archives": [dl_manager.iter_archive(audio_archives["test-clean"]), dl_manager.iter_archive(audio_archives["test-other"])],
                    "local_extracted_archives": [local_extracted_archives.get("test-clean"), local_extracted_archives.get("test-other")],
                }
            ),
            datasets.SplitGenerator(
                name="test-clean",
                gen_kwargs={
                    "metadata_fpaths": [metadata["test-clean"]],
                    "audio_archives": [dl_manager.iter_archive(audio_archives["test-clean"])],
                    "local_extracted_archives": [local_extracted_archives.get("test-clean")],
                }
            ),   
            datasets.SplitGenerator(
                name="test-other",
                gen_kwargs={
                    "metadata_fpaths": [metadata["test-other"]],
                    "audio_archives": [dl_manager.iter_archive(audio_archives["test-other"])],
                    "local_extracted_archives": [local_extracted_archives.get("test-other")],
                }
            ),   
        ]

        return train_splits + dev_splits + test_splits

    def _generate_examples(self, metadata_fpaths, audio_archives, local_extracted_archives):
        """Generate examples from a Multilingual LibriSpeech data dir."""
        words, phones = dict(), dict()
        for metadata_fpath in metadata_fpaths:
            with open(metadata_fpath, "r", encoding="utf-8") as file:
                for line in file:
                    audio_fpath, word, phone = line.strip().split("\t")
                    audio_id = audio_fpath.split('/')[-1].split(".flac")[0]
                    words[audio_id] = word
                    phones[audio_id] = phone

        # limited_ids, limited_ids_archives_names = [], []
        # if limited_ids_paths:
        #     for path in limited_ids_paths:
        #         with open(path, "r", encoding="utf-8") as file:
        #             limited_ids.extend([line.strip() for line in file.readlines()])

            # limited_ids = set(limited_ids)

        for archive_idx, audio_archive in enumerate(audio_archives):
            #  TODO: check that archive doesn't contain needed ids
            # if limited_ids and audio_archive not in limited_ids_archives_names:
            #     continue

            for audio_filename, file in audio_archive:
                audio_id = audio_filename.split('/')[-1].split(".flac")[0]
                speaker_id, chapter_id = (int(item) for item in audio_id.split("-")[:2])
                word = words.get(audio_id, None)
                if word == None: 
                    continue

                local_audio_file_path = os.path.join(
                    local_extracted_archives[archive_idx], audio_filename
                ) if local_extracted_archives[archive_idx] else None

                yield audio_filename, {
                    "file": local_audio_file_path,
                    "audio": {
                        "path": local_audio_file_path if local_audio_file_path else audio_filename,
                        "bytes": file.read()
                    },
                    "word": word,
                    "phoneme": phones.get(audio_id, None),
                    "speaker_id": speaker_id,
                    "chapter_id": chapter_id,
                    "id": audio_id
                }