Datasets:
File size: 17,382 Bytes
52e0a14 66ef9d0 1046739 66ef9d0 1046739 66ef9d0 1046739 71c47a6 8bb33d1 71c47a6 8bb33d1 71c47a6 8bb33d1 71c47a6 e85eb38 8bb33d1 52e0a14 b1560c5 1046739 eb85a2f 52e0a14 68802b0 52e0a14 68802b0 1046739 deaefb5 1046739 deaefb5 1046739 deaefb5 1046739 8bb33d1 ddcb65a 8bb33d1 71c47a6 52e0a14 |
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# coding=utf-8
# Copyright 2021 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."""
import os
import datasets
from datasets.tasks import AutomaticSpeechRecognition
_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}
}
"""
_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.87
"""
_URL = "http://www.openslr.org/12"
_DL_URL = "http://www.openslr.org/resources/12/"
_DL_URLS = {
"clean": {
"dev": _DL_URL + "dev-clean.tar.gz",
"test": _DL_URL + "test-clean.tar.gz",
"train.100": _DL_URL + "train-clean-100.tar.gz",
"train.360": _DL_URL + "train-clean-360.tar.gz",
},
"other": {
"test": _DL_URL + "test-other.tar.gz",
"dev": _DL_URL + "dev-other.tar.gz",
"train.500": _DL_URL + "train-other-500.tar.gz",
},
"all": {
"dev.clean": _DL_URL + "dev-clean.tar.gz",
"dev.other": _DL_URL + "dev-other.tar.gz",
"test.clean": _DL_URL + "test-clean.tar.gz",
"test.other": _DL_URL + "test-other.tar.gz",
"train.clean.100": _DL_URL + "train-clean-100.tar.gz",
"train.clean.360": _DL_URL + "train-clean-360.tar.gz",
"train.other.500": _DL_URL + "train-other-500.tar.gz",
},
"validation.clean": {
"dev.clean": _DL_URL + "dev-clean.tar.gz",
},
"validation.other": {
"dev.other": _DL_URL + "dev-other.tar.gz",
},
"test.clean": {
"test.clean": _DL_URL + "test-clean.tar.gz",
},
"test.other": {
"test.other": _DL_URL + "test-other.tar.gz",
},
"train.clean.100":{
"train.clean.100": _DL_URL + "train-clean-100.tar.gz",
},
"train.clean.360":{
"train.clean.360": _DL_URL + "train-clean-360.tar.gz",
},
"train.other.500":{
"train.other.500": _DL_URL + "train-other-500.tar.gz",
},
"train.10": {
"train.10": "https://dl.fbaipublicfiles.com/librilight/data/librispeech_finetuning.tgz",
}
}
class LibrispeechASRConfig(datasets.BuilderConfig):
"""BuilderConfig for LibriSpeechASR."""
def __init__(self, **kwargs):
"""
Args:
data_dir: `string`, the path to the folder containing the files in the
downloaded .tar
citation: `string`, citation for the data set
url: `string`, url for information about the data set
**kwargs: keyword arguments forwarded to super.
"""
super(LibrispeechASRConfig, self).__init__(version=datasets.Version("2.1.0", ""), **kwargs)
class LibrispeechASR(datasets.GeneratorBasedBuilder):
"""Librispeech dataset."""
DEFAULT_WRITER_BATCH_SIZE = 256
DEFAULT_CONFIG_NAME = "all"
BUILDER_CONFIGS = [
LibrispeechASRConfig(name="clean", description="'Clean' speech."),
LibrispeechASRConfig(name="other", description="'Other', more challenging, speech."),
LibrispeechASRConfig(name="all", description="Combined clean and other dataset."),
LibrispeechASRConfig(name="validation.clean", description="Validation Clean only"),
LibrispeechASRConfig(name="validation.other", description="Validation Other only"),
LibrispeechASRConfig(name="test.clean", description="Test Clean only"),
LibrispeechASRConfig(name="test.other", description="Test Other only"),
LibrispeechASRConfig(name="train.clean.100", description="train clean 100 only"),
LibrispeechASRConfig(name="train.clean.360", description="train clean 360 only"),
LibrispeechASRConfig(name="train.other.500", description="train other 500 only"),
LibrispeechASRConfig(name="train.10", description="train 10 only"),
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"file": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=16_000),
"text": datasets.Value("string"),
"speaker_id": datasets.Value("int64"),
"chapter_id": datasets.Value("int64"),
"id": datasets.Value("string"),
}
),
supervised_keys=("file", "text"),
homepage=_URL,
citation=_CITATION,
task_templates=[AutomaticSpeechRecognition(audio_column="audio", transcription_column="text")],
)
def _split_generators(self, dl_manager):
archive_path = dl_manager.download(_DL_URLS[self.config.name])
# (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files:
local_extracted_archive = dl_manager.extract(archive_path) if not dl_manager.is_streaming else {}
if self.config.name == "clean":
train_splits = [
datasets.SplitGenerator(
name="train.100",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.100"),
"files": dl_manager.iter_archive(archive_path["train.100"]),
},
),
datasets.SplitGenerator(
name="train.360",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.360"),
"files": dl_manager.iter_archive(archive_path["train.360"]),
},
),
]
dev_splits = [
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("dev"),
"files": dl_manager.iter_archive(archive_path["dev"]),
},
)
]
test_splits = [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("test"),
"files": dl_manager.iter_archive(archive_path["test"]),
},
)
]
elif self.config.name == "other":
train_splits = [
datasets.SplitGenerator(
name="train.500",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.500"),
"files": dl_manager.iter_archive(archive_path["train.500"]),
},
)
]
dev_splits = [
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("dev"),
"files": dl_manager.iter_archive(archive_path["dev"]),
},
)
]
test_splits = [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("test"),
"files": dl_manager.iter_archive(archive_path["test"]),
},
)
]
elif self.config.name == "all":
train_splits = [
datasets.SplitGenerator(
name="train.clean.100",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.clean.100"),
"files": dl_manager.iter_archive(archive_path["train.clean.100"]),
},
),
datasets.SplitGenerator(
name="train.clean.360",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.clean.360"),
"files": dl_manager.iter_archive(archive_path["train.clean.360"]),
},
),
datasets.SplitGenerator(
name="train.other.500",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.other.500"),
"files": dl_manager.iter_archive(archive_path["train.other.500"]),
},
),
]
dev_splits = [
datasets.SplitGenerator(
name="validation.clean",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("dev.clean"),
"files": dl_manager.iter_archive(archive_path["dev.clean"]),
},
),
datasets.SplitGenerator(
name="validation.other",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("dev.other"),
"files": dl_manager.iter_archive(archive_path["dev.other"]),
},
),
]
test_splits = [
datasets.SplitGenerator(
name="test.clean",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("test.clean"),
"files": dl_manager.iter_archive(archive_path["test.clean"]),
},
),
datasets.SplitGenerator(
name="test.other",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("test.other"),
"files": dl_manager.iter_archive(archive_path["test.other"]),
},
),
]
elif self.config.name == "validation.clean":
train_splits = []
dev_splits = [
datasets.SplitGenerator(
name="validation.clean",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("dev.clean"),
"files": dl_manager.iter_archive(archive_path["dev.clean"]),
},
),
]
test_splits = []
elif self.config.name == "validation.other":
train_splits = []
dev_splits = [
datasets.SplitGenerator(
name="validation.other",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("dev.other"),
"files": dl_manager.iter_archive(archive_path["dev.other"]),
},
),
]
test_splits = []
elif self.config.name == "test.clean":
train_splits = []
dev_splits = []
test_splits = [
datasets.SplitGenerator(
name="test.clean",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("test.clean"),
"files": dl_manager.iter_archive(archive_path["test.clean"]),
},
),
]
elif self.config.name == "test.other":
train_splits = []
dev_splits = []
test_splits = [
datasets.SplitGenerator(
name="test.other",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("test.other"),
"files": dl_manager.iter_archive(archive_path["test.other"]),
},
),
]
elif self.config.name == "train.clean.100":
train_splits = [
datasets.SplitGenerator(
name="train.clean.100",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.clean.100"),
"files": dl_manager.iter_archive(archive_path["train.clean.100"]),
},
),
]
dev_splits = []
test_splits = []
elif self.config.name == "train.clean.100":
train_splits = [
datasets.SplitGenerator(
name="train.clean.100",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.clean.100"),
"files": dl_manager.iter_archive(archive_path["train.clean.100"]),
},
),
]
dev_splits = []
test_splits = []
elif self.config.name == "train.clean.360":
train_splits = [
datasets.SplitGenerator(
name="train.clean.360",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.clean.360"),
"files": dl_manager.iter_archive(archive_path["train.clean.360"]),
},
),
]
dev_splits = []
test_splits = []
elif self.config.name == "train.other.500":
train_splits = [
datasets.SplitGenerator(
name="train.other.500",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.other.500"),
"files": dl_manager.iter_archive(archive_path["train.other.500"]),
},
),
]
dev_splits = []
test_splits = []
elif self.config.name == "train.10":
train_splits = [
datasets.SplitGenerator(
name="train.10",
gen_kwargs={
"local_extracted_archive": local_extracted_archive.get("train.10"),
"files": dl_manager.iter_archive(archive_path["train.10"]),
},
),
]
dev_splits = []
test_splits = []
return train_splits + dev_splits + test_splits
def _generate_examples(self, files, local_extracted_archive):
"""Generate examples from a LibriSpeech archive_path."""
key = 0
audio_data = {}
transcripts = []
for path, f in files:
if path.endswith(".flac"):
id_ = path.split("/")[-1][: -len(".flac")]
audio_data[id_] = f.read()
elif path.endswith(".trans.txt"):
for line in f:
if line:
line = line.decode("utf-8").strip()
id_, transcript = line.split(" ", 1)
audio_file = f"{id_}.flac"
speaker_id, chapter_id = [int(el) for el in id_.split("-")[:2]]
audio_file = (
os.path.join(local_extracted_archive, audio_file)
if local_extracted_archive
else audio_file
)
transcripts.append(
{
"id": id_,
"speaker_id": speaker_id,
"chapter_id": chapter_id,
"file": audio_file,
"text": transcript,
}
)
if audio_data and len(audio_data) == len(transcripts):
for transcript in transcripts:
audio = {"path": transcript["file"], "bytes": audio_data[transcript["id"]]}
yield key, {"audio": audio, **transcript}
key += 1
audio_data = {}
transcripts = []
|