Automatic speech recognition using Distil-Whisper and OpenVINO¶
This tutorial is also available as a Jupyter notebook that can be cloned directly from GitHub. See the installation guide for instructions to run this tutorial locally on Windows, Linux or macOS.
Distil-Whisper is a distilled variant of the Whisper model by OpenAI. The Distil-Whisper is proposed in the paper Robust Knowledge Distillation via Large-Scale Pseudo Labelling. According to authors, compared to Whisper, Distil-Whisper runs 6x faster with 50% fewer parameters, while performing to within 1% word error rate (WER) on out-of-distribution evaluation data.
Whisper is a Transformer based encoder-decoder model, also referred to as a sequence-to-sequence model. It maps a sequence of audio spectrogram features to a sequence of text tokens. First, the raw audio inputs are converted to a log-Mel spectrogram by action of the feature extractor. Then, the Transformer encoder encodes the spectrogram to form a sequence of encoder hidden states. Finally, the decoder autoregressively predicts text tokens, conditional on both the previous tokens and the encoder hidden states.
You can see the model architecture in the diagram below:
whisper_architecture.svg¶
In this tutorial, we consider how to run Distil-Whisper using OpenVINO.
We will use the pre-trained model from the Hugging Face
Transformers
library. To simplify the user experience, the Hugging Face
Optimum library is used to
convert the model to OpenVINO™ IR format. To further improve OpenVINO
Distil-Whisper model performance INT8
post-training quantization
from NNCF is applied.
Table of contents:
Prerequisites¶
%pip uninstall -q -y openvino-dev openvino openvino-nightly
%pip install -q openvino-nightly
%pip install -q "transformers" onnx datasets "git+https://github.com/eaidova/optimum-intel.git@ea/whisper" "gradio>=4.0" "librosa" "soundfile"
%pip install -q "nncf>=2.6.0" "jiwer"
Note: you may need to restart the kernel to use updated packages.
ERROR: tokenizers 0.14.1 has requirement huggingface_hub<0.18,>=0.16.4, but you'll have huggingface-hub 0.19.0 which is incompatible.
Note: you may need to restart the kernel to use updated packages.
Load PyTorch model¶
The AutoModelForSpeechSeq2Seq.from_pretrained
method is used for the
initialization of PyTorch Whisper model using the transformers library.
We will use the distil-whisper/distil-large-v2
model as an example
in this tutorial. The model will be downloaded once during first run and
this process may require some time. More details about this model can be
found in
model_card.
Preprocessing and post-processing are important in this model use.
AutoProcessor
class used for initialization WhisperProcessor
is
responsible for preparing audio input data for the model, converting it
to Mel-spectrogram and decoding predicted output token_ids into string
using tokenizer.
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
distil_model_id = "distil-whisper/distil-large-v2"
processor = AutoProcessor.from_pretrained(distil_model_id)
pt_distil_model = AutoModelForSpeechSeq2Seq.from_pretrained(distil_model_id)
pt_distil_model.eval();
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Prepare input sample¶
The processor expects audio data in numpy array format and information
about the audio sampling rate and returns the input_features
tensor
for making predictions. Conversion of audio to numpy format is handled
by Hugging Face datasets implementation.
from datasets import load_dataset
def extract_input_features(sample):
input_features = processor(
sample["audio"]["array"],
sampling_rate=sample["audio"]["sampling_rate"],
return_tensors="pt",
).input_features
return input_features
dataset = load_dataset(
"hf-internal-testing/librispeech_asr_dummy", "clean", split="validation"
)
sample = dataset[0]
input_features = extract_input_features(sample)
Run model inference¶
To perform speech recognition, one can use generate
interface of the
model. After generation is finished processor.batch_decode can be used
for decoding predicted token_ids into text transcription.
import IPython.display as ipd
predicted_ids = pt_distil_model.generate(input_features)
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
display(ipd.Audio(sample["audio"]["array"], rate=sample["audio"]["sampling_rate"]))
print(f"Reference: {sample['text']}")
print(f"Result: {transcription[0]}")
Reference: MISTER QUILTER IS THE APOSTLE OF THE MIDDLE CLASSES AND WE ARE GLAD TO WELCOME HIS GOSPEL
Result: Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.
Load OpenVINO model using Optimum library¶
The Hugging Face Optimum API is a high-level API that enables us to convert and quantize models from the Hugging Face Transformers library to the OpenVINO™ IR format. For more details, refer to the Hugging Face Optimum documentation.
Optimum Intel can be used to load optimized models from the Hugging
Face Hub and
create pipelines to run an inference with OpenVINO Runtime using Hugging
Face APIs. The Optimum Inference models are API compatible with Hugging
Face Transformers models. This means we just need to replace the
AutoModelForXxx
class with the corresponding OVModelForXxx
class.
Below is an example of the distil-whisper model
-from transformers import AutoModelForSpeechSeq2Seq
+from optimum.intel.openvino import OVModelForSpeechSeq2Seq
from transformers import AutoTokenizer, pipeline
model_id = "distil-whisper/distil-large-v2"
-model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id)
+model = OVModelForSpeechSeq2Seq.from_pretrained(model_id, export=True)
Model class initialization starts with calling the from_pretrained
method. When downloading and converting the Transformers model, the
parameter export=True
should be added. We can save the converted
model for the next usage with the save_pretrained
method. Tokenizers
and Processors are distributed with models also compatible with the
OpenVINO model. It means that we can reuse initialized early processor.
from pathlib import Path
from optimum.intel.openvino import OVModelForSpeechSeq2Seq
distil_model_path = Path(distil_model_id.split("/")[-1])
if not distil_model_path.exists():
ov_distil_model = OVModelForSpeechSeq2Seq.from_pretrained(
distil_model_id, export=True, compile=False
)
ov_distil_model.half()
ov_distil_model.save_pretrained(distil_model_path)
else:
ov_distil_model = OVModelForSpeechSeq2Seq.from_pretrained(
distil_model_path, compile=False
)
INFO:nncf:NNCF initialized successfully. Supported frameworks detected: torch, onnx, openvino
Select Inference device¶
import openvino as ov
import ipywidgets as widgets
core = ov.Core()
device = widgets.Dropdown(
options=core.available_devices + ["AUTO"],
value="AUTO",
description="Device:",
disabled=False,
)
device
Dropdown(description='Device:', index=4, options=('CPU', 'GPU.0', 'GPU.1', 'GPU.2', 'AUTO'), value='AUTO')
Compile OpenVINO model¶
ov_distil_model.to(device.value)
ov_distil_model.compile()
Compiling the encoder to AUTO ...
Compiling the decoder to AUTO ...
Compiling the decoder to AUTO ...
Run OpenVINO model inference¶
predicted_ids = ov_distil_model.generate(input_features)
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
display(ipd.Audio(sample["audio"]["array"], rate=sample["audio"]["sampling_rate"]))
print(f"Reference: {sample['text']}")
print(f"Result: {transcription[0]}")
/home/nsavel/venvs/ov_notebooks_tmp/lib/python3.8/site-packages/optimum/intel/openvino/modeling_seq2seq.py:457: FutureWarning: shared_memory is deprecated and will be removed in 2024.0. Value of shared_memory is going to override share_inputs value. Please use only share_inputs explicitly. last_hidden_state = torch.from_numpy(self.request(inputs, shared_memory=True)["last_hidden_state"]).to( /home/nsavel/venvs/ov_notebooks_tmp/lib/python3.8/site-packages/optimum/intel/openvino/modeling_seq2seq.py:538: FutureWarning: shared_memory is deprecated and will be removed in 2024.0. Value of shared_memory is going to override share_inputs value. Please use only share_inputs explicitly. self.request.start_async(inputs, shared_memory=True)