Datasets:
admin
commited on
Commit
·
70ec2ad
1
Parent(s):
67200e3
syncs
Browse files
CTIS.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import random
|
| 3 |
import hashlib
|
| 4 |
import datasets
|
| 5 |
-
from datasets.tasks import
|
| 6 |
|
| 7 |
|
| 8 |
_HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{os.path.basename(__file__)[:-3]}"
|
|
@@ -98,9 +98,9 @@ _NAMES = {
|
|
| 98 |
"D0176": ["福(新)", "fu2_(reformed)"],
|
| 99 |
"D0177": ["禄(新)", "lu4_(reformed)"],
|
| 100 |
"D0178": ["寿(新)", "shou4_(reformed)"],
|
| 101 |
-
"D0179": ["宜春三星鼓福鼓老鼓", "
|
| 102 |
-
"D0180": ["宜春三星鼓禄鼓老鼓", "
|
| 103 |
-
"D0181": ["宜春三星鼓寿鼓老鼓", "
|
| 104 |
"D0184": ["宜春三星鼓双铛", "yi2_chun1_san1_xing1_gu3_shuang1_ding1"],
|
| 105 |
"D0185": ["宜春三星鼓单铛", "yi2_chun1_san1_xing1_gu3_dan1_ding1"],
|
| 106 |
"D0186": ["宜春三星鼓镲", "yi2_chun1_san1_xing1_gu3_chao3"],
|
|
@@ -234,29 +234,47 @@ _NAMES = {
|
|
| 234 |
_URLS = {
|
| 235 |
"audio": f"{_DOMAIN}/audio.zip",
|
| 236 |
"mel": f"{_DOMAIN}/mel.zip",
|
|
|
|
| 237 |
}
|
| 238 |
|
| 239 |
|
| 240 |
class CTIS(datasets.GeneratorBasedBuilder):
|
| 241 |
def _info(self):
|
| 242 |
return datasets.DatasetInfo(
|
| 243 |
-
features=
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
),
|
| 252 |
-
supervised_keys=("
|
| 253 |
homepage=_HOMEPAGE,
|
| 254 |
license="CC-BY-NC-ND",
|
| 255 |
version="1.2.0",
|
| 256 |
task_templates=[
|
| 257 |
-
|
| 258 |
-
task="
|
| 259 |
-
|
| 260 |
label_column="label",
|
| 261 |
)
|
| 262 |
],
|
|
@@ -268,40 +286,90 @@ class CTIS(datasets.GeneratorBasedBuilder):
|
|
| 268 |
return md5_obj.hexdigest()
|
| 269 |
|
| 270 |
def _split_generators(self, dl_manager):
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
files[item_id]["mel"] = fpath
|
| 289 |
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
def _generate_examples(self, files):
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import random
|
| 3 |
import hashlib
|
| 4 |
import datasets
|
| 5 |
+
from datasets.tasks import ImageClassification
|
| 6 |
|
| 7 |
|
| 8 |
_HOMEPAGE = f"https://www.modelscope.cn/datasets/ccmusic-database/{os.path.basename(__file__)[:-3]}"
|
|
|
|
| 98 |
"D0176": ["福(新)", "fu2_(reformed)"],
|
| 99 |
"D0177": ["禄(新)", "lu4_(reformed)"],
|
| 100 |
"D0178": ["寿(新)", "shou4_(reformed)"],
|
| 101 |
+
"D0179": ["宜春三星鼓福鼓老鼓", "yi2_chun1_san1_xing1_gu3_fu2_gu3_(traditional)"],
|
| 102 |
+
"D0180": ["宜春三星鼓禄鼓老鼓", "yi2_chun1_san1_xing1_gu3_lu4_gu3_(traditional)"],
|
| 103 |
+
"D0181": ["宜春三星鼓寿鼓老鼓", "yi2_chun1_san1_xing1_gu3_shou4_gu3_(traditional)"],
|
| 104 |
"D0184": ["宜春三星鼓双铛", "yi2_chun1_san1_xing1_gu3_shuang1_ding1"],
|
| 105 |
"D0185": ["宜春三星鼓单铛", "yi2_chun1_san1_xing1_gu3_dan1_ding1"],
|
| 106 |
"D0186": ["宜春三星鼓镲", "yi2_chun1_san1_xing1_gu3_chao3"],
|
|
|
|
| 234 |
_URLS = {
|
| 235 |
"audio": f"{_DOMAIN}/audio.zip",
|
| 236 |
"mel": f"{_DOMAIN}/mel.zip",
|
| 237 |
+
"eval": f"{_DOMAIN}/eval.zip",
|
| 238 |
}
|
| 239 |
|
| 240 |
|
| 241 |
class CTIS(datasets.GeneratorBasedBuilder):
|
| 242 |
def _info(self):
|
| 243 |
return datasets.DatasetInfo(
|
| 244 |
+
features=(
|
| 245 |
+
datasets.Features(
|
| 246 |
+
{
|
| 247 |
+
"audio": datasets.Audio(sampling_rate=44100),
|
| 248 |
+
"mel": datasets.Image(),
|
| 249 |
+
"label": datasets.features.ClassLabel(
|
| 250 |
+
names=list(_NAMES.keys())
|
| 251 |
+
),
|
| 252 |
+
"cname": datasets.Value("string"),
|
| 253 |
+
"pinyin": datasets.Value("string"),
|
| 254 |
+
}
|
| 255 |
+
)
|
| 256 |
+
if self.config.name == "default"
|
| 257 |
+
else (
|
| 258 |
+
datasets.Features(
|
| 259 |
+
{
|
| 260 |
+
"mel": datasets.Image(),
|
| 261 |
+
"cqt": datasets.Image(),
|
| 262 |
+
"chroma": datasets.Image(),
|
| 263 |
+
"label": datasets.features.ClassLabel(
|
| 264 |
+
names=list(_NAMES.keys())
|
| 265 |
+
),
|
| 266 |
+
}
|
| 267 |
+
)
|
| 268 |
+
)
|
| 269 |
),
|
| 270 |
+
supervised_keys=("mel", "label"),
|
| 271 |
homepage=_HOMEPAGE,
|
| 272 |
license="CC-BY-NC-ND",
|
| 273 |
version="1.2.0",
|
| 274 |
task_templates=[
|
| 275 |
+
ImageClassification(
|
| 276 |
+
task="image-classification",
|
| 277 |
+
image_column="mel",
|
| 278 |
label_column="label",
|
| 279 |
)
|
| 280 |
],
|
|
|
|
| 286 |
return md5_obj.hexdigest()
|
| 287 |
|
| 288 |
def _split_generators(self, dl_manager):
|
| 289 |
+
if self.config.name == "default":
|
| 290 |
+
files = {}
|
| 291 |
+
audio_files = dl_manager.download_and_extract(_URLS["audio"])
|
| 292 |
+
mel_files = dl_manager.download_and_extract(_URLS["mel"])
|
| 293 |
+
for fpath in dl_manager.iter_files([audio_files]):
|
| 294 |
+
fname = os.path.basename(fpath)
|
| 295 |
+
if fname.endswith(".wav"):
|
| 296 |
+
label = os.path.basename(os.path.dirname(fpath))
|
| 297 |
+
item_id = self._str2md5(label + fname.split(".wa")[0])
|
| 298 |
+
files[item_id] = {"audio": fpath}
|
| 299 |
|
| 300 |
+
for fpath in dl_manager.iter_files([mel_files]):
|
| 301 |
+
fname = os.path.basename(fpath)
|
| 302 |
+
if fname.endswith(".jpg"):
|
| 303 |
+
label = os.path.basename(os.path.dirname(fpath))
|
| 304 |
+
item_id = self._str2md5(label + fname.split(".jp")[0])
|
| 305 |
+
files[item_id]["mel"] = fpath
|
|
|
|
| 306 |
|
| 307 |
+
dataset = list(files.values())
|
| 308 |
+
random.shuffle(dataset)
|
| 309 |
+
return [
|
| 310 |
+
datasets.SplitGenerator(
|
| 311 |
+
name=datasets.Split.TRAIN,
|
| 312 |
+
gen_kwargs={"files": dataset},
|
| 313 |
+
),
|
| 314 |
+
]
|
| 315 |
+
|
| 316 |
+
else:
|
| 317 |
+
files = {key: [] for key in _NAMES}
|
| 318 |
+
data_files = dl_manager.download_and_extract(_URLS["eval"])
|
| 319 |
+
for fpath in dl_manager.iter_files([data_files]):
|
| 320 |
+
fname: str = os.path.basename(fpath)
|
| 321 |
+
if fname.endswith(".jpg") and "mel" in fpath:
|
| 322 |
+
label = os.path.basename(os.path.dirname(fpath))
|
| 323 |
+
files[label].append(fpath)
|
| 324 |
+
|
| 325 |
+
trainset, validset, testset = [], [], []
|
| 326 |
+
for cls in files:
|
| 327 |
+
random.shuffle(files[cls])
|
| 328 |
+
count = len(files[cls])
|
| 329 |
+
if count < 3:
|
| 330 |
+
raise ValueError(f"Class {cls} has items < 3 !")
|
| 331 |
+
|
| 332 |
+
p10 = max(count // 10, 1)
|
| 333 |
+
p20 = p10 * 2
|
| 334 |
+
trainset += files[cls][p20:]
|
| 335 |
+
validset += files[cls][p10:p20]
|
| 336 |
+
testset += files[cls][:p10]
|
| 337 |
+
|
| 338 |
+
random.shuffle(trainset)
|
| 339 |
+
random.shuffle(validset)
|
| 340 |
+
random.shuffle(testset)
|
| 341 |
+
return [
|
| 342 |
+
datasets.SplitGenerator(
|
| 343 |
+
name=datasets.Split.TRAIN,
|
| 344 |
+
gen_kwargs={"files": trainset},
|
| 345 |
+
),
|
| 346 |
+
datasets.SplitGenerator(
|
| 347 |
+
name=datasets.Split.VALIDATION,
|
| 348 |
+
gen_kwargs={"files": validset},
|
| 349 |
+
),
|
| 350 |
+
datasets.SplitGenerator(
|
| 351 |
+
name=datasets.Split.TEST,
|
| 352 |
+
gen_kwargs={"files": testset},
|
| 353 |
+
),
|
| 354 |
+
]
|
| 355 |
|
| 356 |
def _generate_examples(self, files):
|
| 357 |
+
if self.config.name == "default":
|
| 358 |
+
for i, item in enumerate(files):
|
| 359 |
+
label = os.path.basename(os.path.dirname(item["audio"]))
|
| 360 |
+
yield i, {
|
| 361 |
+
"audio": item["audio"],
|
| 362 |
+
"mel": item["mel"],
|
| 363 |
+
"label": label,
|
| 364 |
+
"cname": _NAMES[label][0],
|
| 365 |
+
"pinyin": _NAMES[label][1],
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
else:
|
| 369 |
+
for i, item in enumerate(files):
|
| 370 |
+
yield i, {
|
| 371 |
+
"mel": item,
|
| 372 |
+
"cqt": item.replace("mel", "cqt"),
|
| 373 |
+
"chroma": item.replace("mel", "chroma"),
|
| 374 |
+
"label": os.path.basename(os.path.dirname(item)),
|
| 375 |
+
}
|