import os import random import datasets from datasets.tasks import ImageClassification _NAMES = { "all": ["m_chest", "f_chest", "m_falsetto", "f_falsetto"], "gender": ["female", "male"], "singing_method": ["falsetto", "chest"], } _HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{os.path.basename(__file__)[:-3]}" _DOMAIN = f"{_HOMEPAGE}/resolve/master/data" _URLS = { "audio": f"{_DOMAIN}/audio.zip", "mel": f"{_DOMAIN}/mel.zip", "eval": f"{_DOMAIN}/eval.zip", } class chest_falsetto(datasets.GeneratorBasedBuilder): def _info(self): return datasets.DatasetInfo( features=( datasets.Features( { "audio": datasets.Audio(sampling_rate=22050), "mel": datasets.Image(), "label": datasets.features.ClassLabel(names=_NAMES["all"]), "gender": datasets.features.ClassLabel(names=_NAMES["gender"]), "singing_method": datasets.features.ClassLabel( names=_NAMES["singing_method"] ), } ) if self.config.name == "default" else datasets.Features( { "mel": datasets.Image(), "cqt": datasets.Image(), "chroma": datasets.Image(), "label": datasets.features.ClassLabel(names=_NAMES["all"]), "gender": datasets.features.ClassLabel(names=_NAMES["gender"]), "singing_method": datasets.features.ClassLabel( names=_NAMES["singing_method"] ), } ) ), supervised_keys=("mel", "label"), homepage=_HOMEPAGE, license="CC-BY-NC-ND", version="1.2.0", task_templates=[ ImageClassification( task="image-classification", image_column="mel", label_column="label", ) ], ) def _split_generators(self, dl_manager): dataset = [] if self.config.name == "default": files = {} audio_files = dl_manager.download_and_extract(_URLS["audio"]) mel_files = dl_manager.download_and_extract(_URLS["mel"]) for fpath in dl_manager.iter_files([audio_files]): fname: str = os.path.basename(fpath) if fname.endswith(".wav"): item_id = fname.split(".")[0] files[item_id] = {"audio": fpath} for fpath in dl_manager.iter_files([mel_files]): fname = os.path.basename(fpath) if fname.endswith(".jpg"): item_id = fname.split(".")[0] files[item_id]["mel"] = fpath dataset = list(files.values()) else: data_files = dl_manager.download_and_extract(_URLS["eval"]) for fpath in dl_manager.iter_files([data_files]): if "mel" in fpath and os.path.basename(fpath).endswith(".jpg"): dataset.append(fpath) categories = {} for name in _NAMES["all"]: categories[name] = [] for data in dataset: fpath = data["audio"] if self.config.name == "default" else data filename: str = os.path.basename(fpath)[:-4] label = "_".join(filename.split("_")[1:3]) categories[label].append(data) testset, validset, trainset = [], [], [] for cls in categories: random.shuffle(categories[cls]) count = len(categories[cls]) p60 = int(count * 0.6) p80 = int(count * 0.8) trainset += categories[cls][:p60] validset += categories[cls][p60:p80] testset += categories[cls][p80:] random.shuffle(trainset) random.shuffle(validset) random.shuffle(testset) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={"files": trainset} ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs={"files": validset} ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={"files": testset} ), ] def _generate_examples(self, files): if self.config.name == "default": for i, fpath in enumerate(files): file_name = os.path.basename(fpath["audio"]) sex = file_name.split("_")[1] method = file_name.split("_")[2].split(".")[0] yield i, { "audio": fpath["audio"], "mel": fpath["mel"], "label": f"{sex}_{method}", "gender": "male" if sex == "m" else "female", "singing_method": method, } else: for i, fpath in enumerate(files): file_name: str = os.path.basename(fpath) sex = file_name.split("_")[1] method = file_name.split("_")[2] yield i, { "mel": fpath, "cqt": fpath.replace("mel", "cqt"), "chroma": fpath.replace("mel", "chroma"), "label": f"{sex}_{method}", "gender": "male" if sex == "m" else "female", "singing_method": method, }