模型简介
模型特点
模型能力
使用案例
🚀 自动语音识别模型 - Whisper large-v3-turbo
本项目基于OpenAI的Whisper模型,提供了强大的自动语音识别和语音翻译功能。通过微调,可适应不同语言和任务,在多种场景下实现高效准确的语音处理。
🚀 快速开始
模型支持情况
Whisper large-v3-turbo 支持多种语言,可用于自动语音识别任务。它基于OpenAI的Whisper模型,在性能和速度上有显著提升。
相关资源链接
免费使用资源
模型性能对比
Unsloth支持的模型 | 免费笔记本链接 | 性能表现 | 内存使用 |
---|---|---|---|
Orpheus-TTS | 点击开始 | 快1.5倍 | 减少58% |
Whisper Large V3 | 点击开始 | 快1.5倍 | 减少50% |
Qwen3 (14B) | 点击开始 | 快2倍 | 减少70% |
Llama 3.2 Vision (11B) | 点击开始 | 快1.8倍 | 减少50% |
✨ 主要特性
- 多语言支持:支持多种语言的自动语音识别和翻译,包括英语、中文、德语、西班牙语等。
- 高性能:基于OpenAI的Whisper模型,经过优化,在速度和准确性上表现出色。
- 易于使用:通过Hugging Face Transformers库,可轻松集成到项目中。
- 可微调:可以使用少量标注数据进行微调,以适应特定的语言和任务。
📦 安装指南
要运行Whisper large-v3-turbo模型,首先需要安装Transformers库。为了加载来自Hugging Face Hub的玩具音频数据集,还需要安装datasets
库;为了减少模型加载时间,需要安装accelerate
库。
pip install --upgrade pip
pip install --upgrade transformers datasets[audio] accelerate
💻 使用示例
基础用法
以下示例展示了如何使用pipeline
类转录任意长度的音频:
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-turbo"
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"])
转录本地音频文件
result = pipe("audio.mp3")
并行转录多个音频文件
result = pipe(["audio_1.mp3", "audio_2.mp3"], batch_size=2)
启用解码策略
generate_kwargs = {
"max_new_tokens": 448,
"num_beams": 1,
"condition_on_prev_tokens": False,
"compression_ratio_threshold": 1.35, # zlib压缩比阈值(在token空间中)
"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)
指定源音频语言
result = pipe(sample, generate_kwargs={"language": "english"})
执行语音翻译任务
result = pipe(sample, generate_kwargs={"task": "translate"})
预测时间戳
句子级时间戳
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"])
直接使用模型 + 处理器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-turbo"
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压缩比阈值(在token空间中)
"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)
🔧 技术细节
模型概述
Whisper是一个基于Transformer的编码器 - 解码器模型,也称为序列到序列模型。有英语专用和多语言两种版本。英语专用模型用于英语语音识别任务,多语言模型同时用于多语言语音识别和语音翻译任务。
模型配置
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 | 点击查看 |
large-v3 | 1550 M | x | 点击查看 |
large-v3-turbo | 809 M | x | 点击查看 |
长音频转录算法
顺序算法
适用于以下情况:
- 转录准确性是最重要的因素,对速度要求不高。
- 批量转录长音频文件,此时顺序算法的延迟与分块算法相当,但准确性可提高0.5%。
分块算法
适用于以下情况:
- 转录速度是最重要的因素。
- 转录单个长音频文件。
默认情况下,Transformers使用顺序算法。要启用分块算法,可在pipeline
中传递chunk_length_s
参数。对于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-turbo"
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, # 推理的批大小 - 根据设备设置
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"])
其他性能优化
Torch compile
Whisper的前向传播与torch.compile
兼容,可实现4.5倍的速度提升。
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-turbo"
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True
).to(device)
# 启用静态缓存并编译前向传播
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次热身步骤
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})
# 快速运行
with sdpa_kernel(SDPBackend.MATH):
result = pipe(sample.copy())
print(result["text"])
Flash Attention 2
如果GPU支持,且不使用torch.compile,建议使用Flash-Attention 2。首先安装Flash Attention:
pip install flash-attn --no-build-isolation
然后在from_pretrained
中传递attn_implementation="flash_attention_2"
:
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation="flash_attention_2")
Torch Scale-Product-Attention (SDPA)
如果GPU不支持Flash Attention,建议使用PyTorch的scaled dot-product attention (SDPA)。对于PyTorch 2.1.1或更高版本,此注意力实现默认启用。可通过以下代码检查PyTorch版本是否兼容:
from transformers.utils import is_torch_sdpa_available
print(is_torch_sdpa_available())
如果返回True
,则已安装有效版本的PyTorch,SDPA默认启用。如果返回False
,需要根据官方说明升级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文档。
📚 详细文档
微调
预训练的Whisper模型具有很强的泛化能力,但通过微调可以进一步提高其在特定语言和任务上的预测能力。博客文章Fine-Tune Whisper with ü§ó Transformers提供了使用最少5小时标注数据微调Whisper模型的详细步骤。
评估使用
这些模型的主要目标用户是研究当前模型的鲁棒性、泛化能力、性能、偏差和局限性的AI研究人员。不过,Whisper作为一种自动语音识别解决方案,对开发者也非常有用,尤其适用于英语语音识别。
模型主要在自动语音识别和语音翻译为英语的任务上进行训练和评估,在约10种语言中表现出较强的自动语音识别能力。如果在特定任务(如语音活动检测、说话人分类或说话人分割)上进行微调,可能会展现出更多能力,但在这些领域尚未进行充分评估。强烈建议用户在特定上下文和领域中对模型进行充分评估后再进行部署。
特别提醒,请勿使用Whisper模型转录未经个人同意的录音,或声称使用这些模型进行任何主观分类。不建议在高风险领域(如决策场景)使用,因为准确性缺陷可能导致严重后果。模型旨在转录和翻译语音,用于分类不仅未经过评估,也不合适,尤其不能用于推断人类属性。
性能和局限性
研究表明,与许多现有的自动语音识别系统相比,这些模型在应对口音、背景噪音、专业语言方面表现出更强的鲁棒性,并且能够实现多种语言到英语的零样本翻译。语音识别和翻译的准确性接近当前的先进水平。
然而,由于模型是使用大规模噪声数据进行弱监督训练的,预测结果可能包含音频输入中实际未说出的文本(即幻觉)。推测这是因为模型结合了基于语言知识预测下一个单词和转录音频本身的任务。
模型在不同语言上的表现不均衡,在资源较少、可发现性较低或训练数据较少的语言上准确性较低。在特定语言的不同口音和方言上也表现出差异,可能导致不同性别、种族、年龄或其他人口统计特征的说话人之间的单词错误率更高。完整的评估结果见本次发布的论文。
此外,模型的序列到序列架构容易生成重复文本,虽然可以通过束搜索和温度调度在一定程度上缓解,但无法完全解决。论文对这些局限性进行了进一步分析。在资源较少和/或可发现性较低的语言上,这种行为和幻觉可能更严重。
更广泛的影响
预计Whisper模型的转录能力可用于改进辅助工具。虽然Whisper模型本身不能直接用于实时转录,但其速度和规模表明,开发者可以在此基础上构建接近实时的语音识别和翻译应用。基于Whisper模型构建的有益应用的实际价值表明,这些模型的不均衡性能可能会产生实际的经济影响。
发布Whisper模型也带来了潜在的双重用途问题。虽然希望该技术主要用于有益目的,但自动语音识别技术的普及可能使更多人能够构建强大的监控技术或扩大现有监控工作的规模,因为其速度和准确性使得大量音频通信的自动转录和翻译变得经济可行。此外,这些模型可能具有一定的识别特定个人的能力,这在双重用途和不均衡性能方面都带来了安全问题。实际上,预计转录成本不是扩大监控项目的限制因素。
📄 许可证
本项目采用MIT许可证。
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}
}



