Datasets:
File size: 7,706 Bytes
4cbd76f 2a33ba4 4cbd76f 2a33ba4 4cbd76f |
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
import os
import random
import datasets
from datasets.tasks import ImageClassification
_NAMES = {
"PearlRiver": [2.33, 2.53, 2.37, 2.41],
"YoungChang": [2.53, 2.63, 2.97, 2.71],
"Steinway-T": [3.6, 3.63, 3.67, 3.63],
"Hsinghai": [3.4, 3.27, 3.2, 3.29],
"Kawai": [3.17, 2.5, 2.93, 2.87],
"Steinway": [4.23, 3.67, 4, 3.97],
"Kawai-G": [3.37, 2.97, 3.07, 3.14],
"Yamaha": [3.23, 3.03, 3.17, 3.14],
}
_HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{os.path.basename(__file__)[:-3]}"
_DOMAIN = f"{_HOMEPAGE}/resolve/master/data"
_PITCHES = {
"009": "A2",
"010": "A2#/B2b",
"011": "B2",
"100": "C1",
"101": "C1#/D1b",
"102": "D1",
"103": "D1#/E1b",
"104": "E1",
"105": "F1",
"106": "F1#/G1b",
"107": "G1",
"108": "G1#/A1b",
"109": "A1",
"110": "A1#/B1b",
"111": "B1",
"200": "C",
"201": "C#/Db",
"202": "D",
"203": "D#/Eb",
"204": "E",
"205": "F",
"206": "F#/Gb",
"207": "G",
"208": "G#/Ab",
"209": "A",
"210": "A#/Bb",
"211": "B",
"300": "c",
"301": "c#/db",
"302": "d",
"303": "d#/eb",
"304": "e",
"305": "f",
"306": "f#/gb",
"307": "g",
"308": "g#/ab",
"309": "a",
"310": "a#/bb",
"311": "b",
"400": "c1",
"401": "c1#/d1b",
"402": "d1",
"403": "d1#/e1b",
"404": "e1",
"405": "f1",
"406": "f1#/g1b",
"407": "g1",
"408": "g1#/a1b",
"409": "a1",
"410": "a1#/b1b",
"411": "b1",
"500": "c2",
"501": "c2#/d2b",
"502": "d2",
"503": "d2#/e2b",
"504": "e2",
"505": "f2",
"506": "f2#/g2b",
"507": "g2",
"508": "g2#/a2b",
"509": "a2",
"510": "a2#/b2b",
"511": "b2",
"600": "c3",
"601": "c3#/d3b",
"602": "d3",
"603": "d3#/e3b",
"604": "e3",
"605": "f3",
"606": "f3#/g3b",
"607": "g3",
"608": "g3#/a3b",
"609": "a3",
"610": "a3#/b3b",
"611": "b3",
"700": "c4",
"701": "c4#/d4b",
"702": "d4",
"703": "d4#/e4b",
"704": "e4",
"705": "f4",
"706": "f4#/g4b",
"707": "g4",
"708": "g4#/a4b",
"709": "a4",
"710": "a4#/b4b",
"711": "b4",
"800": "c5",
}
_URLS = {
"audio": f"{_DOMAIN}/audio.zip",
"mel": f"{_DOMAIN}/mel.zip",
"eval": f"{_DOMAIN}/eval.zip",
}
class pianos(datasets.GeneratorBasedBuilder):
def _info(self):
names = list(_NAMES.keys())
if self.config.name == "default":
names = names[:-1]
return datasets.DatasetInfo(
features=(
datasets.Features(
{
"audio": datasets.Audio(sampling_rate=44100),
"mel": datasets.Image(),
"label": datasets.features.ClassLabel(names=names),
"pitch": datasets.features.ClassLabel(
names=list(_PITCHES.values())
),
"bass_score": datasets.Value("float32"),
"mid_score": datasets.Value("float32"),
"treble_score": datasets.Value("float32"),
"avg_score": datasets.Value("float32"),
}
)
if self.config.name != "eval"
else datasets.Features(
{
"mel": datasets.Image(),
"label": datasets.features.ClassLabel(names=names),
"pitch": datasets.features.ClassLabel(
names=list(_PITCHES.values())
),
"bass_score": datasets.Value("float32"),
"mid_score": datasets.Value("float32"),
"treble_score": datasets.Value("float32"),
"avg_score": datasets.Value("float32"),
}
)
),
homepage=_HOMEPAGE,
license="CC-BY-NC-ND",
version="1.2.0",
supervised_keys=("mel", "label"),
task_templates=ImageClassification(
image_column="mel",
label_column="label",
),
)
def _split_generators(self, dl_manager):
dataset = []
if self.config.name != "eval":
subset = {}
audio_files = dl_manager.download_and_extract(_URLS["audio"])
for path in dl_manager.iter_files([audio_files]):
fname = os.path.basename(path)
if fname.endswith(".wav"):
lebal = os.path.basename(os.path.dirname(path))
if self.config.name == "default" and lebal == "Yamaha":
continue
subset[fname.split(".")[0]] = {
"audio": path,
"label": lebal,
"pitch": _PITCHES[fname[1:4]],
"bass_score": _NAMES[lebal][0],
"mid_score": _NAMES[lebal][1],
"treble_score": _NAMES[lebal][2],
"avg_score": _NAMES[lebal][3],
}
mel_files = dl_manager.download_and_extract(_URLS["mel"])
for path in dl_manager.iter_files([mel_files]):
fname = os.path.basename(path)
pname = fname.split(".")[0]
if fname.endswith(".jpg") and pname in subset:
subset[pname]["mel"] = path
dataset = list(subset.values())
else:
data_files = dl_manager.download_and_extract(_URLS["eval"])
for path in dl_manager.iter_files([data_files]):
fname: str = os.path.basename(path)
if fname.endswith(".jpg"):
lebal = os.path.basename(os.path.dirname(path))
dataset.append(
{
"mel": path,
"label": lebal,
"pitch": _PITCHES[fname.split("_")[0]],
"bass_score": _NAMES[lebal][0],
"mid_score": _NAMES[lebal][1],
"treble_score": _NAMES[lebal][2],
"avg_score": _NAMES[lebal][3],
}
)
names = list(_NAMES.keys())
if self.config.name == "default":
names = names[:-1]
categories = {}
for name in names:
categories[name] = []
for data in dataset:
categories[data["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):
for i, path in enumerate(files):
yield i, path
|