The model candidates in Part 4 improved on DSP's F1 but exceeded the cloud container's dependency budget. I next tried running a model directly on the ESP32-S3, where it could score audio without an upload or network round trip. That meant fitting code and weights into the available flash while the device continued sampling audio in real time.

The flash budget, measured before a line of model code was written

The firmware partition is 1.5 MB, with about 1.05 MB already occupied by Wi-Fi, I2S capture, the DSP filters from Part 1, upload logic and flash queueing. Roughly 450 KB remained for the TensorFlow Lite Micro interpreter, operator resolver and model weights. Enlarging that partition would take space from clip storage, which was already suspected of contributing to dropped clips when the queue filled. I kept the existing allocation.

The device has 8 MB of octal PSRAM for inference working memory, including the tensor arena. Flash for code and weights was the tighter constraint.

Checking the two frontend implementations

The mel-spectrogram frontend runs in Python during training and in hand-written C on the device. To fit the flash budget, the C path uses a from-scratch radix-2 FFT with no ESP-IDF dependencies. The two implementations needed a direct comparison: an unnoticed difference would change the model's inputs between training and inference.

A golden-vector test compared the Python and C frontends on real captured clips. The largest disagreement was 8.34 × 10⁻⁴, at the noise floor of the highest mel frequency bin, matching the signature predicted before the test. This established close numerical agreement on the tested inputs, rather than assuming the implementations matched because they used the same mathematics.

The architecture, and why it's this small

The input is 40×150 log-mel frames. Two convolutional blocks use 3×3 kernels, 16 then 32 channels, stride 2, ReLU and max-pool, followed by global average pooling and a single-unit head. I chose this small architecture with the 450 KB flash budget in mind.

Quantised to int8, the weights occupy 9.5 KB. With the TFLite Micro interpreter and operator resolver, the reported total increase was roughly 67 KB, inside the 450 KB budget, with remaining headroom given as about 410 KB. The original interpreter-overhead estimate was roughly 2.3× pessimistic. A resolver limited to the five ops this model needs was much smaller than one supporting the full TFLite Micro operator set.

Bar chart of three cross-validation folds for the on-device CNN, each clearing 0.93 F1, with the mean marked
The honest per-fold numbers — a fresh model trained per fold, scored only on days it never trained on. Real numbers from chip_cv_results.json.

Training data and an evaluation leak

I started with a small seed set from the public ESC-50 dataset, resampled to match the device's 16 kHz mono capture. Once there were enough window recordings, I fine-tuned using the day-grouped folds from the cloud comparison in Part 4.

The first evaluation reported 0.948 mean F1, but the model had been fine-tuned on two of the three folds used for scoring. That leaked training data into the evaluation. I changed the procedure to train a fresh model per fold, holding the test fold out entirely. Early stopping uses a separate day held out from the training folds, without consulting the test fold.

The corrected result was F1 0.941 ± 0.009, above the DSP baseline of 0.871 ± 0.033 on every fold as well as on average. Closing the leak reduced the score from 0.948 to 0.941. The numerical change was small, but the original procedure was still invalid.

A small score change can still reveal a leak

The difference between 0.948 and 0.941 would not have identified the leak on its own. Checking which data each model had seen did. Training fresh models per fold and separating early-stopping data from test data made that boundary explicit.

Turning a model in a folder into firmware

Integrating inference into the firmware exposed several further issues:

  • Runtime version. I pinned TFLite Micro to an exact version, deliberately avoiding the newest release, which was only one day old. The planned one-month field trial needed a build that could be reproduced months later.
  • Linked size. A build containing the model files but never calling them was byte-identical to a build without them. The linker discarded the unreferenced code, so flash usage only became meaningful once the inference path was invoked.
  • Rounding conventions. NumPy's round rounds half-to-even; C's roundf rounds half-away-from-zero. That difference produced a structured ±1 discrepancy, small enough to mistake for numerical noise unless the pattern was examined.

The final firmware occupied 1.09 MB of the 1.5 MB partition. I added a build-time assertion to reject a firmware image that exceeds the flash budget. A second golden-vector check compared the device's quantised input path with the training pipeline on the same audio. It passed within tolerance, with a maximum disagreement of one count.

Precision and recall at frozen thresholds

I froze operating thresholds for DSP and the chip model out-of-fold, without using the labels against which each detector would then be scored:

DetectorThresholdPrecisionRecallClears 90/90?
chip0.6593.9%94.9%yes
dsp0.4084.7%86.5%no

The chip model cleared the 90/90 point-estimate requirement in this comparison and ran on the intended hardware. This was still an offline result, not the final trial verdict. Part 6 covers the live soak and the inverse-probability-weighted evaluation specified for it.

What made this different from the cloud attempt

The cloud comparison in Part 4 used a pre-set deployment budget, and all the model candidates exceeded it. Here I chose the architecture and quantisation specifically for the device's available flash. The result fitted the target hardware as well as improving on DSP in the offline evaluation.

What's next

DSP and the chip model now had out-of-fold results at frozen thresholds, but every label used so far pre-dated the soak. The next step was a planned month-long live trial without retuning, including the possibility of an inconclusive result. Part 6 reports how that trial ran.