This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grapheme_aligner.py
174 lines (142 loc) · 5.75 KB
/
grapheme_aligner.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
from dataclasses import dataclass
from typing import Union, List
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.nn.utils.rnn import pad_sequence
import torchaudio
@dataclass
class Point:
token_index: int
time_index: int
score: float
@dataclass
class Segment:
label: str
start: int
end: int
score: float
def __repr__(self):
return f"{self.label}\t({self.score:4.2f}): [{self.start:5d}, {self.end:5d})"
@property
def length(self):
return self.end - self.start
class GraphemeAligner(nn.Module):
def __init__(self, orig_sr):
super().__init__()
self._wav2vec2 = torchaudio.pipelines.WAV2VEC2_ASR_BASE_960H.get_model()
self._labels = torchaudio.pipelines.WAV2VEC2_ASR_BASE_960H.get_labels()
self._char2index = {c: i for i, c in enumerate(self._labels)}
self._unk_index = self._char2index["<unk>"]
self._resampler = torchaudio.transforms.Resample(
orig_freq=orig_sr, new_freq=16_000
)
def _decode_text(self, text):
text = text.replace(" ", "|").upper()
return torch.tensor([
self._char2index.get(char, self._unk_index)
for char in text
]).long()
@torch.no_grad()
def forward(
self,
waveform: torch.Tensor,
waveform_lengths: torch.Tensor,
text: Union[str, List[str]],
*args,
**kwargs
):
wav2vec_device = next(self._wav2vec2.parameters()).device
waveform = waveform.to(wav2vec_device)
if isinstance(text, str):
text = [text]
batch_size = waveform.shape[0]
durations = []
for index in range(batch_size):
current_wav = waveform[index, :waveform_lengths[index]].unsqueeze(dim=0)
current_wav = self._resampler(current_wav)
emission, _ = self._wav2vec2(current_wav)
emission = emission.log_softmax(dim=-1).squeeze(dim=0).cpu()
tokens = self._decode_text(text[index])
trellis = self._get_trellis(emission, tokens)
path = self._backtrack(trellis, emission, tokens)
segments = self._merge_repeats(text[index], path)
num_frames = emission.shape[0]
relative_durations = torch.tensor([
segment.length / num_frames for segment in segments
])
durations.append(relative_durations)
durations = pad_sequence(durations).transpose(0, 1)
return durations
def _get_trellis(self, emission, tokens, blank_id=0):
num_frame = emission.size(0)
num_tokens = len(tokens)
# Trellis has extra dimension for both time axis and tokens.
# The extra dim for tokens represents <SoS> (start-of-sentence)
# The extra dim for time axis is for simplification of the code.
trellis = torch.full((num_frame + 1, num_tokens + 1), float("-inf"))
trellis[:, 0] = 0
for t in range(num_frame):
trellis[t + 1, 1:] = torch.maximum(
# Score for staying at the same token
trellis[t, 1:] + emission[t, blank_id],
# Score for changing to the next token
trellis[t, :-1] + emission[t, tokens],
)
return trellis
def _backtrack(self, trellis, emission, tokens, blank_id=0):
# Note:
# j and t are indices for trellis, which has extra dimensions
# for time and tokens at the beginning.
# When refering to time frame index `T` in trellis,
# the corresponding index in emission is `T-1`.
# Similarly, when refering to token index `J` in trellis,
# the corresponding index in transcript is `J-1`.
j = trellis.size(1) - 1
t_start = torch.argmax(trellis[:, j]).item()
path = []
for t in range(t_start, 0, -1):
# 1. Figure out if the current position was stay or change
# Note (again):
# `emission[J-1]` is the emission at time frame `J` of trellis dimension.
# Score for token staying the same from time frame J-1 to T.
stayed = trellis[t - 1, j] + emission[t - 1, blank_id]
# Score for token changing from C-1 at T-1 to J at T.
changed = trellis[t - 1, j - 1] + emission[t - 1, tokens[j - 1]]
# 2. Store the path with frame-wise probability.
prob = emission[t - 1, tokens[j - 1]
if changed > stayed else 0].exp().item()
# Return token index and time index in non-trellis coordinate.
path.append(Point(j - 1, t - 1, prob))
# 3. Update the token
if changed > stayed:
j -= 1
if j == 0:
break
else:
raise ValueError("Failed to align")
return path[::-1]
def _merge_repeats(self, text, path):
i1, i2 = 0, 0
segments = []
while i1 < len(path):
while i2 < len(path) and path[i1].token_index == path[i2].token_index:
i2 += 1
score = sum(path[k].score for k in range(i1, i2)) / (i2 - i1)
segments.append(
Segment(
text[path[i1].token_index],
path[i1].time_index,
path[i2 - 1].time_index + 1,
score
)
)
i1 = i2
return segments
@staticmethod
def plot_trellis_with_path(trellis, path):
# to plot trellis with path, we take advantage of 'nan' value
trellis_with_path = trellis.clone()
for i, p in enumerate(path):
trellis_with_path[p.time_index, p.token_index] = float("nan")
plt.imshow(trellis_with_path[1:, 1:].T, origin="lower")