🚀 Wav2Vec2-Large-XLSR-53-荷蘭語
本項目基於 Common Voice 數據集,對荷蘭語進行微調 facebook/wav2vec2-large-xlsr-53 模型。
使用此模型時,請確保語音輸入的採樣率為 16kHz。
🚀 快速開始
本模型在荷蘭語語音識別任務上進行了微調,可用於將荷蘭語語音轉換為文本。使用時需注意輸入語音的採樣率要求。
✨ 主要特性
- 微調模型:基於
facebook/wav2vec2-large-xlsr-53
在荷蘭語數據集上進行微調。
- 特定採樣率要求:語音輸入需採樣率為 16kHz。
📦 安裝指南
文檔未提供安裝步驟,暫不展示。
💻 使用示例
基礎用法
import torch
import torchaudio
from datasets import load_dataset
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
test_dataset = load_dataset("common_voice", "nl", split="test[:2%]")
processor = Wav2Vec2Processor.from_pretrained("simonsr/wav2vec2-large-xlsr-dutch")
model = Wav2Vec2ForCTC.from_pretrained("simonsr/wav2vec2-large-xlsr-dutch")
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])
高級用法
import torch
import torchaudio
from datasets import load_dataset, load_metric
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import unidecode
import re
test_dataset = load_dataset("common_voice", "nl", split="test")
wer = load_metric("wer")
processor = Wav2Vec2Processor.from_pretrained("{model_id}")
model = Wav2Vec2ForCTC.from_pretrained("{model_id}")
model.to("cuda")
chars_to_ignore_regex = '[\,\?\.\!\-\;\:\"\“\%\‘\”\�\(\)\=\´\–\&\…\—\’]'
resampler = torchaudio.transforms.Resample(48_000, 16_000)
def speech_file_to_array_fn(batch):
batch["sentence"] = unidecode.unidecode(batch["sentence"])
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"])))
📚 詳細文檔
評估
本模型可在 Common Voice 的荷蘭語測試數據上進行如下評估:
上述高級用法中的代碼即為評估代碼,測試結果顯示字錯率(WER)為 38.74%。
訓練
訓練使用了 Common Voice 的 train
、validation
等數據集。訓練腳本可在 此處 找到(需補充訓練腳本鏈接)。
🔧 技術細節
文檔未提供足夠技術細節,暫不展示。
📄 許可證
本模型採用 apache-2.0
許可證。
模型信息表格
屬性 |
詳情 |
模型類型 |
Wav2Vec2-Large-XLSR-53-荷蘭語 |
訓練數據 |
Common Voice 荷蘭語數據集 |
評估指標 |
字錯率(WER) |
測試 WER |
38.74% |
許可證 |
apache-2.0 |