| |
|
|
| """VoxCeleb dataset.""" |
|
|
|
|
| import os |
| from typing import List |
| from pathlib import Path |
|
|
| import librosa |
| import datasets |
| from rich import print |
|
|
|
|
| DATA_DIR_STRUCTURE = """ |
| test/ |
| βββ wav |
| βββ id10270 |
| ... |
| βββ id10309 |
| βββ A3ZvNuG8_oM |
| ... |
| βββ UbApEUoPvzY |
| βββ 00001.wav |
| ... |
| βββ 00005.wav |
| """ |
|
|
| SAMPLING_RATE = 16_000 |
|
|
|
|
| class VoxCelebConfig(datasets.BuilderConfig): |
| """BuilderConfig for VoxCeleb.""" |
| |
| def __init__(self, features, **kwargs): |
| super(VoxCelebConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs) |
| self.features = features |
|
|
|
|
| class VoxCeleb(datasets.GeneratorBasedBuilder): |
|
|
| BUILDER_CONFIGS = [ |
| VoxCelebConfig( |
| features=datasets.Features( |
| { |
| "audio": datasets.Audio(sampling_rate=SAMPLING_RATE), |
| "speaker": datasets.Value("string"), |
| |
| } |
| ), |
| name="verification", |
| description="", |
| ), |
| ] |
|
|
| DEFAULT_CONFIG_NAME = "verification" |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description="VoxCeleb for verification", |
| features=self.config.features, |
| ) |
|
|
| @property |
| def manual_download_instructions(self): |
| return ( |
| "To use VoxCeleb you have to download it manually. " |
| "The tree structure of the downloaded data looks like: \n" |
| f"{DATA_DIR_STRUCTURE}" |
| ) |
|
|
| 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. " |
| f"Manual download instructions: \n{self.manual_download_instructions}" |
| ) |
|
|
| dev_archive_path = os.path.join(data_dir, 'dev', 'wav') |
| test_archive_path = os.path.join(data_dir, 'test', 'wav') |
|
|
| for path in [dev_archive_path, test_archive_path]: |
| if not os.path.isdir(path): |
| raise FileExistsError(f"{path} does not exist. Make sure you have converted m4a to wav format.") |
|
|
| return [ |
| datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"split": "train", "archive_path": dev_archive_path}), |
| datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"split": "test", "archive_path": test_archive_path}), |
| ] |
|
|
| def _generate_examples(self, split, archive_path): |
| """Generate examples from VoxCeleb""" |
| |
| extensions = ['.wav'] |
|
|
| _, wav_paths = fast_scandir(archive_path, extensions, recursive=True) |
|
|
| for guid, wav_path in enumerate(wav_paths): |
| fileid = Path(wav_path).name |
| speaker = Path(wav_path).parent.parent.name |
| |
| |
| |
| try: |
| yield guid, { |
| "id": str(guid), |
| "audio": wav_path, |
| "speaker": speaker, |
| |
| } |
| except: |
| continue |
|
|
|
|
| def fast_scandir(path: str, extensions: List[str], recursive: bool = False): |
| |
| |
| subfolders, files = [], [] |
|
|
| try: |
| for f in os.scandir(path): |
| try: |
| if f.is_dir(): |
| subfolders.append(f.path) |
| elif f.is_file(): |
| if os.path.splitext(f.name)[1].lower() in extensions: |
| files.append(f.path) |
| except Exception: |
| pass |
| except Exception: |
| pass |
|
|
| if recursive: |
| for path in list(subfolders): |
| sf, f = fast_scandir(path, extensions, recursive=recursive) |
| subfolders.extend(sf) |
| files.extend(f) |
|
|
| return subfolders, files |