-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscoders.py
More file actions
260 lines (210 loc) · 11 KB
/
transcoders.py
File metadata and controls
260 lines (210 loc) · 11 KB
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
import numpy as np
import yaml
import numpy as np
import numpy as np
import torch.utils.data
import torch
import utils.pinv_transcoder as pt
import models as md
import yaml
from pann.pann_mel_inference import PannMelInference
from yamnet.yamnet_mel_inference import YamnetMelInference
from utils.util import get_transforms
class ThirdOctaveToMelTranscoderPinv():
def __init__(self, model_path, model_name, device, classifier='PANN'):
self.device = device
#MODELS_PATH = project_data_path / 'exp_models' / ('exp_models_' + transcoder)
# MODELS_PATH = project_data_path / 'model'
#SETTINGS_PATH = project_data_path / 'exp_settings'
# SETTINGS_PATH = project_data_path.parent.parent / "data_settings"
#settings = ut.load_settings(Path(SETTINGS_PATH / (config+'_settings'+'.yaml')))
with open(model_path + "/" + model_name + '_settings.yaml') as file:
settings_model = yaml.load(file, Loader=yaml.FullLoader)
self.input_shape = settings_model.get('input_shape')
self.output_shape = settings_model.get('output_shape')
cnn_kernel_size = settings_model.get('cnn_kernel_size')
cnn_dilation = settings_model.get('cnn_dilation')
cnn_nb_layers = settings_model.get('cnn_nb_layers')
cnn_nb_channels = settings_model.get('cnn_nb_channels')
mlp_hl_1 = settings_model.get('mlp_hl_1')
mlp_hl_2 = settings_model.get('mlp_hl_2')
#from settings of the model
classifier = settings_model.get('mels_type')
self.tho_tr, self.mels_tr = get_transforms(sr=32000,
flen=4096,
hlen=4000,
classifier=classifier,
device=device)
# self.tho_tr = tho_tr
# self.mels_tr = mels_tr
def transcode_from_thirdo(self, x, dtype=torch.FloatTensor):
x_inf = pt.pinv(x, self.tho_tr, self.mels_tr, reshape=self.output_shape[0], device=self.device)
x_inf = x_inf[0].T.numpy()
return(x_inf)
def transcode_from_1s_wav(self, x, dtype=torch.FloatTensor):
x_tho = self.tho_tr.wave_to_third_octave(x)
x_tho = torch.from_numpy(x_tho.T)
x_tho = x_tho.unsqueeze(0)
x_tho = x_tho.type(dtype)
x_mels_inf = self.transcode_from_thirdo(x_tho)
return(x_mels_inf)
def transcode_from_wav(self, x):
chunk_size = self.tho_tr.sr
x_sliced = [x[i:i+chunk_size] for i in range(0, len(x), chunk_size)]
x_mels_inf_tot = np.empty((self.mels_tr.mel_bins, 0))
for x_i in x_sliced:
x_mels_inf = self.transcode_from_1s_wav(x_i)
x_mels_inf_tot = np.concatenate((x_mels_inf_tot, x_mels_inf), axis=1)
return(x_mels_inf_tot)
class ThirdOctaveToMelTranscoder():
def __init__(self, transcoder, model_name, model_path, device):
self.device = device
#MODELS_PATH = project_data_path / 'exp_models' / ('exp_models_' + transcoder)
#model_path = project_data_path / 'model'
#SETTINGS_PATH = project_data_path / 'exp_settings'
# SETTINGS_PATH = project_data_path.parent.parent / "data_settings"
#settings = ut.load_settings(Path(SETTINGS_PATH / (config+'_settings'+'.yaml')))
with open(model_path + "/" + model_name + '_settings.yaml') as file:
settings_model = yaml.load(file, Loader=yaml.FullLoader)
input_shape = settings_model.get('input_shape')
output_shape = settings_model.get('output_shape')
cnn_kernel_size = settings_model.get('cnn_kernel_size')
cnn_dilation = settings_model.get('cnn_dilation')
cnn_nb_layers = settings_model.get('cnn_nb_layers')
cnn_nb_channels = settings_model.get('cnn_nb_channels')
mlp_hl_1 = settings_model.get('mlp_hl_1')
mlp_hl_2 = settings_model.get('mlp_hl_2')
#from settings of the model
classifier = settings_model.get('mels_type')
print('CLASSIFIER')
print(classifier)
self.tho_tr, self.mels_tr = get_transforms(sr=32000,
flen=4096,
hlen=4000,
classifier=classifier,
device=device)
if transcoder == "cnn_pinv":
self.model = md.CNN(input_shape=input_shape, output_shape=output_shape, tho_tr=self.tho_tr, mels_tr=self.mels_tr, kernel_size=cnn_kernel_size, dilation=cnn_dilation, nb_layers=cnn_nb_layers, nb_channels=cnn_nb_channels, device=device)
if transcoder == "mlp":
self.model = md.MLP(input_shape, output_shape, hl_1=mlp_hl_1, hl_2=mlp_hl_2)
state_dict = torch.load(model_path + "/" + model_name, map_location=device)
self.model.load_state_dict(state_dict)
# self.tho_tr = tho_tr
# self.mels_tr = mels_tr
if classifier == 'PANN':
self.classif_inference = PannMelInference(verbose=False)
if classifier == 'YamNet':
self.classif_inference = YamnetMelInference(verbose=False)
def transcode_from_thirdo(self, x):
x_inf = self.model(x).detach()
x_inf = x_inf[0].T.numpy()
return(x_inf)
def transcode_from_1s_wav(self, x, dtype=torch.FloatTensor):
x_tho = self.tho_tr.wave_to_third_octave(x)
x_tho = torch.from_numpy(x_tho.T)
x_tho = x_tho.unsqueeze(0)
x_tho = x_tho.type(dtype)
x_mels_inf = self.transcode_from_thirdo(x_tho)
return(x_mels_inf)
def transcode_from_wav(self, x):
chunk_size = self.tho_tr.sr
x_sliced = [x[i:i+chunk_size] for i in range(0, len(x), chunk_size)]
x_mels_inf_tot = []
x_mels_inf_tot = np.empty((self.mels_tr.mel_bins, 0))
x_logits_tot = np.empty((self.classif_inference.n_labels, 0))
for x_i in x_sliced:
x_mels_inf = self.transcode_from_1s_wav(x_i)
x_logits = self.mels_to_logit(x_mels_inf)
x_mels_inf_tot = np.concatenate((x_mels_inf_tot, x_mels_inf), axis=1)
x_logits_tot =np.concatenate((x_logits_tot, x_logits), axis=1)
#x_mels_inf_tot.append(x_mels_inf)
x_mels_inf_tot = np.array(x_mels_inf_tot)
x_logits_tot = np.array(x_logits_tot)
# x_mels_inf_tot = np.expand_dims(x_mels_inf_tot, axis=2)
# x_mels_inf_tot = np.transpose(x_mels_inf_tot, (1, 3, 2, 0))
#x_logits_tot = self.mels_to_logit_sliced(x_mels_inf_tot)
#x_logits_tot = self.mels_to_logit(x_mels_inf_tot)
return(x_mels_inf_tot, x_logits_tot)
def transcode_from_wav_entire_file(self, x):
chunk_size = self.tho_tr.sr
x_sliced = [x[i:i+chunk_size] for i in range(0, len(x), chunk_size)]
x_mels_inf_tot = np.empty((self.mels_tr.mel_bins, 0))
x_logits_tot = np.empty((self.classif_inference.n_labels, 0))
for k, x_i in enumerate(x_sliced):
x_mels_inf = self.transcode_from_1s_wav(x_i)
if k == len(x_sliced)-1:
x_mels_inf_tot = np.concatenate((x_mels_inf_tot, x_mels_inf), axis=1)
else:
x_mels_inf_tot = np.concatenate((x_mels_inf_tot, x_mels_inf[:, :-1]), axis=1)
x_mels_inf_tot = np.array(x_mels_inf_tot)
return(x_mels_inf_tot)
def wave_to_mels_sliced(self, x):
chunk_size = self.mels_tr.sr
x_sliced = [x[i:i+chunk_size] for i in range(0, len(x), chunk_size)]
X = []
for x_i in x_sliced:
xi_mels = self.mels_tr.wave_to_mels(x_i)
X.append(xi_mels)
X = np.array(X)
X = np.expand_dims(X, axis=2)
X = np.transpose(X, (1, 3, 2, 0))
return(X)
def mels_to_logit(self, x):
temp = torch.from_numpy(np.expand_dims(np.expand_dims(x.T, axis=0), axis=0))
temp = temp.to(torch.float32)
x_logits = self.classif_inference.simple_inference(temp, no_grad=True, mean=True)
#COMMENT EVERYTHING IF MEAN IS TAKEN
if len(x_logits.shape) > 2:
# Determine the number of full batches of 101 samples that can be extracted from the tensor
num_batches = x_logits.size(1) // 101
# Slice the tensor to remove any extra samples that don't fit into a full batch
x_logits = x_logits[:, :num_batches * 101, :]
# Reshape the tensor to split the second dimension into batches of 101 samples
x_logits = x_logits.reshape(1, -1, 101, 527)
#print('BEFORE')
#print(x_logits.shape)
# Compute the maximum value along the second dimension (i.e., the batch dimension)
x_logits, _ = torch.max(x_logits, dim=2)
#print('AFTER')
#print(x_logits.shape)
# The resulting tensor will have size [1, 10, 527], where 10 is the number of batches of 101 samples in the original tensor
#print(x_logits.size())
x_logits = x_logits.numpy().T
return(x_logits)
def mels_to_logit_entire_file(self, x, slice=True, batch_size=100):
temp = torch.from_numpy(np.expand_dims(np.expand_dims(x.T, axis=0), axis=0))
temp = temp.to(torch.float32)
x_logits = self.classif_inference.simple_inference(temp, no_grad=True, mean=not slice)
if (len(x_logits.shape) > 2 & slice==True):
x_logits = x_logits[:, :-1, :]
# Determine the number of full batches of 101 samples that can be extracted from the tensor
num_batches = x_logits.size(1) // batch_size
# Slice the tensor to remove any extra samples that don't fit into a full batch
x_logits = x_logits[:, :num_batches * batch_size, :]
# Reshape the tensor to split the second dimension into batches of 101 samples
x_logits = x_logits.reshape(1, -1, batch_size, x_logits.shape[2])
# Compute the maximum (or mean) value along the second dimension (i.e., the batch dimension)
x_logits = torch.mean(x_logits, dim=2)
x_logits, _ = torch.max(x_logits, dim=1)
#x_logits = torch.mean(x_logits, dim=1)
x_logits = x_logits.numpy().T
return(x_logits)
def mels_to_logit_sliced(self, x):
X = []
if len(x.shape) > 2:
for i in range(x.shape[-1]):
xi = x[:, :, 0, i]
temp = torch.from_numpy(np.expand_dims(np.expand_dims(xi.T, axis=0), axis=0))
temp = temp.to(torch.float32)
x_logits = self.classif_inference.simple_inference(temp, no_grad=True)
x_logits = x_logits.numpy().T
X.append(x_logits)
X = np.array(X)
#X = X.max(axis=0)
X = X.mean(axis=0)
else:
temp = torch.from_numpy(np.expand_dims(np.expand_dims(x.T, axis=0), axis=0))
temp = temp.to(torch.float32)
x_logits = self.classif_inference.simple_inference(temp, no_grad=True)
X = x_logits.numpy().T
return(X)