Datasets:
Delete loading script
Browse files- librispeech_asr.py +0 -279
librispeech_asr.py
DELETED
|
@@ -1,279 +0,0 @@
|
|
| 1 |
-
# coding=utf-8
|
| 2 |
-
# Copyright 2021 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
| 3 |
-
#
|
| 4 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
-
# you may not use this file except in compliance with the License.
|
| 6 |
-
# You may obtain a copy of the License at
|
| 7 |
-
#
|
| 8 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
-
#
|
| 10 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
-
# See the License for the specific language governing permissions and
|
| 14 |
-
# limitations under the License.
|
| 15 |
-
|
| 16 |
-
# Lint as: python3
|
| 17 |
-
"""Librispeech automatic speech recognition dataset."""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
import os
|
| 21 |
-
|
| 22 |
-
import datasets
|
| 23 |
-
from datasets.tasks import AutomaticSpeechRecognition
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
_CITATION = """\
|
| 27 |
-
@inproceedings{panayotov2015librispeech,
|
| 28 |
-
title={Librispeech: an ASR corpus based on public domain audio books},
|
| 29 |
-
author={Panayotov, Vassil and Chen, Guoguo and Povey, Daniel and Khudanpur, Sanjeev},
|
| 30 |
-
booktitle={Acoustics, Speech and Signal Processing (ICASSP), 2015 IEEE International Conference on},
|
| 31 |
-
pages={5206--5210},
|
| 32 |
-
year={2015},
|
| 33 |
-
organization={IEEE}
|
| 34 |
-
}
|
| 35 |
-
"""
|
| 36 |
-
|
| 37 |
-
_DESCRIPTION = """\
|
| 38 |
-
LibriSpeech is a corpus of approximately 1000 hours of read English speech with sampling rate of 16 kHz,
|
| 39 |
-
prepared by Vassil Panayotov with the assistance of Daniel Povey. The data is derived from read
|
| 40 |
-
audiobooks from the LibriVox project, and has been carefully segmented and aligned.87
|
| 41 |
-
"""
|
| 42 |
-
|
| 43 |
-
_URL = "http://www.openslr.org/12"
|
| 44 |
-
_DL_URL = "http://www.openslr.org/resources/12/"
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
_DL_URLS = {
|
| 48 |
-
"clean": {
|
| 49 |
-
"dev": _DL_URL + "dev-clean.tar.gz",
|
| 50 |
-
"test": _DL_URL + "test-clean.tar.gz",
|
| 51 |
-
"train.100": _DL_URL + "train-clean-100.tar.gz",
|
| 52 |
-
"train.360": _DL_URL + "train-clean-360.tar.gz",
|
| 53 |
-
},
|
| 54 |
-
"other": {
|
| 55 |
-
"test": _DL_URL + "test-other.tar.gz",
|
| 56 |
-
"dev": _DL_URL + "dev-other.tar.gz",
|
| 57 |
-
"train.500": _DL_URL + "train-other-500.tar.gz",
|
| 58 |
-
},
|
| 59 |
-
"all": {
|
| 60 |
-
"dev.clean": _DL_URL + "dev-clean.tar.gz",
|
| 61 |
-
"dev.other": _DL_URL + "dev-other.tar.gz",
|
| 62 |
-
"test.clean": _DL_URL + "test-clean.tar.gz",
|
| 63 |
-
"test.other": _DL_URL + "test-other.tar.gz",
|
| 64 |
-
"train.clean.100": _DL_URL + "train-clean-100.tar.gz",
|
| 65 |
-
"train.clean.360": _DL_URL + "train-clean-360.tar.gz",
|
| 66 |
-
"train.other.500": _DL_URL + "train-other-500.tar.gz",
|
| 67 |
-
},
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
class LibrispeechASRConfig(datasets.BuilderConfig):
|
| 72 |
-
"""BuilderConfig for LibriSpeechASR."""
|
| 73 |
-
|
| 74 |
-
def __init__(self, **kwargs):
|
| 75 |
-
"""
|
| 76 |
-
Args:
|
| 77 |
-
data_dir: `string`, the path to the folder containing the files in the
|
| 78 |
-
downloaded .tar
|
| 79 |
-
citation: `string`, citation for the data set
|
| 80 |
-
url: `string`, url for information about the data set
|
| 81 |
-
**kwargs: keyword arguments forwarded to super.
|
| 82 |
-
"""
|
| 83 |
-
super(LibrispeechASRConfig, self).__init__(version=datasets.Version("2.1.0", ""), **kwargs)
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
class LibrispeechASR(datasets.GeneratorBasedBuilder):
|
| 87 |
-
"""Librispeech dataset."""
|
| 88 |
-
|
| 89 |
-
DEFAULT_WRITER_BATCH_SIZE = 256
|
| 90 |
-
DEFAULT_CONFIG_NAME = "all"
|
| 91 |
-
BUILDER_CONFIGS = [
|
| 92 |
-
LibrispeechASRConfig(name="clean", description="'Clean' speech."),
|
| 93 |
-
LibrispeechASRConfig(name="other", description="'Other', more challenging, speech."),
|
| 94 |
-
LibrispeechASRConfig(name="all", description="Combined clean and other dataset."),
|
| 95 |
-
]
|
| 96 |
-
|
| 97 |
-
def _info(self):
|
| 98 |
-
return datasets.DatasetInfo(
|
| 99 |
-
description=_DESCRIPTION,
|
| 100 |
-
features=datasets.Features(
|
| 101 |
-
{
|
| 102 |
-
"file": datasets.Value("string"),
|
| 103 |
-
"audio": datasets.Audio(sampling_rate=16_000),
|
| 104 |
-
"text": datasets.Value("string"),
|
| 105 |
-
"speaker_id": datasets.Value("int64"),
|
| 106 |
-
"chapter_id": datasets.Value("int64"),
|
| 107 |
-
"id": datasets.Value("string"),
|
| 108 |
-
}
|
| 109 |
-
),
|
| 110 |
-
supervised_keys=("file", "text"),
|
| 111 |
-
homepage=_URL,
|
| 112 |
-
citation=_CITATION,
|
| 113 |
-
task_templates=[AutomaticSpeechRecognition(audio_column="audio", transcription_column="text")],
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
def _split_generators(self, dl_manager):
|
| 117 |
-
archive_path = dl_manager.download(_DL_URLS[self.config.name])
|
| 118 |
-
# (Optional) In non-streaming mode, we can extract the archive locally to have actual local audio files:
|
| 119 |
-
local_extracted_archive = dl_manager.extract(archive_path) if not dl_manager.is_streaming else {}
|
| 120 |
-
|
| 121 |
-
if self.config.name == "clean":
|
| 122 |
-
train_splits = [
|
| 123 |
-
datasets.SplitGenerator(
|
| 124 |
-
name="train.100",
|
| 125 |
-
gen_kwargs={
|
| 126 |
-
"local_extracted_archive": local_extracted_archive.get("train.100"),
|
| 127 |
-
"files": dl_manager.iter_archive(archive_path["train.100"]),
|
| 128 |
-
},
|
| 129 |
-
),
|
| 130 |
-
datasets.SplitGenerator(
|
| 131 |
-
name="train.360",
|
| 132 |
-
gen_kwargs={
|
| 133 |
-
"local_extracted_archive": local_extracted_archive.get("train.360"),
|
| 134 |
-
"files": dl_manager.iter_archive(archive_path["train.360"]),
|
| 135 |
-
},
|
| 136 |
-
),
|
| 137 |
-
]
|
| 138 |
-
dev_splits = [
|
| 139 |
-
datasets.SplitGenerator(
|
| 140 |
-
name=datasets.Split.VALIDATION,
|
| 141 |
-
gen_kwargs={
|
| 142 |
-
"local_extracted_archive": local_extracted_archive.get("dev"),
|
| 143 |
-
"files": dl_manager.iter_archive(archive_path["dev"]),
|
| 144 |
-
},
|
| 145 |
-
)
|
| 146 |
-
]
|
| 147 |
-
test_splits = [
|
| 148 |
-
datasets.SplitGenerator(
|
| 149 |
-
name=datasets.Split.TEST,
|
| 150 |
-
gen_kwargs={
|
| 151 |
-
"local_extracted_archive": local_extracted_archive.get("test"),
|
| 152 |
-
"files": dl_manager.iter_archive(archive_path["test"]),
|
| 153 |
-
},
|
| 154 |
-
)
|
| 155 |
-
]
|
| 156 |
-
elif self.config.name == "other":
|
| 157 |
-
train_splits = [
|
| 158 |
-
datasets.SplitGenerator(
|
| 159 |
-
name="train.500",
|
| 160 |
-
gen_kwargs={
|
| 161 |
-
"local_extracted_archive": local_extracted_archive.get("train.500"),
|
| 162 |
-
"files": dl_manager.iter_archive(archive_path["train.500"]),
|
| 163 |
-
},
|
| 164 |
-
)
|
| 165 |
-
]
|
| 166 |
-
dev_splits = [
|
| 167 |
-
datasets.SplitGenerator(
|
| 168 |
-
name=datasets.Split.VALIDATION,
|
| 169 |
-
gen_kwargs={
|
| 170 |
-
"local_extracted_archive": local_extracted_archive.get("dev"),
|
| 171 |
-
"files": dl_manager.iter_archive(archive_path["dev"]),
|
| 172 |
-
},
|
| 173 |
-
)
|
| 174 |
-
]
|
| 175 |
-
test_splits = [
|
| 176 |
-
datasets.SplitGenerator(
|
| 177 |
-
name=datasets.Split.TEST,
|
| 178 |
-
gen_kwargs={
|
| 179 |
-
"local_extracted_archive": local_extracted_archive.get("test"),
|
| 180 |
-
"files": dl_manager.iter_archive(archive_path["test"]),
|
| 181 |
-
},
|
| 182 |
-
)
|
| 183 |
-
]
|
| 184 |
-
elif self.config.name == "all":
|
| 185 |
-
train_splits = [
|
| 186 |
-
datasets.SplitGenerator(
|
| 187 |
-
name="train.clean.100",
|
| 188 |
-
gen_kwargs={
|
| 189 |
-
"local_extracted_archive": local_extracted_archive.get("train.clean.100"),
|
| 190 |
-
"files": dl_manager.iter_archive(archive_path["train.clean.100"]),
|
| 191 |
-
},
|
| 192 |
-
),
|
| 193 |
-
datasets.SplitGenerator(
|
| 194 |
-
name="train.clean.360",
|
| 195 |
-
gen_kwargs={
|
| 196 |
-
"local_extracted_archive": local_extracted_archive.get("train.clean.360"),
|
| 197 |
-
"files": dl_manager.iter_archive(archive_path["train.clean.360"]),
|
| 198 |
-
},
|
| 199 |
-
),
|
| 200 |
-
datasets.SplitGenerator(
|
| 201 |
-
name="train.other.500",
|
| 202 |
-
gen_kwargs={
|
| 203 |
-
"local_extracted_archive": local_extracted_archive.get("train.other.500"),
|
| 204 |
-
"files": dl_manager.iter_archive(archive_path["train.other.500"]),
|
| 205 |
-
},
|
| 206 |
-
),
|
| 207 |
-
]
|
| 208 |
-
dev_splits = [
|
| 209 |
-
datasets.SplitGenerator(
|
| 210 |
-
name="validation.clean",
|
| 211 |
-
gen_kwargs={
|
| 212 |
-
"local_extracted_archive": local_extracted_archive.get("dev.clean"),
|
| 213 |
-
"files": dl_manager.iter_archive(archive_path["dev.clean"]),
|
| 214 |
-
},
|
| 215 |
-
),
|
| 216 |
-
datasets.SplitGenerator(
|
| 217 |
-
name="validation.other",
|
| 218 |
-
gen_kwargs={
|
| 219 |
-
"local_extracted_archive": local_extracted_archive.get("dev.other"),
|
| 220 |
-
"files": dl_manager.iter_archive(archive_path["dev.other"]),
|
| 221 |
-
},
|
| 222 |
-
),
|
| 223 |
-
]
|
| 224 |
-
test_splits = [
|
| 225 |
-
datasets.SplitGenerator(
|
| 226 |
-
name="test.clean",
|
| 227 |
-
gen_kwargs={
|
| 228 |
-
"local_extracted_archive": local_extracted_archive.get("test.clean"),
|
| 229 |
-
"files": dl_manager.iter_archive(archive_path["test.clean"]),
|
| 230 |
-
},
|
| 231 |
-
),
|
| 232 |
-
datasets.SplitGenerator(
|
| 233 |
-
name="test.other",
|
| 234 |
-
gen_kwargs={
|
| 235 |
-
"local_extracted_archive": local_extracted_archive.get("test.other"),
|
| 236 |
-
"files": dl_manager.iter_archive(archive_path["test.other"]),
|
| 237 |
-
},
|
| 238 |
-
),
|
| 239 |
-
]
|
| 240 |
-
|
| 241 |
-
return train_splits + dev_splits + test_splits
|
| 242 |
-
|
| 243 |
-
def _generate_examples(self, files, local_extracted_archive):
|
| 244 |
-
"""Generate examples from a LibriSpeech archive_path."""
|
| 245 |
-
key = 0
|
| 246 |
-
audio_data = {}
|
| 247 |
-
transcripts = []
|
| 248 |
-
for path, f in files:
|
| 249 |
-
if path.endswith(".flac"):
|
| 250 |
-
id_ = path.split("/")[-1][: -len(".flac")]
|
| 251 |
-
audio_data[id_] = f.read()
|
| 252 |
-
elif path.endswith(".trans.txt"):
|
| 253 |
-
for line in f:
|
| 254 |
-
if line:
|
| 255 |
-
line = line.decode("utf-8").strip()
|
| 256 |
-
id_, transcript = line.split(" ", 1)
|
| 257 |
-
audio_file = f"{id_}.flac"
|
| 258 |
-
speaker_id, chapter_id = [int(el) for el in id_.split("-")[:2]]
|
| 259 |
-
audio_file = (
|
| 260 |
-
os.path.join(local_extracted_archive, audio_file)
|
| 261 |
-
if local_extracted_archive
|
| 262 |
-
else audio_file
|
| 263 |
-
)
|
| 264 |
-
transcripts.append(
|
| 265 |
-
{
|
| 266 |
-
"id": id_,
|
| 267 |
-
"speaker_id": speaker_id,
|
| 268 |
-
"chapter_id": chapter_id,
|
| 269 |
-
"file": audio_file,
|
| 270 |
-
"text": transcript,
|
| 271 |
-
}
|
| 272 |
-
)
|
| 273 |
-
if audio_data and len(audio_data) == len(transcripts):
|
| 274 |
-
for transcript in transcripts:
|
| 275 |
-
audio = {"path": transcript["file"], "bytes": audio_data[transcript["id"]]}
|
| 276 |
-
yield key, {"audio": audio, **transcript}
|
| 277 |
-
key += 1
|
| 278 |
-
audio_data = {}
|
| 279 |
-
transcripts = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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