Traditional Chinese medicinal material anomaly detection method and device based on mask encoder and related equipment
By using a dual-branch masked autoencoder network to jointly model and reconstruct the spatial-spectral information of hyperspectral data, and by employing a threshold correction strategy of standardized error fusion and bilateral regularization, the problems of insufficient feature utilization and inadequate robustness of decision-making mechanisms in the anomaly detection of Chinese medicinal materials are solved, thus achieving high-precision, high-robust, non-destructive, and rapid detection.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- INST OF INTELLIGENT MFG GUANGDONG ACAD OF SCI
- Filing Date
- 2025-12-26
- Publication Date
- 2026-05-19
AI Technical Summary
Existing hyperspectral technologies for detecting anomalies in Chinese medicinal materials suffer from insufficient feature utilization and decision-making robustness, resulting in inadequate detection accuracy and reliability.
A dual-branch masked autoencoder network is used to jointly model the spatial-spectral information of hyperspectral data, and the detection accuracy and robustness are improved by standardization error fusion and bilateral regularization threshold correction strategy.
It significantly improves the overall performance of abnormal detection in Chinese medicinal materials, enhances the sensitivity to identify various complex defects, reduces the false alarm rate and false negative rate, and achieves high-precision, high-robust, non-destructive rapid detection.
Smart Images

