Agricultural product quality traceability anomaly detection method

By constructing a dynamic graph structure and feature aggregation method of multi-source heterogeneous data, explicitly modeling the causal relationship between agricultural operations and environmental parameters, the problem of abnormal signals being flooded in agricultural product quality traceability is solved, efficient processing of complex timing data and optimization of category imbalance, and the robustness and reliability of the model are improved.

CN120410342AActive Publication Date: 2025-08-01HUZHOU WATER SHIELD PERCH ECOLOGICAL AGRI DEV CO LTD

Patent Information

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

AI Technical Summary

Technical Problem

The prior art fails to effectively explicitly model the causal relationship between operational events and environmental parameters in agricultural product quality traceability, resulting in the subtlety and complexity of the abnormal pattern, the invisibility and complexity of the abnormal pattern, the indiscriminate types that rely on specific scale characteristics, and the inability to effectively deal with category imbalance and cost-sensitive problems.

Method used

By constructing a dynamic graph structure of multi-source heterogeneous data, agricultural operation events are used as nodes and environmental parameters are used as edge properties, time-sequence-aware feature alignment embedding, multi-scale anomaly enhancement based on spectral residuals, hierarchical graph convolution feature extraction and multi-grained attention aggregation, combined with causal expansion convolution and cost-sensitive focus loss function, explicitly model the cross-modal dependence between operation events and environmental parameters, capture the spatiotemporal correlation of multi-source data, and adaptively aggregate features of different granularity, optimize model training to improve robustness.

Benefits of technology

Effectively capture the spatiotemporal correlation of multi-source data, solve the problem of flooding of abnormal signals, improve the model's processing ability of complex timing data, can flexibly adjust the feature aggregation granularity, handle category imbalance and cost-sensitive problems, and improve the robustness and reliability of the model.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120410342A_ABST
    Figure CN120410342A_ABST
Patent Text Reader

Abstract

The invention discloses an agricultural product quality traceability anomaly detection method. The method comprises the steps of agricultural product quality traceability data acquisition and labeling, dynamic graph structure construction of multi-source heterogeneous data, anomaly detection model construction and real-time or batch anomaly detection of newly inflowing agricultural product production data by using a trained model. According to the method, the space-time relevance of multi-source data is effectively captured, and the problem that abnormal signals are submerged in a traditional method is solved; features of different granularities are adaptively aggregated, and the problem that fine-grained information is lost in a traditional global pooling method is solved; a causal expansion convolutional layer is adopted, the problem that a traditional RNN model is difficult to capture overlong-range time dependence is solved, the cost of misinformation and missed information is balanced, and the robustness and reliability of the model are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

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

[0002] With the continuous improvement of people's attention to food safety and quality, it has become particularly important to ensure the traceability and monitoring of the whole process of agricultural products from production to consumption. However, the production process of agricultural products involves multiple factors, including environmental parameters, farming operation events, and laboratory test results. These data sources are extensive, diverse in type, and inconsistent in time resolution, forming a typical multi-source heterogeneous data scenario.

[0003] In the prior art, patents such as those with publication numbers CN120034384A and CN119995910A do not consider the causal relationship between operation events and environmental parameters, resulting in the key anomaly signals being submerged, unable to effectively detect anomalies, lacking explicit modeling of the spatio-temporal correlation between operation events and environmental parameters, being difficult to capture the concealment and complexity of anomaly patterns, easily losing fine-grained discriminant information, making the model unable to distinguish anomaly types that depend on specific scale features, being insensitive to small sample categories, and unable to effectively handle the problems of class imbalance and cost sensitivity, resulting in the model being biased towards the majority class and ignoring anomaly detection. Summary of the Invention

[0004] To solve the technical problems of handling class imbalance and cost sensitivity in the prior art, the present invention provides a method for detecting anomalies in the traceability of agricultural product quality.

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

[0006] A method for detecting anomalies in the traceability of agricultural product quality includes:

[0007] S1. Collection of agricultural product quality traceability data, specifically, collecting data related to the whole process of agricultural product production through a multi-source heterogeneous method, including data covering environmental parameters, farming operation events, and test results;

[0008] S2. Labeling of agricultural product quality traceability data, specifically, assigning labels to the collected original data to construct a training data set required for supervised learning;

[0009] S3. Construction of a dynamic graph structure for multi-source heterogeneous data, specifically including using farming operation events as nodes and environmental parameters as edge attributes;

[0010] S4. Construction of an anomaly detection model, 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 an anomaly probability distribution through a fully connected network;

[0011] S5. Anomaly detection for agricultural product quality traceability, using 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 for multi-source heterogeneous data includes defining a set of farming operation nodes based on the farming operation events occurring at the current moment, defining a set of temporal causal edges by quantifying the causal strength using covariance and standard deviation, and encoding environmental parameters such as temperature and humidity as an edge feature matrix.

[0013] Furthermore, the process of defining the set of temporal causal edges also includes making a determination using a preset threshold. When the causal score exceeds the threshold, an edge is added between the nodes, and an edge set is constructed to represent the temporal dependency relationship to capture the causal influence between the operation events, expressed as:

[0014]

[0015] In the formula, represents the set of dynamic graph edges at time used to model the dependency relationship between nodes; represents the edge connecting node and indicating the existence of temporal causality between the two nodes; is the th node in the dynamic graph node set is the th node in the dynamic graph node set ; is the node correlation calculation function; is the causal determination threshold.

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

[0017] Furthermore, the temporal-aware feature alignment embedding adopts a two-stream feature alignment module, combines the event stream and the sensor stream, and aligns the temporal features of multi-source data through a learnable delay operator.

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

[0019] Furthermore, the hierarchical graph convolutional feature extraction is based on a two-layer graph convolutional network. The bottom layer operates on graph convolution to model the spatial dependencies between nodes, and the top layer performs cross-batch graph convolution to capture the abnormal propagation patterns among samples, so as to extract hierarchical features.

[0020] Furthermore, the multi-granularity attention aggregation adopts a dual-channel 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 constructs the loss function by combining class 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 classes; is the positive and negative sample loss weight factor of the loss function.

[0025] Compared with the prior art, the beneficial effects of the present invention are as follows:

[0026] Based on the dynamic graph structure, taking agricultural operation events as nodes and environmental parameters as edge attributes, it explicitly models the cross-modal dependency relationship between operation events and environmental parameters, and effectively captures the spatio-temporal correlation of multi-source data. By capturing the causal influence between operation events through the temporal causal edge set, the problem that abnormal signals are submerged in traditional methods is solved;

[0027] Adopting a dual-channel attention mechanism of time granularity and feature granularity, it adaptively aggregates features of different granularities, focuses on key information, and solves the problem that fine-grained information is lost in traditional global pooling methods. It can flexibly adjust the feature aggregation granularity according to the detection requirements of different anomaly types;

[0028] Adopting a causal dilated convolutional layer, it expands the receptive field through an exponentially increasing dilation rate, models the long-period lag effect, and at the same time ensures temporal causality, solving the problem that it is difficult for traditional RNN-based models to capture ultra-long-range time dependencies. By enhancing the nonlinear expression ability of features through a gating mechanism, the processing ability of the model for complex temporal data is further improved;

[0029] Construct a loss function by combining class weights and modulation factors to address class imbalance and cost-sensitive issues, optimize model training, and make the model pay more attention to rare abnormal samples. By means of the modulation factors of the positive sample loss term and the negative sample loss term, the costs of false positives and false negatives are balanced, and the robustness and reliability of the model are improved. Description of the Drawings

[0030] The drawings described herein are used to provide a further understanding of the present application and form 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 of the present application. In the drawings:

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

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

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

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

[0035] Figure 5 is the comparison result of the training and inference time costs of different anomaly detection methods. Detailed Embodiments

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

[0037] The following specific examples illustrate the implementation manners of the present invention. Those skilled in the art can easily understand other advantages and effects of the present invention from the content disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all of the embodiments. The present invention can also be implemented or applied through other different specific implementation manners, and various details in this specification can also be modified or changed based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that, without conflict, the following embodiments and the features in the embodiments can be combined with each other. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts belong to the scope of protection of the present invention.

[0038] It should also be noted that the diagrams provided in the following embodiments only illustrate the basic concept of the present invention in a schematic manner. The diagrams only show the components related to the present invention, rather than being drawn according to the number, shape, and size of the components in actual implementation. The form, quantity, and ratio of each component in actual implementation can be arbitrarily changed, and the layout form of its components may also be more complex.

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

[0040] S1. Collection of agricultural product quality traceability data

[0041] Specifically, data related to the entire process of agricultural product production is collected through a multi-source heterogeneous method, and the data covers key dimensions such as environmental parameters, farming operation events, and detection results.

[0042] The training data of the present invention comes from an environmental sensor network, a farming operation record system, and a laboratory detection platform, specifically including:

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

[0044] The farming operation record system records events such as fertilization, spraying, irrigation, and harvesting manually or automatically triggered by farmers or managers through mobile terminals or Internet of Things devices. The event data is stored in the form of discrete time points, specifically at a daily level or event-triggered level frequency;

[0045] The laboratory detection platform regularly conducts chemical and biological tests on agricultural product samples to generate test report data, such as pesticide residue levels, heavy metal contents, and microbial indicators. The detection frequency is set according to the production cycle, for example, before and after harvesting.

[0046] The specific content collected includes time stamps, environmental parameter values, operation event types, and detection result indicators. Among them, the time stamp is accurate to the minute or the moment when the event occurs, the environmental parameter values such as temperature, humidity, and light intensity, the operation event types such as fertilization types and spraying doses, and the detection result indicators such as abnormal threshold exceedance flags.

[0047] S2. Labeling of agricultural product quality traceability data

[0048] Based on authoritative test results and expert knowledge, label assignment is performed on the collected raw data to construct a training data set required for supervised learning, ensuring that the subsequent anomaly detection model can accurately identify normal and abnormal patterns.

