A method for detecting abnormalities in agricultural product quality traceability

By constructing a dynamic graph structure for multi-source heterogeneous data and causal dilation convolution feature extraction, combined with a multi-granularity attention mechanism and a cost-sensitive loss function, the class imbalance and cost sensitivity problems of multi-source heterogeneous data in agricultural product quality traceability are solved, the causal relationship between operation events and environmental parameters is explicitly modeled, and the accuracy and robustness of anomaly detection are improved.

CN120410342BActive Publication Date: 2025-09-05HUZHOU WATER SHIELD PERCH ECOLOGICAL AGRI DEV CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202510925244.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-07-05
Publication Date
2025-09-05
Estimated Expiration
2045-07-05

AI Technical Summary

Technical Problem

Existing technologies fail to effectively deal with the category imbalance and cost sensitivity problems of multi-source heterogeneous data in the process of agricultural product quality traceability, and are unable to explicitly model the causal relationship between operational events and environmental parameters, resulting in the submerging of abnormal signals, making it difficult to capture the hiddenness and complexity of abnormal patterns, and difficult to distinguish abnormal types that rely on specific scale characteristics.

Method used

By constructing a dynamic graph structure of multi-source heterogeneous data, using agricultural operation events as nodes and environmental parameters as edge attributes, combining temporal causal edge sets and multi-granularity attention mechanism, adopting causal dilated convolution and hierarchical graph convolution feature extraction, building an anomaly detection model, and using cost-sensitive focal loss function to optimize training.

Benefits of technology

Explicitly modeling the cross-modal dependency between operational events and environmental parameters captures the spatiotemporal correlation of multi-source data, solves the problem of abnormal signals being drowned out, improves the model's ability to process complex time series data, optimizes category imbalance and cost-sensitivity issues, and improves the robustness and reliability of the model.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120410342B_ABST
    Figure CN120410342B_ABST
Patent Text Reader

Abstract

This invention discloses a method for detecting anomalies in agricultural product quality traceability, including the collection and annotation of agricultural product quality traceability data, the construction of a dynamic graph structure for multi-source heterogeneous data, the construction of an anomaly detection model, and the use of a trained model to perform real-time or batch anomaly detection on newly incoming agricultural product production data. This method effectively captures the spatiotemporal correlations of multi-source data, resolving the problem of anomaly signals being drowned out in traditional methods. It also adaptively aggregates features of varying granularity, addressing the loss of fine-grained information in traditional global pooling methods. Furthermore, it employs a causal dilation convolutional layer to address the difficulty traditional RNN-like models have in capturing very long-range temporal dependencies, balancing the costs of false positives and false negatives, and improving the robustness and reliability of the model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data processing and artificial intelligence technology, and in particular to a method for detecting anomalies in agricultural product quality traceability. Background Art

[0002] As public awareness of food safety and quality continues to grow, ensuring the traceability and monitoring of agricultural products from production to consumption has become increasingly important. However, the agricultural production process involves a wide range of factors, including environmental parameters, agricultural operations, and laboratory test results. This data comes from a wide range of sources, is diverse in type, and has inconsistent temporal resolution, resulting in a typical multi-source, heterogeneous data scenario.

[0003] In the existing technology, such as the patents with publication numbers CN120034384A and CN119995910A, there is a problem that does not consider the causal relationship between operational events and environmental parameters, resulting in the submergence of key abnormal signals and the inability to effectively detect anomalies. There is a lack of explicit modeling of the spatiotemporal correlation between operational events and environmental parameters, making it difficult to capture the hiddenness and complexity of abnormal patterns, and it is easy to lose fine-grained discriminant information, making it impossible for the model to distinguish abnormal types that rely on specific scale features. It is insensitive to small sample categories and cannot effectively handle category imbalance and cost sensitivity problems, resulting in the model being biased towards the majority class and ignoring anomaly detection. Summary of the Invention

[0004] In order to solve the technical problems of processing category imbalance and cost sensitivity in the prior art, the present invention provides a method for detecting anomalies in agricultural product quality traceability.

[0005] The present invention is achieved through the following technical solutions:

[0006] A method for detecting anomalies in agricultural product quality traceability, comprising:

[0007] S1. Agricultural product quality traceability data collection: specifically, collecting data related to the entire agricultural product production process through a multi-source heterogeneous approach, including data covering environmental parameters, agricultural operation events, and test results;

[0008] S2. Agricultural product quality traceability data labeling, specifically assigning labels to the collected raw data to construct the training dataset required for supervised learning;

[0009] S3. Dynamic graph structure construction of multi-source heterogeneous data, specifically including agricultural operation events as nodes and environmental parameters as edge attributes;

[0010] S4. Anomaly detection model construction, the model includes an input layer, a core processing module, and an output layer. The input layer receives the dynamic graph structure data of the multi-source heterogeneous data, and the output layer maps the aggregated features to the anomaly probability distribution through a fully connected network;

[0011] S5. Agricultural product quality traceability anomaly detection: Use the trained model to perform real-time or batch anomaly detection on newly incoming agricultural product production data.

[0012] Furthermore, the construction of the dynamic graph structure of multi-source heterogeneous data includes defining a set of agricultural operation nodes based on the agricultural operation events occurring at the current moment, defining a set of temporal causal edges by quantifying the causal strength through covariance and standard deviation, and encoding environmental parameters such as temperature and humidity into an edge feature matrix.

[0013] Furthermore, the process of defining the temporal causal edge set also includes applying a preset threshold for judgment. When the causal score exceeds the threshold, an edge is added between the nodes to construct an edge set to represent the temporal dependency relationship to capture the causal influence between the operation events, which is expressed as:

[0014]

[0015] Where, express The dynamic graph edge set at each moment is used to model the dependencies between nodes; Represents a connection node and The edge indicates that there is a temporal causal relationship between the two nodes; A collection of dynamic graph nodes Middle nodes; A collection of dynamic graph nodes Middle nodes; Calculate the node association function; is the causal determination threshold.

