projectlosangeles
commited on
Commit
•
bf0b976
1
Parent(s):
0dce85b
Upload 11 files
Browse files- Master_MIDI_Dataset_GPU_Search_and_Filter.ipynb +154 -22
- TMIDIX.py +24 -0
- master_midi_dataset_gpu_search_and_filter.py +128 -22
- midi_to_colab_audio.py +0 -0
Master_MIDI_Dataset_GPU_Search_and_Filter.ipynb
CHANGED
@@ -11,7 +11,7 @@
|
|
11 |
"id": "SiTIpPjArIyr"
|
12 |
},
|
13 |
"source": [
|
14 |
-
"# Master MIDI Dataset GPU Search and Filter (ver.
|
15 |
"\n",
|
16 |
"***\n",
|
17 |
"\n",
|
@@ -119,10 +119,15 @@
|
|
119 |
"import statistics\n",
|
120 |
"import shutil\n",
|
121 |
"\n",
|
|
|
|
|
|
|
122 |
"import cupy as cp\n",
|
123 |
"\n",
|
124 |
"from huggingface_hub import hf_hub_download\n",
|
125 |
"\n",
|
|
|
|
|
126 |
"print('Loading TMIDIX module...')\n",
|
127 |
"os.chdir('/content/Los-Angeles-MIDI-Dataset')\n",
|
128 |
"\n",
|
@@ -257,17 +262,15 @@
|
|
257 |
"random.shuffle(sigs_data)\n",
|
258 |
"\n",
|
259 |
"signatures_file_names = []\n",
|
260 |
-
"sigs_matrixes = [ [0]*(len(TMIDIX.ALL_CHORDS)+
|
261 |
"\n",
|
262 |
"idx = 0\n",
|
263 |
"for s in tqdm(sigs_data):\n",
|
264 |
"\n",
|
265 |
" signatures_file_names.append(s[0])\n",
|
266 |
"\n",
|
267 |
-
" counts_sum = sum([c[1] for c in s[1]])\n",
|
268 |
-
"\n",
|
269 |
" for ss in s[1]:\n",
|
270 |
-
" sigs_matrixes[idx][ss[0]] = ss[1]
|
271 |
"\n",
|
272 |
" idx += 1\n",
|
273 |
"\n",
|
@@ -305,10 +308,15 @@
|
|
305 |
"\n",
|
306 |
"#@markdown NOTE: You can stop the search at any time to render partial results\n",
|
307 |
"\n",
|
308 |
-
"number_of_top_matches_MIDIs_to_collect =
|
309 |
-
"search_matching_type = \"
|
|
|
|
|
|
|
|
|
310 |
"distances_norm_order = 3 # @param {type:\"slider\", min:1, max:10, step:1}\n",
|
311 |
-
"
|
|
|
312 |
"\n",
|
313 |
"print('=' * 70)\n",
|
314 |
"print('Master MIDI Dataset GPU Search and Filter')\n",
|
@@ -338,8 +346,8 @@
|
|
338 |
"\n",
|
339 |
" ###################\n",
|
340 |
"\n",
|
341 |
-
" if not os.path.exists('/content/Output-MIDI-Dataset'):\n",
|
342 |
-
" os.makedirs('/content/Output-MIDI-Dataset')\n",
|
343 |
"\n",
|
344 |
" ###################\n",
|
345 |
"\n",
|
@@ -368,6 +376,8 @@
|
|
368 |
" e[1] = int(e[1] / 16)\n",
|
369 |
" e[2] = int(e[2] / 16)\n",
|
370 |
"\n",
|
|
|
|
|
371 |
" src_sigs = []\n",
|
372 |
"\n",
|
373 |
" for i in range(-6, 6):\n",
|
@@ -380,10 +390,12 @@
|
|
380 |
" cscore = TMIDIX.chordify_score([1000, escore_copy])\n",
|
381 |
"\n",
|
382 |
" sig = []\n",
|
|
|
383 |
"\n",
|
384 |
" for c in cscore:\n",
|
385 |
"\n",
|
386 |
" pitches = sorted(set([p[4] for p in c if p[3] != 9]))\n",
|
|
|
387 |
"\n",
|
388 |
" if pitches:\n",
|
389 |
" if len(pitches) > 1:\n",
|
@@ -397,20 +409,28 @@
|
|
397 |
"\n",
|
398 |
" sig.append(sig_token)\n",
|
399 |
"\n",
|
400 |
-
"
|
|
|
|
|
|
|
|
|
|
|
401 |
"\n",
|
402 |
-
"
|
403 |
"\n",
|
404 |
-
"
|
405 |
"\n",
|
406 |
" for s in fsig:\n",
|
407 |
"\n",
|
408 |
-
" src_sig_mat[s[0]] = s[1]
|
409 |
"\n",
|
410 |
" src_sigs.append(src_sig_mat)\n",
|
411 |
"\n",
|
412 |
" src_signatures = cp.stack(cp.array(src_sigs))\n",
|
413 |
"\n",
|
|
|
|
|
|
|
414 |
" #=======================================================\n",
|
415 |
"\n",
|
416 |
" print('Searching for matches...Please wait...')\n",
|
@@ -432,18 +452,49 @@
|
|
432 |
"\n",
|
433 |
" for target_sig in tqdm(src_signatures):\n",
|
434 |
"\n",
|
435 |
-
" if
|
|
|
|
|
|
|
|
|
436 |
"\n",
|
437 |
-
"
|
438 |
-
"
|
439 |
"\n",
|
440 |
-
"
|
441 |
"\n",
|
442 |
-
"
|
|
|
|
|
|
|
443 |
"\n",
|
444 |
" distances = cp.power(cp.sum(cp.power(cp.abs(signatures_data - target_sig), distances_norm_order), axis=1), 1 / distances_norm_order)\n",
|
445 |
"\n",
|
446 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
447 |
"\n",
|
448 |
" unique_means = cp.unique(results)\n",
|
449 |
" sorted_means = cp.sort(unique_means)[::-1]\n",
|
@@ -462,7 +513,19 @@
|
|
462 |
"\n",
|
463 |
" tv_idx += 1\n",
|
464 |
"\n",
|
465 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
466 |
"\n",
|
467 |
" #=======================================================\n",
|
468 |
"\n",
|
@@ -487,7 +550,7 @@
|
|
487 |
" #=======================================================\n",
|
488 |
"\n",
|
489 |
" dir_str = str(fn1)\n",
|
490 |
-
" copy_path = '/content/Output-MIDI-Dataset/'+dir_str\n",
|
491 |
" if not os.path.exists(copy_path):\n",
|
492 |
" os.mkdir(copy_path)\n",
|
493 |
"\n",
|
@@ -534,6 +597,75 @@
|
|
534 |
"execution_count": null,
|
535 |
"outputs": []
|
536 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
537 |
{
|
538 |
"cell_type": "markdown",
|
539 |
"metadata": {
|
|
|
11 |
"id": "SiTIpPjArIyr"
|
12 |
},
|
13 |
"source": [
|
14 |
+
"# Master MIDI Dataset GPU Search and Filter (ver. 5.0)\n",
|
15 |
"\n",
|
16 |
"***\n",
|
17 |
"\n",
|
|
|
119 |
"import statistics\n",
|
120 |
"import shutil\n",
|
121 |
"\n",
|
122 |
+
"import locale\n",
|
123 |
+
"locale.getpreferredencoding = lambda: \"UTF-8\"\n",
|
124 |
+
"\n",
|
125 |
"import cupy as cp\n",
|
126 |
"\n",
|
127 |
"from huggingface_hub import hf_hub_download\n",
|
128 |
"\n",
|
129 |
+
"from google.colab import files\n",
|
130 |
+
"\n",
|
131 |
"print('Loading TMIDIX module...')\n",
|
132 |
"os.chdir('/content/Los-Angeles-MIDI-Dataset')\n",
|
133 |
"\n",
|
|
|
262 |
"random.shuffle(sigs_data)\n",
|
263 |
"\n",
|
264 |
"signatures_file_names = []\n",
|
265 |
+
"sigs_matrixes = [ [0]*(len(TMIDIX.ALL_CHORDS)+256) for i in range(len(sigs_data))]\n",
|
266 |
"\n",
|
267 |
"idx = 0\n",
|
268 |
"for s in tqdm(sigs_data):\n",
|
269 |
"\n",
|
270 |
" signatures_file_names.append(s[0])\n",
|
271 |
"\n",
|
|
|
|
|
272 |
" for ss in s[1]:\n",
|
273 |
+
" sigs_matrixes[idx][ss[0]] = ss[1]\n",
|
274 |
"\n",
|
275 |
" idx += 1\n",
|
276 |
"\n",
|
|
|
308 |
"\n",
|
309 |
"#@markdown NOTE: You can stop the search at any time to render partial results\n",
|
310 |
"\n",
|
311 |
+
"number_of_top_matches_MIDIs_to_collect = 30 #@param {type:\"slider\", min:5, max:50, step:1}\n",
|
312 |
+
"search_matching_type = \"Ratios\" # @param [\"Ratios\", \"Distances\", \"Correlations\"]\n",
|
313 |
+
"maximum_match_ratio_to_search_for = 1 #@param {type:\"slider\", min:0, max:1, step:0.001}\n",
|
314 |
+
"match_results_weight = 2 # @param {type:\"slider\", min:0.1, max:3, step:0.1}\n",
|
315 |
+
"match_lengths_weight = 1 # @param {type:\"slider\", min:0.1, max:3, step:0.1}\n",
|
316 |
+
"match_counts_weight = 1 # @param {type:\"slider\", min:0.1, max:3, step:0.1}\n",
|
317 |
"distances_norm_order = 3 # @param {type:\"slider\", min:1, max:10, step:1}\n",
|
318 |
+
"epsilon = 0.5 # @param {type:\"slider\", min:0.001, max:1, step:0.001}\n",
|
319 |
+
"match_drums = False # @param {type:\"boolean\"}\n",
|
320 |
"\n",
|
321 |
"print('=' * 70)\n",
|
322 |
"print('Master MIDI Dataset GPU Search and Filter')\n",
|
|
|
346 |
"\n",
|
347 |
" ###################\n",
|
348 |
"\n",
|
349 |
+
" if not os.path.exists('/content/Output-MIDI-Dataset/'+search_matching_type):\n",
|
350 |
+
" os.makedirs('/content/Output-MIDI-Dataset/'+search_matching_type)\n",
|
351 |
"\n",
|
352 |
" ###################\n",
|
353 |
"\n",
|
|
|
376 |
" e[1] = int(e[1] / 16)\n",
|
377 |
" e[2] = int(e[2] / 16)\n",
|
378 |
"\n",
|
379 |
+
" drums_offset = len(TMIDIX.ALL_CHORDS) + 128\n",
|
380 |
+
"\n",
|
381 |
" src_sigs = []\n",
|
382 |
"\n",
|
383 |
" for i in range(-6, 6):\n",
|
|
|
390 |
" cscore = TMIDIX.chordify_score([1000, escore_copy])\n",
|
391 |
"\n",
|
392 |
" sig = []\n",
|
393 |
+
" dsig = []\n",
|
394 |
"\n",
|
395 |
" for c in cscore:\n",
|
396 |
"\n",
|
397 |
" pitches = sorted(set([p[4] for p in c if p[3] != 9]))\n",
|
398 |
+
" drums = sorted(set([p[4] for p in c if p[3] == 9]))\n",
|
399 |
"\n",
|
400 |
" if pitches:\n",
|
401 |
" if len(pitches) > 1:\n",
|
|
|
409 |
"\n",
|
410 |
" sig.append(sig_token)\n",
|
411 |
"\n",
|
412 |
+
" if drums:\n",
|
413 |
+
" dsig.extend(drums)\n",
|
414 |
+
"\n",
|
415 |
+
" sig_p = dict.fromkeys(sig+dsig, 0)\n",
|
416 |
+
" for item in sig+dsig:\n",
|
417 |
+
" sig_p[item] += 1\n",
|
418 |
"\n",
|
419 |
+
" fsig = [list(v) for v in sig_p.items()]\n",
|
420 |
"\n",
|
421 |
+
" src_sig_mat = [0] * (len(TMIDIX.ALL_CHORDS)+256)\n",
|
422 |
"\n",
|
423 |
" for s in fsig:\n",
|
424 |
"\n",
|
425 |
+
" src_sig_mat[s[0]] = s[1]\n",
|
426 |
"\n",
|
427 |
" src_sigs.append(src_sig_mat)\n",
|
428 |
"\n",
|
429 |
" src_signatures = cp.stack(cp.array(src_sigs))\n",
|
430 |
"\n",
|
431 |
+
" if not match_drums:\n",
|
432 |
+
" src_signatures = cp.where(src_signatures < drums_offset, src_signatures, epsilon)\n",
|
433 |
+
"\n",
|
434 |
" #=======================================================\n",
|
435 |
"\n",
|
436 |
" print('Searching for matches...Please wait...')\n",
|
|
|
452 |
"\n",
|
453 |
" for target_sig in tqdm(src_signatures):\n",
|
454 |
"\n",
|
455 |
+
" if not match_drums:\n",
|
456 |
+
" target_sig = cp.where(target_sig < drums_offset, target_sig, epsilon)\n",
|
457 |
+
"\n",
|
458 |
+
" comps_lengths = cp.vstack((cp.repeat(cp.sum(target_sig != 0), signatures_data.shape[0]), cp.sum(signatures_data != 0, axis=1)))\n",
|
459 |
+
" comps_lengths_ratios = cp.divide(cp.min(comps_lengths, axis=0), cp.max(comps_lengths, axis=0))\n",
|
460 |
"\n",
|
461 |
+
" comps_counts_sums = cp.vstack((cp.repeat(cp.sum(target_sig), signatures_data.shape[0]), cp.sum(signatures_data, axis=1)))\n",
|
462 |
+
" comps_counts_sums_ratios = cp.divide(cp.min(comps_counts_sums, axis=0), cp.max(comps_counts_sums, axis=0))\n",
|
463 |
"\n",
|
464 |
+
" if search_matching_type == 'Ratios':\n",
|
465 |
"\n",
|
466 |
+
" ratios = cp.where(target_sig != 0, cp.divide(cp.minimum(signatures_data, target_sig), cp.maximum(signatures_data, target_sig)), epsilon)\n",
|
467 |
+
" results = cp.mean(ratios, axis=1)\n",
|
468 |
+
"\n",
|
469 |
+
" elif search_matching_type == 'Distances':\n",
|
470 |
"\n",
|
471 |
" distances = cp.power(cp.sum(cp.power(cp.abs(signatures_data - target_sig), distances_norm_order), axis=1), 1 / distances_norm_order)\n",
|
472 |
"\n",
|
473 |
+
" distances_mean = cp.mean(distances)\n",
|
474 |
+
" distances_std = cp.std(distances)\n",
|
475 |
+
"\n",
|
476 |
+
" results = 1 - cp.divide((distances - distances_mean), distances_std)\n",
|
477 |
+
"\n",
|
478 |
+
" elif search_matching_type == 'Correlations':\n",
|
479 |
+
"\n",
|
480 |
+
" main_array_mean = cp.mean(signatures_data, axis=1, keepdims=True)\n",
|
481 |
+
" main_array_std = cp.std(signatures_data, axis=1, keepdims=True)\n",
|
482 |
+
" target_array_mean = cp.mean(target_sig)\n",
|
483 |
+
" target_array_std = cp.std(target_sig)\n",
|
484 |
+
"\n",
|
485 |
+
" signatures_data_normalized = cp.where(main_array_std != 0, (signatures_data - main_array_mean) / main_array_std, epsilon)\n",
|
486 |
+
" target_sig_normalized = cp.where(target_array_std != 0, (target_sig - target_array_mean) / target_array_std, epsilon)\n",
|
487 |
+
"\n",
|
488 |
+
" correlations = cp.divide(cp.einsum('ij,j->i', signatures_data_normalized, target_sig_normalized), (signatures_data.shape[1] - 1))\n",
|
489 |
+
" scaled_correlations = cp.divide(correlations, cp.sqrt(cp.sum(correlations**2)))\n",
|
490 |
+
" exp = cp.exp(scaled_correlations - cp.max(scaled_correlations))\n",
|
491 |
+
" results = cp.multiply(cp.divide(exp, cp.sum(exp)), 1e5)\n",
|
492 |
+
"\n",
|
493 |
+
" results_weight = match_results_weight\n",
|
494 |
+
" comp_lengths_weight = match_lengths_weight\n",
|
495 |
+
" comp_counts_sums_weight = match_counts_weight\n",
|
496 |
+
"\n",
|
497 |
+
" results = cp.divide(cp.add(cp.add(results_weight, comp_lengths_weight), comp_counts_sums_weight), cp.add(cp.add(cp.divide(results_weight, cp.where(results !=0, results, epsilon)), cp.divide(comp_lengths_weight, cp.where(comps_lengths_ratios !=0, comps_lengths_ratios, epsilon))), cp.divide(comp_counts_sums_weight, cp.where(comps_counts_sums_ratios !=0, comps_counts_sums_ratios, epsilon))))\n",
|
498 |
"\n",
|
499 |
" unique_means = cp.unique(results)\n",
|
500 |
" sorted_means = cp.sort(unique_means)[::-1]\n",
|
|
|
513 |
"\n",
|
514 |
" tv_idx += 1\n",
|
515 |
"\n",
|
516 |
+
" f_results = sorted(zip(all_filtered_means, all_filtered_idxs, all_filtered_tvs), key=lambda x: x[0], reverse=True)\n",
|
517 |
+
"\n",
|
518 |
+
" triplet_dict = {}\n",
|
519 |
+
"\n",
|
520 |
+
" for triplet in f_results:\n",
|
521 |
+
"\n",
|
522 |
+
" if triplet[0] not in triplet_dict:\n",
|
523 |
+
" triplet_dict[triplet[0]] = triplet\n",
|
524 |
+
" else:\n",
|
525 |
+
" if triplet[2] == 0:\n",
|
526 |
+
" triplet_dict[triplet[0]] = triplet\n",
|
527 |
+
"\n",
|
528 |
+
" filtered_results = list(triplet_dict.values())[:filter_size]\n",
|
529 |
"\n",
|
530 |
" #=======================================================\n",
|
531 |
"\n",
|
|
|
550 |
" #=======================================================\n",
|
551 |
"\n",
|
552 |
" dir_str = str(fn1)\n",
|
553 |
+
" copy_path = '/content/Output-MIDI-Dataset/'+search_matching_type+'/'+dir_str\n",
|
554 |
" if not os.path.exists(copy_path):\n",
|
555 |
" os.mkdir(copy_path)\n",
|
556 |
"\n",
|
|
|
597 |
"execution_count": null,
|
598 |
"outputs": []
|
599 |
},
|
600 |
+
{
|
601 |
+
"cell_type": "markdown",
|
602 |
+
"source": [
|
603 |
+
"# (DOWNLOAD RESULTS)"
|
604 |
+
],
|
605 |
+
"metadata": {
|
606 |
+
"id": "7Lyy0vjV0dlI"
|
607 |
+
}
|
608 |
+
},
|
609 |
+
{
|
610 |
+
"cell_type": "code",
|
611 |
+
"source": [
|
612 |
+
"#@title Zip and download all search results\n",
|
613 |
+
"\n",
|
614 |
+
"print('=' * 70)\n",
|
615 |
+
"\n",
|
616 |
+
"try:\n",
|
617 |
+
" os.remove('Master_MIDI_Dataset_Search_Results.zip')\n",
|
618 |
+
"except OSError:\n",
|
619 |
+
" pass\n",
|
620 |
+
"\n",
|
621 |
+
"print('Zipping... Please wait...')\n",
|
622 |
+
"print('=' * 70)\n",
|
623 |
+
"\n",
|
624 |
+
"%cd /content/Output-MIDI-Dataset/\n",
|
625 |
+
"!zip -r Master_MIDI_Dataset_Search_Results.zip *\n",
|
626 |
+
"%cd /content/\n",
|
627 |
+
"\n",
|
628 |
+
"print('=' * 70)\n",
|
629 |
+
"print('Done!')\n",
|
630 |
+
"print('=' * 70)\n",
|
631 |
+
"\n",
|
632 |
+
"print('Downloading final zip file...')\n",
|
633 |
+
"print('=' * 70)\n",
|
634 |
+
"\n",
|
635 |
+
"files.download('/content/Output-MIDI-Dataset/Master_MIDI_Dataset_Search_Results.zip')\n",
|
636 |
+
"\n",
|
637 |
+
"print('Done!')\n",
|
638 |
+
"print('=' * 70)"
|
639 |
+
],
|
640 |
+
"metadata": {
|
641 |
+
"cellView": "form",
|
642 |
+
"id": "1psdj0RJ0aWH"
|
643 |
+
},
|
644 |
+
"execution_count": null,
|
645 |
+
"outputs": []
|
646 |
+
},
|
647 |
+
{
|
648 |
+
"cell_type": "code",
|
649 |
+
"source": [
|
650 |
+
"# @title Delete search results directory and files\n",
|
651 |
+
"\n",
|
652 |
+
"#@markdown WARNING: This can't be undone so make sure you downloaded the search results first\n",
|
653 |
+
"\n",
|
654 |
+
"print('=' * 70)\n",
|
655 |
+
"print('Deleting... Please wait...')\n",
|
656 |
+
"print('=' * 70)\n",
|
657 |
+
"\n",
|
658 |
+
"!rm -rf /content/Output-MIDI-Dataset\n",
|
659 |
+
"print('Done!')\n",
|
660 |
+
"print('=' * 70)"
|
661 |
+
],
|
662 |
+
"metadata": {
|
663 |
+
"cellView": "form",
|
664 |
+
"id": "z3B-YHIz0jDt"
|
665 |
+
},
|
666 |
+
"execution_count": null,
|
667 |
+
"outputs": []
|
668 |
+
},
|
669 |
{
|
670 |
"cell_type": "markdown",
|
671 |
"metadata": {
|
TMIDIX.py
CHANGED
@@ -1461,6 +1461,7 @@ import tqdm
|
|
1461 |
|
1462 |
from itertools import zip_longest
|
1463 |
from itertools import groupby
|
|
|
1464 |
|
1465 |
from operator import itemgetter
|
1466 |
|
@@ -4538,6 +4539,29 @@ def check_and_fix_tones_chord(tones_chord):
|
|
4538 |
|
4539 |
###################################################################################
|
4540 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4541 |
# This is the end of the TMIDI X Python module
|
4542 |
|
4543 |
###################################################################################
|
|
|
1461 |
|
1462 |
from itertools import zip_longest
|
1463 |
from itertools import groupby
|
1464 |
+
from collections import Counter
|
1465 |
|
1466 |
from operator import itemgetter
|
1467 |
|
|
|
4539 |
|
4540 |
###################################################################################
|
4541 |
|
4542 |
+
def create_similarity_matrix(list_of_values, matrix_length=0):
|
4543 |
+
|
4544 |
+
counts = Counter(list_of_values).items()
|
4545 |
+
|
4546 |
+
if matrix_length > 0:
|
4547 |
+
sim_matrix = [0] * max(matrix_length, len(list_of_values))
|
4548 |
+
else:
|
4549 |
+
sim_matrix = [0] * len(counts)
|
4550 |
+
|
4551 |
+
for c in counts:
|
4552 |
+
sim_matrix[c[0]] = c[1]
|
4553 |
+
|
4554 |
+
similarity_matrix = [[0] * len(sim_matrix) for _ in range(len(sim_matrix))]
|
4555 |
+
|
4556 |
+
for i in range(len(sim_matrix)):
|
4557 |
+
for j in range(len(sim_matrix)):
|
4558 |
+
if max(sim_matrix[i], sim_matrix[j]) != 0:
|
4559 |
+
similarity_matrix[i][j] = min(sim_matrix[i], sim_matrix[j]) / max(sim_matrix[i], sim_matrix[j])
|
4560 |
+
|
4561 |
+
return similarity_matrix, sim_matrix
|
4562 |
+
|
4563 |
+
###################################################################################
|
4564 |
+
|
4565 |
# This is the end of the TMIDI X Python module
|
4566 |
|
4567 |
###################################################################################
|
master_midi_dataset_gpu_search_and_filter.py
CHANGED
@@ -6,7 +6,7 @@ Automatically generated by Colaboratory.
|
|
6 |
Original file is located at
|
7 |
https://colab.research.google.com/github/asigalov61/Los-Angeles-MIDI-Dataset/blob/main/Extras/Master_MIDI_Dataset_GPU_Search_and_Filter.ipynb
|
8 |
|
9 |
-
# Master MIDI Dataset GPU Search and Filter (ver.
|
10 |
|
11 |
***
|
12 |
|
@@ -49,10 +49,15 @@ import pprint
|
|
49 |
import statistics
|
50 |
import shutil
|
51 |
|
|
|
|
|
|
|
52 |
import cupy as cp
|
53 |
|
54 |
from huggingface_hub import hf_hub_download
|
55 |
|
|
|
|
|
56 |
print('Loading TMIDIX module...')
|
57 |
os.chdir('/content/Los-Angeles-MIDI-Dataset')
|
58 |
|
@@ -142,17 +147,15 @@ print('=' * 70)
|
|
142 |
random.shuffle(sigs_data)
|
143 |
|
144 |
signatures_file_names = []
|
145 |
-
sigs_matrixes = [ [0]*(len(TMIDIX.ALL_CHORDS)+
|
146 |
|
147 |
idx = 0
|
148 |
for s in tqdm(sigs_data):
|
149 |
|
150 |
signatures_file_names.append(s[0])
|
151 |
|
152 |
-
counts_sum = sum([c[1] for c in s[1]])
|
153 |
-
|
154 |
for ss in s[1]:
|
155 |
-
sigs_matrixes[idx][ss[0]] = ss[1]
|
156 |
|
157 |
idx += 1
|
158 |
|
@@ -174,10 +177,15 @@ print('=' * 70)
|
|
174 |
|
175 |
#@markdown NOTE: You can stop the search at any time to render partial results
|
176 |
|
177 |
-
number_of_top_matches_MIDIs_to_collect =
|
178 |
-
search_matching_type = "
|
|
|
|
|
|
|
|
|
179 |
distances_norm_order = 3 # @param {type:"slider", min:1, max:10, step:1}
|
180 |
-
|
|
|
181 |
|
182 |
print('=' * 70)
|
183 |
print('Master MIDI Dataset GPU Search and Filter')
|
@@ -207,8 +215,8 @@ if filez:
|
|
207 |
|
208 |
###################
|
209 |
|
210 |
-
if not os.path.exists('/content/Output-MIDI-Dataset'):
|
211 |
-
os.makedirs('/content/Output-MIDI-Dataset')
|
212 |
|
213 |
###################
|
214 |
|
@@ -237,6 +245,8 @@ if filez:
|
|
237 |
e[1] = int(e[1] / 16)
|
238 |
e[2] = int(e[2] / 16)
|
239 |
|
|
|
|
|
240 |
src_sigs = []
|
241 |
|
242 |
for i in range(-6, 6):
|
@@ -249,10 +259,12 @@ if filez:
|
|
249 |
cscore = TMIDIX.chordify_score([1000, escore_copy])
|
250 |
|
251 |
sig = []
|
|
|
252 |
|
253 |
for c in cscore:
|
254 |
|
255 |
pitches = sorted(set([p[4] for p in c if p[3] != 9]))
|
|
|
256 |
|
257 |
if pitches:
|
258 |
if len(pitches) > 1:
|
@@ -266,20 +278,28 @@ if filez:
|
|
266 |
|
267 |
sig.append(sig_token)
|
268 |
|
269 |
-
|
|
|
|
|
|
|
|
|
|
|
270 |
|
271 |
-
|
272 |
|
273 |
-
|
274 |
|
275 |
for s in fsig:
|
276 |
|
277 |
-
src_sig_mat[s[0]] = s[1]
|
278 |
|
279 |
src_sigs.append(src_sig_mat)
|
280 |
|
281 |
src_signatures = cp.stack(cp.array(src_sigs))
|
282 |
|
|
|
|
|
|
|
283 |
#=======================================================
|
284 |
|
285 |
print('Searching for matches...Please wait...')
|
@@ -301,18 +321,49 @@ if filez:
|
|
301 |
|
302 |
for target_sig in tqdm(src_signatures):
|
303 |
|
304 |
-
if
|
|
|
305 |
|
306 |
-
|
307 |
-
|
308 |
|
309 |
-
|
|
|
310 |
|
311 |
-
|
|
|
|
|
|
|
|
|
|
|
312 |
|
313 |
distances = cp.power(cp.sum(cp.power(cp.abs(signatures_data - target_sig), distances_norm_order), axis=1), 1 / distances_norm_order)
|
314 |
|
315 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
|
317 |
unique_means = cp.unique(results)
|
318 |
sorted_means = cp.sort(unique_means)[::-1]
|
@@ -331,7 +382,19 @@ if filez:
|
|
331 |
|
332 |
tv_idx += 1
|
333 |
|
334 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
335 |
|
336 |
#=======================================================
|
337 |
|
@@ -356,7 +419,7 @@ if filez:
|
|
356 |
#=======================================================
|
357 |
|
358 |
dir_str = str(fn1)
|
359 |
-
copy_path = '/content/Output-MIDI-Dataset/'+dir_str
|
360 |
if not os.path.exists(copy_path):
|
361 |
os.mkdir(copy_path)
|
362 |
|
@@ -396,4 +459,47 @@ else:
|
|
396 |
print('Could not find any MIDI files. Please check Dataset dir...')
|
397 |
print('=' * 70)
|
398 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
399 |
"""# Congrats! You did it! :)"""
|
|
|
6 |
Original file is located at
|
7 |
https://colab.research.google.com/github/asigalov61/Los-Angeles-MIDI-Dataset/blob/main/Extras/Master_MIDI_Dataset_GPU_Search_and_Filter.ipynb
|
8 |
|
9 |
+
# Master MIDI Dataset GPU Search and Filter (ver. 5.0)
|
10 |
|
11 |
***
|
12 |
|
|
|
49 |
import statistics
|
50 |
import shutil
|
51 |
|
52 |
+
import locale
|
53 |
+
locale.getpreferredencoding = lambda: "UTF-8"
|
54 |
+
|
55 |
import cupy as cp
|
56 |
|
57 |
from huggingface_hub import hf_hub_download
|
58 |
|
59 |
+
from google.colab import files
|
60 |
+
|
61 |
print('Loading TMIDIX module...')
|
62 |
os.chdir('/content/Los-Angeles-MIDI-Dataset')
|
63 |
|
|
|
147 |
random.shuffle(sigs_data)
|
148 |
|
149 |
signatures_file_names = []
|
150 |
+
sigs_matrixes = [ [0]*(len(TMIDIX.ALL_CHORDS)+256) for i in range(len(sigs_data))]
|
151 |
|
152 |
idx = 0
|
153 |
for s in tqdm(sigs_data):
|
154 |
|
155 |
signatures_file_names.append(s[0])
|
156 |
|
|
|
|
|
157 |
for ss in s[1]:
|
158 |
+
sigs_matrixes[idx][ss[0]] = ss[1]
|
159 |
|
160 |
idx += 1
|
161 |
|
|
|
177 |
|
178 |
#@markdown NOTE: You can stop the search at any time to render partial results
|
179 |
|
180 |
+
number_of_top_matches_MIDIs_to_collect = 30 #@param {type:"slider", min:5, max:50, step:1}
|
181 |
+
search_matching_type = "Ratios" # @param ["Ratios", "Distances", "Correlations"]
|
182 |
+
maximum_match_ratio_to_search_for = 1 #@param {type:"slider", min:0, max:1, step:0.001}
|
183 |
+
match_results_weight = 2 # @param {type:"slider", min:0.1, max:3, step:0.1}
|
184 |
+
match_lengths_weight = 1 # @param {type:"slider", min:0.1, max:3, step:0.1}
|
185 |
+
match_counts_weight = 1 # @param {type:"slider", min:0.1, max:3, step:0.1}
|
186 |
distances_norm_order = 3 # @param {type:"slider", min:1, max:10, step:1}
|
187 |
+
epsilon = 0.5 # @param {type:"slider", min:0.001, max:1, step:0.001}
|
188 |
+
match_drums = False # @param {type:"boolean"}
|
189 |
|
190 |
print('=' * 70)
|
191 |
print('Master MIDI Dataset GPU Search and Filter')
|
|
|
215 |
|
216 |
###################
|
217 |
|
218 |
+
if not os.path.exists('/content/Output-MIDI-Dataset/'+search_matching_type):
|
219 |
+
os.makedirs('/content/Output-MIDI-Dataset/'+search_matching_type)
|
220 |
|
221 |
###################
|
222 |
|
|
|
245 |
e[1] = int(e[1] / 16)
|
246 |
e[2] = int(e[2] / 16)
|
247 |
|
248 |
+
drums_offset = len(TMIDIX.ALL_CHORDS) + 128
|
249 |
+
|
250 |
src_sigs = []
|
251 |
|
252 |
for i in range(-6, 6):
|
|
|
259 |
cscore = TMIDIX.chordify_score([1000, escore_copy])
|
260 |
|
261 |
sig = []
|
262 |
+
dsig = []
|
263 |
|
264 |
for c in cscore:
|
265 |
|
266 |
pitches = sorted(set([p[4] for p in c if p[3] != 9]))
|
267 |
+
drums = sorted(set([p[4] for p in c if p[3] == 9]))
|
268 |
|
269 |
if pitches:
|
270 |
if len(pitches) > 1:
|
|
|
278 |
|
279 |
sig.append(sig_token)
|
280 |
|
281 |
+
if drums:
|
282 |
+
dsig.extend(drums)
|
283 |
+
|
284 |
+
sig_p = dict.fromkeys(sig+dsig, 0)
|
285 |
+
for item in sig+dsig:
|
286 |
+
sig_p[item] += 1
|
287 |
|
288 |
+
fsig = [list(v) for v in sig_p.items()]
|
289 |
|
290 |
+
src_sig_mat = [0] * (len(TMIDIX.ALL_CHORDS)+256)
|
291 |
|
292 |
for s in fsig:
|
293 |
|
294 |
+
src_sig_mat[s[0]] = s[1]
|
295 |
|
296 |
src_sigs.append(src_sig_mat)
|
297 |
|
298 |
src_signatures = cp.stack(cp.array(src_sigs))
|
299 |
|
300 |
+
if not match_drums:
|
301 |
+
src_signatures = cp.where(src_signatures < drums_offset, src_signatures, epsilon)
|
302 |
+
|
303 |
#=======================================================
|
304 |
|
305 |
print('Searching for matches...Please wait...')
|
|
|
321 |
|
322 |
for target_sig in tqdm(src_signatures):
|
323 |
|
324 |
+
if not match_drums:
|
325 |
+
target_sig = cp.where(target_sig < drums_offset, target_sig, epsilon)
|
326 |
|
327 |
+
comps_lengths = cp.vstack((cp.repeat(cp.sum(target_sig != 0), signatures_data.shape[0]), cp.sum(signatures_data != 0, axis=1)))
|
328 |
+
comps_lengths_ratios = cp.divide(cp.min(comps_lengths, axis=0), cp.max(comps_lengths, axis=0))
|
329 |
|
330 |
+
comps_counts_sums = cp.vstack((cp.repeat(cp.sum(target_sig), signatures_data.shape[0]), cp.sum(signatures_data, axis=1)))
|
331 |
+
comps_counts_sums_ratios = cp.divide(cp.min(comps_counts_sums, axis=0), cp.max(comps_counts_sums, axis=0))
|
332 |
|
333 |
+
if search_matching_type == 'Ratios':
|
334 |
+
|
335 |
+
ratios = cp.where(target_sig != 0, cp.divide(cp.minimum(signatures_data, target_sig), cp.maximum(signatures_data, target_sig)), epsilon)
|
336 |
+
results = cp.mean(ratios, axis=1)
|
337 |
+
|
338 |
+
elif search_matching_type == 'Distances':
|
339 |
|
340 |
distances = cp.power(cp.sum(cp.power(cp.abs(signatures_data - target_sig), distances_norm_order), axis=1), 1 / distances_norm_order)
|
341 |
|
342 |
+
distances_mean = cp.mean(distances)
|
343 |
+
distances_std = cp.std(distances)
|
344 |
+
|
345 |
+
results = 1 - cp.divide((distances - distances_mean), distances_std)
|
346 |
+
|
347 |
+
elif search_matching_type == 'Correlations':
|
348 |
+
|
349 |
+
main_array_mean = cp.mean(signatures_data, axis=1, keepdims=True)
|
350 |
+
main_array_std = cp.std(signatures_data, axis=1, keepdims=True)
|
351 |
+
target_array_mean = cp.mean(target_sig)
|
352 |
+
target_array_std = cp.std(target_sig)
|
353 |
+
|
354 |
+
signatures_data_normalized = cp.where(main_array_std != 0, (signatures_data - main_array_mean) / main_array_std, epsilon)
|
355 |
+
target_sig_normalized = cp.where(target_array_std != 0, (target_sig - target_array_mean) / target_array_std, epsilon)
|
356 |
+
|
357 |
+
correlations = cp.divide(cp.einsum('ij,j->i', signatures_data_normalized, target_sig_normalized), (signatures_data.shape[1] - 1))
|
358 |
+
scaled_correlations = cp.divide(correlations, cp.sqrt(cp.sum(correlations**2)))
|
359 |
+
exp = cp.exp(scaled_correlations - cp.max(scaled_correlations))
|
360 |
+
results = cp.multiply(cp.divide(exp, cp.sum(exp)), 1e5)
|
361 |
+
|
362 |
+
results_weight = match_results_weight
|
363 |
+
comp_lengths_weight = match_lengths_weight
|
364 |
+
comp_counts_sums_weight = match_counts_weight
|
365 |
+
|
366 |
+
results = cp.divide(cp.add(cp.add(results_weight, comp_lengths_weight), comp_counts_sums_weight), cp.add(cp.add(cp.divide(results_weight, cp.where(results !=0, results, epsilon)), cp.divide(comp_lengths_weight, cp.where(comps_lengths_ratios !=0, comps_lengths_ratios, epsilon))), cp.divide(comp_counts_sums_weight, cp.where(comps_counts_sums_ratios !=0, comps_counts_sums_ratios, epsilon))))
|
367 |
|
368 |
unique_means = cp.unique(results)
|
369 |
sorted_means = cp.sort(unique_means)[::-1]
|
|
|
382 |
|
383 |
tv_idx += 1
|
384 |
|
385 |
+
f_results = sorted(zip(all_filtered_means, all_filtered_idxs, all_filtered_tvs), key=lambda x: x[0], reverse=True)
|
386 |
+
|
387 |
+
triplet_dict = {}
|
388 |
+
|
389 |
+
for triplet in f_results:
|
390 |
+
|
391 |
+
if triplet[0] not in triplet_dict:
|
392 |
+
triplet_dict[triplet[0]] = triplet
|
393 |
+
else:
|
394 |
+
if triplet[2] == 0:
|
395 |
+
triplet_dict[triplet[0]] = triplet
|
396 |
+
|
397 |
+
filtered_results = list(triplet_dict.values())[:filter_size]
|
398 |
|
399 |
#=======================================================
|
400 |
|
|
|
419 |
#=======================================================
|
420 |
|
421 |
dir_str = str(fn1)
|
422 |
+
copy_path = '/content/Output-MIDI-Dataset/'+search_matching_type+'/'+dir_str
|
423 |
if not os.path.exists(copy_path):
|
424 |
os.mkdir(copy_path)
|
425 |
|
|
|
459 |
print('Could not find any MIDI files. Please check Dataset dir...')
|
460 |
print('=' * 70)
|
461 |
|
462 |
+
"""# (DOWNLOAD RESULTS)"""
|
463 |
+
|
464 |
+
# Commented out IPython magic to ensure Python compatibility.
|
465 |
+
#@title Zip and download all search results
|
466 |
+
|
467 |
+
print('=' * 70)
|
468 |
+
|
469 |
+
try:
|
470 |
+
os.remove('Master_MIDI_Dataset_Search_Results.zip')
|
471 |
+
except OSError:
|
472 |
+
pass
|
473 |
+
|
474 |
+
print('Zipping... Please wait...')
|
475 |
+
print('=' * 70)
|
476 |
+
|
477 |
+
# %cd /content/Output-MIDI-Dataset/
|
478 |
+
!zip -r Master_MIDI_Dataset_Search_Results.zip *
|
479 |
+
# %cd /content/
|
480 |
+
|
481 |
+
print('=' * 70)
|
482 |
+
print('Done!')
|
483 |
+
print('=' * 70)
|
484 |
+
|
485 |
+
print('Downloading final zip file...')
|
486 |
+
print('=' * 70)
|
487 |
+
|
488 |
+
files.download('/content/Output-MIDI-Dataset/Master_MIDI_Dataset_Search_Results.zip')
|
489 |
+
|
490 |
+
print('Done!')
|
491 |
+
print('=' * 70)
|
492 |
+
|
493 |
+
# @title Delete search results directory and files
|
494 |
+
|
495 |
+
#@markdown WARNING: This can't be undone so make sure you downloaded the search results first
|
496 |
+
|
497 |
+
print('=' * 70)
|
498 |
+
print('Deleting... Please wait...')
|
499 |
+
print('=' * 70)
|
500 |
+
|
501 |
+
!rm -rf /content/Output-MIDI-Dataset
|
502 |
+
print('Done!')
|
503 |
+
print('=' * 70)
|
504 |
+
|
505 |
"""# Congrats! You did it! :)"""
|
midi_to_colab_audio.py
ADDED
The diff for this file is too large to render.
See raw diff
|
|