[0049] The labeling method adopts a multi-stage method combining manual and automatic, and the specific steps are as follows:

[0050] First, use the quantitative indicators in the laboratory test report as the gold standard, such as the excessive value of pesticide residues, the concentration of heavy metal pollution, etc. Domain experts label the data points as "normal" or "abnormal" according to the preset threshold, which can be an artificial experience value or the specified threshold of national standards or industry norms. The abnormal category is further divided into subcategories such as chemical pollution and biological pollution to match the actual production problems;

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

[0052] The labeled dataset is strictly aligned with the collected original features to ensure that the input and output dimensions are consistent. For example, each labeled sample contains a timestamp, environmental sensor readings, agricultural operation codes, and corresponding abnormal labels.

[0053] S3. Construction of the dynamic graph structure for multi-source heterogeneous data

[0054] The agricultural product quality traceability data involves multi-source heterogeneous time-series data, such as environmental sensors, agricultural operations, and test reports. The data has large dimensional differences, strong spatio-temporal correlations, and hidden abnormal patterns. Conventional processing methods directly concatenate multi-source features, but ignore the causal relationship between operation events and environmental parameters, resulting in key abnormal signals being submerged and unable to effectively detect abnormalities.

[0055] The present invention constructs a dynamic graph structure, taking agricultural operation events as nodes and environmental parameters as edge attributes to explicitly model the cross-modal dependence relationship between operation events and environmental parameters, thereby capturing the spatio-temporal correlation of multi-source data and avoiding abnormal signals being submerged. The specific steps are as follows:

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

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

[0058]

[0059] In the formula, represents the set of dynamic graph nodes at time

[0060] represents the th agricultural operation node, representing a specific event;

[0061] is the agricultural operation index, traversing all operations occurring at the moment;

[0062] is the set of agricultural operation events at the moment, containing all operation instances to be modeled;

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

[0064] S302: Define the set of temporal causal edges

[0065] Calculate the temporal causal relevance between nodes, quantify the causal strength through covariance and standard deviation, and make a determination using a preset threshold. When the causal score exceeds the threshold, add an edge between the nodes to construct an edge set to represent the temporal dependence relationship, so as to capture the causal influence between operation events, expressed as:

[0066]

[0067] In the formula, represents the set of dynamic graph edges at the moment, used to model the dependence relationship between nodes;

[0068] represents the edge connecting nodes and characterizing the existence of temporal causality between the two nodes;

[0069] is the set of dynamic graph nodes the th node in;

[0070] is the set of dynamic graph nodes the th node in;

[0071] is the node relevance calculation function, quantifying the causal strength through covariance and standard deviation, and the calculation method is expressed as ;

[0072] is the covariance of nodes and measuring the correlation of their change trends;

[0073] is the standard deviation of node used to normalize the covariance;

[0074] is the standard deviation of the node , which is used for normalizing the covariance;

[0075] is the causal determination threshold. For example, , when , it is determined that the edge exists.

[0076] S303: Define the edge feature matrix

[0077] Encode environmental parameters such as temperature and humidity into the edge feature matrix. 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, which is expressed as:

[0078]

[0079] In the formula, represents the edge feature matrix at time , which is used to encode environmental parameters as edge attributes and store environmental parameters;

[0080] is the edge set size, which determines the number of rows of the matrix;

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

[0082] Define the edge feature matrix at time the th row and th column element of is ;

[0083] represents the real matrix space, with the dimension of the number of edges multiplied by the feature dimension.

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

[0085] S4. Abnormal detection model construction

[0086] S401. Define the abnormal detection model architecture

[0087] The present invention integrates multiple modules to construct an abnormal 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] Model parameters consist of trainable weight matrices and vectors, which are optimized through loss functions.

[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 operation importance weight

[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] represents the th agricultural operation node, characterizing a specific event;

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

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

[0104] is the hyperbolic tangent activation function;

[0105] is the Softmax normalization exponential function to ensure that the weights sum to 1.

[0106] 2) Reconstruct the event stream features

[0107] Fuse the operation importance weights and the exponential decay factor based on the time difference, map the node features through a multi-layer perceptron and apply element-wise multiplication to generate an event stream feature vector, modeling the timeliness of event impact, expressed as:

[0108]

[0109] In the formula, represents the event stream feature vector at time

[0110] is the output of the multi-layer perceptron, represents mapping to the hidden space;

[0111] is the th occurrence time of the agricultural operation;

[0112] represents the current time and the absolute value of the time difference between the operation time ;

[0113] is the decay factor, controlling the exponential decay rate of the event impact, e.g., ;

[0114] characterizes the exponential decay function, modeling the timeliness of event impact;

[0115] is the natural constant;

[0116] is the element-wise multiplication operator.

[0117] 3) Align sensor stream features

[0118] Define a dynamic time window based on the maximum time difference between the current time and all operation times. Use a bidirectional long short-term memory network to integrate the sensor data within the window and output a sensor stream feature vector to capture temporal dependencies, expressed as:

[0119]

[0120]

