openai/whisper-large-v3
摘要
OpenAI 发布了 Whisper large-v3,这是一个更新后的自动语音识别模型,使用 128 个梅尔频带和一个新的粤语词元,在 500 万小时的数据上训练,错误率较 large-v2 降低 10-20%。
查看缓存全文
缓存时间: 2026/08/11 08:03
openai/whisper-large-v3 · Hugging Face
来源:https://huggingface.co/openai/whisper-large-v3 Whisper 是一种用于自动语音识别(ASR)和语音翻译的最先进模型,由 OpenAI 的 Alec Radford 等人提出,论文为《通过大规模弱监督实现稳健语音识别》(https://huggingface.co/papers/2212.04356)。该模型在超过 5M 小时的有标注数据上训练,展现出在零样本设置下对许多数据集和领域的强大泛化能力。
Whisper large-v3 与之前的 large (https://huggingface.co/openai/whisper-large) 和 large-v2 (https://huggingface.co/openai/whisper-large-v2) 模型具有相同的架构,但有以下细微差别:
- 声谱图输入使用 128 个 Mel 频率通道,而不是 80 个
- 新增了粤语的语言标记
Whisper large-v3 模型在 100 万小时弱标注音频和 400 万小时使用 Whisper large-v2 (https://huggingface.co/openai/whisper-large-v2) 收集的伪标注音频上进行了训练。该模型在该混合数据集上训练了 2.0 个 epoch。
与 Whisper large-v2 (https://huggingface.co/openai/whisper-large-v2) 相比,large-v3 模型在多种语言上展现出更优的性能,错误率降低了 10% 到 20%。有关可用不同检查点的更多详细信息,请参阅模型详情 (https://huggingface.co/openai/whisper-large-v3#model-details) 部分。
免责声明:此模型卡的内容部分由 🤗 Hugging Face 团队撰写,部分复制并粘贴自原始模型卡。
https://huggingface.co/openai/whisper-large-v3#usage 用法
Hugging Face 🤗 Transformers 支持 Whisper large-v3。要运行该模型,首先安装 Transformers 库。在此示例中,我们还会安装 🤗 Datasets 以从 Hugging Face Hub 加载玩具音频数据集,以及 🤗 Accelerate 以减少模型加载时间:
pip install --upgrade pip pip install --upgrade transformers datasets[audio] accelerate
该模型可以与 pipeline (https://huggingface.co/docs/transformers/main_classes/pipelines#transformers.AutomaticSpeechRecognitionPipeline) 类一起使用,以转录任意长度的音频:
`` import torch from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline from datasets import load_dataset
device = “cuda:0” if torch.cuda.is_available() else “cpu” torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = “openai/whisper-large-v3”
model = AutoModelForSpeechSeq2Seq.from_pretrained( model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True ) model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
pipe = pipeline( “automatic-speech-recognition”, model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, torch_dtype=torch_dtype, device=device, )
dataset = load_dataset(“distil-whisper/librispeech_long”, “clean”, split=“validation”) sample = dataset[0][“audio”]
result = pipe(sample) print(result[“text”]) ``
要转录本地音频文件,只需在调用 pipeline 时传递音频文件的路径:
result = pipe("audio.mp3")
可以通过将多个音频文件指定为列表并设置 batch_size 参数来并行转录:
result = pipe(["audio_1.mp3", "audio_2.mp3"], batch_size=2)
Transformers 兼容所有 Whisper 解码策略,例如温度回退和基于先前 token 的条件生成。以下示例演示如何启用这些启发式方法:
`` generate_kwargs = { “max_new_tokens”: 448, “num_beams”: 1, “condition_on_prev_tokens”: False, “compression_ratio_threshold”: 1.35, # zlib compression ratio threshold (in token space) “temperature”: (0.0, 0.2, 0.4, 0.6, 0.8, 1.0), “logprob_threshold”: -1.0, “no_speech_threshold”: 0.6, “return_timestamps”: True, }
result = pipe(sample, generate_kwargs=generate_kwargs) ``
Whisper 会自动预测源音频的语言。如果源音频语言是先验已知的,则可以将其作为参数传递给 pipeline:
result = pipe(sample, generate_kwargs={"language": "english"})
默认情况下,Whisper 执行语音转录任务,即源音频语言与目标文本语言相同。要执行语音翻译(目标文本为英语),请将任务设置为 "translate":
result = pipe(sample, generate_kwargs={"task": "translate"})
最后,该模型可以预测时间戳。对于句子级时间戳,请传递 return_timestamps 参数:
result = pipe(sample, return_timestamps=True) print(result["chunks"])
对于词级时间戳:
result = pipe(sample, return_timestamps="word") print(result["chunks"])
上述参数可以单独使用,也可以组合使用。例如,要执行源音频为法语、且需要返回句子级时间戳的语音翻译任务,可以使用以下命令:
result = pipe(sample, return_timestamps=True, generate_kwargs={"language": "french", "task": "translate"}) print(result["chunks"])
如需对生成参数进行更多控制,请直接使用 model + processor API:
`` import torch from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor from datasets import Audio, load_dataset
device = “cuda:0” if torch.cuda.is_available() else “cpu” torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = “openai/whisper-large-v3”
model = AutoModelForSpeechSeq2Seq.from_pretrained( model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True ) model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
dataset = load_dataset(“hf-internal-testing/librispeech_asr_dummy”, “clean”, split=“validation”) dataset = dataset.cast_column(“audio”, Audio(processor.feature_extractor.sampling_rate)) sample = dataset[0][“audio”]
inputs = processor( sample[“array”], sampling_rate=sample[“sampling_rate”], return_tensors=“pt”, truncation=False, padding=“longest”, return_attention_mask=True, ) inputs = inputs.to(device, dtype=torch_dtype)
gen_kwargs = { “max_new_tokens”: 448, “num_beams”: 1, “condition_on_prev_tokens”: False, “compression_ratio_threshold”: 1.35, # zlib compression ratio threshold (in token space) “temperature”: (0.0, 0.2, 0.4, 0.6, 0.8, 1.0), “logprob_threshold”: -1.0, “no_speech_threshold”: 0.6, “return_timestamps”: True, }
pred_ids = model.generate(**inputs, **gen_kwargs) pred_text = processor.batch_decode(pred_ids, skip_special_tokens=True, decode_with_timestamps=False)
print(pred_text) ``
https://huggingface.co/openai/whisper-large-v3#additional-speed–memory-improvements 额外的速度与内存优化
您可以对 Whisper 应用额外的速度和内存优化,以进一步减少推理速度和 VRAM 需求。
https://huggingface.co/openai/whisper-large-v3#chunked-long-form 分块长音频
Whisper 的感受野为 30 秒。要转录超过此长度的音频,需要两种长音频算法之一:
- **顺序(Sequential):**使用“滑动窗口”进行缓冲推理,逐个转录 30 秒的片段
- **分块(Chunked):**将长音频文件拆分为较短的片段(片段之间有小部分重叠),独立转录每个片段,并在边界处拼接生成的转录文本
在以下任一场景中,应使用顺序长音频算法:
- 转录准确性是最重要的因素,速度是次要考虑
- 您正在转录批次的长音频文件,此时顺序算法的延迟与分块算法相当,而 WER 准确率可提高高达 0.5%
相反,在以下情况下应使用分块算法:
- 转录速度是最重要的因素
- 您正在转录单个长音频文件
默认情况下,Transformers 使用顺序算法。要启用分块算法,请传递 chunk_length_s 参数给 pipeline。对于 large-v3,30 秒的块长度是最佳的。要激活长音频文件的批处理,请传递 batch_size 参数:
`` import torch from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline from datasets import load_dataset
device = “cuda:0” if torch.cuda.is_available() else “cpu” torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = “openai/whisper-large-v3”
model = AutoModelForSpeechSeq2Seq.from_pretrained( model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True ) model.to(device)
processor = AutoProcessor.from_pretrained(model_id)
pipe = pipeline( “automatic-speech-recognition”, model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, chunk_length_s=30, batch_size=16, # batch size for inference - set based on your device torch_dtype=torch_dtype, device=device, )
dataset = load_dataset(“distil-whisper/librispeech_long”, “clean”, split=“validation”) sample = dataset[0][“audio”]
result = pipe(sample) print(result[“text”]) ``
https://huggingface.co/openai/whisper-large-v3#torch-compile Torch compile
Whisper 的前向传播与 torch.compile (https://pytorch.org/docs/stable/generated/torch.compile.html) 兼容,可实现 4.5 倍加速。
注意:torch.compile 目前与分块长音频算法或 Flash Attention 2 不兼容 ⚠️
`` import torch from torch.nn.attention import SDPBackend, sdpa_kernel from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline from datasets import load_dataset from tqdm import tqdm
torch.set_float32_matmul_precision(“high”)
device = “cuda:0” if torch.cuda.is_available() else “cpu” torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id = “openai/whisper-large-v3”
model = AutoModelForSpeechSeq2Seq.from_pretrained( model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True ).to(device)
Enable static cache and compile the forward pass
model.generation_config.cache_implementation = “static” model.generation_config.max_new_tokens = 256 model.forward = torch.compile(model.forward, mode=“reduce-overhead”, fullgraph=True)
processor = AutoProcessor.from_pretrained(model_id)
pipe = pipeline( “automatic-speech-recognition”, model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, torch_dtype=torch_dtype, device=device, )
dataset = load_dataset(“distil-whisper/librispeech_long”, “clean”, split=“validation”) sample = dataset[0][“audio”]
2 warmup steps
for _ in tqdm(range(2), desc=“Warm-up step”): with sdpa_kernel(SDPBackend.MATH): result = pipe(sample.copy(), generate_kwargs={“min_new_tokens”: 256, “max_new_tokens”: 256})
fast run
with sdpa_kernel(SDPBackend.MATH): result = pipe(sample.copy())
print(result[“text”]) ``
https://huggingface.co/openai/whisper-large-v3#flash-attention-2 Flash Attention 2
如果您的 GPU 支持 Flash-Attention 2 (https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#flashattention-2) 且您未使用 torch.compile (https://huggingface.co/openai/whisper-large-v3#torch-compile),我们建议使用它。为此,首先安装 Flash Attention (https://github.com/Dao-AILab/flash-attention):
pip install flash-attn --no-build-isolation
然后传递 attn_implementation="flash_attention_2" 给 from_pretrained:
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation="flash_attention_2")
https://huggingface.co/openai/whisper-large-v3#torch-scale-product-attention-sdpa Torch 缩放点积注意力(SDPA)
如果您的 GPU 不支持 Flash Attention,我们建议使用 PyTorch 缩放点积注意力(SDPA)(https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html)。对于 PyTorch 2.1.1 或更高版本,此注意力实现默认启用。要检查您是否具有兼容的 PyTorch 版本,请运行以下 Python 代码片段:
`` from transformers.utils import is_torch_sdpa_available
print(is_torch_sdpa_available()) ``
如果上述代码返回 True,则您安装了有效的 PyTorch 版本,并且 SDPA 默认已启用。如果返回 False,则需要根据官方说明 (https://pytorch.org/get-started/locally/) 升级您的 PyTorch 版本。
一旦安装了有效的 PyTorch 版本,SDPA 将默认启用。也可以通过指定 attn_implementation="sdpa" 来显式设置,如下所示:
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation="sdpa")
有关如何使用 SDPA 的更多信息,请参阅 Transformers SDPA 文档 (https://huggingface.co/docs/transformers/en/perf_infer_gpu_one#pytorch-scaled-dot-product-attention)。
https://huggingface.co/openai/whisper-large-v3#model-details 模型详情
Whisper 是一个基于 Transformer 的编码器-解码器模型,也称为序列到序列模型。Whisper 模型有两种类型:仅英语和多语言。仅英语模型在英语语音识别任务上训练。多语言模型同时在多语言语音识别和语音翻译上训练。对于语音识别,模型预测与音频相同语言的转录文本。对于语音翻译,模型预测与音频不同语言的转录文本。
Whisper 检查点有五种不同大小的配置。最小的四个可作为仅英语和多语言版本使用。最大的检查点仅支持多语言。所有十个预训练检查点均可从 Hugging Face Hub (https://huggingface.co/models?search=openai/whisper) 获取。下表总结了这些检查点,并附有 Hub 上模型的链接:
大小 | 参数 | 仅英语 | 多语言 tiny | 39 M | ✓ (https://huggingface.co/openai/whisper-tiny.en) | ✓ (https://huggingface.co/openai/whisper-tiny) base | 74 M | ✓ (https://huggingface.co/openai/whisper-base.en) | ✓ (https://huggingface.co/openai/whisper-base) small | 244 M | ✓ (https://huggingface.co/openai/whisper-small.en) | ✓ (https://huggingface.co/openai/whisper-small) medium | 769 M | ✓ (https://huggingface.co/openai/whisper-medium.en) | ✓ (https://huggingface.co/openai/whisper-medium) large | 1550 M | x | ✓ (https://huggingface.co/openai/whisper-large) large-v2 | 1550 M | x | ✓ (https://huggingface.co/openai/whisper-large-v2) large-v3 | 1550 M | x | ✓ (https://huggingface.co/openai/whisper-large-v3)
https://huggingface.co/openai/whisper-large-v3#fine-tuning 微调
预训练的 Whisper 模型展现出对不同数据集和领域的强大泛化能力。然而,通过微调,其针对某些语言和任务的预测能力可以进一步提升。博客文章《使用 🤗 Transformers 微调 Whisper》(https://huggingface.co/blog/fine-tune-whisper) 提供了使用仅 5 小时标注数据微调 Whisper 模型的分步指南。
https://huggingface.co/openai/whisper-large-v3#evaluated-use 评估用途
这些模型的主要预期用户是研究当前模型的稳健性、泛化能力、能力、偏见和约束的 AI 研究人员。然而,Whisper 作为开发人员的 ASR 解决方案也可能非常有用,尤其是对于英语语音识别。我们认识到,一旦模型发布,不可能将访问权限仅限于“预期”用户。
相似文章
Whisper 介绍
OpenAI 推出 Whisper,这是一个端到端的编码器-解码器 Transformer 模型,在大规模多样化音频数据上进行训练,可提供强大的多语言语音识别、语言识别和语音到英文翻译功能。Whisper 在多样化数据集上的错误率比专业模型低 50%,并且在语音翻译方面优于有监督基准,尽管未针对特定数据集进行微调。
vaibhavs10/incredibly-fast-whisper
一个高度优化的OpenAI Whisper Large v3版本,使用Transformers、Optimum和Flash Attention 2,能够在Replicate上在2分钟内转录150分钟的音频。
Whisper Live - 一个近乎实时的Open AI Whisper实现,免费且开源
WhisperLive 是一个开源实时转录工具,使用OpenAI的Whisper,支持多种后端(如faster-whisper和TensorRT)用于实时语音转文字。
Apple 新 SpeechAnalyzer API 与 Whisper 及前代产品的基准测试对比
Apple 的新 SpeechAnalyzer API 在英语设备端转录的准确性和速度上,显著优于其前代 SFSpeechRecognizer 和 OpenAI 的 Whisper 模型,该基准测试在 M2 Pro 机器上进行。新 API 在清晰语音上的词错误率为 2.12%,而 Whisper Small 为 3.74%,且运行速度快三倍。
@XieZhifei14110: 别再使用Whisper做语音识别了!开源Mega-ASR——首个全场景SOTA工业级ASR模型,专为……
开源Mega-ASR,一个全场景SOTA工业级ASR模型,专为远场、噪声等复杂音频环境设计,在真实世界基准测试中比现有开源和闭源模型性能高出10-30%。