模型简介
模型特点
模型能力
使用案例
🚀 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
。



