Distil-Whisper 快速入门:6 倍加速的语音识别方案
Distil-Whisper 是 Whisper 的蒸馏版本,专为英语语音识别设计,实现了6 倍加速、49% 体积缩减,同时保持1% 以内的词错误率。作为轻量级语音识别解决方案,它在保留 Whisper 核心能力的同时,显著提升了推理速度和资源效率,是开发者构建实时语音应用的理想选择。
为什么选择 Distil-Whisper?
Distil-Whisper 作为 Whisper 的高效替代方案,具备五大核心优势:
Distil-Whisper 是 Whisper 的蒸馏版本,专为英语语音识别设计。相比原始模型,它实现了 6 倍推理速度提升和 49% 体积缩减,词错误率增加不超过 1%。支持无缝迁移、推测解码及 Flash Attention 优化。通过 Transformers 库即可安装使用,适用于实时语音转写、视频字幕生成等场景。提供短音频识别、长音频自动分段及结合教师模型的推测解码模式示例。
Distil-Whisper 是 Whisper 的蒸馏版本,专为英语语音识别设计,实现了6 倍加速、49% 体积缩减,同时保持1% 以内的词错误率。作为轻量级语音识别解决方案,它在保留 Whisper 核心能力的同时,显著提升了推理速度和资源效率,是开发者构建实时语音应用的理想选择。
Distil-Whisper 作为 Whisper 的高效替代方案,具备五大核心优势:
在保持 99% 以上识别准确率的同时,词错误率(WER)仅增加不到 1%,完全满足生产环境需求。
作为 Whisper 的"即插即用"替代方案,无需修改现有代码架构,可直接集成到现有语音识别 pipeline 中。
可作为 Whisper 的辅助模型实现2 倍额外加速,同时数学保证输出结果与原始模型一致。
采用 MIT 许可证,允许商业用途,适合企业级应用开发。
确保已安装 Python 3.8+,然后通过 pip 安装核心依赖:
pip install --upgrade pip
pip install --upgrade transformers accelerate datasets[audio]
如需进一步提升速度和降低内存占用,可安装以下优化组件:
pip install flash-attn --no-build-isolation
pip install --upgrade optimum
以下代码展示如何使用 Distil-Whisper 处理短音频文件:
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
import torch
from datasets import load_dataset
# 加载模型和处理器
model_id = "distil-whisper/distil-large-v3"
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch.float16, low_cpu_mem_usage=True
)
# 加载示例音频
dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
sample = dataset[0]["audio"]
# 处理音频并生成转录结果
inputs = processor(sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt")
inputs = inputs.to("cuda" if torch.cuda.is_available() else "cpu")
predicted_ids = model.generate(**inputs, max_new_tokens=256)
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
Distil-Whisper 支持自动分段处理长音频文件:
from transformers import pipeline
pipe = pipeline(
"automatic-speech-recognition",
model=model_id,
torch_dtype=torch.float16,
device="cuda:0" if torch.cuda.is_available() else "cpu",
)
# 处理长音频(自动分段)
result = pipe(sample["array"], max_new_tokens=1024, return_timestamps=True)
print(result["text"])
结合 Whisper 教师模型实现更高速度:
from transformers import AutoModelForSpeechSeq2Seq
# 加载教师模型和辅助模型
teacher_model_id = "openai/whisper-large-v3"
assistant_model_id = "distil-whisper/distil-large-v3"
teacher_model = AutoModelForSpeechSeq2Seq.from_pretrained(
teacher_model_id, torch_dtype=torch.float16, low_cpu_mem_usage=True
)
assistant_model = AutoModelForSpeechSeq2Seq.from_pretrained(
assistant_model_id, torch_dtype=torch.float16, low_cpu_mem_usage=True
)
# 使用推测解码
inputs = processor(sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt").to("cuda")
outputs = teacher_model.generate(
**inputs, assistant_model=assistant_model, do_sample=False,
)
transcription = processor.batch_decode(outputs, skip_special_tokens=True)[0]
启用 Flash Attention 可进一步提升性能:
model = AutoModelForSpeechSeq2Seq.from_pretrained(
model_id, torch_dtype=torch.float16, low_cpu_mem_usage=True, use_flash_attention_2=True # 启用 Flash Attention
)
如需训练自定义 Distil-Whisper 模型,可参考相关脚本,支持多语言蒸馏和领域适配。训练流程包括:
Distil-Whisper 已广泛应用于:
如果使用 Distil-Whisper,请引用相关论文:
@article{distilwhisper,
title={Distil-Whisper: Robust Knowledge Distillation via Large-Scale Pseudo Labelling},
author={Gandhi, Sanchit and von Werra, Leandro and Xu, Peng-Jen and Ju, Da and Conneau, Alexis},
journal={arXiv preprint arXiv:2311.00430},
year={2023}
}
完整技术细节可参考 Distil-Whisper 论文 (arXiv:2311.00430)。
Distil-Whisper 通过知识蒸馏技术,在保持高识别精度的同时,实现了显著的性能提升和模型压缩。无论是构建实时语音应用还是优化现有系统,它都能提供高效可靠的语音识别能力。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online