[0016] Furthermore, the core processing module includes time-aware feature alignment embedding, multi-scale anomaly enhancement based on spectral residual, hierarchical graph convolution feature extraction, time-series causal dilation convolution and multi-granularity attention aggregation.

[0017] Furthermore, the time-series-aware feature alignment embedding adopts a dual-stream feature alignment module, which combines the event stream and the sensor stream to align the time-series features of multi-source data through a learnable delay operator.

[0018] Furthermore, the multi-scale anomaly enhancement based on spectral residuals includes performing fast Fourier transform, calculating the logarithmic amplitude spectrum and spectral residuals, applying inverse Fourier transform to reconstruct residual features based on the spectral residuals and the original phase angle, and superimposing the residual feature matrix on the original alignment features with preset weight coefficients to generate enhanced features.

[0019] Furthermore, the hierarchical graph convolution feature extraction is based on a two-layer graph convolutional network. The bottom-layer operation graph convolution models the spatial dependency between nodes, and the top-layer cross-batch graph convolution captures the abnormal propagation pattern between samples to extract hierarchical features.

[0020] Furthermore, the multi-granularity attention aggregation adopts a dual-path attention mechanism, including time-granularity attention and feature-granularity attention, to adaptively aggregate features of different granularities.

[0021] Furthermore, the loss function of the anomaly detection model is a cost-sensitive focal loss function, which is constructed by combining category weights and modulation factors.

[0022] Furthermore, the loss function is expressed as:

[0023]

[0024] Where, is the total loss function; is the positive sample loss component; is the negative sample loss component; N is the total number of samples in the training set; C is the total number of categories; is the positive and negative sample loss weight factor of the loss function.

[0025] Compared with the prior art, the present invention has the following beneficial effects:

[0026] Based on a dynamic graph structure, this approach uses agricultural operation events as nodes and environmental parameters as edge attributes to explicitly model the cross-modal dependencies between operation events and environmental parameters, effectively capturing the spatiotemporal correlations of multi-source data. By capturing the causal influences between operation events through a collection of temporal causal edges, it solves the problem of abnormal signals being drowned out in traditional methods.

[0027] A dual-path attention mechanism based on time and feature granularity is used to adaptively aggregate features of different granularities and focus on key information, solving the problem of traditional global pooling methods losing fine-grained information. The feature aggregation granularity can be flexibly adjusted according to the detection requirements of different anomaly types.

[0028] The causal expansion convolution layer uses an exponentially increasing expansion rate to expand the receptive field, modeling long-term lag effects while ensuring temporal causality. This solves the problem that traditional RNN models have difficulty capturing very long-range temporal dependencies. The gating mechanism enhances the nonlinear expression of features, further improving the model's ability to handle complex time series data.

[0029] Combining class weights and modulation factors to construct a loss function addresses class imbalance and cost sensitivity, optimizing model training and making the model more attentive to rare, abnormal samples. Modulating the positive and negative sample loss terms balances the costs of false positives and false negatives, improving the model's robustness and reliability. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0031] Figure 1 This is a flow chart of a method for detecting anomalies in agricultural product quality traceability according to an embodiment of the present application;

[0032] Figure 2 This is the F1 score comparison result of different anomaly detection methods;

[0033] Figure 3 is the latency comparison result of different anomaly detection methods;

[0034] Figure 4 is the comparison result of false alarm rate of different anomaly detection methods;

[0035] Figure 5 This is a comparison of the training and inference time costs of different anomaly detection methods. DETAILED DESCRIPTION

[0036] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.

[0037] The following describes the embodiments of the present invention through specific examples. Those skilled in the art can easily understand other advantages and effects of the present invention from the contents disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. The present invention can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that, in the absence of conflict, the following embodiments and features in the embodiments can be combined with each other. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0038] It should also be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present invention. The illustrations only show components related to the present invention and are not drawn according to the number, shape and size of components in actual implementation. In actual implementation, the form, quantity and proportion of each component can be changed at will, and the component layout form may also be more complicated.

[0039] See also Figure 1 , a method for detecting anomalies in agricultural product quality traceability, comprising the following steps:

[0040] S1. Agricultural product quality traceability data collection

[0041] Specifically, data related to the entire process of agricultural product production is collected in a multi-source heterogeneous manner, covering key dimensions such as environmental parameters, agricultural operation events and test results.

[0042] The training data of the present invention comes from the environmental sensor network, the agricultural operation recording system and the laboratory testing platform, and specifically includes:

[0043] Environmental sensors, such as temperature and humidity sensors, light sensors, and soil pH sensors, are deployed in farmland or greenhouses to automatically collect real-time environmental parameters at a frequency of minutes.

[0044] The agricultural operation recording system uses mobile terminals or IoT devices to record events such as fertilization, spraying, irrigation, and harvesting manually or automatically triggered by farmers or managers. Event data is stored in the form of discrete time points, specifically at the daily level or event trigger level frequency;

[0045] The laboratory testing platform regularly conducts chemical and biological tests on agricultural product samples and generates test report data, such as pesticide residues, heavy metal content, and microbial indicators. The testing frequency is set according to the production cycle, such as before and after harvest.

[0046] The collected content specifically includes timestamp, environmental parameter values, operation event type and detection result indicators. Among them, the timestamp is accurate to the minute or the time when the event occurs, environmental parameter values ​​such as temperature, humidity, light intensity, etc., operation event type such as fertilizer type, spray dosage, etc., and detection result indicators such as abnormal threshold exceeding mark.

[0047] S2. Agricultural product quality traceability data labeling

[0048] The collected raw data is labeled based on authoritative detection results and expert knowledge to build the training dataset required for supervised learning, ensuring that subsequent anomaly detection models can accurately identify normal and abnormal patterns.

[0049] The annotation method adopts a multi-stage manual and automatic combination method. The specific steps are as follows:

[0050] First, using quantitative indicators from laboratory test reports as the gold standard, such as excessive pesticide residues and heavy metal pollution concentrations, domain experts label data points as "normal" or "abnormal" based on preset thresholds. The preset thresholds can be manually determined or based on national standards or industry regulations. Abnormal categories are further subdivided into subcategories such as chemical pollution and biological pollution to match actual production issues.

