|
| 1 | +# microWakeWord (MWW) Training & Tuning Toolchain |
| 2 | + |
| 3 | +This directory contains offline scripts to train, quantize, verify, and export streaming keyword-spotting models for the SOF `microwakeword` (MWW) module using real SOF MFCC features extracted via `sof-testbench4`. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Toolchain Overview |
| 8 | + |
| 9 | +1. **Keyword Dataset Generation & Ingestion:** |
| 10 | + - [sof_mww_generate_keyword_dataset_from_dir.sh](sof_mww_generate_keyword_dataset_from_dir.sh): Ingests recorded real speech WAV samples from a directory, preserves clean reference clips, and complements them with Room Impulse Response (RIR) reverberation, additive background noise (at varying SNRs), tempo/pitch perturbations, and Gaussian gain jitter. |
| 11 | + - [sof_mww_generate_keyword_dataset_piper_tts.sh](sof_mww_generate_keyword_dataset_piper_tts.sh): Synthesizes positive keyword WAVs with single-speaker Piper TTS voices, pitch/tempo perturbation (via `sox`), Gaussian gain jitter, and Room Impulse Response (RIR) reverberation. |
| 12 | + - [sof_mww_generate_keyword_dataset.sh](sof_mww_generate_keyword_dataset.sh): Multi-speaker generation with `piper-sample-generator` (LibriTTS-R). |
| 13 | + - [sof_mww_prepare_silence_unknown.sh](sof_mww_prepare_silence_unknown.sh): Prepares negative dataset classes (`silence` and `unknown`) by slicing background noise and non-target words from Google Speech Commands v2. |
| 14 | + |
| 15 | +2. **DSP Feature Extraction:** |
| 16 | + - [sof_mfcc_extract_features.sh](sof_mfcc_extract_features.sh): Emits 184-byte hop records ($24\text{-byte } \texttt{struct mfcc\_data\_header} + 40 \times \text{int32\_t}$ Q9.23 mel values) by running the SOF host testbench with 10 ms mel-40 topology. |
| 17 | + |
| 18 | +3. **Model Training & Export:** |
| 19 | + - [sof_mww_dataset.py](sof_mww_dataset.py): Parses testbench `.raw` feature files, applies soft mel-log AGC ($0\text{ dB}$ target, $0.5\text{ dB/s}$ release recovery), and builds training slices. |
| 20 | + - [sof_mww_train.py](sof_mww_train.py): Trains a streaming Keras model, quantizes to `int8` with TFLite Micro resource variables, packages behind SOF IPC4 ABI headers, and emits: |
| 21 | + - `mww_model_data_<name>.h`: Built-in model header selected by Kconfig. |
| 22 | + - `<name>.conf`: ALSA Topology v2 configuration blob for `Object.Base.data`. |
| 23 | + - `<name>.txt`: Runtime `sof-ctl` binary control file. |
| 24 | + - `sof_mww_train_pipeline.sh --hard-negative-keyword <phrase>`: Synthesizes |
| 25 | + Piper TTS competing wake phrases into `unknown/` as hard negatives before |
| 26 | + feature extraction. Set `PIPER_VOICE` and optionally |
| 27 | + `HARD_NEGATIVE_SAMPLES` (default: 200 per phrase). |
| 28 | + |
| 29 | + For a `Hi Intel` model, useful phrases include `hey siri`, `hey google`, |
| 30 | + `ok google`, `alexa`, `hey copilot`, and `hey jarvis`. |
| 31 | + |
| 32 | +4. **Off-Device Verification & Automation:** |
| 33 | + - [sof_mww_verify.py](sof_mww_verify.py): Feeds 3-hop streaming slices through the quantized TFLite model to verify detection rates and false alarm rates. |
| 34 | + - [sof_mww_train_pipeline.sh](sof_mww_train_pipeline.sh): End-to-end automation runner. |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## Quickstart |
| 39 | + |
| 40 | +### Environment Setup |
| 41 | + |
| 42 | +#### Setup Piper Virtual Environment (for Dataset Generation) |
| 43 | +```bash |
| 44 | +python3 -m venv ~/venvs/piper |
| 45 | +source ~/venvs/piper/bin/activate |
| 46 | +pip install --upgrade pip |
| 47 | +pip install piper-sample-generator |
| 48 | +``` |
| 49 | + |
| 50 | +#### Setup `piper-sample-generator` and Model Checkpoint |
| 51 | +```bash |
| 52 | +git clone https://github.com/rhasspy/piper-sample-generator ~/git/piper-sample-generator |
| 53 | +mkdir -p ~/git/piper-sample-generator/models |
| 54 | +wget -O ~/git/piper-sample-generator/models/en_US-libritts_r-medium.pt \ |
| 55 | + https://github.com/rhasspy/piper-sample-generator/releases/download/v2.0.0/en_US-libritts_r-medium.pt |
| 56 | +``` |
| 57 | + |
| 58 | +#### Setup Model Training Virtual Environment (Python 3.10 via `uv`) |
| 59 | +For systems with newer default Python versions (such as Ubuntu 24.04/26.04), install `uv` to manage a Python 3.10 environment required by TensorFlow 2.16: |
| 60 | +```bash |
| 61 | +curl -LsSf https://astral.sh/uv/install.sh | sh |
| 62 | +uv venv ~/venvs/mww-train --python 3.10 |
| 63 | +source ~/venvs/mww-train/bin/activate |
| 64 | +uv pip install --upgrade pip |
| 65 | +uv pip install "tensorflow==2.16.1" "tf-keras==2.16.0" |
| 66 | +``` |
| 67 | + |
| 68 | +### 1. Generate Synthetic Keyword Data |
| 69 | + |
| 70 | +#### Option A: Multi-speaker dataset with `piper-sample-generator` (PyTorch `.pt` model) |
| 71 | +```bash |
| 72 | +./sof_mww_generate_keyword_dataset.sh \ |
| 73 | + --keyword "hey jarvis" \ |
| 74 | + --label hey_jarvis \ |
| 75 | + --venv ~/venvs/piper \ |
| 76 | + ~/wov/wavs |
| 77 | +``` |
| 78 | +By default, the script automatically uses: |
| 79 | +- `PIPER_MODEL="$HOME/git/piper-sample-generator/models/en_US-libritts_r-medium.pt"` (auto-detected) |
| 80 | +- `MAX_SAMPLES=1000` (clips synthesized per speaking-rate variation) |
| 81 | +- `MAX_SPEAKERS=200` (cap on LibriTTS-R speaker indices for diverse, high-quality voices) |
| 82 | +- `GAIN_AUG=1` (Gaussian level jitter enabled: peak target -10 dBFS, sigma 5 dB) |
| 83 | + |
| 84 | +These can be overridden via environment variables if desired (e.g., `MAX_SAMPLES=1500 MAX_SPEAKERS=300 ./sof_mww_generate_keyword_dataset.sh ...`). |
| 85 | + |
| 86 | +#### Option B: Single-speaker dataset with `piper-tts` (ONNX `.onnx` model) |
| 87 | +```bash |
| 88 | +./sof_mww_generate_keyword_dataset_piper_tts.sh \ |
| 89 | + --keyword "hey jarvis" \ |
| 90 | + --label hey_jarvis \ |
| 91 | + --voice ~/.local/share/piper-voices/en_US-lessac-medium.onnx \ |
| 92 | + ~/wov/wavs |
| 93 | +``` |
| 94 | + |
| 95 | +#### Option C: Real speech dataset from a directory of recorded WAV samples |
| 96 | +To use recorded human speech recordings instead of synthetic TTS: |
| 97 | +```bash |
| 98 | +./sof_mww_generate_keyword_dataset_from_dir.sh \ |
| 99 | + --src-dir /path/to/recorded/audio \ |
| 100 | + --label hi_intel \ |
| 101 | + ~/wov/wavs |
| 102 | +``` |
| 103 | +This converts the input audio to 16 kHz 16-bit mono, keeps the clean unperturbed samples, and automatically complements them with RIR convolution, background noise mixing (10–30 dB SNR), pitch/tempo shifts, and Gaussian level jitter to reach a full training set size (`MAX_SAMPLES=1000` by default). |
| 104 | + |
| 105 | +You can also pass `--keyword-dir <label>:<path>` directly to `sof_mww_train_pipeline.sh`: |
| 106 | +```bash |
| 107 | +./sof_mww_train_pipeline.sh \ |
| 108 | + --keyword-dir hi_intel:/path/to/recorded/audio \ |
| 109 | + --name hi_intel \ |
| 110 | + ~/wov/wavs ~/wov/feats ~/wov/model |
| 111 | +``` |
| 112 | + |
| 113 | +### 2. Run End-to-End Retraining Pipeline |
| 114 | +With the `mww-train` virtual environment activated: |
| 115 | +```bash |
| 116 | +source ~/venvs/mww-train/bin/activate |
| 117 | +./sof_mww_train_pipeline.sh \ |
| 118 | + --keyword hey_jarvis \ |
| 119 | + --name hey_jarvis \ |
| 120 | + ~/wov/wavs ~/wov/feats ~/wov/model |
| 121 | +``` |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +### Training Multiple Keyword Variants into the Same Model |
| 126 | + |
| 127 | +You can train a single wake-word model to trigger on multiple spoken variants (such as `"hey jarvis"` and `"hi jarvis"`) with **no increase in model size or DSP RAM**: |
| 128 | + |
| 129 | +#### 1. Generate Datasets for Both Phrases |
| 130 | +```bash |
| 131 | +./sof_mww_generate_keyword_dataset.sh \ |
| 132 | + --keyword "hey jarvis" --label hey_jarvis \ |
| 133 | + --keyword "hi jarvis" --label hi_jarvis \ |
| 134 | + --venv ~/venvs/piper \ |
| 135 | + ~/wov/wavs |
| 136 | +``` |
| 137 | + |
| 138 | +#### 2. Train with Multiple `--keyword` Arguments |
| 139 | +Pass both directory labels to the training pipeline and specify `--name hey_jarvis`: |
| 140 | +```bash |
| 141 | +source ~/venvs/mww-train/bin/activate |
| 142 | +./sof_mww_train_pipeline.sh \ |
| 143 | + --keyword hey_jarvis \ |
| 144 | + --keyword hi_jarvis \ |
| 145 | + --name hey_jarvis \ |
| 146 | + ~/wov/wavs ~/wov/feats ~/wov/model |
| 147 | +``` |
| 148 | + |
| 149 | +Both phrase directories are pooled as the positive target class ($y = 1$). The streaming verification report provides individual detection recall for each phrase, and the exported `mww_model_data_<name>.h` model triggers when either phrase is spoken. |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +### Visualizing Streaming Diagnostics from mtrace |
| 154 | + |
| 155 | +You can visualize real-time hop metrics, energy/noise floor, int8 feature quantization, and detection probability curves captured from device `mtrace` logs: |
| 156 | + |
| 157 | +```bash |
| 158 | +# Capture mtrace output from device: |
| 159 | +sof-mtrace-reader > /tmp/mtrace.txt |
| 160 | + |
| 161 | +# Plot streaming diagnostics: |
| 162 | +./sof_mww_plot_mtrace.py /tmp/mtrace.txt -o /tmp/mww_diag.png |
| 163 | +``` |
0 commit comments