[0121] In the formula, represents the sensor stream feature vector at time

[0122] is the original sensor data at time

[0123] is the dynamic time window;

[0124] is the window length, taking the maximum time difference between the current time and all operation times ;

[0125] represents a bidirectional long short-term memory network, capturing temporal dependencies before and after;

[0126] represents the set of sensor data for all time points within the window;

[0127] is the specific time point index within the time window ;

[0128] is a function that takes the maximum value of the farming operation index .

[0129] 4) Generate aligned features

[0130] Concatenate the event stream features and sensor stream features along the feature dimension to form an aligned feature matrix covering all time steps, achieving the fusion and time synchronization of multi-source data, expressed as:

[0131]

[0132] In the formula, is the aligned feature matrix;

[0133] is the feature concatenation operator, connecting vectors along the feature dimension;

[0134] Denote the set of eigenvectors from to time instants;

[0135] is the total number of time steps.

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

[0137] Agricultural product anomalies usually appear as minor deviations and are easily masked by normal data fluctuations. Conventional processing methods directly input the original data into the model. However, due to the weak anomaly signals, the model has difficulty focusing on and identifying these anomaly patterns, resulting in a decline in detection performance.

[0138] The present invention adopts a spectral residual enhancement module. By performing Fourier transform on the aligned features, the spectral residual is calculated to amplify the anomaly components and enhance the detection ability of minor deviations. The specific steps are as follows:

[0139] 1) Perform fast Fourier transform

[0140] In anomaly enhancement, performing fast Fourier transform converts the aligned features from the time domain to the frequency domain, obtaining the frequency domain features in complex form, denoted as:

[0141] where represents the frequency domain features, in complex form;

[0142] is the time step index, ;

[0143] is the frequency index, ;

[0144] is the imaginary unit, satisfying ;

[0145] characterizes the Fourier transform kernel function.

[0146] 2) Calculate the logarithmic amplitude spectrum

[0147] Take the modulus of the frequency domain features and apply the logarithmic function to output the logarithmic amplitude spectrum in real form, extracting the key information in the frequency domain, denoted as:

[0148]

[0149] where represents the logarithmic amplitude spectrum, in real form;

[0150] Is the frequency-domain feature The modulus of;

[0151] Is the logarithmic function, and the default base is the natural constant.

[0152] 3) Calculate the spectral residual

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

[0154]

[0155] In the formula, Represents the spectral residual, amplifying the abnormal component;

[0156]

[0157] Represents the Hanning window smoothing operation to achieve low-pass filtering.4) Reconstruct the residual feature

[0158] Combining the spectral residual and the original phase angle, applying the inverse Fourier transform and taking the real part, the residual feature matrix is output, expressed as:

[0159]

[0160] In the formula, Is the residual feature matrix;

[0161] Represents the inverse Fourier transform;

[0162] Characterizes the complex frequency-domain signal to construct the kernel function;

[0163] Is the imaginary unit, satisfying ;

[0164] Is the original frequency-domain signal The phase angle of;

[0165] Represents taking the real part of the complex number.

[0166] 5) Generate enhanced features

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

[0168]

[0169] In the formula, Is the enhanced feature matrix, as the output;

[0170] is the residual weight coefficient, which controls the intensity of anomaly enhancement. For example, .

[0171] S404. Hierarchical graph convolutional feature extraction

[0172] Anomaly patterns have spatial propagation in agricultural product traceability. For example, pollution spreads from the soil to the 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 detection ability of anomaly propagation.

[0173] The present invention uses a two-layer graph convolutional network. The bottom layer operates on graph convolution to model the spatial dependencies between nodes, and the top layer performs cross-batch graph convolution to capture the anomaly propagation patterns between samples in order to extract hierarchical features. The specific steps are as follows:

[0174] 1) Construct the adjacency matrix of the bottom layer operation

[0175] In graph convolutional feature extraction, the adjacency matrix of the bottom layer operation is constructed. Based on the edge set, the original adjacency matrix is defined and the identity matrix is added to include self-loop connections, generating an adjacency matrix with self-connections to ensure that the node's own information is retained and the spatial dependency relationship is modeled, expressed as:

[0176]

[0177] In the formula, is the adjacency matrix with self-connections, and the dimension is , is the total number of nodes;

[0178] is the original adjacency matrix;

[0179] is the identity matrix, and adding self-loop connections ensures that the node's own information is retained.

[0180] It should be noted that the original adjacency matrix A is defined based on , that is, when and only when the edge ∈ exists, , indicating that there is an edge connection between the th node and the th node, so as to model the spatial dependencies between agricultural operation nodes through graph convolution, such as the propagation of anomalies in the operation chain, and capture the anomaly patterns of non-Euclidean structures, such as the transfer of pollution from the soil to the 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 half - power is calculated. The output normalized degree matrix is used for the normalization process of graph convolution, which is expressed as:

[0183]

[0184]

[0185] In the formula, is the degree matrix, which is a diagonal matrix with dimension ;

