-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.py
498 lines (398 loc) · 21.5 KB
/
benchmark.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
import json
import os
from audiocommons_ffont.key_estimation.evaluation_metrics import *
from audiocommons_ffont.tempo_estimation.evaluation_metrics import *
import numpy as np
ANNOTATIONS_FILE = "./annotation_dict.json"
FSL10K_PATH = "/mnt/f/code/research/freesound-loop-annotator/static/FSL10K/"
METADATA_FILE = os.path.join(FSL10K_PATH,"metadata.json")
AUDIO_FILE = os.path.join(FSL10K_PATH, "audio/wav/")
BENCHMARK_PATH = os.path.join(FSL10K_PATH,"benchmarking/")
def get_annotations():
with open(ANNOTATIONS_FILE, "r") as ann_file:
annotations = json.load(ann_file)
return annotations
def evaluate_user_annotations(annotations,strong_aggreement=False):
with open(METADATA_FILE, "r") as md_file:
md = json.load(md_file)
same_tempo_count = 0
different_tempo_count = 0
for sound_id in annotations:
if strong_aggreement:
same_tempo = 0
for annotation in annotations[sound_id]:
try:
if float(annotation["bpm"]) == md[sound_id]["annotations"]["bpm"]:
same_tempo +=1
except Exception as e:
continue
if same_tempo == len(annotations[sound_id]):
same_tempo_count+=1
else:
different_tempo_count+=1
else:
same_tempo = False
for annotation in annotations[sound_id]:
try:
if float(annotation["bpm"]) == md[sound_id]["annotations"]["bpm"]:
same_tempo = True
except Exception as e:
continue
if same_tempo == True:
same_tempo_count+=1
else:
different_tempo_count+=1
total = same_tempo_count + different_tempo_count
print(str(same_tempo_count) + " of " + str(total) + " have the same tempo as the one provided by the uploader")
print(str(different_tempo_count) + " of " + str(total) + " have a different tempo than the one provided by the uploader")
print(str(100*same_tempo_count/total) + "%% of the sounds have the same tempo as the one provided by the uploader")
print(str(100*different_tempo_count/total) + "%% of the sounds have a different tempo than the one provided by the uploader")
def plot_tempo_dist(annotations):
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from collections import Counter
all_tempos = []
same_tempos = []
for sound_id in annotations:
#Compile all annotations for all loops
for annotation in annotations[sound_id]:
try:
all_tempos.append(float(annotation["bpm"]))
except Exception as e:
continue
#If there are two annotations for the loop
if len(annotations[sound_id]) == 2:
if annotations[sound_id][0]["bpm"] == annotations[sound_id][1]["bpm"]:
try:
same_tempos.append(float(annotations[sound_id][0]["bpm"]))
except Exception as e:
continue
#If there is only one annotation for the loop
else:
try:
same_tempos.append(float(annotations[sound_id][0]["bpm"]))
except Exception as e:
continue
min_tempo = 30
max_tempo = 300
plt.figure(figsize=(8,4))
fig = plt.hist(all_tempos, bins=max_tempo-min_tempo, range=(min_tempo,max_tempo), label="Histogram of all tempo annotations")
plt.xticks(ticks=range(min_tempo,max_tempo,10),rotation='vertical')
plt.savefig('histogram_all_tempo.pdf',bbox_inches='tight')
plt.clf()
plt.hist(same_tempos, bins=max_tempo-min_tempo, range=(min_tempo,max_tempo), label="Histogram of just the matching tempo annotations")
plt.xticks(ticks=range(min_tempo,max_tempo,10), rotation='vertical')
plt.savefig('histogram_same_tempo.png')
plt.clf()
print(Counter(same_tempos))
return None
def plot_key_dist(annotations):
import matplotlib.pyplot as plt
from collections import Counter
all_keys = []
same_keys = []
all_modes = []
same_modes = []
all_keys_modes = []
same_keys_modes = []
for sound_id in annotations:
#Collect all annotations of key and mode
for annotation in annotations[sound_id]:
all_keys.append(annotation["key"])
all_modes.append(annotation["mode"])
all_keys_modes.append(annotation["key"] + " " + annotation["mode"])
#If there are two annotations for the loop
if len(annotations[sound_id]) == 2:
if annotations[sound_id][0]["key"] == annotations[sound_id][1]["key"]:
same_keys.append(annotations[sound_id][0]["key"])
if annotations[sound_id][0]["mode"] == annotations[sound_id][1]["mode"]:
same_modes.append(annotations[sound_id][0]["mode"])
if annotations[sound_id][0]["mode"] == annotations[sound_id][1]["mode"] and annotations[sound_id][0]["key"] == annotations[sound_id][1]["key"]:
same_keys_modes.append(annotation["key"] + " " + annotation["mode"])
else:
same_keys.append(annotations[sound_id][0]["key"])
same_keys.append(annotations[sound_id][0]["mode"])
same_keys_modes.append(annotation["key"] + " " + annotation["mode"])
for annotation in [all_keys, same_keys, all_modes, same_modes, all_keys_modes, same_keys_modes]:
print(Counter(annotation))
total = len(all_keys_modes)
return all_keys_modes,same_keys_modes
def get_key_dist(annotations):
from collections import Counter
all_keys_modes = []
for sound_id in annotations:
for annotation in annotations[sound_id]:
if annotation["key"] not in [None, "none","unknown"]:
all_keys_modes.append(annotation["key"] + " " + annotation["mode"])
count = Counter(all_keys_modes)
total = len(all_keys_modes)
print("& maj"+" & "+"min"+" & "+'none'+" & "+'unknown')
for key in ["c","c#","d",'d#','e','f','f#','g','g#','a','a#','b']:
print()
print(key, end ="")
for mode in ["maj","min",'none','unknown']:
keymode = key + " " + mode
print(" & " + "{:.2%}".format(count[keymode]/total), end ="")
def plot_inst_dist(annotations):
instrumentations = {"instrumentation_percussion":0,"instrumentation_bass":0,"instrumentation_chords":0,"instrumentation_melody":0,"instrumentation_fx":0,"instrumentation_vocal":0}
count = 0
count1 = 0
count2 = 0
for sound_id in annotations:
#Compile all annotations for all loops
if len(annotations[sound_id]) == 1:
count1 = count1 + 1
else:
count2 = count2 + 1
for annotation in annotations[sound_id]:
count = count + 1
for instrument in instrumentations.keys():
if annotation[instrument] == 'True':
instrumentations[instrument] = instrumentations[instrument] + 1
for instrument in instrumentations:
print(instrument + " & {:.2%}".format(instrumentations[instrument]/count))
print("N Sounds annotated:" + str(len(annotations)))
print("N Annotations" + str(count))
print("1ann" + str(count1))
print("2ann" + str(count2))
def plt_genre_dist(annotations):
genres = {"genre_bass_music":0,"genre_live_sounds":0,"genre_cinematic":0,"genre_global":0,"genre_hip_hop":0,"genre_house_techno":0,"genre_other_dance_music":0}
count = 0
for sound_id in annotations:
#Compile all annotations for all loops
for annotation in annotations[sound_id]:
count = count + 1
for gen in genres.keys():
if annotation[gen] == 'True':
genres[gen] = genres[gen] + 1
for gen in genres:
print(gen + " & {:.2%}".format(genres[gen]/count))
def benchmark_tempo_annotations():
from collections import defaultdict
any_tempos1, any_tempos2, same_tempos, one_tempos, user_tempos = compile_tempo_annotations(get_annotations())
one_tempo = [same_tempos, one_tempos, user_tempos]
one_tempo_names = ['Same Tempos', 'One Annotation Tempos', 'User Provided Tempos']
analysis_algorithms = ['analysis_rhythm_essentia_basic','analysis_rhythm_gkiokas12','analysis_rhythm_madmom','analysis_rhythm_madmom_acf',
'analysis_rhythm_madmom_dbn','analysis_rhythm_percival_essentia','analysis_rhythm_percival14','analysis_rhythm_percival14_mod']
analysis_data = defaultdict(dict)
for algorithm in analysis_algorithms:
with open(os.path.join(BENCHMARK_PATH, algorithm+".json"),"r") as anly_file:
analysis = json.load(anly_file)
for sound_id in analysis:
analysis_data[sound_id].update(analysis[sound_id])
for idx, annotations in enumerate(one_tempo):
data_for_eval = defaultdict(dict)
for sound_id in annotations:
try:
data_for_eval[sound_id]["annotations"] = {"bpm": float(annotations[sound_id]["annotations"]["bpm"])}
data_for_eval[sound_id]["analysis"] = analysis_data[sound_id]
except Exception as ex:
ex
algorithm_list = ['Percival14_essentia','Percival14','Zapata14','Degara12','Bock15','Bock15ACF','Bock15DBN',]
print(one_tempo_names[idx])
print("Algorithm & Accuracy1 & Accuracy1e & Accuracy2 & Mean Accuracy")
for algorithm in algorithm_list:
a1 = np.mean(accuracy1(data_for_eval,algorithm))*100
a1e = np.mean(accuracy1e(data_for_eval,algorithm))*100
a2 = np.mean(accuracy2(data_for_eval,algorithm))*100
ma = np.mean([a1,a1e,a2])
print(algorithm + " & " + "{:.2f}".format(a1) + " & " + "{:.2f}".format(a1e) + " & " + "{:.2f}".format(a2) + " & " + "{:.2f}".format(ma))
data_for_eval1 = defaultdict(dict)
data_for_eval2 = defaultdict(dict)
for sound_id in any_tempos1:
try:
data_for_eval1[sound_id]["annotations"] = {"bpm": float(any_tempos1[sound_id]["annotations"]["bpm"])}
data_for_eval1[sound_id]["analysis"] = analysis_data[sound_id]
data_for_eval2[sound_id]["annotations"] = {"bpm": float(any_tempos2[sound_id]["annotations"]["bpm"])}
data_for_eval2[sound_id]["analysis"] = analysis_data[sound_id]
except Exception as ex:
ex
algorithm_list = ['Percival14_essentia','Percival14','Zapata14','Degara12','Bock15','Bock15ACF','Bock15DBN']
print("Different Tempos")
print("Algorithm & Accuracy1 & Accuracy1e & Accuracy2 & Mean Accuracy")
for algorithm in algorithm_list:
a1 = np.mean(list(map(max,accuracy1(data_for_eval1,algorithm),accuracy1(data_for_eval2,algorithm))))*100
a1e = np.mean(list(map(max,accuracy1e(data_for_eval1,algorithm),accuracy1e(data_for_eval2,algorithm))))*100
a2 = np.mean(list(map(max,accuracy2(data_for_eval1,algorithm),accuracy2(data_for_eval2,algorithm))))*100
ma = np.mean([a1,a1e,a2])
print(algorithm + " & " + "{:.2f}".format(a1) + " & " + "{:.2f}".format(a1e) + " & " + "{:.2f}".format(a2) + " & " + "{:.2f}".format(ma))
def compile_tempo_annotations(annotations):
from collections import defaultdict
any_tempos1 = defaultdict(dict)
any_tempos2 = defaultdict(dict)
same_tempos = defaultdict(dict)
one_tempos = defaultdict(dict)
for sound_id in annotations:
#If there are two annotations for the loop
if len(annotations[sound_id]) == 2:
if annotations[sound_id][0]["bpm"] == annotations[sound_id][1]["bpm"]:
try:
same_tempos[sound_id]["annotations"] = {"bpm": annotations[sound_id][0]["bpm"]}
any_tempos1[sound_id]["annotations"] = {"bpm": annotations[sound_id][0]["bpm"]}
any_tempos2[sound_id]["annotations"] = {"bpm": annotations[sound_id][1]["bpm"]}
except Exception as e:
print(e)
else:
try:
any_tempos1[sound_id]["annotations"] = {"bpm": annotations[sound_id][0]["bpm"]}
any_tempos2[sound_id]["annotations"] = {"bpm": annotations[sound_id][1]["bpm"]}
except Exception as e:
print(e)
#If there is only one annotation for the loop
else:
try:
one_tempos[sound_id]["annotations"] = {"bpm": annotations[sound_id][0]["bpm"]}
except Exception as e:
print(e)
with open(METADATA_FILE, "r") as md_file:
user_tempos = json.load(md_file)
return any_tempos1, any_tempos2, same_tempos, one_tempos, user_tempos
def replace_key(key):
keys_to_replace = ['C#','D#','F#','G#','A#']
correct_keys = ['Db','Eb','Gb','Ab','Bb']
if key in keys_to_replace:
idx = keys_to_replace.index(key)
return correct_keys[idx]
else:
return key
def replace_mode(mode):
if mode == 'maj':
return 'major'
if mode == 'min':
return 'minor'
def compile_key_annotations(annotations):
from collections import defaultdict
any_keys1 = defaultdict(dict)
any_keys2 = defaultdict(dict)
same_keys = defaultdict(dict)
one_keys = defaultdict(dict)
for sound_id in annotations:
#If there are two annotations for the loop
if len(annotations[sound_id]) == 2:
if annotations[sound_id][0]["key"] == annotations[sound_id][1]["key"] and \
annotations[sound_id][0]["mode"] == annotations[sound_id][1]["mode"]:
if annotations[sound_id][0]["key"] != 'none' and annotations[sound_id][0]["mode"] != 'none' and \
annotations[sound_id][0]["key"] != 'unknown' and annotations[sound_id][0]["mode"] != 'unknown':
try:
same_keys[sound_id]["annotations"] = {"key": replace_key(annotations[sound_id][0]["key"]) + " " + replace_mode(annotations[sound_id][0]["mode"])}
any_keys1[sound_id]["annotations"] = {"key": replace_key(annotations[sound_id][0]["key"]) + " " + replace_mode(annotations[sound_id][0]["mode"])}
any_keys2[sound_id]["annotations"] = {"key": replace_key(annotations[sound_id][1]["key"]) + " " + replace_mode(annotations[sound_id][1]["mode"])}
except Exception as e:
print(e)
else:
try:
if annotations[sound_id][0]["key"] != 'none' and annotations[sound_id][1]["key"] != 'none' and \
annotations[sound_id][0]["mode"] != 'none' and annotations[sound_id][1]["mode"] != 'none' and \
annotations[sound_id][0]["key"] != 'unknown' and annotations[sound_id][0]["mode"] != 'unknown'and \
annotations[sound_id][1]["key"] != 'unknown' and annotations[sound_id][1]["mode"] != 'unknown' :
any_keys1[sound_id]["annotations"] = {"key": replace_key(annotations[sound_id][0]["key"]) + " " + replace_mode(annotations[sound_id][0]["mode"])}
any_keys2[sound_id]["annotations"] = {"key": replace_key(annotations[sound_id][1]["key"]) + " " + replace_mode(annotations[sound_id][1]["mode"])}
except Exception as e:
e
#If there is only one annotation for the loop
else:
try:
if annotations[sound_id][0]["key"] != 'none' and annotations[sound_id][0]["mode"] != 'none' and \
annotations[sound_id][0]["key"] != 'unknown' and annotations[sound_id][0]["mode"] != 'unknown':
one_keys[sound_id]["annotations"] = {"key": replace_key(annotations[sound_id][0]["key"]) + " " + replace_mode(annotations[sound_id][0]["mode"])}
except Exception as e:
e
return any_keys1, any_keys2, same_keys, one_keys
def benchmark_key_annotations():
from collections import defaultdict
any_keys1, any_keys2, same_keys, one_keys = compile_key_annotations(get_annotations())
one_key = [same_keys, one_keys]
one_key_names = ['Same Keys', 'One Annotation Keys']
analysis_algorithms = ['analysis_tonal_qmul_key_detector','analysis_tonal_key_essentia_basic','analysis_tonal_edmkey']
analysis_data = defaultdict(dict)
for algorithm in analysis_algorithms:
with open(os.path.join(BENCHMARK_PATH, algorithm+".json"),"r") as anly_file:
analysis = json.load(anly_file)
for sound_id in analysis:
analysis_data[sound_id].update(analysis[sound_id])
for idx, annotations in enumerate(one_key):
data_for_eval = defaultdict(dict)
for sound_id in annotations:
try:
data_for_eval[sound_id]["annotations"] = {"key": annotations[sound_id]["annotations"]["key"]}
data_for_eval[sound_id]["analysis"] = analysis_data[sound_id]
except Exception as ex:
print(ex)
algorithm_list = ["Edmkey","EdmkeyKrumhansl","EdmkeyTemperley","EdmkeyShaath","EssentiaBasic","QMULKeyDetector"]
print(one_key_names[idx])
print("Algorithm & Same & Fifth & Relative & Parallel & Mirex")
for algorithm in algorithm_list:
same = np.mean(mireval_key_same(data_for_eval,algorithm))*100
fifth = np.mean(mireval_key_fifth(data_for_eval,algorithm))*100
relative = np.mean(mireval_key_relative(data_for_eval,algorithm))*100
parallel = np.mean(mireval_key_parallel(data_for_eval,algorithm))*100
mirex = same + 0.5*fifth + 0.3*relative + 0.2*parallel
print(algorithm + " & " + "{:.2f}".format(same) + " & " + "{:.2f}".format(fifth) + " & " + "{:.2f}".format(relative) + " & " + "{:.2f}".format(parallel) + " & " + "{:.2f}".format(mirex))
data_for_eval1 = defaultdict(dict)
data_for_eval2 = defaultdict(dict)
keys = set(any_keys1.keys())
keys.update(any_keys2.keys())
for sound_id in keys:
try:
data_for_eval1[sound_id]["annotations"] = {"key": any_keys1[sound_id]["annotations"]["key"]}
data_for_eval1[sound_id]["analysis"] = analysis_data[sound_id]
data_for_eval2[sound_id]["annotations"] = {"key": any_keys2[sound_id]["annotations"]["key"]}
data_for_eval2[sound_id]["analysis"] = analysis_data[sound_id]
except Exception as ex:
print(ex)
print("Different Keys")
print("Algorithm & Same & Fifth & Relative & Parallel & Mirex")
for algorithm in algorithm_list:
max_accuracies = list(map(max,mireval_key_same(data_for_eval1,algorithm),mireval_key_same(data_for_eval2,algorithm), \
[0.5 * x for x in mireval_key_fifth(data_for_eval1,algorithm)], [0.5 * x for x in mireval_key_fifth(data_for_eval2,algorithm)], \
[0.3 * x for x in mireval_key_relative(data_for_eval1,algorithm)], [0.3 * x for x in mireval_key_relative(data_for_eval2,algorithm)], \
[0.2 * x for x in mireval_key_parallel(data_for_eval1,algorithm)], [0.2 * x for x in mireval_key_parallel(data_for_eval2,algorithm)]))
same = (max_accuracies.count(1)/len(max_accuracies))*100
fifth = (max_accuracies.count(0.5)/len(max_accuracies))*100
relative = (max_accuracies.count(0.3)/len(max_accuracies))*100
parallel = (max_accuracies.count(0.2)/len(max_accuracies))*100
mirex = 100 * sum(max_accuracies)/len(max_accuracies)
print(algorithm + " & " + "{:.2f}".format(same) + " & " + "{:.2f}".format(fifth) + " & " + "{:.2f}".format(relative) + " & " + "{:.2f}".format(parallel) + " & " + "{:.2f}".format(mirex))
#print(algorithm + " & " + "{:.2f}".format(same) + " & " + "{:.2f}".format(fifth) + " & " + "{:.2f}".format(relative) + " & " + "{:.2f}".format(parallel) + " & " + "{:.2f}".format(mirex))
def get_loops_with_tempo(tempo,annotations):
from shutil import copyfile
dest_dir = "./jordan_loops/"
tempo_annotations = {}
ids_to_move = []
for sound_id in annotations:
#If there are two annotations for the loop
if len(annotations[sound_id]) == 2:
if annotations[sound_id][0]["bpm"] == annotations[sound_id][1]["bpm"]:
try:
if annotations[sound_id][0]["bpm"] == "120":
tempo_annotations[sound_id] = annotations[sound_id]
ids_to_move.append(sound_id)
except Exception as e:
continue
#If there is only one annotation for the loop
else:
try:
if annotations[sound_id][0]["bpm"] == "120":
tempo_annotations[sound_id] = annotations[sound_id]
ids_to_move.append(sound_id)
except Exception as e:
continue
with open('jordan_loops.json', 'w') as filehandle:
json.dump(tempo_annotations, filehandle)
for sound_id in ids_to_move:
for root, subdirs, files in os.walk(AUDIO_FILE):
for filename in files:
if sound_id + "_" in filename:
copyfile(os.path.join(AUDIO_FILE, filename),os.path.join(dest_dir,filename))
plot_inst_dist(get_annotations())
get_key_dist(get_annotations())
plt_genre_dist(get_annotations())
plot_tempo_dist(get_annotations())
print("Weak Agreement (if the estimation matches one of the annotations)")
evaluate_user_annotations(get_annotations(),False)
print()
print("Strong Agreement (if the estimation matches both of the annotations)")
evaluate_user_annotations(get_annotations(),True)
benchmark_tempo_annotations()
benchmark_key_annotations()