Bofeng Huang
Machine Learning Engineer at Zaion, working on speech recognition (ASR) and automatic natural language processing (NLP).
TL;DR
Selon l’entreprise OpenAI, la robustesse et la précision de la reconnaissance vocale en anglais n’a jamais été aussi proche de l’humain que grâce à son nouveau modèle Whisper. À travers cet article de blog, nous allons expliquer notre approche de fine-tuning du modèle Whisper pour atteindre ce niveau de performance sur d’autres langues. C’est ce travail qui nous a permis de remporter le premier prix du Whisper Fine-Tuning Event organisé par HuggingFace 🤗 et Lambda Labs, sur la langue française ainsi que la langue allemande. Les modèles et les démos issus de cet événement sont disponibles sur le Hugging Face Hub.
Foreword
Je suis Bofeng Huang, ingénieur en Machine Learning chez Zaion, le leader sur le marché européen des solutions d’IA pour relation client. Un des objectifs de Zaion est de fournir une transcription exacte et précise des conversations du service client. Il est donc crucial pour nous de disposer d’un système de reconnaissance vocale fiable, robuste et prêt à l’usage dans des conditions réelles.
At Zaion LabZaion's research team, we're constantly on the lookout for the latest trends and innovations in speech recognition. I had the opportunity to take part in the Whisper Fine-Tuning Event organized by Hugging Face 🤗 and Lambda Labs, which aims to democratize the use of the Whisper model and make it robust for as many languages as possible. I took part in both the French and German language challenges, and won first prize for both (leaderboard).
Introduction
In September 2022, OpenAI released a pre-trained automatic speech recognition (ASR) model called Whisper. Self-supervised learning models, such as wav2vec 2.0, are typically pre-trained on masked prediction tasks using unlabeled audio data, then fine-tuned on labeled data for various downstream tasks, including automatic speech recognition. Whisper models, on the other hand, are trained directly on a large amount of weakly labeled data collected from the web.
This corpus consists of 680,000 hours of multilingual, multitasking data, including transcription in several languages, translation from these languages into English, and timestamp prediction. At this scale, the model is highly robust to accents, background noise and technical language.
Whisper is a sequence-to-sequenceWhisper is a sequence-to-sequence model - a Transformer-based encoder-decoder - that maps the spectrogram representation of a recording to the corresponding sequence of sub-lexical units. This acoustic representation is calculated by a mathematical transformation from the raw signal waveforms, then analyzed by the Transformer encoder. The decoder then auto-regressively predicts the next output (or sub-lexical unit) as a function of the previous outputs and the encoder's hidden states. The figure below summarizes the architecture of the model.
Dans ce blog, nous allons montrer comment fine-tuner la version du modèle Whisper medium sur la langue française. Cette version du modèle comporte 24 couches d’encodeur et de décodeur et 769 millions de paramètres. Le code complet peut être trouvé sur le GitHub.
Data and model preparation
Loading the model
Let's start by loading the medium version of the pre-trained Whisper model:
You'll find certain arguments defined in the Whisper model, such as "forced_decoder_ids" and "suppress_tokens". These arguments are defined in GenerationConfig However, these arguments are not used during training, so that the model can learn them on its own.
We also disable the use_cache function in the Whisper decoder. It allows us to reuse the key and calculated values of the self-attention and cross-attention blocks to speed up the current decoding stage. However, it is incompatible with gradient checkpointing which will be applied in a later step to reduce the memory footprint.
Loading datasets
We'll use the 🤗 Datasets library to download and prepare the datasets. We mix training data from Common Voice 11.0 and Multilingual LibriSpeech to form a larger training set, and we only use Common Voice 11.0 test data for evaluation.
It is always recommended to collect as much training data as possible. There are other speech recognition datasets available on the Hugging Face Hub, such as Voxpopuli and Flowers. If you'd like to load your local corpus, take a look at this page.
The audio signal sampling rate is 48kHz in Common Voice and 16kHz in Multilingual LibriSpeech. We ensure that audio samples are resampled to 16kHz, not only to unify the sampling rate of different data sets, but also because this is the sampling rate of 680,000 hours of Whisper model pre-training corpus. Resampling can easily be carried out on the fly using the "cast_column method and the "Audio object.
In order to mix different datasets, it is also necessary to ensure that all datasets have the same data fields. Here, we retain only the audio column and the phrase from two datasets.
If you have disk space constraints, you can load data sets on the fly with the streaming mode.
Data enhancement
We have found that the audio samples in the LibriSpeech Multilingual dataset are quite intelligible. To ensure that the model is robust in noisy environments and can be generalized to different speakers, we augment the data using the library Audiomentations library. Several augmentations are applied to the audio samples, including TimeStretch,Gain,PitchShiftand one of the AddBackgroundNoiseor AddGaussianNoise options.
This method of increase is carried out as follows:
Next, we apply the augmentation to all learning examples using the "map" method:
Note: Data augmentation is only performed on the training set. We also keep an original version of the training set, then compose it with the augmented training set.
Text standardization
"While diversity in audio quality can help a model be robust, diversity in transcription quality is not as beneficial."
The diversity here is reflected in the transcription format, i.e. case and punctuation exist in the Common Voice dataset, but not in the Multilingual LibriSpeech dataset. We need to make sure that the transcriptions are in lower case and remove any punctuation when we use them together. This will simplify the task - since the model no longer needs to distinguish between upper and lower case characters, or to predict punctuation marks between characters.
However, if you wish to have transcriptions that are easy to read or require case or punctuation, it is preferable to keep them and only use case and punctuation data sets such as Common VoiceandFlowers.
Note: The model is always evaluated on standardized transcriptions, i.e. without capital letters and without punctuation.
You can find the standardization used for English and other languages in Appendix C of the Whisper article.
Data pre-processing
You can find the standardization used for English and other languages in Appendix C of the the article Whisper.
As we showed in the introduction, the Whisper model takes the log-Mel spectrogram as input and produces BPE tokens. We therefore need to prepare our data in the appropriate format. This can be achieved by two utility classes; WhisperFeatureExtractor and WhisperTokenizer, used respectively on audio inputs and transcriptions or model predictions. The transformer library groups these two classes into a single WhisperProcessor class, which can be loaded as shown below:
All we need to do is specify the target language and task, and WhisperTokenizer will prefix the corresponding language and task tokens when encoding transcriptions into label IDs.
Let's see what's in our data preparation function:
Next, we apply the data preparation function to all the examples in the dataset, using the "map" method:
Deleting long audio
In the previous step, examples with audio longer than 30s were truncated by the WhisperFeatureExtractor. If the audio is truncated, the transcription is not, which seriously destabilizes the learning process. Here, we define a function to filter all audio longer than 30s:
We then apply our filter function to all the examples using the "filter" method:
Deleting long texts
The Whisper decoder uses a learned position representation with a maximum length of 448 tokens. It can therefore not decode a transcript of more than 448 token identifiers. Here, we define a filter function on the label identifiers:
Then apply it to all examples using the "filter" method:
Learning and assessment
Data collator
The data collator takes a list of pre-processed samples and assembles them into a batch of Pytorch tensors. We need to make sure that all the audio features in the batch have the same length, and this rule also applies to all the labels in the batch.
Audio features are already padded or truncated to a fixed dimension by the "WhisperFeatureExtractor", so we only need to convert them to Pytorch tensors using the "pad" method. On the other hand, label identifiers are not padded. We must first paginate them to the maximum batch length using the "pad" method, then replace the pagination tokens with "-100" so that these tokens are not taken into account in the loss calculation.
Let's define our data collator as follows:
Next, we can initialize the data collator we have just defined:
Evaluation measures
Nous utilisons la métrique du taux d’erreur de mots (WER) pour évaluer les performances du modèle. La métrique WER peut être simplement chargée par 🤗 Evaluate :
We then need to define a function that takes the actual token ids and model predictions, then returns the WER metric. In this function, we need to replace "-100" with the "pad_token_id" (overriding the step in the data collator to ignore padded tokens) so that label ids can be properly de-tokenized into strings.
Learning configuration
Dans cette étape, nous définissons tous les paramètres liés à l’apprentissage. Pour plus de détails sur les autres arguments d’apprentissage, reportez-vous à la documentation « Seq2SeqTrainingArguments » .
Learning
In the last step, we initialize the trainer by passing the model, dataset, data collator, training arguments and metrics calculation function as arguments.
Let's start learning!
Don't forget to save your model and processor once you've finished training:
Let's recap!
Dans ce blog, nous avons présenté un guide étape par étape sur le fine-tuning de Whisper pour ASR sur des données françaises. Le WER de la version Whisper medium a été réduit de 16.00% à 9.03% sur Common Voice. Avec la version Whisper large , il est passé de 13.90% à 8.15%.
Vous pouvez trouver ici une démo pour l’ASR français utilisant des modèles de Whisper fine-tunés.
You can also fine-tune Whisper in other languages: simply collect and clean datasets in that language, then specify the corresponding language code when loading "WhisperProcessor".
References
- Robust Speech Recognition via Large-Scale Weak Supervision
- Fine-Tune Whisper For Multilingual ASR with 🤗 Transformers
- Whisper Fine-Tuning Event
Finally, I would like to thank my colleagues at Zaion Labfor their invaluable help and constructive comments: Mohamed Bouaziz, Tiphaine Fievet,Imed Laaridh,Lorraine Vanel,Yingzhi Wang andAlya Yacoubi!