🚀 Wav2Vec2-Large-XLSR-53-lg
本模型基于卢干达语(Luganda)对 facebook/wav2vec2-large-xlsr-53 进行了微调,使用了 Common Voice 数据集,涵盖训练集、验证集及其他数据(不包括测试集中的语音),同时将测试数据用于验证和测试。使用此模型时,请确保语音输入的采样率为 16kHz。
模型信息
属性 |
详情 |
模型类型 |
Wav2Vec2-Large-XLSR-53-lg |
训练数据 |
Common Voice 卢干达语数据集(训练集、验证集及其他数据,排除测试集中的语音) |
评估指标 |
词错误率(WER) |
标签 |
音频、自动语音识别、语音、xlsr 微调周 |
许可证 |
Apache 2.0 |
模型评估结果
任务名称 |
数据集 |
评估指标 |
值 |
语音识别 |
Common Voice lg |
测试 WER |
29.52 |
🚀 快速开始
✨ 主要特性
📦 安装指南
文档未提供安装步骤,故跳过该章节。
💻 使用示例
基础用法
import torch
import torchaudio
from datasets import load_dataset
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
test_dataset = load_dataset("common_voice", "lg", split="test[:2%]")
processor = Wav2Vec2Processor.from_pretrained("lucio/wav2vec2-large-xlsr-luganda")
model = Wav2Vec2ForCTC.from_pretrained("lucio/wav2vec2-large-xlsr-luganda")
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[:2]["speech"], 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 re
import unidecode
test_dataset = load_dataset("common_voice", "lg", split="test")
wer = load_metric("wer")
processor = Wav2Vec2Processor.from_pretrained("lucio/wav2vec2-large-xlsr-luganda")
model = Wav2Vec2ForCTC.from_pretrained("lucio/wav2vec2-large-xlsr-luganda")
model.to("cuda")
chars_to_ignore_regex = '[\[\],?.!;:%"“”(){}‟ˮʺ″«»/…‽�–]'
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
def remove_special_characters(batch):
batch["norm_text"] = re.sub(r'[‘’´`]', r"'", batch["sentence"])
batch["norm_text"] = re.sub(chars_to_ignore_regex, "", batch["norm_text"]).lower().strip()
batch["norm_text"] = re.sub(r"(-|' | '| +)", " ", batch["norm_text"])
batch["norm_text"] = unidecode.unidecode(batch["norm_text"])
return batch
test_dataset = test_dataset.map(speech_file_to_array_fn)
test_dataset = test_dataset.map(remove_special_characters)
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["norm_text"])))
测试结果:29.52 %
📚 详细文档
训练过程
- 数据使用:使用了 Common Voice 的
train
、validation
和 other
数据集进行训练,排除了同时存在于 other
和 test
数据集中的语音。
- 数据增强:将数据增强至原始大小的两倍,添加了噪声,并对音高、相位和强度进行了调整。
- 训练配置:在 OVHcloud 提供的 1 个 V100 GPU 上进行了 60 个 epoch 的训练,使用
test
数据进行验证。
- 训练脚本:训练使用的脚本 点击此处查看,该脚本改编自 transformers 仓库提供的示例脚本。
📄 许可证
本模型使用 Apache 2.0 许可证。