模型概述
模型特點
模型能力
使用案例
🚀 語音識別模型Whisper
Whisper是一款用於自動語音識別(ASR)和語音翻譯的預訓練模型。它在68萬小時的標註數據上進行訓練,無需微調,就能在眾多數據集和領域中展現出強大的泛化能力。
🚀 快速開始
Whisper是由OpenAI的Alec Radford等人在論文Robust Speech Recognition via Large-Scale Weak Supervision中提出的。原始代碼倉庫可在此處找到。與Whisper大模型相比,large-v2模型的訓練輪數增加了2.5倍,並增加了正則化,性能有所提升。
✨ 主要特性
- 泛化能力強:在68萬小時的標註語音數據上訓練,無需微調即可在多個數據集和領域中表現出色。
- 多語言支持:有僅支持英語和多語言兩種版本,多語言版本可進行語音識別和語音翻譯。
- 多種模型尺寸:提供五種不同大小的模型配置,滿足不同場景需求。
📚 詳細文檔
模型細節
Whisper是基於Transformer的編碼器 - 解碼器模型,也稱為“序列到序列”模型。它使用大規模弱監督對68萬小時的標註語音數據進行訓練。
模型分為僅英語數據訓練和多語言數據訓練兩種。僅英語模型用於語音識別任務,多語言模型則用於語音識別和語音翻譯。對於語音識別,模型預測與音頻相同語言的轉錄;對於語音翻譯,模型預測與音頻不同語言的轉錄。
Whisper檢查點有五種不同大小的配置。最小的四種在僅英語或多語言數據上訓練,最大的檢查點僅為多語言版本。所有十個預訓練檢查點都可在Hugging Face Hub上獲取。具體檢查點信息如下表所示:
大小 | 參數 | 僅英語版本 | 多語言版本 |
---|---|---|---|
tiny | 39 M | ✓ | ✓ |
base | 74 M | ✓ | ✓ |
small | 244 M | ✓ | ✓ |
medium | 769 M | ✓ | ✓ |
large | 1550 M | x | ✓ |
large-v2 | 1550 M | x | ✓ |
💻 使用示例
基礎用法
要轉錄音頻樣本,模型必須與WhisperProcessor
一起使用。WhisperProcessor
用於預處理音頻輸入(將其轉換為模型所需的對數梅爾頻譜圖)和後處理模型輸出(將其從令牌轉換為文本)。
上下文令牌的典型序列如下:
<|startoftranscript|> <|en|> <|transcribe|> <|notimestamps|>
這告訴模型以英語進行解碼,執行語音識別任務,並且不預測時間戳。
model.config.forced_decoder_ids = WhisperProcessor.get_decoder_prompt_ids(language="english", task="transcribe")
此代碼強制模型在語音識別任務下以英語進行預測。
高級用法
英語到英語轉錄
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import load_dataset
>>> # 加載模型和處理器
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-large-v2")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v2")
>>> model.config.forced_decoder_ids = None
>>> # 加載虛擬數據集並讀取音頻文件
>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
>>> sample = ds[0]["audio"]
>>> input_features = processor(sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt").input_features
>>> # 生成令牌ID
>>> predicted_ids = model.generate(input_features)
>>> # 解碼令牌ID為文本
>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=False)
['<|startoftranscript|><|en|><|transcribe|><|notimestamps|> Mr. Quilter is the apostle of the middle classes and we are glad to welcome his gospel.<|endoftext|>']
>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
[' Mr. Quilter is the apostle of the middle classes and we are glad to welcome his gospel.']
法語到法語轉錄
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import Audio, load_dataset
>>> # 加載模型和處理器
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-large-v2")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v2")
>>> forced_decoder_ids = processor.get_decoder_prompt_ids(language="french", task="transcribe")
>>> # 加載流式數據集並讀取第一個音頻樣本
>>> ds = load_dataset("common_voice", "fr", split="test", streaming=True)
>>> ds = ds.cast_column("audio", Audio(sampling_rate=16_000))
>>> input_speech = next(iter(ds))["audio"]
>>> input_features = processor(input_speech["array"], sampling_rate=input_speech["sampling_rate"], return_tensors="pt").input_features
>>> # 生成令牌ID
>>> predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
>>> # 解碼令牌ID為文本
>>> transcription = processor.batch_decode(predicted_ids)
['<|startoftranscript|><|fr|><|transcribe|><|notimestamps|> Un vrai travail intéressant va enfin être mené sur ce sujet.<|endoftext|>']
>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
[' Un vrai travail intéressant va enfin être mené sur ce sujet.']
法語到英語翻譯
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import Audio, load_dataset
>>> # 加載模型和處理器
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-large-v2")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v2")
>>> forced_decoder_ids = processor.get_decoder_prompt_ids(language="french", task="translate")
>>> # 加載流式數據集並讀取第一個音頻樣本
>>> ds = load_dataset("common_voice", "fr", split="test", streaming=True)
>>> ds = ds.cast_column("audio", Audio(sampling_rate=16_000))
>>> input_speech = next(iter(ds))["audio"]
>>> input_features = processor(input_speech["array"], sampling_rate=input_speech["sampling_rate"], return_tensors="pt").input_features
>>> # 生成令牌ID
>>> predicted_ids = model.generate(input_features, forced_decoder_ids=forced_decoder_ids)
>>> # 解碼令牌ID為文本
>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
[' A very interesting work, we will finally be given on this subject.']
評估Whisper Large模型
>>> from datasets import load_dataset
>>> from transformers import WhisperForConditionalGeneration, WhisperProcessor
>>> import torch
>>> from evaluate import load
>>> librispeech_test_clean = load_dataset("librispeech_asr", "clean", split="test")
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-large-v2")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v2").to("cuda")
>>> def map_to_pred(batch):
>>> audio = batch["audio"]
>>> input_features = processor(audio["array"], sampling_rate=audio["sampling_rate"], return_tensors="pt").input_features
>>> batch["reference"] = processor.tokenizer._normalize(batch['text'])
>>>
>>> with torch.no_grad():
>>> predicted_ids = model.generate(input_features.to("cuda"))[0]
>>> transcription = processor.decode(predicted_ids)
>>> batch["prediction"] = processor.tokenizer._normalize(transcription)
>>> return batch
>>> result = librispeech_test_clean.map(map_to_pred)
>>> wer = load("wer")
>>> print(100 * wer.compute(references=result["reference"], predictions=result["prediction"]))
3.0003583080317572
長音頻轉錄
>>> import torch
>>> from transformers import pipeline
>>> from datasets import load_dataset
>>> device = "cuda:0" if torch.cuda.is_available() else "cpu"
>>> pipe = pipeline(
>>> "automatic-speech-recognition",
>>> model="openai/whisper-large-v2",
>>> chunk_length_s=30,
>>> device=device,
>>> )
>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
>>> sample = ds[0]["audio"]
>>> prediction = pipe(sample.copy(), batch_size=8)["text"]
" Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel."
>>> # 也可以返回預測的時間戳
>>> prediction = pipe(sample.copy(), batch_size=8, return_timestamps=True)["chunks"]
[{'text': ' Mr. Quilter is the apostle of the middle classes and we are glad to welcome his gospel.',
'timestamp': (0.0, 5.44)}]
🔧 技術細節
微調
預訓練的Whisper模型在不同數據集和領域中具有很強的泛化能力。然而,通過微調,可以進一步提高其在特定語言和任務上的預測能力。博客文章Fine-Tune Whisper with 🤗 Transformers提供了使用僅5小時標註數據微調Whisper模型的詳細步驟。
📄 許可證
本模型採用Apache 2.0許可證。
訓練數據
模型在從互聯網收集的68萬小時音頻及相應轉錄文本上進行訓練。其中65%(即43.8萬小時)是英語音頻和匹配的英語轉錄文本,約18%(即12.6萬小時)是非英語音頻和英語轉錄文本,最後的17%(即11.7萬小時)是非英語音頻和相應的轉錄文本。這些非英語數據代表了98種不同的語言。
性能和侷限性
研究表明,與許多現有的ASR系統相比,該模型在口音、背景噪音、專業語言方面具有更強的魯棒性,並且在多種語言到英語的零樣本翻譯中表現出色;語音識別和翻譯的準確性接近當前的先進水平。
然而,由於模型是使用大規模噪聲數據進行弱監督訓練的,預測結果可能包含音頻輸入中實際未說出的文本(即幻覺現象)。此外,模型在不同語言上的表現不均衡,在資源較少和/或可發現性較低的語言上準確性較低。模型在特定語言的不同口音和方言上也表現出差異,可能導致不同性別、種族、年齡或其他人口統計學標準的說話者的單詞錯誤率較高。
更廣泛的影響
預計Whisper模型的轉錄能力可用於改進輔助工具。雖然Whisper模型本身不能直接用於即時轉錄,但其速度和規模表明,其他人可以基於它構建接近即時的語音識別和翻譯應用程序。
發佈Whisper也存在潛在的雙重用途問題。雖然希望該技術主要用於有益目的,但使ASR技術更易於獲取可能會使更多人能夠構建強大的監控技術或擴大現有監控工作的規模。此外,這些模型可能具有直接識別特定個人的能力,這帶來了與雙重用途和不同性能相關的安全問題。
BibTeX引用
@misc{radford2022whisper,
doi = {10.48550/ARXIV.2212.04356},
url = {https://arxiv.org/abs/2212.04356},
author = {Radford, Alec and Kim, Jong Wook and Xu, Tao and Brockman, Greg and McLeavey, Christine and Sutskever, Ilya},
title = {Robust Speech Recognition via Large-Scale Weak Supervision},
publisher = {arXiv},
year = {2022},
copyright = {arXiv.org perpetual, non-exclusive license}
}
重要提示
- 請勿使用Whisper模型在未經個人同意的情況下轉錄其錄音,或聲稱使用這些模型進行任何主觀分類。
- 不建議在高風險領域(如決策場景)中使用,因為準確性的缺陷可能導致結果出現明顯缺陷。
- 模型旨在轉錄和翻譯語音,將其用於分類不僅未經過評估,而且不合適,特別是用於推斷人類屬性。



