模型概述
模型特點
模型能力
使用案例
🚀 Whisper
Whisper是一個用於自動語音識別(ASR)和語音翻譯的預訓練模型。該模型在68萬小時的標註數據上進行訓練,能夠在無需微調的情況下,很好地泛化到許多數據集和領域。
Whisper由OpenAI的Alec Radford等人在論文Robust Speech Recognition via Large-Scale Weak Supervision中提出。原始代碼倉庫可在這裡找到。
免責聲明:此模型卡片的部分內容由Hugging Face團隊編寫,部分內容從原始模型卡片複製粘貼而來。
✨ 主要特性
- 基於Transformer的編碼器 - 解碼器架構,即所謂的_序列到序列_模型。
- 支持多語言的自動語音識別和語音翻譯任務。
- 提供多種不同大小配置的預訓練模型,可根據需求選擇。
- 可通過上下文令牌控制輸出語言和任務。
- 支持長音頻轉錄,可處理任意長度的音頻。
📦 安裝指南
文檔中未提及安裝步驟,故跳過此章節。
💻 使用示例
基礎用法
以下是使用Whisper進行英語轉錄的示例,上下文令牌為“非強制”,模型將自動預測輸出語言(英語)和任務(轉錄):
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import load_dataset
>>> # 加載模型和處理器
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-tiny")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
>>> 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-tiny")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
>>> 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-tiny")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny")
>>> 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 Tiny模型
>>> 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-tiny")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny").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"]))
7.547098647858638
長音頻轉錄
>>> 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-tiny",
>>> 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是一個基於Transformer的編碼器 - 解碼器模型,也稱為_序列到序列_模型。它在68萬小時的標註語音數據上進行訓練,這些數據使用大規模弱監督進行標註。
模型分為僅英語數據訓練和多語言數據訓練兩種。僅英語模型針對語音識別任務進行訓練,而多語言模型則針對語音識別和語音翻譯任務進行訓練。對於語音識別,模型會預測與音頻相同語言的轉錄文本;對於語音翻譯,模型會預測與音頻不同語言的轉錄文本。
Whisper有五種不同大小配置的檢查點。其中最小的四個檢查點可以在僅英語數據或多語言數據上訓練,而最大的檢查點僅支持多語言。所有十個預訓練檢查點都可以在Hugging Face Hub上找到。以下表格總結了這些檢查點,並提供了它們在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|>
用於語音識別,<|translate|>
用於語音翻譯 - 此外,如果模型不應包含時間戳預測,則添加
<|notimestamps|>
令牌
因此,典型的上下文令牌序列可能如下所示:
<|startoftranscript|> <|en|> <|transcribe|> <|notimestamps|>
這告訴模型以英語進行解碼,執行語音識別任務,並且不預測時間戳。
這些令牌可以是強制的或非強制的。如果是強制的,模型將在每個位置預測每個令牌,這允許控制Whisper模型的輸出語言和任務。如果是非強制的,Whisper模型將自動預測輸出語言和任務。
可以相應地設置上下文令牌:
model.config.forced_decoder_ids = WhisperProcessor.get_decoder_prompt_ids(language="english", task="transcribe")
這將強制模型在語音識別任務中以英語進行預測。
評估和微調
文檔中還介紹瞭如何評估Whisper Tiny模型,以及如何通過微調進一步提高模型在特定語言和任務上的預測能力。具體可參考Fine-Tune Whisper with 🤗 Transformers博客文章,該文章提供了使用僅5小時標註數據微調Whisper模型的分步指南。
評估使用
這些模型的主要目標用戶是研究當前模型的魯棒性、泛化能力、功能、偏差和限制的AI研究人員。然而,Whisper作為一種ASR解決方案,對開發者也可能非常有用,特別是對於英語語音識別。
模型主要在ASR和英語語音翻譯任務上進行訓練和評估,在約10種語言中顯示出強大的ASR結果。它們可能具有其他功能,特別是在針對某些任務(如語音活動檢測、說話人分類或說話人分離)進行微調時,但在這些領域尚未進行全面評估。強烈建議用戶在特定上下文和領域中對模型進行全面評估後再進行部署。
特別提醒,請勿在未經個人同意的情況下使用Whisper模型轉錄其錄音,或聲稱使用這些模型進行任何主觀分類。不建議在高風險領域(如決策環境)中使用,因為準確性的缺陷可能導致結果出現明顯缺陷。模型旨在轉錄和翻譯語音,將其用於分類不僅未經過評估,而且不合適,特別是用於推斷人類屬性。
訓練數據
模型在從互聯網收集的68萬小時音頻及其相應轉錄文本上進行訓練。其中65%(即43.8萬小時)是英語音頻和匹配的英語轉錄文本,約18%(即12.6萬小時)是非英語音頻和英語轉錄文本,最後的17%(即11.7萬小時)是非英語音頻及其相應的轉錄文本。這些非英語數據代表了98種不同的語言。
如隨附論文中所述,給定語言的轉錄性能與該語言的訓練數據量直接相關。
性能和侷限性
研究表明,與許多現有的ASR系統相比,這些模型在口音、背景噪音、專業語言方面表現出更強的魯棒性,並且能夠實現從多種語言到英語的零樣本翻譯;語音識別和翻譯的準確性接近當前的先進水平。
然而,由於模型是使用大規模噪聲數據進行弱監督訓練的,預測結果可能包含音頻輸入中實際未說出的文本(即幻覺)。推測這是因為模型在嘗試預測音頻中的下一個單詞時,會結合其對語言的一般知識,同時嘗試轉錄音頻本身。
模型在不同語言上的表現參差不齊,在資源較少和/或可發現性較低的語言,或訓練數據較少的語言上,準確性較低。此外,模型在特定語言的不同口音和方言上也表現出差異,這可能包括不同性別、種族、年齡或其他人口統計學標準的說話者之間的較高詞錯誤率。完整的評估結果見本次發佈的隨附論文。
此外,模型的序列到序列架構使其容易生成重複文本,儘管可以通過束搜索和溫度調度在一定程度上緩解,但無法完全解決。論文中對這些侷限性進行了進一步分析。在資源較少和/或可發現性較低的語言上,這種行為和幻覺可能會更嚴重。
更廣泛的影響
預計Whisper模型的轉錄功能可用於改進輔助工具。雖然Whisper模型本身不能直接用於即時轉錄,但其速度和大小表明,其他人可以在其基礎上構建允許接近即時語音識別和翻譯的應用程序。基於Whisper模型構建的有益應用程序的實際價值表明,這些模型的不同表現可能會產生實際的經濟影響。
發佈Whisper也存在潛在的雙重用途問題。雖然希望該技術主要用於有益目的,但使ASR技術更易於獲取可能會使更多人能夠構建強大的監控技術或擴大現有的監控工作,因為其速度和準確性允許對大量音頻通信進行經濟實惠的自動轉錄和翻譯。此外,這些模型可能具有直接識別特定個人的能力,這反過來又帶來了與雙重用途和不同表現相關的安全問題。實際上,預計轉錄成本不是擴大監控項目的限制因素。
引用信息
@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}
}
🔧 技術細節
文檔中未提供足夠的技術實現細節,故跳過此章節。
📄 許可證
該模型使用的許可證為apache - 2.0
。