Figure CN122066635A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of hyperspectral data analysis technology, and in particular to a method, apparatus and related equipment for detecting anomalies in Chinese medicinal materials based on a mask encoder. Background Technology
[0002] The quality and safety of Chinese medicinal herbs are directly related to clinical efficacy and medication safety. Common quality defects include mold, dampness, adulteration with non-medicinal parts, insect infestation, and illegal sulfur fumigation and dyeing. Traditional identification methods, such as manual experience, microscopic observation, and physicochemical analysis, suffer from low efficiency, high subjectivity, sample destruction, and difficulty in achieving rapid online detection. Hyperspectral imaging technology, as an emerging non-destructive testing method, can simultaneously acquire spatial image information and continuous spectral information of Chinese medicinal herbs, forming a "data cube" containing rich material composition and morphological texture features. This provides an ideal data foundation for achieving multi-indicator, rapid online quality testing of Chinese medicinal herbs.
[0003] However, applying hyperspectral technology to the detection of anomalies in traditional Chinese medicine, especially in unsupervised scenarios, still faces significant challenges. First, regarding feature utilization, many existing methods tend to focus on single-dimensional information analysis, such as using only average spectra for component analysis or only spatial texture for morphological discrimination, failing to fully utilize the inherent "spatial-spectral joint" information of hyperspectral data. This results in insufficient model recognition of complex and hidden defects. Second, regarding the decision-making mechanism, the core of unsupervised anomaly detection lies in how to set a robust threshold to distinguish between normal and abnormal samples based solely on learning from normal samples. Existing methods often directly set a global threshold based on simple statistics of reconstruction error or feature distance (such as the mean plus or minus several standard deviations). This lacks adaptability to the fluctuations inherent in normal samples and the complex distribution of anomaly scores, easily leading to overly sensitive or insensitive threshold settings. Consequently, this results in high false alarm or false negative rates in practical applications, restricting the reliability and practicality of the detection system.
[0004] Therefore, developing a method for detecting anomalies in Chinese medicinal materials that can deeply integrate hyperspectral spatial and spectral information and adaptively and robustly determine anomaly thresholds under unsupervised conditions is of great significance for improving the accuracy and reliability of intelligent detection of the quality of Chinese medicinal materials. Summary of the Invention
[0005] To address the aforementioned issues, this invention provides a method, apparatus, and related equipment for detecting anomalies in Chinese medicinal materials based on a mask encoder. By fully integrating the spatial-spectral information of hyperspectral data through a dual-branch mask autoencoder network and utilizing an adaptive threshold decision mechanism based on bilateral regularization, the overall accuracy and robustness of anomaly detection in Chinese medicinal materials are improved, effectively reducing false alarms and missed detections, even when trained using only normal samples.
[0006] In a first aspect, embodiments of the present invention provide a method for detecting anomalies in traditional Chinese medicine (TCM) materials based on a mask encoder. The method includes: acquiring hyperspectral data of TCM material samples and constructing a training set containing only normal samples and a test set containing both normal and abnormal samples; constructing a bi-branch mask autoencoder network and training the bi-branch mask autoencoder network using the training set samples; wherein, the first branch of the bi-branch mask autoencoder network performs mask reconstruction of the spectral features of the training set samples, and the second branch performs mask reconstruction of the spatial image features of the training set samples; the training process jointly optimizes the network parameters by minimizing the reconstruction errors of the two branches; calculating the reconstruction errors in the training set samples and the test set samples respectively using the trained bi-branch mask autoencoder network, and fusing the reconstruction errors of the two branches to obtain a comprehensive anomaly score for each sample; determining an anomaly judgment threshold based on the comprehensive anomaly score distribution of the training set samples using a bilateral regularization method, and judging the test set samples as normal or abnormal according to the threshold.
[0007] In one possible implementation, the spectral features are the average spectrum of the training set samples, and the spatial image features are the principal component analysis images of the training set samples.
[0008] In one possible implementation, the masking method for the first branch and the second branch is a random mask or a mesh zero-mask.
[0009] In one possible implementation, the reconstruction errors of the two branches are fused to obtain a comprehensive anomaly score for each sample, including:
[0010] The reconstruction error of each sample in the first branch and the reconstruction error in the second branch are standardized separately. The standardization process is to subtract the mean of the reconstruction errors of all training set samples in the branch from the reconstruction error of a single sample in a certain branch, and then divide by the standard deviation of the reconstruction errors of all training set samples in the branch.
[0011] The standardized results are summed to obtain the overall anomaly score for each sample.
[0012] In one possible implementation, the statistical parameters on which the standardization process is based are derived from the reconstruction error of the training set samples on the corresponding branch.
[0013] In one possible implementation, an anomaly determination threshold is determined based on the comprehensive anomaly score distribution of the training set samples using a two-sided regularization method, including:
[0014] Based on the comprehensive anomaly score distribution of the training set samples, determine the low-side score boundary value and the high-side score boundary value;
[0015] The comprehensive outlier scores of the training set samples are scaled and corrected using the low-side score boundary value and the high-side score boundary value.
[0016] Based on the corrected score distribution, an anomaly detection threshold is determined.
[0017] In one possible implementation, the specific method for scaling and correcting the overall anomaly score of the training set samples is as follows:
[0018]
[0019] in, To adjust the scaling result, for The lower-side score boundary value, It is the boundary value of the high-side fraction.
[0020] Secondly, embodiments of the present invention provide a device for detecting anomalies in traditional Chinese medicine materials based on a mask encoder. This device includes: an acquisition module, a construction module, a calculation module, and a judgment module. Wherein:
[0021] The acquisition module is used to acquire hyperspectral data of Chinese medicinal material samples and construct a training set containing only normal samples and a test set containing both normal and abnormal samples.
[0022] The module is used to construct a dual-branch masked autoencoder network and train the dual-branch masked autoencoder network using training set samples. The first branch of the dual-branch masked autoencoder network performs masked reconstruction of the spectral features of the training set samples, and the second branch performs masked reconstruction of the spatial image features of the training set samples. The training process optimizes the network parameters by minimizing the reconstruction error of the two branches.
[0023] The calculation module is used to calculate the reconstruction error in the training set samples and the test set samples respectively using the trained dual-branch masked autoencoder network, and fuse the reconstruction errors of the two branches to obtain the comprehensive anomaly score of each sample.
[0024] The judgment module is used to determine the anomaly judgment threshold based on the comprehensive anomaly score distribution of the training set samples through a two-sided regularization method, and to judge the test set samples as normal or abnormal according to the threshold.
[0025] Thirdly, embodiments of the present invention provide a computer storage medium storing multiple instructions adapted for loading by a processor and executing the steps of the above-described method.
[0026] Fourthly, embodiments of the present invention provide an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being adapted to be loaded by the processor and to execute the steps of the above-described method.
[0027] The beneficial effects of the technical solutions provided by some embodiments of the present invention include at least the following: joint modeling and reconstruction of the spatial-spectral information of hyperspectral data through a dual-branch masked autoencoder network, and innovative adoption of a threshold correction strategy of standardized error fusion and bilateral regularization, thereby significantly improving the comprehensive performance of abnormal detection of Chinese medicinal materials under the premise of training with only normal samples: on the one hand, the model enhances the sensitivity and accuracy of identifying various complex defects such as mold, contamination, and adulteration through deep fusion of spatial-spectral information; on the other hand, the adaptive threshold decision mechanism effectively overcomes the influence of normal sample fluctuations and long-tail distribution, greatly reduces the false alarm rate and false negative rate, and finally achieves high-precision, high-robust, non-destructive rapid detection. Attached Figure Description
[0028] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0029] Figure 1 This is an exemplary system architecture diagram of a Chinese medicinal herb anomaly detection system based on a mask encoder, provided in an embodiment of the present invention.
[0030] Figure 2 A flowchart illustrating a method for detecting anomalies in Chinese medicinal materials based on a mask encoder, provided in an embodiment of the present invention;
[0031] Figure 3 A logic block diagram of a method for detecting anomalies in Chinese medicinal materials based on a mask encoder, provided in an embodiment of the present invention;
[0032] Figure 4 A two-dimensional mask autoencoder network structure diagram provided in an embodiment of the present invention;
[0033] Figure 5 A comparison chart of prediction results provided in an embodiment of the present invention;
[0034] Figure 6 A structural block diagram of a Chinese medicinal herb anomaly detection device based on a mask encoder provided in an embodiment of the present invention;
[0035] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention. Detailed Implementation
[0036] To make the features and advantages of the present invention more apparent and understandable, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0037] In the following description, when referring to the accompanying drawings, the same numbers in different drawings denote the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. Rather, they are merely examples of apparatuses and methods consistent with some aspects of the invention as detailed in the appended claims.
[0038] In the description of this invention, it should be understood that the terms "first," "second," etc., are used for descriptive purposes only and should not be construed as indicating or implying relative importance. Those skilled in the art can understand the specific meaning of these terms in this invention based on the specific circumstances. Furthermore, in the description of this invention, unless otherwise stated, "multiple" refers to two or more. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. The character " / " generally indicates that the preceding and following related objects have an "or" relationship.
[0039] As mentioned earlier, the intrinsic quality of Chinese medicinal herbs, as the core carrier of traditional medicine, directly affects clinical efficacy and medication safety. In actual distribution and storage, medicinal herbs are prone to various quality defects due to environmental or human factors, mainly including: dampness and mold, insect infestation, adulteration with non-medicinal parts or impurities, changes in medicinal properties due to improper processing, and adulteration due to sulfur fumigation, dyeing, or changes in origin or year. Traditional testing methods, such as manual sensory identification, microscopic observation, physicochemical analysis, and chromatography / mass spectrometry, are either highly subjective and inefficient due to reliance on experience, or cumbersome and destructive processes, making them unsuitable for the large-scale, online, and intelligent quality control requirements of the modern Chinese medicine industry.
[0040] Hyperspectral imaging technology combines the advantages of spectral analysis and digital imaging, enabling non-destructive and rapid scanning of the object under test. It simultaneously acquires the continuous spectral curves and spatial distribution images of each pixel, forming a "data cube" rich in information about material composition and morphological texture. This technology provides a highly promising data foundation for the integrated, non-contact detection of various quality defects in traditional Chinese medicine materials. In recent years, the integration of artificial intelligence algorithms, especially unsupervised anomaly detection models, has become a research hotspot in this field. These models aim to learn the inherent characteristic patterns of normal samples and automatically identify anomalous samples that deviate from those patterns.
[0041] However, when applying hyperspectral imaging and unsupervised anomaly detection to the quality monitoring of Chinese medicinal materials, existing conventional methods face two prominent technical bottlenecks:
[0042] First, the use of features is one-sided, and spatial-spectral information is not deeply integrated. The value of hyperspectral data lies in both the spectral and spatial dimensions. Many existing models either use only the average spectrum for one-dimensional analysis, losing spatial contextual information, or only extract features from two-dimensional images, ignoring crucial spectral fingerprints. This one-sided approach results in models failing to fully capture complex features that simultaneously reflect spatial anomalies and spectral characteristic changes, such as localized mold spots, uneven staining, or subtle impurity embeddings, thus inherently limiting their detection capabilities.
[0043] Secondly, the decision-making mechanism is crude, and the robustness of the anomaly detection threshold is insufficient. In unsupervised settings, the model learns the concept of "normal" only from normal samples. The key challenge is how to set a threshold with strong generalization and high stability to distinguish "abnormalities." Existing methods typically set fixed thresholds directly based on simple statistics from the model output. However, normal samples themselves have a certain degree of volatility, and their reconstruction errors may exhibit a non-Gaussian long-tail distribution. Directly adopting such rigid standards easily leads to inaccurate threshold settings: a threshold that is too strict will misclassify some normal fluctuations as anomalies, while a threshold that is too wide will cause real defects to be missed. This unstable judgment standard severely restricts the reliability and practicality of the detection system in actual production environments.
[0044] In view of this, the present invention provides a method, apparatus, and related equipment for anomaly detection in traditional Chinese medicine based on a mask encoder. The aim is to jointly model and reconstruct the spatial-spectral information of hyperspectral data through a dual-branch mask autoencoder network, and to innovatively employ a threshold correction strategy of standardized error fusion and bilateral regularization. This significantly improves the overall performance of anomaly detection in traditional Chinese medicine using only normal samples for training. On the one hand, the model enhances the sensitivity and accuracy of identifying various complex defects such as mold, contamination, and adulteration through deep fusion of spatial-spectral information. On the other hand, the adaptive threshold decision mechanism effectively overcomes the influence of fluctuations and long-tail distributions in normal samples, significantly reducing the false alarm rate and false negative rate, ultimately achieving high-precision, highly robust, and non-destructive rapid detection.
[0045] Please see Figure 1 , Figure 1 This is an exemplary system architecture diagram of a method for detecting anomalies in Chinese medicinal materials based on a mask encoder, provided in an embodiment of the present invention.
[0046] like Figure 1As shown, the system architecture may include a terminal 101, a network 102, and a server 103. The network 102 serves as the medium for providing a communication link between the terminal 101 and the server 103. The network 102 may include various types of wired or wireless communication links, such as wired communication links including fiber optic cables, twisted-pair cables, or coaxial cables, and wireless communication links including Bluetooth communication links, Wireless-Fidelity (Wi-Fi) communication links, or microwave communication links, etc.
[0047] Terminal 101 can interact with server 103 via network 102 to receive messages from or send messages to server 103. Alternatively, terminal 101 can interact with server 103 via network 102 to receive messages or data sent to server 103 by other users. Terminal 101 can be hardware or software. When terminal 101 is hardware, it can be various electronic devices, including but not limited to smartwatches, smartphones, tablets, laptops, and desktop computers. When terminal 101 is software, it can be installed in the aforementioned electronic devices and can be implemented as multiple software programs or software modules (e.g., to provide distributed services) or as a single software program or software module; no specific limitation is made here.
[0048] In this embodiment of the invention, terminal 101 can acquire hyperspectral data of Chinese medicinal material samples and construct a training set containing only normal samples and a test set containing both normal and abnormal samples; construct a bi-branch masked autoencoder network and train the bi-branch masked autoencoder network using the training set samples; wherein, the first branch of the bi-branch masked autoencoder network performs masked reconstruction of the spectral features of the training set samples, and the second branch performs masked reconstruction of the spatial image features of the training set samples, and the training process jointly optimizes the network parameters by minimizing the reconstruction errors of the two branches; the reconstruction errors in the training set samples and the test set samples are calculated using the trained bi-branch masked autoencoder network respectively, and the reconstruction errors of the two branches are fused to obtain a comprehensive abnormality score for each sample; based on the comprehensive abnormality score distribution of the training set samples, an abnormality judgment threshold is determined by a bilateral regularization method, and the test set samples are judged as normal or abnormal according to the threshold.
[0049] Server 103 can be a business server providing various services. It should be noted that server 103 can be either hardware or software. When server 103 is hardware, it can be implemented as a distributed server cluster consisting of multiple servers, or as a single server. When server 103 is software, it can be implemented as multiple software programs or software modules (e.g., used to provide distributed services), or as a single software program or software module; no specific limitations are made here.
[0050] Alternatively, the system architecture may not include server 103. In other words, server 103 may be an optional device in the embodiments of this specification. That is, the method provided in the embodiments of this specification can be applied to a system structure that only includes terminal 101. The embodiments of this invention do not limit this.
[0051] It should be understood that Figure 1 The number of terminals, networks, and servers shown is only illustrative; the number can be any number of terminals, networks, and servers depending on the implementation requirements.
[0052] Please see Figure 2 , Figure 2 This is a flowchart illustrating a method for detecting anomalies in traditional Chinese medicine (TCM) materials based on a mask encoder, provided in an embodiment of the present invention. The execution entity of this embodiment can be an electronic device performing the TCM material anomaly detection based on a mask encoder, a processor within an electronic device performing the TCM material anomaly detection method based on a mask encoder, or a TCM material anomaly detection service within an electronic device performing the TCM material anomaly detection method based on a mask encoder. For ease of description, the following uses a processor within an electronic device as an example to describe the specific execution process of the TCM material anomaly detection method based on a mask encoder.
[0053] like Figure 2 As shown, the method for detecting anomalies in Chinese medicinal materials based on a mask encoder may include at least:
[0054] S201. Obtain hyperspectral data of Chinese medicinal material samples, and construct a training set containing only normal samples and a test set containing both normal and abnormal samples.
[0055] Specifically, please refer to Figure 3 , Figure 3 Please refer to the logic block diagram of a method for detecting anomalies in Chinese medicinal materials based on a mask encoder, as provided in this embodiment of the invention. Figure 3First, hyperspectral data of Chinese medicinal herb samples must be collected. Specifically, a hyperspectral imaging system is used to scan Chinese medicinal herb samples in a known state. The samples should include confirmed normal samples and abnormal samples with clear defect types. Imaging should cover a specific wavelength range, such as 900nm to 1700nm, and should be performed under constant illumination and environmental conditions to ensure data consistency. Abnormal samples include, but are not limited to, damp samples, moldy samples, and soil-contaminated samples. After acquiring the original hyperspectral image cube, sample data needs to be constructed. First, image segmentation is performed on each sample to remove the background and extract the effective pixel region of the medicinal material. This can usually be achieved through watershed algorithms or threshold-based segmentation methods, thereby obtaining hyperspectral sub-images containing only the main body of the medicinal material. Subsequently, feature extraction is performed on these sub-images. On the one hand, the average reflectance of all pixels of each sample is calculated along the spectral dimension to form a one-dimensional average spectral vector representing its overall chemical composition. On the other hand, principal component analysis is used to reduce the dimensionality of the image in the spatial dimension, extracting two-dimensional feature images of the top n main components to capture their texture and morphological information. Based on the processed feature data, all normal samples are randomly divided into two parts. One part is used as the training set for subsequent model construction and parameter learning, while the other part is merged with all abnormal samples to form a test set for evaluating the model's generalization ability and detection performance. This division process ensures that the training process only comes into contact with the feature distribution of normal samples, which conforms to the basic paradigm of unsupervised anomaly detection.
[0056] S202. Construct a dual-branch masked autoencoder network and train it using training set samples. The first branch of the dual-branch masked autoencoder network performs masked reconstruction of the spectral features of the training set samples, and the second branch performs masked reconstruction of the spatial image features of the training set samples. The training process optimizes the network parameters by minimizing the reconstruction error of the two branches.
[0057] For details, please continue reading Figure 3 To achieve collaborative learning and feature extraction of spectral and spatial information from hyperspectral data, it is necessary to construct and train a dual-branch masked autoencoder network specifically designed for anomaly detection. Please refer to [link to relevant documentation]. Figure 4 , Figure 4 A two-dimensional masked autoencoder network structure diagram is provided for an embodiment of the present invention, such as... Figure 4As shown, the network consists of two parts: a one-dimensional masked autoencoder branch and a two-dimensional masked autoencoder branch. The first branch is a one-dimensional autoencoder network, whose input is the average spectral vector of each sample in the training set. This vector undergoes random or fixed-pattern masking before input, i.e., zeroing out a certain proportion (e.g., 30% to 80%) of the spectral band data. The network's task is to learn the latent feature representation of the masked spectrum through the encoder, and then attempt to completely reconstruct the original unmasked average spectrum through the decoder. The one-dimensional autoencoder network uses random masking or mesh-based zeroing masking, with a masking rate of 0.3–0.8 and a mask window size of [missing information]. Among them, W spectral The first branch refers to the length of the spectral signal. The second branch is a two-dimensional autoencoder network. Its input is the first n principal component images obtained after principal component analysis of the same training sample. These images are also subjected to two-dimensional spatial masking before input, that is, some regions of the image are zeroed out using a grid or random block method. The goal of this branch is to learn the contextual information of the masked spatial features and reconstruct the original principal component images. The network structures of the two branches can be designed independently. For example, the one-dimensional branch can use fully connected layers or one-dimensional convolutional layers, and the two-dimensional branch can use two-dimensional convolutional layers. Its encoder progressively compresses the input dimension to extract abstract features, while the decoder performs symmetrical upsampling or deconvolution to restore the original dimension. The masking method of the two-dimensional autoencoder network is: random masking or grid-based zeroing masking, with a masking rate of 0.3 to 0.8 and a mask window size of [missing information]. Among them, W spatial The length of a spatial signal.
[0058] Furthermore, a loss function for the mask encoder anomaly detection network is constructed, specifically consisting of a one-dimensional mask autoencoder network loss function and a two-dimensional mask autoencoder network loss function:
[0059] L = 0.4 * L AE_1D +0.6*L AE_2D
[0060] Among them, L AE_1D L AD_2D The loss functions for one-dimensional and two-dimensional masked autoencoder modules are respectively; the loss function L of the one-dimensional masked autoencoder network. AE_1D for:
[0061]
[0062] in, The average spectrum before the mask of the i-th training sample. The reconstructed spectrum is obtained by passing the masked average spectrum of the i-th training sample through a one-dimensional autoencoder network; the loss function of the two-dimensional masked autoencoder network is L. AE_2D for:
[0063]
[0064] in, The principal component image before the mask of the i-th training sample. The principal component image is the reconstructed principal component image obtained by passing the principal component image masked by the i-th training sample through a two-dimensional autoencoder network.
[0065] The training process employs the gradient descent algorithm. The reconstruction output of each training sample on both branches is calculated through forward propagation. Then, the mean square error between the reconstruction output and the original unmasked input is calculated as the reconstruction loss for each branch. The losses of the two branches are summed with preset weighting coefficients to obtain the joint loss function. The gradient of this joint loss with respect to all network weights and bias parameters is calculated through backpropagation. The parameters are then iteratively updated using an optimizer. This process is repeated until the joint loss of the network on the two reconstruction tasks converges to a stable state, thereby forcing the network to learn a robust and complementary internal representation based solely on the spectral and spatial features of normal samples.
[0066] S203. Calculate the reconstruction error in the training set samples and the test set samples using the trained dual-branch masked autoencoder network, and fuse the reconstruction errors of the two branches to obtain the comprehensive anomaly score for each sample.
[0067] Specifically, after the network training is completed, it is fixed for quantitative assessment of the anomaly degree of Chinese medicinal material samples. Please continue reading. Figure 3 For each sample in the training and testing sets, its extracted spectral and spatial image features are first input into the corresponding branches of the trained dual-branch masked autoencoder network, respectively. No masking is applied during this process; the complete original features are used as input. The network's forward propagation then outputs the corresponding reconstructed spectrum and reconstructed image. Subsequently, the original reconstruction error for each sample on each of the two branches is calculated. For example, for the first branch, the mean square error between the sample's original average spectral vector and its reconstructed spectral vector is calculated; for the second branch, the mean square error of all pixels between its original principal component image and its reconstructed image is calculated.
[0068] Furthermore, to fuse error values from two different dimensions and potentially inconsistent units into a unified and comparable anomaly index, standardization is required. First, based on all training set samples, the mean and standard deviation of the original errors on the one-dimensional branch and the two-dimensional branch are calculated separately. Then, for any sample to be evaluated, its spectral reconstruction error is subtracted from the mean of the training set's spectral errors, and the difference is divided by the standard deviation of the training set's spectral errors to obtain the standardized spectral error for that sample. The spatial reconstruction error undergoes the same process: subtracting the mean of the training set's spatial errors and then dividing by the corresponding standard deviation yields the standardized spatial error. After this step, the error values from the two branches are transformed to a zero-centered statistical scale with comparable dispersion. For example, the mean square error between the reconstructed signal obtained from the autoencoder module and the unmasked original signal can be used as the anomaly score. The anomaly is classified into a standardized combination of bi-branch anomaly components. The specific calculation method for the bi-branch standardized combination of anomaly components is as follows:
[0069]
[0070] in, For the i-th sample, the outlier score obtained from the one-dimensional branch is... It is the mean of the outlier segments obtained from the one-dimensional branch of the training samples, σ 1d It is the standard deviation of the outlier scores obtained from the one-dimensional branch of all training samples; The outlier score obtained for the i-th sample in the two-dimensional branch is... It is the mean of the outlier segments obtained from the training samples in the two-dimensional branch, σ 2d It is the standard deviation of the outlier scores obtained from all training samples in the two-dimensional branch.
[0071] The standardized spectral error and standardized spatial error obtained for each sample are combined to generate a single index characterizing the overall degree of anomalousness. The combination method involves directly adding the two standardized error values; the sum is the comprehensive anomalousness score for that sample. This score comprehensively reflects the degree of deviation of the sample from the overall distribution of normal samples learned by the network, across the two key dimensions of spectral composition and spatial morphology. A higher score indicates that the sample is more likely to be anomalous.
[0072] S204. Based on the comprehensive anomaly score distribution of the training set samples, determine the anomaly judgment threshold through a two-sided regularization method, and judge the test set samples as normal or abnormal according to the threshold.
[0073] Specifically, after obtaining the comprehensive anomaly scores for all samples in the training set, a threshold for the final judgment needs to be determined based on the distribution characteristics of these scores. Please continue reading. Figure 3First, analyzing the overall distribution of abnormal scores in the training set reveals an asymmetrical, long-tailed distribution pattern. Most scores cluster in the lower value range, but a small number of samples have significantly higher scores. Directly setting a global threshold based on this raw distribution is highly susceptible to the inappropriate influence of the high-value tails, leading to an overly high threshold and missed anomalies in subsequent tests.
[0074] To address this issue, a two-sided regularization strategy is employed to correct the score distribution. This method first determines the statistical boundary values for the low and high sides based on the distribution of the training set scores. Specifically, the low-side boundary value is obtained by subtracting a certain number of standard deviations from the mean of all scores, defining the lower limit of normal score fluctuations; the high-side boundary value is obtained by adding a certain number of standard deviations to the mean, defining the theoretical upper limit of normal score fluctuations. These two boundary values together define the expected main fluctuation range of normal sample scores in the training set. Subsequently, these two boundary values are used to scale and transform the original composite abnormal scores of each training set sample. For any original score, the aforementioned low-side boundary value is subtracted, and the resulting difference is divided by the difference between the high and low side boundary values. This operation linearly maps the original scores of all training set samples to a new numerical range based on their own distribution characteristics. After this step, the original long-tailed distribution is effectively "compressed" and regularized into a more concentrated and symmetrical distribution shape, significantly reducing the impact of extremely high scores on the overall distribution shape. Based on this, the final anomaly detection threshold is determined using the score distribution of the training set after scaling transformation. This threshold can be set according to actual application needs; for example, a high quantile of the transformed score can be chosen as the threshold, or the corresponding score can be derived by back-calculating the cumulative distribution function after scaling based on an acceptable preset false alarm rate. This threshold is the critical criterion used to judge whether a new sample is abnormal. Finally, for each sample in the test set, its comprehensive anomaly score is first calculated using the exact same process as the training set, and the original score of the test sample is scaled using the same low-side and high-side boundary values calculated from the training set. Then, this scaled score is compared with the aforementioned final detection threshold: if the score is greater than the threshold, the sample is judged as abnormal; if it is less than or equal to the threshold, it is judged as normal. Through this series of adaptive processing based on the training set distribution, more accurate and robust detection of abnormal samples can be achieved while effectively suppressing the fluctuation interference of normal samples themselves. Please refer to [link to relevant documentation]. Figure 5 , Figure 5 A comparison chart of prediction results provided for an embodiment of the present invention, such as Figure 5 As shown, the anomaly identification for the original training samples is... The result was obtained by modifying the method using bilateral regularization. The specific bilateral regularization method is as follows:
[0075]
[0076] in, The abnormal score is obtained from the low abnormal score side according to the "3σ criterion". The abnormal score is obtained from the side with high anomaly scores according to the "3σ criterion". Then, based on the corrected distribution of abnormal scores in the training set, a long-tail correction threshold H′ is determined according to a given false alarm rate ρ or the "3σ criterion" to determine whether the test sample is normal or abnormal. Normal samples undergo 10 random samplings and corresponding training, and the average value of the anomaly detection results is used for model evaluation. One-dimensional autoencoder networks (AE_1D) and two-dimensional autoencoder networks (AE_2D) are used as two comparison methods. Both comparison methods use reconstruction error as the anomaly score, without correction for the anomaly score, and the final threshold value method is consistent with the method of this invention. The model is evaluated using the area under the curve (AUC), F1 score, and accuracy for each class of samples. The calculation results show that the method of this invention has an average AUC of 97.1% for this dataset, the one-dimensional autoencoder network has an average AUC of 91.9%, and the two-dimensional autoencoder network has an average AUC of 82.0%, indicating a significant improvement in the accuracy of anomaly identification.
[0077] This application provides a mask encoder-based method for detecting anomalies in traditional Chinese medicine (TCM) materials. It employs a dual-branch mask autoencoder network to jointly model and reconstruct the spatial-spectral information of hyperspectral data. Furthermore, it innovatively adopts a threshold correction strategy that combines standardized error fusion with bilateral regularization. This significantly improves the overall performance of TCM anomaly detection even when trained using only normal samples. On one hand, the model enhances the sensitivity and accuracy of identifying various complex defects such as mold, contamination, and adulteration through deep fusion of spatial-spectral information. On the other hand, the adaptive threshold decision mechanism effectively overcomes the influence of fluctuations and long-tailed distributions in normal samples, greatly reducing false alarm and false negative rates, ultimately achieving high-precision, robust, and non-destructive rapid detection.
[0078] Please see Figure 6 , Figure 6 This is a structural block diagram of a Chinese medicinal herb anomaly detection device based on a mask encoder, provided as an embodiment of the present invention. Figure 6 As shown: The Chinese medicinal herb anomaly detection device 600 based on a mask encoder includes: an acquisition module 610, a construction module 620, a calculation module 630, and a judgment module 640. Wherein:
[0079] The acquisition module 610 is used to acquire hyperspectral data of Chinese medicinal material samples and construct a training set containing only normal samples and a test set containing both normal and abnormal samples.
[0080] Module 620 is used to construct a dual-branch mask autoencoder network and train the dual-branch mask autoencoder network using training set samples. The first branch of the dual-branch mask autoencoder network performs mask reconstruction of the spectral features of the training set samples, and the second branch performs mask reconstruction of the spatial image features of the training set samples. The training process optimizes the network parameters by minimizing the reconstruction error of the two branches.
[0081] The calculation module 630 is used to calculate the reconstruction error in the training set samples and the test set samples respectively using the trained dual-branch masked autoencoder network, and fuse the reconstruction errors of the two branches to obtain the comprehensive anomaly score of each sample.
[0082] The determination module 640 is used to determine the anomaly determination threshold based on the comprehensive anomaly score distribution of the training set samples through a two-sided regularization method, and to determine whether the test set samples are normal or abnormal based on the threshold.
[0083] In some possible embodiments, the spectral features are the average spectrum of the training set samples, and the spatial image features are the principal component analysis images of the training set samples.
[0084] In some possible embodiments, the first branch and the second branch are masked by random masking or mesh zeroing masking.
[0085] In some possible embodiments, the computing module 630 includes:
[0086] The standardization unit is used to standardize the reconstruction error of each sample in the first branch and the reconstruction error in the second branch respectively. The standardization process is to subtract the mean of the reconstruction errors of all training set samples in the branch from the reconstruction error of a single sample in a certain branch, and then divide by the standard deviation of the reconstruction errors of all training set samples in the branch.
[0087] The obtained unit is used to sum the standardized results to obtain the comprehensive anomaly score corresponding to the sample.
[0088] In some possible implementations, the statistical parameters on which the normalization process is based are derived from the reconstruction error of the training set samples on the corresponding branch.
[0089] In some possible embodiments, the determination module 640 includes:
[0090] The first determining unit is used to determine the low-side score boundary value and the high-side score boundary value based on the comprehensive abnormal score distribution of the training set samples.
[0091] The correction unit is used to scale and correct the overall outlier scores of the training set samples using the low-side score boundary value and the high-side score boundary value.
[0092] The second determining unit is used to determine the anomaly detection threshold based on the corrected score distribution.
[0093] In some possible embodiments, the specific calculation formula for the correction unit is as follows:
[0094]
[0095] in, To adjust the scaling result, for The lower-side score boundary value, It is the boundary value of the high-side fraction.
[0096] It should be noted that the above embodiments of the herbal medicine anomaly detection device based on a mask encoder, when executing the herbal medicine anomaly detection method based on a mask encoder, are only illustrative examples of the above functional module division. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. Furthermore, the herbal medicine anomaly detection device based on a mask encoder and the herbal medicine anomaly detection method embodiments provided above belong to the same concept, and their implementation process is detailed in the method embodiments, which will not be repeated here.
[0097] The sequence numbers of the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0098] Please see Figure 7 , Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention. Figure 7 As shown, the electronic device 700 may include: at least one processor 701, at least one network interface 704, a user interface 703, a memory 705, and at least one communication bus 702.
[0099] The communication bus 702 is used to enable communication between these components.
[0100] The user interface 703 may include a display screen and a camera. Optional user interfaces 703 may include standard wired interfaces and wireless interfaces.
[0101] The network interface 704 may optionally include a standard wired interface or a wireless interface (such as a Wi-Fi interface).
[0102] The processor 701 may include one or more processing cores. The processor 701 connects to various parts within the electronic device 700 using various interfaces and lines, and performs various functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in the memory 705, and by calling data stored in the memory 705. Optionally, the processor 701 may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 701 may integrate one or a combination of several of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), and modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the content required for display; and the modem handles wireless communication. It is understood that the modem may also not be integrated into the processor 701 and may be implemented as a separate chip.
[0103] The memory 705 may include random access memory (RAM) or read-only memory. Optionally, the memory 705 may include a non-transitory computer-readable storage medium. The memory 705 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 705 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as touch function, sound playback function, image playback function, etc.), instructions for implementing the above-described method embodiments, etc.; the data storage area may store data involved in the above-described method embodiments, etc. Optionally, the memory 705 may also be at least one storage device located remotely from the aforementioned processor 701. Figure 7 As shown, the memory 705, which serves as a computer storage medium, may include an operating system, a network communication module, a user interface module, and a Chinese medicinal herb anomaly detection application based on a mask encoder.
[0104] exist Figure 7In the illustrated electronic device 700, the user interface 703 is mainly used to provide an input interface for the user and acquire user input data; while the processor 701 can be used to call the Chinese herbal medicine anomaly detection application based on a mask encoder stored in the memory 705, and specifically perform the following operations: acquire hyperspectral data of Chinese herbal medicine samples, and construct a training set containing only normal samples and a test set containing both normal and abnormal samples; construct a dual-branch mask autoencoder network, and train the dual-branch mask autoencoder network using the training set samples; wherein, the first branch of the dual-branch mask autoencoder network performs mask reconstruction of the spectral features of the training set samples, and the second branch performs mask reconstruction of the spatial image features of the training set samples, and the training process jointly optimizes the network parameters by minimizing the reconstruction errors of the two branches; calculate the reconstruction errors in the training set samples and the test set samples respectively using the trained dual-branch mask autoencoder network, and fuse the reconstruction errors of the two branches to obtain a comprehensive anomaly score for each sample; based on the comprehensive anomaly score distribution of the training set samples, determine the anomaly judgment threshold through a bilateral regularization method, and judge the test set samples as normal or abnormal according to the threshold.
[0105] In some possible embodiments, the spectral features are the average spectrum of the training set samples, and the spatial image features are the principal component analysis images of the training set samples.
[0106] In some possible embodiments, the first branch and the second branch are masked by random masking or mesh zeroing masking.
[0107] In some possible embodiments, processor 701 performs the fusion of reconstruction errors from the two branches to obtain a comprehensive anomaly score for each sample, specifically by performing:
[0108] The reconstruction error of each sample in the first branch and the reconstruction error in the second branch are standardized separately. The standardization process is to subtract the mean of the reconstruction errors of all training set samples in the branch from the reconstruction error of a single sample in a certain branch, and then divide by the standard deviation of the reconstruction errors of all training set samples in the branch. The standardized results are then summed to obtain the comprehensive anomaly score of the sample.
[0109] In some possible implementations, the statistical parameters on which the normalization process is based are derived from the reconstruction error of the training set samples on the corresponding branch.
[0110] In some possible embodiments, processor 701 executes a comprehensive anomaly score distribution based on training set samples, and determines anomaly determination thresholds using a two-sided regularization method, specifically for executing:
[0111] Based on the comprehensive anomaly score distribution of the training set samples, determine the low-side score boundary value and the high-side score boundary value;
[0112] The comprehensive outlier scores of the training set samples are scaled and corrected using the low-side score boundary value and the high-side score boundary value.
[0113] Based on the corrected score distribution, an anomaly detection threshold is determined.
[0114] In some possible embodiments, the processor 701 performs the scaling correction of the overall anomaly score of the training set samples in the following specific manner:
[0115]
[0116] in, To adjust the scaling result, for The lower-side score boundary value, It is the boundary value of the high-side fraction.
[0117] This invention also provides a computer-readable storage medium storing instructions that, when executed on a computer or processor, cause the computer or processor to perform the above-described instructions. Figure 2 One or more steps in the illustrated embodiment. If the constituent modules of the above-described mask encoder-based anomaly detection device for Chinese medicinal materials are implemented as software functional units and sold or used as independent products, they can be stored in the computer-readable storage medium.
[0118] In the above embodiments, implementation can be achieved, in whole or in part, through software, hardware, firmware, or any combination thereof. When implemented in software, it can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted through the computer-readable storage medium. The computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, Digital Subscriber Line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium accessible to a computer or a data storage device such as a server or data center that integrates one or more available media. The available media may be magnetic media (e.g., floppy disks, hard disks, magnetic tapes), optical media (e.g., digital versatile discs (DVDs)), or semiconductor media (e.g., solid-state drives (SSDs)).
[0119] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. This program can be stored in a computer-readable storage medium, and when executed, it can include the processes of the embodiments of the methods described above. The aforementioned storage medium includes various media capable of storing program code, such as read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks. Unless otherwise specified, the technical features of this embodiment and its implementation schemes can be combined arbitrarily.
[0120] The above description of the disclosed embodiments enables those skilled in the art to make or use the invention. It will be apparent to those skilled in the art that various modifications can be made to these embodiments, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for detecting anomalies in traditional Chinese medicinal materials based on a mask encoder, characterized in that, The method includes: Obtain hyperspectral data of Chinese medicinal material samples, and construct a training set containing only normal samples and a test set containing both normal and abnormal samples. A dual-branch masked autoencoder network is constructed and trained using the training set samples. The first branch of the dual-branch masked autoencoder network performs masked reconstruction of the spectral features of the training set samples, and the second branch performs masked reconstruction of the spatial image features of the training set samples. The training process optimizes the network parameters by minimizing the reconstruction error of the two branches. The reconstruction error in the training set samples and the test set samples is calculated using a trained dual-branch masked autoencoder network, and the reconstruction errors of the two branches are fused to obtain a comprehensive anomaly score for each sample. Based on the comprehensive anomaly score distribution of the training set samples, an anomaly judgment threshold is determined by a two-sided regularization method, and the test set samples are judged as normal or abnormal according to the threshold.
2. The method as described in claim 1, characterized in that, The spectral features are the average spectra of the training set samples, and the spatial image features are the principal component analysis images of the training set samples.
3. The method as described in claim 2, characterized in that, The masking method for the first branch and the second branch is either random masking or mesh zeroing masking.
4. The method as described in claim 1, characterized in that, The fusion of the reconstruction errors from the two branches to obtain a comprehensive anomaly score for each sample includes: The reconstruction error of each sample in the first branch and the reconstruction error in the second branch are respectively standardized; wherein, the standardization process is to subtract the mean of the reconstruction errors of all training set samples in the branch from the reconstruction error of a single sample in a certain branch, and then divide by the standard deviation of the reconstruction errors of all training set samples in the branch. The standardized results are summed to obtain the comprehensive anomaly score corresponding to the sample.
5. The method as described in claim 4, characterized in that, The statistical parameters upon which the standardization process is based are derived from the reconstruction error of the training set samples on the corresponding branch.
6. The method as described in claim 1, characterized in that, The comprehensive anomaly score distribution based on the training set samples, and the determination of the anomaly judgment threshold using a two-sided regularization method, include: Based on the comprehensive anomaly score distribution of the training set samples, determine the low-side score boundary value and the high-side score boundary value; The comprehensive outlier scores of the training set samples are scaled and corrected using the low-side score boundary values and the high-side score boundary values. The anomaly detection threshold is determined based on the corrected score distribution.
7. The method as described in claim 6, characterized in that, The specific method for scaling and correcting the overall anomaly score of the training set samples is as follows: in, To adjust the scaling result, for The lower-side score boundary value, It is the boundary value of the high-side fraction.
8. A device for detecting anomalies in traditional Chinese medicinal materials based on a mask encoder, characterized in that, The device includes: The acquisition module is used to acquire hyperspectral data of Chinese medicinal material samples and construct a training set containing only normal samples and a test set containing both normal and abnormal samples. A construction module is used to construct a dual-branch masked autoencoder network and train the dual-branch masked autoencoder network using the training set samples; wherein, the first branch of the dual-branch masked autoencoder network performs masked reconstruction of the spectral features of the training set samples, and the second branch performs masked reconstruction of the spatial image features of the training set samples. The training process jointly optimizes the network parameters by minimizing the reconstruction error of the two branches. The calculation module is used to calculate the reconstruction error in the training set samples and the test set samples respectively using the trained dual-branch masked autoencoder network, and fuse the reconstruction errors of the two branches to obtain the comprehensive anomaly score of each sample. The determination module is used to determine the anomaly determination threshold based on the comprehensive anomaly score distribution of the training set samples using a two-sided regularization method, and to determine whether the test set samples are normal or abnormal based on the threshold.
9. A computer storage medium, characterized in that, The computer storage medium stores a plurality of instructions adapted for loading by a processor and executing the steps of the method as described in any one of claims 1 to 7.
10. An electronic device, characterized in that, It includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the program, implements the steps of the method as described in any one of claims 1 to 7.