模型简介
模型特点
模型能力
使用案例
🚀 语音识别模型Whisper
Whisper是一款用于自动语音识别(ASR)和语音翻译的预训练模型。它在68万小时的标注数据上进行训练,无需微调,就能在众多数据集和领域中展现出强大的泛化能力。
🚀 快速开始
Whisper是由OpenAI的Alec Radford等人在论文Robust Speech Recognition via Large-Scale Weak Supervision中提出的。原始代码仓库可在此处找到。与Whisper大模型相比,large-v2模型的训练轮数增加了2.5倍,并增加了正则化,性能有所提升。
✨ 主要特性
- 泛化能力强:在68万小时的标注语音数据上训练,无需微调即可在多个数据集和领域中表现出色。
- 多语言支持:有仅支持英语和多语言两种版本,多语言版本可进行语音识别和语音翻译。
- 多种模型尺寸:提供五种不同大小的模型配置,满足不同场景需求。
📚 详细文档
模型细节
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
一起使用。WhisperProcessor
用于预处理音频输入(将其转换为模型所需的对数梅尔频谱图)和后处理模型输出(将其从令牌转换为文本)。
上下文令牌的典型序列如下:
<|startoftranscript|> <|en|> <|transcribe|> <|notimestamps|>
这告诉模型以英语进行解码,执行语音识别任务,并且不预测时间戳。
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-large-v2")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v2")
>>> 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-large-v2")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v2")
>>> 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-large-v2")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v2")
>>> 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 Large模型
>>> 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-large-v2")
>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-large-v2").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.0003583080317572
长音频转录
>>> 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-large-v2",
>>> 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模型在不同数据集和领域中具有很强的泛化能力。然而,通过微调,可以进一步提高其在特定语言和任务上的预测能力。博客文章Fine-Tune Whisper with 🤗 Transformers提供了使用仅5小时标注数据微调Whisper模型的详细步骤。
📄 许可证
本模型采用Apache 2.0许可证。
训练数据
模型在从互联网收集的68万小时音频及相应转录文本上进行训练。其中65%(即43.8万小时)是英语音频和匹配的英语转录文本,约18%(即12.6万小时)是非英语音频和英语转录文本,最后的17%(即11.7万小时)是非英语音频和相应的转录文本。这些非英语数据代表了98种不同的语言。
性能和局限性
研究表明,与许多现有的ASR系统相比,该模型在口音、背景噪音、专业语言方面具有更强的鲁棒性,并且在多种语言到英语的零样本翻译中表现出色;语音识别和翻译的准确性接近当前的先进水平。
然而,由于模型是使用大规模噪声数据进行弱监督训练的,预测结果可能包含音频输入中实际未说出的文本(即幻觉现象)。此外,模型在不同语言上的表现不均衡,在资源较少和/或可发现性较低的语言上准确性较低。模型在特定语言的不同口音和方言上也表现出差异,可能导致不同性别、种族、年龄或其他人口统计学标准的说话者的单词错误率较高。
更广泛的影响
预计Whisper模型的转录能力可用于改进辅助工具。虽然Whisper模型本身不能直接用于实时转录,但其速度和规模表明,其他人可以基于它构建接近实时的语音识别和翻译应用程序。
发布Whisper也存在潜在的双重用途问题。虽然希望该技术主要用于有益目的,但使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}
}
重要提示
- 请勿使用Whisper模型在未经个人同意的情况下转录其录音,或声称使用这些模型进行任何主观分类。
- 不建议在高风险领域(如决策场景)中使用,因为准确性的缺陷可能导致结果出现明显缺陷。
- 模型旨在转录和翻译语音,将其用于分类不仅未经过评估,而且不合适,特别是用于推断人类属性。



