Wav2Vec Aligner
Item
dataclass
A data class that represents an item with a sentence, a path to a wav file, and an output path.
Source code in training/preprocess/wav2vec_aligner.py
15 16 17 18 19 20 21 22 23 |
|
Point
dataclass
A data class that represents a point with a token index, a time index, and a score.
Source code in training/preprocess/wav2vec_aligner.py
26 27 28 29 30 31 32 33 34 |
|
Segment
dataclass
A data class that represents a segment with a label, a start time, an end time, a duration, and a score.
Source code in training/preprocess/wav2vec_aligner.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
Wav2VecAligner
Bases: Module
Wav2VecAligner model.
The Wav2VecAligner model is designed for aligning audio data with text data. This class handles the training and validation of the Wav2VecAligner model.
Attributes config (AutoConfig): The configuration for the pre-trained model. model (AutoModelForCTC): The pre-trained model. processor (AutoProcessor): The processor for the pre-trained model. labels (List): The labels from the vocabulary of the tokenizer. blank_id (int): The ID of the blank token.
Methods load_audio: Load an audio file from the specified path. encode: Encode the labels. decode: Decode the tokens. align_single_sample: Align a single sample of audio data with the corresponding text. get_trellis: Build a trellis matrix that represents the probabilities of each source token being at a certain time step. backtrack: Walk backwards from the last merge_repeats: Merge repeated tokens into a single segment. merge_words: Merge words in the given path. forward: Perform the forward pass of the model, which involves loading the audio data, aligning the audio with the text, building the trellis, backtracking to find the optimal path, merging repeated tokens, and finally merging words.
Source code in training/preprocess/wav2vec_aligner.py
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
|
__init__(model_name='facebook/wav2vec2-base-960h')
Initialize a new instance of the Wav2VecAligner class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name |
str
|
The name of the pre-trained model to use. Defaults to "facebook/wav2vec2-base-960h". |
'facebook/wav2vec2-base-960h'
|
Source code in training/preprocess/wav2vec_aligner.py
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 |
|
align_single_sample(audio_input, text)
Align a single sample of audio data with the corresponding text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
audio_input |
Tensor
|
The audio data. |
required |
text |
str
|
The corresponding text. |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, List, str]
|
Tuple[torch.Tensor, List, str]: A tuple containing the emissions, the tokens, and the transcript. |
Source code in training/preprocess/wav2vec_aligner.py
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 |
|
backtrack(trellis, emission, tokens)
Walk backwards from the last (sentence_token, time_step) pair to build the optimal sequence alignment path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trellis |
Tensor
|
The trellis matrix. |
required |
emission |
Tensor
|
The emission tensor. |
required |
tokens |
List
|
The list of tokens. |
required |
Returns:
Type | Description |
---|---|
List[Point]
|
List[Point]: The optimal sequence alignment path. |
Source code in training/preprocess/wav2vec_aligner.py
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 |
|
forward(wav_path, text)
Perform the forward pass of the model, which involves loading the audio data, aligning the audio with the text, building the trellis, backtracking to find the optimal path, merging repeated tokens, and finally merging words.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wav_path |
str
|
The path to the audio file. |
required |
text |
str
|
The corresponding text. |
required |
Returns:
Type | Description |
---|---|
List[Segment]
|
List[Segment]: The list of segments representing the alignment of the audio data with the text. |
Source code in training/preprocess/wav2vec_aligner.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|
get_trellis(emission, tokens)
Build a trellis matrix of shape (num_frames + 1, num_tokens + 1) that represents the probabilities of each source token being at a certain time step.
Since we are looking for the most likely transitions, we take the more likely path for the value of $k_{(t+1,j+1)}$, that is:
$k_{t+1, j+1} = \max(k_{t, j} p_{t+1, c_{j+1}}, k_{t, j+1} p_{t+1, \text{repeat}})$
Parameters:
Name | Type | Description | Default |
---|---|---|---|
emission |
Tensor
|
The emission tensor. |
required |
tokens |
List
|
The list of tokens. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The trellis matrix. |
Source code in training/preprocess/wav2vec_aligner.py
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 |
|
load_audio(wav_path)
Load an audio file from the specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wav_path |
str
|
The path to the audio file. |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, int]
|
Tuple[torch.Tensor, int]: A tuple containing the loaded audio data and the sample rate, or a FileNotFoundError if the file does not exist. |
Source code in training/preprocess/wav2vec_aligner.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
merge_repeats(path, transcript)
Merge repeated tokens into a single segment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
List[Point]
|
The sequence alignment path. |
required |
transcript |
str
|
The transcript. |
required |
Returns:
Type | Description |
---|---|
List[Segment]
|
List[Segment]: The list of segments. |
Note: this shouldn't affect repeated characters from the
original sentences (e.g. ll
in hello
)
Source code in training/preprocess/wav2vec_aligner.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
|
merge_words(segments, separator='|')
Merge words in the given path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
segments |
List[Segment]
|
The list of segments. |
required |
separator |
str
|
The separator character. Defaults to "|". |
'|'
|
Returns:
Type | Description |
---|---|
List[Segment]
|
List[Segment]: The list of merged words. |
Source code in training/preprocess/wav2vec_aligner.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
save_segments(wav_path, text, save_dir)
Perform the forward pass of the model to get the segments and save each segment to a file. Used for debugging purposes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wav_path |
str
|
The path to the audio file. |
required |
text |
str
|
The corresponding text. |
required |
save_dir |
str
|
The directory where the audio files should be saved. |
required |
Returns:
Type | Description |
---|---|
None |
Source code in training/preprocess/wav2vec_aligner.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
|