Skip to content

Commit bb051f7

Browse files
authored
Merge pull request speechbrain#949 from TParcollet/ctc_interface
CTC ASR Interface (EncoderASR)
2 parents 56e0282 + 8092d95 commit bb051f7

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

222 KB
Binary file not shown.

speechbrain/pretrained/interfaces.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,121 @@ def transcribe_batch(self, wavs, wav_lens):
484484
return predicted_words, predicted_tokens
485485

486486

487+
class EncoderASR(Pretrained):
488+
"""A ready-to-use Encoder ASR model
489+
490+
The class can be used either to run only the encoder (encode()) to extract
491+
features or to run the entire encoder + decoder function model
492+
(transcribe()) to transcribe speech. The given YAML must contains the fields
493+
specified in the *_NEEDED[] lists.
494+
495+
Example
496+
-------
497+
>>> from speechbrain.pretrained import EncoderASR
498+
>>> tmpdir = getfixture("tmpdir")
499+
>>> asr_model = EncoderASR.from_hparams(
500+
... source="speechbrain/asr-wav2vec2-commonvoice-fr",
501+
... savedir=tmpdir,
502+
... ) # doctest: +SKIP
503+
>>> asr_model.transcribe_file("samples/audio_samples/example_fr.wav") # doctest: +SKIP
504+
"""
505+
506+
HPARAMS_NEEDED = ["tokenizer", "decoding_function"]
507+
MODULES_NEEDED = ["encoder"]
508+
509+
def __init__(self, *args, **kwargs):
510+
super().__init__(*args, **kwargs)
511+
self.tokenizer = self.hparams.tokenizer
512+
self.decoding_function = self.hparams.decoding_function
513+
514+
def transcribe_file(self, path):
515+
"""Transcribes the given audiofile into a sequence of words.
516+
517+
Arguments
518+
---------
519+
path : str
520+
Path to audio file which to transcribe.
521+
522+
Returns
523+
-------
524+
str
525+
The audiofile transcription produced by this ASR system.
526+
"""
527+
waveform = self.load_audio(path)
528+
# Fake a batch:
529+
batch = waveform.unsqueeze(0)
530+
rel_length = torch.tensor([1.0])
531+
predicted_words, predicted_tokens = self.transcribe_batch(
532+
batch, rel_length
533+
)
534+
return str(predicted_words[0])
535+
536+
def encode_batch(self, wavs, wav_lens):
537+
"""Encodes the input audio into a sequence of hidden states
538+
539+
The waveforms should already be in the model's desired format.
540+
You can call:
541+
``normalized = EncoderASR.normalizer(signal, sample_rate)``
542+
to get a correctly converted signal in most cases.
543+
544+
Arguments
545+
---------
546+
wavs : torch.tensor
547+
Batch of waveforms [batch, time, channels] or [batch, time]
548+
depending on the model.
549+
wav_lens : torch.tensor
550+
Lengths of the waveforms relative to the longest one in the
551+
batch, tensor of shape [batch]. The longest one should have
552+
relative length 1.0 and others len(waveform) / max_length.
553+
Used for ignoring padding.
554+
555+
Returns
556+
-------
557+
torch.tensor
558+
The encoded batch
559+
"""
560+
wavs = wavs.float()
561+
wavs, wav_lens = wavs.to(self.device), wav_lens.to(self.device)
562+
encoder_out = self.modules.encoder(wavs, wav_lens)
563+
return encoder_out
564+
565+
def transcribe_batch(self, wavs, wav_lens):
566+
"""Transcribes the input audio into a sequence of words
567+
568+
The waveforms should already be in the model's desired format.
569+
You can call:
570+
``normalized = EncoderASR.normalizer(signal, sample_rate)``
571+
to get a correctly converted signal in most cases.
572+
573+
Arguments
574+
---------
575+
wavs : torch.tensor
576+
Batch of waveforms [batch, time, channels] or [batch, time]
577+
depending on the model.
578+
wav_lens : torch.tensor
579+
Lengths of the waveforms relative to the longest one in the
580+
batch, tensor of shape [batch]. The longest one should have
581+
relative length 1.0 and others len(waveform) / max_length.
582+
Used for ignoring padding.
583+
584+
Returns
585+
-------
586+
list
587+
Each waveform in the batch transcribed.
588+
tensor
589+
Each predicted token id.
590+
"""
591+
with torch.no_grad():
592+
wav_lens = wav_lens.to(self.device)
593+
encoder_out = self.encode_batch(wavs, wav_lens)
594+
predictions = self.decoding_function(encoder_out, wav_lens)
595+
predicted_words = [
596+
self.tokenizer.decode_ids(token_seq)
597+
for token_seq in predictions
598+
]
599+
return predicted_words, predictions
600+
601+
487602
class EncoderClassifier(Pretrained):
488603
"""A ready-to-use class for utterance-level classification (e.g, speaker-id,
489604
language-id, emotion recognition, keyword spotting, etc).

0 commit comments

Comments
 (0)