wav2vec2_with_lm
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm
¶
Speech processor class for Wav2Vec2
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2DecoderWithLMOutput
dataclass
¶
Bases: ModelOutput
Output type of [Wav2Vec2DecoderWithLM
], with transcription.
PARAMETER | DESCRIPTION |
---|---|
text |
Decoded logits in text from. Usually the speech transcription.
TYPE:
|
logit_score |
Total logit score of the beams associated with produced text.
TYPE:
|
lm_score |
Fused lm_score of the beams associated with produced text.
TYPE:
|
word_offsets |
Offsets of the decoded words. In combination with sampling rate and model downsampling rate word offsets can be used to compute time stamps for each word.
TYPE:
|
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM
¶
Bases: ProcessorMixin
Constructs a Wav2Vec2 processor which wraps a Wav2Vec2 feature extractor, a Wav2Vec2 CTC tokenizer and a decoder with language model support into a single processor for language model boosted speech recognition decoding.
PARAMETER | DESCRIPTION |
---|---|
feature_extractor |
An instance of [
TYPE:
|
tokenizer |
An instance of [
TYPE:
|
decoder |
An instance of [
TYPE:
|
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
|
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM.language_model
property
¶
This method returns the language model associated with the Wav2Vec2ProcessorWithLM instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The Wav2Vec2ProcessorWithLM instance.
|
RETURNS | DESCRIPTION |
---|---|
None. |
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM.__call__(*args, **kwargs)
¶
When used in normal mode, this method forwards all its arguments to Wav2Vec2FeatureExtractor's
[~Wav2Vec2FeatureExtractor.__call__
] and returns its output. If used in the context
[~Wav2Vec2ProcessorWithLM.as_target_processor
] this method forwards all its arguments to
Wav2Vec2CTCTokenizer's [~Wav2Vec2CTCTokenizer.__call__
]. Please refer to the docstring of the above two
methods for more information.
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
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 |
|
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM.__init__(feature_extractor, tokenizer, decoder)
¶
Initializes a Wav2Vec2ProcessorWithLM object.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
feature_extractor |
The feature extractor used for processing input audio data.
TYPE:
|
tokenizer |
The tokenizer used for tokenizing input text data.
TYPE:
|
decoder |
The decoder used for decoding the model's output.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. This method does not return any value. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the provided 'decoder' parameter is not an instance of BeamSearchDecoderCTC. |
ValueError
|
If there are missing tokens in the decoder's alphabet that are present in the tokenizer's vocabulary. |
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
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 |
|
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM.as_target_processor()
¶
Temporarily sets the processor for processing the target. Useful for encoding the labels when fine-tuning Wav2Vec2.
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
|
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM.batch_decode(logits, pool=None, num_processes=None, beam_width=None, beam_prune_logp=None, token_min_logp=None, hotwords=None, hotword_weight=None, alpha=None, beta=None, unk_score_offset=None, lm_score_boundary=None, output_word_offsets=False, n_best=1)
¶
Batch decode output logits to audio transcription with language model support.
This function makes use of Python's multiprocessing. Currently, multiprocessing is available only on Unix systems (see this issue).
If you are decoding multiple batches, consider creating a Pool
and passing it to batch_decode
. Otherwise,
batch_decode
will be very slow since it will create a fresh Pool
for each call. See usage example below.
PARAMETER | DESCRIPTION |
---|---|
logits |
The logits output vector of the model representing the log probabilities for each token.
TYPE:
|
pool |
An optional user-managed pool. If not set, one will be automatically created and closed. The pool
should be instantiated after Currently, only pools created with a 'fork' context can be used. If a 'spawn' pool is passed, it will be ignored and sequential decoding will be used instead.
TYPE:
|
num_processes |
If
TYPE:
|
beam_width |
Maximum number of beams at each step in decoding. Defaults to pyctcdecode's DEFAULT_BEAM_WIDTH.
TYPE:
|
beam_prune_logp |
Beams that are much worse than best beam will be pruned Defaults to pyctcdecode's DEFAULT_PRUNE_LOGP.
TYPE:
|
token_min_logp |
Tokens below this logp are skipped unless they are argmax of frame Defaults to pyctcdecode's DEFAULT_MIN_TOKEN_LOGP.
TYPE:
|
hotwords |
List of words with extra importance, can be OOV for LM
TYPE:
|
hotword_weight |
Weight factor for hotword importance Defaults to pyctcdecode's DEFAULT_HOTWORD_WEIGHT.
TYPE:
|
alpha |
Weight for language model during shallow fusion
TYPE:
|
beta |
Weight for length score adjustment of during scoring
TYPE:
|
unk_score_offset |
Amount of log score offset for unknown tokens
TYPE:
|
lm_score_boundary |
Whether to have kenlm respect boundaries when scoring
TYPE:
|
output_word_offsets |
Whether or not to output word offsets. Word offsets can be used in combination with the sampling rate and model downsampling rate to compute the time-stamps of transcribed words.
TYPE:
|
n_best |
Number of best hypotheses to return. If Please take a look at the Example of [
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
[ |
Example
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
|
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM.decode(logits, beam_width=None, beam_prune_logp=None, token_min_logp=None, hotwords=None, hotword_weight=None, alpha=None, beta=None, unk_score_offset=None, lm_score_boundary=None, output_word_offsets=False, n_best=1)
¶
Decode output logits to audio transcription with language model support.
PARAMETER | DESCRIPTION |
---|---|
logits |
The logits output vector of the model representing the log probabilities for each token.
TYPE:
|
beam_width |
Maximum number of beams at each step in decoding. Defaults to pyctcdecode's DEFAULT_BEAM_WIDTH.
TYPE:
|
beam_prune_logp |
A threshold to prune beams with log-probs less than best_beam_logp + beam_prune_logp. The value should be <= 0. Defaults to pyctcdecode's DEFAULT_PRUNE_LOGP.
TYPE:
|
token_min_logp |
Tokens with log-probs below token_min_logp are skipped unless they are have the maximum log-prob for an utterance. Defaults to pyctcdecode's DEFAULT_MIN_TOKEN_LOGP.
TYPE:
|
hotwords |
List of words with extra importance which can be missing from the LM's vocabulary, e.g. ["huggingface"]
TYPE:
|
hotword_weight |
Weight multiplier that boosts hotword scores. Defaults to pyctcdecode's DEFAULT_HOTWORD_WEIGHT.
TYPE:
|
alpha |
Weight for language model during shallow fusion
TYPE:
|
beta |
Weight for length score adjustment of during scoring
TYPE:
|
unk_score_offset |
Amount of log score offset for unknown tokens
TYPE:
|
lm_score_boundary |
Whether to have kenlm respect boundaries when scoring
TYPE:
|
output_word_offsets |
Whether or not to output word offsets. Word offsets can be used in combination with the sampling rate and model downsampling rate to compute the time-stamps of transcribed words.
TYPE:
|
n_best |
Number of best hypotheses to return. If Please take a look at the example below to better understand how to make use of
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
[ |
Example
>>> # Let's see how to retrieve time steps for a model
>>> from transformers import AutoTokenizer, AutoProcessor, AutoModelForCTC
>>> from datasets import load_dataset
>>> import datasets
>>> import torch
...
>>> # import model, feature extractor, tokenizer
>>> model = AutoModelForCTC.from_pretrained("patrickvonplaten/wav2vec2-base-100h-with-lm")
>>> processor = AutoProcessor.from_pretrained("patrickvonplaten/wav2vec2-base-100h-with-lm")
...
>>> # load first sample of English common_voice
>>> dataset = load_dataset("mozilla-foundation/common_voice_11_0", "en", split="train", streaming=True)
>>> dataset = dataset.cast_column("audio", datasets.Audio(sampling_rate=16_000))
>>> dataset_iter = iter(dataset)
>>> sample = next(dataset_iter)
...
>>> # forward sample through model to get greedily predicted transcription ids
>>> input_values = processor(sample["audio"]["array"], return_tensors="pt").input_values
>>> with torch.no_grad():
... logits = model(input_values).logits[0].cpu().numpy()
...
>>> # retrieve word stamps (analogous commands for `output_char_offsets`)
>>> outputs = processor.decode(logits, output_word_offsets=True)
>>> # compute `time_offset` in seconds as product of downsampling ratio and sampling_rate
>>> time_offset = model.config.inputs_to_logits_ratio / processor.feature_extractor.sampling_rate
...
>>> word_offsets = [
... {
... "word": d["word"],
... "start_time": round(d["start_offset"] * time_offset, 2),
... "end_time": round(d["end_offset"] * time_offset, 2),
... }
... for d in outputs.word_offsets
... ]
>>> # compare word offsets with audio `en_train_0/common_voice_en_19121553.mp3` online on the dataset viewer:
>>> # https://hf-mirror.com/datasets/mozilla-foundation/common_voice_11_0/viewer/en
>>> word_offsets[:4]
[{'word': 'THE', 'start_time': 0.68, 'end_time': 0.78},
{'word': 'TRACK', 'start_time': 0.88, 'end_time': 1.1},
{'word': 'APPEARS', 'start_time': 1.18, 'end_time': 1.66},
{'word': 'ON', 'start_time': 1.86, 'end_time': 1.92}]
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 |
|
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM.from_pretrained(pretrained_model_name_or_path, **kwargs)
classmethod
¶
Instantiate a [Wav2Vec2ProcessorWithLM
] from a pretrained Wav2Vec2 processor.
This class method is simply calling Wav2Vec2FeatureExtractor's
[~feature_extraction_utils.FeatureExtractionMixin.from_pretrained
], Wav2Vec2CTCTokenizer's
[~tokenization_utils_base.PreTrainedTokenizerBase.from_pretrained
], and
[pyctcdecode.BeamSearchDecoderCTC.load_from_hf_hub
].
Please refer to the docstrings of the methods above for more information.
PARAMETER | DESCRIPTION |
---|---|
pretrained_model_name_or_path |
This can be either:
TYPE:
|
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
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 |
|
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM.get_missing_alphabet_tokens(decoder, tokenizer)
staticmethod
¶
This method 'get_missing_alphabet_tokens' is defined in the class 'Wav2Vec2ProcessorWithLM' and is responsible for identifying missing alphabet tokens by comparing the tokenizer's vocabulary with the decoder's alphabet labels.
PARAMETER | DESCRIPTION |
---|---|
decoder |
The decoder object used for decoding tokens. It should be of type 'Decoder' and is required as an input parameter for the method.
TYPE:
|
tokenizer |
The tokenizer object used for tokenizing input data. It should be of type 'Tokenizer' and is required as an input parameter for the method.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
set
|
This method returns a set of missing tokens from the tokenizer's vocabulary that are not present in the decoder's alphabet labels. If no missing tokens are found, it returns an empty set. |
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
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 |
|
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM.pad(*args, **kwargs)
¶
When used in normal mode, this method forwards all its arguments to Wav2Vec2FeatureExtractor's
[~Wav2Vec2FeatureExtractor.pad
] and returns its output. If used in the context
[~Wav2Vec2ProcessorWithLM.as_target_processor
] this method forwards all its arguments to
Wav2Vec2CTCTokenizer's [~Wav2Vec2CTCTokenizer.pad
]. Please refer to the docstring of the above two methods
for more information.
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
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 |
|
mindnlp.transformers.models.wav2vec2_with_lm.processing_wav2vec2_with_lm.Wav2Vec2ProcessorWithLM.save_pretrained(save_directory)
¶
Save the Wav2Vec2ProcessorWithLM instance and the associated language model to the specified directory.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Wav2Vec2ProcessorWithLM class.
TYPE:
|
save_directory |
The directory path where the processor and language model will be saved.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
OSError
|
If the save_directory cannot be accessed or does not exist. |
ValueError
|
If the save_directory is not a valid directory path. |
TypeError
|
If the save_directory parameter is not a string. |
Source code in mindnlp/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|