Upload folder using huggingface_hub
Browse filesFiles changed (3)
- README.mdAdded+72
- metrics.jsonAdded+66
- milk_nir_pls.onnxAdded+3
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172---license: cc-by-4.0library_name: onnxtags: - nir-spectroscopy - chemometrics - dairy - milk - pls - edge-aidatasets: - zenodo/8263430metrics: - rmse - r_squared---
# Raw-milk composition from NIR transmittance (PLS, ONNX)
Predicts **fat, protein and lactose (%)** of raw cow milk from a long-wave NIR transmittance spectrum(960–1690 nm, 256 pixels, ~2.86 nm/pixel). Preprocessing is inside the model: feed raw transmittance, get percentages.
Built by [Amazeng](https://amazeng.com.tr) as an open reference model for on-farm and in-line milk analysis on edge hardware.
## Usage
```pythonimport numpy as np, onnxruntime as ort
sess = ort.InferenceSession("milk_nir_pls.onnx")t = np.asarray(spectra, dtype=np.float32) # (batch, 256) transmittance, 960 -> 1690 nmfat, protein, lactose = sess.run(None, {"transmittance": t})[0].T```
**Input:** dark/white-corrected transmittance `(S - D) / (W - D)`, 256 values on the wavelength grid of the trainingspectrometer (960–1690 nm). A different spectrometer must resample to this grid **and** be recalibrated (see Limitations).
**Inside the model:** transmittance → absorbance (−log10) → SNV → Savitzky–Golay 1st derivative (window 15, order 2)→ one PLS regression per target, collapsed to a single linear layer. 7.7 KB, three outputs.
## Results
Temporal split: trained on weeks 1–6 (n=894), tested on weeks 7–8 (n=330) — a calibration used in the weeks after it was built.
| Target | RMSE (%) | R² | PLS components ||---|---|---|---|| Fat | 0.112 | 0.979 | 19 || Protein | 0.063 | 0.965 | 39 || Lactose | 0.126 | 0.48 | 36 |
A 1D-CNN trained on the same data was worse on every released target (fat R² 0.95, protein 0.41); with ~900 spectra,linear chemometrics wins.
## Limitations
- **One instrument, one farm, eight weeks** (41 Holstein cows, Belgium, 2017). Expect a bias on any other spectrometer or herd; recalibrate with your own reference samples before trusting absolute values.- **Lactose is weak** (R² ~0.5, sensitive to the PLS component count). Use fat and protein; treat lactose as indicative.- **Not included:** somatic cell count and urea. Neither was predictable from these spectra (test R² ≤ 0.13); SCC > 200k cells/mL screening reached only AUC 0.76. This model is **not** a mastitis or bacteria test.- Not a substitute for certified laboratory analysis.
## Training data and attribution
J.A. Diaz-Olivares, I. Adriaens, E. Stevens, W. Saeys, B. Aernouts — *Near-infrared spectra dataset of milk compositionin transmittance mode*, Zenodo, [10.5281/zenodo.8263430](https://doi.org/10.5281/zenodo.8263430), CC BY 4.0.Data in Brief (2023) 109767, [10.1016/j.dib.2023.109767](https://doi.org/10.1016/j.dib.2023.109767).
Related: Diaz-Olivares et al., *Online milk composition analysis with an on-farm near-infrared sensor*,Comput. Electron. Agric. 178 (2020) 105734.
This model is released under CC BY 4.0. Cite the dataset above when you use it.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566{ "split": "train weeks 1-6 (n=894), test weeks 7-8 (n=330)", "pls": { "Fat": { "rmse": 0.11183657606466905, "r2": 0.9791059615618545, "rpd": 6.918131351470947 }, "Prot": { "rmse": 0.06288773017244294, "r2": 0.9646598907421994, "rpd": 5.319441795349121 }, "Lact": { "rmse": 0.12580625033959672, "r2": 0.4784692290799788, "rpd": 1.3847137689590454 }, "logSCC": { "rmse": 0.5589001108519704, "r2": 0.09509268925551306, "rpd": 1.0512304306030273, "auc_scc>200k": 0.763937575030012 }, "Urea": { "rmse": 4.512190080604686, "r2": -0.16566286259174, "rpd": 0.9262186884880066 } }, "cnn": { "Fat": { "rmse": 0.17650680243968964, "r2": 0.9479551911354065, "rpd": 4.383400917053223 }, "Prot": { "rmse": 0.2565610706806183, "r2": 0.411810040473938, "rpd": 1.3038908243179321 }, "Lact": { "rmse": 0.13689003884792328, "r2": 0.38252532482147217, "rpd": 1.2725956439971924 }, "logSCC": { "rmse": 0.5491849780082703, "r2": 0.12627851963043213, "rpd": 1.0698268413543701, "auc_scc>200k": 0.7145258103241295 }, "Urea": { "rmse": 4.5135016441345215, "r2": -0.16634070873260498, "rpd": 0.9259495139122009 } }, "pls_components": { "Fat": 19, "Prot": 39, "Lact": 36, "logSCC": 17, "Urea": 6 }}123version https://git-lfs.github.com/spec/v1oid sha256:c50fa0862b6072f393bbc4735e5fee0904740e3adea6f1a5a176c6b7d60d6d31size 7686README.mdAdded+72
metrics.jsonAdded+66
milk_nir_pls.onnxAdded+3