A nondestructive detection method for freshness of chilled mutton based on a dual-branch hierarchical spectral feature perception network
Through a dual-branch hierarchical spectral feature perception network, combined with multi-index evaluation and feature selection, and using position coding and multi-scale attention mechanism, the noise interference and feature redundancy problems in mutton freshness detection are solved, and high-precision non-destructive detection of mutton freshness is achieved.
Patent Information
- Application Number
- CN202510480795.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-17
- Publication Date
- 2025-08-15
- Estimated Expiration
- 2045-04-17
AI Technical Summary
The existing hyperspectral combined with deep learning algorithms are susceptible to sample preparation specifications and environmental fluctuations in the detection of mutton freshness. The spectral data are prone to noise interference and feature information is lost. The deep learning model has samples dependence, feature redundancy and category imbalance, resulting in high misjudgment rate.
Using a method based on a dual-branch hierarchical spectral feature perception network, through multi-index evaluation, optimization preprocessing and feature selection, combined with position-encoding bidirectional cross attention module and multi-scale enhanced multi-head attention mechanism, a hierarchical classifier with dynamic loss weight is designed to construct a hyperspectral feature perception network for non-destructive detection of mutton freshness.
It improves the accuracy and stability of mutton freshness detection, enhances the ability to identify spectral characteristics, and realizes high-precision non-destructive detection of freshness of cold mutton freshness.
Smart Images

