🚀 Wav2Vec2-Large-XLSR-53
本項目是基於Galician語微調的語音識別模型,在Galician語語音識別任務中表現出色,能有效提升語音識別的準確性。
🚀 快速開始
當使用此模型時,請確保您的語音輸入採樣率為16kHz。
✨ 主要特性
📦 安裝指南
文檔未提及具體安裝步驟,可參考相關依賴庫的安裝方式,確保安裝 torch
、torchaudio
、datasets
、transformers
等庫。
💻 使用示例
基礎用法
模型可以直接使用(無需語言模型),示例代碼如下:
import torch
import torchaudio
from datasets import load_dataset
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
test_dataset = load_dataset("common_voice", "gl", split="test[:2%]")
processor = Wav2Vec2Processor.from_pretrained("diego-fustes/wav2vec2-large-xlsr-gl")
model = Wav2Vec2ForCTC.from_pretrained("diego-fustes/wav2vec2-large-xlsr-gl")
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])
高級用法
以下是在Galician語測試數據上評估模型的代碼示例:
import torch
import torchaudio
from datasets import load_dataset, load_metric
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import re
test_dataset = load_dataset("common_voice", "gl", split="test")
wer = load_metric("wer")
processor = Wav2Vec2Processor.from_pretrained("diego-fustes/wav2vec2-large-xlsr-gl")
model = Wav2Vec2ForCTC.from_pretrained("diego-fustes/wav2vec2-large-xlsr-gl")
model.to("cuda")
chars_to_ignore_regex = '[^a-záéíóúñ ]'
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"])))
📚 詳細文檔
測試結果
在OpenSLR數據集劃分上的測試結果:字錯率(WER)為16.79%。
訓練信息
- 數據集:使用OpenSLR SLR77 數據集進行訓練和驗證,數據集劃分比例為:70%用於訓練,15%用於驗證,15%用於測試。
- 訓練腳本:訓練使用的腳本可在 此處 找到。
📄 許可證
本項目採用Apache-2.0許可證。
🔧 技術細節