模型简介
模型特点
模型能力
使用案例
🚀 S2T-SMALL-LIBRISPEECH-ASR
s2t-small-librispeech-asr
是一个用于自动语音识别(ASR)的语音转文本Transformer(S2T)模型。S2T模型在 这篇论文 中被提出,并在 这个仓库 中发布。
🚀 快速开始
模型描述
S2T 是一个端到端的序列到序列Transformer模型。它使用标准的自回归交叉熵损失进行训练,并自回归地生成转录文本。
预期用途和限制
该模型可用于端到端的语音识别(ASR)。你可以查看 模型中心 以寻找其他S2T检查点。
如何使用
由于这是一个标准的序列到序列Transformer模型,你可以使用 generate
方法,将语音特征传递给模型来生成转录文本。
⚠️ 重要提示
Speech2TextProcessor
对象使用 torchaudio 来提取滤波器组特征。在运行此示例之前,请确保安装torchaudio
包。
⚠️ 重要提示
特征提取器依赖于 torchaudio,分词器依赖于 sentencepiece,因此在运行示例之前,请确保安装这些包。
你可以使用 pip install transformers"[speech, sentencepiece]"
作为额外的语音依赖项来安装它们,或者使用 pip install torchaudio sentencepiece
分别安装这些包。
import torch
from transformers import Speech2TextProcessor, Speech2TextForConditionalGeneration
from datasets import load_dataset
model = Speech2TextForConditionalGeneration.from_pretrained("facebook/s2t-small-librispeech-asr")
processor = Speech2TextProcessor.from_pretrained("facebook/s2t-small-librispeech-asr")
ds = load_dataset(
"patrickvonplaten/librispeech_asr_dummy",
"clean",
split="validation"
)
input_features = processor(
ds[0]["audio"]["array"],
sampling_rate=16_000,
return_tensors="pt"
).input_features # Batch size 1
generated_ids = model.generate(input_ids=input_features)
transcription = processor.batch_decode(generated_ids)
在LibriSpeech测试集上的评估
以下脚本展示了如何在 LibriSpeech 的 "clean" 和 "other" 测试数据集上评估该模型。
from datasets import load_dataset, load_metric
from transformers import Speech2TextForConditionalGeneration, Speech2TextProcessor
librispeech_eval = load_dataset("librispeech_asr", "clean", split="test") # change to "other" for other test dataset
wer = load_metric("wer")
model = Speech2TextForConditionalGeneration.from_pretrained("facebook/s2t-small-librispeech-asr").to("cuda")
processor = Speech2TextProcessor.from_pretrained("facebook/s2t-small-librispeech-asr", do_upper_case=True)
librispeech_eval = librispeech_eval.map(map_to_array)
def map_to_pred(batch):
features = processor(batch["audio"]["array"], sampling_rate=16000, padding=True, return_tensors="pt")
input_features = features.input_features.to("cuda")
attention_mask = features.attention_mask.to("cuda")
gen_tokens = model.generate(input_ids=input_features, attention_mask=attention_mask)
batch["transcription"] = processor.batch_decode(gen_tokens, skip_special_tokens=True)
return batch
result = librispeech_eval.map(map_to_pred, batched=True, batch_size=8, remove_columns=["speech"])
print("WER:", wer(predictions=result["transcription"], references=result["text"]))
结果(WER):
"clean" | "other" |
---|---|
4.3 | 9.0 |
📦 安装指南
你可以使用 pip install transformers"[speech, sentencepiece]"
作为额外的语音依赖项来安装所需包,或者使用 pip install torchaudio sentencepiece
分别安装这些包。
💻 使用示例
基础用法
import torch
from transformers import Speech2TextProcessor, Speech2TextForConditionalGeneration
from datasets import load_dataset
model = Speech2TextForConditionalGeneration.from_pretrained("facebook/s2t-small-librispeech-asr")
processor = Speech2TextProcessor.from_pretrained("facebook/s2t-small-librispeech-asr")
ds = load_dataset(
"patrickvonplaten/librispeech_asr_dummy",
"clean",
split="validation"
)
input_features = processor(
ds[0]["audio"]["array"],
sampling_rate=16_000,
return_tensors="pt"
).input_features # Batch size 1
generated_ids = model.generate(input_ids=input_features)
transcription = processor.batch_decode(generated_ids)
高级用法
from datasets import load_dataset, load_metric
from transformers import Speech2TextForConditionalGeneration, Speech2TextProcessor
librispeech_eval = load_dataset("librispeech_asr", "clean", split="test") # change to "other" for other test dataset
wer = load_metric("wer")
model = Speech2TextForConditionalGeneration.from_pretrained("facebook/s2t-small-librispeech-asr").to("cuda")
processor = Speech2TextProcessor.from_pretrained("facebook/s2t-small-librispeech-asr", do_upper_case=True)
librispeech_eval = librispeech_eval.map(map_to_array)
def map_to_pred(batch):
features = processor(batch["audio"]["array"], sampling_rate=16000, padding=True, return_tensors="pt")
input_features = features.input_features.to("cuda")
attention_mask = features.attention_mask.to("cuda")
gen_tokens = model.generate(input_ids=input_features, attention_mask=attention_mask)
batch["transcription"] = processor.batch_decode(gen_tokens, skip_special_tokens=True)
return batch
result = librispeech_eval.map(map_to_pred, batched=True, batch_size=8, remove_columns=["speech"])
print("WER:", wer(predictions=result["transcription"], references=result["text"]))
📚 详细文档
训练数据
S2T-SMALL-LIBRISPEECH-ASR 在 LibriSpeech ASR语料库 上进行训练,该数据集包含大约1000小时的16kHz朗读英语语音。
训练过程
预处理
语音数据通过PyKaldi或torchaudio从WAV/FLAC音频文件中自动提取符合Kaldi标准的80通道对数梅尔滤波器组特征进行预处理。此外,对每个示例应用了基于语句级别的CMVN(倒谱均值和方差归一化)。
文本被转换为小写,并使用SentencePiece进行分词,词汇表大小为10,000。
训练
该模型使用标准的自回归交叉熵损失和 SpecAugment 进行训练。编码器接收语音特征,解码器自回归地生成转录文本。
BibTeX引用和引用信息
@inproceedings{wang2020fairseqs2t,
title = {fairseq S2T: Fast Speech-to-Text Modeling with fairseq},
author = {Changhan Wang and Yun Tang and Xutai Ma and Anne Wu and Dmytro Okhonko and Juan Pino},
booktitle = {Proceedings of the 2020 Conference of the Asian Chapter of the Association for Computational Linguistics (AACL): System Demonstrations},
year = {2020},
}
🔧 技术细节
S2T是一个端到端的序列到序列Transformer模型,使用标准的自回归交叉熵损失进行训练。语音数据经过特征提取和归一化处理,文本经过小写转换和分词处理。模型训练使用了SpecAugment技术,编码器接收语音特征,解码器自回归地生成转录文本。
📄 许可证
本项目采用MIT许可证。
信息表格
属性 | 详情 |
---|---|
模型类型 | 语音转文本Transformer(S2T)模型 |
训练数据 | LibriSpeech ASR语料库,包含约1000小时的16kHz朗读英语语音 |



