mozilla/DeepSpeech · 上手攻略

  • 仓库:mozilla/DeepSpeech
  • 链接:https://github.com/mozilla/DeepSpeech
  • 分类:speech-recognition · offline-stt · embedded-ai
  • 作者:Tom
  • 更新:2026-08-18

⚠️ 项目已终止

重要声明:Mozilla DeepSpeech 项目已在 2023 年左右正式宣布 DISCONTINUED(终止维护)。最新版本为 v0.9.3(发布于 ~2021 年),无后续更新。预训练模型文件仍在 GitHub Releases 可下载,但不再有安全更新或新功能。

如果你需要离线语音识别,推荐评估替代方案:Whisper(OpenAI)、Whisper.cpp、Vosk、ESPnet;而非选择本项目。


这是什么

DeepSpeech 是 Mozilla 开发的开源离线语音识别(Speech-to-Text)引擎,基于百度 Deep Speech 论文(arXiv:1412.5567),使用 TensorFlow 实现。项目核心目标:不依赖云端,在本地设备上实现实时语音转文字,覆盖从树莓派 4 到高算力 GPU 服务器的硬件范围。

最后一版 v0.9.3 提供: - 英文预训练模型(pbmm 格式 + scorer 语言模型) - 支持 CUDA GPU 加速推理 - TFLite 导出,可在 Android/iOS/嵌入式设备运行 - 训练代码,支持用 Mozilla Common Voice 数据集微调


解决什么问题

  • 完全离线:无需网络,所有推理在本地运行,适合隐私敏感场景
  • 嵌入式部署:TFLite 模型可在树莓派 4、Android 手机上运行(~300MB 内存占用)
  • 用自己的数据微调:上传 Common Voice 等数据集,训练专属语音识别模型
  • 多语言支持:支持用 UTF-8 模式训练任意语言的模型(需自己准备数据)

快速安装

仅安装推理包(最简)

# CPU 版本
pip install deepspeech

# GPU 版本(需 CUDA)
pip install deepspeech-gpu

下载预训练模型(英文 v0.9.3)

# 模型权重
curl -LO https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/deepspeech-0.9.3-models.pbmm

# 语言模型 scorer(提升准确率)
curl -LO https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/deepspeech-0.9.3-models.scorer

# 示例音频
curl -LO https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/audio-0.9.3.tar.gz
tar xvf audio-0.9.3.tar.gz

验证安装

deepspeech --model deepspeech-0.9.3-models.pbmm \
           --scorer deepspeech-0.9.3-models.scorer \
           --audio audio/2830-3980-0043.wav

⚠️ 需提前安装 portaudio19-dev(Linux)或等效依赖;详细 runtime 依赖见 文档


核心用法

1. 命令行推理

# 基本用法
deepspeech --model deepspeech-0.9.3-models.pbmm \
           --scorer deepspeech-0.9.3-models.scorer \
           --audio my_audio.wav

# 完整帮助
deepspeech -h

2. Python API

from deepspeech import Model
import numpy as np

# 加载模型
model_path = "deepspeech-0.9.3-models.pbmm"
scorer_path = "deepspeech-0.9.3-models.scorer"

model = Model(model_path)
model.enableDecoderWithLM(scorer_path, 0.75, 1.5)

# 读取音频(需 16-bit PCM,16kHz mono)
# 可用 librosa / scipy 预处理:
import scipy.io.wavfile as wavfile
fs, audio = wavfile.read("audio.wav")

# 推理
text = model.stt(audio)
print(text)

3. 流式推理(实时语音)

# 适用于麦克风输入流,逐块送入模型
import deepspeech

model = deepspeech.Model("deepspeech-0.9.3-models.pbmm")
model.enableDecoderWithLM("deepspeech-0.9.3-models.scorer", 0.75, 1.5)

# stream API 示例
stream = model.createStream()
# 每次读取音频 chunk 后:
stream.feedAudioContent(chunk)
# 结束时获取结果:
text = stream.finishStream()

4. Node.js / Electron

npm install deepspeech
const { Model } = require('deepspeech');
const model = new Model('deepspeech-0.9.3-models.pbmm');
model.enableDecoderWithLM('deepspeech-0.9.3-models.scorer', 0.75, 1.5);

const text = model.stt(audioBuffer);
console.log(text);

5. Android(TFLite)

# 导出 TFLite 模型
python3 DeepSpeech.py \
    --export_dir /tmp/deepspeech \
    --export_tflite \
    --n_timesteps 16 \
    --n_features 26

# 结果文件:output_tflite.tflite

Android 示例(Java):

// 使用 TFLite Interpreter
Interpreter interpreter = new Interpreter(loadModelFile("output_tflite.tflite"));
interpreter.run(audioBuffer, output);

