🚀 Wav2Vec2-Large-XLSR-53-马耳他语
本项目是对 facebook/wav2vec2-large-xlsr-53 模型进行马耳他语微调的成果,使用了 Common Voice 数据集。使用该模型时,请确保语音输入的采样率为 16kHz。
🚀 快速开始
本模型可直接使用(无需语言模型),以下是具体操作步骤。
💻 使用示例
基础用法
import torchaudio
from datasets import load_dataset, load_metric
from transformers import (
Wav2Vec2ForCTC,
Wav2Vec2Processor,
)
import torch
import re
import sys
model_name = "Akashpb13/xlsr_maltese_wav2vec2"
device = "cuda"
chars_to_ignore_regex = '[\\,\\?\\.\\!\\-\\;\\:\\\"\\“\\%\\‘\\”\\�\\)\\(\\*)]'
model = Wav2Vec2ForCTC.from_pretrained(model_name).to(device)
processor = Wav2Vec2Processor.from_pretrained(model_name)
ds = load_dataset("common_voice", "mt", split="test", data_dir="./cv-corpus-6.1-2020-12-11")
resampler = torchaudio.transforms.Resample(orig_freq=48_000, new_freq=16_000)
def map_to_array(batch):
speech, _ = torchaudio.load(batch["path"])
batch["speech"] = resampler.forward(speech.squeeze(0)).numpy()
batch["sampling_rate"] = resampler.new_freq
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower() + " "
return batch
ds = ds.map(map_to_array)
def map_to_pred(batch):
features = processor(batch["speech"], sampling_rate=batch["sampling_rate"][0], padding=True, return_tensors="pt")
input_values = features.input_values.to(device)
attention_mask = features.attention_mask.to(device)
with torch.no_grad():
logits = model(input_values, attention_mask=attention_mask).logits
pred_ids = torch.argmax(logits, dim=-1)
batch["predicted"] = processor.batch_decode(pred_ids)
batch["target"] = batch["sentence"]
return batch
result = ds.map(map_to_pred, batched=True, batch_size=1, remove_columns=list(ds.features.keys()))
wer = load_metric("wer")
print(wer.compute(predictions=result["predicted"], references=result["target"]))
测试结果:29.42 %
📄 许可证
本项目采用 Apache-2.0 许可证。
📚 详细文档
模型信息
属性 |
详情 |
模型类型 |
微调后的 Wav2Vec2-Large-XLSR-53 马耳他语模型 |
训练数据 |
Common Voice 马耳他语数据集 |
任务类型 |
自动语音识别 |
评估指标 |
测试集词错误率(WER)为 29.42% |