模型简介
模型特点
模型能力
使用案例
语言:
- 英语 标签:
- 音频
- 自动语音识别
- 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-tiny.en
结果:
- 任务:
名称: 自动语音识别
类型: automatic-speech-recognition
数据集:
名称: LibriSpeech (干净)
类型: librispeech_asr
配置: clean
分割: test
参数:
语言: en
指标:
- 名称: 测试WER 类型: wer 值: 8.4372112320138
- 任务:
名称: 自动语音识别
类型: automatic-speech-recognition
数据集:
名称: LibriSpeech (其他)
类型: librispeech_asr
配置: other
分割: test
参数:
语言: en
指标:
- 名称: 测试WER 类型: wer 值: 14.857607503498355 流水线标签: 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提供五种不同规模的配置。前四种小规模模型有仅英语和多语言版本,大规模模型仅有多语言版本。所有预训练模型均可在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-tiny.en")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.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 tiny.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-tiny.en")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.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"]))
5.655609406528749
长音频转录
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-tiny.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研究人员。同时,Whisper也可作为英语语音识别解决方案供开发者使用。模型在约10种语言的ASR任务中表现优异,但在语音活动检测、说话人分类等任务需额外评估。
特别提醒:禁止未经同意转录个人录音或用于主观分类。高风险领域(如决策场景)不建议使用,因为准确性缺陷可能导致严重后果。模型设计用途仅为语音转录和翻译,不适用于人类属性推断。
训练数据
模型训练使用68万小时互联网音频及对应文本。其中65%(43.8万小时)为英语音频+文本,18%(12.6万小时)为非英语音频+英语文本,17%(11.7万小时)为非英语音频+对应语言文本,涵盖98种语言。如论文所述,特定语言的转录性能与训练数据量直接相关。
性能与局限
相比现有ASR系统,模型在口音、背景噪声、专业术语等方面表现更鲁棒,零样本翻译能力接近最先进水平。但由于采用弱监督训练,可能出现幻听文本(即输出未在音频中出现的内容)。不同语言表现差异显著,低资源语言准确率较低,不同口音和方言的表现也存在差异。完整评估结果详见论文。
序列到序列架构可能导致重复文本生成,虽可通过束搜索和温度调度缓解,但无法完全消除。低资源语言的幻听现象可能更严重。
社会影响
预期可用于改进无障碍工具。虽然原生不支持实时转录,但其速度和规模使开发者能构建近实时应用。性能差异可能带来实际经济影响。
同时存在双重用途风险:一方面可赋能监控技术,自动转录大规模音频;另一方面可能具备说话人识别能力,引发安全和公平性担忧。实践中,转录成本通常不是监控项目扩展的主要限制因素。
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}
}



