sanchit-gandhi
commited on
Commit
·
db84090
1
Parent(s):
aae2b5c
handle sharded data
Browse files- tedlium.py +101 -94
tedlium.py
CHANGED
@@ -68,9 +68,9 @@ def _make_builder_configs():
|
|
68 |
""",
|
69 |
url="https://www.openslr.org/7/",
|
70 |
download_urls={
|
71 |
-
"train": _DL_URL + os.path.join("TEDLIUM_release1", "train.tar.gz"),
|
72 |
-
"validation": _DL_URL + os.path.join("TEDLIUM_release1", "dev.tar.gz"),
|
73 |
-
"test": _DL_URL + os.path.join("TEDLIUM_release1", "test.tar.gz"),
|
74 |
},
|
75 |
split_paths=[
|
76 |
(datasets.Split.TRAIN, "train"),
|
@@ -106,9 +106,9 @@ def _make_builder_configs():
|
|
106 |
""",
|
107 |
url="https://www.openslr.org/19/",
|
108 |
download_urls={
|
109 |
-
"train": _DL_URL + os.path.join("TEDLIUM_release2", "train.tar.gz"),
|
110 |
-
"validation": _DL_URL + os.path.join("TEDLIUM_release2", "dev.tar.gz"),
|
111 |
-
"test": _DL_URL + os.path.join("TEDLIUM_release2", "test.tar.gz"),
|
112 |
},
|
113 |
split_paths=[
|
114 |
(datasets.Split.TRAIN, "train"),
|
@@ -158,9 +158,14 @@ def _make_builder_configs():
|
|
158 |
""",
|
159 |
url="https://www.openslr.org/51/",
|
160 |
download_urls={
|
161 |
-
"train":
|
162 |
-
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
164 |
},
|
165 |
split_paths=[
|
166 |
(datasets.Split.TRAIN, "train"),
|
@@ -193,9 +198,9 @@ def _make_builder_configs():
|
|
193 |
""",
|
194 |
url="https://www.openslr.org/51/",
|
195 |
download_urls={
|
196 |
-
"train": _DL_URL + os.path.join("TEDLIUM_release3", "speaker-adaptation", "train.tar.gz"),
|
197 |
-
"validation": _DL_URL + os.path.join("TEDLIUM_release3", "speaker-adaptation", "dev.tar.gz"),
|
198 |
-
"test": _DL_URL + os.path.join("TEDLIUM_release3", "speaker-adaptation", "test.tar.gz"),
|
199 |
},
|
200 |
split_paths=[
|
201 |
(datasets.Split.TRAIN, "train"),
|
@@ -242,7 +247,7 @@ class TedLium(datasets.GeneratorBasedBuilder):
|
|
242 |
splits = []
|
243 |
for split, path in self.config.split_paths:
|
244 |
kwargs = {
|
245 |
-
"filepath": dl_manager.iter_archive(archive_path[split]
|
246 |
"local_extracted_archive": local_extracted_archive.get(split),
|
247 |
"split_path": path,
|
248 |
}
|
@@ -252,92 +257,94 @@ class TedLium(datasets.GeneratorBasedBuilder):
|
|
252 |
def _generate_examples(self, filepath, local_extracted_archive, split_path):
|
253 |
"""Generate examples from a TED-LIUM stm file."""
|
254 |
if local_extracted_archive:
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
line
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
|
|
286 |
|
287 |
else:
|
288 |
audio_data = {}
|
289 |
transcripts = defaultdict(list)
|
290 |
-
for
|
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 |
-
if audio_data and audio_data.keys() == transcripts.keys():
|
320 |
-
for fn, speaker in transcripts.items():
|
321 |
-
for transcript in speaker:
|
322 |
-
segment, sampling_rate = audio_data[transcript["fn"]]
|
323 |
-
samples = _extract_audio_segment(
|
324 |
-
segment,
|
325 |
-
int(transcript["channel"]),
|
326 |
-
float(transcript["start"]),
|
327 |
-
float(transcript["end"]),
|
328 |
-
)
|
329 |
-
audio = {"path": transcript["file"], "array": samples, "sampling_rate": sampling_rate}
|
330 |
-
key = transcript["id"]
|
331 |
-
yield key, {
|
332 |
-
"audio": audio,
|
333 |
-
"text": transcript["text"],
|
334 |
-
"speaker_id": transcript["speaker_id"],
|
335 |
-
"gender": transcript["gender"],
|
336 |
-
"file": transcript["file"],
|
337 |
-
"id": transcript["id"],
|
338 |
-
}
|
339 |
-
audio_data = {}
|
340 |
-
transcripts = defaultdict(list)
|
341 |
|
342 |
|
343 |
def _maybe_trim_suffix(transcript):
|
|
|
68 |
""",
|
69 |
url="https://www.openslr.org/7/",
|
70 |
download_urls={
|
71 |
+
"train": [_DL_URL + os.path.join("TEDLIUM_release1", "train.tar.gz")],
|
72 |
+
"validation": [_DL_URL + os.path.join("TEDLIUM_release1", "dev.tar.gz")],
|
73 |
+
"test": [_DL_URL + os.path.join("TEDLIUM_release1", "test.tar.gz")],
|
74 |
},
|
75 |
split_paths=[
|
76 |
(datasets.Split.TRAIN, "train"),
|
|
|
106 |
""",
|
107 |
url="https://www.openslr.org/19/",
|
108 |
download_urls={
|
109 |
+
"train": [_DL_URL + os.path.join("TEDLIUM_release2", "train.tar.gz")],
|
110 |
+
"validation": [_DL_URL + os.path.join("TEDLIUM_release2", "dev.tar.gz")],
|
111 |
+
"test": [_DL_URL + os.path.join("TEDLIUM_release2", "test.tar.gz")],
|
112 |
},
|
113 |
split_paths=[
|
114 |
(datasets.Split.TRAIN, "train"),
|
|
|
158 |
""",
|
159 |
url="https://www.openslr.org/51/",
|
160 |
download_urls={
|
161 |
+
"train": [
|
162 |
+
_DL_URL + os.path.join("TEDLIUM_release3", "legacy", "train_1.tar.gz"),
|
163 |
+
_DL_URL + os.path.join("TEDLIUM_release3", "legacy", "train_2.tar.gz"),
|
164 |
+
_DL_URL + os.path.join("TEDLIUM_release3", "legacy", "train_3.tar.gz"),
|
165 |
+
_DL_URL + os.path.join("TEDLIUM_release3", "legacy", "train_4.tar.gz"),
|
166 |
+
],
|
167 |
+
"validation": [_DL_URL + os.path.join("TEDLIUM_release3", "legacy", "dev.tar.gz")],
|
168 |
+
"test": [_DL_URL + os.path.join("TEDLIUM_release3", "legacy", "test.tar.gz")],
|
169 |
},
|
170 |
split_paths=[
|
171 |
(datasets.Split.TRAIN, "train"),
|
|
|
198 |
""",
|
199 |
url="https://www.openslr.org/51/",
|
200 |
download_urls={
|
201 |
+
"train": [_DL_URL + os.path.join("TEDLIUM_release3", "speaker-adaptation", "train.tar.gz")],
|
202 |
+
"validation": [_DL_URL + os.path.join("TEDLIUM_release3", "speaker-adaptation", "dev.tar.gz")],
|
203 |
+
"test": [_DL_URL + os.path.join("TEDLIUM_release3", "speaker-adaptation", "test.tar.gz")],
|
204 |
},
|
205 |
split_paths=[
|
206 |
(datasets.Split.TRAIN, "train"),
|
|
|
247 |
splits = []
|
248 |
for split, path in self.config.split_paths:
|
249 |
kwargs = {
|
250 |
+
"filepath": [dl_manager.iter_archive(sharded_path) for sharded_path in archive_path[split]],
|
251 |
"local_extracted_archive": local_extracted_archive.get(split),
|
252 |
"split_path": path,
|
253 |
}
|
|
|
257 |
def _generate_examples(self, filepath, local_extracted_archive, split_path):
|
258 |
"""Generate examples from a TED-LIUM stm file."""
|
259 |
if local_extracted_archive:
|
260 |
+
for local_archive in local_extracted_archive:
|
261 |
+
# The stm directory houses the speaker and transcription information in .stm format
|
262 |
+
stm_dir = os.path.join(local_archive, split_path, "stm")
|
263 |
+
# The sph directory houses the audio files in .sph format
|
264 |
+
sph_dir = os.path.join(local_archive, split_path, "sph")
|
265 |
+
stm_files = [os.path.join(stm_dir, f) for f in os.listdir(stm_dir) if f.endswith(".stm")]
|
266 |
+
for file in stm_files:
|
267 |
+
# the .sph speaker file almost always has the same file name as the .stm file
|
268 |
+
speaker_file = Path(file).stem
|
269 |
+
audio_file = os.path.join(sph_dir, speaker_file + ".sph")
|
270 |
+
segment, sampling_rate = sf.read(audio_file, dtype=np.int16)
|
271 |
+
with open(file) as f:
|
272 |
+
for line in f:
|
273 |
+
line = line.strip()
|
274 |
+
fn, channel, speaker, start, end, label, transcript = line.split(" ", 6)
|
275 |
+
transcript = _maybe_trim_suffix(transcript)
|
276 |
+
if speaker_file != fn:
|
277 |
+
# handle the case where the stm file does not have the same file name as the transcript
|
278 |
+
speaker_file = fn
|
279 |
+
audio_file = os.path.join(sph_dir, speaker_file + ".sph")
|
280 |
+
segment, sampling_rate = sf.read(audio_file, dtype=np.int16)
|
281 |
+
samples = _extract_audio_segment(segment, int(channel), float(start), float(end))
|
282 |
+
key = "-".join([speaker, start, end, label])
|
283 |
+
example = {
|
284 |
+
"audio": {"path": audio_file, "array": samples, "sampling_rate": sampling_rate},
|
285 |
+
"text": transcript,
|
286 |
+
"speaker_id": speaker,
|
287 |
+
"gender": _parse_gender(label),
|
288 |
+
"file": audio_file,
|
289 |
+
"id": key,
|
290 |
+
}
|
291 |
+
yield key, example
|
292 |
|
293 |
else:
|
294 |
audio_data = {}
|
295 |
transcripts = defaultdict(list)
|
296 |
+
for file in filepath:
|
297 |
+
for path, f in file:
|
298 |
+
if path.endswith(".sph"):
|
299 |
+
# get the speaker id
|
300 |
+
fn = path.split("/")[-1].strip(".sph")
|
301 |
+
# read the audio data from raw byte form and add key-value pair to dict
|
302 |
+
audio_data[fn] = sf.read(BytesIO(f.read()), dtype=np.int16)
|
303 |
+
elif path.endswith(".stm"):
|
304 |
+
for line in f:
|
305 |
+
if line:
|
306 |
+
line = line.decode("utf-8").strip()
|
307 |
+
fn, channel, speaker, start, end, label, transcript = line.split(" ", 6)
|
308 |
+
transcript = _maybe_trim_suffix(transcript)
|
309 |
+
audio_file = path.replace("stm", "sph")
|
310 |
+
key = "-".join([speaker, start, end, label])
|
311 |
+
# append metadata information to the dict of transcripts for the associated speaker
|
312 |
+
transcripts[fn].append(
|
313 |
+
{
|
314 |
+
"text": transcript,
|
315 |
+
"speaker_id": speaker,
|
316 |
+
"gender": _parse_gender(label),
|
317 |
+
"file": audio_file,
|
318 |
+
"id": key,
|
319 |
+
"start": start,
|
320 |
+
"end": end,
|
321 |
+
"channel": channel,
|
322 |
+
"fn": fn,
|
323 |
+
}
|
324 |
+
)
|
325 |
+
|
326 |
+
if audio_data and audio_data.keys() == transcripts.keys():
|
327 |
+
for fn, speaker in transcripts.items():
|
328 |
+
for transcript in speaker:
|
329 |
+
segment, sampling_rate = audio_data[transcript["fn"]]
|
330 |
+
samples = _extract_audio_segment(
|
331 |
+
segment,
|
332 |
+
int(transcript["channel"]),
|
333 |
+
float(transcript["start"]),
|
334 |
+
float(transcript["end"]),
|
335 |
+
)
|
336 |
+
audio = {"path": transcript["file"], "array": samples, "sampling_rate": sampling_rate}
|
337 |
+
key = transcript["id"]
|
338 |
+
yield key, {
|
339 |
+
"audio": audio,
|
340 |
+
"text": transcript["text"],
|
341 |
+
"speaker_id": transcript["speaker_id"],
|
342 |
+
"gender": transcript["gender"],
|
343 |
+
"file": transcript["file"],
|
344 |
+
"id": transcript["id"],
|
345 |
}
|
346 |
+
audio_data = {}
|
347 |
+
transcripts = defaultdict(list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
348 |
|
349 |
|
350 |
def _maybe_trim_suffix(transcript):
|