[0186] represents the degree of the -th node, is the value of the \(i\) - th row and \(j\) - th column of the self - connected adjacency matrix; it is calculated by summing the rows of , and, represents the degree of the first node, represents the degree of the -th node;

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

[0188] represents constructing a diagonal matrix.

[0189] 3) Perform the underlying operation of graph convolution

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

[0191]

[0192] In the formula, represents the output features of the underlying graph convolution, with dimension ;

[0193] is the trainable parameter matrix, with dimension ;

[0194] is the ReLU activation function;

[0195] is the hidden - layer feature dimension.

[0196] 4) Construct the batch similarity graph matrix

[0197] Calculate the Gaussian - kernel similarity based on the sample features to form the similarity matrix, and then generate the batch similarity graph matrix through matrix transposition and multiplication operations to model the relationships between samples, which is expressed as:

[0198]

[0199]

[0200] In the formula, is the batch similarity graph matrix, with dimension , is the number of batch samples;

[0201] is the similarity matrix;

[0202] represents the Gaussian kernel similarity between the th sample and the th sample;

[0203] is the original feature vector of the th sample;

[0204] is the original feature vector of the th sample;

[0205] is the bandwidth parameter, which controls the similarity decay rate. For example, ;

[0206] represents the L2 norm, which has the same calculation method as the Euclidean distance;

[0207] represents the transpose of K.

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

[0209] Apply graph convolution operation to the output features of the bottom layer using the batch similarity graph matrix, and output the top-level features through the ReLU activation function to capture the abnormal propagation pattern across samples, which is expressed as:

[0210]

[0211] In the formula, represents the output features of the top-level graph convolution, with dimension ;

[0212] is the trainable parameter matrix, with dimension ;

[0213] is the output feature dimension.

[0214] S405. Temporal causal dilated convolution

[0215] In time series data, anomalies may have long-period lag effects. For example, early fertilization operations can lead to contamination during the harvesting period. Conventional processing methods use RNN-based models, but these models are difficult to capture ultra-long-range time dependencies, have limited memory capabilities, and the vanishing gradient problem makes it difficult to learn long-distance correlations.

[0216] The present invention uses a causal dilated convolutional layer to expand the receptive field through an exponentially increasing dilation rate, model the long-period lag effect, and ensure temporal causality. The specific steps are as follows:

[0217] 1) Set the dilation rate

[0218] According to the depth of the current convolutional layer in the model, set the dilation rate parameter according to an exponential law. The dilation rate parameter controls the time step interval skipped by the convolutional kernel when processing time series data, enabling the model to perceive historical information over a longer time span without increasing the number of parameters. It is expressed as:

[0219]

[0220] In the formula, represents the dilation rate, which controls the convolutional kernel sampling interval; is the index of the current convolutional layer, , is the total number of layers; represents the exponential growth function, which makes the receptive field expand exponentially with the number of layers.

[0221] 2) Perform causal dilated convolution

[0222] Perform causal dilated convolution calculation on the time series features. Use a convolutional kernel of a preset size, combine it with the dilation rate parameter of the current layer, perform weighted summation on the input features at specific past time points, and add the bias term to obtain the convolutional output. Moreover, the calculation of the convolutional kernel strictly depends on the features at the current moment and past moments, ensuring that the model prediction does not use future information and meets the requirements of temporal causality. It is expressed as:

[0223]

[0224] In the formula, represents the convolutional output of the th layer at the

[0225] th time; is the th weight vector of the convolutional kernel of the

[0226] th layer; is the input feature from the th layer at the

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

[0228] is the layer bias vector;

[0229] It should be noted that when , set the input feature at is equal to the output feature of the top - layer graph convolution .

[0230] It also should be noted that is used to specify the time point of the input feature. It calculates a past time point, and the offset between this point and the current time is , the offset of allows the convolution kernel to skip intermediate time points with a step size of , significantly increasing the receptive field without increasing the number of parameters. Moreover, due to and , always satisfies , that is, it only depends on the current or past time points, ensuring the causality of the convolution and preventing future information leakage.

[0231] 3) Apply the gating mechanism

[0232] The gating mechanism is adopted to enhance the non - linear expression ability of features. The hyperbolic tangent activation function and the Sigmoid activation function are simultaneously applied to the convolution output feature, and the results of the two are multiplied element - by - element. The output of the Sigmoid function acts as a gate, determining the passing degree of the feature after hyperbolic tangent activation, expressed as:

[0233]

[0234] In the formula, represents the feature after gating enhancement;

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

[0236] is the element - by - element multiplication operator.

[0237] 4) Output the normalized feature

[0238] The output feature after gating enhancement in the final layer is subjected to layer normalization processing. The data is standardized along the feature dimension to generate the final temporal feature matrix for subsequent modules, expressed as:

[0239]

[0240] wherein, is the output timing feature matrix;

[0241] is the layer gating enhanced feature;

[0242] represents layer normalization operation, which is normalized along the feature dimension;

[0243] is the total number of convolutional layers.

[0244] S406, Multi-granularity Attention Aggregation

