WeSpeaker ResNet293-LM for Core ML
Speaker embeddings on the Apple Neural Engine, 253ร real time on an iPhone 16, with the accuracy of the PyTorch original.
The largest WeSpeaker VoxCeleb model, converted to Core ML to run entirely on the Apple Neural Engine (ANE), the NPU in iPhone, iPad and Mac. 3 s of 16 kHz speech in, a 256-d voice embedding out. Verify a speaker, recognise enrolled people, or cluster speakers after diarization: on-device AI, no server.
Swift package: saalse/wespeaker-coreml. By Alex Samson.
At a glance
- Upstream: Wespeaker/wespeaker-voxceleb-resnet293-LM, 28.6 M parameters, VoxCeleb2
- Package:
WeSpeakerResNet293LM.mlpackage, 55 MB, float16, macOS 13 / iOS 16 - Input:
fbankfloat32(1, 298, 80), Kaldi log-mel of exactly 3 s. CMN is inside the model. - Output:
embeddingfloat32(1, 256). L2-normalise, compare by cosine. - Accuracy: VoxCeleb1-O EER 0.617% on Neural Engine, GPU and CPU, same as PyTorch on the same windows
- Speed: 12 ms per 3 s window on an iPhone 16 (15 ms on iPhone 13 Pro, 17 ms on M1 Pro), 100% of the graph on the ANE
Quick start
Swift
.package(url: "https://github.com/saalse/wespeaker-coreml", from: "0.1.0")
import WeSpeakerCoreML
let model = try await WeSpeaker.load() // downloads this package once, then cached
let a = try model.embedding(contentsOf: urlA) // wav, m4a, mp3โฆ
let b = try model.embedding(contentsOf: urlB)
print(WeSpeaker.similarity(a, b)) // higher means the same speaker
Python
hf download saalse/wespeaker-resnet293-lm-coreml --local-dir wespeaker-coreml && cd wespeaker-coreml
pip install coremltools numpy soundfile
python example.py a.wav b.wav # 16 kHz mono
fbank.py is the frontend in plain numpy. windows() repeats short audio to 3 s and cuts long audio
into 3 s windows, whose embeddings are averaged:
import coremltools as ct, numpy as np, soundfile as sf
from fbank import fbank, windows
model = ct.models.MLModel("WeSpeakerResNet293LM.mlpackage", compute_units=ct.ComputeUnit.CPU_AND_NE)
def embed(path):
x, _ = sf.read(path, dtype="float32")
e = [model.predict({"fbank": fbank(w)[None]})["embedding"][0] for w in windows(x)]
e = np.mean([v / np.linalg.norm(v) for v in e], axis=0)
return e / np.linalg.norm(e)
print(embed("a.wav") @ embed("b.wav"))
Input features
WeSpeaker's own inference frontend (torchaudio.compliance.kaldi.fbank):
- 16 kHz mono, samples in [-1, 1] times 32768
- 25 ms frames every 10 ms,
snip_edges=true: 298 frames per 3 s - Hamming window, DC removed, pre-emphasis 0.97, no dither
- 512-point FFT, power spectrum, 80 mel bins from 20 Hz to 8 kHz, natural log floored at float32 eps
- CMN per window is done inside the model (idempotent, so normalised input is fine too)
Accuracy
VoxCeleb1-O cleaned (37,611 trials), cosine scoring, no score normalisation, eval_voxceleb.py:
- PyTorch, whole utterance (how WeSpeaker reports it): EER 0.521%, minDCF(0.01) 0.057
- PyTorch, mean of 3 s windows: EER 0.612%, minDCF 0.067
- Core ML on Neural Engine, GPU or CPU, mean of 3 s windows: EER 0.617%, minDCF 0.067 to 0.069
The frontend is right (0.521% against 0.532% on WeSpeaker's card). The conversion loses nothing measurable: every utterance agrees with PyTorch at cosine 0.9998 or higher. The fixed 3 s window costs about 0.1 point of EER.
On 14 real recorded calls (9 h, the author's voice against everyone else), the same network beat CAM++ and TitaNet-L: EER 2.1% against 2.4% and 3.1%. Two lessons from them matter more than the model: 1 s turns fail about 5 times as often as 3 s turns, and a sound-alike voice needs its own enrolled print. Thread on Bluesky.
Speed
Median ms per 3 s window, one window per prediction, fbank excluded:
- iPhone 16 (A18, iOS 27.0.1): Neural Engine 11.8 ms (253ร real time), CPU 39.5, GPU 56.6
- iPhone 13 Pro (A15, iOS 27.2.1): Neural Engine 15.0 ms (201ร), CPU 53.1, GPU 81.3
- M1 Pro (16 GB, macOS 27.2.1): Neural Engine 16.9 ms (177ร), GPU 37.0, CPU 58.9
The whole graph runs on the Neural Engine on all three, and .all is as fast as .cpuAndNeuralEngine.
On iPhone the CPU beats the GPU, so keep the default .cpuAndNeuralEngine and never pick .cpuAndGPU.
End to end on the Neural Engine (read the file, Swift fbank at 0.2 to 0.4 ms per window, all windows): iPhone 16 207ร, iPhone 13 Pro 154ร, M1 Pro 143ร real time. The first Neural Engine load takes 2.5 to 5 s while Core ML compiles for it. The system caches that, later loads take about 0.1 s.
How it was measured: speaker-bench from the Swift package
on the Mac, and the same code in its iPhone bench app on the iPhones. Devices on power and idle, iPhones with
Low Power Mode off and thermal state nominal throughout, each compute unit in its own process, median of
30 runs after 5 warmup runs, 3 rounds. For comparison, WeSpeaker's ONNX model in sherpa-onnx ran at about
7ร real time on the M1 Pro CPU in an earlier test.
Conversion notes
- WeSpeaker ONNX files in sherpa-onnx skip CMN. The models are trained on mean-normalised fbank,
but sherpa-onnx applies CMN only when the ONNX metadata has
feature_normalize_type=global-mean, and WeSpeaker's files lack it. Add that entry. This package does CMN in the graph instead. - Flexible input shapes and the Neural Engine do not mix. A 1 to 30 s flexible build loaded for 20 to 180 s, ran at half speed and failed on 10 s inputs. A fixed 3 s window runs fully on the ANE.
- Only float16 reaches the Neural Engine. The float32 export runs on CPU or GPU, even with
CPU_AND_NE. - Two Kaldi frontends. sherpa-onnx uses a Povey window and
snip_edges=false(300 frames), WeSpeaker uses Hamming andsnip_edges=true(298 frames). This package expects 298. - Check placement with
MLComputePlan, not by timing.benchmark.pyprints it.
Files and reproduce
WeSpeakerResNet293LM.mlpackage: the modelfbank.py,example.py: frontend and a two-file comparisonconvert.py: rebuilds the package from the pinned WeSpeaker checkpoint (SHA-256 checked)benchmark.py: parity with PyTorch, timing and compute plan per compute uniteval_voxceleb.py: EER and minDCF on VoxCeleb1-O
pip install -r requirements.txt
python convert.py
python benchmark.py --checkpoint avg_model.pt clips/*.wav
python eval_voxceleb.py --wav-root vox1_test/wav --trials veri_test2.txt --checkpoint avg_model.pt
Built with torch 2.15.0 and coremltools 9.0 from avg_model.pt at revision
6e6bffe5bf3d772a1f143dc6dbfea58a0799ea83.
Limitations
- Trained on VoxCeleb2: celebrity interviews, mostly English and clean. Set your threshold on your own audio.
- Not a biometric authentication system. Voices can be imitated and synthesised.
- Speaker embeddings are personal data in many jurisdictions. Get consent before you enrol people.
License and credit
- Weights: by the WeSpeaker team, trained on
VoxCeleb, CC BY 4.0 (
LICENSE). Converted to Core ML with float16 weights, a fixed 3 s input and CMN in the graph. Not retrained. - Scripts: Apache 2.0 (
LICENSE-CODE).convert.pyrebuilds WeSpeaker'sresnet.py(Apache 2.0). - Conversion, evaluation and Swift package: Alex Samson. Results on your own data are welcome in the Community tab.
Citation
@inproceedings{wang2023wespeaker,
title = {Wespeaker: A research and production oriented speaker embedding learning toolkit},
author = {Wang, Hongji and Liang, Chengdong and Wang, Shuai and Chen, Zhengyang and
Zhang, Binbin and Xiang, Xu and Deng, Yanlei and Qian, Yanmin},
booktitle = {ICASSP 2023, IEEE International Conference on Acoustics, Speech and Signal Processing},
year = {2023}
}
@inproceedings{chung2018voxceleb2,
title = {VoxCeleb2: Deep speaker recognition},
author = {Chung, Joon Son and Nagrani, Arsha and Zisserman, Andrew},
booktitle = {INTERSPEECH},
year = {2018}
}
- Downloads last month
- 2
Model tree for saalse/wespeaker-resnet293-lm-coreml
Base model
Wespeaker/wespeaker-voxceleb-resnet293-LM