Wav2vec2 Large Xlsr 53 Es
基于Facebook的wav2vec2-large-xlsr-53模型,在西班牙语Common Voice数据集上微调的语音识别模型,测试WER为10.50%。
下载量 147
发布时间 : 3/2/2022
模型简介
这是一个针对西班牙语优化的自动语音识别(ASR)模型,能够将西班牙语语音转换为文本。
模型特点
低词错误率
在Common Voice西班牙语测试集上达到10.50%的WER
保留变音符号
保留了西班牙语中的变音符号,确保语义准确性
无需语言模型
可直接使用,无需额外语言模型支持
多阶段训练
采用分阶段训练策略,逐步优化模型性能
模型能力
西班牙语语音识别
16kHz音频处理
批量语音转文本
使用案例
语音转录
西班牙语语音转文字
将西班牙语语音内容转换为文本格式
准确率约89.5% (WER 10.5%)
语音助手
西班牙语语音指令识别
用于西班牙语语音助手的基础识别组件
🚀 西班牙文Wav2Vec2-Large-XLSR-53模型
本项目基于Common Voice数据集,在西班牙文上对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", "es", split="test[:2%]")
processor = Wav2Vec2Processor.from_pretrained("pcuenq/wav2vec2-large-xlsr-53-es")
model = Wav2Vec2ForCTC.from_pretrained("pcuenq/wav2vec2-large-xlsr-53-es")
resampler = torchaudio.transforms.Resample(48_000, 16_000)
# Preprocessing the datasets.
# We need to read the audio files as arrays
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])
✨ 主要特性
- 基于大规模预训练模型
facebook/wav2vec2-large-xlsr-53
进行微调,适用于西班牙文语音识别任务。 - 对Common Voice数据集中的西班牙文数据进行了处理,去除了大量非西班牙文的字符,保留了西班牙文的变音符号,在准确性和语义理解上取得了较好的平衡。
- 通过多次调整训练参数和策略,不断优化模型的WER(词错误率),最终达到了约10.5%的测试WER。
💻 使用示例
基础用法
import torch
import torchaudio
from datasets import load_dataset
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
test_dataset = load_dataset("common_voice", "es", split="test[:2%]")
processor = Wav2Vec2Processor.from_pretrained("pcuenq/wav2vec2-large-xlsr-53-es")
model = Wav2Vec2ForCTC.from_pretrained("pcuenq/wav2vec2-large-xlsr-53-es")
resampler = torchaudio.transforms.Resample(48_000, 16_000)
# Preprocessing the datasets.
# We need to read the audio files as arrays
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 re
test_dataset = load_dataset("common_voice", "es", split="test")
wer = load_metric("wer")
processor = Wav2Vec2Processor.from_pretrained("pcuenq/wav2vec2-large-xlsr-53-es")
model = Wav2Vec2ForCTC.from_pretrained("pcuenq/wav2vec2-large-xlsr-53-es")
model.to("cuda")
## Text pre-processing
chars_to_ignore_regex = '[\,\¿\?\.\¡\!\-\;\:\"\“\%\‘\”\\…\’\ː\'\‹\›\`\´\®\—\→]'
chars_to_ignore_pattern = re.compile(chars_to_ignore_regex)
def remove_special_characters(batch):
batch["sentence"] = chars_to_ignore_pattern.sub('', batch["sentence"]).lower() + " "
return batch
def replace_diacritics(batch):
sentence = batch["sentence"]
sentence = re.sub('ì', 'í', sentence)
sentence = re.sub('ù', 'ú', sentence)
sentence = re.sub('ò', 'ó', sentence)
sentence = re.sub('à', 'á', sentence)
batch["sentence"] = sentence
return batch
def replace_additional(batch):
sentence = batch["sentence"]
sentence = re.sub('ã', 'a', sentence) # Portuguese, as in São Paulo
sentence = re.sub('ō', 'o', sentence) # Japanese
sentence = re.sub('ê', 'e', sentence) # Português
batch["sentence"] = sentence
return batch
## Audio pre-processing
# I tried to perform the resampling using a `torchaudio` `Resampler` transform,
# but found that the process deadlocked when using multiple processes.
# Perhaps my torchaudio is using the wrong sox library under the hood, I'm not sure.
# Fortunately, `librosa` seems to work fine, so that's what I'll use for now.
import librosa
def speech_file_to_array_fn(batch):
speech_array, sample_rate = torchaudio.load(batch["path"])
batch["speech"] = librosa.resample(speech_array.squeeze().numpy(), sample_rate, 16_000)
return batch
# One-pass mapping function
# Text transformation and audio resampling
def cv_prepare(batch):
batch = remove_special_characters(batch)
batch = replace_diacritics(batch)
batch = replace_additional(batch)
batch = speech_file_to_array_fn(batch)
return batch
# Number of CPUs or None
num_proc = 16
test_dataset = test_dataset.map(cv_prepare, remove_columns=['path'], num_proc=num_proc)
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)
# WER Metric computation
# `wer.compute` crashes in my computer with more than ~10000 samples.
# Until I confirm in a different one, I created a "chunked" version of the computation.
# It gives the same results as `wer.compute` for smaller datasets.
import jiwer
def chunked_wer(targets, predictions, chunk_size=None):
if chunk_size is None: return jiwer.wer(targets, predictions)
start = 0
end = chunk_size
H, S, D, I = 0, 0, 0, 0
while start < len(targets):
chunk_metrics = jiwer.compute_measures(targets[start:end], predictions[start:end])
H = H + chunk_metrics["hits"]
S = S + chunk_metrics["substitutions"]
D = D + chunk_metrics["deletions"]
I = I + chunk_metrics["insertions"]
start += chunk_size
end += chunk_size
return float(S + D + I) / float(H + S + D)
print("WER: {:2f}".format(100 * chunked_wer(result["sentence"], result["pred_strings"], chunk_size=4000)))
#print("WER: {:2f}".format(100 * wer.compute(predictions=result["pred_strings"], references=result["sentence"])))
📚 详细文档
文本处理
Common Voice的西班牙文数据集即使在去除分隔符和标点符号后,仍包含大量非西班牙文的字符。因此,对这些字符进行了处理,去除了大部分无关字符,并保留了所有西班牙文的变音符号。虽然仅使用非重音字符可能会获得更好的WER分数,但保留变音符号在语义理解上更为准确。具体的处理规则在评估脚本中有所体现。
训练过程
使用了Common Voice的train
和validation
数据集进行训练。为了更好地观察训练进度并及时调整策略,最初将train
和validation
数据集按10%进行分割。具体训练过程如下:
- 仅在第一个分割数据集上训练30个epoch,使用与Patrick在演示笔记本中类似的参数,批量大小为24,梯度累积步数为2,在完整测试集上的WER约为16.3%。
- 在剩余的9个分割数据集上分别训练3个epoch,使用更快的75步热身,WER约为11.7%。
- 在10个分割数据集上分别训练3个epoch,使用较小的学习率
1e-4
和75步热身,最终模型的WER约为11.7%。 - 使用完整数据集进行训练,选择带有硬重启的余弦调度,参考学习率为
3e-5
,训练10个epoch,无热身,最终WER约为10.5%。
其他尝试
- 从相同的微调模型开始,比较了恒定学习率
1e-4
和带热身的线性调度,线性调度效果更好(WER分别为11.85%和12.72%)。 - 尝试使用西班牙文模型改进巴斯克文模型,但未取得效果。
- 标签平滑方法未起作用。
问题与技术挑战
Datasets
抽象基于内存映射文件,可处理任意大小的数据集,但需要了解其局限性和权衡。缓存使用方便,但磁盘空间消耗快,需要了解缓存文件的存储方式,并在必要时手动保存数据。- 训练开始前存在明显延迟,已找到原因并讨论出解决方案。
wer.compute
在处理大数据集时会崩溃,因此实现了一个分块计算的版本。torchaudio
在使用多进程时会出现死锁问题,目前使用librosa
进行重采样。- 在笔记本中使用
num_proc
时,无法看到进度条,可能是权限问题。
🔧 技术细节
模型信息
属性 | 详情 |
---|---|
模型类型 | 基于Wav2Vec2-Large-XLSR-53微调的西班牙文语音识别模型 |
训练数据 | Common Voice的西班牙文train 和validation 数据集 |
训练参数调整
在训练过程中,多次调整了学习率、热身步数、训练轮数等参数,以优化模型的性能。具体的参数调整过程和效果在训练部分有详细描述。
数据处理
对文本数据进行了特殊字符去除、变音符号替换等处理,对音频数据进行了重采样,以确保输入数据的质量和一致性。具体的处理函数和规则在评估脚本中有所体现。
📄 许可证
本模型使用Apache-2.0许可证。
Voice Activity Detection
MIT
基于pyannote.audio 2.1版本的语音活动检测模型,用于识别音频中的语音活动时间段
语音识别
V
pyannote
7.7M
181
Wav2vec2 Large Xlsr 53 Portuguese
Apache-2.0
这是一个针对葡萄牙语语音识别任务微调的XLSR-53大模型,基于Common Voice 6.1数据集训练,支持葡萄牙语语音转文本。
语音识别 其他
W
jonatasgrosman
4.9M
32
Whisper Large V3
Apache-2.0
Whisper是由OpenAI提出的先进自动语音识别(ASR)和语音翻译模型,在超过500万小时的标注数据上训练,具有强大的跨数据集和跨领域泛化能力。
语音识别 支持多种语言
W
openai
4.6M
4,321
Whisper Large V3 Turbo
MIT
Whisper是由OpenAI开发的最先进的自动语音识别(ASR)和语音翻译模型,经过超过500万小时标记数据的训练,在零样本设置下展现出强大的泛化能力。
语音识别
Transformers 支持多种语言

