w
weasonlee/voice-activity-detection
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

pyannote voice-activity-detection — Ascend NPU 适配

原始模型 pyannote/voice-activity-detection 只是 pipeline 壳(仓库内无权重文件,仅配置文件,关键参数 onset=0.81 / offset=0.48 / min_duration_on / min_duration_off)。

适配说明

本适配不直接使用 pyannote/voice-activity-detection 仓库(它没有权重),而是按官方 pipeline 的设计,使用同框架的说话人分割模型 pyannote/segmentation (Interspeech 2021,17.7 MB)作为底层模型:

  1. 加载 Model.from_pretrained("models/pyannote/segmentation") 得到 PyanNet 分段模型;
  2. 前向得到逐帧的 (frames, 3) 逐说话人概率,按官方 VAD pipeline 的 pre_aggregation_hook(max over speaker channels)聚合为 voice activity 概率;
  3. 用官方 Binarize(pyannote.core + pyannote.audio.utils.signal 的滞回阈值逻辑) 以 onset=0.81 / offset=0.48 阈值后处理,输出语音区间(speech regions)。

特征提取(波形预处理、pyannote I/O 类)始终在 CPU 上运行;矩阵运算在所选设备 (CPU 或 Ascend NPU)上执行。模型权重通过 .to("npu") 迁移到 NPU。

目录结构

.
├── inference.py                     # 推理脚本(--device npu|cpu)
├── readme.md                        # 本文档
├── requirements.txt                 # 依赖
├── .gitignore                       # 忽略 models/
├── models/pyannote/segmentation/    # 本地权重(不入库)
│   ├── config.json
│   ├── model.safetensors
│   └── pytorch_model.bin
└── assets/
    ├── agent_workflow.png           # 工作流图
    ├── npu_device_call.png          # NPU 设备信息图
    ├── model_result.png             # 结果图(2x2)
    └── data/                        # 中间数据

部署步骤

# 1. 安装依赖(Python 3.11;torch_npu 需匹配 CANN 环境)
pip install -r requirements.txt

# 2. 准备权重(models/ 已被 .gitignore 排除,需自行放置)
mkdir -p models/pyannote/segmentation
# 将 pyannote/segmentation 的 config.json 与 model.safetensors 放入上述目录,
# 或由本目录 models/seg/ 的 pytorch_model.bin 转换得到 model.safetensors。

# 3. CPU 推理
python inference.py --device cpu

# 4. NPU 推理(Ascend 910,torch_npu 2.9.0.post1)
python inference.py --device npu

推理示例

以 10 秒合成音频(4 段正弦波"语音" + 静音)为例,NPU 输出:

[NPU] pyannote/segmentation-based VAD
  voice-activity frames : 589
  mean voice prob       : 0.7228  max: 0.9952
  speech regions (3):
    [0.450s, 1.689s]  (1.239s)
    [2.929s, 6.749s]  (3.820s)
    [7.971s, 9.992s]  (2.021s)
  inference time        :  xxx ms

CPU vs NPU 精度对比

指标值
cos_sim(逐帧 voice 概率)~1.000000
max_abs_err(逐帧 voice 概率)< 1e-5
NPU 加速比(vs CPU)见 assets/model_result.png

精度对比表与可视化详见 assets/model_result.png(① 模型信息卡 ② CPU vs NPU 耗时柱状图+加速比 ③ 精度表 ④ 音频波形 + 逐帧 voice 概率曲线 + 检出语音区间高亮)。