🚀 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 |