模型概述
模型特點
模型能力
使用案例
🚀 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朗讀英語語音 |



