Case study 01 · Android · on-device ML

StressSensein 5.77 KB

A stress-detection model that runs on the phone itself. I trained a strong gradient-boosting model, then distilled it into an INT8 TensorFlow Lite model small enough to ship inside the app.

In progressKotlin · Jetpack Compose · TFLiteRole: solo, end to end

Read this firstThe interesting part isn't the accuracy. It's what the model gave up to fit in a phone, and how little that cost.

93.5%Teacher accuracy
91.2%Student accuracy
5.77 KBModel on device
<1.8 msPer prediction
Data flow · how a record moves

Sensor reading to on-device prediction

Training happens once on a laptop; the trained student then runs on every reading, inside the phone. Click a stage to read what happens there.

01 · The constraint

Fast enough to disappear inside one frame

A phone redraws the screen about every 16.7 ms at 60 Hz. One prediction takes under 1.8 ms, so the app can run the model more than nine times before the user would notice a dropped frame.

0 ms8 ms16.7 ms · one frame at 60 Hz
■ one prediction  ·  ▢ room for eight more

Why on-deviceNo server round trip, works offline, and the user's data never leaves the phone.

Measure<1.8 ms is the measured upper bound per prediction.

02 · Distillation

Teach a small model to copy a big one

Gradient-boosted trees were the most accurate model, but a forest of trees is too heavy to ship. So the trees became the teacher: a small student learned from the teacher's predictions, then was quantized to 8-bit integers.

Trade-offThe student keeps 97.5% of the teacher's accuracy: 2.3 points lost.

Honest scaleThe bars start at zero on purpose. On a cropped axis the gap would look ten times bigger than it is.

03 · Every byte

This is the whole model

Each dot below is one byte of the shipped model, about 5,900 bytes in total. Hover to read a position. An old 1.44 MB floppy disk would hold roughly 249 copies.

5.77 KB · INT8 weights + graph

Why INT8Storing weights as 8-bit integers instead of 32-bit floats makes them four times smaller and faster on mobile CPUs.

04 · The pipeline

Six stages, end to end

The same six stages as the homepage. Dashed rows are still being written up.

00DataDRAFTWhich signals feed the model, where the data came from, how many samples and how stress was labelled.
01CleanDRAFTMissing readings, outliers, class balance between stressed and calm.
02FeaturesDRAFTThe features computed on the phone, and which ones mattered most.
03TrainGradient-boosting teacher, 93.5% accuracy.
04EvaluateDistilled INT8 student, 91.2% accuracy, 5.77 KB.
05DeployTensorFlow Lite inside a Kotlin + Jetpack Compose app, under 1.8 ms per prediction.
KotlinJetpack ComposeTensorFlow LiteGradient boostingKnowledge distillationINT8 quantization

NextAdd the evaluation split, a confusion matrix, and a short screen recording of the app.