projectlosangeles
commited on
Commit
•
142b9a5
1
Parent(s):
cec716f
Upload 6 files
Browse files- Master_MIDI_Dataset_GPU_Search_and_Filter.ipynb +574 -0
- TMIDIX.py +30 -2
- master_midi_dataset_gpu_search_and_filter.py +399 -0
Master_MIDI_Dataset_GPU_Search_and_Filter.ipynb
ADDED
@@ -0,0 +1,574 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"gradient": {
|
7 |
+
"editing": false,
|
8 |
+
"id": "ac5a4cf0-d9d2-47b5-9633-b53f8d99a4d2",
|
9 |
+
"kernelId": ""
|
10 |
+
},
|
11 |
+
"id": "SiTIpPjArIyr"
|
12 |
+
},
|
13 |
+
"source": [
|
14 |
+
"# Master MIDI Dataset GPU Search and Filter (ver. 2.0)\n",
|
15 |
+
"\n",
|
16 |
+
"***\n",
|
17 |
+
"\n",
|
18 |
+
"Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools\n",
|
19 |
+
"\n",
|
20 |
+
"***\n",
|
21 |
+
"\n",
|
22 |
+
"#### Project Los Angeles\n",
|
23 |
+
"\n",
|
24 |
+
"#### Tegridy Code 2024\n",
|
25 |
+
"\n",
|
26 |
+
"***"
|
27 |
+
]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"cell_type": "markdown",
|
31 |
+
"metadata": {
|
32 |
+
"gradient": {
|
33 |
+
"editing": false,
|
34 |
+
"id": "fa0a611c-1803-42ae-bdf6-a49b5a4e781b",
|
35 |
+
"kernelId": ""
|
36 |
+
},
|
37 |
+
"id": "gOd93yV0sGd2"
|
38 |
+
},
|
39 |
+
"source": [
|
40 |
+
"# (SETUP ENVIRONMENT)"
|
41 |
+
]
|
42 |
+
},
|
43 |
+
{
|
44 |
+
"cell_type": "markdown",
|
45 |
+
"source": [
|
46 |
+
"# ( GPU CHECK)"
|
47 |
+
],
|
48 |
+
"metadata": {
|
49 |
+
"id": "0rMwKVc9FFRw"
|
50 |
+
}
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "code",
|
54 |
+
"source": [
|
55 |
+
"# @title NVIDIA GPU Check\n",
|
56 |
+
"!nvidia-smi"
|
57 |
+
],
|
58 |
+
"metadata": {
|
59 |
+
"cellView": "form",
|
60 |
+
"id": "dVSaUaEZFIip"
|
61 |
+
},
|
62 |
+
"execution_count": null,
|
63 |
+
"outputs": []
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"cell_type": "markdown",
|
67 |
+
"source": [
|
68 |
+
"# (SETUP ENVIRONMENT)"
|
69 |
+
],
|
70 |
+
"metadata": {
|
71 |
+
"id": "YRTt3Hx0FQeu"
|
72 |
+
}
|
73 |
+
},
|
74 |
+
{
|
75 |
+
"cell_type": "code",
|
76 |
+
"execution_count": null,
|
77 |
+
"metadata": {
|
78 |
+
"cellView": "form",
|
79 |
+
"gradient": {
|
80 |
+
"editing": false,
|
81 |
+
"id": "a1a45a91-d909-4fd4-b67a-5e16b971d179",
|
82 |
+
"kernelId": ""
|
83 |
+
},
|
84 |
+
"id": "fX12Yquyuihc"
|
85 |
+
},
|
86 |
+
"outputs": [],
|
87 |
+
"source": [
|
88 |
+
"#@title Install all dependencies (run only once per session)\n",
|
89 |
+
"\n",
|
90 |
+
"!git clone --depth 1 https://github.com/asigalov61/Los-Angeles-MIDI-Dataset\n",
|
91 |
+
"!pip install huggingface_hub"
|
92 |
+
]
|
93 |
+
},
|
94 |
+
{
|
95 |
+
"cell_type": "code",
|
96 |
+
"execution_count": null,
|
97 |
+
"metadata": {
|
98 |
+
"gradient": {
|
99 |
+
"editing": false,
|
100 |
+
"id": "b8207b76-9514-4c07-95db-95a4742e52c5",
|
101 |
+
"kernelId": ""
|
102 |
+
},
|
103 |
+
"id": "z7n9vnKmug1J",
|
104 |
+
"cellView": "form"
|
105 |
+
},
|
106 |
+
"outputs": [],
|
107 |
+
"source": [
|
108 |
+
"#@title Import all needed modules\n",
|
109 |
+
"\n",
|
110 |
+
"print('Loading core modules... Please wait...')\n",
|
111 |
+
"\n",
|
112 |
+
"import os\n",
|
113 |
+
"import copy\n",
|
114 |
+
"from collections import Counter\n",
|
115 |
+
"import random\n",
|
116 |
+
"import pickle\n",
|
117 |
+
"from tqdm import tqdm\n",
|
118 |
+
"import pprint\n",
|
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",
|
129 |
+
"import TMIDIX\n",
|
130 |
+
"\n",
|
131 |
+
"os.chdir('/content/')\n",
|
132 |
+
"\n",
|
133 |
+
"print('Creating IO dirs... Please wait...')\n",
|
134 |
+
"\n",
|
135 |
+
"if not os.path.exists('/content/Master-MIDI-Dataset'):\n",
|
136 |
+
" os.makedirs('/content/Master-MIDI-Dataset')\n",
|
137 |
+
"\n",
|
138 |
+
"if not os.path.exists('/content/Master-MIDI-Dataset'):\n",
|
139 |
+
" os.makedirs('/content/Master-MIDI-Dataset')\n",
|
140 |
+
"\n",
|
141 |
+
"if not os.path.exists('/content/Output-MIDI-Dataset'):\n",
|
142 |
+
" os.makedirs('/content/Output-MIDI-Dataset')\n",
|
143 |
+
"\n",
|
144 |
+
"print('Done!')\n",
|
145 |
+
"print('Enjoy! :)')"
|
146 |
+
]
|
147 |
+
},
|
148 |
+
{
|
149 |
+
"cell_type": "markdown",
|
150 |
+
"metadata": {
|
151 |
+
"gradient": {
|
152 |
+
"editing": false,
|
153 |
+
"id": "20b8698a-0b4e-4fdb-ae49-24d063782e77",
|
154 |
+
"kernelId": ""
|
155 |
+
},
|
156 |
+
"id": "ObPxlEutsQBj"
|
157 |
+
},
|
158 |
+
"source": [
|
159 |
+
"# (PREP MAIN MIDI DATASET)"
|
160 |
+
]
|
161 |
+
},
|
162 |
+
{
|
163 |
+
"cell_type": "code",
|
164 |
+
"source": [
|
165 |
+
"#@title Download Los Angeles MIDI Dataset\n",
|
166 |
+
"print('=' * 70)\n",
|
167 |
+
"print('Downloading Los Angeles MIDI Dataset...Please wait...')\n",
|
168 |
+
"print('=' * 70)\n",
|
169 |
+
"\n",
|
170 |
+
"hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset',\n",
|
171 |
+
" filename='Los-Angeles-MIDI-Dataset-Ver-4-0-CC-BY-NC-SA.zip',\n",
|
172 |
+
" repo_type=\"dataset\",\n",
|
173 |
+
" local_dir='/content/Main-MIDI-Dataset',\n",
|
174 |
+
" local_dir_use_symlinks=False)\n",
|
175 |
+
"print('=' * 70)\n",
|
176 |
+
"print('Done! Enjoy! :)')\n",
|
177 |
+
"print('=' * 70)"
|
178 |
+
],
|
179 |
+
"metadata": {
|
180 |
+
"cellView": "form",
|
181 |
+
"id": "7aItlhq9cRxZ"
|
182 |
+
},
|
183 |
+
"execution_count": null,
|
184 |
+
"outputs": []
|
185 |
+
},
|
186 |
+
{
|
187 |
+
"cell_type": "code",
|
188 |
+
"source": [
|
189 |
+
"#@title Unzip Los Angeles MIDI Dataset\n",
|
190 |
+
"%cd /content/Main-MIDI-Dataset/\n",
|
191 |
+
"\n",
|
192 |
+
"print('=' * 70)\n",
|
193 |
+
"print('Unzipping Los Angeles MIDI Dataset...Please wait...')\n",
|
194 |
+
"!unzip 'Los-Angeles-MIDI-Dataset-Ver-4-0-CC-BY-NC-SA.zip'\n",
|
195 |
+
"print('=' * 70)\n",
|
196 |
+
"\n",
|
197 |
+
"print('Done! Enjoy! :)')\n",
|
198 |
+
"print('=' * 70)\n",
|
199 |
+
"%cd /content/"
|
200 |
+
],
|
201 |
+
"metadata": {
|
202 |
+
"cellView": "form",
|
203 |
+
"id": "zMF4vdMNDYYg"
|
204 |
+
},
|
205 |
+
"execution_count": null,
|
206 |
+
"outputs": []
|
207 |
+
},
|
208 |
+
{
|
209 |
+
"cell_type": "code",
|
210 |
+
"source": [
|
211 |
+
"#@title Create Los Angeles MIDI Dataset files list\n",
|
212 |
+
"print('=' * 70)\n",
|
213 |
+
"print('Creating dataset files list...')\n",
|
214 |
+
"dataset_addr = \"/content/Main-MIDI-Dataset/MIDIs\"\n",
|
215 |
+
"\n",
|
216 |
+
"# os.chdir(dataset_addr)\n",
|
217 |
+
"filez = list()\n",
|
218 |
+
"for (dirpath, dirnames, filenames) in os.walk(dataset_addr):\n",
|
219 |
+
" filez += [os.path.join(dirpath, file) for file in filenames]\n",
|
220 |
+
"\n",
|
221 |
+
"if filez == []:\n",
|
222 |
+
" print('Could not find any MIDI files. Please check Dataset dir...')\n",
|
223 |
+
" print('=' * 70)\n",
|
224 |
+
"\n",
|
225 |
+
"print('=' * 70)\n",
|
226 |
+
"print('Randomizing file list...')\n",
|
227 |
+
"random.shuffle(filez)\n",
|
228 |
+
"print('=' * 70)\n",
|
229 |
+
"\n",
|
230 |
+
"LAMD_files_list = []\n",
|
231 |
+
"\n",
|
232 |
+
"for f in tqdm(filez):\n",
|
233 |
+
" LAMD_files_list.append([f.split('/')[-1].split('.mid')[0], f])\n",
|
234 |
+
"print('Done!')\n",
|
235 |
+
"print('=' * 70)"
|
236 |
+
],
|
237 |
+
"metadata": {
|
238 |
+
"cellView": "form",
|
239 |
+
"id": "btrUDk8MDfdw"
|
240 |
+
},
|
241 |
+
"execution_count": null,
|
242 |
+
"outputs": []
|
243 |
+
},
|
244 |
+
{
|
245 |
+
"cell_type": "code",
|
246 |
+
"source": [
|
247 |
+
"#@title Load Los Angeles MIDI Dataset Signatures Data\n",
|
248 |
+
"\n",
|
249 |
+
"print('=' * 70)\n",
|
250 |
+
"print('Loading LAMDa Signatures Data...')\n",
|
251 |
+
"sigs_data = pickle.load(open('/content/Main-MIDI-Dataset/SIGNATURES_DATA/LAMDa_SIGNATURES_DATA.pickle', 'rb'))\n",
|
252 |
+
"print('=' * 70)\n",
|
253 |
+
"\n",
|
254 |
+
"print('Prepping signatures...')\n",
|
255 |
+
"print('=' * 70)\n",
|
256 |
+
"\n",
|
257 |
+
"random.shuffle(sigs_data)\n",
|
258 |
+
"\n",
|
259 |
+
"signatures_file_names = []\n",
|
260 |
+
"sigs_matrixes = [ [0]*(len(TMIDIX.ALL_CHORDS)+128) for i in range(len(sigs_data))]\n",
|
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] / counts_sum\n",
|
271 |
+
"\n",
|
272 |
+
" idx += 1\n",
|
273 |
+
"\n",
|
274 |
+
"print('=' * 70)\n",
|
275 |
+
"print('Loading signatures...')\n",
|
276 |
+
"print('=' * 70)\n",
|
277 |
+
"\n",
|
278 |
+
"signatures_data = cp.array(sigs_matrixes)\n",
|
279 |
+
"\n",
|
280 |
+
"print('Done!')\n",
|
281 |
+
"print('=' * 70)"
|
282 |
+
],
|
283 |
+
"metadata": {
|
284 |
+
"id": "Mv-pjxbrIqi2",
|
285 |
+
"cellView": "form"
|
286 |
+
},
|
287 |
+
"execution_count": null,
|
288 |
+
"outputs": []
|
289 |
+
},
|
290 |
+
{
|
291 |
+
"cell_type": "markdown",
|
292 |
+
"source": [
|
293 |
+
"# (SEARCH AND FILTER)\n",
|
294 |
+
"\n",
|
295 |
+
"### DO NOT FORGET TO UPLOAD YOUR MASTER DATASET TO \"Master-MIDI-Dataset\" FOLDER"
|
296 |
+
],
|
297 |
+
"metadata": {
|
298 |
+
"id": "iaeqXuIHI0_T"
|
299 |
+
}
|
300 |
+
},
|
301 |
+
{
|
302 |
+
"cell_type": "code",
|
303 |
+
"source": [
|
304 |
+
"#@title Master MIDI Dataset Search and Filter\n",
|
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 = 20 #@param {type:\"slider\", min:5, max:50, step:1}\n",
|
309 |
+
"search_matching_type = \"ratios\" # @param [\"ratios\", \"distances\"]\n",
|
310 |
+
"distances_norm_order = 3 # @param {type:\"slider\", min:1, max:10, step:1}\n",
|
311 |
+
"maximum_match_ratio_to_search_for = 0.999 #@param {type:\"slider\", min:0, max:1, step:0.001}\n",
|
312 |
+
"\n",
|
313 |
+
"print('=' * 70)\n",
|
314 |
+
"print('Master MIDI Dataset GPU Search and Filter')\n",
|
315 |
+
"print('=' * 70)\n",
|
316 |
+
"\n",
|
317 |
+
"###########\n",
|
318 |
+
"\n",
|
319 |
+
"print('Loading MIDI files...')\n",
|
320 |
+
"print('This may take a while on a large dataset in particular.')\n",
|
321 |
+
"\n",
|
322 |
+
"dataset_addr = \"/content/Master-MIDI-Dataset\"\n",
|
323 |
+
"\n",
|
324 |
+
"filez = list()\n",
|
325 |
+
"\n",
|
326 |
+
"for (dirpath, dirnames, filenames) in os.walk(dataset_addr):\n",
|
327 |
+
" for file in filenames:\n",
|
328 |
+
" if file.endswith(('.mid', '.midi', '.kar')):\n",
|
329 |
+
" filez.append(os.path.join(dirpath, file))\n",
|
330 |
+
"\n",
|
331 |
+
"print('=' * 70)\n",
|
332 |
+
"\n",
|
333 |
+
"if filez:\n",
|
334 |
+
"\n",
|
335 |
+
" print('Randomizing file list...')\n",
|
336 |
+
" random.shuffle(filez)\n",
|
337 |
+
" print('=' * 70)\n",
|
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",
|
346 |
+
" input_files_count = 0\n",
|
347 |
+
" files_count = 0\n",
|
348 |
+
"\n",
|
349 |
+
" for f in filez:\n",
|
350 |
+
" try:\n",
|
351 |
+
"\n",
|
352 |
+
" input_files_count += 1\n",
|
353 |
+
"\n",
|
354 |
+
" fn = os.path.basename(f)\n",
|
355 |
+
" fn1 = os.path.splitext(fn)[0]\n",
|
356 |
+
" ext = os.path.splitext(f)[1]\n",
|
357 |
+
"\n",
|
358 |
+
" print('Processing MIDI File #', files_count+1, 'out of', len(filez))\n",
|
359 |
+
" print('MIDI file name', fn)\n",
|
360 |
+
" print('-' * 70)\n",
|
361 |
+
"\n",
|
362 |
+
" #=======================================================\n",
|
363 |
+
"\n",
|
364 |
+
" raw_score = TMIDIX.midi2single_track_ms_score(open(f, 'rb').read())\n",
|
365 |
+
" escore = TMIDIX.advanced_score_processor(raw_score, return_score_analysis=False, return_enhanced_score_notes=True)[0]\n",
|
366 |
+
"\n",
|
367 |
+
" for e in escore:\n",
|
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",
|
374 |
+
"\n",
|
375 |
+
" escore_copy = copy.deepcopy(escore)\n",
|
376 |
+
"\n",
|
377 |
+
" for e in escore_copy:\n",
|
378 |
+
" e[4] += i\n",
|
379 |
+
"\n",
|
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",
|
390 |
+
" tones_chord = sorted(set([p % 12 for p in pitches]))\n",
|
391 |
+
" checked_tones_chord = TMIDIX.check_and_fix_tones_chord(tones_chord)\n",
|
392 |
+
"\n",
|
393 |
+
" sig_token = TMIDIX.ALL_CHORDS.index(checked_tones_chord) + 128\n",
|
394 |
+
"\n",
|
395 |
+
" elif len(pitches) == 1:\n",
|
396 |
+
" sig_token = pitches[0]\n",
|
397 |
+
"\n",
|
398 |
+
" sig.append(sig_token)\n",
|
399 |
+
"\n",
|
400 |
+
" fsig = [list(v) for v in Counter(sig).most_common()]\n",
|
401 |
+
"\n",
|
402 |
+
" src_sig_mat = [0] * (len(TMIDIX.ALL_CHORDS)+128)\n",
|
403 |
+
"\n",
|
404 |
+
" counts_sum = sum([c[1] for c in fsig])\n",
|
405 |
+
"\n",
|
406 |
+
" for s in fsig:\n",
|
407 |
+
"\n",
|
408 |
+
" src_sig_mat[s[0]] = s[1] / counts_sum\n",
|
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",
|
417 |
+
" print('-' * 70)\n",
|
418 |
+
"\n",
|
419 |
+
" lower_threshold = 0.0\n",
|
420 |
+
" upper_threshold = maximum_match_ratio_to_search_for\n",
|
421 |
+
" filter_size = number_of_top_matches_MIDIs_to_collect\n",
|
422 |
+
"\n",
|
423 |
+
" final_ratios = []\n",
|
424 |
+
"\n",
|
425 |
+
" avg_idxs = []\n",
|
426 |
+
"\n",
|
427 |
+
" all_filtered_means = []\n",
|
428 |
+
" all_filtered_idxs = []\n",
|
429 |
+
" all_filtered_tvs = []\n",
|
430 |
+
"\n",
|
431 |
+
" tv_idx = -6\n",
|
432 |
+
"\n",
|
433 |
+
" for target_sig in tqdm(src_signatures):\n",
|
434 |
+
"\n",
|
435 |
+
" if search_matching_type == 'ratios':\n",
|
436 |
+
"\n",
|
437 |
+
" ratios = cp.where(target_sig != 0, cp.divide(cp.minimum(signatures_data, target_sig), cp.maximum(signatures_data, target_sig)), 0)\n",
|
438 |
+
" max_comp_lengths = cp.maximum(cp.repeat(cp.sum(target_sig != 0), signatures_data.shape[0]), cp.sum(signatures_data != 0, axis=1))\n",
|
439 |
+
"\n",
|
440 |
+
" results = cp.divide(cp.sum(ratios, axis=1), max_comp_lengths)\n",
|
441 |
+
"\n",
|
442 |
+
" elif search_matching_type == 'distances':\n",
|
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 |
+
" results = cp.max(distances) - distances\n",
|
447 |
+
"\n",
|
448 |
+
" unique_means = cp.unique(results)\n",
|
449 |
+
" sorted_means = cp.sort(unique_means)[::-1]\n",
|
450 |
+
"\n",
|
451 |
+
" filtered_means = sorted_means[(sorted_means >= lower_threshold) & (sorted_means <= upper_threshold)][:filter_size]\n",
|
452 |
+
"\n",
|
453 |
+
" filtered_idxs = cp.where(cp.in1d(results, filtered_means))[0]\n",
|
454 |
+
"\n",
|
455 |
+
" all_filtered_means.extend(results[cp.in1d(results, filtered_means)].tolist())\n",
|
456 |
+
"\n",
|
457 |
+
" all_filtered_idxs.extend(filtered_idxs.tolist())\n",
|
458 |
+
"\n",
|
459 |
+
" filtered_tvs = [tv_idx] * filtered_idxs.shape[0]\n",
|
460 |
+
"\n",
|
461 |
+
" all_filtered_tvs.extend(filtered_tvs)\n",
|
462 |
+
"\n",
|
463 |
+
" tv_idx += 1\n",
|
464 |
+
"\n",
|
465 |
+
" filtered_results = sorted(zip(all_filtered_means, all_filtered_idxs, all_filtered_tvs), key=lambda x: x[0], reverse=True)[:filter_size]\n",
|
466 |
+
"\n",
|
467 |
+
" #=======================================================\n",
|
468 |
+
"\n",
|
469 |
+
" print('Done!')\n",
|
470 |
+
" print('-' * 70)\n",
|
471 |
+
" print('Max match ratio:', filtered_results[0][0])\n",
|
472 |
+
" print('Max match transpose value:', filtered_results[0][2])\n",
|
473 |
+
" print('Max match signature index:', filtered_results[0][1])\n",
|
474 |
+
" print('Max match file name:', signatures_file_names[filtered_results[0][1]])\n",
|
475 |
+
" print('-' * 70)\n",
|
476 |
+
" print('Copying max ratios MIDIs...')\n",
|
477 |
+
"\n",
|
478 |
+
" for fr in filtered_results:\n",
|
479 |
+
"\n",
|
480 |
+
" max_ratio_index = fr[1]\n",
|
481 |
+
"\n",
|
482 |
+
" ffn = signatures_file_names[fr[1]]\n",
|
483 |
+
" ffn_idx = [y[0] for y in LAMD_files_list].index(ffn)\n",
|
484 |
+
"\n",
|
485 |
+
" ff = LAMD_files_list[ffn_idx][1]\n",
|
486 |
+
"\n",
|
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",
|
494 |
+
" fff = str(fr[0] * 100) + '_' + str(fr[2]) + '_' + ffn + '.mid'\n",
|
495 |
+
"\n",
|
496 |
+
" shutil.copy2(ff, copy_path+'/'+fff)\n",
|
497 |
+
"\n",
|
498 |
+
" shutil.copy2(f, copy_path+'/'+fn)\n",
|
499 |
+
"\n",
|
500 |
+
" #======================================================='''\n",
|
501 |
+
" print('Done!')\n",
|
502 |
+
" print('=' * 70)\n",
|
503 |
+
"\n",
|
504 |
+
" #=======================================================\n",
|
505 |
+
"\n",
|
506 |
+
" # Processed files counter\n",
|
507 |
+
" files_count += 1\n",
|
508 |
+
"\n",
|
509 |
+
" except KeyboardInterrupt:\n",
|
510 |
+
" print('Quitting...')\n",
|
511 |
+
" print('Total number of processed MIDI files', files_count)\n",
|
512 |
+
" print('=' * 70)\n",
|
513 |
+
" break\n",
|
514 |
+
"\n",
|
515 |
+
" except Exception as ex:\n",
|
516 |
+
" print('WARNING !!!')\n",
|
517 |
+
" print('=' * 70)\n",
|
518 |
+
" print('Bad file:', f)\n",
|
519 |
+
" print('Error detected:', ex)\n",
|
520 |
+
" print('=' * 70)\n",
|
521 |
+
" continue\n",
|
522 |
+
"\n",
|
523 |
+
" print('Total number of processed MIDI files', files_count)\n",
|
524 |
+
" print('=' * 70)\n",
|
525 |
+
"\n",
|
526 |
+
"else:\n",
|
527 |
+
" print('Could not find any MIDI files. Please check Dataset dir...')\n",
|
528 |
+
" print('=' * 70)"
|
529 |
+
],
|
530 |
+
"metadata": {
|
531 |
+
"cellView": "form",
|
532 |
+
"id": "M0JWCPzBGNvh"
|
533 |
+
},
|
534 |
+
"execution_count": null,
|
535 |
+
"outputs": []
|
536 |
+
},
|
537 |
+
{
|
538 |
+
"cell_type": "markdown",
|
539 |
+
"metadata": {
|
540 |
+
"id": "YzCMd94Tu_gz"
|
541 |
+
},
|
542 |
+
"source": [
|
543 |
+
"# Congrats! You did it! :)"
|
544 |
+
]
|
545 |
+
}
|
546 |
+
],
|
547 |
+
"metadata": {
|
548 |
+
"colab": {
|
549 |
+
"private_outputs": true,
|
550 |
+
"provenance": [],
|
551 |
+
"gpuType": "T4",
|
552 |
+
"machine_shape": "hm"
|
553 |
+
},
|
554 |
+
"kernelspec": {
|
555 |
+
"display_name": "Python 3",
|
556 |
+
"name": "python3"
|
557 |
+
},
|
558 |
+
"language_info": {
|
559 |
+
"codemirror_mode": {
|
560 |
+
"name": "ipython",
|
561 |
+
"version": 3
|
562 |
+
},
|
563 |
+
"file_extension": ".py",
|
564 |
+
"mimetype": "text/x-python",
|
565 |
+
"name": "python",
|
566 |
+
"nbconvert_exporter": "python",
|
567 |
+
"pygments_lexer": "ipython3",
|
568 |
+
"version": "3.9.7"
|
569 |
+
},
|
570 |
+
"accelerator": "GPU"
|
571 |
+
},
|
572 |
+
"nbformat": 4,
|
573 |
+
"nbformat_minor": 0
|
574 |
+
}
|
TMIDIX.py
CHANGED
@@ -3852,7 +3852,8 @@ ALL_CHORDS = [[0], [7], [5], [9], [2], [4], [11], [10], [8], [6], [3], [1], [0,
|
|
3852 |
[2, 5, 7, 9, 11], [1, 3, 5, 7, 10], [0, 2, 4, 7, 10], [1, 3, 5, 7, 9],
|
3853 |
[1, 3, 5, 9, 11], [1, 5, 7, 9, 11], [1, 3, 7, 9, 11], [3, 5, 7, 9, 11],
|
3854 |
[2, 4, 6, 8, 10], [0, 4, 6, 8, 10], [0, 2, 6, 8, 10], [1, 3, 5, 7, 11],
|
3855 |
-
[0, 2, 4, 8, 10], [0, 2, 4, 6, 8], [0, 2, 4, 6, 10]]
|
|
|
3856 |
|
3857 |
def find_exact_match_variable_length(list_of_lists, target_list, uncertain_indices):
|
3858 |
# Infer possible values for each uncertain index
|
@@ -3981,7 +3982,7 @@ def analyze_score_pitches(score, channels_to_analyze=[0]):
|
|
3981 |
|
3982 |
###################################################################################
|
3983 |
|
3984 |
-
ALL_CHORDS_GROUPED = [
|
3985 |
[[0, 2, 5, 7, 10], [0, 2, 4, 7, 9], [0, 2, 5, 7, 9], [1, 4, 6, 9, 11],
|
3986 |
[1, 3, 6, 8, 11], [1, 3, 6, 8, 10], [1, 4, 6, 8, 11], [1, 3, 5, 8, 10],
|
3987 |
[2, 4, 6, 9, 11], [2, 4, 7, 9, 11], [0, 3, 5, 7, 10], [0, 3, 5, 8, 10],
|
@@ -4510,6 +4511,33 @@ def ascii_text_words_counter(ascii_text):
|
|
4510 |
|
4511 |
###################################################################################
|
4512 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4513 |
# This is the end of the TMIDI X Python module
|
4514 |
|
4515 |
###################################################################################
|
|
|
3852 |
[2, 5, 7, 9, 11], [1, 3, 5, 7, 10], [0, 2, 4, 7, 10], [1, 3, 5, 7, 9],
|
3853 |
[1, 3, 5, 9, 11], [1, 5, 7, 9, 11], [1, 3, 7, 9, 11], [3, 5, 7, 9, 11],
|
3854 |
[2, 4, 6, 8, 10], [0, 4, 6, 8, 10], [0, 2, 6, 8, 10], [1, 3, 5, 7, 11],
|
3855 |
+
[0, 2, 4, 8, 10], [0, 2, 4, 6, 8], [0, 2, 4, 6, 10], [0, 2, 4, 6, 8, 10],
|
3856 |
+
[1, 3, 5, 7, 9, 11]]
|
3857 |
|
3858 |
def find_exact_match_variable_length(list_of_lists, target_list, uncertain_indices):
|
3859 |
# Infer possible values for each uncertain index
|
|
|
3982 |
|
3983 |
###################################################################################
|
3984 |
|
3985 |
+
ALL_CHORDS_GROUPED = [[[1, 3, 5, 7, 9, 11], [0, 2, 4, 6, 8, 10]],
|
3986 |
[[0, 2, 5, 7, 10], [0, 2, 4, 7, 9], [0, 2, 5, 7, 9], [1, 4, 6, 9, 11],
|
3987 |
[1, 3, 6, 8, 11], [1, 3, 6, 8, 10], [1, 4, 6, 8, 11], [1, 3, 5, 8, 10],
|
3988 |
[2, 4, 6, 9, 11], [2, 4, 7, 9, 11], [0, 3, 5, 7, 10], [0, 3, 5, 8, 10],
|
|
|
4511 |
|
4512 |
###################################################################################
|
4513 |
|
4514 |
+
def check_and_fix_tones_chord(tones_chord):
|
4515 |
+
|
4516 |
+
lst = tones_chord
|
4517 |
+
|
4518 |
+
if len(lst) == 2:
|
4519 |
+
if lst[1] - lst[0] == 1:
|
4520 |
+
return [lst[-1]]
|
4521 |
+
else:
|
4522 |
+
if 0 in lst and 11 in lst:
|
4523 |
+
lst.remove(0)
|
4524 |
+
return lst
|
4525 |
+
|
4526 |
+
non_consecutive = [lst[0]]
|
4527 |
+
|
4528 |
+
if len(lst) > 2:
|
4529 |
+
for i in range(1, len(lst) - 1):
|
4530 |
+
if lst[i-1] + 1 != lst[i] and lst[i] + 1 != lst[i+1]:
|
4531 |
+
non_consecutive.append(lst[i])
|
4532 |
+
non_consecutive.append(lst[-1])
|
4533 |
+
|
4534 |
+
if 0 in non_consecutive and 11 in non_consecutive:
|
4535 |
+
non_consecutive.remove(0)
|
4536 |
+
|
4537 |
+
return non_consecutive
|
4538 |
+
|
4539 |
+
###################################################################################
|
4540 |
+
|
4541 |
# This is the end of the TMIDI X Python module
|
4542 |
|
4543 |
###################################################################################
|
master_midi_dataset_gpu_search_and_filter.py
ADDED
@@ -0,0 +1,399 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Master_MIDI_Dataset_GPU_Search_and_Filter.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
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. 2.0)
|
10 |
+
|
11 |
+
***
|
12 |
+
|
13 |
+
Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools
|
14 |
+
|
15 |
+
***
|
16 |
+
|
17 |
+
#### Project Los Angeles
|
18 |
+
|
19 |
+
#### Tegridy Code 2024
|
20 |
+
|
21 |
+
***
|
22 |
+
|
23 |
+
# (SETUP ENVIRONMENT)
|
24 |
+
|
25 |
+
# ( GPU CHECK)
|
26 |
+
"""
|
27 |
+
|
28 |
+
# @title NVIDIA GPU Check
|
29 |
+
!nvidia-smi
|
30 |
+
|
31 |
+
"""# (SETUP ENVIRONMENT)"""
|
32 |
+
|
33 |
+
#@title Install all dependencies (run only once per session)
|
34 |
+
|
35 |
+
!git clone --depth 1 https://github.com/asigalov61/Los-Angeles-MIDI-Dataset
|
36 |
+
!pip install huggingface_hub
|
37 |
+
|
38 |
+
#@title Import all needed modules
|
39 |
+
|
40 |
+
print('Loading core modules... Please wait...')
|
41 |
+
|
42 |
+
import os
|
43 |
+
import copy
|
44 |
+
from collections import Counter
|
45 |
+
import random
|
46 |
+
import pickle
|
47 |
+
from tqdm import tqdm
|
48 |
+
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 |
+
|
59 |
+
import TMIDIX
|
60 |
+
|
61 |
+
os.chdir('/content/')
|
62 |
+
|
63 |
+
print('Creating IO dirs... Please wait...')
|
64 |
+
|
65 |
+
if not os.path.exists('/content/Master-MIDI-Dataset'):
|
66 |
+
os.makedirs('/content/Master-MIDI-Dataset')
|
67 |
+
|
68 |
+
if not os.path.exists('/content/Master-MIDI-Dataset'):
|
69 |
+
os.makedirs('/content/Master-MIDI-Dataset')
|
70 |
+
|
71 |
+
if not os.path.exists('/content/Output-MIDI-Dataset'):
|
72 |
+
os.makedirs('/content/Output-MIDI-Dataset')
|
73 |
+
|
74 |
+
print('Done!')
|
75 |
+
print('Enjoy! :)')
|
76 |
+
|
77 |
+
"""# (PREP MAIN MIDI DATASET)"""
|
78 |
+
|
79 |
+
#@title Download Los Angeles MIDI Dataset
|
80 |
+
print('=' * 70)
|
81 |
+
print('Downloading Los Angeles MIDI Dataset...Please wait...')
|
82 |
+
print('=' * 70)
|
83 |
+
|
84 |
+
hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset',
|
85 |
+
filename='Los-Angeles-MIDI-Dataset-Ver-4-0-CC-BY-NC-SA.zip',
|
86 |
+
repo_type="dataset",
|
87 |
+
local_dir='/content/Main-MIDI-Dataset',
|
88 |
+
local_dir_use_symlinks=False)
|
89 |
+
print('=' * 70)
|
90 |
+
print('Done! Enjoy! :)')
|
91 |
+
print('=' * 70)
|
92 |
+
|
93 |
+
# Commented out IPython magic to ensure Python compatibility.
|
94 |
+
#@title Unzip Los Angeles MIDI Dataset
|
95 |
+
# %cd /content/Main-MIDI-Dataset/
|
96 |
+
|
97 |
+
print('=' * 70)
|
98 |
+
print('Unzipping Los Angeles MIDI Dataset...Please wait...')
|
99 |
+
!unzip 'Los-Angeles-MIDI-Dataset-Ver-4-0-CC-BY-NC-SA.zip'
|
100 |
+
print('=' * 70)
|
101 |
+
|
102 |
+
print('Done! Enjoy! :)')
|
103 |
+
print('=' * 70)
|
104 |
+
# %cd /content/
|
105 |
+
|
106 |
+
#@title Create Los Angeles MIDI Dataset files list
|
107 |
+
print('=' * 70)
|
108 |
+
print('Creating dataset files list...')
|
109 |
+
dataset_addr = "/content/Main-MIDI-Dataset/MIDIs"
|
110 |
+
|
111 |
+
# os.chdir(dataset_addr)
|
112 |
+
filez = list()
|
113 |
+
for (dirpath, dirnames, filenames) in os.walk(dataset_addr):
|
114 |
+
filez += [os.path.join(dirpath, file) for file in filenames]
|
115 |
+
|
116 |
+
if filez == []:
|
117 |
+
print('Could not find any MIDI files. Please check Dataset dir...')
|
118 |
+
print('=' * 70)
|
119 |
+
|
120 |
+
print('=' * 70)
|
121 |
+
print('Randomizing file list...')
|
122 |
+
random.shuffle(filez)
|
123 |
+
print('=' * 70)
|
124 |
+
|
125 |
+
LAMD_files_list = []
|
126 |
+
|
127 |
+
for f in tqdm(filez):
|
128 |
+
LAMD_files_list.append([f.split('/')[-1].split('.mid')[0], f])
|
129 |
+
print('Done!')
|
130 |
+
print('=' * 70)
|
131 |
+
|
132 |
+
#@title Load Los Angeles MIDI Dataset Signatures Data
|
133 |
+
|
134 |
+
print('=' * 70)
|
135 |
+
print('Loading LAMDa Signatures Data...')
|
136 |
+
sigs_data = pickle.load(open('/content/Main-MIDI-Dataset/SIGNATURES_DATA/LAMDa_SIGNATURES_DATA.pickle', 'rb'))
|
137 |
+
print('=' * 70)
|
138 |
+
|
139 |
+
print('Prepping signatures...')
|
140 |
+
print('=' * 70)
|
141 |
+
|
142 |
+
random.shuffle(sigs_data)
|
143 |
+
|
144 |
+
signatures_file_names = []
|
145 |
+
sigs_matrixes = [ [0]*(len(TMIDIX.ALL_CHORDS)+128) for i in range(len(sigs_data))]
|
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] / counts_sum
|
156 |
+
|
157 |
+
idx += 1
|
158 |
+
|
159 |
+
print('=' * 70)
|
160 |
+
print('Loading signatures...')
|
161 |
+
print('=' * 70)
|
162 |
+
|
163 |
+
signatures_data = cp.array(sigs_matrixes)
|
164 |
+
|
165 |
+
print('Done!')
|
166 |
+
print('=' * 70)
|
167 |
+
|
168 |
+
"""# (SEARCH AND FILTER)
|
169 |
+
|
170 |
+
### DO NOT FORGET TO UPLOAD YOUR MASTER DATASET TO "Master-MIDI-Dataset" FOLDER
|
171 |
+
"""
|
172 |
+
|
173 |
+
#@title Master MIDI Dataset Search and Filter
|
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 = 20 #@param {type:"slider", min:5, max:50, step:1}
|
178 |
+
search_matching_type = "ratios" # @param ["ratios", "distances"]
|
179 |
+
distances_norm_order = 3 # @param {type:"slider", min:1, max:10, step:1}
|
180 |
+
maximum_match_ratio_to_search_for = 0.999 #@param {type:"slider", min:0, max:1, step:0.001}
|
181 |
+
|
182 |
+
print('=' * 70)
|
183 |
+
print('Master MIDI Dataset GPU Search and Filter')
|
184 |
+
print('=' * 70)
|
185 |
+
|
186 |
+
###########
|
187 |
+
|
188 |
+
print('Loading MIDI files...')
|
189 |
+
print('This may take a while on a large dataset in particular.')
|
190 |
+
|
191 |
+
dataset_addr = "/content/Master-MIDI-Dataset"
|
192 |
+
|
193 |
+
filez = list()
|
194 |
+
|
195 |
+
for (dirpath, dirnames, filenames) in os.walk(dataset_addr):
|
196 |
+
for file in filenames:
|
197 |
+
if file.endswith(('.mid', '.midi', '.kar')):
|
198 |
+
filez.append(os.path.join(dirpath, file))
|
199 |
+
|
200 |
+
print('=' * 70)
|
201 |
+
|
202 |
+
if filez:
|
203 |
+
|
204 |
+
print('Randomizing file list...')
|
205 |
+
random.shuffle(filez)
|
206 |
+
print('=' * 70)
|
207 |
+
|
208 |
+
###################
|
209 |
+
|
210 |
+
if not os.path.exists('/content/Output-MIDI-Dataset'):
|
211 |
+
os.makedirs('/content/Output-MIDI-Dataset')
|
212 |
+
|
213 |
+
###################
|
214 |
+
|
215 |
+
input_files_count = 0
|
216 |
+
files_count = 0
|
217 |
+
|
218 |
+
for f in filez:
|
219 |
+
try:
|
220 |
+
|
221 |
+
input_files_count += 1
|
222 |
+
|
223 |
+
fn = os.path.basename(f)
|
224 |
+
fn1 = os.path.splitext(fn)[0]
|
225 |
+
ext = os.path.splitext(f)[1]
|
226 |
+
|
227 |
+
print('Processing MIDI File #', files_count+1, 'out of', len(filez))
|
228 |
+
print('MIDI file name', fn)
|
229 |
+
print('-' * 70)
|
230 |
+
|
231 |
+
#=======================================================
|
232 |
+
|
233 |
+
raw_score = TMIDIX.midi2single_track_ms_score(open(f, 'rb').read())
|
234 |
+
escore = TMIDIX.advanced_score_processor(raw_score, return_score_analysis=False, return_enhanced_score_notes=True)[0]
|
235 |
+
|
236 |
+
for e in escore:
|
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):
|
243 |
+
|
244 |
+
escore_copy = copy.deepcopy(escore)
|
245 |
+
|
246 |
+
for e in escore_copy:
|
247 |
+
e[4] += i
|
248 |
+
|
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:
|
259 |
+
tones_chord = sorted(set([p % 12 for p in pitches]))
|
260 |
+
checked_tones_chord = TMIDIX.check_and_fix_tones_chord(tones_chord)
|
261 |
+
|
262 |
+
sig_token = TMIDIX.ALL_CHORDS.index(checked_tones_chord) + 128
|
263 |
+
|
264 |
+
elif len(pitches) == 1:
|
265 |
+
sig_token = pitches[0]
|
266 |
+
|
267 |
+
sig.append(sig_token)
|
268 |
+
|
269 |
+
fsig = [list(v) for v in Counter(sig).most_common()]
|
270 |
+
|
271 |
+
src_sig_mat = [0] * (len(TMIDIX.ALL_CHORDS)+128)
|
272 |
+
|
273 |
+
counts_sum = sum([c[1] for c in fsig])
|
274 |
+
|
275 |
+
for s in fsig:
|
276 |
+
|
277 |
+
src_sig_mat[s[0]] = s[1] / counts_sum
|
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...')
|
286 |
+
print('-' * 70)
|
287 |
+
|
288 |
+
lower_threshold = 0.0
|
289 |
+
upper_threshold = maximum_match_ratio_to_search_for
|
290 |
+
filter_size = number_of_top_matches_MIDIs_to_collect
|
291 |
+
|
292 |
+
final_ratios = []
|
293 |
+
|
294 |
+
avg_idxs = []
|
295 |
+
|
296 |
+
all_filtered_means = []
|
297 |
+
all_filtered_idxs = []
|
298 |
+
all_filtered_tvs = []
|
299 |
+
|
300 |
+
tv_idx = -6
|
301 |
+
|
302 |
+
for target_sig in tqdm(src_signatures):
|
303 |
+
|
304 |
+
if search_matching_type == 'ratios':
|
305 |
+
|
306 |
+
ratios = cp.where(target_sig != 0, cp.divide(cp.minimum(signatures_data, target_sig), cp.maximum(signatures_data, target_sig)), 0)
|
307 |
+
max_comp_lengths = cp.maximum(cp.repeat(cp.sum(target_sig != 0), signatures_data.shape[0]), cp.sum(signatures_data != 0, axis=1))
|
308 |
+
|
309 |
+
results = cp.divide(cp.sum(ratios, axis=1), max_comp_lengths)
|
310 |
+
|
311 |
+
elif search_matching_type == 'distances':
|
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 |
+
results = cp.max(distances) - distances
|
316 |
+
|
317 |
+
unique_means = cp.unique(results)
|
318 |
+
sorted_means = cp.sort(unique_means)[::-1]
|
319 |
+
|
320 |
+
filtered_means = sorted_means[(sorted_means >= lower_threshold) & (sorted_means <= upper_threshold)][:filter_size]
|
321 |
+
|
322 |
+
filtered_idxs = cp.where(cp.in1d(results, filtered_means))[0]
|
323 |
+
|
324 |
+
all_filtered_means.extend(results[cp.in1d(results, filtered_means)].tolist())
|
325 |
+
|
326 |
+
all_filtered_idxs.extend(filtered_idxs.tolist())
|
327 |
+
|
328 |
+
filtered_tvs = [tv_idx] * filtered_idxs.shape[0]
|
329 |
+
|
330 |
+
all_filtered_tvs.extend(filtered_tvs)
|
331 |
+
|
332 |
+
tv_idx += 1
|
333 |
+
|
334 |
+
filtered_results = sorted(zip(all_filtered_means, all_filtered_idxs, all_filtered_tvs), key=lambda x: x[0], reverse=True)[:filter_size]
|
335 |
+
|
336 |
+
#=======================================================
|
337 |
+
|
338 |
+
print('Done!')
|
339 |
+
print('-' * 70)
|
340 |
+
print('Max match ratio:', filtered_results[0][0])
|
341 |
+
print('Max match transpose value:', filtered_results[0][2])
|
342 |
+
print('Max match signature index:', filtered_results[0][1])
|
343 |
+
print('Max match file name:', signatures_file_names[filtered_results[0][1]])
|
344 |
+
print('-' * 70)
|
345 |
+
print('Copying max ratios MIDIs...')
|
346 |
+
|
347 |
+
for fr in filtered_results:
|
348 |
+
|
349 |
+
max_ratio_index = fr[1]
|
350 |
+
|
351 |
+
ffn = signatures_file_names[fr[1]]
|
352 |
+
ffn_idx = [y[0] for y in LAMD_files_list].index(ffn)
|
353 |
+
|
354 |
+
ff = LAMD_files_list[ffn_idx][1]
|
355 |
+
|
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 |
+
|
363 |
+
fff = str(fr[0] * 100) + '_' + str(fr[2]) + '_' + ffn + '.mid'
|
364 |
+
|
365 |
+
shutil.copy2(ff, copy_path+'/'+fff)
|
366 |
+
|
367 |
+
shutil.copy2(f, copy_path+'/'+fn)
|
368 |
+
|
369 |
+
#======================================================='''
|
370 |
+
print('Done!')
|
371 |
+
print('=' * 70)
|
372 |
+
|
373 |
+
#=======================================================
|
374 |
+
|
375 |
+
# Processed files counter
|
376 |
+
files_count += 1
|
377 |
+
|
378 |
+
except KeyboardInterrupt:
|
379 |
+
print('Quitting...')
|
380 |
+
print('Total number of processed MIDI files', files_count)
|
381 |
+
print('=' * 70)
|
382 |
+
break
|
383 |
+
|
384 |
+
except Exception as ex:
|
385 |
+
print('WARNING !!!')
|
386 |
+
print('=' * 70)
|
387 |
+
print('Bad file:', f)
|
388 |
+
print('Error detected:', ex)
|
389 |
+
print('=' * 70)
|
390 |
+
continue
|
391 |
+
|
392 |
+
print('Total number of processed MIDI files', files_count)
|
393 |
+
print('=' * 70)
|
394 |
+
|
395 |
+
else:
|
396 |
+
print('Could not find any MIDI files. Please check Dataset dir...')
|
397 |
+
print('=' * 70)
|
398 |
+
|
399 |
+
"""# Congrats! You did it! :)"""
|