模型概述
模型特點
模型能力
使用案例
🚀 用於GigaSpeech的Conformer模型
本倉庫提供了在SpeechBrain中基於預訓練於GigaSpeech(英文,XL分割)的端到端系統進行自動語音識別所需的所有工具。為獲得更好的體驗,建議你進一步瞭解 SpeechBrain。 該模型在全上下文模式(非流式)下的性能如下:
發佈日期 | 測試字錯率 (WER) | GPU 配置 |
---|---|---|
2024-11-08 | 11.00% | 4xA100 40GB |
在流式模式下,不同塊大小在測試集上的結果如下:
全上下文 | cs=32 (1280ms) | 24 (960ms) | 16 (640ms) | 12 (480ms) | 8 (320ms) | |
---|---|---|---|---|---|---|
全上下文 | 11.00% | - | - | - | - | - |
16 | - | - | - | 11.70% | 11.84% | 12.14% |
8 | - | - | 11.50% | 11.72% | 11.88% | 12.28% |
4 | - | 11.40% | 11.53% | 11.81% | 12.03% | 12.64% |
2 | - | 11.46% | 11.67% | 12.03% | 12.43% | 13.25% |
1* | - | 11.59% | 11.85% | 12.39% | 12.93% | 14.13% |
(*: 模型從未在該設置下顯式訓練過)
在比較不同配置時,請記住測試集的語音片段長度是有限的。所有字錯率(WER)值均使用 此腳本 進行評估(直到我們集成更便捷的方式來評估不同條件下的流式WER)。
🚀 快速開始
本項目提供了在SpeechBrain中基於預訓練於GigaSpeech的端到端系統進行自動語音識別的工具。你可以按照以下步驟進行操作:
- 安裝SpeechBrain。
- 使用示例代碼轉錄自己的英文音頻文件。
- 可根據需求在GPU上進行推理或進行批量並行推理。
✨ 主要特性
- 多模式支持:支持全上下文模式和流式模式,適用於不同場景。
- 多塊大小適配:模型支持不同塊大小的訓練和推理,適用於各種流式和離線轉錄需求。
- 自動音頻處理:代碼會自動對音頻進行歸一化處理(重採樣 + 單聲道選擇)。
📦 安裝指南
安裝SpeechBrain
首先,請使用以下命令安裝SpeechBrain:
pip install speechbrain
建議你閱讀我們的教程,進一步瞭解 SpeechBrain。
💻 使用示例
基礎用法
轉錄自己的英文音頻文件:
from speechbrain.inference.ASR import StreamingASR
from speechbrain.utils.dynamic_chunk_training import DynChunkTrainConfig
asr_model = StreamingASR.from_hparams("speechbrain/asr-streaming-conformer-gigaspeech")
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表選擇適合你用例的值。
高級用法
命令行工具轉錄文件或即時流
# 從即時流(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-gigaspeech --device=cpu -v
# 從文件解碼
python3 asr.py some-english-speech.wav --model-source=speechbrain/asr-streaming-conformer-gigaspeech --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解碼
python3 gradio-asr.py --model-source speechbrain/asr-streaming-conformer-gigaspeech --ip=localhost --device=cpu
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
)支持。我們希望未來能提供更高效的批量推理功能。
📚 詳細文檔
管道描述
此自動語音識別(ASR)系統是一個基於Conformer模型的系統,使用RNN-T損失函數進行訓練(並使用輔助的CTC損失來穩定訓練過程)。該模型使用單字分詞器進行操作。架構細節在 訓練超參數文件 中有詳細描述。
流式支持採用了動態塊訓練(Dynamic Chunk Training)技術。多頭注意力模塊使用了分塊注意力機制,並採用了 動態塊卷積 的實現。該模型在訓練時支持不同的塊大小(甚至全上下文),因此適用於各種塊大小的流式處理和離線轉錄。
該系統使用採樣率為16kHz(單聲道)的錄音進行訓練。在調用 transcribe_file
時,如果需要,代碼會自動對音頻進行歸一化處理(即重採樣 + 單聲道選擇)。
訓練
該模型使用SpeechBrain v1.0.2
進行訓練。要從頭開始訓練該模型,請按照以下步驟操作:
- 克隆SpeechBrain倉庫:
git clone https://github.com/speechbrain/speechbrain/
- 安裝依賴:
cd speechbrain
pip install -r requirements.txt
pip install -e .
- 按照 README 中的步驟進行操作。
侷限性
SpeechBrain團隊不保證該模型在其他數據集上的性能。
🔧 技術細節
模型信息
屬性 | 詳情 |
---|---|
模型類型 | Conformer模型,使用RNN-T損失函數訓練,有輔助CTC損失 |
訓練數據 | speechcolab/gigaspeech |
評估指標
模型在GigaSpeech測試集上的評估指標為字錯率(WER),全上下文模式下測試WER為11.00%,流式模式下不同塊大小有不同的WER值。
技術實現
採用動態塊訓練技術實現流式支持,多頭注意力模塊使用分塊注意力,採用動態塊卷積實現。
📄 許可證
本項目採用Apache-2.0許可證。
關於SpeechBrain
- 網站:https://speechbrain.github.io/
- 代碼倉庫:https://github.com/speechbrain/speechbrain/
- HuggingFace頁面:https://huggingface.co/speechbrain/
引用SpeechBrain
如果你在研究或業務中使用了SpeechBrain,請引用以下文獻:
@misc{speechbrainV1,
title={Open-Source Conversational AI with SpeechBrain 1.0},
author={Mirco Ravanelli and Titouan Parcollet and Adel Moumen and Sylvain de Langen and Cem Subakan and Peter Plantinga and Yingzhi Wang and Pooneh Mousavi and Luca Della Libera and Artem Ploujnikov and Francesco Paissan and Davide Borra and Salah Zaiem and Zeyu Zhao and Shucong Zhang and Georgios Karakasidis and Sung-Lin Yeh and Pierre Champion and Aku Rouhe and Rudolf Braun and Florian Mai and Juan Zuluaga-Gomez and Seyed Mahed Mousavi and Andreas Nautsch and Xuechen Liu and Sangeet Sagar and Jarod Duret and Salima Mdhaffar and Gaelle Laperriere and Mickael Rouvier and Renato De Mori and Yannick Esteve},
year={2024},
eprint={2407.00463},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/2407.00463},
}
@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}
}



