Tensorflow Lite 的yes/no语音识别音频预处理模型训练教程

发布于:2025-09-06 ⋅ 阅读:(23) ⋅ 点赞:(0)

音频预处理模型训练指南

本文档详细介绍如何生成 audio_preprocessor_int8.tflite 预处理模型,该模型用于将原始 PCM 音频转换为 49×40 的 int8 特征,供微语音模型使用。

参考来源:https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/examples/micro_speech/README.md

概述

音频预处理模型是 KWS 双模型管线的第一阶段,负责:

  • 将 16kHz PCM 音频转换为频谱特征
  • 输出 49×40 的 int8 量化特征矩阵
  • 为微语音模型提供标准化输入

环境准备

WSL Ubuntu 环境(推荐)

由于 Bazel 构建系统在 Linux 环境下更稳定,推荐使用 WSL:

# 安装 WSL Ubuntu
wsl --install -d Ubuntu-22.04

# 进入 Ubuntu 环境
wsl -d Ubuntu-22.04

环境配置

1. 准备工作目录

WSL 环境:

# 重要:必须在 Ubuntu 文件系统下工作,不能在 Windows 挂载目录
cd ~
mkdir -p ~/src && cd ~/src

# 克隆 TensorFlow Lite Micro 仓库
git clone https://github.com/tensorflow/tflite-micro.git
cd tflite-micro
git submodule update --init --recursive

2. Python 虚拟环境

# 创建并激活虚拟环境
python3.10 -m venv .venvpy310_ubuntu
source .venvpy310_ubuntu/bin/activate

# 退出虚拟环境命令(需要时使用)
# deactivate

3. 安装 Bazel

# 创建 Bazel 安装目录
mkdir -p ~/.bazel/bin
cd ~/.bazel/bin

# 下载 Bazel 5.4.0
curl -fLO https://releases.bazel.build/5.4.0/release/bazel-5.4.0-linux-x86_64
chmod +x bazel-5.4.0-linux-x86_64
ln -sf bazel-5.4.0-linux-x86_64 bazel

# 验证安装
bazel version

# 返回工作目录
cd -

4. 修复 Bazel 配置

Bazel 5.4.0 不支持 --noenable_bzlmod 选项,需要修改配置:

# 编辑 .bazelrc 文件
nano /home/haihuiqiu/src/tflite-micro/.bazelrc

# 找到以下行并注释掉:
# build --noenable_bzlmod

# 保存并退出 (Ctrl+O → Enter → Ctrl+X)

5. 清理构建缓存

bazel clean --expunge

构建音频预处理模型

1. 构建预处理器

# 构建音频预处理器工具
bazel build tensorflow/lite/micro/examples/micro_speech:audio_preprocessor

2. 生成 int8 量化模型

# 生成带有 int8 输出的预处理模型
bazel-bin/tensorflow/lite/micro/examples/micro_speech/audio_preprocessor --output_type=int8

3. 验证输出

生成的模型文件位于:

/tmp/audio_preprocessor_int8.tflite

模型参数说明

输入规格

  • 采样率:16 kHz
  • 帧长:30ms (480 采样点)
  • 帧移:20ms (320 采样点)
  • 输入格式:16-bit PCM

输出规格

  • 特征维度:49×40 (时间×频率)
  • 数据类型:int8 量化
  • 特征类型:对数梅尔频谱图

信号处理参数

  • FFT 长度:512 点
  • 梅尔滤波器数量:40
  • 频率范围:80Hz - 7600Hz
  • 窗口函数:汉宁窗

集成到项目

1. 复制模型文件

# 将生成的模型复制到项目目录
cp /tmp/audio_preprocessor_int8.tflite /path/to/your/project/models/

2. 转换为 C 数组

使用项目中的工具将 .tflite 文件转换为 C 数组:

# 使用 xxd 工具转换
xxd -i audio_preprocessor_int8.tflite > audio_preprocessor_int8_model_data.c

或者使用 Python 脚本:

def tflite_to_c_array(tflite_path, output_path, array_name):
    with open(tflite_path, 'rb') as f:
        model_data = f.read()
    
    with open(output_path, 'w') as f:
        f.write(f'const unsigned char {array_name}[] = {{\n')
        for i, byte in enumerate(model_data):
            if i % 16 == 0:
                f.write('  ')
            f.write(f'0x{byte:02x}')
            if i < len(model_data) - 1:
                f.write(',')
            if (i + 1) % 16 == 0:
                f.write('\n')
            elif i < len(model_data) - 1:
                f.write(' ')
        f.write('\n};\n')
        f.write(f'const unsigned int {array_name}_len = {len(model_data)};\n')

# 使用示例
tflite_to_c_array(
    'audio_preprocessor_int8.tflite',
    'audio_preprocessor_int8_model_data.c',
    'audio_preprocessor_int8_tflite'
)

3. 更新头文件

创建对应的头文件 audio_preprocessor_int8_model_data.h

#ifndef AUDIO_PREPROCESSOR_INT8_MODEL_DATA_H_
#define AUDIO_PREPROCESSOR_INT8_MODEL_DATA_H_

extern const unsigned char audio_preprocessor_int8_tflite[];
extern const unsigned int audio_preprocessor_int8_tflite_len;

#endif  // AUDIO_PREPROCESSOR_INT8_MODEL_DATA_H_

测试和验证

1. 模型大小检查

# 检查模型文件大小
ls -lh /tmp/audio_preprocessor_int8.tflite

预期大小约为 10-15KB。

2. 功能测试

使用项目中的测试代码验证模型:

#include "audio_preprocessor_int8_model_data.h"
#include "tensorflow/lite/micro/micro_interpreter.h"

// 加载模型并测试推理
void test_audio_preprocessor() {
    // 初始化解释器
    const tflite::Model* model = tflite::GetModel(audio_preprocessor_int8_tflite);
    
    // 创建解释器实例
    tflite::MicroInterpreter interpreter(model, resolver, tensor_arena, 
                                       kTensorArenaSize, error_reporter);
    
    // 分配张量
    interpreter.AllocateTensors();
    
    // 获取输入输出张量
    TfLiteTensor* input = interpreter.input(0);
    TfLiteTensor* output = interpreter.output(0);
    
    // 验证张量形状
    assert(output->dims->data[1] == 49);  // 时间维度
    assert(output->dims->data[2] == 40);  // 频率维度
}

故障排除

常见问题

  1. Bazel 版本兼容性

    • 确保使用 Bazel 5.4.0
    • 注释掉 .bazelrc 中的 --noenable_bzlmod
  2. 路径问题

    • WSL 环境下必须在 Ubuntu 文件系统中工作
    • 避免在 Windows 挂载路径下构建
  3. 内存不足

    • 增加 WSL 内存限制
    • 使用 bazel clean 清理缓存
  4. 权限问题

    • 确保 Bazel 二进制文件有执行权限
    • 检查输出目录写权限

调试命令

# 查看构建详细信息
bazel build --verbose_failures tensorflow/lite/micro/examples/micro_speech:audio_preprocessor

# 检查依赖关系
bazel query 'deps(//tensorflow/lite/micro/examples/micro_speech:audio_preprocessor)'

# 清理并重新构建
bazel clean --expunge
bazel build tensorflow/lite/micro/examples/micro_speech:audio_preprocessor

高级配置

自定义参数

可以通过修改 audio_preprocessor.py 中的参数来调整模型:

# 修改特征提取参数
SAMPLE_RATE = 16000
WINDOW_SIZE_MS = 30.0
WINDOW_STRIDE_MS = 20.0
MEL_FILTER_COUNT = 40
LOWER_EDGE_HERTZ = 80.0
UPPER_EDGE_HERTZ = 7600.0

输出格式选项

# 生成浮点模型
bazel-bin/tensorflow/lite/micro/examples/micro_speech/audio_preprocessor --output_type=float

# 生成 int8 模型(默认)
bazel-bin/tensorflow/lite/micro/examples/micro_speech/audio_preprocessor --output_type=int8

性能优化

模型量化

int8 量化可以显著减少模型大小和推理时间:

  • 模型大小减少约 75%
  • 推理速度提升 2-4 倍
  • 精度损失通常小于 1%

内存使用

预处理模型的内存需求:

  • 模型权重:~12KB
  • 运行时张量:~8KB
  • 总计:~20KB

这符合 MCU 的内存约束要求。


网站公告

今日签到

点亮在社区的每一天
去签到