W
openai
4.0M
2,317
Wav2vec2 Large Xlsr 53 Russian
Apache-2.0
基于facebook/wav2vec2-large-xlsr-53模型微调的俄语语音识别模型,支持16kHz采样率的语音输入
语音识别 其他
W
jonatasgrosman
3.9M
54
Wav2vec2 Large Xlsr 53 Chinese Zh Cn
Apache-2.0
基于facebook/wav2vec2-large-xlsr-53模型微调的中文语音识别模型,支持16kHz采样率的语音输入。
语音识别 中文
W
jonatasgrosman
3.8M
110
Wav2vec2 Large Xlsr 53 Dutch
Apache-2.0
基于facebook/wav2vec2-large-xlsr-53微调的荷兰语语音识别模型,在Common Voice和CSS10数据集上训练,支持16kHz音频输入。
语音识别 其他
W
jonatasgrosman
3.0M
12
Wav2vec2 Large Xlsr 53 Japanese
Apache-2.0
基于facebook/wav2vec2-large-xlsr-53模型微调的日语语音识别模型,支持16kHz采样率的语音输入
语音识别 日语
W
jonatasgrosman
2.9M
33
Mms 300m 1130 Forced Aligner
基于Hugging Face预训练模型的文本与音频强制对齐工具,支持多种语言,内存效率高
语音识别
Transformers 支持多种语言

M
MahmoudAshraf
2.5M
50
Wav2vec2 Large Xlsr 53 Arabic
Apache-2.0
基于facebook/wav2vec2-large-xlsr-53微调的阿拉伯语语音识别模型,在Common Voice和阿拉伯语语音语料库上训练
语音识别 阿拉伯语
W
jonatasgrosman
2.3M
37
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers 支持多种语言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers 英语

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统 中文
R
uer
2,694
98