DEV Community

SleepTrace
SleepTrace

Posted on

What a 40MB CoreML model actually detects in your bedroom audio

What a 40MB CoreML model actually detects in your bedroom audio

A 40MB CoreML sleep model sounds ambitious until you realize the discriminative power in bedroom audio is concentrated in features that cost almost nothing to compute. The model's job is mostly to clean up edge cases the linear features already separate well. Here is what the model is really doing, layer by layer, and why it stays small.

The two-stage classifier

Stage one is a linear separator over six engineered features:

  1. Spectral tilt slope over 0.5–4 kHz — snoring tilts down, speech tilts flat.
  2. High-band zero-crossing density (4–16 kHz) — low for snoring, high for sibilants.
  3. Harmonic-to-noise ratio in the 100–800 Hz envelope — snoring is strongly periodic.
  4. Band energy ratio (low/mid) relative to the trailing 30-second baseline.
  5. Spectral flatness — distinguishes aperiodic snoring bursts from tonal noise.
  6. Temporal rise/fall asymmetry of the envelope — snoring ramps up slowly, speech transients sharply.

A logistic regression over these six features on-device matches a 200MB neural net on 87% of the validation set. The neural net is not smarter; it is just covering the confusion region.

The small net that earns its 40MB

The on-phone model is a 5-layer MLP with 128-width hidden layers, trained on 180k labeled 2-second frames from SleepTrace users (opt-in, all on-device labeled). Quantization to int8 brings it to 38 MB on disk. It runs in 1.8 ms per frame on the Neural Engine of an iPhone 13, which is the entire frame budget including feature extraction.

The model's job is a single classification head with a snooze/talk/ambient/apnea-pause four-way soft output, plus a confidence score that gates whether a frame participates in the smoothing pass. The full feature engineering rationale — why spectral tilt separates snoring from sentences, why harmonic structure separates voiced apnea gasps — is published on the engineering blog.

Why not just the linear model?

Two reasons:

  • Position-dependent false positives. HVAC rumble has low tilt and low harmonic content; it trips the linear model. The MLP learns a positional embedding over the night so HVAC rumble only scores as snoring when it sits between midnight and 04:00 with the right rhythm.
  • Confidence calibration. The MLP emits a calibrated confidence the smoothing pass uses to weight temporal consensus. Without it, every borderline frame either becomes a hard event or gets dropped entirely.

The size constraint is real

iOS throttles the resident memory of on-device CoreML models in background tasks. We hit that ceiling at ~45 MB resident; beyond it the queue stalls and the night develops silent gaps that look like quiet sleep. That is why the model architecture and the quantization scheme are co-designed around a 40 MB ceiling — and why on-device, unlike cloud, the size of the model is a correctness budget, not a performance budget.

Top comments (0)