Datasets:
Size:
10K<n<100K
License:
Upload music_genre.py
Browse files- music_genre.py +222 -222
music_genre.py
CHANGED
@@ -1,222 +1,222 @@
|
|
1 |
-
import os
|
2 |
-
import random
|
3 |
-
import datasets
|
4 |
-
from datasets.tasks import ImageClassification
|
5 |
-
|
6 |
-
_NAMES_1 = {
|
7 |
-
1: "Classic",
|
8 |
-
2: "Non_classic",
|
9 |
-
}
|
10 |
-
|
11 |
-
_NAMES_2 = {
|
12 |
-
3: "Symphony",
|
13 |
-
4: "Opera",
|
14 |
-
5: "Solo",
|
15 |
-
6: "Chamber",
|
16 |
-
7: "Pop",
|
17 |
-
8: "Dance_and_house",
|
18 |
-
9: "Indie",
|
19 |
-
10: "Soul_or_RnB",
|
20 |
-
11: "Rock",
|
21 |
-
}
|
22 |
-
|
23 |
-
_NAMES_3 = {
|
24 |
-
3: "Symphony",
|
25 |
-
4: "Opera",
|
26 |
-
5: "Solo",
|
27 |
-
6: "Chamber",
|
28 |
-
12: "Pop_vocal_ballad",
|
29 |
-
13: "Adult_contemporary",
|
30 |
-
14: "Teen_pop",
|
31 |
-
15: "Contemporary_dance_pop",
|
32 |
-
16: "Dance_pop",
|
33 |
-
17: "Classic_indie_pop",
|
34 |
-
18: "Chamber_cabaret_and_art_pop",
|
35 |
-
10: "Soul_or_RnB",
|
36 |
-
19: "Adult_alternative_rock",
|
37 |
-
20: "Uplifting_anthemic_rock",
|
38 |
-
21: "Soft_rock",
|
39 |
-
22: "Acoustic_pop",
|
40 |
-
}
|
41 |
-
|
42 |
-
_DBNAME = os.path.basename(__file__).split(".")[0]
|
43 |
-
|
44 |
-
_HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{_DBNAME}"
|
45 |
-
|
46 |
-
_DOMAIN = f"https://www.modelscope.cn/api/v1/datasets/ccmusic-database/{_DBNAME}/repo?Revision=master&FilePath=data"
|
47 |
-
|
48 |
-
_CITATION = """\
|
49 |
-
@dataset{zhaorui_liu_2021_5676893,
|
50 |
-
author = {Monan Zhou, Shenyang Xu, Zhaorui Liu, Zhaowen Wang, Feng Yu, Wei Li and Baoqiang Han},
|
51 |
-
title = {CCMusic: an Open and Diverse Database for Chinese and General Music Information Retrieval Research},
|
52 |
-
month = {mar},
|
53 |
-
year = {2024},
|
54 |
-
publisher = {HuggingFace},
|
55 |
-
version = {1.2},
|
56 |
-
url = {https://huggingface.co/ccmusic-database}
|
57 |
-
}
|
58 |
-
"""
|
59 |
-
|
60 |
-
_DESCRIPTION = """\
|
61 |
-
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.
|
62 |
-
|
63 |
-
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.
|
64 |
-
"""
|
65 |
-
|
66 |
-
_URLS = {
|
67 |
-
"audio": f"{_DOMAIN}/audio.zip",
|
68 |
-
"mel": f"{_DOMAIN}/mel.zip",
|
69 |
-
"eval": f"{_DOMAIN}/eval.zip",
|
70 |
-
}
|
71 |
-
|
72 |
-
|
73 |
-
class music_genre(datasets.GeneratorBasedBuilder):
|
74 |
-
BUILDER_CONFIGS = [
|
75 |
-
|
76 |
-
|
77 |
-
]
|
78 |
-
|
79 |
-
def _info(self):
|
80 |
-
return datasets.DatasetInfo(
|
81 |
-
features=(
|
82 |
-
datasets.Features(
|
83 |
-
{
|
84 |
-
"audio": datasets.Audio(sampling_rate=22050),
|
85 |
-
"mel": datasets.Image(),
|
86 |
-
"fst_level_label": datasets.features.ClassLabel(
|
87 |
-
names=list(_NAMES_1.values())
|
88 |
-
),
|
89 |
-
"sec_level_label": datasets.features.ClassLabel(
|
90 |
-
names=list(_NAMES_2.values())
|
91 |
-
),
|
92 |
-
"thr_level_label": datasets.features.ClassLabel(
|
93 |
-
names=list(_NAMES_3.values())
|
94 |
-
),
|
95 |
-
}
|
96 |
-
)
|
97 |
-
if self.config.name == "
|
98 |
-
else datasets.Features(
|
99 |
-
{
|
100 |
-
"mel": datasets.Image(),
|
101 |
-
"cqt": datasets.Image(),
|
102 |
-
"chroma": datasets.Image(),
|
103 |
-
"fst_level_label": datasets.features.ClassLabel(
|
104 |
-
names=list(_NAMES_1.values())
|
105 |
-
),
|
106 |
-
"sec_level_label": datasets.features.ClassLabel(
|
107 |
-
names=list(_NAMES_2.values())
|
108 |
-
),
|
109 |
-
"thr_level_label": datasets.features.ClassLabel(
|
110 |
-
names=list(_NAMES_3.values())
|
111 |
-
),
|
112 |
-
}
|
113 |
-
)
|
114 |
-
),
|
115 |
-
supervised_keys=("mel", "sec_level_label"),
|
116 |
-
homepage=_HOMEPAGE,
|
117 |
-
license="CC-BY-NC-ND",
|
118 |
-
version="1.2.0",
|
119 |
-
citation=_CITATION,
|
120 |
-
description=_DESCRIPTION,
|
121 |
-
task_templates=[
|
122 |
-
ImageClassification(
|
123 |
-
task="image-classification",
|
124 |
-
image_column="mel",
|
125 |
-
label_column="sec_level_label",
|
126 |
-
)
|
127 |
-
],
|
128 |
-
)
|
129 |
-
|
130 |
-
def _split_generators(self, dl_manager):
|
131 |
-
dataset = []
|
132 |
-
if self.config.name == "
|
133 |
-
files = {}
|
134 |
-
audio_files = dl_manager.download_and_extract(_URLS["audio"])
|
135 |
-
mel_files = dl_manager.download_and_extract(_URLS["mel"])
|
136 |
-
for path in dl_manager.iter_files([audio_files]):
|
137 |
-
fname: str = os.path.basename(path)
|
138 |
-
if fname.endswith(".mp3"):
|
139 |
-
files[fname.split(".mp")[0]] = {"audio": path}
|
140 |
-
|
141 |
-
for path in dl_manager.iter_files([mel_files]):
|
142 |
-
fname = os.path.basename(path)
|
143 |
-
if fname.endswith(".jpg"):
|
144 |
-
files[fname.split(".jp")[0]]["mel"] = path
|
145 |
-
|
146 |
-
dataset = list(files.values())
|
147 |
-
|
148 |
-
else:
|
149 |
-
data_files = dl_manager.download_and_extract(_URLS["eval"])
|
150 |
-
for path in dl_manager.iter_files([data_files]):
|
151 |
-
if os.path.basename(path).endswith(".jpg") and "mel" in path:
|
152 |
-
dataset.append(
|
153 |
-
{
|
154 |
-
"mel": path,
|
155 |
-
"cqt": path.replace("\\mel\\", "\\cqt\\").replace(
|
156 |
-
"/mel/", "/cqt/"
|
157 |
-
),
|
158 |
-
"chroma": path.replace("\\mel\\", "\\chroma\\").replace(
|
159 |
-
"/mel/", "/chroma/"
|
160 |
-
),
|
161 |
-
}
|
162 |
-
)
|
163 |
-
|
164 |
-
random.shuffle(dataset)
|
165 |
-
data_count = len(dataset)
|
166 |
-
p80 = int(data_count * 0.8)
|
167 |
-
p90 = int(data_count * 0.9)
|
168 |
-
|
169 |
-
return [
|
170 |
-
datasets.SplitGenerator(
|
171 |
-
name=datasets.Split.TRAIN,
|
172 |
-
gen_kwargs={"files": dataset[:p80]},
|
173 |
-
),
|
174 |
-
datasets.SplitGenerator(
|
175 |
-
name=datasets.Split.VALIDATION,
|
176 |
-
gen_kwargs={"files": dataset[p80:p90]},
|
177 |
-
),
|
178 |
-
datasets.SplitGenerator(
|
179 |
-
name=datasets.Split.TEST,
|
180 |
-
gen_kwargs={"files": dataset[p90:]},
|
181 |
-
),
|
182 |
-
]
|
183 |
-
|
184 |
-
def _calc_label(self, path, depth, substr="/mel/"):
|
185 |
-
spect = substr
|
186 |
-
dirpath: str = os.path.dirname(path)
|
187 |
-
substr_index = dirpath.find(spect)
|
188 |
-
if substr_index < 0:
|
189 |
-
spect = spect.replace("/", "\\")
|
190 |
-
substr_index = dirpath.find(spect)
|
191 |
-
|
192 |
-
labstr = dirpath[substr_index + len(spect) :]
|
193 |
-
labs = labstr.split("/")
|
194 |
-
if len(labs) < 2:
|
195 |
-
labs = labstr.split("\\")
|
196 |
-
|
197 |
-
if depth <= len(labs):
|
198 |
-
return int(labs[depth - 1].split("_")[0])
|
199 |
-
else:
|
200 |
-
return int(labs[-1].split("_")[0])
|
201 |
-
|
202 |
-
def _generate_examples(self, files):
|
203 |
-
if self.config.name == "
|
204 |
-
for i, path in enumerate(files):
|
205 |
-
yield i, {
|
206 |
-
"audio": path["audio"],
|
207 |
-
"mel": path["mel"],
|
208 |
-
"fst_level_label": _NAMES_1[self._calc_label(path["mel"], 1)],
|
209 |
-
"sec_level_label": _NAMES_2[self._calc_label(path["mel"], 2)],
|
210 |
-
"thr_level_label": _NAMES_3[self._calc_label(path["mel"], 3)],
|
211 |
-
}
|
212 |
-
|
213 |
-
else:
|
214 |
-
for i, path in enumerate(files):
|
215 |
-
yield i, {
|
216 |
-
"mel": path["mel"],
|
217 |
-
"cqt": path["cqt"],
|
218 |
-
"chroma": path["chroma"],
|
219 |
-
"fst_level_label": _NAMES_1[self._calc_label(path["mel"], 1)],
|
220 |
-
"sec_level_label": _NAMES_2[self._calc_label(path["mel"], 2)],
|
221 |
-
"thr_level_label": _NAMES_3[self._calc_label(path["mel"], 3)],
|
222 |
-
}
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import datasets
|
4 |
+
from datasets.tasks import ImageClassification
|
5 |
+
|
6 |
+
_NAMES_1 = {
|
7 |
+
1: "Classic",
|
8 |
+
2: "Non_classic",
|
9 |
+
}
|
10 |
+
|
11 |
+
_NAMES_2 = {
|
12 |
+
3: "Symphony",
|
13 |
+
4: "Opera",
|
14 |
+
5: "Solo",
|
15 |
+
6: "Chamber",
|
16 |
+
7: "Pop",
|
17 |
+
8: "Dance_and_house",
|
18 |
+
9: "Indie",
|
19 |
+
10: "Soul_or_RnB",
|
20 |
+
11: "Rock",
|
21 |
+
}
|
22 |
+
|
23 |
+
_NAMES_3 = {
|
24 |
+
3: "Symphony",
|
25 |
+
4: "Opera",
|
26 |
+
5: "Solo",
|
27 |
+
6: "Chamber",
|
28 |
+
12: "Pop_vocal_ballad",
|
29 |
+
13: "Adult_contemporary",
|
30 |
+
14: "Teen_pop",
|
31 |
+
15: "Contemporary_dance_pop",
|
32 |
+
16: "Dance_pop",
|
33 |
+
17: "Classic_indie_pop",
|
34 |
+
18: "Chamber_cabaret_and_art_pop",
|
35 |
+
10: "Soul_or_RnB",
|
36 |
+
19: "Adult_alternative_rock",
|
37 |
+
20: "Uplifting_anthemic_rock",
|
38 |
+
21: "Soft_rock",
|
39 |
+
22: "Acoustic_pop",
|
40 |
+
}
|
41 |
+
|
42 |
+
_DBNAME = os.path.basename(__file__).split(".")[0]
|
43 |
+
|
44 |
+
_HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{_DBNAME}"
|
45 |
+
|
46 |
+
_DOMAIN = f"https://www.modelscope.cn/api/v1/datasets/ccmusic-database/{_DBNAME}/repo?Revision=master&FilePath=data"
|
47 |
+
|
48 |
+
_CITATION = """\
|
49 |
+
@dataset{zhaorui_liu_2021_5676893,
|
50 |
+
author = {Monan Zhou, Shenyang Xu, Zhaorui Liu, Zhaowen Wang, Feng Yu, Wei Li and Baoqiang Han},
|
51 |
+
title = {CCMusic: an Open and Diverse Database for Chinese and General Music Information Retrieval Research},
|
52 |
+
month = {mar},
|
53 |
+
year = {2024},
|
54 |
+
publisher = {HuggingFace},
|
55 |
+
version = {1.2},
|
56 |
+
url = {https://huggingface.co/ccmusic-database}
|
57 |
+
}
|
58 |
+
"""
|
59 |
+
|
60 |
+
_DESCRIPTION = """\
|
61 |
+
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.
|
62 |
+
|
63 |
+
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.
|
64 |
+
"""
|
65 |
+
|
66 |
+
_URLS = {
|
67 |
+
"audio": f"{_DOMAIN}/audio.zip",
|
68 |
+
"mel": f"{_DOMAIN}/mel.zip",
|
69 |
+
"eval": f"{_DOMAIN}/eval.zip",
|
70 |
+
}
|
71 |
+
|
72 |
+
|
73 |
+
class music_genre(datasets.GeneratorBasedBuilder):
|
74 |
+
# BUILDER_CONFIGS = [
|
75 |
+
# datasets.BuilderConfig(name="default"),
|
76 |
+
# datasets.BuilderConfig(name="eval"),
|
77 |
+
# ]
|
78 |
+
|
79 |
+
def _info(self):
|
80 |
+
return datasets.DatasetInfo(
|
81 |
+
features=(
|
82 |
+
datasets.Features(
|
83 |
+
{
|
84 |
+
"audio": datasets.Audio(sampling_rate=22050),
|
85 |
+
"mel": datasets.Image(),
|
86 |
+
"fst_level_label": datasets.features.ClassLabel(
|
87 |
+
names=list(_NAMES_1.values())
|
88 |
+
),
|
89 |
+
"sec_level_label": datasets.features.ClassLabel(
|
90 |
+
names=list(_NAMES_2.values())
|
91 |
+
),
|
92 |
+
"thr_level_label": datasets.features.ClassLabel(
|
93 |
+
names=list(_NAMES_3.values())
|
94 |
+
),
|
95 |
+
}
|
96 |
+
)
|
97 |
+
if self.config.name == "raw"
|
98 |
+
else datasets.Features(
|
99 |
+
{
|
100 |
+
"mel": datasets.Image(),
|
101 |
+
"cqt": datasets.Image(),
|
102 |
+
"chroma": datasets.Image(),
|
103 |
+
"fst_level_label": datasets.features.ClassLabel(
|
104 |
+
names=list(_NAMES_1.values())
|
105 |
+
),
|
106 |
+
"sec_level_label": datasets.features.ClassLabel(
|
107 |
+
names=list(_NAMES_2.values())
|
108 |
+
),
|
109 |
+
"thr_level_label": datasets.features.ClassLabel(
|
110 |
+
names=list(_NAMES_3.values())
|
111 |
+
),
|
112 |
+
}
|
113 |
+
)
|
114 |
+
),
|
115 |
+
supervised_keys=("mel", "sec_level_label"),
|
116 |
+
homepage=_HOMEPAGE,
|
117 |
+
license="CC-BY-NC-ND",
|
118 |
+
version="1.2.0",
|
119 |
+
citation=_CITATION,
|
120 |
+
description=_DESCRIPTION,
|
121 |
+
task_templates=[
|
122 |
+
ImageClassification(
|
123 |
+
task="image-classification",
|
124 |
+
image_column="mel",
|
125 |
+
label_column="sec_level_label",
|
126 |
+
)
|
127 |
+
],
|
128 |
+
)
|
129 |
+
|
130 |
+
def _split_generators(self, dl_manager):
|
131 |
+
dataset = []
|
132 |
+
if self.config.name == "raw":
|
133 |
+
files = {}
|
134 |
+
audio_files = dl_manager.download_and_extract(_URLS["audio"])
|
135 |
+
mel_files = dl_manager.download_and_extract(_URLS["mel"])
|
136 |
+
for path in dl_manager.iter_files([audio_files]):
|
137 |
+
fname: str = os.path.basename(path)
|
138 |
+
if fname.endswith(".mp3"):
|
139 |
+
files[fname.split(".mp")[0]] = {"audio": path}
|
140 |
+
|
141 |
+
for path in dl_manager.iter_files([mel_files]):
|
142 |
+
fname = os.path.basename(path)
|
143 |
+
if fname.endswith(".jpg"):
|
144 |
+
files[fname.split(".jp")[0]]["mel"] = path
|
145 |
+
|
146 |
+
dataset = list(files.values())
|
147 |
+
|
148 |
+
else:
|
149 |
+
data_files = dl_manager.download_and_extract(_URLS["eval"])
|
150 |
+
for path in dl_manager.iter_files([data_files]):
|
151 |
+
if os.path.basename(path).endswith(".jpg") and "mel" in path:
|
152 |
+
dataset.append(
|
153 |
+
{
|
154 |
+
"mel": path,
|
155 |
+
"cqt": path.replace("\\mel\\", "\\cqt\\").replace(
|
156 |
+
"/mel/", "/cqt/"
|
157 |
+
),
|
158 |
+
"chroma": path.replace("\\mel\\", "\\chroma\\").replace(
|
159 |
+
"/mel/", "/chroma/"
|
160 |
+
),
|
161 |
+
}
|
162 |
+
)
|
163 |
+
|
164 |
+
random.shuffle(dataset)
|
165 |
+
data_count = len(dataset)
|
166 |
+
p80 = int(data_count * 0.8)
|
167 |
+
p90 = int(data_count * 0.9)
|
168 |
+
|
169 |
+
return [
|
170 |
+
datasets.SplitGenerator(
|
171 |
+
name=datasets.Split.TRAIN,
|
172 |
+
gen_kwargs={"files": dataset[:p80]},
|
173 |
+
),
|
174 |
+
datasets.SplitGenerator(
|
175 |
+
name=datasets.Split.VALIDATION,
|
176 |
+
gen_kwargs={"files": dataset[p80:p90]},
|
177 |
+
),
|
178 |
+
datasets.SplitGenerator(
|
179 |
+
name=datasets.Split.TEST,
|
180 |
+
gen_kwargs={"files": dataset[p90:]},
|
181 |
+
),
|
182 |
+
]
|
183 |
+
|
184 |
+
def _calc_label(self, path, depth, substr="/mel/"):
|
185 |
+
spect = substr
|
186 |
+
dirpath: str = os.path.dirname(path)
|
187 |
+
substr_index = dirpath.find(spect)
|
188 |
+
if substr_index < 0:
|
189 |
+
spect = spect.replace("/", "\\")
|
190 |
+
substr_index = dirpath.find(spect)
|
191 |
+
|
192 |
+
labstr = dirpath[substr_index + len(spect) :]
|
193 |
+
labs = labstr.split("/")
|
194 |
+
if len(labs) < 2:
|
195 |
+
labs = labstr.split("\\")
|
196 |
+
|
197 |
+
if depth <= len(labs):
|
198 |
+
return int(labs[depth - 1].split("_")[0])
|
199 |
+
else:
|
200 |
+
return int(labs[-1].split("_")[0])
|
201 |
+
|
202 |
+
def _generate_examples(self, files):
|
203 |
+
if self.config.name == "raw":
|
204 |
+
for i, path in enumerate(files):
|
205 |
+
yield i, {
|
206 |
+
"audio": path["audio"],
|
207 |
+
"mel": path["mel"],
|
208 |
+
"fst_level_label": _NAMES_1[self._calc_label(path["mel"], 1)],
|
209 |
+
"sec_level_label": _NAMES_2[self._calc_label(path["mel"], 2)],
|
210 |
+
"thr_level_label": _NAMES_3[self._calc_label(path["mel"], 3)],
|
211 |
+
}
|
212 |
+
|
213 |
+
else:
|
214 |
+
for i, path in enumerate(files):
|
215 |
+
yield i, {
|
216 |
+
"mel": path["mel"],
|
217 |
+
"cqt": path["cqt"],
|
218 |
+
"chroma": path["chroma"],
|
219 |
+
"fst_level_label": _NAMES_1[self._calc_label(path["mel"], 1)],
|
220 |
+
"sec_level_label": _NAMES_2[self._calc_label(path["mel"], 2)],
|
221 |
+
"thr_level_label": _NAMES_3[self._calc_label(path["mel"], 3)],
|
222 |
+
}
|