import os import random import hashlib import datasets from datasets.tasks import ImageClassification _NAMES = { "all": ["m_bel", "f_bel", "m_folk", "f_folk"], "gender": ["female", "male"], "singing_method": ["Folk_Singing", "Bel_Canto"], } _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 bel_canto(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 _str2md5(self, original_string: str): md5_obj = hashlib.md5() md5_obj.update(original_string.encode("utf-8")) return md5_obj.hexdigest() 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"): cls = os.path.basename(os.path.dirname(fpath)) + "/" item_id = self._str2md5(cls + fname.split(".wa")[0]) files[item_id] = {"audio": fpath} for fpath in dl_manager.iter_files([mel_files]): fname = os.path.basename(fpath) if fname.endswith(".jpg"): cls = os.path.basename(os.path.dirname(fpath)) + "/" item_id = self._str2md5(cls + fname.split(".jp")[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]): fname = os.path.basename(fpath) if "mel" in fpath and fname.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 label = os.path.basename(os.path.dirname(fpath)) categories[label].append(data) testset, validset, trainset = [], [], [] for cls in categories: random.shuffle(categories[cls]) count = len(categories[cls]) p80 = int(count * 0.8) p90 = int(count * 0.9) trainset += categories[cls][:p80] validset += categories[cls][p80:p90] testset += categories[cls][p90:] 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, item in enumerate(files): label: str = os.path.basename(os.path.dirname(item["audio"])) yield i, { "audio": item["audio"], "mel": item["mel"], "label": label, "gender": ("male" if label.split("_")[0] == "m" else "female"), "singing_method": ( "Bel_Canto" if label.split("_")[1] == "bel" else "Folk_Singing" ), } else: for i, fpath in enumerate(files): label = os.path.basename(os.path.dirname(fpath)) yield i, { "mel": fpath, "cqt": fpath.replace("mel", "cqt"), "chroma": fpath.replace("mel", "chroma"), "label": label, "gender": ("male" if label.split("_")[0] == "m" else "female"), "singing_method": ( "Bel_Canto" if label.split("_")[1] == "bel" else "Folk_Singing" ), }