| |
|
|
| """MSWC keyword spotting classification dataset.""" |
|
|
|
|
| import os |
| import textwrap |
| import datasets |
| import itertools |
| import typing as tp |
| from pathlib import Path |
|
|
| from ._mswc import ( |
| TRAIN_ENG, VALIDATION_ENG, TEST_ENG, |
| TRAIN_SPA, VALIDATION_SPA, TEST_SPA, |
| TRAIN_IND, VALIDATION_IND, TEST_IND, |
| ) |
|
|
| FOLDER_IN_ARCHIVE = "genres" |
| SAMPLE_RATE = 16_000 |
|
|
| _ENG_FILENAME = 'eng-kw-archive.tar.gz' |
| _SPA_FILENAME = 'spa-kw-archive.tar.gz' |
| _IND_FILENAME = 'ind-kw-archive.tar.gz' |
|
|
| CLASS_ENG = list(set([fileid.split('_')[0] for fileid in TRAIN_ENG])) |
| CLASS_SPA = list(set([fileid.split('_')[0] for fileid in TRAIN_SPA])) |
| CLASS_IND = list(set([fileid.split('_')[0] for fileid in TRAIN_IND])) |
|
|
|
|
| class MswcConfig(datasets.BuilderConfig): |
| """BuilderConfig for MSWC.""" |
| |
| def __init__(self, features, **kwargs): |
| super(MswcConfig, self).__init__(version=datasets.Version("0.0.1", ""), **kwargs) |
| self.features = features |
|
|
|
|
| class MSWC(datasets.GeneratorBasedBuilder): |
|
|
| BUILDER_CONFIGS = [ |
| MswcConfig( |
| features=datasets.Features( |
| { |
| "file": datasets.Value("string"), |
| "audio": datasets.Audio(sampling_rate=SAMPLE_RATE), |
| "keyword": datasets.Value("string"), |
| "label": datasets.ClassLabel(names=CLASS_ENG), |
| } |
| ), |
| name="english", |
| description=textwrap.dedent( |
| """\ |
| Keyword spotting classifies each audio for its keywords as a multi-class |
| classification, where keywords are in the same pre-defined set for both training and testing. |
| The evaluation metric is accuracy (ACC). |
| """ |
| ), |
| ), |
| MswcConfig( |
| features=datasets.Features( |
| { |
| "file": datasets.Value("string"), |
| "audio": datasets.Audio(sampling_rate=SAMPLE_RATE), |
| "keyword": datasets.Value("string"), |
| "label": datasets.ClassLabel(names=CLASS_SPA), |
| } |
| ), |
| name="spanish", |
| description=textwrap.dedent( |
| """\ |
| Keyword spotting classifies each audio for its keywords as a multi-class |
| classification, where keywords are in the same pre-defined set for both training and testing. |
| The evaluation metric is accuracy (ACC). |
| """ |
| ), |
| ), |
| MswcConfig( |
| features=datasets.Features( |
| { |
| "file": datasets.Value("string"), |
| "audio": datasets.Audio(sampling_rate=SAMPLE_RATE), |
| "keyword": datasets.Value("string"), |
| "label": datasets.ClassLabel(names=CLASS_IND), |
| } |
| ), |
| name="indian", |
| description=textwrap.dedent( |
| """\ |
| Keyword spotting classifies each audio for its keywords as a multi-class |
| classification, where keywords are in the same pre-defined set for both training and testing. |
| The evaluation metric is accuracy (ACC). |
| """ |
| ), |
| ), |
| ] |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description="", |
| features=self.config.features, |
| supervised_keys=None, |
| homepage="", |
| citation="", |
| task_templates=None, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| """Returns SplitGenerators.""" |
|
|
| if self.config.name == "english": |
| archive_path = dl_manager.extract(_ENG_FILENAME) |
| elif self.config.name == "spanish": |
| archive_path = dl_manager.extract(_SPA_FILENAME) |
| elif self.config.name == "indian": |
| archive_path = dl_manager.extract(_IND_FILENAME) |
| |
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "split": "train"} |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.VALIDATION, gen_kwargs={"archive_path": archive_path, "split": "validation"} |
| ), |
| datasets.SplitGenerator( |
| name=datasets.Split.TEST, gen_kwargs={"archive_path": archive_path, "split": "test"} |
| ), |
| ] |
|
|
| def _generate_examples(self, archive_path, split=None): |
| |
| if self.config.name == 'english': |
| extensions = ['.wav'] |
| _, _walker = fast_scandir(archive_path, extensions, recursive=True) |
| if split == 'train': |
| _walker = [fileid for fileid in _walker if Path(fileid).stem in TRAIN_ENG] |
| elif split == 'validation': |
| _walker = [fileid for fileid in _walker if Path(fileid).stem in VALIDATION_ENG] |
| elif split == 'test': |
| _walker = [fileid for fileid in _walker if Path(fileid).stem in TEST_ENG] |
| |
| elif self.config.name == 'spanish': |
| extensions = ['.wav'] |
| _, _walker = fast_scandir(archive_path, extensions, recursive=True) |
| if split == 'train': |
| _walker = [fileid for fileid in _walker if Path(fileid).stem in TRAIN_SPA] |
| elif split == 'validation': |
| _walker = [fileid for fileid in _walker if Path(fileid).stem in VALIDATION_SPA] |
| elif split == 'test': |
| _walker = [fileid for fileid in _walker if Path(fileid).stem in TEST_SPA] |
| |
| elif self.config.name == 'indian': |
| extensions = ['.wav'] |
| _, _walker = fast_scandir(archive_path, extensions, recursive=True) |
| if split == 'train': |
| _walker = [fileid for fileid in _walker if Path(fileid).stem in TRAIN_IND] |
| elif split == 'validation': |
| _walker = [fileid for fileid in _walker if Path(fileid).stem in VALIDATION_IND] |
| elif split == 'test': |
| _walker = [fileid for fileid in _walker if Path(fileid).stem in TEST_IND] |
|
|
| for guid, audio_path in enumerate(_walker): |
| yield guid, { |
| "id": str(guid), |
| "file": audio_path, |
| "audio": audio_path, |
| "keyword": Path(audio_path).stem.split('_')[0], |
| "label": Path(audio_path).stem.split('_')[0], |
| } |
|
|
|
|
| def fast_scandir(path: str, exts: tp.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 exts: |
| files.append(f.path) |
| except Exception: |
| pass |
| except Exception: |
| pass |
|
|
| if recursive: |
| for path in list(subfolders): |
| sf, f = fast_scandir(path, exts, recursive=recursive) |
| subfolders.extend(sf) |
| files.extend(f) |
|
|
| return subfolders, files |