模型简介
模型特点
模型能力
使用案例
语言:
- 英语 标签:
- 音频
- 自动语音识别
- hf-asr排行榜 小部件:
- 示例标题: Librispeech样本1 来源: https://cdn-media.huggingface.co/speech_samples/sample1.flac
- 示例标题: Librispeech样本2 来源: https://cdn-media.huggingface.co/speech_samples/sample2.flac 模型索引:
- 名称: whisper-small.en
结果:
- 任务:
名称: 自动语音识别
类型: automatic-speech-recognition
数据集:
名称: LibriSpeech (干净)
类型: librispeech_asr
配置: clean
拆分: test
参数:
语言: en
指标:
- 名称: 测试WER 类型: wer 值:
- 任务:
名称: 自动语音识别
类型: automatic-speech-recognition
数据集:
名称: LibriSpeech (其他)
类型: librispeech_asr
配置: other
拆分: test
参数:
语言: en
指标:
- 名称: 测试WER 类型: wer 值: 管道标签: automatic-speech-recognition 许可证: apache-2.0
- 任务:
名称: 自动语音识别
类型: automatic-speech-recognition
数据集:
名称: LibriSpeech (干净)
类型: librispeech_asr
配置: clean
拆分: test
参数:
语言: en
指标:
Whisper
Whisper是一个预训练的自动语音识别(ASR)和语音翻译模型。在68万小时的标注数据上训练后,Whisper模型展现出强大的泛化能力,无需微调即可适应多种数据集和领域。
Whisper由OpenAI的Alec Radford等人在论文通过大规模弱监督实现鲁棒语音识别中提出。原始代码仓库可在此处找到。
免责声明: 本模型卡内容部分由Hugging Face团队撰写,部分内容复制自原始模型卡。
模型详情
Whisper是一个基于Transformer的编码器-解码器模型,也称为序列到序列模型。它使用大规模弱监督标注的68万小时语音数据进行训练。
模型分为仅英语数据和多语言数据训练版本。仅英语模型专用于语音识别任务,多语言模型则同时训练语音识别和语音翻译能力。语音识别时,模型预测与音频同语言的文本;语音翻译时,预测不同语言的文本。
Whisper提供五种不同规模的预训练配置,其中四种小规模模型有英语和 multilingual 版本,大规模模型仅 multilingual 版本。所有预训练模型均可在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
使用,该处理器负责:
- 音频预处理(转换为对数梅尔频谱)
- 输出后处理(将标记转换为文本)
转录示例
>>> 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)
>>> # 解码为文本
>>> 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
长音频转录
Whisper原生支持30秒以内音频,但通过分块算法可处理任意长度音频。使用Transformers的pipeline
并设置chunk_length_s=30
即可启用分块处理,还支持批量推理和时间戳预测:
>>> 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分块处理了解分块算法细节。
微调
虽然预训练模型已展现强大泛化能力,但通过微调可进一步提升特定语言和任务的表现。博客文章使用🤗 Transformers微调Whisper提供了仅需5小时标注数据的微调指南。
使用评估
主要目标用户是研究模型鲁棒性、泛化能力和局限性的AI研究人员,同时也适用于英语语音识别开发者。需注意模型发布后无法限制使用场景。
模型主要评估ASR和英译任务,在约10种语言上表现良好。虽可能通过微调实现说话人分类等功能,但未经严格评估。建议部署前进行领域特定评估。
特别警示:禁止未经同意转录个人录音或用于主观分类。高风险领域(如决策场景)不建议使用,模型设计用途仅为语音转录/翻译,不适用于属性推断。
训练数据
模型训练使用68万小时互联网采集的音频及文本,其中:
- 65%(43.8万小时)为英语音频及对应文本
- 18%(12.6万小时)为非英语音频配英文字幕
- 17%(11.7万小时)为98种非英语语言数据
如论文所述,特定语言的转录性能与其训练数据量直接相关。
性能与局限
相比现有ASR系统,模型在口音、噪声、术语等方面表现更鲁棒,零样本翻译能力接近SOTA。但由于弱监督训练特性,可能出现幻听文本(hallucination),推测是模型在预测音频内容时结合了语言知识所致。
不同语言表现不均,低资源语言准确率较低。同一语言内不同口音/方言(涉及性别、种族、年龄等)也存在差异,完整评估见论文。
序列到序列架构可能导致重复文本,虽可通过束搜索缓解但无法根除,低资源语言中此类问题可能更显著。
社会影响
预期可用于提升辅助工具,虽然原生不支持实时转录,但其速度与体积为近实时应用开发提供可能。性能差异可能带来实际经济影响。
同时需警惕双刃剑效应:技术普及可能降低监控成本,使大规模音频监控更易实施。模型可能具备说话人识别能力,带来安全隐忧。但实践中转录成本并非监控项目扩展的主要限制因素。
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}
}



