{ "cells": [ { "cell_type": "markdown", "id": "75b58048-7d14-4fc6-8085-1fc08c81b4a6", "metadata": { "id": "75b58048-7d14-4fc6-8085-1fc08c81b4a6" }, "source": [ "# Fine-Tune Whisper For Multilingual ASR with πŸ€— Transformers" ] }, { "cell_type": "markdown", "id": "fbfa8ad5-4cdc-4512-9058-836cbbf65e1a", "metadata": { "id": "fbfa8ad5-4cdc-4512-9058-836cbbf65e1a" }, "source": [ "In this Colab, we present a step-by-step guide on how to fine-tune Whisper \n", "for any multilingual ASR dataset using Hugging Face πŸ€— Transformers. This is a \n", "more \"hands-on\" version of the accompanying [blog post](https://huggingface.co/blog/fine-tune-whisper). \n", "For a more in-depth explanation of Whisper, the Common Voice dataset and the theory behind fine-tuning, the reader is advised to refer to the blog post." ] }, { "cell_type": "markdown", "id": "afe0d503-ae4e-4aa7-9af4-dbcba52db41e", "metadata": { "id": "afe0d503-ae4e-4aa7-9af4-dbcba52db41e" }, "source": [ "## Introduction" ] }, { "cell_type": "markdown", "id": "9ae91ed4-9c3e-4ade-938e-f4c2dcfbfdc0", "metadata": { "id": "9ae91ed4-9c3e-4ade-938e-f4c2dcfbfdc0" }, "source": [ "Whisper is a pre-trained model for automatic speech recognition (ASR) \n", "published in [September 2022](https://openai.com/blog/whisper/) by the authors \n", "Alec Radford et al. from OpenAI. Unlike many of its predecessors, such as \n", "[Wav2Vec 2.0](https://arxiv.org/abs/2006.11477), which are pre-trained \n", "on un-labelled audio data, Whisper is pre-trained on a vast quantity of \n", "**labelled** audio-transcription data, 680,000 hours to be precise. \n", "This is an order of magnitude more data than the un-labelled audio data used \n", "to train Wav2Vec 2.0 (60,000 hours). What is more, 117,000 hours of this \n", "pre-training data is multilingual ASR data. This results in checkpoints \n", "that can be applied to over 96 languages, many of which are considered \n", "_low-resource_.\n", "\n", "When scaled to 680,000 hours of labelled pre-training data, Whisper models \n", "demonstrate a strong ability to generalise to many datasets and domains.\n", "The pre-trained checkpoints achieve competitive results to state-of-the-art \n", "ASR systems, with near 3% word error rate (WER) on the test-clean subset of \n", "LibriSpeech ASR and a new state-of-the-art on TED-LIUM with 4.7% WER (_c.f._ \n", "Table 8 of the [Whisper paper](https://cdn.openai.com/papers/whisper.pdf)).\n", "The extensive multilingual ASR knowledge acquired by Whisper during pre-training \n", "can be leveraged for other low-resource languages; through fine-tuning, the \n", "pre-trained checkpoints can be adapted for specific datasets and languages \n", "to further improve upon these results. We'll show just how Whisper can be fine-tuned \n", "for low-resource languages in this Colab." ] }, { "cell_type": "markdown", "id": "e59b91d6-be24-4b5e-bb38-4977ea143a72", "metadata": { "id": "e59b91d6-be24-4b5e-bb38-4977ea143a72" }, "source": [ "
\n", "\"Trulli\"\n", "
Figure 1: Whisper model. The architecture \n", "follows the standard Transformer-based encoder-decoder model. A \n", "log-Mel spectrogram is input to the encoder. The last encoder \n", "hidden states are input to the decoder via cross-attention mechanisms. The \n", "decoder autoregressively predicts text tokens, jointly conditional on the \n", "encoder hidden states and previously predicted tokens. Figure source: \n", "OpenAI Whisper Blog.
\n", "
" ] }, { "cell_type": "markdown", "id": "21b6316e-8a55-4549-a154-66d3da2ab74a", "metadata": { "id": "21b6316e-8a55-4549-a154-66d3da2ab74a" }, "source": [ "The Whisper checkpoints come in five configurations of varying model sizes.\n", "The smallest four are trained on either English-only or multilingual data.\n", "The largest checkpoint is multilingual only. All nine of the pre-trained checkpoints \n", "are available on the [Hugging Face Hub](https://huggingface.co/models?search=openai/whisper). The \n", "checkpoints are summarised in the following table with links to the models on the Hub:\n", "\n", "| Size | Layers | Width | Heads | Parameters | English-only | Multilingual |\n", "|--------|--------|-------|-------|------------|------------------------------------------------------|---------------------------------------------------|\n", "| tiny | 4 | 384 | 6 | 39 M | [βœ“](https://huggingface.co/openai/whisper-tiny.en) | [βœ“](https://huggingface.co/openai/whisper-tiny.) |\n", "| base | 6 | 512 | 8 | 74 M | [βœ“](https://huggingface.co/openai/whisper-base.en) | [βœ“](https://huggingface.co/openai/whisper-base) |\n", "| small | 12 | 768 | 12 | 244 M | [βœ“](https://huggingface.co/openai/whisper-small.en) | [βœ“](https://huggingface.co/openai/whisper-small) |\n", "| medium | 24 | 1024 | 16 | 769 M | [βœ“](https://huggingface.co/openai/whisper-medium.en) | [βœ“](https://huggingface.co/openai/whisper-medium) |\n", "| large | 32 | 1280 | 20 | 1550 M | x | [βœ“](https://huggingface.co/openai/whisper-large) |\n", "\n", "For demonstration purposes, we'll fine-tune the multilingual version of the \n", "[`\"small\"`](https://huggingface.co/openai/whisper-small) checkpoint with 244M params (~= 1GB). \n", "As for our data, we'll train and evaluate our system on a low-resource language \n", "taken from the [Common Voice](https://huggingface.co/datasets/mozilla-foundation/fleurs_11_0)\n", "dataset. We'll show that with as little as 8 hours of fine-tuning data, we can achieve \n", "strong performance in this language." ] }, { "cell_type": "markdown", "id": "3a680dfc-cbba-4f6c-8a1f-e1a5ff3f123a", "metadata": { "id": "3a680dfc-cbba-4f6c-8a1f-e1a5ff3f123a" }, "source": [ "------------------------------------------------------------------------\n", "\n", "\\\\({}^1\\\\) The name Whisper follows from the acronym β€œWSPSR”, which stands for β€œWeb-scale Supervised Pre-training for Speech Recognition”." ] }, { "cell_type": "markdown", "id": "b219c9dd-39b6-4a95-b2a1-3f547a1e7bc0", "metadata": { "id": "b219c9dd-39b6-4a95-b2a1-3f547a1e7bc0" }, "source": [ "## Load Dataset\n", "Loading MS-MY Dataset from FLEURS.\n", "Combine train and validation set." ] }, { "cell_type": "code", "execution_count": 3, "id": "18406a25", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Token is valid.\n", "Your token has been saved in your configured git credential helpers (store).\n", "Your token has been saved to /home/ubuntu/.huggingface/token\n", "Login successful\n" ] } ], "source": [ "from huggingface_hub import notebook_login\n", "\n", "notebook_login()" ] }, { "cell_type": "code", "execution_count": 4, "id": "a2787582-554f-44ce-9f38-4180a5ed6b44", "metadata": { "id": "a2787582-554f-44ce-9f38-4180a5ed6b44" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Downloading builder script: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 12.8k/12.8k [00:00<00:00, 18.2MB/s]\n", "Downloading readme: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 11.2k/11.2k [00:00<00:00, 13.4MB/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Downloading and preparing dataset fleurs/ms_my to /home/ubuntu/.cache/huggingface/datasets/google___fleurs/ms_my/2.0.0/aabb39fb29739c495517ac904e2886819b6e344702f0a5b5283cb178b087c94a...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Downloading data: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 64.8M/64.8M [00:00<00:00, 83.1MB/s]\n", "Downloading data files: 0%| | 0/1 [00:00\n", "\"Trulli\"\n", "
Figure 2: Conversion of sampled audio array to log-Mel spectrogram.\n", "Left: sampled 1-dimensional audio signal. Right: corresponding log-Mel spectrogram. Figure source:\n", "Google SpecAugment Blog.\n", "
" ] }, { "cell_type": "markdown", "id": "b2ef54d5-b946-4c1d-9fdc-adc5d01b46aa", "metadata": { "id": "b2ef54d5-b946-4c1d-9fdc-adc5d01b46aa" }, "source": [ "We'll load the feature extractor from the pre-trained checkpoint with the default values:" ] }, { "cell_type": "code", "execution_count": 6, "id": "bc77d7bb-f9e2-47f5-b663-30f7a4321ce5", "metadata": { "id": "bc77d7bb-f9e2-47f5-b663-30f7a4321ce5" }, "outputs": [], "source": [ "from transformers import WhisperFeatureExtractor\n", "\n", "feature_extractor = WhisperFeatureExtractor.from_pretrained(\"openai/whisper-medium\")" ] }, { "cell_type": "markdown", "id": "93748af7-b917-4ecf-a0c8-7d89077ff9cb", "metadata": { "id": "93748af7-b917-4ecf-a0c8-7d89077ff9cb" }, "source": [ "### Load WhisperTokenizer" ] }, { "cell_type": "markdown", "id": "2bc82609-a9fb-447a-a2af-99597c864029", "metadata": { "id": "2bc82609-a9fb-447a-a2af-99597c864029" }, "source": [ "The Whisper model outputs a sequence of _token ids_. The tokenizer maps each of these token ids to their corresponding text string. For Hindi, we can load the pre-trained tokenizer and use it for fine-tuning without any further modifications. We simply have to \n", "specify the target language and the task. These arguments inform the \n", "tokenizer to prefix the language and task tokens to the start of encoded \n", "label sequences:" ] }, { "cell_type": "code", "execution_count": 7, "id": "c7b07f9b-ae0e-4f89-98f0-0c50d432eab6", "metadata": { "id": "c7b07f9b-ae0e-4f89-98f0-0c50d432eab6", "outputId": "5c004b44-86e7-4e00-88be-39e0af5eed69" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Downloading: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 830/830 [00:00<00:00, 1.18MB/s]\n", "Downloading: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1.04M/1.04M [00:00<00:00, 76.4MB/s]\n", "Downloading: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 494k/494k [00:00<00:00, 65.8MB/s]\n", "Downloading: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 52.7k/52.7k [00:00<00:00, 29.9MB/s]\n", "Downloading: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 2.11k/2.11k [00:00<00:00, 3.93MB/s]\n", "Downloading: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 2.06k/2.06k [00:00<00:00, 3.75MB/s]\n" ] } ], "source": [ "from transformers import WhisperTokenizer\n", "\n", "tokenizer = WhisperTokenizer.from_pretrained(\"openai/whisper-medium\", language=\"Malay\", task=\"transcribe\")" ] }, { "cell_type": "markdown", "id": "d2ef23f3-f4a8-483a-a2dc-080a7496cb1b", "metadata": { "id": "d2ef23f3-f4a8-483a-a2dc-080a7496cb1b" }, "source": [ "### Combine To Create A WhisperProcessor" ] }, { "cell_type": "markdown", "id": "5ff67654-5a29-4bb8-a69d-0228946c6f8d", "metadata": { "id": "5ff67654-5a29-4bb8-a69d-0228946c6f8d" }, "source": [ "To simplify using the feature extractor and tokenizer, we can _wrap_ \n", "both into a single `WhisperProcessor` class. This processor object \n", "inherits from the `WhisperFeatureExtractor` and `WhisperProcessor`, \n", "and can be used on the audio inputs and model predictions as required. \n", "In doing so, we only need to keep track of two objects during training: \n", "the `processor` and the `model`:" ] }, { "cell_type": "code", "execution_count": 8, "id": "77d9f0c5-8607-4642-a8ac-c3ab2e223ea6", "metadata": { "id": "77d9f0c5-8607-4642-a8ac-c3ab2e223ea6" }, "outputs": [], "source": [ "from transformers import WhisperProcessor\n", "\n", "processor = WhisperProcessor.from_pretrained(\"openai/whisper-medium\", language=\"Malay\", task=\"transcribe\")" ] }, { "cell_type": "markdown", "id": "381acd09-0b0f-4d04-9eb3-f028ac0e5f2c", "metadata": { "id": "381acd09-0b0f-4d04-9eb3-f028ac0e5f2c" }, "source": [ "### Prepare Data" ] }, { "cell_type": "markdown", "id": "9649bf01-2e8a-45e5-8fca-441c13637b8f", "metadata": { "id": "9649bf01-2e8a-45e5-8fca-441c13637b8f" }, "source": [ "Let's print the first example of the Common Voice dataset to see \n", "what form the data is in:" ] }, { "cell_type": "code", "execution_count": 9, "id": "6e6b0ec5-0c94-4e2c-ae24-c791be1b2255", "metadata": { "id": "6e6b0ec5-0c94-4e2c-ae24-c791be1b2255" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'audio': {'path': '14466102005240085063.wav', 'array': array([0. , 0. , 0. , ..., 0.0003528 , 0.00013924,\n", " 0.00014025]), 'sampling_rate': 16000}, 'transcription': 'apabila anda telah selesa dengan pemformatan dan penyuntingan di web kemudian anda mungkin membina laman web sendiri'}\n" ] } ], "source": [ "print(fleurs[\"train\"][0])" ] }, { "cell_type": "markdown", "id": "5a679f05-063d-41b3-9b58-4fc9c6ccf4fd", "metadata": { "id": "5a679f05-063d-41b3-9b58-4fc9c6ccf4fd" }, "source": [ "Since \n", "our input audio is sampled at 48kHz, we need to _downsample_ it to \n", "16kHz prior to passing it to the Whisper feature extractor, 16kHz being the sampling rate expected by the Whisper model. \n", "\n", "We'll set the audio inputs to the correct sampling rate using dataset's \n", "[`cast_column`](https://huggingface.co/docs/datasets/package_reference/main_classes.html?highlight=cast_column#datasets.DatasetDict.cast_column)\n", "method. This operation does not change the audio in-place, \n", "but rather signals to `datasets` to resample audio samples _on the fly_ the \n", "first time that they are loaded:" ] }, { "cell_type": "code", "execution_count": 10, "id": "f12e2e57-156f-417b-8cfb-69221cc198e8", "metadata": { "id": "f12e2e57-156f-417b-8cfb-69221cc198e8" }, "outputs": [], "source": [ "from datasets import Audio\n", "\n", "fleurs = fleurs.cast_column(\"audio\", Audio(sampling_rate=16000))" ] }, { "cell_type": "markdown", "id": "00382a3e-abec-4cdd-a54c-d1aaa3ea4707", "metadata": { "id": "00382a3e-abec-4cdd-a54c-d1aaa3ea4707" }, "source": [ "Re-loading the first audio sample in the Common Voice dataset will resample \n", "it to the desired sampling rate:" ] }, { "cell_type": "code", "execution_count": 11, "id": "87122d71-289a-466a-afcf-fa354b18946b", "metadata": { "id": "87122d71-289a-466a-afcf-fa354b18946b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'audio': {'path': '14466102005240085063.wav', 'array': array([0. , 0. , 0. , ..., 0.0003528 , 0.00013924,\n", " 0.00014025]), 'sampling_rate': 16000}, 'transcription': 'apabila anda telah selesa dengan pemformatan dan penyuntingan di web kemudian anda mungkin membina laman web sendiri'}\n" ] } ], "source": [ "print(fleurs[\"train\"][0])" ] }, { "cell_type": "markdown", "id": "3df7378a-a4c0-45d7-8d07-defbd1062ab6", "metadata": {}, "source": [ "We'll define our pre-processing strategy. We advise that you **do not** lower-case the transcriptions or remove punctuation unless mixing different datasets. This will enable you to fine-tune Whisper models that can predict punctuation and casing. Later, you will see how we can evaluate the predictions without punctuation or casing, so that the models benefit from the WER improvement obtained by normalising the transcriptions while still predicting fully formatted transcriptions." ] }, { "cell_type": "code", "execution_count": 12, "id": "d041650e-1c48-4439-87b3-5b6f4a514107", "metadata": {}, "outputs": [], "source": [ "from transformers.models.whisper.english_normalizer import BasicTextNormalizer\n", "\n", "do_lower_case = False\n", "do_remove_punctuation = False\n", "\n", "normalizer = BasicTextNormalizer()" ] }, { "cell_type": "markdown", "id": "89e12c2e-2f14-479b-987b-f0c75c881095", "metadata": {}, "source": [ "Now we can write a function to prepare our data ready for the model:\n", "1. We load and resample the audio data by calling `batch[\"audio\"]`. As explained above, πŸ€— Datasets performs any necessary resampling operations on the fly.\n", "2. We use the feature extractor to compute the log-Mel spectrogram input features from our 1-dimensional audio array.\n", "3. We perform any optional pre-processing (lower-case or remove punctuation).\n", "4. We encode the transcriptions to label ids through the use of the tokenizer." ] }, { "cell_type": "code", "execution_count": 13, "id": "c085911c-a10a-41ef-8874-306e0503e9bb", "metadata": {}, "outputs": [], "source": [ "def prepare_dataset(batch):\n", " # load and (possibly) resample audio data to 16kHz\n", " audio = batch[\"audio\"]\n", "\n", " # compute log-Mel input features from input audio array \n", " batch[\"input_features\"] = processor.feature_extractor(audio[\"array\"], sampling_rate=audio[\"sampling_rate\"]).input_features[0]\n", " # compute input length of audio sample in seconds\n", " batch[\"input_length\"] = len(audio[\"array\"]) / audio[\"sampling_rate\"]\n", " \n", " # optional pre-processing steps\n", " transcription = batch[\"transcription\"]\n", " if do_lower_case:\n", " transcription = transcription.lower()\n", " if do_remove_punctuation:\n", " transcription = normalizer(transcription).strip()\n", " \n", " # encode target text to label ids\n", " batch[\"labels\"] = processor.tokenizer(transcription).input_ids\n", " return batch" ] }, { "cell_type": "markdown", "id": "8c960965-9fb6-466f-9dbd-c9d43e71d9d0", "metadata": { "id": "70b319fb-2439-4ef6-a70d-a47bf41c4a13" }, "source": [ "We can apply the data preparation function to all of our training examples using dataset's `.map` method. The argument `num_proc` specifies how many CPU cores to use. Setting `num_proc` > 1 will enable multiprocessing. If the `.map` method hangs with multiprocessing, set `num_proc=1` and process the dataset sequentially." ] }, { "cell_type": "code", "execution_count": 14, "id": "7b73ab39-ffaf-4b9e-86e5-782963c6134b", "metadata": { "id": "7b73ab39-ffaf-4b9e-86e5-782963c6134b" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "#0: 0%| | 0/1496 [00:00 Dict[str, torch.Tensor]:\n", " # split inputs and labels since they have to be of different lengths and need different padding methods\n", " # first treat the audio inputs by simply returning torch tensors\n", " input_features = [{\"input_features\": feature[\"input_features\"]} for feature in features]\n", " batch = self.processor.feature_extractor.pad(input_features, return_tensors=\"pt\")\n", "\n", " # get the tokenized label sequences\n", " label_features = [{\"input_ids\": feature[\"labels\"]} for feature in features]\n", " # pad the labels to max length\n", " labels_batch = self.processor.tokenizer.pad(label_features, return_tensors=\"pt\")\n", "\n", " # replace padding with -100 to ignore loss correctly\n", " labels = labels_batch[\"input_ids\"].masked_fill(labels_batch.attention_mask.ne(1), -100)\n", "\n", " # if bos token is appended in previous tokenization step,\n", " # cut bos token here as it's append later anyways\n", " if (labels[:, 0] == self.processor.tokenizer.bos_token_id).all().cpu().item():\n", " labels = labels[:, 1:]\n", "\n", " batch[\"labels\"] = labels\n", "\n", " return batch" ] }, { "cell_type": "markdown", "id": "3cae7dbf-8a50-456e-a3a8-7fd005390f86", "metadata": { "id": "3cae7dbf-8a50-456e-a3a8-7fd005390f86" }, "source": [ "Let's initialise the data collator we've just defined:" ] }, { "cell_type": "code", "execution_count": 18, "id": "fc834702-c0d3-4a96-b101-7b87be32bf42", "metadata": { "id": "fc834702-c0d3-4a96-b101-7b87be32bf42" }, "outputs": [], "source": [ "data_collator = DataCollatorSpeechSeq2SeqWithPadding(processor=processor)" ] }, { "cell_type": "markdown", "id": "d62bb2ab-750a-45e7-82e9-61d6f4805698", "metadata": { "id": "d62bb2ab-750a-45e7-82e9-61d6f4805698" }, "source": [ "### Evaluation Metrics" ] }, { "cell_type": "markdown", "id": "66fee1a7-a44c-461e-b047-c3917221572e", "metadata": { "id": "66fee1a7-a44c-461e-b047-c3917221572e" }, "source": [ "We'll use the word error rate (WER) metric, the 'de-facto' metric for assessing \n", "ASR systems. For more information, refer to the WER [docs](https://huggingface.co/metrics/wer). We'll load the WER metric from πŸ€— Evaluate:" ] }, { "cell_type": "code", "execution_count": 19, "id": "b22b4011-f31f-4b57-b684-c52332f92890", "metadata": { "id": "b22b4011-f31f-4b57-b684-c52332f92890" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Downloading builder script: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 4.49k/4.49k [00:00<00:00, 7.77MB/s]\n" ] } ], "source": [ "import evaluate\n", "\n", "metric = evaluate.load(\"wer\")" ] }, { "cell_type": "markdown", "id": "4f32cab6-31f0-4cb9-af4c-40ba0f5fc508", "metadata": { "id": "4f32cab6-31f0-4cb9-af4c-40ba0f5fc508" }, "source": [ "We then simply have to define a function that takes our model \n", "predictions and returns the WER metric. This function, called\n", "`compute_metrics`, first replaces `-100` with the `pad_token_id`\n", "in the `label_ids` (undoing the step we applied in the \n", "data collator to ignore padded tokens correctly in the loss).\n", "It then decodes the predicted and label ids to strings. Finally,\n", "it computes the WER between the predictions and reference labels. \n", "Here, we have the option of evaluating with the 'normalised' transcriptions \n", "and predictions. We recommend you set this to `True` to benefit from the WER \n", "improvement obtained by normalising the transcriptions." ] }, { "cell_type": "code", "execution_count": 23, "id": "23959a70-22d0-4ffe-9fa1-72b61e75bb52", "metadata": { "id": "23959a70-22d0-4ffe-9fa1-72b61e75bb52" }, "outputs": [], "source": [ "#Β evaluate with the 'normalised' WER\n", "do_normalize_eval = True\n", "\n", "def compute_metrics(pred):\n", " pred_ids = pred.predictions\n", " label_ids = pred.label_ids\n", "\n", " # replace -100 with the pad_token_id\n", " label_ids[label_ids == -100] = processor.tokenizer.pad_token_id\n", "\n", " # we do not want to group tokens when computing the metrics\n", " pred_str = processor.tokenizer.batch_decode(pred_ids, skip_special_tokens=True)\n", " label_str = processor.tokenizer.batch_decode(label_ids, skip_special_tokens=True)\n", "\n", " if do_normalize_eval:\n", " pred_str = [normalizer(pred) for pred in pred_str]\n", " label_str = [normalizer(label) for label in label_str]\n", "\n", " wer = 100 * metric.compute(predictions=pred_str, references=label_str)\n", "\n", " return {\"wer\": wer}" ] }, { "cell_type": "markdown", "id": "daf2a825-6d9f-4a23-b145-c37c0039075b", "metadata": { "id": "daf2a825-6d9f-4a23-b145-c37c0039075b" }, "source": [ "###Β Load a Pre-Trained Checkpoint" ] }, { "cell_type": "markdown", "id": "437a97fa-4864-476b-8abc-f28b8166cfa5", "metadata": { "id": "437a97fa-4864-476b-8abc-f28b8166cfa5" }, "source": [ "Now let's load the pre-trained Whisper `small` checkpoint. Again, this \n", "is trivial through use of πŸ€— Transformers!" ] }, { "cell_type": "code", "execution_count": 24, "id": "5a10cc4b-07ec-4ebd-ac1d-7c601023594f", "metadata": { "id": "5a10cc4b-07ec-4ebd-ac1d-7c601023594f" }, "outputs": [], "source": [ "from transformers import WhisperForConditionalGeneration\n", "\n", "model = WhisperForConditionalGeneration.from_pretrained(\"openai/whisper-medium\")" ] }, { "cell_type": "markdown", "id": "a15ead5f-2277-4a39-937b-585c2497b2df", "metadata": { "id": "a15ead5f-2277-4a39-937b-585c2497b2df" }, "source": [ "Override generation arguments - no tokens are forced as decoder outputs (see [`forced_decoder_ids`](https://huggingface.co/docs/transformers/main_classes/text_generation#transformers.generation_utils.GenerationMixin.generate.forced_decoder_ids)), no tokens are suppressed during generation (see [`suppress_tokens`](https://huggingface.co/docs/transformers/main_classes/text_generation#transformers.generation_utils.GenerationMixin.generate.suppress_tokens)). Set `use_cache` to False since we're using gradient checkpointing, and the two are incompatible:" ] }, { "cell_type": "code", "execution_count": 26, "id": "62038ba3-88ed-4fce-84db-338f50dcd04f", "metadata": { "id": "62038ba3-88ed-4fce-84db-338f50dcd04f" }, "outputs": [], "source": [ "model.config.forced_decoder_ids = None\n", "model.config.suppress_tokens = []\n", "model.config.use_cache = False" ] }, { "cell_type": "markdown", "id": "2178dea4-80ca-47b6-b6ea-ba1915c90c06", "metadata": { "id": "2178dea4-80ca-47b6-b6ea-ba1915c90c06" }, "source": [ "### Define the Training Configuration" ] }, { "cell_type": "markdown", "id": "c21af1e9-0188-4134-ac82-defc7bdcc436", "metadata": { "id": "c21af1e9-0188-4134-ac82-defc7bdcc436" }, "source": [ "In the final step, we define all the parameters related to training. For more detail on the training arguments, refer to the Seq2SeqTrainingArguments [docs](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.Seq2SeqTrainingArguments)." ] }, { "cell_type": "code", "execution_count": 27, "id": "0ae3e9af-97b7-4aa0-ae85-20b23b5bcb3a", "metadata": { "id": "0ae3e9af-97b7-4aa0-ae85-20b23b5bcb3a" }, "outputs": [], "source": [ "from transformers import Seq2SeqTrainingArguments\n", "\n", "training_args = Seq2SeqTrainingArguments(\n", " output_dir=\"./\",\n", " per_device_train_batch_size=32,\n", " gradient_accumulation_steps=1, # increase by 2x for every 2x decrease in batch size\n", " learning_rate=1e-5,\n", " warmup_steps=500,\n", " max_steps=5000,\n", " gradient_checkpointing=True,\n", " fp16=True,\n", " evaluation_strategy=\"steps\",\n", " per_device_eval_batch_size=16,\n", " predict_with_generate=True,\n", " generation_max_length=225,\n", " save_steps=1000,\n", " eval_steps=1000,\n", " logging_steps=25,\n", " report_to=[\"tensorboard\"],\n", " load_best_model_at_end=True,\n", " metric_for_best_model=\"wer\",\n", " greater_is_better=False,\n", " push_to_hub=True,\n", ")" ] }, { "cell_type": "markdown", "id": "b3a944d8-3112-4552-82a0-be25988b3857", "metadata": { "id": "b3a944d8-3112-4552-82a0-be25988b3857" }, "source": [ "**Note**: if one does not want to upload the model checkpoints to the Hub, \n", "set `push_to_hub=False`." ] }, { "cell_type": "markdown", "id": "bac29114-d226-4f54-97cf-8718c9f94e1e", "metadata": { "id": "bac29114-d226-4f54-97cf-8718c9f94e1e" }, "source": [ "We can forward the training arguments to the πŸ€— Trainer along with our model,\n", "dataset, data collator and `compute_metrics` function:" ] }, { "cell_type": "code", "execution_count": 28, "id": "d546d7fe-0543-479a-b708-2ebabec19493", "metadata": { "id": "d546d7fe-0543-479a-b708-2ebabec19493" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/ubuntu/whisper-medium-ms/./ is already a clone of https://huggingface.co/Scrya/whisper-medium-ms. Make sure you pull the latest changes with `repo.git_pull()`.\n", "max_steps is given, it will override any value given in num_train_epochs\n", "Using cuda_amp half precision backend\n" ] } ], "source": [ "from transformers import Seq2SeqTrainer\n", "\n", "trainer = Seq2SeqTrainer(\n", " args=training_args,\n", " model=model,\n", " train_dataset=fleurs[\"train\"],\n", " eval_dataset=fleurs[\"test\"],\n", " data_collator=data_collator,\n", " compute_metrics=compute_metrics,\n", " tokenizer=processor.feature_extractor,\n", ")" ] }, { "cell_type": "markdown", "id": "uOrRhDGtN5S4", "metadata": { "id": "uOrRhDGtN5S4" }, "source": [ "We'll save the processor object once before starting training. Since the processor is not trainable, it won't change over the course of training:" ] }, { "cell_type": "code", "execution_count": 29, "id": "-2zQwMfEOBJq", "metadata": { "id": "-2zQwMfEOBJq" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Feature extractor saved in ./preprocessor_config.json\n", "tokenizer config file saved in ./tokenizer_config.json\n", "Special tokens file saved in ./special_tokens_map.json\n", "added tokens file saved in ./added_tokens.json\n" ] } ], "source": [ "processor.save_pretrained(training_args.output_dir)" ] }, { "cell_type": "markdown", "id": "7f404cf9-4345-468c-8196-4bd101d9bd51", "metadata": { "id": "7f404cf9-4345-468c-8196-4bd101d9bd51" }, "source": [ "### Training" ] }, { "cell_type": "markdown", "id": "5e8b8d56-5a70-4f68-bd2e-f0752d0bd112", "metadata": { "id": "5e8b8d56-5a70-4f68-bd2e-f0752d0bd112" }, "source": [ "Training will take approximately 5-10 hours depending on your GPU. The peak GPU memory for the given training configuration is approximately 36GB. \n", "Depending on your GPU, it is possible that you will encounter a CUDA `\"out-of-memory\"` error when you launch training. \n", "In this case, you can reduce the `per_device_train_batch_size` incrementally by factors of 2 \n", "and employ [`gradient_accumulation_steps`](https://huggingface.co/docs/transformers/main_classes/trainer#transformers.Seq2SeqTrainingArguments.gradient_accumulation_steps)\n", "to compensate.\n", "\n", "To launch training, simply execute:" ] }, { "cell_type": "code", "execution_count": null, "id": "ee8b7b8e-1c9a-4d77-9137-1778a629e6de", "metadata": { "id": "ee8b7b8e-1c9a-4d77-9137-1778a629e6de", "scrolled": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "The following columns in the training set don't have a corresponding argument in `WhisperForConditionalGeneration.forward` and have been ignored: input_length. If input_length are not expected by `WhisperForConditionalGeneration.forward`, you can safely ignore this message.\n", "/home/ubuntu/hf_env/lib/python3.8/site-packages/transformers/optimization.py:306: FutureWarning: This implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this warning\n", " warnings.warn(\n", "***** Running training *****\n", " Num examples = 2985\n", " Num Epochs = 54\n", " Instantaneous batch size per device = 32\n", " Total train batch size (w. parallel, distributed & accumulation) = 32\n", " Gradient Accumulation steps = 1\n", " Total optimization steps = 5000\n", " Number of trainable parameters = 763857920\n" ] }, { "data": { "text/html": [ "\n", "
\n", " \n", " \n", " [1028/5000 2:10:54 < 8:26:47, 0.13 it/s, Epoch 10.93/54]\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
StepTraining LossValidation LossWer
10000.0024000.24378310.344360

" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "The following columns in the evaluation set don't have a corresponding argument in `WhisperForConditionalGeneration.forward` and have been ignored: input_length. If input_length are not expected by `WhisperForConditionalGeneration.forward`, you can safely ignore this message.\n", "***** Running Evaluation *****\n", " Num examples = 749\n", " Batch size = 16\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "/home/ubuntu/hf_env/lib/python3.8/site-packages/transformers/generation/utils.py:1134: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use a generation configuration file (see https://huggingface.co/docs/transformers/main_classes/text_generation)\n", " warnings.warn(\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Generate config GenerationConfig {\n", " \"begin_suppress_tokens\": [\n", " 220,\n", " 50257\n", " ],\n", " \"bos_token_id\": 50257,\n", " \"decoder_start_token_id\": 50258,\n", " \"eos_token_id\": 50257,\n", " \"max_length\": 448,\n", " \"pad_token_id\": 50257,\n", " \"suppress_tokens\": [],\n", " \"transformers_version\": \"4.26.0.dev0\",\n", " \"use_cache\": false\n", "}\n", "\n", "Saving model checkpoint to ./checkpoint-1000\n", "Configuration saved in ./checkpoint-1000/config.json\n", "Model weights saved in ./checkpoint-1000/pytorch_model.bin\n", "Feature extractor saved in ./checkpoint-1000/preprocessor_config.json\n", "Feature extractor saved in ./preprocessor_config.json\n" ] } ], "source": [ "trainer.train()" ] }, { "cell_type": "markdown", "id": "810ced54-7187-4a06-b2fe-ba6dcca94dc3", "metadata": { "id": "810ced54-7187-4a06-b2fe-ba6dcca94dc3" }, "source": [ "We can label our checkpoint with the `whisper-event` tag on push by setting the appropriate key-word arguments (kwargs):" ] }, { "cell_type": "code", "execution_count": null, "id": "c704f91e-241b-48c9-b8e0-f0da396a9663", "metadata": { "id": "c704f91e-241b-48c9-b8e0-f0da396a9663" }, "outputs": [], "source": [ "kwargs = {\n", " \"dataset_tags\": \"google/fleurs\",\n", " \"dataset\": \"FLEURS\", # a 'pretty' name for the training dataset\n", " \"language\": \"ms\",\n", " \"model_name\": \"Whisper Medium MS - FLEURS\", # a 'pretty' name for your model\n", " \"finetuned_from\": \"openai/whisper-medium\",\n", " \"tasks\": \"automatic-speech-recognition\",\n", " \"tags\": \"whisper-event\",\n", "}" ] }, { "cell_type": "markdown", "id": "090d676a-f944-4297-a938-a40eda0b2b68", "metadata": { "id": "090d676a-f944-4297-a938-a40eda0b2b68" }, "source": [ "The training results can now be uploaded to the Hub. To do so, execute the `push_to_hub` command and save the preprocessor object we created:" ] }, { "cell_type": "code", "execution_count": null, "id": "d7030622-caf7-4039-939b-6195cdaa2585", "metadata": { "id": "d7030622-caf7-4039-939b-6195cdaa2585" }, "outputs": [], "source": [ "trainer.push_to_hub(**kwargs)" ] }, { "cell_type": "markdown", "id": "ca743fbd-602c-48d4-ba8d-a2fe60af64ba", "metadata": { "id": "ca743fbd-602c-48d4-ba8d-a2fe60af64ba" }, "source": [ "## Closing Remarks" ] }, { "cell_type": "markdown", "id": "7f737783-2870-4e35-aa11-86a42d7d997a", "metadata": { "id": "7f737783-2870-4e35-aa11-86a42d7d997a" }, "source": [ "In this blog, we covered a step-by-step guide on fine-tuning Whisper for multilingual ASR \n", "using πŸ€— Datasets, Transformers and the Hugging Face Hub. For more details on the Whisper model, the Common Voice dataset and the theory behind fine-tuning, refere to the accompanying [blog post](https://huggingface.co/blog/fine-tune-whisper). If you're interested in fine-tuning other \n", "Transformers models, both for English and multilingual ASR, be sure to check out the \n", "examples scripts at [examples/pytorch/speech-recognition](https://github.com/huggingface/transformers/tree/main/examples/pytorch/speech-recognition)." ] } ], "metadata": { "colab": { "include_colab_link": true, "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }