lrl_transfer_hubert / lrl_transfer_hubert.py
albertvillanova's picture
Remove deprecated tasks (#1)
bc71683 verified
Raw
History Blame Contribute Delete
4.91 kB
"""LibriAdapt Dataset"""
import os
from pathlib import Path
import datasets
import csv
_CITATION = """\
@inproceedings{mathur20,
doi = {10.1109/icassp40776.2020.9053074},
url = {https://doi.org/10.1109\%2Ficassp40776.2020.9053074},
year = 2020,
month = {may},
publisher = {{IEEE}},
author = {Akhil Mathur and Fahim Kawsar and Nadia Berthouze and Nicholas D. Lane},
title = {Libri-Adapt: a New Speech Dataset for Unsupervised Domain Adaptation},
booktitle = {{ICASSP} 2020 - 2020 {IEEE} International Conference on Acoustics, Speech and Signal Processing ({ICASSP})}
}
"""
_DESCRIPTION = """\
LibriAdapt (For more information refer to the original paper at https://doi.org/10.1109%2Ficassp40776.2020.9053074)
"""
_HOMEPAGE = "https://github.com/akhilmathurs/libriadapt"
class LRL_TranserConfig(datasets.BuilderConfig):
"""BuilderConfig for TimitASR."""
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(LRL_TranserConfig, self).__init__(
version=datasets.Version("1.0.0", ""), **kwargs)
class LRL_Transfer(datasets.GeneratorBasedBuilder):
"""LRL Transfer dataset."""
BUILDER_CONFIGS = [
LRL_TranserConfig(name="clean", description="'Clean' speech.")]
@property
def manual_download_instructions(self):
return (
"If you want to use noise, you can download the dataset and script here: https://github.com/akhilmathurs/libriadapt"
)
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"),
"accent": datasets.Value("string"),
"microphone": datasets.Value("string"),
"id": datasets.Value("string"),
}
),
supervised_keys=("file", "text"),
homepage=_HOMEPAGE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
data_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
if not os.path.exists(data_dir):
raise FileNotFoundError(
f"{data_dir} does not exist. Make sure you insert a manual dir via `datasets.load_dataset('timit_asr', data_dir=...)` that includes files unzipped from the TIMIT zip. Manual download instructions: {self.manual_download_instructions}"
)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN,
gen_kwargs={"split": "train",
"data_dir": data_dir}),
datasets.SplitGenerator(name=datasets.Split.TEST,
gen_kwargs={"split": "test",
"data_dir": data_dir}),
]
def _generate_examples(self, split, data_dir):
"""Generate examples from dataset based on the test/train csv information."""
# Iterating the contents of the data to extract the relevant information
wav_paths = sorted(Path(data_dir).glob(f"**/{split}/**/*.wav"))
wav_paths = wav_paths if wav_paths else sorted(
Path(data_dir).glob(f"**/{split.upper()}/**/*.WAV"))
for key, wav_path in enumerate(wav_paths):
# extract microphone
mic = str(wav_path).split("clean")[-1].split("/")[1]
# extract accent
accent = str(wav_path).split("en-")[-1][0:2]
# extract transcript
num = str(wav_path).split("/")[-1].split(".")[0]
csv_path = str(wav_path).split("clean")[
0] + "clean/" + split + "_files_" + mic + ".csv"
with open(csv_path) as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
if row[0].split("/")[-1].split(".")[0] == num:
transcript = row[2]
id_ = wav_path.stem
example = {
"file": str(wav_path),
"audio": str(wav_path),
"text": transcript,
"accent": accent,
"microphone": mic,
"id": id_,
}
yield key, example
def with_case_insensitive_suffix(path: Path, suffix: str):
path = path.with_suffix(suffix.lower())
path = path if path.exists() else path.with_suffix(suffix.upper())
return path