s2t-large-librispeech-asr是一个用于自动语音识别(ASR)的Speech to Text Transformer(S2T)模型。该S2T模型是在 this paper 中提出并在 this repository 中发布的。
S2T是一个端到端序列到序列的转换器模型。它使用标准的自回归交叉熵损失进行训练,并以自回归的方式生成转录。
该模型可用于端到端的语音识别(ASR)。请参阅 model hub 以查找其他S2T检查点。
由于这是一个标准的序列到序列转换器模型,您可以使用generate方法通过将语音特征传递给模型来生成转录。
注意:Speech2TextProcessor对象使用 torchaudio 来提取滤波器组特征。在运行此示例之前,请确保安装torchaudio软件包。
您可以使用pip install transformers"[speech, sentencepiece]"命令将其安装为额外的语音依赖项,或者使用pip install torchaudio sentencepiece命令分别安装这些软件包。
import torch from transformers import Speech2TextProcessor, Speech2TextForConditionalGeneration from datasets import load_dataset import soundfile as sf model = Speech2TextForConditionalGeneration.from_pretrained("facebook/s2t-large-librispeech-asr") processor = Speech2Textprocessor.from_pretrained("facebook/s2t-large-librispeech-asr") def map_to_array(batch): speech, _ = sf.read(batch["file"]) batch["speech"] = speech return batch ds = load_dataset( "patrickvonplaten/librispeech_asr_dummy", "clean", split="validation" ) ds = ds.map(map_to_array) input_features = processor( ds["speech"][0], 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 import soundfile as sf 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-large-librispeech-asr").to("cuda") processor = Speech2TextProcessor.from_pretrained("facebook/s2t-large-librispeech-asr", do_upper_case=True) def map_to_array(batch): speech, _ = sf.read(batch["file"]) batch["speech"] = speech return batch librispeech_eval = librispeech_eval.map(map_to_array) def map_to_pred(batch): features = processor(batch["speech"], 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" |
---|---|
3.3 | 7.5 |
S2T-LARGE-LIBRISPEECH-ASR是在 LibriSpeech ASR Corpus 上训练的,该数据集包含约1000小时的16kHz英语朗读音频。
语音数据通过从WAV / FLAC音频文件中自动提取符合Kaldi规范的80通道对数梅尔滤波器组特征进行预处理,可以使用PyKaldi或torchaudio完成。对每个例子都应用了基于句子的CMVN(倒谱平均值和方差归一化)。
文本使用SentencePiece进行小写和分词处理,词汇表大小为10,000。
模型使用标准的自回归交叉熵损失进行训练,并使用 SpecAugment 进行优化。编码器接收语音特征,解码器自回归生成转录。
@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}, }