Datasets:
Size:
10K<n<100K
License:
File size: 8,564 Bytes
a78bf79 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
import os
import random
import datasets
from datasets.tasks import ImageClassification
_NAMES_1 = {
1: "Classic",
2: "Non_classic",
}
_NAMES_2 = {
3: "Symphony",
4: "Opera",
5: "Solo",
6: "Chamber",
7: "Pop",
8: "Dance_and_house",
9: "Indie",
10: "Soul_or_RnB",
11: "Rock",
}
_NAMES_3 = {
3: "Symphony",
4: "Opera",
5: "Solo",
6: "Chamber",
12: "Pop_vocal_ballad",
13: "Adult_contemporary",
14: "Teen_pop",
15: "Contemporary_dance_pop",
16: "Dance_pop",
17: "Classic_indie_pop",
18: "Chamber_cabaret_and_art_pop",
10: "Soul_or_RnB",
19: "Adult_alternative_rock",
20: "Uplifting_anthemic_rock",
21: "Soft_rock",
22: "Acoustic_pop",
}
_DBNAME = os.path.basename(__file__).split(".")[0]
_HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{_DBNAME}"
_DOMAIN = f"https://www.modelscope.cn/api/v1/datasets/ccmusic-database/{_DBNAME}/repo?Revision=master&FilePath=data"
_CITATION = """\
@dataset{zhaorui_liu_2021_5676893,
author = {Monan Zhou, Shenyang Xu, Zhaorui Liu, Zhaowen Wang, Feng Yu, Wei Li and Baoqiang Han},
title = {CCMusic: an Open and Diverse Database for Chinese and General Music Information Retrieval Research},
month = {mar},
year = {2024},
publisher = {HuggingFace},
version = {1.2},
url = {https://huggingface.co/ccmusic-database}
}
"""
_DESCRIPTION = """\
The raw dataset comprises approximately 1,700 musical pieces in .mp3 format, sourced from the NetEase music. The lengths of these pieces range from 270 to 300 seconds. All are sampled at the rate of 48,000 Hz. As the website providing the audio music includes style labels for the downloaded music, there are no specific annotators involved. Validation is achieved concurrently with the downloading process. They are categorized into a total of 16 genres.
For the pre-processed version, audio is cut into an 11.4-second segment, resulting in 36,375 files, which are then transformed into Mel, CQT and Chroma. In the end, the data entry has six columns: the first three columns represent the Mel, CQT, and Chroma spectrogram slices in .jpg format, respectively, while the last three columns contain the labels for the three levels. The first level comprises two categories, the second level consists of nine categories, and the third level encompasses 16 categories. The entire dataset is shuffled and split into training, validation, and test sets in a ratio of 8:1:1. This dataset can be used for genre classification.
"""
_URLS = {
"audio": f"{_DOMAIN}/audio.zip",
"mel": f"{_DOMAIN}/mel.zip",
"eval": f"{_DOMAIN}/eval.zip",
}
class music_genre(datasets.GeneratorBasedBuilder):
# BUILDER_CONFIGS = [
# datasets.BuilderConfig(name="default"),
# datasets.BuilderConfig(name="eval"),
# ]
def _info(self):
return datasets.DatasetInfo(
features=(
datasets.Features(
{
"audio": datasets.Audio(sampling_rate=22050),
"mel": datasets.Image(),
"fst_level_label": datasets.features.ClassLabel(
names=list(_NAMES_1.values())
),
"sec_level_label": datasets.features.ClassLabel(
names=list(_NAMES_2.values())
),
"thr_level_label": datasets.features.ClassLabel(
names=list(_NAMES_3.values())
),
}
)
if self.config.name == "raw"
else datasets.Features(
{
"mel": datasets.Image(),
"cqt": datasets.Image(),
"chroma": datasets.Image(),
"fst_level_label": datasets.features.ClassLabel(
names=list(_NAMES_1.values())
),
"sec_level_label": datasets.features.ClassLabel(
names=list(_NAMES_2.values())
),
"thr_level_label": datasets.features.ClassLabel(
names=list(_NAMES_3.values())
),
}
)
),
supervised_keys=("mel", "sec_level_label"),
homepage=_HOMEPAGE,
license="CC-BY-NC-ND",
version="1.2.0",
citation=_CITATION,
description=_DESCRIPTION,
task_templates=[
ImageClassification(
task="image-classification",
image_column="mel",
label_column="sec_level_label",
)
],
)
def _split_generators(self, dl_manager):
dataset = []
if self.config.name == "raw":
files = {}
audio_files = dl_manager.download_and_extract(_URLS["audio"])
mel_files = dl_manager.download_and_extract(_URLS["mel"])
for path in dl_manager.iter_files([audio_files]):
fname: str = os.path.basename(path)
if fname.endswith(".mp3"):
files[fname.split(".mp")[0]] = {"audio": path}
for path in dl_manager.iter_files([mel_files]):
fname = os.path.basename(path)
if fname.endswith(".jpg"):
files[fname.split(".jp")[0]]["mel"] = path
dataset = list(files.values())
else:
data_files = dl_manager.download_and_extract(_URLS["eval"])
for path in dl_manager.iter_files([data_files]):
if os.path.basename(path).endswith(".jpg") and "mel" in path:
dataset.append(
{
"mel": path,
"cqt": path.replace("\\mel\\", "\\cqt\\").replace(
"/mel/", "/cqt/"
),
"chroma": path.replace("\\mel\\", "\\chroma\\").replace(
"/mel/", "/chroma/"
),
}
)
random.shuffle(dataset)
data_count = len(dataset)
p80 = int(data_count * 0.8)
p90 = int(data_count * 0.9)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"files": dataset[:p80]},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={"files": dataset[p80:p90]},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"files": dataset[p90:]},
),
]
def _calc_label(self, path, depth, substr="/mel/"):
spect = substr
dirpath: str = os.path.dirname(path)
substr_index = dirpath.find(spect)
if substr_index < 0:
spect = spect.replace("/", "\\")
substr_index = dirpath.find(spect)
labstr = dirpath[substr_index + len(spect) :]
labs = labstr.split("/")
if len(labs) < 2:
labs = labstr.split("\\")
if depth <= len(labs):
return int(labs[depth - 1].split("_")[0])
else:
return int(labs[-1].split("_")[0])
def _generate_examples(self, files):
if self.config.name == "raw":
for i, path in enumerate(files):
yield i, {
"audio": path["audio"],
"mel": path["mel"],
"fst_level_label": _NAMES_1[self._calc_label(path["mel"], 1)],
"sec_level_label": _NAMES_2[self._calc_label(path["mel"], 2)],
"thr_level_label": _NAMES_3[self._calc_label(path["mel"], 3)],
}
else:
for i, path in enumerate(files):
yield i, {
"mel": path["mel"],
"cqt": path["cqt"],
"chroma": path["chroma"],
"fst_level_label": _NAMES_1[self._calc_label(path["mel"], 1)],
"sec_level_label": _NAMES_2[self._calc_label(path["mel"], 2)],
"thr_level_label": _NAMES_3[self._calc_label(path["mel"], 3)],
}
|