Figure CN119992541B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of mutton freshness detection, and more particularly to a non-destructive detection method for the freshness of chilled mutton based on a double-branch hierarchical spectral feature perception network. Background Art
[0002] In the context of modern animal husbandry, lamb relies heavily on cold storage and cold chain transportation. The nutritional value and flavor of lamb are closely related to its freshness, making freshness a key factor in assessing its quality. Therefore, accurately testing the freshness level of lamb is crucial for ensuring consumer safety and real-time quality monitoring.
[0003] With increasing attention to food safety, methods for testing the freshness of mutton have gradually evolved from early sensory evaluation to more reliable physicochemical analysis, instrumental testing, and dye development. However, physicochemical analysis is complex, time-consuming, sample-damaging, and costly, and is often used to provide accurate quantitative data for other testing techniques. Hyperspectral technology offers unique advantages due to its rapid, non-invasive, and nondestructive nature. Compared with traditional testing methods, its correlation analysis of different wavelengths not only reveals subtle changes in meat during the spoilage process but also enables accurate freshness classification through the integration of data analysis models. However, in the practical application of mutton testing, existing hyperspectral-based deep learning algorithms are susceptible to noise and idiosyncratic distributions due to fluctuations in mutton sample preparation specifications and experimental environments. Inappropriate data processing strategies can lead to the loss of spectral feature information, and most models fail to fully exploit band information. Furthermore, deep learning models commonly suffer from sample dependence, feature redundancy, and class imbalance in practical applications, resulting in high misclassification rates, particularly in the fuzzy boundaries between adjacent freshness levels.
[0004] Therefore, how to improve the detection accuracy of non-destructive detection of mutton freshness is an urgent problem that needs to be solved by technical personnel in this field. Summary of the Invention
[0005] In view of this, the present invention provides a non-destructive detection method for the freshness of chilled mutton based on a double-branch hierarchical spectral feature perception network to improve the accuracy of mutton freshness grade identification.
[0006] In order to achieve the above object, the present invention provides the following technical solutions:
[0007] The present invention discloses a non-destructive detection method for the freshness of chilled mutton based on a double-branch hierarchical spectral feature perception network, and the specific steps are as follows:
[0008] Obtain mutton samples of different freshness, measure freshness indexes and collect hyperspectral images;
[0009] Determining the sample label of the corresponding training sample according to the freshness index of each mutton sample;
[0010] Processing the hyperspectral images using different preprocessing and feature selection combination methods as sample features of training samples, and constructing several training data sets corresponding to the combination methods;
[0011] Constructing a dual-branch hierarchical spectral feature perception network as a detection model, and using different training data sets to train them respectively;
[0012] The evaluation indicators of each trained detection model were calculated, and the detection model corresponding to the optimal preprocessing and feature selection combination method was used as the final freshness detection model for non-destructive freshness detection of fresh chilled mutton.
[0013] Furthermore, the freshness indicators include: volatile basic nitrogen, total colony count and approximate E. coli count; the sample labels are mutton freshness grades, including: fresh, sub-fresh, slightly corrupt and corrupt.
[0014] Furthermore, the preprocessing includes: SG filter, multivariate scatter correction, standard normal variable transformation, first-order derivative and moving average method; the feature selection includes: maximum mutual information minimization feature selection method and incremental feature selection method.
[0015] Furthermore, the sample features include global feature data and local feature data; the global feature data is the image data containing all band information after the hyperspectral image is preprocessed, and the local feature data is the image data containing part of the band information after feature selection of the global feature data.
[0016] Furthermore, the dual-branch hierarchical spectral feature perception network includes: a feature extraction module, a feature interaction module, a feature fusion module and an output module connected in sequence;
[0017] The feature extraction module performs feature extraction on the global feature data and the local feature data to obtain global features and local features;
[0018] The feature interaction module uses a bidirectional cross-attention mechanism to perform information interaction on the global features and the local features to obtain optimized global features and optimized local features;
[0019] The feature fusion module performs multi-scale enhancement on the optimized global features and the optimized local features, and fuses the multi-scale features using a multi-head attention mechanism to obtain a fused feature;
[0020] The output module outputs the recognition result using a hierarchical classifier based on the fusion features.
[0021] Furthermore, the feature extraction module includes a global feature extraction branch and a local feature extraction branch, each feature extraction branch is composed of a convolution layer, an activation function layer and a pooling layer connected in series. The global feature extraction branch performs feature extraction on the global feature data, and the local feature extraction branch performs feature extraction on the local feature data.
[0022] Furthermore, the feature interaction module includes a position encoding unit and a bidirectional cross attention unit;
[0023] The position encoding unit performs position encoding on the global feature and the local feature respectively to generate corresponding dynamic position vectors. The formula is:
[0024] ;
[0025] in, is the band index, is the position encoding dimension, is the dimension of the input features;
[0026] The bidirectional cross attention unit constructs a bidirectional interactive channel between global features and local features through bidirectional cross attention, and performs information flow between global and local features. The formula is:
[0027] ;
[0028] in, 、 They are respectively used to optimize global features and local features. 、 They are global features and local features respectively.
[0029] Furthermore, the feature fusion module includes: a multi-scale pooling layer, a multi-head attention layer and a fully connected layer connected in sequence;
[0030] The multi-scale pooling layer extracts features of different granularities through different scale pooling strategies. The formula is:
[0031] ;
[0032] in, 、 They are respectively used to optimize global features and local features. 、 They are the corresponding pooled results, 、 Represent global pooling and local pooling respectively;
[0033] The multi-head attention layer is equipped with four parallel attention heads. Each attention head takes the multi-scale pooled features as input and first performs a linear transformation to generate the corresponding query, key, and value. The formula is:
[0034] ;
[0035] in, is the input feature, 、 and Respectively The query, key, and value of each attention head, 、 and Respectively The linear transformation matrix of query, key and value corresponding to each attention head;
[0036] Then calculate the attention of each attention head , the formula is:
[0037] ;
[0038] in, is the dimension of the key, represents the activation function, Represents the square root operation, Represents a matrix transpose operation;
[0039] The fully connected layer concatenates the outputs of all attention heads and obtains fused features through linear transformation. , the formula is:
[0040] ;
[0041] in, represents the fully connected layer, is the linear transformation matrix, Represents a splicing operation.
[0042] Furthermore, the hierarchical classifier includes a coarse-grained classifier and a fine-grained classifier. First, the coarse-grained classifier is used to classify the fusion features. Divide into major categories and obtain intermediate features ; Then, the fine-grained classifier is used to perform refinement classification based on the intermediate features to obtain the final detection result , the specific formula is as follows:
[0043] ;
[0044] ;
[0045] in, are the weights and biases of the coarse-grained classifier, are the weight and bias of the fine-grained classifier, is the activation function.
[0046] Furthermore, the coarse-grained classifier and the fine-grained classifier both adopt a cross-entropy loss function, and by introducing dynamic weights, the total loss function of the output module is obtained. The specific formula is:
[0047] ;
[0048] in, represents the total loss function, are the loss functions of the coarse-grained classifier and the fine-grained classifier respectively; Represents dynamic weight, the formula is:
[0049] ;
[0050] ;
[0051] in, denote the number of training rounds and the maximum number of training rounds, respectively. 、 Represent the dynamic weights at the early and late stages of training, respectively.
[0052] As can be seen from the above technical solution, compared with the existing technology, the present invention discloses a non-destructive detection method for the freshness of chilled mutton based on a dual-branch hierarchical spectral feature perception network, which has the following outstanding technical features and innovative advantages:
[0053] (1) Freshness evaluation was performed using multiple indicators (TVB-N, TAC, MPN) combined with hyperspectral data, providing more biologically and chemically meaningful data support for the model;
[0054] (2) By optimizing the preprocessing and characteristic wavelength selection methods, a more universal and data-adaptive spectral feature extraction strategy is achieved;
[0055] (3) In the feature interaction stage, the bidirectional cross attention module (PBCA) combined with position encoding is introduced for the first time, which improves the model's ability to jointly model the long-range dependencies and local differences of spectral sequences;
[0056] (4) In the feature fusion stage, a multi-scale enhanced multi-head attention mechanism (MSMHA) was designed to significantly enhance the model's ability to recognize complex freshness change patterns;
[0057] (5) In the classification output stage, a hierarchical classification mechanism (HCM) driven by dynamic loss weights is proposed to effectively alleviate the problems of intermediate level ambiguity and misjudgment, and enhance the stability and accuracy of the model in multi-category scenarios.
[0058] The present invention discloses a non-destructive detection method for the freshness of chilled mutton with high precision, strong generalization ability and practical deployment potential, which has significant theoretical significance and application value. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are merely embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0060] Figure 1 Schematic diagram of the overall process of an embodiment of the present invention.
[0061] Figure 2 Schematic diagram of the overall structure of the dual-branch hierarchical spectral feature perception network of the present invention. DETAILED DESCRIPTION
[0062] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.
[0063] The embodiment of the present invention discloses a non-destructive detection method for the freshness of chilled mutton based on a double-branch hierarchical spectral feature perception network. Figure 1 The specific steps are as follows:
[0064] Obtain mutton samples of different freshness, measure freshness indexes and collect hyperspectral images;
[0065] Determine the sample label of the corresponding training sample according to the freshness index of each mutton sample;
[0066] Using different preprocessing and feature selection combination methods to process hyperspectral images as sample features of training samples, several training data sets corresponding to the combination methods are constructed;
[0067] A dual-branch hierarchical spectral feature perception network is constructed as a detection model and trained using different training datasets.
[0068] The evaluation indicators of each trained detection model were calculated, and the detection model corresponding to the optimal preprocessing and feature selection combination method was used as the final freshness detection model for non-destructive freshness detection of fresh chilled mutton.
[0069] In a specific embodiment, the freshness indicators include: volatile basic nitrogen, total colony count and approximate E. coli count; the sample labels are mutton freshness grades, including: fresh, sub-fresh, slightly corrupt and corrupt.
[0070] Specifically, a hyperspectral system is composed of a hyperspectral imager, a scanning platform and two adjustable 100W halogen lamps. The spectral range covered by the imager is 400nm~1000nm, with a resolution of 2.8nm, and a total of 750 spectral channels. Before collecting the spectral image, preheat the hyperspectral imager 30 minutes in advance and adjust the maximum light flux to F2.0. During the experiment, the mutton sample was placed on the scanning platform about 30cm away from the lens and placed directly opposite the imaging lens. The number of pixel mixing was set to 6 times, and then the exposure time was adjusted to keep the DN below 8500 to avoid signal loss. Two 100W halogen lamps were symmetrically arranged on both sides of the sample at an angle of 45°, about 35cm away from the sample. By gradually adjusting the light knob, the uniformity of the light distribution was higher than 90%. Click the acquisition button to obtain a hyperspectral image of the sample in the computer system. Using the formula The spectral image is subjected to black and white correction, where I represents the original image, B represents the black correction image, W represents the white correction image, and G represents the corrected spectral image. Hyperspectral data acquisition is completed by the hyperspectral system and data acquisition software.
[0071] After hyperspectral image acquisition, software was used to select regions of interest (ROIs) from the hyperspectral images of the chilled fresh mutton samples. First, a false-color image was constructed using the RGB principle, selecting the 650nm, 554nm, and 553nm bands to more clearly visualize the distribution of tissue on the sample surface. Then, to eliminate interference from small amounts of connective tissue, only the lean meat areas were marked. Twenty ROIs of 10×10 pixels were randomly selected from each image, and the raw spectral pixel brightness values for all bands within the regions were recorded. Finally, the spectral information corresponding to each ROI was used to generate a freshness raw spectral dataset in band order, resulting in a total of 20 × 210 = 4200 sample data points.
[0072] Finally, freshness indicators for samples within 14 days were measured: volatile basic nitrogen (TVB-N) content was determined according to GB / 5009.228-2016, "National Food Safety Standard for Foods - Determination of Volatile Basic Nitrogen in Foods - Semi-micro Kjeldahl Nitrogen Method," total aerobic count (TAC) was measured according to GB / 4789.2-2022, "National Food Safety Standard for Foods - Microbiological Examination - Determination of Total Aerobic Counts," and proximate coliform count (MPN) was counted according to GB / 4789.3-2016, "National Food Safety Standard for Foods - Microbiological Examination - Escherichia coli Count." Most current studies categorize freshness into two or three simple categories. To reflect the synergistic effects of multiple indicators and refine freshness levels, this example uses a threshold cross-classification method to precisely categorize freshness levels based on the content ranges of three freshness indicators. Four freshness labels are set: Fresh, Subfresh, Pre-Spoiled, and Spoiled. Table 1 shows the corresponding relationship between indicator content and freshness.
[0073] Table 1 Freshness classification
[0074]
[0075] In a specific embodiment, the preprocessing includes: SG filter (SG), multivariate scatter correction (MSC), standard normal variable transformation (SNV), first-order derivative (FD) and moving average method (MA); feature selection includes: maximum mutual information minimization feature selection method and incremental feature selection method.
[0076] Specifically, raw freshness spectral data is susceptible to noise interference from instrumentation, lighting, and other conditions, as well as the unevenness and uncontrollable surface roughness of mutton cuts. Therefore, preprocessing methods must be used to clean the data.
[0077] In hyperspectral data analysis, feature selection can significantly reduce data dimensionality and effectively retain information highly relevant to the target variable. This example uses the TVB-N, TAC, and MPN content of chilled lamb as marker variables. Using a preprocessed freshness spectral dataset, the maximum mutual information minimization (MIM) and incremental feature selection (IFS) methods were employed to extract the spectral bands most relevant to the freshness indicators. Four key regression metrics were used to evaluate model performance using different pre-feature selection methods: coefficient of determination (R²), root mean square error (RMSE), mean absolute error (MAE), and residual prediction deviation (RPD). R² measures the goodness of fit of the model; higher R² values indicate better model performance. RMSE and MAE measure prediction error; smaller values indicate higher model accuracy. RPD assesses the model's generalization ability; a value greater than 3.0 indicates excellent regression performance. Furthermore, the SHAP value (SHapley Additive exPlanations) was used to further verify the effectiveness of key features.
[0078] Table 2: Comparison of prediction performance of feature selection under different preprocessing methods
[0079]
[0080] Table 2 compares the prediction performance of MIM and IFS for freshness metrics using different preprocessing methods. The combined methods' performance was evaluated using regression metrics such as R², RMSE, MAE, and RPD. The contributions of the characteristic bands were also verified based on the interpretability of SHAP values. The tabular results and the visualization of SHAP values indicate that the selection of characteristic bands significantly impacts both the preprocessing method and the prediction performance. The prediction results indicate that the application of MIM and IFS significantly improves the model's prediction performance. By modeling the full-spectrum of TVB-N, TAC, and MPN using MSC, SNV, and FD preprocessing, the key bands selected by MIM and IFS demonstrate high prediction accuracy. The FD-IFS combination achieves a coefficient of determination (R²) and regression error RMSE (RMSE) of 0.8757 and 0.3601 for TVB-N, respectively, demonstrating the best error performance compared to the original data and other combined methods. Furthermore, RPDs exceeding 3.5 for the prediction of TAC and MPN indicators demonstrate strong model stability. The combination of preprocessing and feature selection not only significantly improves the predictive accuracy of the hyperspectral data model, but also optimizes computational efficiency and enhances the robustness and interpretability of local branches. The selected key bands are highly feasible for input into the subsequent detection network, providing precise feature information, making the local network's focus more efficient, and providing reliable spectral feature support for the freshness assessment task. Details of MIM, IFS, and SHAP are described below.
[0081] Based on the mutual information theory, the maximum mutual information minimization method calculates the mutual information between a single spectral band and the target variable to measure the statistical correlation, thereby screening a set of characteristic bands with high correlation, which can reflect the importance of spectral bands in predicting indicators such as TVB-N, TAC, and MPN. The specific formula is: ;in, Representation characteristics X and the target variable Y The mutual information between , Characteristics X With the target variable Y Entropy; It is a feature X and the target variable Y The joint entropy of .
[0082] Incremental feature selection, based on the joint mutual information algorithm, can screen out band sets with high correlation and high interactivity by analyzing the joint information between spectral band combinations and target variables. Unlike traditional single-variable feature selection, incremental feature selection considers the synergy between features and uses combined evaluation technology to deeply explore the importance of features. The specific formula is:
[0083] ;
[0084] in, Representation characteristics Xi and Xj With the target variable y The joint mutual information between and A single feature Xi and Xj With the target variable y The mutual information between them.
[0085] The SHAP value is based on the Shapley value in game theory to quantify the contribution of each feature to the model prediction results. The specific formula is:
[0086] ;
[0087] in, is the Shapley value of the i-th feature; N The full set of features, S is a feature subset, f(S) In the feature subset SThe prediction output under [ 1 ]. A SHAP contribution plot is generated to demonstrate the relative importance of all spectral bands and selected features in the prediction. Furthermore, a regression evaluation is performed on all freshness spectral feature sets to verify the predictive power of the selected feature set in practical applications and to determine the optimal feature selection method. To further verify the performance of the dual-branch information interaction mechanism, the feature band set is used as the focus information for the local branch to improve freshness detection performance.
[0088] In a specific embodiment, the sample features include global feature data and local feature data; the global feature data is the image data containing all band information after hyperspectral image preprocessing, and the local feature data is the image data containing part of the band information after feature selection of the global feature data.
[0089] In a specific embodiment, Figure 2 As shown in Figure 1, the dual-branch hierarchical spectral feature perception network includes: a feature extraction module, a feature interaction module (PBCA), a feature fusion module (MSMHA) and an output module (HCM) connected in sequence;
[0090] The feature extraction module extracts features from global feature data and local feature data respectively to obtain global features and local features;
[0091] The feature interaction module uses a bidirectional cross-attention mechanism to interact with global features and local features to obtain optimized global features and optimized local features;
[0092] The feature fusion module performs multi-scale enhancement on the optimized global features and local features, and uses the multi-head attention mechanism to fuse the multi-scale features to obtain the fused features;
[0093] The output module uses a hierarchical classifier to output the recognition results based on the fusion features.
[0094] In a specific embodiment, the feature extraction module includes a global feature extraction branch and a local feature extraction branch. Each feature extraction branch is composed of a convolution layer, an activation function layer and a pooling layer connected in series. The global feature extraction branch performs feature extraction on global feature data, and the local feature extraction branch performs feature extraction on local feature data.
[0095] Specifically, the global feature extraction branch is primarily used to extract global features from the entire input data, while the local feature extraction branch is used to extract local feature data. Each feature extraction branch consists of a series of convolutional layers, activation function layers, and pooling layers connected in series, aiming to extract features at different scales through a hierarchical structure. Furthermore, the outputs of the global and local feature extraction branches are fused in subsequent network modules to further improve the model's expressiveness and classification accuracy.
[0096] In a specific embodiment, the feature interaction module includes a position encoding unit and a bidirectional cross attention unit;
[0097] The position encoding unit performs position encoding on global features and local features respectively to generate the corresponding dynamic position vector. The formula is:
[0098] ;
[0099] in, is the band index; is the position encoding dimension; is the dimension of the input feature, indicating the total dimension of the encoded feature, and is used to normalize the position;
[0100] The bidirectional cross attention unit constructs a bidirectional interactive channel between global features and local features through bidirectional cross attention, and performs information flow between global and local features. The formula is:
[0101] ;
[0102] in, 、 They are respectively used to optimize global features and local features. 、 are global features and local features respectively. In cross attention, and It serves as both key and value input to guide global and local features to focus on global context information, thereby enhancing the discriminability of local features.
[0103] Specifically, in the feature interaction stage, the traditional two-branch method usually processes global features and local features independently, lacks interaction modeling between features, and causes the loss of dependency. Especially in the hyperspectral data analysis task, the high dimensionality of the data and the redundancy between bands make it difficult for simple feature fusion methods to fully capture the dependency between bands, resulting in insufficient utilization of the spatial similarity of features or band correlation. In addition, hyperspectral bands are sequentially sensitive, and the position of each band in the spectrum provides key contextual information. If the spectral data is directly input into the network, the relative position information of the bands may be lost. Therefore, the model requires an efficient feature interaction module to assist the model in dynamically learning and capturing the interaction information between global features and local features, so as to improve the model's learning ability for long-range dependencies, thereby enhancing its classification discrimination. Therefore, the present invention designs a PBCA module, which firstly encodes the global and local features of the input respectively through position encoding ( Figure 2The proposed method processes the spectral features of the hyperspectral image (PE) to generate a dynamic position vector related to the band order. This adds a learnable position encoding to the vector, enabling the model to effectively distinguish the relative importance and sequential relationship of each band, thereby enhancing the feature interaction of hyperspectral data. A bidirectional cross-attention mechanism is then introduced: this mechanism constructs a bidirectional interaction channel between global and local features, enabling information flow between global and local features, further promoting the model's learning of long-range dependencies and improving the expressive power of features.
[0104] In a specific embodiment, the feature fusion module includes: a multi-scale pooling layer, a multi-head attention layer, and a fully connected layer connected in sequence;
[0105] The multi-scale pooling layer extracts features of different granularities through different scale pooling strategies. The formula is:
[0106] ;
[0107] in, 、 They are respectively used to optimize global features and local features. 、 They are the corresponding pooled results, 、 Represent global pooling and local pooling respectively;
[0108] The multi-head attention layer is equipped with four parallel attention heads. Each attention head takes the multi-scale pooled features as input and first performs a linear transformation to generate the corresponding query, key, and value. The formula is:
[0109] ;
[0110] in, is the input feature, 、 and Respectively The query, key, and value of each attention head, 、 and Respectively The linear transformation matrix of query, key and value corresponding to each attention head;
[0111] Then calculate the attention of each attention head , the formula is:
[0112] ;
[0113] in, is the dimension of the key, Represents the activation function, which is used to limit the output to the range of 0 to 1 and perform probability distribution processing. Represents a square root operation (used for querying ( Q ) and key ( K ) to avoid excessively large values when calculating the attention score as the dimension increases). Represents the matrix transpose operation (when calculating the attention score, the query vector Q With key vector K A dot product is required, so the key vector needs to be transposed);
[0114] The fully connected layer concatenates the outputs of all attention heads and obtains fused features through linear transformation , the formula is:
[0115] ;
[0116] in, represents the fully connected layer, is the linear transformation matrix, Represents the concatenation operation (joining multiple attention heads ( Head 1 , Head 2 , ..., Head i ) are concatenated together to form a new feature vector).
[0117] Specifically, in the feature fusion stage, many existing studies are limited to simple global and local feature splicing or weighted averaging for feature fusion. However, these methods fail to fully capture the complex dependencies between different scales and different features; and may ignore slight differences between feature data; in addition, in the task of freshness detection of chilled mutton, the cross-scale correlation between bands is crucial for the discriminability and robustness of the freshness level. Effectively capturing the multi-level feature relationships can reduce the model's dependence on large-scale samples. Therefore, how to more accurately model the complex interactions between different feature scales and enhance their representation capabilities in freshness detection has become the key to improving detection accuracy. Therefore, the present invention designs the MSMHA module and proposes a multi-scale enhanced multi-head attention mechanism, which aims to make full use of multi-scale features through the combination of multi-scale pooling and multi-head attention, thereby modeling complex relationships across scales.
[0118] First, a multi-scale pooling strategy is applied to extract features of different granularities, optimizing both global and local features. After multi-scale enhancement, the model is able to capture features of different granularities and provide rich cross-scale feature information.
[0119] Then, a multi-head attention mechanism is used with four parallel attention heads. Head 1 (Global Trend Attention Head): Global trend modeling focuses on capturing global spectral trends and modeling the overall variation pattern in the freshness spectrum. Head 2 (Local Detail Attention Head): Local feature modeling focuses on specific bands strongly correlated with freshness chemical components, enhancing the model's sensitivity to spectral bands. Head 3 (Long-distance Dependency Attention Head): Cross-band long-distance dependency modeling enhances understanding of the nonlinear dependencies between different bands in hyperspectral data. Head 4 (Multi-scale Interaction Attention Head): Multi-scale feature interaction modeling compensates for potential distribution differences between features at different scales. Each attention head can focus on different spectral feature patterns and spectral data dependencies, providing the model with multi-perspective relationship modeling capabilities and forming a richer feature representation.
[0120] In a specific embodiment, the hierarchical classifier includes a coarse-grained classifier and a fine-grained classifier. First, the coarse-grained classifier is used to classify the classifier based on the fusion feature. Divide into major categories and obtain intermediate features ; Then, through the fine-grained classifier, the classification is refined based on the intermediate features to obtain the final detection result , the specific formula is as follows:
[0121] ;
[0122] ;
[0123] in, are the weights and biases of the coarse-grained classifier, are the weight and bias of the fine-grained classifier, is the activation function.
[0124] Specifically, in the output stage, the extracted fusion features are input into the hierarchical classifier for two-stage classification. It aims to address the problems of overlapping category features, uneven sample distribution, and insufficient intermediate features when detecting the freshness of chilled mutton. The present invention designs an HCM module and proposes a hierarchical classifier with dynamic joint loss function optimization. Through the phased optimization of coarse-grained and fine-grained classifiers and the dynamic balance design of the joint loss function, the classification difficulties are gradually solved. First, the samples are divided into large categories by a coarse-grained classifier to simplify the task and ensure that the model can initially distinguish large categories and provide intermediate features. Then, a fine-grained classifier is used to receive the intermediate features and perform refined classification, process the subtle differences between categories, and finally output the prediction results.
[0125] In a specific embodiment, both the coarse-grained classifier and the fine-grained classifier use a cross-entropy loss function, and by introducing dynamic weights, the total loss function of the output module is obtained. The specific formula is:
[0126] ;
[0127] in, represents the total loss function, are the loss functions of the coarse-grained classifier and the fine-grained classifier respectively; Represents dynamic weight, the formula is:
[0128] ;
[0129] ;
[0130] in, denote the number of training rounds and the maximum number of training rounds, respectively. 、 Represent the dynamic weights at the early and late stages of training, respectively.
[0131] Specifically, the present invention introduces dynamic weights to dynamically adjust the focus of coarse-grained and fine-grained tasks during training. Initially, the dynamic weights for coarse-grained tasks are relatively large, and the model is adjusted by training rounds and maximum rounds, gradually shifting its focus to fine-grained tasks. Furthermore, during training, the dynamic weights are adjusted by the relative value of the loss at each stage. This dynamic loss optimization strategy allows the model to automatically adjust weights based on the different stages of training, allowing it to focus more on the tasks or objectives that require optimization at each stage, thereby accelerating convergence and improving performance.
[0132] In a specific embodiment, to meet the high-precision requirements of mutton freshness detection, accuracy, weighted precision, weighted recall, and weighted F1 score are used as evaluation criteria to evaluate the performance of a two-branch hierarchical spectral feature perception network.
[0133] Set the number of categories to n , No. i The number of samples of the class is N i The total number of samples is N For sample classification, the effective positive class (effective positive, EP i) refers to the number of samples correctly identified as positive; undetected positive, UP i ) is the number of positive samples that are not detected and mistakenly classified as negative; false alert positive, FAP i ) is the number of negative samples incorrectly classified as positive; effective negative ( EN i ) refers to the number of samples correctly identified as negative classes.
[0134] The accuracy calculation formula is:
[0135] ;
[0136] The formula for calculating weighted recall is:
[0137] ;
[0138] The weighted precision calculation formula is:
[0139] ;
[0140] The weighted F1 score calculation formula is:
[0141] .
[0142] in, EP i Indicates the i The number of samples correctly identified in the class, EN Represents the total number of samples correctly identified as non-target classes, UP i Indicates the i The number of samples in the class that are misclassified as other classes, FAP i Indicates that other classes are misclassified as i The number of samples of the class, N i Indicates the i The total number of samples of the class, N represents the total number of samples, n Indicates the total number of categories.
[0143] In order to verify the effectiveness of the dual-branch hierarchical spectral feature perception network (DBHSNet) in freshness detection of chilled mutton, an ablation experiment and a comparative analysis of the classification indicators of typical algorithm models were set up, and the detection results were output.
[0144] First, a system performance evaluation framework was constructed using CNN, SVM, LightGBM, RF, and DBHSNet to ensure the accuracy of mutton freshness detection results. A preprocessed full-spectral dataset of chilled fresh mutton was used as the input to the DBHSNet network, and the spectral band information obtained by different combination methods was used as the focus of local branches. Table 3 compares the detection performance of the models using different preprocessing and feature selection combinations.
[0145] Table 3 Comparison of detection performance of models under different preprocessing-feature selection combinations
[0146]
[0147] Comparing experimental results, the DBHSNet model performs best across all metrics using the FD-IFS combination. Its accuracy is 99.72%, a 0.97% improvement over the next-best model, LightGBM, and its weighted F1 score exceeds 99.5%, demonstrating its robustness under multi-class and imbalanced conditions. Furthermore, its weighted precision and recall exceed 99%, demonstrating its efficient utilization of intermediate features in classifying freshness levels. In practical applications, DBHSNet can effectively prevent spoiled mutton from entering the market, minimizing health hazards and economic losses. Furthermore, among the different preprocessing methods, FD and MSC improve model performance by extracting frequency-domain features, correcting for scattered noise, and enhancing chemical feature stability, respectively. Therefore, both contribute significantly to extracting freshness-related features. In contrast, the moving average (MA), while capable of smoothing noise, can result in the loss of some spoilage signals, highlighting its limitations in freshness detection. In addition, under the FD and MSC methods, CNN, LightGBM and RF showed higher performance, indicating that the two preprocessing methods can more appropriately handle the noise and redundancy in the original spectral data.
[0148] On the other hand, an ablation experiment of the DBHSNet model is set up, and the experimental results are shown in Table 4.
[0149] Table 4 Ablation experiment results of DBHSNet
[0150]
[0151] The ablation experiment results in Table 4 show that the overall model performance exhibits significant nonlinear gains with the gradual enhancement and combination of modules. First, the HCM (hierarchical classification) module demonstrates fundamental optimization capabilities when enabled alone. When only HCM is enabled, the accuracy of all three experimental groups is significantly improved: by 3.33% in the FD-MIM group, 2.36% in the FD-IFS group, and 2.92% in the MSC-MIM group. Furthermore, the difference between the weighted F1 score and the accuracy of the model is consistently less than 0.07%, indicating that it enhances the inter-class discrimination of the freshness feature. Second, in the FD-MIM group, the PBCA+HCM combination improves by 2.08% compared to the MSMHA+HCM combination. Adding MSMHA to form a full module combination increases the gain to 5.83%. Superposition results demonstrate that the hierarchical classifier constructed with HCM effectively exploits the cross-scale feature fusion information of MSMHA. Module combinations demonstrate the specificity of the method. Therefore, in practical detection applications, module combination strategies should be customized based on data characteristics. Third, full-module collaboration achieved peak performance in freshness detection, with the FD-IFS group achieving an 11.80% improvement in accuracy compared to the baseline, and the fluctuation in the index being less than 1%, indicating a positive coupling between FD-IFS and the model's spectral feature perception capabilities. Weighted indices for both the FD-MIM and FD-IFS groups were greater than 99% when all modules were activated. Therefore, the PBCA, MSMHA, and HCM proposed in this invention, when combined, can effectively improve model performance, enhance feature expression capabilities, and enhance network robustness, which is of great value for real-time quality monitoring of chilled fresh mutton.
[0152] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. Reference can be made to the common and similar parts between the various embodiments. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple, and the relevant parts can be referred to the method description.
[0153] The above description of the disclosed embodiments is intended to enable one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be readily apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention is not limited to the embodiments shown herein but is intended to conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A non-destructive detection method for the freshness of chilled mutton based on a double-branch hierarchical spectral feature perception network, characterized in that: The specific steps are as follows: Obtain mutton samples of different freshness, measure freshness indexes and collect hyperspectral images; Determining the sample label of the corresponding training sample according to the freshness index of each mutton sample; Processing the hyperspectral images using different preprocessing and feature selection combination methods as sample features of training samples, and constructing several training data sets corresponding to the combination methods; The preprocessing includes: SG filter, multivariate scatter correction, standard normal variable transformation, first-order derivative and moving average method; the feature selection includes: maximum mutual information minimization feature selection method and incremental feature selection method; the sample features include global feature data and local feature data; the global feature data is the image data containing all band information after the hyperspectral image is preprocessed, and the local feature data is the image data containing part of the band information after the global feature data is feature selected; Constructing a dual-branch hierarchical spectral feature perception network as a detection model, and using different training data sets to train them respectively; The dual-branch hierarchical spectral feature perception network includes: a feature extraction module, a feature interaction module, a feature fusion module and an output module connected in sequence; The feature extraction module performs feature extraction on the global feature data and the local feature data to obtain global features and local features; The feature interaction module uses a bidirectional cross-attention mechanism to perform information interaction on the global features and the local features to obtain optimized global features and optimized local features; The feature fusion module performs multi-scale enhancement on the optimized global features and the optimized local features, and fuses the multi-scale features using a multi-head attention mechanism to obtain a fused feature; The output module outputs the recognition result using a hierarchical classifier based on the fusion features; The feature interaction module includes a position encoding unit and a bidirectional cross attention unit; The position encoding unit performs position encoding on the global features and the local features respectively to generate corresponding dynamic position vectors. The formula is: PE(x)=sin(x / 10000 (2j / d) ,cos(x / 10000 (2j+1 / d) ); Among them, x is the band index, j is the position encoding dimension, and d is the dimension of the input feature; The bidirectional cross attention unit constructs a bidirectional interactive channel between global features and local features through bidirectional cross attention, and performs information flow between global and local features. The formula is: Among them, Z global 、Z local They are respectively optimized for global features and local features, X global 、X local They are global features and local features respectively; The feature fusion module includes: a multi-scale pooling layer, a multi-head attention layer and a fully connected layer connected in sequence; The multi-scale pooling layer extracts features of different granularities through different scale pooling strategies. The formula is: Among them, Z global 、Z local They are respectively used to optimize global features and local features. They are the corresponding pooling results, Global Pooling and Local Pooling represent global pooling and local pooling respectively; The multi-head attention layer is equipped with four parallel attention heads. Each attention head takes the multi-scale pooled features as input and first performs a linear transformation to generate the corresponding query, key, and value. The formula is: Among them, X is the input feature, Q i , K i and V i are the query, key, and value of the i-th attention head, respectively. and are the linear transformation matrices of the query, key, and value corresponding to the i-th attention head respectively; Then calculate the attention Head of each attention head i , the formula is: Among them, d k is the dimension of the key, Softmax represents the activation function, sqrt represents the square root operation, Represents a matrix transpose operation; The fully connected layer concatenates the outputs of all attention heads and obtains the fusion feature Z through linear transformation. fused , the formula is: Z fused =Linear(Concat(Head1,Head2,...,Head i ))W O ; Among them, Linear represents the fully connected layer, W O is the linear transformation matrix, Concat represents the concatenation operation; The hierarchical classifier includes a coarse-grained classifier and a fine-grained classifier. First, the coarse-grained classifier is used to classify the fusion features Z fused Perform broad classification to obtain intermediate features H; then use the fine-grained classifier to perform refined classification based on the intermediate features to obtain the final detection result Y. The specific formula is as follows: H=ReLU(Z fused IN shared +b coarse ); Y=HW fine +b fine ; Among them, W shared 、b coarse are the weight and bias of the coarse-grained classifier, W fine 、b fine are the weights and biases of the fine-grained classifier, respectively, and ReLU is the activation function; The coarse-grained classifier and the fine-grained classifier both use the cross entropy loss function, and by introducing dynamic weights, the total loss function of the output module is obtained. The specific formula is: L total =w dynamic L coarse +(1-w dynamice )L fine ; Among them, L total Represents the total loss function, L coarse , L fine are the loss functions of the coarse-grained classifier and the fine-grained classifier respectively; w dynamic Represents dynamic weight, the formula is: In 1 dynamic =epoch / epoch max ; w 2 dynamic =L coarse / (L coarse +L fine ); Among them, epoch, epoch max Represents the training round and the maximum training round, w 1 dynamic 、W 2 dynamic Represent the dynamic weights at the early and late stages of training respectively; The evaluation indicators of each trained detection model were calculated, and the detection model corresponding to the optimal preprocessing and feature selection combination method was used as the final freshness detection model for non-destructive freshness detection of fresh chilled mutton.
2. The nondestructive detection method for freshness of chilled mutton based on a double-branch hierarchical spectral feature perception network according to claim 1 is characterized in that: The freshness indicators include: volatile basic nitrogen, total colony count and approximate E. coli count; the sample labels are mutton freshness grades, including: fresh, sub-fresh, slightly corrupt and corrupt.
3. The nondestructive detection method for freshness of chilled mutton based on a double-branch hierarchical spectral feature perception network according to claim 1 is characterized in that: The feature extraction module includes a global feature extraction branch and a local feature extraction branch. Each feature extraction branch is composed of a convolution layer, an activation function layer and a pooling layer connected in series. The global feature extraction branch performs feature extraction on the global feature data, and the local feature extraction branch performs feature extraction on the local feature data.
Citation Information
Patent Citations
Hyperspectral detection method for ripeness of camellia oleifera fruits based on deep learning and sorting equipment
CN114112932A
Hyperspectral image classification method based on interactive double-branch feature fusion network
CN117274655A
Nondestructive testing method for freshness of chicken
CN117953490A
Metalearning-based few-sample pneumonia classification method and medium
CN118864946A