Using Hugging Face Transformers, Loading Automatic Speech Recognition Dataset

In this course, we will explain how to load and use automatic speech recognition (ASR) datasets using Hugging Face’s Transformers library. To put it simply, deep learning-based speech recognition technology has rapidly advanced in recent years, and the Hugging Face library provides tools to easily implement these technologies.

1. Introduction to Hugging Face Transformers

Hugging Face is well-known for its library that helps easily utilize various natural language processing (NLP) models. Recently, it has also supported speech recognition models, allowing researchers and developers to directly integrate speech recognition technology. The Transformers library provides transfer learning and various pre-trained models, which allow for quickly building high-performance models without complex algorithm implementations.

2. Overview of Automatic Speech Recognition (ASR)

Automatic Speech Recognition (ASR) is the process of converting speech to text. This process includes acoustic models, language models, and pronunciation models. Recent deep learning-based ASR systems demonstrate high accuracy in recognizing human speech by utilizing large amounts of speech datasets.

3. Securing Sufficient Datasets

**Hugging Face provides various datasets for ASR.** For instance, there are Common Voice, LibriSpeech, TED-LIUM, etc. These datasets can all be easily accessed from Hugging Face’s dataset hub, allowing you to load the necessary datasets directly.

4. Loading Datasets

Now, let’s look at an example of loading an automatic speech recognition dataset. First, we need to install the necessary packages. Below is the command to install the required packages:

pip install transformers datasets

4.1. Example of Loading a Dataset

Now, we will use the datasets library to load the Common Voice dataset. The code below is an example written in Python.


from datasets import load_dataset

# Load Common Voice dataset
dataset = load_dataset("common_voice", "en", split="train")

# Print the first few samples of the dataset
for i in range(5):
    print(dataset[i])

4.2. Code Explanation

In the above code, the load_dataset function provides an easy way to load various datasets offered by the Hugging Face datasets library. Here we are loading the English version of the Common Voice dataset. The loaded dataset is stored in the dataset variable, which can be used to train a speech recognition model.

4.3. Dataset Structure

The Common Voice dataset has several fields. Each sample typically consists of the following structure:

  • audio: Information about the recorded audio data
  • sentence: Text recognized from speech
  • speaker_id: ID of the speaker
  • lang: Language information

5. Training a Simple Speech Recognition Model

Now that we have loaded the dataset, let’s proceed to train a simple speech recognition model. We will do this by taking a pretrained model and applying transfer learning.

5.1. Loading the Model and Preparing Training Data


from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
import torch

# Load pretrained model and tokenizer
model_name = "facebook/wav2vec2-large-960h"
tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)
model = Wav2Vec2ForCTC.from_pretrained(model_name)

# Convert audio signal to text
def transcribe(input_audio):
    inputs = tokenizer(input_audio, return_tensors="pt", padding="longest")
    with torch.no_grad():
        logits = model(inputs.input_values).logits
    predicted_ids = torch.argmax(logits, dim=-1)
    transcription = tokenizer.batch_decode(predicted_ids)
    return transcription[0]

# Perform transcription on the first audio sample
transcription_result = transcribe(dataset[0]['audio']['array'])
print("Transcription:", transcription_result)

5.2. Code Explanation

The above code uses the pretrained Wav2Vec2 model to transcribe audio data into text. This model was developed by Facebook and was trained on 960 hours of diverse speech data. The transcribe function converts the input audio sample into text, and the result is printed to the console.

6. Analyzing Results

To evaluate the model’s performance, transcription can be performed on several audio samples and compared with the actual text. Generally, the accuracy of a speech recognition model can vary based on the speaker’s pronunciation, speech speed, background noise, etc. It is important to analyze the strengths and weaknesses of the model by comparing multiple samples.

7. Conclusion

In this course, we explored how to load automatic speech recognition datasets using the Hugging Face Transformers library and build a simple speech recognition model. While there is a plethora of video and audio data available, it is crucial to think about which model to use and how to train it. We hope to achieve high accuracy in various situations through more advanced models in the future.

I plan to continue writing articles that will help in learning and applying various deep learning technologies, so please look forward to it!

8. References