[0245] For different types of anomalies, such as chemical contamination and biological contamination, the detection depends on features of different granularities. Conventional processing methods use global pooling to aggregate features, but it is easy to lose fine-grained discriminative information, making the model unable to distinguish anomaly types that rely on features of a specific scale.

[0246] The present invention adopts a dual-channel 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-granularity attention weight

[0248] Based on the normalized timing feature matrix, using a trainable weight matrix and a query vector, combined with the hyperbolic tangent function and Softmax normalization, generate attention weight values for each time step. The magnitude of the attention weight value reflects the importance of the feature at the corresponding time point for the final anomaly detection task, and the model adaptively focuses on key time points, expressed as:

[0249]

[0250] wherein, represents the time attention weight at the moment, and the value range is

[0251] is the exponential function with the natural constant as the base;

[0252] is the input feature vector at the

[0253] is the input feature vector at the

[0254] is a trainable weight matrix;

[0255] is a trainable query vector.

[0256] 2) Calculate the feature granularity attention weights

[0257] Based on the normalized temporal feature matrix, operations are performed on the feature dimension, combined with the hyperbolic tangent function and Softmax normalization, to generate the attention weights for each feature dimension. The magnitude of the attention weights for this feature dimension reflects the importance of the corresponding feature dimension for the final anomaly detection task. The model adaptively focuses on the key feature dimensions accordingly, expressed as:

[0258]

[0259] In the formula, represents the attention weight of the th feature dimension, with the value range ;

[0260] is the vector of the th feature dimension at all time steps;

[0261] is the vector of the th feature dimension at all time steps;

[0262] is a trainable weight matrix;

[0263] is a trainable query vector.

[0264] 3) Generate the dual-channel fusion feature

[0265] Combining the calculated temporal attention weights and feature attention weights, perform weighted aggregation on the original temporal feature matrix. First, within each time step, use the feature attention weights to perform weighted summation on each feature dimension. Then, use the temporal attention weights to perform weighted summation again on the weighted results of all time steps. Finally, output the feature vector that fuses the key information of time and feature dimensions, expressed as:

[0266] In the formula, is the final output feature vector; represents the feature value of the th dimension at time

[0267] S407. Calculate the anomaly detection classification result

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

[0269] Specifically, the final output feature vector is input into a two-layer fully connected network:

[0270] The first layer uses the ReLU activation function to perform high-order non-linear combination on the features, expanding the expression ability of the model;

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

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

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

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

[0275] In the anomaly detection task, the anomaly samples usually account for a small proportion, and the misclassification cost is asymmetric. The cost of false negatives is much higher than that of false positives. The conventional processing method uses the cross-entropy loss function, but it is not sensitive to small-sample categories and cannot effectively handle the problems of class imbalance and cost sensitivity, resulting in the model being biased towards the majority class and ignoring anomaly detection.

[0276] The present invention constructs a loss function by combining class weights and modulation factors to handle class imbalance and cost sensitivity problems to optimize model training. The specific steps are as follows:

[0277] 1) Calculate class weights

[0278] Calculate the weight coefficient of each class, which is calculated based on the number of samples of each class in the training set. Specifically, it is the square root of the ratio of the total number of samples to the number of samples of this class, and is used to assign a higher weight to the rare sample class in the loss calculation to alleviate the class imbalance problem, expressed as:

[0279]

[0280] In the formula, represents 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] is the number of samples in the

[0284] 2) Calculate the positive sample loss term

[0285] For a sample with a true label of a certain positive class, use the model to predict the probability that the sample belongs to this positive class, and combine the preset hard sample focusing parameter to calculate its loss component. When the model prediction probability is low, the modulation factor of the positive sample loss term will significantly increase its loss weight, forcing the model to pay more attention to these hard positive samples, expressed as:

[0286]

[0287] In the formula, represents the positive sample loss component;

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

[0289] is the probability that the model predicts the sample belongs to the th category, and the value range is ;

[0290] is the hard sample focusing parameter, increasing the penalty for hard samples with low prediction probability, such as, ;

[0291] represents the modulation factor of the positive sample loss term, increasing the loss weight when is small.

[0292] 3) Calculate the negative sample loss term

[0293] For a sample with a true label of a negative class, use the model to predict the probability that the sample belongs to a certain positive class, and combine the preset easy sample suppression parameter to calculate its loss component. When the model predicts that the sample is a positive class with a high probability, the modulation factor of the negative sample loss term will reduce its loss weight, reducing the model's excessive attention to easy negative samples that can be well classified, expressed as:

[0294]

[0295] In the formula, represents the negative sample loss component;

[0296] is the simple sample suppression parameter, which reduces the attention to simple samples with high prediction probabilities. For example, ;

[0297] is the modulation factor characterizing the negative sample loss term, which reduces the loss weight when is relatively large.

[0298] 4) Calculate the total loss

[0299] Integrate the weighted positive sample loss term and negative sample loss term, use the preset positive and negative sample loss weight factors to perform weighted summation on the positive and negative sample loss terms, and then calculate the average loss value over the entire training batch as the objective function for model optimization. The total loss function takes into account class imbalance, hard sample learning, and the difference in positive and negative sample misclassification costs, and is expressed as:

