-
Notifications
You must be signed in to change notification settings - Fork 5
/
estimate_alignment.py
281 lines (218 loc) · 10.8 KB
/
estimate_alignment.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
"""
This script saves the phoneme or word onsets estimated by a specified model (tag) on a specified dataset
"""
import argparse
import testx
import torch
import numpy as np
import os
import soundfile as sf
import json
import matplotlib.pyplot as plt
import data
import model
import utils
def compute_phoneme_onsets(optimal_path_matrix, hop_length, sampling_rate, return_skipped_idx=False):
"""
Args:
optimal_path_matrix: binary numpy array with shape (N, M)
hop_length: int, hop length of the STFT
sampling_rate: int, sampling frequency of the audio files
Returns:
phoneme_onsets: list of phoneme onsets
"""
phoneme_indices = np.argmax(optimal_path_matrix, axis=1)
# find positions that have been skiped:
skipped_idx = [x+1 for i, (x, y) in
enumerate(zip(phoneme_indices[:-1], phoneme_indices[1:]))
if x == y - 2]
# compute index of list elements whose right neighbor is different from itself
last_idx_before_change = [i for i, (x, y) in
enumerate(zip(phoneme_indices[:-1], phoneme_indices[1:]))
if x != y]
phoneme_onsets = [(n + 1) * hop_length / sampling_rate for n in last_idx_before_change]
phoneme_onsets.insert(0, 0) # the first token's onset is 0
if return_skipped_idx:
return phoneme_onsets, skipped_idx
else:
for idx in skipped_idx:
# set the onset of skipped tokens to the onset of the previous token
phoneme_onsets.insert(idx, phoneme_onsets[idx])
return phoneme_onsets
def compute_word_alignment(phonemes, phoneme_onsets):
"""
Args:
phonemes: list of phoneme symbols as strings. '>' as space character between words
phoneme_onsets: list of phoneme onsets. Must have same length as phonemes
Returns:
word_onsets: list of word onsets
word_offsets: list of word offsets
"""
word_onsets = []
word_offsets = []
for idx, phoneme in enumerate(phonemes):
if idx == 0:
word_onsets.append(phoneme_onsets[1]) # first word onset is first phoneme onset after space
continue # skip the first space token
if phoneme == '>' and idx != len(phonemes) - 1:
word_offsets.append(phoneme_onsets[idx]) # space onset is offset of previous word
word_onsets.append(phoneme_onsets[idx+1]) # word onset is phoneme onset after space character
word_offsets.append(phoneme_onsets[-1]) # last token (space token) onset is the last word's offset
return word_onsets, word_offsets
def accumulated_cost_numpy(score_matrix, mode='max', init=None):
"""
Computes the accumulated score matrix by the "DTW forward operation"
Args:
score_matrix: distance matrix, shape(batch_size, length_sequence1, length_sequence2)
mode:
Returns:
dtw_matrix: accumulated score matrix
"""
B, N, M = score_matrix.size()
score_matrix = score_matrix.numpy().astype('float64')
if mode == 'max':
# there is an issue with pytorch backward computation when using 'faster' with pytorch 1.2.0:
# https://github.com/pytorch/pytorch/issues/24853
#dtw_matrix = np.zeros((N + 1, M + 1)) #, device=device)
dtw_matrix = np.ones((N + 1, M + 1)) * -100000
dtw_matrix[0, 0] = init
# Sweep diagonally through alphas (as done in https://github.com/lyprince/sdtw_pytorch/blob/master/sdtw.py)
# See also https://towardsdatascience.com/gpu-optimized-dynamic-programming-8d5ba3d7064f
for (m,n),(m_m1,n_m1) in zip(model.MatrixDiagonalIndexIterator(m = M + 1, n = N + 1, k_start=1),
model.MatrixDiagonalIndexIterator(m = M, n= N, k_start=0)):
d1 = dtw_matrix[n_m1, m] # shape(number_of_considered_values)
d2 = dtw_matrix[n_m1, m_m1]
max_values = np.maximum(d1, d2)
dtw_matrix[n, m] = score_matrix[0, n_m1, m_m1] + max_values
return dtw_matrix[1:N+1, 1:M+1]
def optimal_alignment_path(matrix, mode='max_numpy', init=200000):
# matrix is torch.tensor with size (1, sequence_length1, sequence_length2)
# forward step DTW
if mode == 'max_numpy':
accumulated_scores = accumulated_cost_numpy(matrix, mode='max', init=init)
N, M = accumulated_scores.shape
optimal_path_matrix = np.zeros((N, M))
optimal_path_matrix[-1, -1] = 1 # last phoneme is active at last time frame
# backtracking: go backwards through time steps n and put value of active m to 1 in optimal_path_matrix
n = N - 2
m = M - 1
while m > 0:
d1 = accumulated_scores[n, m] # score at n of optimal phoneme at n-1
d2 = accumulated_scores[n, m - 1] # score at n of phoneme before optimal phoneme at n-1
arg_max = np.argmax([d1, d2]) # = 0 if same phoneme active as before, = 1 if previous phoneme active
optimal_path_matrix[n, m - arg_max] = 1
n -= 1
m -= arg_max
if n == -2:
print("DTW backward pass failed. n={} but m={}".format(n, m))
break
optimal_path_matrix[0:n+1, 0] = 1
return optimal_path_matrix
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Alignment evaluation')
# decide model
parser.add_argument('--tag', type=str, default='JOINT3')
parser.add_argument('--vad-threshold', type=float, default=20)
args, _ = parser.parse_known_args()
tag = args.tag
parser.add_argument(
'--eval-tag',
type=str,
default= args.tag,
help ='tag for evaluation folder etc. if different from tag')
parser.add_argument(
'--testset',
type=str,
default='Hansen',
help ='dataset on which to run the evaluation')
args, _ = parser.parse_known_args()
# decide test set
test_set = args.testset
if test_set == 'Hansen':
dataset = data.Hansen()
elif test_set == 'Jamendo':
dataset = data.Jamendo()
elif test_set == 'NUS_acapella':
dataset = data.NUS(acapella=True)
elif test_set == 'NUS':
parser.add_argument('--snr', type=int, default=5) # SNR for mixing vocals and music
args, _ = parser.parse_known_args()
dataset = data.NUS(acapella=False, snr=args.snr)
model_path = 'trained_models/{}'.format(tag)
#device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
device = 'cpu'
print("Device:", device)
target = 'vocals'
# load model
model_to_test = testx.load_model(target, model_path, device)
model_to_test.return_alphas = True
model_to_test.eval()
# load model config
with open(os.path.join(model_path, target + '.json'), 'r') as stream:
config = json.load(stream)
samplerate = config['args']['samplerate']
text_units = config['args']['text_units']
nfft = config['args']['nfft']
nhop = config['args']['nhop']
mean_onset_errors = []
median_onset_errors = []
if test_set[:3] == 'NUS':
# phoneme level alignment
if test_set == 'NUS':
path_to_save_alignment = 'evaluation/{}/alignments/{}_snr{}'.format(args.eval_tag, test_set, args.snr)
else:
path_to_save_alignment = 'evaluation/{}/alignments/{}'.format(args.eval_tag, test_set)
if not os.path.isdir(path_to_save_alignment):
os.makedirs(path_to_save_alignment)
for idx in range(len(dataset)):
test_example = dataset[idx]
name = test_example['name']
audio = test_example['audio'].unsqueeze(dim=0).unsqueeze(dim=1)
phoneme_idx = test_example['text_phoneme_idx'].unsqueeze(dim=0)
true_onsets = test_example['true_onsets']
with torch.no_grad():
vocals_estimate, alphas, scores = model_to_test((audio, phoneme_idx))
optimal_path_scores = optimal_alignment_path(scores, mode='max_numpy', init=200)
phoneme_onsets = compute_phoneme_onsets(optimal_path_scores, hop_length=nhop, sampling_rate=samplerate)
np.save(os.path.join(path_to_save_alignment, name + '_onsets'), np.array(phoneme_onsets))
print(name)
abs_errors_onsets = abs(np.array(phoneme_onsets, dtype=np.float) - np.array(true_onsets, dtype=np.float))
print('onset error', np.mean(abs_errors_onsets), np.median(abs_errors_onsets))
mean_onset_errors.append(np.mean(abs_errors_onsets))
median_onset_errors.append(np.median(abs_errors_onsets))
else:
# word level alignment
path_to_save_alignment = 'evaluation/{}/alignments/{}'.format(args.eval_tag, test_set)
if not os.path.isdir(path_to_save_alignment):
os.makedirs(path_to_save_alignment)
for idx in range(len(dataset)):
test_example = dataset[idx]
name = test_example['name']
audio = test_example['audio'].unsqueeze(dim=0).unsqueeze(dim=1)
phoneme_idx = test_example['text_phoneme_idx'].unsqueeze(dim=0)
true_onsets = test_example['true_onsets']
phoneme_symbols = test_example['text_phoneme_symbols']
if dataset == 'Hansen':
true_offsets = test_example['true_offsets']
with torch.no_grad():
vocals_estimate, alphas, scores = model_to_test((audio, phoneme_idx))
# vocal activity detection
vocals_estimate = vocals_estimate[:, 0, 0, :].numpy().T
vocals_mag = np.sum(vocals_estimate, axis=0)
predicted_silence = np.nonzero(vocals_mag < args.vad_threshold)
is_space_token = torch.nonzero(phoneme_idx == 3, as_tuple=True)
score_vocals = scores
for n in predicted_silence[0]:
score_vocals[:, n, is_space_token[1]] = score_vocals.max()
optimal_path_scores_vocals = model.optimal_alignment_path(score_vocals)
phoneme_onsets = compute_phoneme_onsets(optimal_path_scores_vocals, hop_length=256, sampling_rate=16000)
word_onsets, word_offsets = compute_word_alignment(phonemes=phoneme_symbols, phoneme_onsets=phoneme_onsets)
np.save(os.path.join(path_to_save_alignment, name + '_onsets'), np.array(word_onsets))
np.save(os.path.join(path_to_save_alignment, name + '_offsets'), np.array(word_offsets))
print(name)
abs_errors_onsets = abs(np.array(word_onsets, dtype=np.float) - np.array(true_onsets, dtype=np.float))
print('onset error', np.mean(abs_errors_onsets), np.median(abs_errors_onsets))
mean_onset_errors.append(np.mean(abs_errors_onsets))
median_onset_errors.append(np.median(abs_errors_onsets))
print("Mean mean absolute error onsets:", np.mean(np.array(mean_onset_errors)))
print("Mean median absolute error onsets:", np.mean(np.array(median_onset_errors)))