How to Fine-Tune the Greek XLSR Wav2Vec2 Model for Speech Recognition

Category :

If you’re looking to step into the world of automatic speech recognition (ASR) using the Greek language, you’ve landed in the right place! In this guide, we’ll walk through the process of fine-tuning the Wav2Vec2-Large-XLSR-53 model on the Common Voice dataset. We’ll go through everything step-by-step, ensuring that you have a user-friendly experience.

Understanding the Model and Dataset

Imagine you have a talented chef (our Wav2Vec2-Large-XLSR-53 model) who has access to some amazing recipes (the Common Voice dataset). Your goal is to teach this chef how to cook a particular dish (Greek speech recognition). By providing the chef with specific ingredients and steps, you can adapt their skills to meet this new challenge.

The Greek Speech Recognition Ingredients:

  • Model: Wav2Vec2-Large-XLSR-53
  • Dataset: Common Voice, specifically the Greek language
  • Metrics: Testing with a Word Error Rate (WER) of approximately 45.05%

Usage of the Model

To start using this model, follow these coding steps:

python
import torch
import torchaudio
from datasets import load_dataset
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor

test_dataset = load_dataset("common_voice", "el", split="test[:2%]")

processor = Wav2Vec2Processor.from_pretrained("skylordgreek_lsr_1")
model = Wav2Vec2ForCTC.from_pretrained("skylordgreek_lsr_1")

resampler = torchaudio.transforms.Resample(48_000, 16_000)

def speech_file_to_array_fn(batch):
    speech_array, sampling_rate = torchaudio.load(batch["path"])
    batch["speech"] = resampler(speech_array).squeeze().numpy()
    return batch

test_dataset = test_dataset.map(speech_file_to_array_fn)
inputs = processor(test_dataset["speech"][:2], sampling_rate=16_000, return_tensors="pt", padding=True)

with torch.no_grad():
    logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
    predicted_ids = torch.argmax(logits, dim=-1)

print("Prediction:", processor.batch_decode(predicted_ids))
print("Reference:", test_dataset["sentence"][:2])

In the above script, you are first preparing your dataset by loading the audio files, and then processing them through the model, much like gradually creating a delicious meal step by step.

Evaluating the Model

After fine-tuning your model, it’s crucial to evaluate its performance:

python
import torch
import torchaudio
from datasets import load_dataset, load_metric
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor

test_dataset = load_dataset("common_voice", "el", split="test")
wer = load_metric("wer")

processor = Wav2Vec2Processor.from_pretrained("skylordgreek_lsr_1")
model = Wav2Vec2ForCTC.from_pretrained("skylordgreek_lsr_1")
model.to("cuda")

chars_to_ignore_regex = '[,?.!-;:“]'
resampler = torchaudio.transforms.Resample(48_000, 16_000)

def speech_file_to_array_fn(batch):
    batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower()
    speech_array, sampling_rate = torchaudio.load(batch["path"])
    batch["speech"] = resampler(speech_array).squeeze().numpy()
    return batch

test_dataset = test_dataset.map(speech_file_to_array_fn)

def evaluate(batch):
    inputs = processor(batch["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
    with torch.no_grad():
        logits = model(inputs.input_values.to("cuda"), attention_mask=inputs.attention_mask.to("cuda")).logits
    pred_ids = torch.argmax(logits, dim=-1)
    batch["pred_strings"] = processor.batch_decode(pred_ids)
    return batch

result = test_dataset.map(evaluate, batched=True, batch_size=8)
print("WER: {:.2f}%".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))

Here, you are evaluating the model’s predictions against the references from the dataset to determine how accurately it recognizes speech. Think of it as tasting the dish to ensure it’s seasoned just right!

Troubleshooting Tips

If you encounter any hiccups while fine-tuning or evaluating your model, here are some troubleshooting tips:

  • Ensure the audio inputs are consistently sampled at 16kHz. An incorrect sampling rate can lead to inaccurate predictions.
  • If you run into memory issues, consider using a smaller batch size.
  • Check if all required packages and dependencies are installed correctly.
  • Feel free to reach out for support or insights – stay connected with fxis.ai for updates on AI development projects!

At **fxis.ai**, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Conclusion

Congratulations! You’ve learned how to fine-tune the Greek XLSR Wav2Vec2 model and evaluate its performance. With this powerful tool at your fingertips, you can now explore the world of automatic speech recognition tailored for the Greek language.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox

Latest Insights

© 2024 All Rights Reserved

×