模型简介
模型特点
模型能力
使用案例
🚀 XLSR - Wav2Vec2希腊语自动语音识别(ASR)模型
本项目由希腊陆军学院和克里特技术大学联合打造,提供了适用于希腊语的自动语音识别模型,可有效处理希腊语语音数据,在相关测试集上展现出良好的识别性能。
🚀 快速开始
若要使用本模型进行推理或训练,可参考以下内容:
- 推理:在
ASR_Inference.ipynb
中提供了在 CommonVoice 提取数据上进行测试的说明,代码片段也可在文档中查看。 - 训练:在
Fine_Tune_XLSR_Wav2Vec2_on_Greek_ASR_with_🤗_Transformers.ipynb
笔记本中提供了复现训练过程的说明和代码。
✨ 主要特性
- 多语言支持:XLSR - Wav2Vec2 能够学习跨多种语言的语音表示,适用于希腊语的语音识别任务。
- 性能良好:在 CommonVoice 希腊语测试数据上,词错误率(WER)为 10.497628%,字符错误率(CER)为 2.875260%。
- 训练优化:使用额外的 1.22GB CSS10 数据集进行微调,提升了模型性能。
📦 安装指南
文档未提及具体安装步骤,可参考相关依赖库的官方安装说明,确保安装 transformers
、datasets
、torchaudio
、librosa
、torch
等库。
💻 使用示例
基础用法
#!/usr/bin/env python
# coding: utf-8
# Loading dependencies and defining preprocessing functions
from transformers import Wav2Vec2ForCTC
from transformers import Wav2Vec2Processor
from datasets import load_dataset, load_metric
import re
import torchaudio
import librosa
import numpy as np
from datasets import load_dataset, load_metric
import torch
chars_to_ignore_regex = '[\\\\,\\\\?\\\\.\\\\!\\\\-\\\\;\\\\:\\\\\"\\\\“\\\\%\\\\‘\\\\”\\\\�]'
def remove_special_characters(batch):
batch["text"] = re.sub(chars_to_ignore_regex, '', batch["sentence"]).lower() + " "
return batch
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["text"]
return batch
def resample(batch):
batch["speech"] = librosa.resample(np.asarray(batch["speech"]), 48_000, 16_000)
batch["sampling_rate"] = 16_000
return batch
def prepare_dataset(batch):
# check that all files have the correct sampling rate
assert (
len(set(batch["sampling_rate"])) == 1
), f"Make sure all inputs have the same sampling rate of {processor.feature_extractor.sampling_rate}."
batch["input_values"] = processor(batch["speech"], sampling_rate=batch["sampling_rate"][0]).input_values
with processor.as_target_processor():
batch["labels"] = processor(batch["target_text"]).input_ids
return batch
# Loading model and dataset processor
model = Wav2Vec2ForCTC.from_pretrained("lighteternal/wav2vec2-large-xlsr-53-greek").to("cuda")
processor = Wav2Vec2Processor.from_pretrained("lighteternal/wav2vec2-large-xlsr-53-greek")
# Preparing speech dataset to be suitable for inference
common_voice_test = load_dataset("common_voice", "el", split="test")
common_voice_test = common_voice_test.remove_columns(["accent", "age", "client_id", "down_votes", "gender", "locale", "segment", "up_votes"])
common_voice_test = common_voice_test.map(remove_special_characters, remove_columns=["sentence"])
common_voice_test = common_voice_test.map(speech_file_to_array_fn, remove_columns=common_voice_test.column_names)
common_voice_test = common_voice_test.map(resample, num_proc=8)
common_voice_test = common_voice_test.map(prepare_dataset, remove_columns=common_voice_test.column_names, batch_size=8, num_proc=8, batched=True)
# Loading test dataset
common_voice_test_transcription = load_dataset("common_voice", "el", split="test")
#Performing inference on a random sample. Change the "example" value to try inference on different CommonVoice extracts
example = 123
input_dict = processor(common_voice_test["input_values"][example], return_tensors="pt", sampling_rate=16_000, padding=True)
logits = model(input_dict.input_values.to("cuda")).logits
pred_ids = torch.argmax(logits, dim=-1)
print("Prediction:")
print(processor.decode(pred_ids[0]))
# πού θέλεις να πάμε ρώτησε φοβισμένα ο βασιλιάς
print("Reference:")
print(common_voice_test_transcription["sentence"][example].lower())
# πού θέλεις να πάμε; ρώτησε φοβισμένα ο βασιλιάς.
高级用法
文档未提及高级用法相关代码,可根据实际需求对基础用法代码进行扩展,如调整模型参数、优化数据预处理流程等。
📚 详细文档
模型描述
Wav2Vec2 是用于自动语音识别(ASR)的预训练模型,于 2020 年 9 月由 Alexei Baevski、Michael Auli 和 Alex Conneau 发布。在 Wav2Vec2 在英语 ASR 数据集 LibriSpeech 上展示出卓越性能后不久,Facebook AI 推出了 XLSR - Wav2Vec2。XLSR 代表跨语言语音表示,指的是 XLSR - Wav2Vec2 能够学习对多种语言都有用的语音表示。
与 Wav2Vec2 类似,XLSR - Wav2Vec2 从超过 50 种语言的数十万小时未标记语音中学习强大的语音表示。与 BERT 的掩码语言建模类似,该模型通过在将特征向量传递给变压器网络之前随机掩码特征向量来学习上下文相关的语音表示。
更新:我们使用来自 CSS10 的额外 1.22GB 数据集重复了微调过程。
评估
该模型可在 Common Voice 的希腊语测试数据上进行如下评估:
import torch
import torchaudio
from datasets import load_dataset, load_metric
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import re
test_dataset = load_dataset("common_voice", "el", split="test")
wer = load_metric("wer")
processor = Wav2Vec2Processor.from_pretrained("lighteternal/wav2vec2-large-xlsr-53-greek")
model = Wav2Vec2ForCTC.from_pretrained("lighteternal/wav2vec2-large-xlsr-53-greek")
model.to("cuda")
chars_to_ignore_regex = '[\\\\,\\\\?\\\\.\\\\!\\\\-\\\\;\\\\:\\\\\"\\\\“\\\\%\\\\‘\\\\”\\\\�]'
resampler = torchaudio.transforms.Resample(48_000, 16_000)
# Preprocessing the datasets.
# We need to read the aduio files as arrays
def speech_file_to_array_fn(batch):
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)
# Preprocessing the datasets.
# We need to read the aduio files as arrays
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"])))
测试结果:10.497628 %
训练说明
在 Fine_Tune_XLSR_Wav2Vec2_on_Greek_ASR_with_🤗_Transformers.ipynb
笔记本中提供了复现训练过程的说明和代码。
指标
属性 | 详情 |
---|---|
训练损失 | 0.0545 |
验证损失 | 0.1661 |
CommonVoice 测试集字符错误率(CER) | 2.8753% |
CommonVoice 测试集词错误率(WER) | 10.4976% |
注:参考转录文本已转换为小写,并去除了标点符号和特殊字符。
致谢
本研究工作得到了希腊研究与创新基金会(HFRI)的 HFRI 博士奖学金资助(奖学金编号:50,第二轮)。
本项目基于 Patrick von Platen 的教程:[https://huggingface.co/blog/fine - tune - xlsr - wav2vec2](https://huggingface.co/blog/fine - tune - xlsr - wav2vec2) 原始 Colab 笔记本链接:https://colab.research.google.com/github/patrickvonplaten/notebooks/blob/master/Fine_Tune_XLSR_Wav2Vec2_on_Turkish_ASR_with_%F0%9F%A4%97_Transformers.ipynb#scrollTo=V7YOT2mnUiea
🔧 技术细节
本模型基于 XLSR - Wav2Vec2 架构,在单个 NVIDIA RTX 3080 上训练了 50 个 epoch,约 8 小时。使用了 CommonVoice(EL)364MB 数据集(https://commonvoice.mozilla.org/el/datasets)和 CSS10(EL)1.22GB 数据集(https://github.com/Kyubyong/css10)进行训练。
📄 许可证
本项目采用 Apache - 2.0 许可证。