[0051] Secondly, for time periods not covered by the test report, semi-automatic annotation is performed using expert rules, such as continuous environmental parameter mutations or high-frequency operation events, supplemented by manual review to ensure consistency.

[0052] The labeled dataset strictly aligns the collected original features to ensure the consistency of input and output dimensions. For example, each labeled sample contains a timestamp, environmental sensor readings, agricultural operation codes, and corresponding anomaly labels.

[0053] S3, Dynamic Graph Structure Construction of Multi-source Heterogeneous Data

[0054] Agricultural product quality traceability data involves multi-source heterogeneous time series data, such as environmental sensors, agricultural operations, and test reports. The data dimensions vary greatly, the temporal and spatial correlations are strong, and the abnormal patterns are hidden. Conventional processing methods directly splice multi-source features together, but ignore the causal relationship between operational events and environmental parameters, resulting in the key abnormal signals being submerged and the inability to effectively detect anomalies.

[0055] The present invention constructs a dynamic graph structure, using agricultural operation events as nodes and environmental parameters as edge attributes to explicitly model the cross-modal dependency between operation events and environmental parameters, thereby capturing the spatiotemporal correlation of multi-source data and preventing abnormal signals from being overwhelmed. The specific steps are as follows:

[0056] S301: Define a set of agricultural operation nodes

[0057] When constructing the dynamic graph structure, the present invention defines a set of nodes based on the agricultural operation events occurring at the current moment, such as fertilizing or spraying. Each node corresponds to a specific operation instance. These nodes are used to represent discrete agricultural operation events, forming the basic structure of the dynamic graph, thereby explicitly encoding the operation information, which can be expressed as:

[0058]

[0059] Where, express A collection of dynamic graph nodes at each moment, used to encode agricultural operation events;

[0060] Indicates the A farming operation node represents a specific event;

[0061] Index for agricultural operations, traverse all The operations that occur at the moment;

[0062] for The set of agricultural operation events at the moment, including all operation instances to be modeled;

[0063] It is a time index, representing the current moment.

[0064] S302: Define a temporal causal edge set

[0065] Calculate the temporal causal correlation between nodes, quantify the causal strength through covariance and standard deviation, and apply a preset threshold for judgment. When the causal score exceeds the threshold, add an edge between the nodes and construct an edge set to represent the temporal dependency relationship to capture the causal influence between operation events, which is expressed as:

[0066]

[0067] Where, express The dynamic graph edge set at each moment is used to model the dependencies between nodes;

[0068] Represents a connection node and The edge indicates that there is a temporal causal relationship between the two nodes;

[0069] A collection of dynamic graph nodes Middle nodes;

[0070] A collection of dynamic graph nodes Middle nodes;

[0071] The node correlation calculation function quantifies the causal strength through covariance and standard deviation. The calculation method is expressed as ;

[0072] For nodes and The covariance of , which measures the correlation of its changing trend;

[0073] For nodes Standard deviation, used to normalize covariance;

[0074] For nodes The standard deviation of , used to normalize the covariance;

[0075] is the causal determination threshold, such as, ,when When the edge exists.

[0076] S303: Define edge feature matrix

[0077] Environmental parameters such as temperature and humidity are encoded as edge feature matrices. Each edge corresponds to a feature vector that stores the environmental state when the operation occurs to solve the heterogeneity problem of multi-source data. It is expressed as:

[0078]

[0079] Where, express The edge feature matrix at the time is used to encode the environment parameters as edge attributes and store the environment parameters;

[0080] Edge Set The size of determines the number of matrix rows;

[0081] is the feature dimension, indicating the number of environmental parameters;

[0082] definition Edge feature matrix at time No. Rank The elements of the column are , Characterizing Edges The feature vector of , encoding the state of the environment when the operation occurs, that is, ;

[0083] represents a real matrix space with dimensions equal to the number of edges times the feature dimension.

[0084] It should be noted that the edge feature matrix is ​​part of the graph structure, but it does not need to be used for processing and feature extraction. It is only used when storing the graph structure.

[0085] S4. Anomaly detection model construction

[0086] S401. Define the anomaly detection model architecture

[0087] The present invention integrates multiple modules to build an anomaly detection model. The model architecture consists of an input layer, a core processing module, and an output layer:

[0088] The input layer receives the structural data of the dynamic graph of the multi-source heterogeneous data, including a node set, an edge set and an edge feature matrix, as well as raw time series data, such as an environmental sensor stream and a farming operation event stream;

[0089] The core processing modules include time-aware feature alignment embedding, multi-scale anomaly enhancement based on spectral residual, hierarchical graph convolution feature extraction, time-series causal expansion convolution, and multi-granularity attention aggregation.

[0090] The output layer maps the aggregated features to the abnormal probability distribution through a fully connected network.

[0091] The entire architecture adopts a modular design, and data is transferred between sub-modules through feature tensors. For example, the output of the feature alignment module is directly input into the anomaly enhancement module to ensure the coherence of information flow.

[0092] The model parameters consist of trainable weight matrices and vectors, which are optimized through the loss function.

[0093] S402, time-aware feature alignment embedding

[0094] In terms of feature alignment, due to differences in sensor sampling frequencies, such as minute-level temperature and humidity data and daily agricultural operation data, features are misaligned in time. Conventional processing methods such as linear interpolation use direct interpolation to align data, but this generates noise, destroys the sudden characteristics of events, and cannot capture the delayed impact of agricultural operations on the environment, thus affecting the accuracy of anomaly detection.

[0095] This paper uses a dual-stream feature alignment module, combining event streams and sensor streams, to align the temporal features of multi-source data through a learnable delay operator to model the delayed impact of agricultural operations on the environment and ensure that features are synchronized in time. The specific steps are as follows:

[0096] 1) Calculate the importance weight of the operation

