模型简介
模型特点
模型能力
使用案例
🚀 语音识别模型Whisper
Whisper是一款用于自动语音识别(ASR)和语音翻译的预训练模型。它在68万小时的标注数据上进行训练,无需微调,就能在众多数据集和领域中展现出强大的泛化能力。
🚀 快速开始
Whisper是OpenAI的Alec Radford等人在论文 Robust Speech Recognition via Large-Scale Weak Supervision 中提出的自动语音识别和语音翻译预训练模型。原始代码仓库可在 此处 找到。
支持语言
- en、zh、de、es、ru、ko、fr、ja、pt、tr、pl、ca、nl、ar、sv、it、id、hi、fi、vi、he、uk、el、ms、cs、ro、da、hu、ta、'no'、th、ur、hr、bg、lt、la、mi、ml、cy、sk、te、fa、lv、bn、sr、az、sl、kn、et、mk、br、eu、is、hy、ne、mn、bs、kk、sq、sw、gl、mr、pa、si、km、sn、yo、so、af、oc、ka、be、tg、sd、gu、am、yi、lo、uz、fo、ht、ps、tk、nn、mt、sa、lb、my、bo、tl、mg、as、tt、haw、ln、ha、ba、jw、su
模型示例
模型指标
任务 | 数据集 | 指标 | 值 |
---|---|---|---|
自动语音识别 | LibriSpeech (clean) | 测试词错误率 (WER) | 3.432213777886737 |
自动语音识别 | LibriSpeech (other) | 测试词错误率 (WER) | 7.628304527060248 |
自动语音识别 | Common Voice 11.0 | 测试词错误率 (WER) | 87.3 |
自动语音识别 | Common Voice 13.0 | 词错误率 (WER) | 125.69809089960707 |
✨ 主要特性
- 多语言支持:支持多种语言的语音识别和翻译。
- 弱监督训练:使用大规模弱监督标注的68万小时语音数据进行训练。
- 多任务处理:可同时处理语音识别和语音翻译任务。
- 多种模型大小:提供五种不同大小配置的检查点,满足不同需求。
📚 详细文档
模型详情
Whisper是基于Transformer的编码器 - 解码器模型,也称为 序列到序列 模型。它使用大规模弱监督对68万小时的标注语音数据进行训练。
模型分为仅英语数据训练和多语言数据训练两种。仅英语模型用于语音识别任务,多语言模型则同时用于语音识别和语音翻译任务。对于语音识别,模型预测与音频相同语言的转录文本;对于语音翻译,模型预测与音频不同语言的转录文本。
Whisper检查点有五种不同大小的配置,其中最小的四种可在仅英语或多语言数据上训练,最大的检查点仅支持多语言。所有十个预训练检查点都可在 Hugging Face Hub 上获取。具体信息如下表所示:
大小 | 参数 | 仅英语模型 | 多语言模型 |
---|---|---|---|
tiny | 39 M | 链接 | 链接 |
base | 74 M | 链接 | 链接 |
small | 244 M | 链接 | 链接 |
medium | 769 M | 链接 | 链接 |
large | 1550 M | 无 | 链接 |
large-v2 | 1550 M | 无 | 链接 |
使用方法
要转录音频样本,模型需要与 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")
这将强制模型以英语执行语音识别任务。
转录示例
英语到英语
在此示例中,上下文令牌为“非强制”,即模型自动预测输出语言(英语)和任务(转录)。
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import load_dataset
>>> # 加载模型和处理器
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-small")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
>>> 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.']
通过设置 skip_special_tokens=True
可以从转录开头移除上下文令牌。
法语到法语
以下示例通过适当设置解码器ID,演示了法语到法语的转录。
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import Audio, load_dataset
>>> # 加载模型和处理器
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-small")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
>>> 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.']
翻译示例
将任务设置为“translate”可强制Whisper模型执行语音翻译。
法语到英语
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import Audio, load_dataset
>>> # 加载模型和处理器
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-small")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
>>> 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.']
评估示例
以下代码展示了如何在 LibriSpeech test-clean 上评估Whisper Small:
>>> 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-small")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small").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.432213777886737
长音频转录
Whisper模型本质上设计用于处理最长30秒的音频样本。但是,通过使用分块算法,可以用于转录任意长度的音频样本。这可以通过Transformers的 pipeline
方法实现。在实例化管道时设置 chunk_length_s=30
可启用分块。启用分块后,管道可以进行批量推理,还可以通过传递 return_timestamps=True
扩展以预测序列级时间戳:
>>> 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-small",
>>> 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)}]
有关分块算法的更多详细信息,请参考博客文章 ASR Chunking。
微调
预训练的Whisper模型在不同数据集和领域中具有很强的泛化能力。但是,通过 微调,可以进一步提高其在某些语言和任务上的预测能力。博客文章 Fine-Tune Whisper with ü§ó Transformers 提供了使用仅5小时标注数据微调Whisper模型的详细步骤。
评估使用
这些模型的主要目标用户是研究当前模型的鲁棒性、泛化能力、性能、偏差和局限性的AI研究人员。然而,Whisper作为一种自动语音识别解决方案,对开发者也非常有用,特别是在英语语音识别方面。
模型主要在自动语音识别和语音翻译为英语的任务上进行训练和评估,在约10种语言中表现出较强的自动语音识别能力。它们可能具有其他功能,特别是在针对某些任务(如语音活动检测、说话人分类或说话人分割)进行微调时,但在这些领域尚未进行全面评估。强烈建议用户在特定上下文和领域中对模型进行全面评估后再进行部署。
特别要注意,请勿在未经个人同意的情况下使用Whisper模型转录其录音,也不要将这些模型用于任何主观分类。不建议在高风险领域(如决策场景)中使用,因为准确性的缺陷可能导致结果出现明显偏差。这些模型旨在转录和翻译语音,将其用于分类不仅未经过评估,而且不合适,特别是用于推断人类属性时。
训练数据
模型在从互联网收集的68万小时音频及其对应的转录文本上进行训练。其中,65%(约43.8万小时)是英语音频和匹配的英语转录文本,约18%(约12.6万小时)是非英语音频和英语转录文本,最后17%(约11.7万小时)是非英语音频及其对应的转录文本,这些非英语数据涵盖98种不同语言。
正如 随附论文 中所讨论的,特定语言的转录性能与该语言的训练数据量直接相关。
性能和局限性
研究表明,与许多现有的自动语音识别系统相比,这些模型在口音、背景噪音、专业语言方面具有更强的鲁棒性,并且在多语言到英语的零样本翻译方面表现出色;语音识别和翻译的准确性接近当前的先进水平。
然而,由于模型是使用大规模噪声数据进行弱监督训练的,预测结果可能包含音频输入中实际未说出的文本(即幻觉)。推测这是因为模型结合了根据语言常识预测音频中的下一个单词和转录音频本身的任务。
模型在不同语言上的表现不均衡,在资源较少和/或可发现性较低的语言或训练数据较少的语言上,准确性较低。此外,模型在特定语言的不同口音和方言上也表现出差异,可能导致不同性别、种族、年龄或其他人口统计标准的说话者的词错误率较高。完整的评估结果见 本次发布的随附论文。
此外,模型的序列到序列架构使其容易生成重复文本,尽管可以通过束搜索和温度调度在一定程度上缓解,但无法完全消除。论文 对这些局限性进行了进一步分析。在资源较少和/或可发现性较低的语言上,这种行为和幻觉可能更严重。
更广泛的影响
预计Whisper模型的转录能力可用于改进辅助工具。虽然Whisper模型本身不能直接用于实时转录,但其速度和规模表明,其他人可以在此基础上构建接近实时的语音识别和翻译应用程序。基于Whisper模型构建的有益应用的实际价值表明,这些模型的不同性能可能具有实际的经济影响。
发布Whisper也存在潜在的双重用途问题。虽然希望该技术主要用于有益目的,但使自动语音识别技术更易于获取可能会使更多人能够构建强大的监控技术或扩大现有监控工作,因为其速度和准确性使得大量音频通信的自动转录和翻译变得经济可行。此外,这些模型可能具有直接识别特定个人的能力,这反过来又带来了与双重用途和不同性能相关的安全问题。实际上,预计转录成本不是扩大监控项目的限制因素。
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}
}
📄 许可证
本项目采用 Apache-2.0 许可证。



