Near-infrared spectroscopy (NIRS) offers significant advantages for the rapid and non-destructive detection of nutrients in livestock manure slurry. However, conventional models based only on spectral features often show limited robustness under cross-seasonal and multi-farm conditions due to differences in farm source, treatment stage, and complex spatiotemporal background. To improve the accuracy and applicability of total nitrogen (TN) prediction in dairy farm manure slurry, this study used 747 samples collected from 36 large-scale dairy farms in Tianjin, China, covering 24 treatment stages and four seasons, together with sample-contextual information such as farm name, longitude, latitude, and season. Competitive adaptive reweighted sampling (CARS) was applied to select key wavelengths from near-infrared spectra. On this basis, a multi-branch gated fusion deep learning model, MultiScaleSE-GatedCNN, was developed to integrate spectral and sample-contextual information. The model combines multi-scale one-dimensional convolution for spectral feature extraction, separate encoding branches for numerical and categorical inputs, and a gated fusion unit for adaptive weighting of different information sources. Results showed that partial least squares regression remained a strong baseline under single-source spectral conditions, but the proposed deep learning fusion model achieved superior predictive performance after introducing sample-contextual information. Ablation experiments demonstrated that different combinations of sample-contextual information contributed differently to model performance, and the combination of spectra, farm name, longitude, and season yielded the best results. Under this optimal input combination, MultiScaleSE-GatedCNN achieved a test-set R2 of 0.905, an RMSEP of 367.389, and an RPD of 3.242. These results demonstrate that integrating NIRS with sample-contextual information can effectively improve the accuracy and robustness of TN prediction in dairy farm manure slurry.
Liang et al. (Tue,) studied this question.