Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Librispeech] Add 'all' config #4184

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 96 additions & 40 deletions datasets/librispeech_asr/librispeech_asr.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def map_to_array(batch):
_URL = "http://www.openslr.org/12"
_DL_URL = "http://www.openslr.org/resources/12/"


def retrieve_subset(name):
# "train-other-500.tar.gz" -> "other-500"
return "-".join(name.split(".")[0].split("-")[1:])


_DL_URLS = {
"clean": {
"dev": _DL_URL + "dev-clean.tar.gz",
Expand All @@ -69,6 +75,14 @@ def map_to_array(batch):
"dev": _DL_URL + "dev-other.tar.gz",
"train.500": _DL_URL + "train-other-500.tar.gz",
},
"all": {
"dev": {retrieve_subset(u): _DL_URL + u for u in ["dev-clean.tar.gz", "dev-other.tar.gz"]},
patrickvonplaten marked this conversation as resolved.
Show resolved Hide resolved
"test": {retrieve_subset(u): _DL_URL + u for u in ["test-clean.tar.gz", "test-other.tar.gz"]},
patrickvonplaten marked this conversation as resolved.
Show resolved Hide resolved
"train": {
patrickvonplaten marked this conversation as resolved.
Show resolved Hide resolved
retrieve_subset(u): _DL_URL + u
for u in ["train-clean-100.tar.gz", "train-clean-360.tar.gz", "train-other-500.tar.gz"]
},
},
}


Expand All @@ -94,6 +108,7 @@ class LibrispeechASR(datasets.GeneratorBasedBuilder):
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."),
]

def _info(self):
Expand All @@ -107,6 +122,7 @@ def _info(self):
"speaker_id": datasets.Value("int64"),
"chapter_id": datasets.Value("int64"),
"id": datasets.Value("string"),
"subset": datasets.Value("string"),
}
),
supervised_keys=("file", "text"),
Expand All @@ -117,61 +133,101 @@ def _info(self):

def _split_generators(self, dl_manager):
archive_path = dl_manager.download(_DL_URLS[self.config.name])

if self.config.name == "clean":
train_splits = [
datasets.SplitGenerator(
name="train.100", gen_kwargs={"files": dl_manager.iter_archive(archive_path["train.100"])}
name="train.100",
gen_kwargs={"files": {"clean-100": dl_manager.iter_archive(archive_path["train.100"])}},
),
datasets.SplitGenerator(
name="train.360", gen_kwargs={"files": dl_manager.iter_archive(archive_path["train.360"])}
name="train.360",
gen_kwargs={"files": {"clean-360": dl_manager.iter_archive(archive_path["train.360"])}},
),
]
dev_splits = [
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={"files": {"clean": dl_manager.iter_archive(archive_path["dev"])}},
)
]
test_splits = [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"files": {"clean": dl_manager.iter_archive(archive_path["test"])}},
)
]
elif self.config.name == "other":
train_splits = [
datasets.SplitGenerator(
name="train.500", gen_kwargs={"files": dl_manager.iter_archive(archive_path["train.500"])}
),
name="train.500",
gen_kwargs={"files": {"other-500": dl_manager.iter_archive(archive_path["train.500"])}},
)
]
dev_splits = [
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={"files": {"other": dl_manager.iter_archive(archive_path["dev"])}},
)
]
test_splits = [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"files": {"other": dl_manager.iter_archive(archive_path["test"])}},
)
]
elif self.config.name == "all":
train_splits = [
datasets.SplitGenerator(
name="train",
gen_kwargs={"files": {k: dl_manager.iter_archive(v) for k, v in archive_path["train"].items()}},
)
]
dev_splits = [
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={"files": {k: dl_manager.iter_archive(v) for k, v in archive_path["dev"].items()}},
)
]
test_splits = [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"files": {k: dl_manager.iter_archive(v) for k, v in archive_path["test"].items()}},
)
]

return train_splits + [
datasets.SplitGenerator(
name=datasets.Split.VALIDATION, gen_kwargs={"files": dl_manager.iter_archive(archive_path["dev"])}
),
datasets.SplitGenerator(
name=datasets.Split.TEST, gen_kwargs={"files": dl_manager.iter_archive(archive_path["test"])}
),
]
return train_splits + dev_splits + test_splits

def _generate_examples(self, files):
"""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]]
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 = []
for subset, iterator in files.items():
for path, f in iterator:
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]]
transcripts.append(
{
"id": id_,
"speaker_id": speaker_id,
"chapter_id": chapter_id,
"file": audio_file,
"text": transcript,
"subset": subset,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albertz @lhoestq - this "subset" arg is one of "clean-100", "clean-360", "other-500" for:

ds = load_dataset("librispeech_asr", "all", split="train")
ds[0]

This should allow the user to easily define which subsets are needed I think

}
)
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 = []