模型简介
模型特点
模型能力
使用案例
🚀 语音识别模型Whisper
Whisper是一个用于自动语音识别(ASR)和语音翻译的预训练模型。该模型在68万小时的标注数据上进行训练,无需微调,就能在许多数据集和领域中展现出强大的泛化能力。
🚀 快速开始
转录音频示例
>>> from transformers import WhisperProcessor, WhisperForConditionalGeneration
>>> from datasets import load_dataset
>>> # 加载模型和处理器
>>> processor = WhisperProcessor.from_pretrained("openai/whisper-small.en")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small.en")
>>> # 加载虚拟数据集并读取音频文件
>>> 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|><|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
从转录开头移除上下文令牌。
评估示例
以下代码片段展示了如何在 LibriSpeech test-clean 上评估 Whisper small.en:
>>> 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.en")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small.en").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.053161596922323
长格式转录示例
>>> 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.en",
>>> 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。
✨ 主要特性
- 强大的泛化能力:在68万小时的标注语音数据上训练,无需微调即可在多个数据集和领域中表现出色。
- 多语言支持:有仅支持英语的模型,也有多语言模型,可用于语音识别和语音翻译。
- 多种模型大小可选:提供五种不同大小配置的检查点,满足不同场景需求。
📦 安装指南
文档中未提及具体安装步骤,可参考 Hugging Face 相关库的安装方法进行安装。
💻 使用示例
基础用法
# 转录音频示例
from transformers import WhisperProcessor, WhisperForConditionalGeneration
from datasets import load_dataset
# 加载模型和处理器
processor = WhisperProcessor.from_pretrained("openai/whisper-small.en")
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small.en")
# 加载虚拟数据集并读取音频文件
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)
print(transcription)
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
print(transcription)
高级用法
# 长格式转录示例
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.en",
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"]
print(prediction)
# 返回预测的时间戳
prediction = pipe(sample.copy(), batch_size=8, return_timestamps=True)["chunks"]
print(prediction)
📚 详细文档
模型详情
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 | ✓ |
训练数据
模型在从互联网收集的68万小时音频及相应转录上进行训练。其中65%(即43.8万小时)是英语音频和匹配的英语转录,约18%(即12.6万小时)是非英语音频和英语转录,最后17%(即11.7万小时)是非英语音频和相应的转录。这些非英语数据代表了98种不同的语言。
正如 随附论文 中所讨论的,我们发现给定语言的转录性能与该语言的训练数据量直接相关。
性能和局限性
研究表明,与许多现有的ASR系统相比,该模型在口音、背景噪音、专业语言方面表现出更强的鲁棒性,并且在从多种语言到英语的零样本翻译方面表现出色;语音识别和翻译的准确性接近当前的先进水平。
然而,由于模型是使用大规模噪声数据进行弱监督训练的,预测结果可能包含音频输入中实际未说出的文本(即幻觉)。我们推测这是因为模型结合了根据语言常识预测音频中的下一个单词和转录音频本身的任务。
模型在不同语言上的表现参差不齐,对于资源较少和/或可发现性较低的语言,或者训练数据较少的语言,我们观察到其准确性较低。模型在特定语言的不同口音和方言上也表现出差异,这可能包括不同性别、种族、年龄或其他人口统计标准的说话者之间较高的单词错误率。完整的评估结果见 本次发布的随附论文。
此外,模型的序列到序列架构使其容易生成重复文本,尽管可以通过束搜索和温度调度在一定程度上缓解,但无法完全解决。论文 中对这些局限性进行了进一步分析。在资源较少和/或可发现性较低的语言上,这种行为和幻觉可能会更严重。
更广泛的影响
我们预计Whisper模型的转录能力可用于改进辅助工具。虽然Whisper模型本身不能直接用于实时转录,但其速度和规模表明,其他人可以基于它们构建允许接近实时语音识别和翻译的应用程序。基于Whisper模型构建的有益应用的真正价值表明,这些模型的不同表现可能会产生实际的经济影响。
发布Whisper也带来了潜在的双重用途担忧。虽然我们希望该技术主要用于有益目的,但使ASR技术更易于获取可能会使更多参与者能够构建强大的监控技术或扩大现有的监控工作,因为其速度和准确性允许对大量音频通信进行经济实惠的自动转录和翻译。此外,这些模型可能具有直接识别特定个人的能力,这反过来又带来了与双重用途和不同表现相关的安全问题。实际上,我们预计转录成本不是扩大监控项目的限制因素。
评估使用
这些模型的主要目标用户是研究当前模型的鲁棒性、泛化能力、性能、偏差和局限性的AI研究人员。然而,Whisper作为一种ASR解决方案对开发者也可能非常有用,特别是对于英语语音识别。我们认识到,一旦模型发布,就不可能将访问限制在“预期”用途上,也难以制定合理的准则来界定什么是研究,什么不是研究。
模型主要在ASR和英语语音翻译任务上进行训练和评估。它们在约10种语言的ASR任务中表现出色。它们可能具有其他能力,特别是在针对某些任务(如语音活动检测、说话人分类或说话人分离)进行微调时,但在这些领域尚未进行全面评估。我们强烈建议用户在特定上下文和领域中对模型进行全面评估后再进行部署。
特别是,我们警告不要使用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}
}
🔧 技术细节
微调
预训练的Whisper模型在不同数据集和领域中表现出强大的泛化能力。然而,通过 微调,可以进一步提高其在特定语言和任务上的预测能力。博客文章 Fine-Tune Whisper with 🤗 Transformers 提供了一个逐步指南,介绍如何使用低至5小时的标注数据对Whisper模型进行微调。
📄 许可证
本项目采用 apache-2.0
许可证。



