ParakeetRebeccaRosario/parakeet/models/fastspeech/utils.py

35 lines
1003 B
Python
Raw Normal View History

2020-01-03 16:25:17 +08:00
import numpy as np
2020-01-22 15:46:35 +08:00
def get_alignment(attn_probs, mel_lens, n_head):
2020-01-03 16:25:17 +08:00
max_F = 0
assert attn_probs[0].shape[0] % n_head == 0
batch_size = int(attn_probs[0].shape[0] // n_head)
2020-01-22 15:46:35 +08:00
#max_attn = attn_probs[0].numpy()[0,batch_size]
2020-01-03 16:25:17 +08:00
for i in range(len(attn_probs)):
multi_attn = attn_probs[i].numpy()
for j in range(n_head):
attn = multi_attn[j*batch_size:(j+1)*batch_size]
F = score_F(attn)
if max_F < F:
max_F = F
max_attn = attn
2020-01-22 15:46:35 +08:00
alignment = compute_duration(max_attn, mel_lens)
2020-01-03 16:25:17 +08:00
return alignment
def score_F(attn):
max = np.max(attn, axis=-1)
mean = np.mean(max)
return mean
2020-01-22 15:46:35 +08:00
def compute_duration(attn, mel_lens):
2020-01-03 16:25:17 +08:00
alignment = np.zeros([attn.shape[0],attn.shape[2]])
2020-01-22 15:46:35 +08:00
mel_lens = mel_lens.numpy()
2020-01-03 16:25:17 +08:00
for i in range(attn.shape[0]):
2020-01-22 15:46:35 +08:00
for j in range(mel_lens[i]):
max_index = np.argmax(attn[i,j])
2020-01-03 16:25:17 +08:00
alignment[i,max_index] += 1
return alignment