[0097] In feature alignment embedding, we first learn the importance weight of each agricultural operation node. Using a trainable weight matrix and query vector, we generate weight values ​​through the hyperbolic tangent and softmax functions, and filter key operation events to focus on important features, which can be expressed as:

[0098]

[0099] Where, Indicates the The importance weight of each farming operation is used to screen key events;

[0100] Index for agricultural operations, traverse Dynamic graph node collection at the moment All nodes in

[0101] Indicates the A farming operation node represents a specific event;

[0102] is a trainable weight matrix used for feature transformation;

[0103] is a trainable query vector used for the attention mechanism;

[0104] is the hyperbolic tangent activation function;

[0105] It is a Softmax normalized exponential function to ensure that the sum of weights is 1.

[0106] 2) Reconstructing event stream features

[0107] By integrating the operation importance weight and the exponential decay factor based on the time difference, the node features are mapped through the multi-layer perceptron and element-by-element multiplication is applied to generate the event stream feature vector, which models the timeliness of the event impact, expressed as:

[0108]

[0109] Where, express The event stream feature vector at the moment;

[0110] is the output of the multi-layer perceptron, Indicates that Mapping to latent space;

[0111] For the The moment when a farming operation occurs;

[0112] Indicates the current time and operation time The absolute value of the time difference;

[0113] is the decay factor, which controls the exponential decay rate of the event effect, e.g. ;

[0114] Characterize the exponential decay function and model the timeliness of event impact;

[0115] is a natural constant;

[0116] is the element-wise multiplication operator.

[0117] 3) Align sensor stream features

[0118] The dynamic time window is defined based on the maximum time difference between the current moment and all operation moments. The sensor data within the window is integrated using a bidirectional long short-term memory network, and the sensor stream feature vector is output to capture the temporal dependency, which is expressed as:

[0119]

[0120]

[0121] Where, express The sensor flow feature vector at time t;

[0122] for Raw sensor data at the moment;

[0123] is a dynamic time window;

[0124] is the window length, take the current time With all operating moments The maximum time difference;

[0125] Represents a bidirectional long short-term memory network, capturing the temporal dependencies between the front and back sequences;

[0126] Represents the set of sensor data at all time points within the window;

[0127] Time window The specific time point index in ;

[0128] To index agricultural operations The function that takes the maximum value.

[0129] 4) Generate alignment features

[0130] The event stream features and sensor stream features are spliced ​​along the feature dimension to form an aligned feature matrix covering all time steps, realizing the fusion and time synchronization of multi-source data, which is expressed as:

[0131]

[0132] Where, is the feature matrix after alignment;

[0133] is a feature concatenation operator that concatenates vectors along the feature dimension;

[0134] Indicates from arrive The set of feature vectors at each moment;

[0135] is the total number of time steps.

[0136] S403, Multi-scale anomaly enhancement based on spectral residual

[0137] Agricultural product anomalies usually manifest as minor deviations and are easily masked by normal data fluctuations. Conventional processing methods directly input the raw data into the model, but because the abnormal signals are weak, it is difficult for the model to focus on and identify these abnormal patterns, resulting in reduced detection performance.

[0138] The present invention adopts a spectral residual enhancement module, performs Fourier transform by aligning features, calculates spectral residuals to amplify abnormal components, and enhances the detection capability of small deviations. The specific steps are as follows:

[0139] 1) Perform a Fast Fourier Transform

[0140] In anomaly enhancement, fast Fourier transform is performed to convert the alignment features from the time domain to the frequency domain, and the complex frequency domain features are obtained, which are expressed as:

[0141] Where, Represents frequency domain features, complex form;

[0142] is the time step index, ;

[0143] is the frequency index, ;

[0144] is an imaginary unit, satisfying ;

[0145] Characterize the Fourier transform kernel function.

[0146] 2) Calculate the logarithmic amplitude spectrum

[0147] Take the modulus of the frequency domain feature and apply the logarithmic function to output the logarithmic amplitude spectrum in real form to extract the key information in the frequency domain, which is expressed as:

[0148]

[0149] Where, represents the logarithmic amplitude spectrum, real number form;

[0150] is the frequency domain feature The model;

[0151] For logarithmic functions, the default base is a natural constant.

[0152] 3) Calculate spectral residual

[0153] The spectral residual is obtained by subtracting the Hanning window smoothing component from the logarithmic amplitude spectrum to amplify the abnormal signal, which is expressed as:

[0154]

[0155] Where, Represents the spectral residual and amplifies the abnormal component;

[0156] Represents the Hanning window smoothing operation to achieve low-pass filtering.

[0157] 4) Reconstruct residual features

[0158] Combine the spectral residual and the original phase angle to apply the inverse Fourier transform and take the real part to output the residual feature matrix, which is expressed as:

[0159]

[0160] Where, is the residual feature matrix;

[0161] represents the inverse Fourier transform;

[0162] Characterize complex frequency domain signals and construct kernel functions;

[0163] is an imaginary unit, satisfying ;

[0164] is the original frequency domain signal The phase angle of

[0165] It means taking the real part of a complex number.

[0166] 5) Generate enhanced features

[0167] The residual feature matrix is ​​superimposed on the original alignment feature with a preset weight coefficient, and the enhanced feature matrix is ​​output to enhance the abnormal component, which is expressed as:

[0168]

[0169] Where, To enhance the feature matrix, as output;

[0170] is the residual weight coefficient, which controls the abnormal enhancement strength, such as, .

[0171] S404, Hierarchical graph convolution feature extraction

[0172] Abnormal patterns have spatial propagation in agricultural product traceability, such as the spread of pollution from soil to crops. Conventional processing methods use traditional convolutional neural networks, but they cannot effectively model the relationship transmission of non-Euclidean structures and cannot capture these complex dependencies, which limits the ability to detect abnormal propagation.

[0173] This paper uses a two-layer graph convolutional network. The bottom layer performs graph convolution to model the spatial dependencies between nodes, and the top layer performs cross-batch graph convolution to capture the abnormal propagation pattern between samples to extract hierarchical features. The specific steps are as follows:

[0174] 1) Construct the underlying operation graph adjacency matrix

[0175] In graph convolution feature extraction, the underlying operation graph adjacency matrix is ​​constructed. The original adjacency matrix is ​​defined based on the edge set and the identity matrix is ​​added to include self-loop connections. The adjacency matrix with self-connections is generated to ensure that the node's own information is preserved and the spatial dependency relationship is modeled. It is expressed as:

[0176]

[0177] Where, is an adjacency matrix with self-connection, dimension , is the total number of nodes;

[0178] is the original adjacency matrix;

[0179] It is the identity matrix, and self-loop connections are added to ensure that the node's own information is preserved.

[0180] It should be noted that the original adjacency matrix A is based on Definition, that is, if and only if the edge ∈ When it exists, , indicating the Node and The nodes are connected by edges to model the spatial dependencies between agricultural operation nodes through graph convolution, such as the propagation of anomalies in the operation chain, and capture abnormal patterns of non-Euclidean structures, such as the transmission of pollution from soil to crops.

[0181] 2) Calculate the normalized degree matrix

[0182] The degree matrix is ​​obtained by summing the rows of the self-connected adjacency matrix, and then its negative power of one half is calculated to output the normalized degree matrix for the normalization processing of graph convolution, which is expressed as:

[0183]

[0184]

[0185] Where, Is the degree matrix, is a diagonal matrix, dimension ;

[0186] Indicates the The degree of a node, is the value of the i-th row and j-th column of the adjacency matrix with self-connection; The row summation of , and represents the degree of the first node, Indicates the The degree of each node;

[0187] is the negative half power of the degree matrix, used for normalization;

[0188] Denotes constructing a diagonal matrix.

[0189] 3) Perform the underlying graph convolution operation

[0190] Apply graph convolution operations to the enhanced feature matrix using the normalized adjacency matrix and degree matrix, output the underlying features through the ReLU activation function, and extract the spatial pattern between the operation nodes, which is expressed as:

[0191]

[0192] Where, Represents the underlying graph convolution output features, dimension ;

[0193] is the trainable parameter matrix, dimension ;

[0194] is the ReLU activation function;

[0195] is the hidden layer feature dimension.

[0196] 4) Construct batch similarity graph matrix

[0197] Based on the sample features, the Gaussian kernel similarity is calculated to form a similarity matrix. Then, through matrix transposition and multiplication operations, a batch similarity graph matrix is ​​generated to model the relationship between samples, which is expressed as:

[0198]

[0199]

[0200] Where, is the batch similarity graph matrix, dimension , is the number of samples in the batch;

[0201] is the similarity matrix;

[0202] Indicates the The sample and Gaussian kernel similarity of samples;

[0203] For the The original feature vector of samples;

[0204] For the The original feature vector of samples;

[0205] is the bandwidth parameter, which controls the similarity decay rate, such as, ;

[0206] Represents the L2 norm, which is calculated in the same way as the Euclidean distance;

[0207] represents the transpose of K.

[0208] 5) Perform top-level cross-batch graph convolution

[0209] The batch similarity graph matrix is ​​used to apply graph convolution operations to the bottom-level output features, and the top-level features are output through the ReLU activation function to capture the abnormal propagation pattern across samples, which is expressed as:

[0210]

[0211] Where, Represents the top-level graph convolution output features, dimension ;

[0212] is the trainable parameter matrix, dimension ;

[0213] is the output feature dimension.

[0214] S405, Time Series Causal Dilation Convolution

[0215] In time series data, anomalies may have long-term lag effects, such as early fertilization operations leading to contamination during the harvest period. Conventional processing methods use RNN models, but these models have difficulty capturing ultra-long-range time dependencies, have limited memory capacity, and the gradient vanishing problem makes long-distance association learning difficult.

[0216] This paper uses a causal dilation convolution layer to expand the receptive field through an exponentially growing dilation rate, modeling long-period lag effects while ensuring temporal causality. The specific steps are as follows:

[0217] 1) Set the expansion rate

[0218] According to the depth of the current convolution layer in the model, the expansion rate parameter is set according to the exponential law. The expansion rate parameter controls the number of time steps skipped by the convolution kernel when processing time series data. This allows the model to perceive historical information with a longer time span without increasing the number of parameters. It is expressed as:

[0219]

[0220] Where, Represents the expansion rate, which controls the convolution kernel sampling interval; is the current convolutional layer index, , is the total number of layers; Represents an exponential growth function, which makes the receptive field expand exponentially with the number of layers.

[0221] 2) Perform causal dilated convolution

[0222] Causal dilation convolution is performed on time series features. A convolution kernel of a preset size is used, combined with the dilation rate parameter of the current layer, to perform a weighted summation of the input features at a specific time point in the past, and add the bias term to obtain the convolution output. Moreover, the calculation of the convolution kernel strictly depends on the features of the current and past moments, ensuring that the model prediction does not use future information, meeting the requirements of time series causality, expressed as:

[0223]

[0224] Where, express Moment Layer convolution output;

[0225] For the The convolution kernel layer weight vectors;

[0226] for Moment from The input features of the layer;

[0227] is the convolution kernel size, which determines the number of time step samples;

[0228] For the layer bias vector;

[0229] It should be noted that when When setting Input features when Equal to the top-level graph convolution output feature .

[0230] It should also be noted that It is used to specify the time point of the input feature. The calculation is based on a certain time point in the past. The time point is different from the current time. The offset is , Offset Allow convolution kernel to have stride Skipping the intermediate time points significantly increases the receptive field without increasing the number of parameters, and, since and , Always satisfied , that is, it only relies on the current or past time points, ensuring the causality of the convolution and preventing future information leakage.

[0231] 3) Application gating mechanism

[0232] The gating mechanism is used to enhance the nonlinear expression ability of features. The hyperbolic tangent activation function and the Sigmoid activation function are applied to the convolution output features at the same time, and the results of the two are multiplied element by element. The output of the Sigmoid function plays a gating role, which determines the degree of passing of the features after hyperbolic tangent activation, which is expressed as:

[0233]

[0234] Where, represents the features after gated enhancement;

[0235] For the Sigmoid activation function, the output range is , acts as a gate here;

[0236] is the element-wise multiplication operator.

[0237] 4) Output normalized features

[0238] The output features after the final layer gate enhancement are normalized, and the data is standardized along the feature dimension to generate the final temporal feature matrix used in subsequent modules, which is expressed as:

[0239]

[0240] Where, is the output time series feature matrix;

[0241] For the Layer-gated enhancement features;

[0242] Representation layer normalization operation, normalization along the feature dimension;

[0243] is the total number of convolution layers.

[0244] S406, Multi-granularity attention aggregation

[0245] Detection of different anomaly types, such as chemical and biological contamination, relies on features of varying granularity. Conventional approaches use global pooling to aggregate features, but this can easily lose fine-grained discriminative information, making it impossible for the model to distinguish anomaly types that rely on features at specific scales.

[0246] This paper adopts a dual-path attention mechanism, including time-granularity attention and feature-granularity attention, to adaptively aggregate features of different granularities and focus on key information for anomaly detection. The specific steps are as follows:

[0247] 1) Calculate the time-granular attention weight

[0248] Based on the normalized time series feature matrix, the trainable weight matrix and the query vector are used, combined with the hyperbolic tangent function and Softmax normalization, to generate an attention weight value for each time step. The size of the attention weight value reflects the importance of the corresponding time point feature for the final anomaly detection task. Based on this, the model adaptively focuses on the key time points, which can be expressed as:

[0249]

[0250] Where, express The time attention weight of the moment, the value range is ;

[0251] is an exponential function with a natural constant as its base;

[0252] for The input feature vector at time t;

[0253] for The input feature vector at time t;

[0254] is the trainable weight matrix;

[0255] is the trainable query vector.

[0256] 2) Calculate feature granularity attention weights

[0257] Based on the normalized time series feature matrix, operations are performed on the feature dimension. Combined with the hyperbolic tangent function and Softmax normalization, the attention weight of the feature dimension is generated for each feature dimension. The size of the attention weight of the feature dimension reflects the importance of the corresponding feature dimension to the final anomaly detection task. Based on this, the model adaptively focuses on the key feature dimensions, which can be expressed as:

[0258]

[0259] Where, Indicates the The attention weight of feature dimensions, the value range ;

[0260] For all time steps A vector of feature dimensions;

[0261] For all time steps A vector of feature dimensions;

[0262] is the trainable weight matrix;

[0263] is the trainable query vector.

[0264] 3) Generate dual-path fusion features

[0265] Combining the calculated time attention weight and feature attention weight, the original time series feature matrix is ​​weighted and aggregated. First, in each time step, the feature attention weight is used to weight the sum of each feature dimension. Then, the time attention weight is used to weight the sum of the weighted results of all time steps again. Finally, the feature vector that integrates the key information of time and feature dimensions is output, which is expressed as:

[0266] Where, is the final output feature vector; express Moment dimensional eigenvalues.

[0267] S407. Calculate anomaly detection classification results

[0268] The final output feature vector based on the output of the multi-granularity attention aggregation module , nonlinear transformation and probability mapping are performed through the fully connected neural network layer to generate the final anomaly classification prediction result.

[0269] Specifically, the final output feature vector Input to a two-layer fully connected network:

[0270] The first layer uses the ReLU activation function to perform high-order nonlinear combinations of features to expand the expressive power of the model;

[0271] The second layer uses the Softmax normalization function to map the features to the probability distribution of all possible abnormal categories, including the "normal" category and subcategories such as chemical pollution and biological pollution.

[0272] The output dimension is strictly consistent with the preset number of abnormal categories, ensuring that the probability value of each category is in the range of [0,1] and the sum is 1.

[0273] For example, for a three-class classification task including "normal", "chemical pollution" and "biological pollution", the model output is as follows ,in represents the normal probability, represents the probability of chemical contamination, represents the probability of biological contamination.

[0274] S408. Calculate cost-sensitive focal loss function

[0275] In anomaly detection tasks, abnormal samples usually account for a small proportion, and the cost of misclassification is asymmetric. The cost of missed reports is much higher than that of false positives. Conventional processing methods use the cross-entropy loss function, but it is insensitive to small sample categories and cannot effectively deal with category imbalance and cost sensitivity problems, resulting in the model being biased towards the majority class and ignoring anomaly detection.

[0276] This paper combines class weights and modulation factors to construct a loss function to address class imbalance and cost sensitivity issues to optimize model training. The specific steps are as follows:

[0277] 1) Calculate category weights

[0278] Calculate the weight coefficient for each category. The weight coefficient is calculated based on the number of samples in each category in the training set. Specifically, it is the square root of the ratio of the total number of samples to the number of samples in that category. It is used to give higher weight to the category with fewer samples in the loss calculation to alleviate the problem of class imbalance. It is expressed as:

[0279]

[0280] Where, Indicates the The weight coefficient of each category is used to balance the sample distribution;

[0281] is the category index;

[0282] is the total number of samples in the training set;

[0283] For the The number of samples for each category.

[0284] 2) Calculate the positive sample loss

[0285] For samples whose true label is a certain positive class, the model is used to predict the probability that the sample belongs to the positive class. Combined with the preset difficult sample focus parameters, its loss component is calculated. When the model predicts a low probability, the modulation factor of the positive sample loss term will significantly increase its loss weight, forcing the model to pay more attention to these difficult positive samples, which can be expressed as:

[0286]

[0287] Where, Represents the positive sample loss component;

[0288] is the true label of the sample, when the sample belongs to When the category , otherwise 0;

[0289] The model predicts that the sample belongs to The probability of each category, the range ;

[0290] Focus parameters for difficult samples and increase the penalty for difficult samples with low prediction probability, such as ;

[0291] Characterizes the modulation factor of the positive sample loss term, when When it is smaller, increase the loss weight.

[0292] 3) Calculate the negative sample loss