[0300]

[0301] In the formula, is the total loss function; C is the total number of classes;

[0302] are the positive and negative sample loss weight factors of the loss function. For example, .

[0303] S409. Iteratively train the model and judge the stopping iteration condition

[0304] In the step of iteratively training the model and judging the stopping iteration condition, the present invention adopts a supervised learning strategy to optimize the parameters of the anomaly detection model to ensure that the model learns effective patterns from the labeled data;

[0305] The training process is based on the labeled data set and is carried out by mini-batch iteration. Each iteration includes forward propagation to calculate the prediction output and backward propagation to update the parameters;

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

[0307] The stopping iteration condition is set as:

[0308] 1) Reach the preset maximum number of iteration rounds, such as 1000 rounds, or the loss on the validation set has not decreased significantly for several consecutive rounds, such as 10 rounds;

[0309] 2) The anomaly detection performance index on the validation set reaches a plateau, and the performance index is such as the F1 score.

[0310] Once any of the stopping conditions is satisfied, the training terminates immediately, and the final model parameters are frozen and saved.

[0311] S5. Perform anomaly detection for agricultural product quality traceability

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

[0313] First, the data includes original multi-source data such as environmental sensors, farming operation events, and detection reports. These data are processed using a dynamic graph structure construction method to generate a node set, an edge set, and an edge feature matrix to explicitly model the operation and environment dependence.

[0314] Then, the dynamic graph is input into the trained anomaly detection model, and the feature alignment, anomaly enhancement, graph convolutional feature extraction, causal dilated convolution, and attention aggregation modules are sequentially executed to extract high-level features.

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

[0316] The detection result is determined by a threshold. For example, a probability greater than 0.5 is regarded as an anomaly, and a detailed report is generated, including the anomaly type, occurrence time, and possible reasons. For example, associating fertilization events with environmental mutations.

[0317] In one embodiment, to evaluate the performance advantage of the proposed agricultural product quality traceability anomaly detection method of the present invention in terms of detection accuracy, the effectiveness of this method in a complex multi-source data scenario is verified by comparing with mainstream anomaly detection techniques. The experimental data uses a test data set containing 10,000 agricultural product production records, covering multi-source heterogeneous data such as environmental parameters, farming operation events, and detection reports. The comparison methods include long short-term memory network, isolation forest, autoencoder, and traditional graph convolutional network. The evaluation index is the F1 score that comprehensively reflects precision and recall. The experimental results are analyzed as shown in Table 1 below: Table 1

[0318] In addition, as Figure 2 shown, it is visually shown by the bar chart that the column height of the method of the present invention significantly exceeds other methods, and the industrial application threshold marked by the red dotted line highlights the reliability advantage of this method in the actual application scenario.

[0319] 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 adopted the streaming data continuously generated on the agricultural product production line, with about 200 records per minute. The average detection delay of each method was measured at 10 consecutive time points. The comparison methods included long short-term memory network, isolation forest, autoencoder, and traditional graph convolutional network. The experimental results are analyzed as shown in Table 2 below: Table 2

[0320] In addition, as Figure 3 shown by the curve of the method of the present invention in the line chart, that is, the curve of the red star marker is always in the lowest position and has the smallest fluctuation amplitude, which proves that this method has the optimal real-time detection ability and can meet the demand for instant warning of the agricultural product production line.

[0321] In one embodiment, to analyze the performance advantage of the method of the present invention in reducing false alarms (false positives) and missed alarms (false negatives) and evaluate the recognition ability of the model for rare abnormal samples, an imbalanced test set containing 5,000 abnormal samples was used, with the abnormal proportion being about 15%. The false positive rate and false negative rate of each method were measured at the same threshold. The comparison methods included long short-term memory network, isolation forest, autoencoder, and traditional graph convolutional network. The experimental results are analyzed as shown in Table 3 below: Table 3

[0322] In addition, as Figure 4 shown, through the grouped bar chart, the blue bars (false positive rate) and orange bars (false negative rate) of the method of the present invention are both the lowest, which proves that this method can reduce the risks of false alarms and missed alarms simultaneously.

[0323] 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, in the same hardware environment (NVIDIA Tesla V100 GPU), 100,000 pieces of training data were used to measure the training time required for each method to achieve the optimal performance and the average inference time for processing a single sample. The experimental results are analyzed as shown in Table 4 below: Table 4

[0324] In addition, as Figure 5 shown, in the double vertical axis chart, the green bars (training time) of the method of the present invention are at a medium level, while the red line points (inference time) are significantly lower than those of other methods, which proves that this method has the optimal real-time detection efficiency while maintaining a reasonable training cost.

[0325] In this embodiment, based on the dynamic graph structure, farming operation events are used as nodes and environmental parameters are used as edge attributes to explicitly model the cross-modal dependence relationship between operation events and environmental parameters, effectively capturing the spatio-temporal correlation of multi-source data. The causal influence between operation events is captured through the temporal causal edge set, solving the problem that abnormal signals are submerged in traditional methods;

