🚀 Wav2Vec2-Large-XLSR-53-Fon
本项目基于 Fon (or Fongbe) 数据集,对 facebook/wav2vec2-large-xlsr-53 模型进行微调,得到适用于 Fon 语言的语音识别模型。使用该模型时,请确保语音输入的采样率为 16kHz。
📦 模型信息
属性 |
详情 |
语言 |
Fon |
数据集 |
Fon 数据集 |
评估指标 |
词错误率(WER) |
标签 |
音频、自动语音识别、语音、xlsr 微调周、HF 自动语音识别排行榜 |
许可证 |
Apache-2.0 |
模型名称 |
Fon XLSR Wav2Vec2 Large 53 |
任务 |
语音识别(自动语音识别) |
测试集 WER |
14.97 |
🚀 快速开始
模型使用
本模型可以直接使用(无需语言模型),示例代码如下:
import json
import random
import torch
import torchaudio
from datasets import load_dataset
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
from datasets import load_dataset, load_metric
for root, dirs, files in os.walk(test/):
test_dataset= load_dataset("json", data_files=[os.path.join(root,i) for i in files],split="train")
chars_to_ignore_regex = '[\\,\\?\\.\\!\\-\\;\\:\\\"\\“\\%\\‘\\”]'
def remove_special_characters(batch):
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower() + " "
return batch
test_dataset = test_dataset.map(remove_special_characters)
processor = Wav2Vec2Processor.from_pretrained("chrisjay/wav2vec2-large-xlsr-53-fon")
model = Wav2Vec2ForCTC.from_pretrained("chrisjay/wav2vec2-large-xlsr-53-fon")
def speech_file_to_array_fn(batch):
speech_array, sampling_rate = torchaudio.load(batch["path"])
batch["speech"]=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():
tlogits = 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])
模型评估
可以使用以下代码在 Fon 测试集上对模型进行评估:
import torch
import torchaudio
from datasets import load_dataset, load_metric
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import re
for root, dirs, files in os.walk(test/):
test_dataset = load_dataset("json", data_files=[os.path.join(root,i) for i in files],split="train")
chars_to_ignore_regex = '[\\,\\?\\.\\!\\-\\;\\:\\\"\\“\\%\\‘\\”]'
batch["sentence"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower() + " "
return batch
test_dataset = test_dataset.map(remove_special_characters)
wer = load_metric("wer")
processor = Wav2Vec2Processor.from_pretrained("chrisjay/wav2vec2-large-xlsr-53-fon")
model = Wav2Vec2ForCTC.from_pretrained("chrisjay/wav2vec2-large-xlsr-53-fon")
model.to("cuda")
def speech_file_to_array_fn(batch):
speech_array, sampling_rate = torchaudio.load(batch["path"])
batch["speech"] = speech_array[0].numpy()
batch["sampling_rate"] = sampling_rate
batch["target_text"] = batch["sentence"]
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"])))
测试结果:14.97 %
模型训练
Fon 数据集 被划分为 训练集
(8235 个样本)、验证集
(1107 个样本)和 测试集
(1061 个样本)。训练脚本可以在 这里 找到。
👥 项目贡献者
- Chris C. Emezue (Twitter)|(chris.emezue@gmail.com)
- Bonaventure F.P. Dossou (HuggingFace 用户名: bonadossou)|(Twitter)|(femipancrace.dossou@gmail.com)
📖 相关研究
本项目是 OkwuGbé: End-to-End Speech Recognition for Fon and Igbo 研究的延续。