NIR Milk Analysis with Open Data: Our First Hugging Face Model and an In-Browser Simulator

Amazeng Technical Team
3 min read
NIR SpectroscopyMilk AnalysisArtificial IntelligenceHugging FaceEdge AI

Introduction

Milk quality is still measured mostly in the lab: a sample is taken, shipped, and the result arrives days later. Near-infrared (NIR) spectroscopy makes it possible to measure fat and protein in seconds, during milking. On the way to the NIR-based milk analyser we are developing at Amazeng, we took the first step with open data and released the result as a model anyone can use on Hugging Face.

Data: 1,224 Raw-Milk Spectra

We trained the model on an open dataset published by KU Leuven: 1,224 raw-milk samples measured over eight weeks by an NIR analyser connected to an automatic milking system on an experimental farm in Belgium. Each sample has a 256-point transmittance spectrum between 960 and 1690 nm, plus lab reference values for fat, protein, lactose, somatic cell count (SCC) and urea. The dataset is licensed CC BY 4.0: open for commercial use, with attribution.

Method: Classical First, Then Deep Learning

Before reaching the model, each spectrum goes through standard chemometrics steps:

  1. Absorbance: transmittance is converted to absorbance with −log10.
  2. SNV (Standard Normal Variate): each spectrum is normalised by its own mean and standard deviation, removing lamp intensity and light-scattering effects.
  3. Savitzky–Golay derivative: the first derivative removes baseline offsets and sharpens absorption bands.

On top of this preprocessing we compared classical PLS regression with a small 1D-CNN. We split the data by time, not at random: trained on the first six weeks and tested on the last two. That answers the practical question: does a calibration built today still hold in the coming weeks?

Results

TargetRMSE (%)R²
Fat0.1120.979
Protein0.0630.965
Lactose0.1260.48

Predicted vs lab reference on the test weeks

With about 900 training spectra, PLS beat deep learning on every target (the CNN reached only R² 0.41 on protein). Classical methods winning on small spectral datasets is common, so the released model uses PLS.

Somatic cell count and urea could not be predicted from these spectra. Both are present at very low concentrations, so this was expected, and we left them out of the model. As the model card states, this model is not a mastitis or bacteria test.

The Simulator: How Good Must the Spectrometer Be?

The fastest way to understand a model is to break it. So we built a simulator that runs in your browser: pick a real milk spectrum from the test weeks, then change lamp intensity, detector noise and wavelength shift, and watch the prediction react instantly.

Milk NIR Simulator: real spectrum, prediction and lab value side by side

What the simulator shows is as valuable for hardware design as the model itself:

  • Lamp intensity changes nothing. SNV normalisation removes it completely.
  • Noise is critical. Adding 0.02% noise on top of the real measurement doubles the protein error; at 0.1% the error reaches 0.56 points.
  • Wavelength shift is the most critical factor. A shift of just 0.2 pixels (0.57 nm) completely breaks the lactose prediction.

Effect of a 0.2-pixel wavelength shift on the prediction

The practical conclusion: you cannot move an NIR model to another spectrometer as it is. Every new instrument must be recalibrated with samples of known lab value, and the sensor side needs low noise (cooled detector, external high-resolution ADC, scan averaging) and a stable wavelength calibration.

Edge AI: Small Enough for a Microcontroller

The released ONNX model is 7.7 KB. Preprocessing is built in, so it takes raw transmittance and returns percentages directly. The compute load is about 5,000 multiply-accumulate operations and 256 logarithms. That means the model can run on an ESP32-S3-class microcontroller, right where the measurement happens, without a GPU. The bottleneck is not the processor but the spectrometer.

Working on spectral data, sensor calibration or edge AI? Get in touch.