模型概述
模型特點
模型能力
使用案例
🚀 適用於LibriSpeech的Conformer模型
本倉庫提供了在SpeechBrain中使用基於LibriSpeech(英文)預訓練的端到端系統進行自動語音識別所需的所有工具。為獲得更好的使用體驗,建議您進一步瞭解 SpeechBrain。
🚀 快速開始
本倉庫提供了在SpeechBrain中使用基於LibriSpeech(英文)預訓練的端到端系統進行自動語音識別所需的所有工具。若想獲得更好的使用體驗,建議您進一步瞭解 SpeechBrain。
模型在全上下文模式(非流式)下的性能如下:
版本 | 測試集純淨數據WER | 測試集其他數據WER | GPU 配置 |
---|---|---|---|
24 - 02 - 26 | 2.72 | 6.47 | 4xA100 40GB |
流式處理時,在測試集純淨數據上不同塊大小的結果如下:
全上下文 | cs=32 (1280ms) | 24 (960ms) | 16 (640ms) | 12 (480ms) | 8 (320ms) | |
---|---|---|---|---|---|---|
全上下文 | 2.72% | - | - | - | - | - |
lc=32 | - | 3.09% | 3.07% | 3.26% | 3.31% | 3.44% |
16 | - | 3.10% | 3.07% | 3.27% | 3.32% | 3.50% |
8 | - | 3.10% | 3.11% | 3.31% | 3.39% | 3.62% |
4 | - | 3.12% | 3.13% | 3.37% | 3.51% | 3.80% |
2 | - | 3.19% | 3.24% | 3.50% | 3.79% | 4.38% |
✨ 主要特性
- 本自動語音識別系統是一個使用RNN - T損失(帶有輔助CTC損失以穩定訓練)訓練的Conformer模型,採用了一元分詞器。
- 支持流式處理,利用了動態塊訓練技術,多頭注意力模塊使用了分塊注意力,並採用了 動態塊卷積 的實現。
- 模型在訓練時支持不同的塊大小(甚至全上下文),適用於各種塊大小和離線轉錄。
- 系統使用採樣率為16kHz(單聲道)的錄音進行訓練,調用
transcribe_file
時,代碼會根據需要自動對音頻進行歸一化處理(即重採樣 + 單聲道選擇)。
📦 安裝指南
首先,請使用以下命令安裝SpeechBrain:
pip install speechbrain
建議您閱讀我們的教程,進一步瞭解 SpeechBrain。
💻 使用示例
基礎用法
對英文音頻文件進行轉錄:
from speechbrain.inference.ASR import StreamingASR
from speechbrain.utils.dynamic_chunk_training import DynChunkTrainConfig
asr_model = StreamingASR.from_hparams(
source="speechbrain/asr-streaming-conformer-librispeech",
savedir="pretrained_models/asr-streaming-conformer-librispeech"
)
asr_model.transcribe_file(
"speechbrain/asr-streaming-conformer-librispeech/test-en.wav",
# 選擇約960ms的塊大小,左上下文為4個塊
DynChunkTrainConfig(24, 4),
# 禁用torchaudio流式處理,以便從HuggingFace獲取數據
# 對於您自己的文件或流,將此設置為True以允許流式文件解碼
use_torchaudio_streaming=False,
)
DynChunkTrainConfig
的值可以根據延遲、計算能力和轉錄準確性進行調整。請參考流式WER表,選擇適合您用例的值。
高級用法
命令行工具轉錄文件或即時流
使用ffmpeg從即時流解碼(BBC Radio 4):
python3 asr.py http://as-hls-ww-live.akamaized.net/pool_904/live/ww/bbc_radio_fourfm/bbc_radio_fourfm.isml/bbc_radio_fourfm-audio%3d96000.norewind.m3u8 --model-source=speechbrain/asr-streaming-conformer-librispeech --device=cpu -v
從文件解碼:
python3 asr.py some-english-speech.wav --model-source=speechbrain/asr-streaming-conformer-librispeech --device=cpu -v
from argparse import ArgumentParser
import logging
parser = ArgumentParser()
parser.add_argument("audio_path")
parser.add_argument("--model-source", required=True)
parser.add_argument("--device", default="cpu")
parser.add_argument("--ip", default="127.0.0.1")
parser.add_argument("--port", default=9431)
parser.add_argument("--chunk-size", default=24, type=int)
parser.add_argument("--left-context-chunks", default=4, type=int)
parser.add_argument("--num-threads", default=None, type=int)
parser.add_argument("--verbose", "-v", default=False, action="store_true")
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.INFO)
logging.info("Loading libraries")
from speechbrain.inference.ASR import StreamingASR
from speechbrain.utils.dynamic_chunk_training import DynChunkTrainConfig
import torch
device = args.device
if args.num_threads is not None:
torch.set_num_threads(args.num_threads)
logging.info(f"Loading model from \"{args.model_source}\" onto device {device}")
asr = StreamingASR.from_hparams(args.model_source, run_opts={"device": device})
config = DynChunkTrainConfig(args.chunk_size, args.left_context_chunks)
logging.info(f"Starting stream from URI \"{args.audio_path}\"")
for text_chunk in asr.transcribe_file_streaming(args.audio_path, config):
print(text_chunk, flush=True, end="")
使用Gradio在瀏覽器中進行即時ASR解碼
from argparse import ArgumentParser
from dataclasses import dataclass
import logging
parser = ArgumentParser()
parser.add_argument("--model-source", required=True)
parser.add_argument("--device", default="cpu")
parser.add_argument("--ip", default="127.0.0.1")
parser.add_argument("--port", default=9431)
parser.add_argument("--chunk-size", default=24, type=int)
parser.add_argument("--left-context-chunks", default=4, type=int)
parser.add_argument("--num-threads", default=None, type=int)
parser.add_argument("--verbose", "-v", default=False, action="store_true")
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.INFO)
logging.info("Loading libraries")
from speechbrain.inference.ASR import StreamingASR, ASRStreamingContext
from speechbrain.utils.dynamic_chunk_training import DynChunkTrainConfig
import torch
import gradio as gr
import torchaudio
import numpy as np
device = args.device
if args.num_threads is not None:
torch.set_num_threads(args.num_threads)
logging.info(f"Loading model from \"{args.model_source}\" onto device {device}")
asr = StreamingASR.from_hparams(args.model_source, run_opts={"device": device})
config = DynChunkTrainConfig(args.chunk_size, args.left_context_chunks)
@dataclass
class GradioStreamingContext:
context: ASRStreamingContext
chunk_size: int
waveform_buffer: torch.Tensor
decoded_text: str
def transcribe(stream, new_chunk):
sr, y = new_chunk
y = y.astype(np.float32)
y = torch.tensor(y, dtype=torch.float32, device=device)
y /= max(1, torch.max(torch.abs(y)).item()) # norm by max abs() within chunk & avoid NaN
if len(y.shape) > 1:
y = torch.mean(y, dim=1) # downmix to mono
# HACK: we are making poor use of the resampler across chunk boundaries
# which may degrade accuracy.
# NOTE: we should also absolutely avoid recreating a resampler every time
resampler = torchaudio.transforms.Resample(orig_freq=sr, new_freq=asr.audio_normalizer.sample_rate).to(device)
y = resampler(y) # janky resample (probably to 16kHz)
if stream is None:
stream = GradioStreamingContext(
context=asr.make_streaming_context(config),
chunk_size=asr.get_chunk_size_frames(config),
waveform_buffer=y,
decoded_text="",
)
else:
stream.waveform_buffer = torch.concat((stream.waveform_buffer, y))
while stream.waveform_buffer.size(0) > stream.chunk_size:
chunk = stream.waveform_buffer[:stream.chunk_size]
stream.waveform_buffer = stream.waveform_buffer[stream.chunk_size:]
# fake batch dim
chunk = chunk.unsqueeze(0)
# list of transcribed strings, of size 1 because the batch size is 1
with torch.no_grad():
transcribed = asr.transcribe_chunk(stream.context, chunk)
stream.decoded_text += transcribed[0]
return stream, stream.decoded_text
# NOTE: latency seems relatively high, which may be due to this:
# https://github.com/gradio-app/gradio/issues/6526
demo = gr.Interface(
transcribe,
["state", gr.Audio(sources=["microphone"], streaming=True)],
["state", "text"],
live=True,
)
demo.launch(server_name=args.ip, server_port=args.port)
在GPU上進行推理
在調用 from_hparams
方法時,添加 run_opts={"device":"cuda"}
即可在GPU上進行推理。
批量並行推理
目前,高級轉錄接口不支持批量推理,但低級接口(如 encode_chunk
)支持。我們希望未來能提供高效的批量推理功能。
📚 詳細文檔
訓練步驟
該模型使用SpeechBrain(提交哈希:3f9e33a
)進行訓練。若要從頭開始訓練,請按以下步驟操作:
- 克隆SpeechBrain倉庫:
git clone https://github.com/speechbrain/speechbrain/
- 安裝SpeechBrain:
cd speechbrain
pip install -r requirements.txt
pip install -e .
- 運行訓練腳本:
cd recipes/LibriSpeech/ASR/transducer
python train.py hparams/conformer_transducer.yaml --data_folder=your_data_folder
更多詳細信息,請參閱 配方目錄。
侷限性
SpeechBrain團隊不保證該模型在其他數據集上的性能。
🔧 技術細節
本自動語音識別系統是一個Conformer模型,使用RNN - T損失(帶有輔助CTC損失以穩定訓練)進行訓練,採用一元分詞器。架構細節可在 訓練超參數文件 中查看。
流式支持利用了動態塊訓練技術。多頭注意力模塊使用分塊注意力,並採用了 動態塊卷積 的實現。模型在訓練時支持不同的塊大小(甚至全上下文),因此適用於各種塊大小和離線轉錄。
系統使用採樣率為16kHz(單聲道)的錄音進行訓練,調用 transcribe_file
時,代碼會根據需要自動對音頻進行歸一化處理(即重採樣 + 單聲道選擇)。
📄 許可證
本項目採用Apache 2.0許可證。
模型信息表格
屬性 | 詳情 |
---|---|
推理狀態 | 需要在Hugging Face中實現流式API |
支持語言 | 英文 |
縮略圖 | 無 |
標籤 | 自動語音識別、Transducer、Conformer、PyTorch、SpeechBrain |
許可證 | Apache 2.0 |
數據集 | LibriSpeech ASR |
評估指標 | 詞錯誤率(WER) |
模型評估結果
模型名稱 | 任務 | 數據集 | 評估指標 | 值 |
---|---|---|---|---|
SpeechBrain的Conformer - Transducer | 自動語音識別 | LibriSpeech(純淨數據) | 測試WER(非流式貪心解碼) | 2.72 |
SpeechBrain的Conformer - Transducer | 自動語音識別 | LibriSpeech(純淨數據) | 測試WER(960ms塊大小,4個左上下文塊) | 3.13 |
SpeechBrain的Conformer - Transducer | 自動語音識別 | LibriSpeech(其他數據) | 測試WER(非流式貪心解碼) | 6.47 |
引用
如果您在研究或業務中使用了SpeechBrain,請引用以下文獻:
@misc{speechbrain,
title={{SpeechBrain}: A General-Purpose Speech Toolkit},
author={Mirco Ravanelli and Titouan Parcollet and Peter Plantinga and Aku Rouhe and Samuele Cornell and Loren Lugosch and Cem Subakan and Nauman Dawalatabad and Abdelwahab Heba and Jianyuan Zhong and Ju-Chieh Chou and Sung-Lin Yeh and Szu-Wei Fu and Chien-Feng Liao and Elena Rastorgueva and François Grondin and William Aris and Hwidong Na and Yan Gao and Renato De Mori and Yoshua Bengio},
year={2021},
eprint={2106.04624},
archivePrefix={arXiv},
primaryClass={eess.AS},
note={arXiv:2106.04624}
}
關於SpeechBrain
- 官網:https://speechbrain.github.io/
- 代碼倉庫:https://github.com/speechbrain/speechbrain/
- Hugging Face頁面:https://huggingface.co/speechbrain/



