Spaces:
Paused
Paused
Create dissector.py
Browse files- dissector.py +180 -0
dissector.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from scipy.ndimage import median_filter
|
2 |
+
import json
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
LOW = 250
|
6 |
+
HIGH = 4000
|
7 |
+
FPS = 100
|
8 |
+
|
9 |
+
BIN_FREQS = [
|
10 |
+
43.06640625, 64.599609375, 86.1328125, 107.666015625, 129.19921875, 150.732421875, 172.265625, 193.798828125,
|
11 |
+
215.33203125, 236.865234375, 258.3984375, 279.931640625, 301.46484375, 322.998046875, 344.53125, 366.064453125,
|
12 |
+
387.59765625, 409.130859375, 430.6640625, 452.197265625, 495.263671875, 516.796875, 538.330078125, 581.396484375,
|
13 |
+
624.462890625, 645.99609375, 689.0625, 732.12890625, 775.1953125, 839.794921875, 882.861328125, 925.927734375,
|
14 |
+
990.52734375, 1055.126953125, 1098.193359375, 1184.326171875, 1248.92578125, 1313.525390625, 1399.658203125,
|
15 |
+
1485.791015625, 1571.923828125, 1658.056640625, 1765.72265625, 1873.388671875, 1981.0546875, 2088.720703125,
|
16 |
+
2217.919921875, 2347.119140625, 2497.8515625, 2627.05078125, 2799.31640625, 2950.048828125, 3143.84765625,
|
17 |
+
3316.11328125, 3509.912109375, 3725.244140625, 3940.576171875, 4177.44140625, 4435.83984375, 4694.23828125,
|
18 |
+
4974.169921875, 5275.634765625, 5577.099609375, 5921.630859375, 6266.162109375, 6653.759765625, 7041.357421875,
|
19 |
+
7450.48828125, 7902.685546875, 8376.416015625, 8871.6796875, 9388.4765625, 9948.33984375, 10551.26953125,
|
20 |
+
11175.732421875, 11843.26171875, 12553.857421875, 13285.986328125, 14082.71484375, 14922.509765625, 15805.37109375
|
21 |
+
]
|
22 |
+
BIN_FREQS = np.array(BIN_FREQS).round().astype(int)
|
23 |
+
|
24 |
+
|
25 |
+
def to_uint8_list(arr):
|
26 |
+
"""Converts a numpy array to a list of uint8 values."""
|
27 |
+
scaled_arr = (arr * 255).astype(np.uint8)
|
28 |
+
return scaled_arr.tolist()
|
29 |
+
|
30 |
+
|
31 |
+
def apply_to_dict(d, func):
|
32 |
+
"""Recursively applies func to the leaf values of a nested dictionary."""
|
33 |
+
for key, value in d.items():
|
34 |
+
if isinstance(value, dict):
|
35 |
+
apply_to_dict(value, func)
|
36 |
+
else:
|
37 |
+
d[key] = func(value)
|
38 |
+
|
39 |
+
def convert_segments(input_data):
|
40 |
+
segments_output = []
|
41 |
+
labels_output = []
|
42 |
+
|
43 |
+
# Extracting segments and appending to the respective lists
|
44 |
+
for segment in input_data["segments"]:
|
45 |
+
segments_output.append(segment["start"])
|
46 |
+
labels_output.append(segment["label"])
|
47 |
+
|
48 |
+
# Appending the end time of the last segment
|
49 |
+
segments_output.append(input_data["segments"][-1]["end"])
|
50 |
+
|
51 |
+
return {"segments": segments_output, "labels": labels_output}
|
52 |
+
|
53 |
+
|
54 |
+
def process(specs, struct, name):
|
55 |
+
i_low = np.flatnonzero(BIN_FREQS < LOW)
|
56 |
+
i_high = np.flatnonzero(BIN_FREQS > HIGH)
|
57 |
+
i_mid = np.flatnonzero((LOW <= BIN_FREQS) & (BIN_FREQS <= HIGH))
|
58 |
+
|
59 |
+
# Compute the max energy value for each frequency band considering all instruments.
|
60 |
+
max_low = specs[:, :, i_low].max()
|
61 |
+
max_mid = specs[:, :, i_mid].max()
|
62 |
+
max_high = specs[:, :, i_high].max()
|
63 |
+
|
64 |
+
wavs_low, wavs_mid, wavs_high = [
|
65 |
+
specs[:, :, indices].mean(axis=-1)
|
66 |
+
# spec[:, indices].mean(axis=1)
|
67 |
+
for indices in [i_low, i_mid, i_high]
|
68 |
+
]
|
69 |
+
wavs_low /= max_low
|
70 |
+
wavs_mid /= max_mid
|
71 |
+
wavs_high /= max_high
|
72 |
+
assert wavs_low.max() <= 1.0
|
73 |
+
assert wavs_mid.max() <= 1.0
|
74 |
+
assert wavs_high.max() <= 1.0
|
75 |
+
|
76 |
+
navs_low = np.array([median_filter(wav, size=FPS) for wav in wavs_low])
|
77 |
+
navs_mid = np.array([median_filter(wav, size=FPS) for wav in wavs_mid])
|
78 |
+
navs_high = np.array([median_filter(wav, size=FPS) for wav in wavs_high])
|
79 |
+
|
80 |
+
navs_low = navs_low
|
81 |
+
navs_mid = navs_low + navs_mid
|
82 |
+
navs_high = navs_mid + navs_high
|
83 |
+
|
84 |
+
max_nav = np.max([navs_low.max(), navs_mid.max(), navs_high.max()])
|
85 |
+
navs_low /= max_nav
|
86 |
+
navs_mid /= max_nav
|
87 |
+
navs_high /= max_nav
|
88 |
+
assert navs_high.max() <= 1.0
|
89 |
+
|
90 |
+
data = {
|
91 |
+
'nav': {},
|
92 |
+
'wav': {},
|
93 |
+
}
|
94 |
+
|
95 |
+
for (
|
96 |
+
eg_low, eg_mid, eg_high,
|
97 |
+
nav_low, nav_mid, nav_high,
|
98 |
+
inst
|
99 |
+
) in zip(
|
100 |
+
wavs_low, wavs_mid, wavs_high,
|
101 |
+
navs_low, navs_mid, navs_high,
|
102 |
+
[
|
103 |
+
'bass',
|
104 |
+
'drum',
|
105 |
+
'other',
|
106 |
+
'vocal',
|
107 |
+
]
|
108 |
+
):
|
109 |
+
data['wav'][inst] = {
|
110 |
+
'low': eg_low,
|
111 |
+
'mid': eg_mid,
|
112 |
+
'high': eg_high,
|
113 |
+
}
|
114 |
+
|
115 |
+
data['nav'][inst] = {
|
116 |
+
'low': nav_low,
|
117 |
+
'mid': nav_mid,
|
118 |
+
'high': nav_high,
|
119 |
+
}
|
120 |
+
|
121 |
+
apply_to_dict(data, to_uint8_list)
|
122 |
+
data['duration'] = specs.shape[1] / FPS
|
123 |
+
|
124 |
+
data['scores'] = {
|
125 |
+
"segment": {
|
126 |
+
"[email protected]":0,
|
127 |
+
"[email protected]":0,
|
128 |
+
"[email protected]":0,
|
129 |
+
"[email protected]":0,
|
130 |
+
"[email protected]":0,
|
131 |
+
"[email protected]":0,
|
132 |
+
"Ref-to-est deviation":0,
|
133 |
+
"Est-to-ref deviation":0,
|
134 |
+
"Pairwise Precision":0,
|
135 |
+
"Pairwise Recall":0,
|
136 |
+
"Pairwise F-measure":0,
|
137 |
+
"Rand Index":0,
|
138 |
+
"Adjusted Rand Index":0,
|
139 |
+
"Mutual Information":0,
|
140 |
+
"Adjusted Mutual Information":0,
|
141 |
+
"Normalized Mutual Information":0,
|
142 |
+
"NCE Over":0,
|
143 |
+
"NCE Under":0,
|
144 |
+
"NCE F-measure":0,
|
145 |
+
"V Precision":0,
|
146 |
+
"V Recall":0,
|
147 |
+
"V-measure":0,
|
148 |
+
"Accuracy":0
|
149 |
+
},
|
150 |
+
"beat": {
|
151 |
+
"f1":0,
|
152 |
+
"precision":0,
|
153 |
+
"recall":0,
|
154 |
+
"cmlt":0,
|
155 |
+
"amlt":0
|
156 |
+
},
|
157 |
+
"downbeat": {
|
158 |
+
"f1":0,
|
159 |
+
"precision":0,
|
160 |
+
"recall":0,
|
161 |
+
"cmlt":0,
|
162 |
+
"amlt":0
|
163 |
+
}
|
164 |
+
}
|
165 |
+
|
166 |
+
data['id'] = name
|
167 |
+
|
168 |
+
data['truths'] = {'beats': struct['beats'], 'downbeats': struct['downbeats'], **convert_segments(struct)}
|
169 |
+
data['inferences'] = data['truths']
|
170 |
+
|
171 |
+
filename = f'dissector/{name}.json'
|
172 |
+
|
173 |
+
with open(filename, 'w') as file:
|
174 |
+
file.write(json.dumps(data))
|
175 |
+
|
176 |
+
return filename
|
177 |
+
|
178 |
+
def generate_dissector_data(name, result):
|
179 |
+
specs = np.load(f'spec/{name}.npy')
|
180 |
+
return process(specs, result, name)
|