Loading…
Boosting Wav2Vec2 with n-grams in 🤗 Transformers
Patrick von Platen
Summary
Wav2Vec2 models fine-tuned with Connectionist Temporal Classification transcribe speech without external language models, but decoding can still suffer from spelling inaccuracies. Hugging Face Transformers addressed this by integrating Kensho Technologies' pyctcdecode library to support decoding with n-gram language models. Instead of decoding using simple argmax operations over logits, the Wav2Vec2ProcessorWithLM class feeds full probability matrices into beam search guided by KenLM n-gram probabilities. KenLM's build_binary utility compresses language model ARPA files into binary formats, reducing file size by more than half for faster loading and hub deployment. In Swedish xls-r-300m-sv benchmarks on Common Voice 7, this 5-gram boosted decoding setup achieved an 18.85% word error rate, delivering an approximate 30% relative performance gain.
Context
Pre-trained Wav2Vec2 models can perform speech recognition without external language models due to transformer contextualization and CTC alignment, but decoding without a language model still produces spelling errors. Additionally, Hugging Face Transformers previously lacked a simple user interface to decode audio using fine-tuned Wav2Vec2 checkpoints combined with a language model.
Approach / What changed
Hugging Face Transformers integrated Kensho Technologies' pyctcdecode library and introduced Wav2Vec2ProcessorWithLM to combine fine-tuned checkpoints with KenLM n-gram language models. The workflow passes raw model output logits directly into pyctcdecode to perform beam search guided by n-gram probabilities, and utilizes KenLM's build_binary tool to convert large ARPA language model files into optimized binary formats.
Takeaways
- Decoding with Wav2Vec2ProcessorWithLM uses full output logits and beam search weighted by n-gram probabilities rather than taking greedy argmax selections.
- Converting KenLM .arpa language model files to binary format via build_binary reduced a 5-gram LM file size from over 4 GB to 1.8 GB.
- Adding a 5-gram language model to the xls-r-300m-sv checkpoint achieved an 18.85% WER on the Common Voice 7 Swedish test set, yielding an approximate 30% relative improvement.
Related reading
huggingface.co ·
Making automatic speech recognition work on large files with Wav2Vec2 in 🤗 Transformers
Transformer-based automatic speech recognition models like Wav2Vec2 crash on long audio inputs because the O(n²) attention mechanism rapidly exhausts GPU memory. Simple audio chunking avoids out-of-memory errors but causes poor transcription accuracy at chunk boundaries due to a lack of surrounding context. To resolve this, Hugging Face Transformers leverages the Connectionist Temporal Classification architecture to process overlapping audio chunks with configurable strides. The pipeline discards low-quality logits at chunk edges and chains the remaining central predictions together to reconstruct a seamless transcript. This striding technique operates out of the box with language model-augmented pipelines and adapts directly to low-latency live audio streaming.
Nicolas Patryhuggingface.co ·
Fine-Tune Wav2Vec2 for English ASR in Hugging Face with 🤗 Transformers
Wav2Vec2 is a pretrained Automatic Speech Recognition model that learns speech representations from over 50,000 hours of unlabeled audio using a contrastive objective and masked feature vectors. The model can be fine-tuned end-to-end on labeled speech datasets using Connectionist Temporal Classification without requiring an external language model. To demonstrate this process, a base-sized checkpoint is fine-tuned on the Timit dataset, which contains five hours of training data. Data preparation involves normalizing transcriptions to lowercase, removing special punctuation characters, and configuring both a specialized feature extractor and tokenizer. Evaluating the fine-tuned acoustic model without padding on the Timit test dataset yields a word error rate of 22.1%.