[0326] A dual-path attention mechanism of time granularity and feature granularity is adopted to adaptively aggregate features of different granularities, focus on key information, and solve the problem that fine-grained information is lost in traditional global pooling methods. It can flexibly adjust the feature aggregation granularity according to the detection requirements of different abnormal types;

[0327] Causal dilated convolutional layers are adopted to expand the receptive field through an exponentially increasing dilation rate, model the long-period lag effect, and ensure temporal causality at the same time, solving the problem that it is difficult for traditional RNN-based models to capture ultra-long-range time dependencies. The nonlinear expression ability of features is enhanced through the gating mechanism, further improving the model's processing ability for complex temporal data;

[0328] A loss function is constructed by combining class weights and modulation factors to handle class imbalance and cost-sensitive problems, optimize model training, and make the model pay more attention to rare abnormal samples. By the modulation factors of the positive sample loss term and the negative sample loss term, the costs of false alarms and missed detections are balanced, improving the robustness and reliability of the model.

[0329] The embodiments described above are only descriptions of the preferred embodiments of the present invention, and do not limit the scope of the present invention. Without departing from the design spirit of the present invention, various deformations and improvements made by those of ordinary skill in the art to the technical solutions of the present invention shall fall within the protection scope determined by the claims of the present invention.

Claims

1. An abnormal detection method for tracing the quality of agricultural products, characterized in that, Including: S1. Collection of agricultural product quality traceability data. Specifically, collect data related to the whole process of agricultural product production through a multi-source heterogeneous method, including environmental parameters, farming operation events, and detection results covered by the data; S2. Annotation of agricultural product quality traceability data. Specifically, assign labels to the collected original data to construct a training data set required for supervised learning; S3. Construction of a dynamic graph structure for multi-source heterogeneous data. Specifically, include farming operation events as nodes and environmental parameters as edge attributes; S4. Construction of an anomaly detection model. 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 an anomaly probability distribution through a fully connected network; S5. Anomaly detection for agricultural product quality traceability. Use the trained model to perform real-time or batch anomaly detection on newly incoming agricultural product production data.

2. The agricultural product quality traceability anomaly detection method according to claim 1, wherein, The construction of the dynamic graph structure of the multi-source heterogeneous data includes defining a set of farming operation nodes based on the farming operation events occurring at the current moment, defining a set of temporal causal edges by quantifying the causal intensity through covariance and standard deviation, and encoding environmental parameters such as temperature and humidity as an edge feature matrix.

3. The agricultural product quality traceability anomaly detection method according to claim 2, wherein, The process of defining the set of temporal causal edges also includes making a determination using a preset threshold. When the causal score exceeds the threshold, add an edge between the nodes to construct an edge set to represent the temporal dependence relationship, so as to capture the causal influence between operation events, expressed as: ; In the formula, represents the set of dynamic graph edges at time for modeling the dependencies between nodes; represents the edge connecting node and which indicates the existence of temporal causality between the two nodes; 4. The agricultural product quality traceability anomaly detection method according to claim 1, characterized in that The core processing module includes temporal-aware feature alignment embedding, multi-scale anomaly enhancement based on spectral residual, hierarchical graph convolution feature extraction, temporal causal dilated convolution, and multi-granularity attention aggregation.

5. The agricultural product quality traceability anomaly detection method according to claim 4, wherein The temporal-aware feature alignment embedding adopts a two-stream feature alignment module, combines the event stream and the sensor stream, and aligns the temporal features of multi-source data through a learnable delay operator.

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

7. The method for detecting anomalies in the traceability of agricultural product quality according to claim 4, wherein The hierarchical graph convolution feature extraction is based on a two-layer graph convolution network. The bottom layer operates on graph convolution to model the spatial dependence between nodes, and the top layer performs cross-batch graph convolution to capture the anomaly propagation pattern between samples to extract hierarchical features.

8. The agricultural product quality traceability anomaly detection method according to claim 4, wherein The multi-granularity attention aggregation adopts a two-way attention mechanism, including time-granularity attention and feature-granularity attention, to adaptively aggregate features of different granularities.

9. The method for detecting anomalies in the traceability of agricultural product quality according to claim 1, wherein, The loss function of the anomaly detection model is a cost-sensitive focal loss function, which constructs a loss function by combining class weights and modulation factors.

10. The agricultural product quality traceability anomaly detection method according to claim 9, characterized in that, The loss function is expressed as: ; In the formula, 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

  • Intelligent analysis method and system for forest and grass soil nutrients based on multi-source data fusion

    CN118898408A

  • Multi-source heterogeneous instrument data fusion and anomaly detection method and system

    CN119089355A

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

    CN119476730A

  • Pumped storage power station construction anomaly detection method and system based on unmanned aerial vehicle image analysis

    CN119888507A

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

    CN120144930A

Cited By

  • Internet of Things terminal anomaly clustering method based on graph neural network

    CN121278413A