模型概述
模型特點
模型能力
使用案例
🚀 XLSR - Wav2Vec2希臘語自動語音識別(ASR)模型
本項目由希臘陸軍學院和克里特技術大學聯合打造,提供了適用於希臘語的自動語音識別模型,可有效處理希臘語語音數據,在相關測試集上展現出良好的識別性能。
🚀 快速開始
若要使用本模型進行推理或訓練,可參考以下內容:
- 推理:在
ASR_Inference.ipynb
中提供了在 CommonVoice 提取數據上進行測試的說明,代碼片段也可在文檔中查看。 - 訓練:在
Fine_Tune_XLSR_Wav2Vec2_on_Greek_ASR_with_🤗_Transformers.ipynb
筆記本中提供了復現訓練過程的說明和代碼。
✨ 主要特性
- 多語言支持:XLSR - Wav2Vec2 能夠學習跨多種語言的語音表示,適用於希臘語的語音識別任務。
- 性能良好:在 CommonVoice 希臘語測試數據上,詞錯誤率(WER)為 10.497628%,字符錯誤率(CER)為 2.875260%。
- 訓練優化:使用額外的 1.22GB CSS10 數據集進行微調,提升了模型性能。
📦 安裝指南
文檔未提及具體安裝步驟,可參考相關依賴庫的官方安裝說明,確保安裝 transformers
、datasets
、torchaudio
、librosa
、torch
等庫。
💻 使用示例
基礎用法
#!/usr/bin/env python
# coding: utf-8
# Loading dependencies and defining preprocessing functions
from transformers import Wav2Vec2ForCTC
from transformers import Wav2Vec2Processor
from datasets import load_dataset, load_metric
import re
import torchaudio
import librosa
import numpy as np
from datasets import load_dataset, load_metric
import torch
chars_to_ignore_regex = '[\\\\,\\\\?\\\\.\\\\!\\\\-\\\\;\\\\:\\\\\"\\\\“\\\\%\\\\‘\\\\”\\\\�]'
def remove_special_characters(batch):
batch["text"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower() + " "
return batch
def speech_file_to_array_fn(batch):
speech_array, sampling_rate = torchaudio.load(batch["path"])
batch["speech"] = speech_array[0].numpy()
batch["sampling_rate"] = sampling_rate
batch["target_text"] = batch["text"]
return batch
def resample(batch):
batch["speech"] = librosa.resample(np.asarray(batch["speech"]), 48_000, 16_000)
batch["sampling_rate"] = 16_000
return batch
def prepare_dataset(batch):
# check that all files have the correct sampling rate
assert (
len(set(batch["sampling_rate"])) == 1
), f"Make sure all inputs have the same sampling rate of {processor.feature_extractor.sampling_rate}."
batch["input_values"] = processor(batch["speech"], sampling_rate=batch["sampling_rate"][0]).input_values
with processor.as_target_processor():
batch["labels"] = processor(batch["target_text"]).input_ids
return batch
# Loading model and dataset processor
model = Wav2Vec2ForCTC.from_pretrained("lighteternal/wav2vec2-large-xlsr-53-greek").to("cuda")
processor = Wav2Vec2Processor.from_pretrained("lighteternal/wav2vec2-large-xlsr-53-greek")
# Preparing speech dataset to be suitable for inference
common_voice_test = load_dataset("common_voice", "el", split="test")
common_voice_test = common_voice_test.remove_columns(["accent", "age", "client_id", "down_votes", "gender", "locale", "segment", "up_votes"])
common_voice_test = common_voice_test.map(remove_special_characters, remove_columns=["sentence"])
common_voice_test = common_voice_test.map(speech_file_to_array_fn, remove_columns=common_voice_test.column_names)
common_voice_test = common_voice_test.map(resample, num_proc=8)
common_voice_test = common_voice_test.map(prepare_dataset, remove_columns=common_voice_test.column_names, batch_size=8, num_proc=8, batched=True)
# Loading test dataset
common_voice_test_transcription = load_dataset("common_voice", "el", split="test")
#Performing inference on a random sample. Change the "example" value to try inference on different CommonVoice extracts
example = 123
input_dict = processor(common_voice_test["input_values"][example], return_tensors="pt", sampling_rate=16_000, padding=True)
logits = model(input_dict.input_values.to("cuda")).logits
pred_ids = torch.argmax(logits, dim=-1)
print("Prediction:")
print(processor.decode(pred_ids[0]))
# πού θέλεις να πάμε ρώτησε φοβισμένα ο βασιλιάς
print("Reference:")
print(common_voice_test_transcription["sentence"][example].lower())
# πού θέλεις να πάμε; ρώτησε φοβισμένα ο βασιλιάς.
高級用法
文檔未提及高級用法相關代碼,可根據實際需求對基礎用法代碼進行擴展,如調整模型參數、優化數據預處理流程等。
📚 詳細文檔
模型描述
Wav2Vec2 是用於自動語音識別(ASR)的預訓練模型,於 2020 年 9 月由 Alexei Baevski、Michael Auli 和 Alex Conneau 發佈。在 Wav2Vec2 在英語 ASR 數據集 LibriSpeech 上展示出卓越性能後不久,Facebook AI 推出了 XLSR - Wav2Vec2。XLSR 代表跨語言語音表示,指的是 XLSR - Wav2Vec2 能夠學習對多種語言都有用的語音表示。
與 Wav2Vec2 類似,XLSR - Wav2Vec2 從超過 50 種語言的數十萬小時未標記語音中學習強大的語音表示。與 BERT 的掩碼語言建模類似,該模型通過在將特徵向量傳遞給變壓器網絡之前隨機掩碼特徵向量來學習上下文相關的語音表示。
更新:我們使用來自 CSS10 的額外 1.22GB 數據集重複了微調過程。
評估
該模型可在 Common Voice 的希臘語測試數據上進行如下評估:
import torch
import torchaudio
from datasets import load_dataset, load_metric
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import re
test_dataset = load_dataset("common_voice", "el", split="test")
wer = load_metric("wer")
processor = Wav2Vec2Processor.from_pretrained("lighteternal/wav2vec2-large-xlsr-53-greek")
model = Wav2Vec2ForCTC.from_pretrained("lighteternal/wav2vec2-large-xlsr-53-greek")
model.to("cuda")
chars_to_ignore_regex = '[\\\\,\\\\?\\\\.\\\\!\\\\-\\\\;\\\\:\\\\\"\\\\“\\\\%\\\\‘\\\\”\\\\�]'
resampler = torchaudio.transforms.Resample(48_000, 16_000)
# Preprocessing the datasets.
# We need to read the aduio files as arrays
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)
# Preprocessing the datasets.
# We need to read the aduio files as arrays
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"])))
測試結果:10.497628 %
訓練說明
在 Fine_Tune_XLSR_Wav2Vec2_on_Greek_ASR_with_🤗_Transformers.ipynb
筆記本中提供了復現訓練過程的說明和代碼。
指標
屬性 | 詳情 |
---|---|
訓練損失 | 0.0545 |
驗證損失 | 0.1661 |
CommonVoice 測試集字符錯誤率(CER) | 2.8753% |
CommonVoice 測試集詞錯誤率(WER) | 10.4976% |
注:參考轉錄文本已轉換為小寫,並去除了標點符號和特殊字符。
致謝
本研究工作得到了希臘研究與創新基金會(HFRI)的 HFRI 博士獎學金資助(獎學金編號:50,第二輪)。
本項目基於 Patrick von Platen 的教程:[https://huggingface.co/blog/fine - tune - xlsr - wav2vec2](https://huggingface.co/blog/fine - tune - xlsr - wav2vec2) 原始 Colab 筆記本鏈接:https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Fine_Tune_XLSR_Wav2Vec2_on_Turkish_ASR_with_%F0%9F%A4%97_Transformers.ipynb#scrollTo=V7YOT2mnUiea
🔧 技術細節
本模型基於 XLSR - Wav2Vec2 架構,在單個 NVIDIA RTX 3080 上訓練了 50 個 epoch,約 8 小時。使用了 CommonVoice(EL)364MB 數據集(https://commonvoice.mozilla.org/el/datasets)和 CSS10(EL)1.22GB 數據集(https://github.com/Kyubyong/css10)進行訓練。
📄 許可證
本項目採用 Apache - 2.0 許可證。