[0293] For samples whose true labels are negative, the model is used to predict the probability that the sample belongs to a certain positive class. Combined with the preset simple sample suppression parameters, its loss component is calculated. When the model predicts that the probability of the sample being positive is high, the modulation factor of the negative sample loss term will reduce its loss weight, reducing the model's excessive attention to simple negative samples that have been well classified. It is expressed as:

[0294]

[0295] Where, Represents the negative sample loss component;

[0296] Suppress parameters for simple samples and pay less attention to simple samples with high prediction probability, such as ;

[0297] Characterizes the modulation factor of the negative sample loss term, when When it is larger, the loss weight is reduced.

[0298] 4) Calculate the total loss

[0299] The weighted positive and negative sample loss terms are integrated, and the preset positive and negative sample loss weight factors are used to perform weighted summation of the positive and negative sample loss terms. The average loss value is then calculated over the entire training batch as the objective function for model optimization. The total loss function takes into account class imbalance, difficult sample learning, and the difference in the cost of positive and negative sample misclassification, and is expressed as:

[0300]

[0301] Where, is the total loss function; C is the total number of categories;

[0302] is the positive and negative sample loss weight factor of the loss function, such as, .

[0303] S409, iterative training model and judgment of stopping iteration conditions

[0304] In the steps of iteratively training the model and determining the conditions for stopping iteration, the present invention uses a supervised learning strategy to optimize the anomaly detection model parameters to ensure that the model learns effective patterns from the labeled data;

[0305] The training process is based on annotated datasets and is performed through small batch iterations. Each iteration includes forward propagation to calculate the predicted output and backpropagation to update the parameters.

[0306] During the training process, the dataset is divided into training set, validation set and test set. The validation set is used to monitor the generalization ability of the model.

[0307] The stop iteration condition is set to:

[0308] 1) The preset maximum number of iterations is reached, such as 1000, or the validation set loss does not decrease significantly for several consecutive rounds, such as 10 rounds;

[0309] 2) The anomaly detection performance indicators on the validation set reach a plateau, such as the F1 score.

[0310] Once any stopping condition is met, the training is terminated immediately and the final model parameters are frozen and saved.

[0311] S5. Conduct abnormal detection of agricultural product quality traceability

[0312] In the agricultural product quality traceability anomaly detection step, the trained model is used to perform real-time or batch anomaly detection on the newly incoming agricultural product production data to achieve early warning of quality risks and traceability analysis.

[0313] First, the data contains raw multi-source data such as environmental sensors, agricultural operation events, and detection reports. These data are processed using a dynamic graph structure construction method to generate node sets, edge sets, and edge feature matrices to explicitly model the dependencies between operations and the environment.

[0314] Then, the dynamic graph is input into the trained anomaly detection model, and feature alignment, anomaly enhancement, graph convolution feature extraction, causal expansion convolution and attention aggregation modules are performed in sequence to extract high-level features;

[0315] Finally, the classification layer outputs the abnormal probability value at each time point.

[0316] The detection results are judged by thresholds. For example, a probability greater than 0.5 is considered abnormal, and a detailed report is generated, including the type of abnormality, the time of occurrence and possible causes, such as the association between fertilization events and environmental mutations.

[0317] In one embodiment, to evaluate the performance advantages of the agricultural product quality traceability anomaly detection method proposed in this invention in terms of detection accuracy, the effectiveness of this method in complex multi-source data scenarios was verified by comparing it with mainstream anomaly detection technologies. The experimental data used a test dataset containing 10,000 agricultural product production records, covering multi-source heterogeneous data such as environmental parameters, agricultural operation events, and test reports. The comparison methods included long short-term memory networks, isolation forests, autoencoders, and traditional graph convolutional networks. The evaluation metric was the F1 score, which comprehensively reflects the precision and recall rate. The experimental results are analyzed in Table 1 below:

[0318] Table 1

[0319]

[0320] In addition, if Figure 2 As shown, the bar graph intuitively shows that the column height of the method of the present invention is significantly higher than that of other methods, and the industrial application threshold marked with a red dotted line highlights the reliability advantage of the method in actual application scenarios.

[0321] In one embodiment, to verify the timeliness advantage of the method of the present invention in real-time detection scenarios and evaluate the time delay performance from the occurrence of an abnormal event to its detection, the experimental data uses streaming data continuously generated on an agricultural product production line, with approximately 200 records per minute. The average detection delay of each method is measured at 10 consecutive time points. The comparison methods include long short-term memory networks, isolation forests, autoencoders, and traditional graph convolutional networks. The experimental results are shown in Table 2 below:

[0322] Table 2

[0323]

[0324] In addition, if Figure 3 As shown in the broken line graph, the curve of the method of the present invention, that is, the red star-marked curve, is always at the bottom position and has the smallest fluctuation amplitude, which proves that the method has the best real-time detection capability and can meet the needs of agricultural product production lines for immediate warning.

[0325] In one embodiment, to analyze the performance advantages of the method of the present invention in reducing false alarms (false positives) and missed alarms (false negatives), and to evaluate the model's ability to identify rare abnormal samples, the experiment used a balanced test set containing 5,000 abnormal samples, with an abnormality ratio of approximately 15%. The false positive rate and false negative rate of each method under the same threshold were measured. The comparison methods included long short-term memory networks, isolation forests, autoencoders, and traditional graph convolutional networks. The experimental results are shown in Table 3 below:

[0326] Table 3

[0327]

[0328] In addition, if Figure 4 As shown, the grouped bar graph shows that the blue column (false positive rate) and the orange column (false negative rate) of the method of the present invention are both the lowest, proving that the method can reduce the risks of false positives and false negatives at the same time.

[0329] In one embodiment, to evaluate the efficiency advantage of the method of the present invention in actual deployment and balance the relationship between model training cost and real-time inference performance, under the same hardware environment (NVIDIA Tesla V100 GPU), 100,000 training data were used to measure the training time required for each method to achieve optimal performance, as well as the average inference time for processing a single sample. The experimental results are shown in Table 4 below:

[0330] Table 4

[0331]

[0332] In addition, if Figure 5As shown in the double vertical axis graph, the green column (training time) of the method of the present invention is at a medium level, while the red broken line point (inference time) is significantly lower than other methods, proving that the method has the best real-time detection efficiency while maintaining a reasonable training cost.

[0333] In this implementation, based on a dynamic graph structure, agricultural operation events are used as nodes and environmental parameters as edge attributes. This explicitly models the cross-modal dependencies between these two parameters, effectively capturing the spatiotemporal correlations of multi-source data. This approach captures the causal influences between operation events through a collection of temporal causal edges, resolving the problem of abnormal signals being drowned out in traditional methods.

[0334] A dual-path attention mechanism based on time and feature granularity is used to adaptively aggregate features of different granularities and focus on key information, solving the problem of traditional global pooling methods losing fine-grained information. The feature aggregation granularity can be flexibly adjusted according to the detection requirements of different anomaly types.

[0335] The causal expansion convolution layer uses an exponentially increasing expansion rate to expand the receptive field, modeling long-term lag effects while ensuring temporal causality. This solves the problem that traditional RNN models have difficulty capturing very long-range temporal dependencies. The gating mechanism enhances the nonlinear expression of features, further improving the model's ability to handle complex time series data.

[0336] Combining class weights and modulation factors to construct a loss function addresses class imbalance and cost sensitivity, optimizing model training and making the model more attentive to rare, abnormal samples. Modulating the positive and negative sample loss terms balances the costs of false positives and false negatives, improving the model's robustness and reliability.

[0337] The embodiments described above are merely descriptions of preferred implementations of the present invention and are not intended to limit the scope of the present invention. Without departing from the spirit of the present invention, various modifications and improvements made to the technical solutions of the present invention by ordinary technicians in this field should fall within the scope of protection determined by the claims of the present invention.

Claims

1. A method for detecting abnormalities in agricultural product quality traceability, characterized in that: include: S1. Agricultural product quality traceability data collection: specifically, collecting data related to the entire agricultural product production process through a multi-source heterogeneous approach, including data covering environmental parameters, agricultural operation events, and test results; S2. Agricultural product quality traceability data labeling, specifically assigning labels to the collected raw data to construct the training dataset required for supervised learning; S3. Dynamic graph structure construction of multi-source heterogeneous data, specifically including agricultural operation events as nodes and environmental parameters as edge attributes; S4. Anomaly detection model construction, the model includes an input layer, a core processing module, and an output layer. The input layer receives the dynamic graph structure data of the multi-source heterogeneous data, and the output layer maps the aggregated features to the anomaly probability distribution through a fully connected network; S5. Agricultural product quality traceability anomaly detection: using trained models to perform real-time or batch anomaly detection on newly incoming agricultural product production data; The dynamic graph structure construction of multi-source heterogeneous data includes defining a set of agricultural operation nodes based on agricultural operation events occurring at the current moment, defining a set of temporal causal edges by quantifying causal strength through covariance and standard deviation, and encoding environmental parameters including temperature and humidity into an edge feature matrix; The core processing modules include time-aware feature alignment embedding, multi-scale anomaly enhancement based on spectral residual, hierarchical graph convolution feature extraction, time-series causal dilation convolution and multi-granularity attention aggregation.

2. The agricultural product quality traceability anomaly detection method according to claim 1, characterized in that: The process of defining the temporal causal edge set also includes applying a preset threshold for judgment. When the causal score exceeds the threshold, an edge is added between the nodes to construct an edge set to represent the temporal dependency relationship to capture the causal impact between operation events, which is expressed as: Where, express The dynamic graph edge set at each moment is used to model the dependencies between nodes; Represents a connection node and The edge indicates that there is a temporal causal relationship between the two nodes; A collection of dynamic graph nodes Middle nodes; A collection of dynamic graph nodes Middle nodes; Calculate the node association function; is the causal determination threshold.

3. The agricultural product quality traceability anomaly detection method according to claim 1, characterized in that: The proposed time-series-aware feature alignment embedding adopts a two-stream feature alignment module, combining event stream and sensor stream, and aligns the time series features of multi-source data through a learnable delay operator.

4. The agricultural product quality traceability anomaly detection method according to claim 1, characterized in that: The multi-scale anomaly enhancement based on spectral residuals includes performing fast Fourier transform, calculating the logarithmic amplitude spectrum and spectral residuals, applying inverse Fourier transform to reconstruct residual features based on the spectral residuals and the original phase angle, and superimposing the residual feature matrix on the original alignment features with preset weight coefficients to generate enhanced features.

5. The agricultural product quality traceability anomaly detection method according to claim 1, characterized in that: The hierarchical graph convolution feature extraction is based on a two-layer graph convolutional network. The bottom-layer operation graph convolution models the spatial dependency between nodes, and the top-layer cross-batch graph convolution captures the abnormal propagation pattern between samples to extract hierarchical features.

6. The agricultural product quality traceability anomaly detection method according to claim 1, characterized in that: The multi-granularity attention aggregation adopts a dual-path attention mechanism, including time-granularity attention and feature-granularity attention, to adaptively aggregate features of different granularities.

7. The agricultural product quality traceability anomaly detection method according to claim 1, characterized in that: The loss function of the anomaly detection model is a cost-sensitive focal loss function, which is constructed by combining category weights and modulation factors.

8. The agricultural product quality traceability anomaly detection method according to claim 7, characterized in that: The loss function is expressed as: Where, is the total loss function; is the positive sample loss component; is the negative sample loss component; N is the total number of samples in the training set; C is the total number of categories; is the positive and negative sample loss weight factor of the loss function.

Citation Information

Patent Citations

  • Security event tracing method and device, storage medium and electronic equipment

    CN119995910A

  • Network attack tracing method based on interpretable graph neural network and related device

    CN120034384A

  • Agricultural product processing quality traceability monitoring method and system based on edge nodes

    CN119476730A

  • Time series data anomaly detection method based on variable time converter

    CN120144930A