reborn-uasr_multilingual-librispeech-no-silence-100hr / reborn-uasr_multilingual-librispeech-no-silence-100hr.py
andybi7676's picture
rename dataset
e2bf0b8
raw
history blame
10.2 kB
# 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 = """\
@article{Pratap2020MLSAL,
title={MLS: A Large-Scale Multilingual Dataset for Speech Research},
author={Vineel Pratap and Qiantong Xu and Anuroop Sriram and Gabriel Synnaeve and Ronan Collobert},
journal={ArXiv},
year={2020},
volume={abs/2012.03411}
}
@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/{name}"
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.format(name=name)
self.metadata_root_url = self.data_root_url.replace("data", "metadata")
class RebornLibrispeech(datasets.GeneratorBasedBuilder):
"""Multilingual Librispeech dataset."""
BUILDER_CONFIGS = [
RebornLibrispeechConfig(name="german", description="MLS 100hr German dataset without silence"),
RebornLibrispeechConfig(name="french", description="MLS 100hr French dataset without silence"),
RebornLibrispeechConfig(name="dutch", description="MLS 100hr Dutch dataset without silence"),
RebornLibrispeechConfig(name="spanish", description="MLS 100hr Spanish dataset without silence"),
RebornLibrispeechConfig(name="italian", description="MLS 100hr Italian dataset without silence"),
RebornLibrispeechConfig(name="portuguese", description="MLS 100hr Portuguese 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_100hr": f"{self.config.metadata_root_url}/train_100hr.tsv",
"dev": f"{self.config.metadata_root_url}/dev.tsv",
"test": f"{self.config.metadata_root_url}/test.tsv",
"dev_small": f"{self.config.metadata_root_url}/dev_small.tsv",
})
all_splits = [
"train_100hr",
"dev",
"test",
]
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_100hr"]],
"audio_archives": [dl_manager.iter_archive(audio_archives["train_100hr"])],
"local_extracted_archives": [local_extracted_archives.get("train_100hr")],
}
),
datasets.SplitGenerator(
name="train.100hr",
gen_kwargs={
"metadata_fpaths": [metadata["train_100hr"]],
"audio_archives": [dl_manager.iter_archive(audio_archives["train_100hr"])],
"local_extracted_archives": [local_extracted_archives.get("train_100hr")],
}
),
]
dev_splits = [
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"metadata_fpath": [metadata["dev"]],
"audio_archives": [dl_manager.iter_archive(audio_archives["dev"])],
"local_extracted_archives": [local_extracted_archives.get("dev")],
}
),
datasets.SplitGenerator(
name="dev",
gen_kwargs={
"metadata_fpath": [metadata["dev"]],
"audio_archives": [dl_manager.iter_archive(audio_archives["dev"])],
"local_extracted_archives": [local_extracted_archives.get("dev")],
}
),
datasets.SplitGenerator(
name="valid",
gen_kwargs={
"metadata_fpath": [metadata["dev"]],
"audio_archives": [dl_manager.iter_archive(audio_archives["dev"])],
"local_extracted_archives": [local_extracted_archives.get("dev")],
}
),
datasets.SplitGenerator(
name="dev.small",
gen_kwargs={
"metadata_fpaths": [metadata["dev_small"]],
"audio_archives": [dl_manager.iter_archive(audio_archives["dev"])],
"local_extracted_archives": [local_extracted_archives.get("dev")],
},
),
]
test_splits = [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"metadata_fpaths": [metadata["test"]],
"audio_archives": [dl_manager.iter_archive(audio_archives["test"])],
"local_extracted_archives": [local_extracted_archives.get("test")],
}
),
]
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
for archive_idx, audio_archive in enumerate(audio_archives):
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
}