opensi·si

The Wav2Vec2 model was proposed in wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations by Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli.

Wav2Vec2

This model was published in HF papers on 2020-06-20 and contributed to Hugging Face Transformers on 2021-02-02.

Wav2Vec2

Overview

The Wav2Vec2 model was proposed in wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations by Alexei Baevski, Henry Zhou, Abdelrahman Mohamed, Michael Auli.

The abstract from the paper is the following:

We show for the first time that learning powerful representations from speech audio alone followed by fine-tuning on transcribed speech can outperform the best semi-supervised methods while being conceptually simpler. wav2vec 2.0 masks the speech input in the latent space and solves a contrastive task defined over a quantization of the latent representations which are jointly learned. Experiments using all labeled data of Librispeech achieve 1.8/3.3 WER on the clean/other test sets. When lowering the amount of labeled data to one hour, wav2vec 2.0 outperforms the previous state of the art on the 100 hour subset while using 100 times less labeled data. Using just ten minutes of labeled data and pre-training on 53k hours of unlabeled data still achieves 4.8/8.2 WER. This demonstrates the feasibility of speech recognition with limited amounts of labeled data.

This model was contributed by patrickvonplaten.

Note: Meta (FAIR) released a new version of Wav2Vec2-BERT 2.0 - it's pretrained on 4.5M hours of audio. We especially recommend using it for fine-tuning tasks, e.g. as per this guide.

Tip Set use_kernels=True in PreTrainedModel.from_pretrained to replace supported layers with optimized kernels from the Hub. Refer to Loading kernels to learn more.

Usage tips

  • Wav2Vec2 is a speech model that accepts a float array corresponding to the raw waveform of the speech signal.
  • Wav2Vec2 model was trained using connectionist temporal classification (CTC) so the model output has to be decoded using Wav2Vec2CTCTokenizer.

Using Flash Attention 2

Flash Attention 2 is a faster, optimized version of the model.

Installation

First, check whether your hardware is compatible with Flash Attention 2. The latest list of compatible hardware can be found in the official documentation.

Next, install the latest version of Flash Attention 2:

pip install -U flash-attn --no-build-isolation

Usage

To load a model using Flash Attention 2, we can pass the argument attn_implementation="flash_attention_2" to PreTrainedModel.from_pretrained. We'll also load the model in half-precision (e.g. torch.float16), since it results in almost no degradation to audio quality but significantly lower memory usage and faster inference:

from transformers import Wav2Vec2Model

model = Wav2Vec2Model.from_pretrained("facebook/wav2vec2-large-960h-lv60-self", attn_implementation="flash_attention_2", device_map="auto")
...

Expected speedups

Below is an expected speedup diagram comparing the pure inference time between the native implementation in transformers of the facebook/wav2vec2-large-960h-lv60-self model and the flash-attention-2 and sdpa (scale-dot-product-attention) versions. We show the average speedup obtained on the librispeech_asr clean validation split:

Resources

A list of official Hugging Face and community (indicated by 🌎) resources to help you get started with Wav2Vec2. If you're interested in submitting a resource to be included here, please feel free to open a Pull Request and we'll review it! The resource should ideally demonstrate something new instead of duplicating an existing resource.

  • A notebook on how to leverage a pretrained Wav2Vec2 model for emotion classification. 🌎

  • Wav2Vec2ForCTC is supported by this notebook.

  • Audio classification task guide

  • A blog post on boosting Wav2Vec2 with n-grams in 🤗 Transformers.

  • A blog post on how to finetune Wav2Vec2 for English ASR with 🤗 Transformers.

  • A blog post on finetuning XLS-R for Multi-Lingual ASR with 🤗 Transformers.

  • A notebook on how to create YouTube captions from any video by transcribing audio with Wav2Vec2. 🌎

  • Wav2Vec2ForCTC is supported by a notebook on how to finetune a speech recognition model in English, and how to finetune a speech recognition model in any language.

  • Automatic speech recognition task guide

🚀 Deploy

  • A blog post on how to deploy Wav2Vec2 for Automatic Speech Recognition with Hugging Face's Transformers & Amazon SageMaker.

Wav2Vec2Config

[[autodoc]] Wav2Vec2Config

Wav2Vec2CTCTokenizer

[[autodoc]] Wav2Vec2CTCTokenizer - call - save_vocabulary - decode - batch_decode - set_target_lang