-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb_classi.py
926 lines (652 loc) · 24.2 KB
/
b_classi.py
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
# -*- coding: utf-8 -*-
"""B_CLassi.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/github/asigalov61/B-CLassi/blob/main/B_CLassi.ipynb
# B CLassi (ver. 1.0)
***
Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools
***
#### Project Los Angeles
#### Tegridy Code 2023
***
# (SETUP ENVIRONMENT)
"""
#@title Install all dependencies (run only once per session)
!git clone https://github.com/asigalov61/B-CLassi
!pip install tqdm
# Commented out IPython magic to ensure Python compatibility.
#@title Import all needed modules
print('=' * 70)
print('Loading needed modules. Please wait...')
import os
import copy
import math
import statistics
import random
import shutil
from collections import Counter
from joblib import Parallel, delayed, parallel_config
from tqdm import tqdm
import matplotlib.pyplot as plt
print('=' * 70)
print('Creating I/O dirs...')
if not os.path.exists('/content/GOOD'):
os.makedirs('/content/GOOD')
if not os.path.exists('/content/BAD'):
os.makedirs('/content/BAD')
if not os.path.exists('/content/EVAL_IN'):
os.makedirs('/content/EVAL_IN')
if not os.path.exists('/content/EVAL_OUT'):
os.makedirs('/content/EVAL_OUT')
if not os.path.exists('/content/EVAL_OUT/GOOD'):
os.makedirs('/content/EVAL_OUT/GOOD')
if not os.path.exists('/content/EVAL_OUT/BAD'):
os.makedirs('/content/EVAL_OUT/BAD')
print('=' * 70)
print('Loading TMIDIX module...')
# %cd /content/B-CLassi/
import TMIDIX
# %cd /content/
print('=' * 70)
print('Loading Tensorflow module...')
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.metrics import Precision, Recall, BinaryAccuracy
from tensorflow.keras.models import load_model
# Avoid OOM errors by setting GPU Memory Consumption Growth
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
tf.config.list_physical_devices('GPU')
print('=' * 70)
print('Done!')
print('Enjoy! :)')
print('=' * 70)
"""# (DOWNLOAD MIDI DATASET)"""
# Commented out IPython magic to ensure Python compatibility.
# @title Donwload and unzip sample MIDI classification dataset
# %cd /content/
!wget https://github.com/asigalov61/Tegridy-MIDI-Dataset/raw/master/B-CLassi-MIDI-Dataset-CC-BY-NC-SA.zip
!unzip B-CLassi-MIDI-Dataset-CC-BY-NC-SA.zip
!rm B-CLassi-MIDI-Dataset-CC-BY-NC-SA.zip
# %cd /content/
"""# (LOAD MIDI PROCESSOR)"""
#@title TMIDIX MIDI Processor
print('=' * 70)
print('Loading TMIDIX MIDI Processor...')
print('=' * 70)
def group_single_elements(lst):
new_lst = []
temp = []
for sublist in lst:
if len(sublist) == 1:
temp.extend(sublist)
else:
if temp:
new_lst.append(temp)
temp = []
new_lst.append(sublist)
if temp:
new_lst.append(temp)
return new_lst
def TMIDIX_MIDI_Processor(midi_file):
melody_chords = []
try:
fn = os.path.basename(midi_file)
# Filtering out GIANT4 MIDIs
file_size = os.path.getsize(midi_file)
if file_size <= 1000000:
#=======================================================
# START PROCESSING
# Convering MIDI to ms score with MIDI.py module
score = TMIDIX.midi2single_track_ms_score(open(midi_file, 'rb').read(), recalculate_channels=False)
# INSTRUMENTS CONVERSION CYCLE
events_matrix = []
itrack = 1
patches = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
while itrack < len(score):
for event in score[itrack]:
if event[0] == 'note' or event[0] == 'patch_change':
events_matrix.append(event)
itrack += 1
events_matrix.sort(key=lambda x: x[1])
events_matrix1 = []
for event in events_matrix:
if event[0] == 'patch_change':
patches[event[2]] = event[3]
if event[0] == 'note':
event.extend([patches[event[3]]])
if events_matrix1:
if (event[1] == events_matrix1[-1][1]):
if ([event[3], event[4]] != events_matrix1[-1][3:5]):
events_matrix1.append(event)
else:
events_matrix1.append(event)
else:
events_matrix1.append(event)
if len(events_matrix1) > 0:
if min([e[1] for e in events_matrix1]) >= 0 and min([e[2] for e in events_matrix1]) >= 0:
#=======================================================
# PRE-PROCESSING
# checking number of instruments in a composition
instruments_list = list(set([y[3] for y in events_matrix1]))
if len(events_matrix1) > 0:
#===================================
# ORIGINAL COMPOSITION
#===================================
# Sorting by patch, pitch, then by start-time
events_matrix1.sort(key=lambda x: x[6])
events_matrix1.sort(key=lambda x: x[4], reverse=True)
events_matrix1.sort(key=lambda x: x[1])
#=======================================================
# FINAL PROCESSING
#=======================================================
# MAIN PROCESSING CYCLE
#=======================================================
for e in events_matrix1:
cha = max(0, min(15, e[3]))
# Pitches
if cha == 9: # Drums patch will be == 128
e[4] = max(1, min(127, e[4]))
else:
e[4] = max(1, min(127, e[4]))+128
events_matrix1.sort(key=lambda x: x[4], reverse=True)
events_matrix1.sort(key=lambda x: x[1])
chords = []
cho = []
pe = events_matrix1[0]
for e in events_matrix1:
if e[1] - pe[1] == 0:
cho.append(e)
else:
if len(cho) > 0:
chords.append(cho)
cho = []
cho.append(e)
pe = e
if len(cho) > 0:
chords.append(cho)
chords1 = group_single_elements(chords)
chords2 = []
for t in chords1:
if len(t) == 1:
chords2.extend([256, t[0][4]])
elif len(t) > 1 and len(list(set([tt[1] for tt in t]))) > 1:
chords2.extend([256] + [tt[4] for tt in t])
elif len(t) > 1 and len(list(set([tt[1] for tt in t]))) == 1:
chords2.extend([257] + [tt[4] for tt in t])
#=======================================================
# TOTAL DICTIONARY SIZE 257
#=======================================================
return chords2, fn
except:
return None
print('Done!')
print('=' * 70)
"""# (PROCESS GOOD MIDI DATA)"""
#@title Save file list
###########
print('=' * 70)
print('Loading MIDI files...')
print('This may take a while on a large dataset in particular.')
dataset_addr = "/content/GOOD"
# os.chdir(dataset_addr)
filez = list()
for (dirpath, dirnames, filenames) in os.walk(dataset_addr):
filez += [os.path.join(dirpath, file) for file in filenames]
print('=' * 70)
if not filez:
print('Could not find any MIDI files. Please check Dataset dir...')
print('=' * 70)
else:
print('Randomizing file list...')
random.shuffle(filez)
print('Done!')
print('=' * 70)
print('Total files:', len(filez))
print('=' * 70)
#@title Process MIDIs with TMIDIX MIDI processor
print('=' * 70)
print('TMIDIX MIDI Processor')
print('=' * 70)
print('Starting up...')
print('=' * 70)
###########
melody_chords_f = []
print('Processing MIDI files. Please wait...')
print('=' * 70)
for i in tqdm(range(0, len(filez), 16)):
with parallel_config(backend='threading', n_jobs=4, verbose = 0):
output = Parallel()(delayed(TMIDIX_MIDI_Processor)(f) for f in filez[i:i+16])
for o in output:
if o is not None:
melody_chords_f.append(o)
print('Done!')
print('=' * 70)
"""# (PROCESS BAD MIDI DATA)"""
#@title Save file list
###########
print('=' * 70)
print('Loading MIDI files...')
print('This may take a while on a large dataset in particular.')
dataset_addr = "/content/BAD"
# os.chdir(dataset_addr)
filez1 = list()
for (dirpath, dirnames, filenames) in os.walk(dataset_addr):
filez1 += [os.path.join(dirpath, file) for file in filenames]
print('=' * 70)
if not filez1:
print('Could not find any MIDI files. Please check Dataset dir...')
print('=' * 70)
else:
print('Randomizing file list...')
random.shuffle(filez1)
print('Done!')
print('=' * 70)
print('Total files:', len(filez1))
print('=' * 70)
#@title Process MIDIs with TMIDIX MIDI processor
print('=' * 70)
print('TMIDIX MIDI Processor')
print('=' * 70)
print('Starting up...')
print('=' * 70)
###########
melody_chords_f1 = []
print('Processing MIDI files. Please wait...')
print('=' * 70)
for i in tqdm(range(0, len(filez1), 16)):
with parallel_config(backend='threading', n_jobs=4, verbose = 0):
output = Parallel()(delayed(TMIDIX_MIDI_Processor)(f) for f in filez1[i:i+16])
for o in output:
if o is not None:
melody_chords_f1.append(o)
print('Done!')
print('=' * 70)
"""# (SAVE/LOAD ALL MIDI DATA)"""
# @title Save
print('=' * 70)
TMIDIX.Tegridy_Any_Pickle_File_Writer([melody_chords_f, melody_chords_f1], '/content/B_CLassi_ALL_MIDI_DATA')
print('=' * 70)
# @title Load
print('=' * 70)
melody_chords_f, melody_chords_f1 = TMIDIX.Tegridy_Any_Pickle_File_Reader('/content/B_CLassi_ALL_MIDI_DATA')
print('Done!')
print('=' * 70)
"""# (PREP ALL MIDI DATA)"""
# @title Prep data
classifier_seq_length_in_notes = 640 # @param {type:"slider", min:128, max:2048, step:128}
composition_sampling_steps_in_notes = 32 # @param {type:"slider", min:8, max:64, step:8}
equalize_data_splits = False # @param {type:"boolean"}
#===============================================================================
SEQ_LEN = classifier_seq_length_in_notes # In notes
STEP = composition_sampling_steps_in_notes # Composition sampling in notes
EQUALIZE_DATA_SPLITS = equalize_data_splits # Balancing equalization
#===============================================================================
gdata = []
bdata = []
data = []
good = 0
bad = 0
print('=' * 70)
print('Prepping data...')
print('=' * 70)
# Good data == 1
for m in tqdm(melody_chords_f):
for i in range(0, len(m[0]), STEP):
if len(m[0][i:i+SEQ_LEN]) == SEQ_LEN:
gdata.append([m[0][i:i+SEQ_LEN], [1]])
good += 1
# Bad data == 0
for m in tqdm(melody_chords_f1):
for i in range(0, len(m[0]), STEP):
if len(m[0][i:i+SEQ_LEN]) == SEQ_LEN:
bdata.append([m[0][i:i+SEQ_LEN], [0]])
bad += 1
random.shuffle(gdata)
random.shuffle(bdata)
if EQUALIZE_DATA_SPLITS:
equal_data_size = min(len(gdata), len(bdata))
data = gdata[:equal_data_size] + bdata[:equal_data_size]
good = bad = equal_data_size
else:
data = gdata + bdata
random.shuffle(data)
print('Done!')
print('=' * 70)
print('Total data size:', len(data))
print('Good data size:', good, '/', good / len(data))
print('Bad data size:', bad, '/', bad / len(data))
print('Good to bad ratio:', good / bad)
print('=' * 70)
# @title Create training datasets and their splits
training_batch_size = 32 # @param {type:"slider", min:4, max:128, step:4}
train_dataset_size_ratio = 0.9 # @param {type:"slider", min:0.66, max:0.96, step:0.01}
validation_dataset_size_ratio = 0.02 # @param {type:"slider", min:0.01, max:0.3, step:0.01}
test_dataset_size_ratio = 0.02 # @param {type:"slider", min:0.01, max:0.3, step:0.01}
#===============================================================================
BATCH_SIZE = training_batch_size
TRAIN_DATASET_SIZE_RATIO = train_dataset_size_ratio
VAL_DATASET_SIZE_RATIO = validation_dataset_size_ratio
TEST_DATASET_SIZE_RATIO = test_dataset_size_ratio
#===============================================================================
def create_dataset(data, batch_size=16):
# Separate the features and labels
features = [lst[0] for lst in data]
labels = [lst[1][0] for lst in data]
# Convert the lists to TensorFlow tensors
tensor_features = tf.constant(features, dtype=tf.float32)
tensor_labels = tf.constant(labels, dtype=tf.int32)
# Create a tf.data.Dataset from the tensors
dataset = tf.data.Dataset.from_tensor_slices((tensor_features, tensor_labels))
# Batch the dataset and discard incomplete batches
batched_dataset = dataset.batch(batch_size, drop_remainder=True)
return batched_dataset
#===============================================================================
print('=' * 70)
print('Creating B Classi Training Datasets...')
dataset = create_dataset(data, BATCH_SIZE)
train_size = int(len(dataset) * TRAIN_DATASET_SIZE_RATIO)
val_size = int(len(dataset) * TEST_DATASET_SIZE_RATIO)
test_size = int(len(dataset) * VAL_DATASET_SIZE_RATIO)
train = dataset.take(train_size)
val = dataset.skip(train_size).take(val_size)
test = dataset.skip(train_size+val_size).take(test_size)
print('Done!')
print('=' * 70)
"""# (CREATE MODEL)"""
# @title Create the model
model_size = 1024 # @param {type:"slider", min:512, max:2048, step:512}
model_dropout = 0.5 # @param {type:"slider", min:0, max:1, step:0.1}
model_kernel_size = 4 # @param {type:"slider", min:2, max:16, step:2}
model_pool_size = 2 # @param {type:"slider", min:2, max:8, step:1}
#=======================================================
MODEL_SIZE = model_size
DROPOUT = model_dropout
KERNEL_SIZE = model_kernel_size
POOL_SIZE = model_pool_size
#=======================================================
print('=' * 70)
print('Creating model...')
# Define the model
model = tf.keras.models.Sequential()
# Add a Conv1D layer
model.add(layers.Conv1D(MODEL_SIZE, KERNEL_SIZE, 1, activation='relu', input_shape=(SEQ_LEN, 1)))
model.add(layers.BatchNormalization())
model.add(layers.Dropout(DROPOUT))
# Add a MaxPooling layer
model.add(layers.MaxPooling1D(pool_size=POOL_SIZE))
# Add a Conv1D layer
model.add(layers.Conv1D(int(MODEL_SIZE // 2), KERNEL_SIZE, 1, activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dropout(DROPOUT))
# Add a MaxPooling layer
model.add(layers.MaxPooling1D(pool_size=POOL_SIZE))
# Add a Conv1D layer
model.add(layers.Conv1D(int(MODEL_SIZE // 4), KERNEL_SIZE, 1, activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dropout(DROPOUT))
# Add a MaxPooling layer
model.add(layers.MaxPooling1D(pool_size=POOL_SIZE))
# Add a Conv1D layer
model.add(layers.Conv1D(int(MODEL_SIZE // 8), KERNEL_SIZE, 1, activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dropout(DROPOUT))
# Add a MaxPooling layer
model.add(layers.MaxPooling1D(pool_size=POOL_SIZE))
# Add a Conv1D layer
model.add(layers.Conv1D(int(MODEL_SIZE // 16), KERNEL_SIZE, 1, activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dropout(DROPOUT))
# Add a MaxPooling layer
model.add(layers.MaxPooling1D(pool_size=POOL_SIZE))
# Add a Flatten layer
model.add(layers.Flatten())
model.add(layers.Dense(MODEL_SIZE, activation='relu'))
model.add(layers.Dropout(DROPOUT))
# Add a Dense layer
model.add(layers.Dense(1, activation='sigmoid'))
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Tensorboard
logdir='LOGS'
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=logdir)
# Add early stopping
es_callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)
print('=' * 70)
print('Done!')
print('=' * 70)
model.summary()
print('=' * 70)
"""# (TRAIN)"""
# @title Train the model
number_of_training_epochs = 10 # @param {type:"slider", min:1, max:15, step:1}
print('=' * 70)
print('Training...Please wait...')
print('=' * 70)
hist = model.fit(train, validation_data=val, batch_size=BATCH_SIZE, epochs=number_of_training_epochs, callbacks=[es_callback, tensorboard_callback])
print('=' * 70)
print('Done!')
print('=' * 70)
"""# (PLOT TRAINING RESULTS)"""
# @title Plot performance
print('=' * 70)
print('Plotting training results...')
print('=' * 70)
fig = plt.figure()
plt.plot(hist.history['loss'], color='teal', label='loss')
plt.plot(hist.history['val_loss'], color='orange', label='val_loss')
fig.suptitle('Loss', fontsize=20)
plt.legend(loc="upper left")
plt.show()
print('=' * 70)
fig = plt.figure()
plt.plot(hist.history['accuracy'], color='teal', label='accuracy')
plt.plot(hist.history['val_accuracy'], color='orange', label='val_accuracy')
fig.suptitle('Accuracy', fontsize=20)
plt.legend(loc="upper left")
plt.show()
print('=' * 70)
"""# (EVAL MODEL)"""
# @title Eval model
pre = Precision()
re = Recall()
acc = BinaryAccuracy()
print('=' * 70)
print('Evaluating model...')
print('=' * 70)
for batch in tqdm(test):
X, y = batch
yhat = model.predict(X, verbose=0)
pre.update_state(y, yhat)
re.update_state(y, yhat)
acc.update_state(y, yhat)
print('Done!')
print('=' * 70)
print('Model Precision:', pre.result().numpy())
print('Model Recall:', re.result().numpy())
print('Model Accuracy:', acc.result().numpy())
print('=' * 70)
"""# (SAVE/LOAD MODEL)"""
#@title Save model
print('=' * 70)
print('Saving model...')
model.save(os.path.join('/content','B_CLassi_Pre_Trained_Model_' + str(round(pre.result().numpy(), 4)) + '_Precision' + '.keras'))
print('=' * 70)
print('Done!')
print('=' * 70)
# @title Load model
full_path_to_trained_model = "/content/B_CLassi_Pre_Trained_Model_0.9942_Precision.keras" # @param {type:"string"}
print('=' * 70)
print('Loading model...')
model = load_model(full_path_to_trained_model)
print('=' * 70)
print('Done!')
print('=' * 70)
"""# (CLASSIFY)
# (CUSTOM MIDI FILE)
"""
# @title Load custom MIDI
full_path_to_custom_MIDI_file = "/content/B-CLassi/Come To My Window.mid" # @param {type:"string"}
sampling_step_in_notes = 32 # @param {type:"slider", min:8, max:128, step:8}
print('=' * 70)
print('Loading custom MIDI file...')
print('=' * 70)
#=======================================================
# START PROCESSING
test_midi = [TMIDIX_MIDI_Processor(full_path_to_custom_MIDI_file)]
test_data = []
# Good data == 1
for m in tqdm(test_midi):
for i in range(0, len(m[0]), sampling_step_in_notes):
if len(m[0][i:i+SEQ_LEN]) == SEQ_LEN:
test_data.append(m[0][i:i+SEQ_LEN])
# Separate the features and labels
features = [lst for lst in test_data] # Corrected this line
# Convert the lists to TensorFlow tensors
tensor_features = tf.constant(features, dtype=tf.float32)
# Create a tf.data.Dataset from the tensors
test_dataset = tf.data.Dataset.from_tensor_slices(tensor_features)
# Batch the dataset and discard incomplete batches
batched_test_dataset = test_dataset.batch(BATCH_SIZE)
print('Done!')
print('=' * 70)
# @title Classify
good_classification_threshold = 0.5 # @param {type:"slider", min:0.1, max:1, step:0.05}
print('=' * 70)
print('Classifying...')
print('-' * 70)
yhat = model.predict(batched_test_dataset)
avg_label = round(sum([y[0] for y in yhat.tolist()]) / len(yhat.tolist()), 4)
print('=' * 70)
if avg_label > good_classification_threshold:
print(f'Predicted class is GOOD')
else:
print(f'Predicted class is BAD')
print('=' * 70)
print('First five labels:', [round(y[0], 4) for y in yhat.tolist() if y[0] >= good_classification_threshold][:5])
print('=' * 70)
print('Total number of classification labels:', len(yhat.tolist()))
print('=' * 70)
print('TOtal number of good classification labels:', len([y[0] for y in yhat.tolist() if y[0] >= good_classification_threshold]))
print('TOtal number of bad classification labels:', len([y[0] for y in yhat.tolist() if y[0] < good_classification_threshold]))
print('=' * 70)
print('Min/Max classification labels:', round(min(yhat.tolist())[0], 4), '/', round(max(yhat.tolist())[0], 4))
print('=' * 70)
print('Average classification label:', round(avg_label, 4))
print('=' * 70)
"""# (CUSTOM MIDI DATASET)"""
#@title Save file list
#@markdown Upload your custom MIDI dataset into EVAL_IN directory
###########
print('=' * 70)
print('Loading MIDI files...')
print('This may take a while on a large dataset in particular.')
dataset_addr = "/content/EVAL_IN"
# os.chdir(dataset_addr)
filez2 = list()
for (dirpath, dirnames, filenames) in os.walk(dataset_addr):
filez2 += [os.path.join(dirpath, file) for file in filenames]
print('=' * 70)
if not filez2:
print('Could not find any MIDI files. Please check Dataset dir...')
print('=' * 70)
else:
print('Randomizing file list...')
random.shuffle(filez2)
print('Done!')
print('=' * 70)
print('Total files:', len(filez2))
print('=' * 70)
#@title Process MIDIs with TMIDIX MIDI processor
sampling_step_in_notes = 32 # @param {type:"slider", min:8, max:128, step:8}
print('=' * 70)
print('TMIDIX MIDI Processor')
print('=' * 70)
print('Starting up...')
print('=' * 70)
###########
melody_chords_f = []
print('Processing MIDI files. Please wait...')
print('=' * 70)
for i in tqdm(range(0, len(filez2), 16)):
with parallel_config(backend='threading', n_jobs=4, verbose = 0):
output = Parallel()(delayed(TMIDIX_MIDI_Processor)(f) for f in filez2[i:i+16])
for o in output:
if o is not None:
melody_chords_f.append(o)
print('Done!')
print('=' * 70)
print('Finalizng MIDI data...')
print('=' * 70)
all_test_data = []
for m in tqdm(melody_chords_f):
test_data = []
for i in range(0, len(m[0]), sampling_step_in_notes):
if len(m[0][i:i+SEQ_LEN]) == SEQ_LEN:
test_data.append(m[0][i:i+SEQ_LEN])
if test_data:
all_test_data.append([test_data, m[1]])
print('Done!')
print('=' * 70)
print('Total number of processed files:', len(all_test_data))
print('=' * 70)
"""# (SAVE/LOAD CUSTOM MIDI DATASET DATA)"""
# @title Save
print('=' * 70)
TMIDIX.Tegridy_Any_Pickle_File_Writer(all_test_data, '/content/B_CLassi_CUSTOM_MIDI_DATASET_DATA')
print('=' * 70)
# @title Load
print('=' * 70)
all_test_data = TMIDIX.Tegridy_Any_Pickle_File_Reader('/content/B_CLassi_CUSTOM_MIDI_DATASET_DATA')
print('Done!')
print('=' * 70)
#@title Classify
good_classification_threshold = 0.5 # @param {type:"slider", min:0.1, max:1, step:0.05}
print('=' * 70)
print('Classifying...')
print('=' * 70)
predictions = []
for a in tqdm(all_test_data):
features = [lst for lst in a[0]] # Corrected this line
tensor_features = tf.constant(features, dtype=tf.float32)
test_dataset = tf.data.Dataset.from_tensor_slices(tensor_features)
batched_test_dataset = test_dataset.batch(BATCH_SIZE)
yhat = model.predict(batched_test_dataset, verbose=0)
yhat_list = [y[0] for y in yhat.tolist()]
avg_label = round((sum(yhat_list) / len(yhat_list)), 4)
min_label, max_label = round(min(yhat_list), 4), round(max(yhat_list), 4)
if avg_label > 0.5:
good_or_bad = 1
else:
good_or_bad = 0
predictions.append([a[1], good_or_bad, avg_label, min_label, max_label])
print('Done!')
print('=' * 70)
print('Total number of classified MIDI:', len(predictions))
print('=' * 70)
print('TOtal number of good MIDIs:', len([y[1] for y in predictions if y[1] >= good_classification_threshold]))
print('TOtal number of bad MIDIs:', len([y[1] for y in predictions if y[1] < good_classification_threshold]))
print('=' * 70)
# @title Copy classified MIDI splits into the output dir (EVAL_OUT)
good_classification_threshold = 0.5 # @param {type:"slider", min:0.1, max:1, step:0.05}
print('=' * 70)
print('Copying...')
print('=' * 70)
GOOD_THRESHOLD = good_classification_threshold
good = 0
bad = 0
for p in tqdm(predictions):
src = '/content/EVAL_IN/' + p[0]
if p[2] > GOOD_THRESHOLD:
dest = '/content/EVAL_OUT/GOOD/' + p[0]
good += 1
else:
dest = '/content/EVAL_OUT/BAD/' + p[0]
bad += 1
shutil.copy2(src, dest)
print('Done!')
print('=' * 70)
print('Good/bad files count:', good, '/', bad)
print('=' * 70)