⚠️ TFLite 导出后无法使用 scorer 语言模型,仅裸模型推理

6. 用自己的数据训练模型

# 克隆仓库
git clone https://github.com/mozilla/DeepSpeech
cd DeepSpeech

# 安装训练依赖
pip install -e .[training]

# 下载 Common Voice 英语数据集(约 24GB)
python3 util/taskcluster.py --target ./dataset
# 或手动下载:https://commonvoice.mozilla.org/datasets

# 开始训练(需 GPU,建议 ≥ 8GB VRAM)
python3 DeepSpeech.py \
    --train_files ./dataset/cv-valid-dev.csv \
    --dev_files ./dataset/cv-valid-test.csv \
    --test_files ./dataset/cv-valid-train.csv \
    --epochs 5 \
    --export_dir ./models

7. 导出自己的模型

# 导出推理用 pbmm
python3 DeepSpeech.py \
    --checkpoint_dir ./checkpoints \
    --export_dir ./exported \
    --export_importer

# 制作 mmap-able 模型(加速冷启动)
python3 util/build_mmap.py \
    --model ./exported/output_graph.pb \
    --lm ./deepspeech-0.9.3-models.scorer \
    --output ./deepspeech-mmap.pbmm

典型适用场景

  1. 隐私敏感离线语音输入:完全本地处理,不上传任何音频数据
  2. 嵌入式 / IoT:TFLite 导出后跑在树莓派 4(需 ~300MB 内存)、Android 低配设备
  3. 特定领域语音识别:用 Common Voice 或领域专有语料微调模型(法律/医疗/工业术语)
  4. 旧设备兼容:不需要 GPU,CPU 即可运行(速度慢,约 0.3–0.5xRT)
  5. 离线实时字幕:配合流式 API 做本地会议字幕

坑与注意

⚠️ 项目已终止(最高注意):无安全更新、无新版本,v0.9.3 为最终版;生产环境慎用

⚠️ 英文模型质量:v0.9.3 在标准英文测试集 WER(Word Error Rate)约为 ~7%,落后于 Whisper(约 3-5%);小词汇量场景尚可,开放词汇场景效果明显差于现代模型

⚠️ scorer 语言模型与 TFLite 不兼容:导出 TFLite 后无法加载 scorer,WER 会明显上升

⚠️ 训练门槛高:完整英文模型训练需要 ≥ 8GB VRAM GPU + 数百GB 语料,普通人无法从头训练

⚠️ 多语言需自训:官方只有英文预训练模型;其他语言必须自己准备数据集并训练

⚠️ Python 依赖较旧:TensorFlow 1.x 系列,不兼容 Python 3.12;训练代码在现代环境可能存在依赖冲突

⚠️ CUDA 版本匹配:deepspeech-gpu 对 CUDA 版本有严格要求(v0.9.3 对应 CUDA 11.x),新版 GPU 架构可能不支持

⚠️ Whisper 是更强的替代:OpenAI Whisper(尤其是 whisper.cpp)几乎在所有维度(准确率、速度、语言覆盖、社区活跃度)优于 DeepSpeech;除非有离线/TFLite/隐私硬需求,否则无充分理由选 DeepSpeech


与同类对比

维度 DeepSpeech v0.9.3 Whisper Whisper.cpp Vosk
状态 ❌ 终止 ✅ 活跃 ✅ 极活跃 ✅ 活跃
离线推理
多语言 需自训 ✅ 99+ ✅ 99+ ✅ 20+
TFLite/移动端 ✅ 原生 需转换
准确率(英文) ~7% WER ~3-5% WER ~3-5% WER ~6% WER
速度(CPU)
模型大小 ~190MB ~155MB (small) ~140MB ~50MB (small)
训练自定义
预训练模型 仅英文 多尺寸多语言 多尺寸多语言 多语言

一句话推荐结论

除非你有 TFLite/嵌入式离线推理、或需要用自己数据微调语音识别的硬需求,否则不建议使用已终止的 DeepSpeech——OpenAI Whisper 或 whisper.cpp 在准确率、速度、社区活跃度上全面超越;如果你确实需要嵌入式场景,评估 Vosk 或 TensorFlow Lite Whisper 移植可能是更好的选择。


来源

  • GitHub(已归档):https://github.com/mozilla/DeepSpeech
  • 文档:https://deepspeech.readthedocs.io/en/r0.9/
  • 最新 Release:https://github.com/mozilla/DeepSpeech/releases/latest
  • 基础论文:https://arxiv.org/abs/1412.5567(百度 Deep Speech)
  • 预训练模型:https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/