如何使用Distil-Whisper:6倍速提升的开源语音识别模型完整指南

【免费下载链接】distil-whisper Distilled variant of Whisper for speech recognition. 6x faster, 50% smaller, within 1% word error rate. 【免费下载链接】distil-whisper 项目地址: https://gitcode.com/gh_mirrors/di/distil-whisper

Distil-Whisper是Whisper的蒸馏版本,专为英语语音识别设计,实现了6倍速度提升、49%模型体积缩减,同时保持与原版Whisper仅1%的词错误率(WER)差异。作为一款高效的开源语音识别工具,它为开发者和普通用户提供了在资源受限环境下实现高性能语音转文本的解决方案。

为什么选择Distil-Whisper?5大核心优势

1. 突破性速度提升

Distil-Whisper在保持识别精度的同时,将推理速度提升了6倍。以下是与原版Whisper的性能对比:

模型 参数规模/M 相对延迟 短音频WER 长音频WER
large-v3 1550 1.0 8.4 11.0
distil-large-v3 756 6.3 9.7 10.8
distil-medium.en 394 6.8 11.1 12.4
distil-small.en 166 5.6 12.1 12.8

2. 出色的抗噪声能力

在低信噪比环境下仍保持稳定的识别性能,适合复杂声学环境应用:

3. 减少幻觉现象

相比原版Whisper,减少1.3倍的重复5-gram词汇,插入错误率(IER)降低2.1%,提升转录文本质量。

4. 支持推测解码

可作为Whisper的辅助模型实现2倍速推理,同时保证输出结果与原版完全一致,是现有Whisper pipeline的理想替代方案。

5. 宽松的MIT许可证

采用MIT许可证,允许商业应用,适合企业级项目集成。

快速开始:安装与基础使用

环境准备

首先确保安装Python环境,然后通过pip安装必要依赖:

pip install --upgrade pip
pip install --upgrade transformers accelerate datasets[audio]

克隆项目仓库

git clone https://gitcode.com/gh_mirrors/di/distil-whisper
cd distil-whisper

短音频转录(<30秒)

以下代码演示如何使用distil-large-v3模型转录短音频:

import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline

device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = "distil-whisper/distil-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)

# 创建ASR pipeline
pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    torch_dtype=torch_dtype,
    device=device,
)

# 转录本地音频文件
result = pipe("audio.mp3")
print(result["text"])

高级应用:长音频处理与性能优化

长音频转录方案

Distil-Whisper提供两种长音频处理策略:

1. 顺序长音频处理

适合对转录精度要求高的场景,兼容OpenAI的滑动窗口缓冲推理算法:

# 启用顺序长音频处理(默认)
result = pipe("long_audio.mp3")
print(result["text"])
2. 分块长音频处理

适合需要最快推理速度的单文件转录,设置分块长度和批处理大小:

pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=25,  # 最佳分块长度
    batch_size=16,       # 批处理大小
    torch_dtype=torch_dtype,
    device=device,
)

性能优化技巧

Flash Attention加速

如果GPU支持,安装Flash Attention可进一步提升速度:

pip install flash-attn --no-build-isolation

加载模型时启用:

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, 
    torch_dtype=torch_dtype, 
    low_cpu_mem_usage=True, 
    use_safetensors=True,
    use_flash_attention_2=True  # 启用Flash Attention 2
)
BetterTransformers优化

不支持Flash Attention时,使用BetterTransformers提升性能:

pip install --upgrade optimum

转换模型:

model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype)
model = model.to_bettertransformer()  # 应用BetterTransformers优化

模型架构与工作原理

Distil-Whisper采用知识蒸馏技术,保留原版Whisper的完整编码器,仅使用2层解码器(从原版的24层中精选),实现模型轻量化:

蒸馏过程中使用22k小时伪标记音频数据进行训练,涵盖10个领域和18k+说话人,确保模型在不同场景下的鲁棒性。

项目资源与扩展应用

训练代码

完整的训练代码位于training目录,支持自定义蒸馏配置,适合研究人员进一步优化模型。

多库支持

Distil-Whisper已集成到多个主流语音处理库:

  • OpenAI Whisper
  • Whisper cpp
  • Transformers.js
  • Candle (Rust)

具体集成方法可参考各模型页面的说明文档。

总结

Distil-Whisper通过创新的蒸馏技术,在保持高性能的同时大幅提升了语音识别速度和模型效率。无论是开发者构建语音应用,还是普通用户需要快速转录音频,Distil-Whisper都是一个理想选择。其开源特性和宽松许可证也为商业应用提供了便利。

要了解更多技术细节,请参考Distil-Whisper论文,或探索项目仓库中的训练代码和评估脚本。

【免费下载链接】distil-whisper Distilled variant of Whisper for speech recognition. 6x faster, 50% smaller, within 1% word error rate. 【免费下载链接】distil-whisper 项目地址: https://gitcode.com/gh_mirrors/di/distil-whisper

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