Food safety intelligent detection method based on improved YOLOv8 model and DeepSeek reasoning
By optimizing the Backbone layer and embedded attention mechanism of the YOLOv8 model, combined with the DeepSeek reasoning engine, the accuracy and real-time problems in complex scenarios in food safety detection are solved, and intelligent real-time supervision and decision-making support for food safety hazards are achieved.
Patent Information
- Application Number
- CN202510689778.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-27
- Publication Date
- 2025-08-08
AI Technical Summary
The existing food safety detection technology has problems such as environmental interference, scenario specificity, real-time bottlenecks and insufficient reasoning interpretability in complex scenarios, resulting in low detection accuracy, poor real-time performance and insufficient decision-making support, making it difficult to meet the real-time intelligent supervision needs of food safety supervision.
By optimizing the Backbone layer of the YOLOv8 model, embedding the GAM and SA attention mechanisms, combining the DeepSeek reasoning engine for semantic analysis, generating structured data and providing hidden danger warnings, real-time intelligent supervision of food safety hazards is achieved.
It improves detection accuracy, meets real-time detection needs, has intelligent hidden danger warning capabilities, can accurately identify food safety hazards and provide handling suggestions in complex environments, and is adapted to edge computing equipment.
Smart Images

Figure CN120451967A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence and computer vision technology, and specifically to an intelligent food safety detection method based on an improved YOLOv8 model and DeepSeek reasoning. Background Art
[0002] As a key technology for safeguarding public health and urban civilization, intelligent food safety detection aims to identify core hidden dangers such as mixing raw and cooked foods, mixing special foods with ordinary foods, and improper food storage through automated means. With the promotion of the "Internet + Food Safety" model, computer vision-based target detection technology has become a core path to improve regulatory efficiency. This paper proposes a food safety intelligent detection method based on an improved YOLOv8 model and DeepSeek reasoning. By integrating deep neural network optimization with large model reasoning, a multi-scenario robust detection system is constructed to provide technical support for real-time supervision in complex environments such as catering venues and supermarkets.
[0003] In the context of civilized city construction and food safety supervision, food safety supervision is the core link in protecting public health. Traditional manual inspection methods have defects such as low efficiency, strong subjectivity, and limited coverage. In recent years, deep learning-based visual detection technology (such as the YOLO series of algorithms) has been gradually applied to the field of food safety, realizing automated hazard identification through real-time image analysis. However, detection in complex scenarios still faces the following challenges: (1) Environmental interference: dense food placement, uneven lighting, frequent occlusions, and other problems make it difficult to extract target features, and existing models are easily affected by background noise. (2) Scenario specificity: Illegal behaviors such as mixing raw and cooked food and selling special foods are dynamic and diverse, and general detection models are difficult to adapt to fine-grained classification requirements. (3) Real-time bottleneck: The existing model has a large number of parameters, and it is difficult to balance detection speed and accuracy when deployed on edge devices, which restricts real-time warning capabilities. (4) Insufficient reasoning interpretability: The existing method only outputs the target category and location, and cannot be combined with food safety regulations for hazard analysis. If it is detected that food is stored improperly, it cannot be automatically associated with the specific clauses in the "National Food Safety Standard", and manual secondary judgment is required.
[0004] Current technologies for food safety target detection can be divided into two-stage target detection algorithms and single-stage target detection algorithms. The bottlenecks of single-stage target detection algorithms are (1) YOLO series models (such as YOLOv5): Although they have an advantage in detection speed (FPS>80), they have a high missed detection rate (recall rate <65%) for small targets (such as bulk special foods), and the feature extraction mechanism is not optimized for food scenarios. (2) Application of attention mechanism (such as SE, CBAM): Existing improvement schemes mostly use a single attention module, which does not achieve the coordinated enhancement of channel and spatial dimensions, and the mAP improvement is limited (<3%) in occlusion scenarios. When there is motion occlusion in the image sequence, the optical flow estimation will have a large error in the occluded area.
[0005] Bottlenecks of two-stage object detection algorithms (1) Low computational efficiency: Two-stage algorithms, such as Faster R-CNN, first generate candidate regions through RPN (Region Proposal Network), and then perform feature extraction and classification on each candidate region, resulting in computational redundancy. Experiments show that in food monitoring video streams (1080P@30fps), the inference speed of Faster R-CNN is only 18-22FPS, which cannot meet the requirements of real-time detection. The candidate region pooling (RoIPooling) requires the storage of a large number of intermediate feature maps, which occupies more than 4GB of video memory when deployed on edge devices (such as Jetson Nano), leading to the risk of system crashes. (2) Insufficient small target detection performance: (Candidate region loss) The default anchor box size of RPN is designed for general targets, which makes it difficult to cover small-sized targets (such as bulk spices and unpackaged foods) in food scenarios, resulting in a small target recall rate of less than 40% in mixed raw and cooked food scenarios; (Feature resolution loss) After multiple downsampling of high-level feature maps, the semantic information is enhanced but the spatial details are lost, and the sensitivity to local food features (such as raw meat texture and cooked food color differences) is reduced.
[0006] Existing solutions rely on synthetic data or general datasets (such as COCO) and lack customized annotations for food violation scenarios, resulting in insufficient model generalization. Furthermore, mainstream systems only output detection boxes and fail to incorporate semantic analysis models to perform causal inference on violations, making it impossible to generate actionable warning recommendations.
[0007] In addition, current technologies have not yet effectively solved the challenges of intelligent supervision in complex food scenarios, specifically:
[0008] (1) Inaccurate feature extraction: Traditional models have poor robustness to food texture, deformation, and occlusion, resulting in mAP@0.5 of less than 75% in the mixed raw and cooked food scene; (2) The contradiction between lightweight and accuracy: Although existing lightweight models (such as YOLOv8n) have fast inference speed (FPS>90), their detection accuracy is 5%-8% lower than that of the baseline model; (3) Insufficient decision support: The detection results lack quantification of the degree of violation and rectification suggestions, making it difficult to assist regulators in responding quickly.
[0009] Therefore, a new solution to the above problems needs to be proposed. Summary of the Invention
[0010] The purpose of the present invention is to provide an intelligent food safety detection method based on an improved YOLOv8 model and DeepSeek reasoning. By optimizing the YOLOv8 network structure, combining the attention mechanism with the data enhancement strategy to improve the detection accuracy, and introducing the DeepSeek reasoning engine to perform semantic analysis and hidden danger warning on the detection results, the method can realize real-time intelligent supervision of food safety hazards in scenarios such as markets and restaurant kitchens, so as to solve the technical problems raised in the background technology.
[0011] To achieve the above objectives, the present invention provides the following technical solution: a food safety intelligent detection method based on an improved YOLOv8 model and DeepSeek reasoning, comprising at least the following steps:
[0012] S1: Based on the YOLOv8 model as the model framework;
[0013] S2: Optimize the structure of the Backbone layer of YOLOv8n, embed GAM and SA attention mechanisms in the 2nd and 10th layers of the Backbone layer of YOLOv8n respectively, enhance YOLOv8n's ability to extract key regional features, strengthen the ability to accurately detect food safety hazard scenarios, obtain an optimized YOLOv8n model, and use the optimized YOLOv8n model to identify food safety hazards, thereby improving the current problem of non-real-time and inaccurate identification of food safety hazards; the input of the optimized YOLOv8n model includes at least images, videos and cameras for food, and the output of the optimized YOLOv8n model includes at least structured data of category, coordinates, confidence and scene label (such as "refrigerator storage" and "mixed shelf sales"), and the structured data is in JSON format;
[0014] S3: The output of the optimized YOLOv8n model is passed to the DeepSeek inference engine. The DeepSeek inference engine performs semantic analysis and compliance judgment on the detection results of the optimized YOLOv8n model. In other words, the DeepSeek inference engine performs contextual reasoning on the detection results and outputs the risk type and treatment suggestions.
[0015] S4: Based on the DeepSeek reasoning engine, a hidden danger warning mechanism is proposed. Combined with the generated rule base, if a violation is detected in the results of the DeepSeek reasoning engine, a screen alarm is triggered and processing suggestions are generated (such as "it is recommended to separate raw and cooked foods into designated areas"). The violations include at least mixing raw and cooked foods, improper storage, and mixing special and ordinary foods.
[0016] Furthermore, the Backbone layer includes five CBS modules, four C2f modules, one SPPF module, and embedded GAM and SA attention mechanisms;
[0017] The Backbone layer extracts features through convolution and pooling operations, and sets multiple jump connections (also called residual connections) to fuse features and reduce computational complexity.
[0018] Furthermore, the CBS module is an important feature extraction module, and the module structure of CBS is as follows: Figure 4 As shown in Figure 2, the CBS module consists of a convolutional layer, a batch normalization layer (BatchNorm, BN), and an activation function (SiLU);
[0019] The convolutional layer, namely the Conv2d layer, uses a convolution kernel with a step size of 2 to achieve downsampling, reducing the spatial dimension of the feature map and increasing the channel capacity, thereby achieving hierarchical feature expression;
[0020] The BN layer following the convolutional layer is used to standardize the batch data, solving the problem of internal covariate drift and, to a certain extent, weakening the disturbance caused by the input distribution to the network.
[0021] At the same time, due to the introduction of the SiLU activation function with nonlinear mapping, the entire network has greatly improved its performance in learning more complex features. The multi-layer stacked modular backbone network extracts semantic information from the image layer by layer, obtains discriminative multi-scale feature representation, accelerates the network convergence speed and obtains better generalization performance. It is easier to achieve better results by using a modular deep neural network structure.
[0022] Furthermore, the C2f module is lighter than the C3 module of YOLOv5. The C2f module is used instead of the C3 module. This replacement change combines the ELAN concept in YOLOv7, enabling the YOLOv8 model to access richer gradient flows during the training phase.
[0023] The processing of the C2f module includes at least the following steps:
[0024] After the input image is convolved by the CBS module, a Split operation is performed to divide the feature map with C channels into two feature maps with C / 2 channels to reduce the amount of calculation and the scale of parameters.
[0025] One of the feature maps will pass through X Bottleneck modules, and the feature map will be further convolved during the process of passing through X Bottleneck modules;
[0026] Then another feature map is residually connected to the feature map output by X Bottleneck modules, enabling the network to learn deeper feature representations;
[0027] Finally, a CBS module is used to restore the normal number of channels of the feature map to facilitate the next step;
[0028] While maintaining high performance, C2f achieves a simultaneous reduction in computational complexity and storage requirements, demonstrating dual optimization of performance and efficiency.
[0029] Furthermore, the SPPF module (Spatial Pyramid Pooling-Fast) is an improved version of the SPP structure, which aims to reduce the amount of computation and increase the speed of the YOLOv8 model;
[0030] The SPP module generates multi-scale feature maps by pooling the input feature maps with different kernel sizes (such as 1×1, 5×5, 9×9, etc.). By integrating features of different scales, the SPP module can capture more contextual information, enabling the YOLOv8 model to perform better when processing objects of different sizes. However, the SPPF module processes the input feature maps through multiple Maxpool layers in series. This structural adjustment reduces computational complexity and memory consumption, speeding up the YOLOv8 model while maintaining the SPP module's ability to process feature maps of different scales.
[0031] Furthermore, the GAM (Global Attention Mechanism) aims to reduce information loss, enhance global interaction features, and improve the performance of deep neural networks;
[0032] The GAM adopts a sequential channel-spatial attention mechanism to reconfigure the CBAM submodules;
[0033] The channel attention submodule in GAM focuses on extracting "important features". It compresses the features of each channel into a global value representing the entire feature map after global pooling of the input feature map. Then, a two-layer multi-layer perceptron (MLP) processes the summarized global information and assigns corresponding weights to each channel. Finally, a sigmoid activation function is applied to generate channel attention.
[0034] The spatial attention submodule in CAM focuses on the "important positions" of "important features". It first compresses the number of channels in the feature map, that is, uses average pooling or maximum pooling operations to compress the number of channels to 1. The feature map of the obtained spatial information is then operated on by the convolution layer. This process generates a spatial attention weight map, in which each point on the map represents the importance of the corresponding position; then, the spatial attention weight map is multiplied by the original feature map elements in the spatial dimension to achieve weighted spatial positions; finally, it is processed and output through the Sigmoid activation function.
[0035] Furthermore, the SA attention mechanism (ShuffleAttention) is a new type of visual attention mechanism. The SA attention mechanism combines spatial attention and channel attention through permutation units. Compared with the previous hybrid attention, it is more efficient and is a lightweight and efficient attention structure.
[0036] The structure of the SA attention mechanism surpasses the current SOTA attention models such as SE and SGE on benchmarks such as ImageNet and COCO, while having lower computational complexity and parameter count.
[0037] The SA attention mechanism includes feature grouping (Group), mixed attention and feature fusion;
[0038] Initially, the input feature map is processed by feature grouping and divided into multiple groups. Each group represents a sub-feature of the input feature map. The input feature map is divided into g groups along the channel dimension, represented as X = [X1, X2, X3, ..., X g ],X g ∈R C / g×H×W , where each sub-feature gradually captures a specific semantic information as it is trained;
[0039] Then, X g It is divided into two branches, still divided along the channel dimension, and the two sub-features are represented as X g1 , X g2 ∈R C / 2g×H×W , is divided into two branches by the Split operation, represented by green and blue boxes respectively;
[0040] The green branch implements channel attention to reveal the dependencies between channels, while the blue branch captures the spatial dependencies between features and generates a spatial attention map.
[0041] In this way, we can focus on both semantic and position information at the same time, and the results of the two attentions are concat- ed together, which is now consistent with the input size of the group.
[0042] Finally, the channel shuffle operation is used to ensure the interaction between each group of sub-features, and all SA units are randomly mixed to obtain the final output feature map.
[0043] Furthermore, the steps S3 and S4 include at least the following steps:
[0044] After receiving the data, the DeepSeek inference engine uses multimodal input fusion technology to normalize the detection frame coordinates into relative position codes, convert category labels into high-dimensional semantic vectors, and inject scenario-related regulatory knowledge and historical case data.
[0045] Based on the hybrid expert architecture (MoE), the gated network dynamically activates dedicated experts (such as "raw and cooked mixed risk experts" and "cold chain storage regulations experts") according to the input, and efficiently processes long-sequence detection data through the low-rank compressed multi-head latent attention (MLA) mechanism to achieve cross-modal association between detection frame coordinates and regulatory clauses and case features. When specific data is detected exceeding the threshold, the corresponding risk modeling is triggered and matched with international clauses, triggering a screen alarm and generating processing suggestions; for example, when the overlap rate of raw and cooked food detection frames is detected to exceed the threshold, the microbial risk modeling is synchronously triggered to match the national standard clauses to generate structured results including risk level, violation basis and rectification suggestions.
[0046] During the reasoning process, the Group Relative Policy Optimization (GRPO) algorithm dynamically calibrates multiple candidate rectification plans, prioritizing the output of highly compliant and actionable suggestions (such as recommending separation containers that comply with the GB 9684 standard);
[0047] The final results are transmitted back through a standardized interface, and risk areas are highlighted and rectification suggestions are popped up on the user side. The detection logs are also stored synchronously in the regulatory database to support risk tracing and statistical analysis.
[0048] Compared with the prior art, the present invention has the following beneficial effects:
[0049] 1. This invention significantly improves detection accuracy through design: by introducing GAM (global attention mechanism) and SA (shuffle attention mechanism), the Backbone network of the YOLOv8 model is improved, and the mAP for scenarios such as mixed raw and cooked food and illegal sales of special foods is increased by about 5%, solving the problems of low efficiency and insufficient accuracy of traditional detection methods.
[0050] 2. The present invention optimizes real-time reasoning capabilities and model computing efficiency through design, adapts to edge computing devices, and meets the real-time detection needs of actual scenarios. The lightweight model design (YOLOv8n-GAM-SA) maintains high accuracy while achieving an FPS of 85-96, meeting real-time detection requirements.
[0051] 3. The design of the present invention has intelligent hidden danger warning: combined with the DeepSeek reasoning engine, it performs semantic analysis and compliance judgment on the test results, that is, performs contextual reasoning on the test results, outputs the hidden danger type and treatment suggestions, that is, generates specific clauses and warning suggestions in accordance with the "National Food Safety Standard".
[0052] 4. The method proposed in this invention has strong cross-scene adaptability: through data enhancement strategies such as random masking and brightness adjustment, the model's robustness to complex lighting and occlusion scenes is enhanced. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for describing the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0054] Figure 1 This is a diagram of the detection reasoning closed loop structure of the present invention;
[0055] Figure 2 These are the implementation effect diagrams of the present invention in three scenarios;
[0056] Figure 3 This is a schematic diagram of the Backbone architecture of the present invention;
[0057] Figure 4 The diagram is a schematic diagram of the CBS structure in the backbone network;
[0058] Figure 5 The structure is a schematic diagram of the C2f structure in the backbone network;
[0059] Figure 6 The diagram is a schematic diagram of the SPPF structure in the backbone network;
[0060] Figure 7Schematic diagram of the GAM attention mechanism provided by the present invention;
[0061] Figure 8 Schematic diagram of the SA attention mechanism provided by the present invention;
[0062] Figure 9 A schematic diagram of the MoE hybrid expert structure provided by the present invention;
[0063] Figure 10 Schematic diagram of the MLP multi-head potential attention structure provided by the present invention. DETAILED DESCRIPTION
[0064] The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments.
[0065] See also Figures 1-10 , an intelligent food safety detection method based on an improved YOLOv8 model and DeepSeek reasoning, comprising at least the following steps:
[0066] S1: Based on the YOLOv8 model as the model framework;
[0067] S2: Optimize the structure of YOLOv8n's Backbone layer, embed GAM and SA attention mechanisms in the 2nd and 10th layers of YOLOv8n's Backbone layer respectively, enhance YOLOv8n's ability to extract key regional features, strengthen its ability to accurately detect food safety hazard scenarios, obtain an optimized YOLOv8n model, and use the optimized YOLOv8n model to identify food safety hazards, improving the current non-real-time and inaccurate problems of food safety hazard identification; the input of the optimized YOLOv8n model includes at least images, videos and cameras for food, and the output of the optimized YOLOv8n model includes at least structured data of category, coordinates, confidence and scene labels (such as "refrigerated storage" and "mixed shelf sales"); the structured data is in JSON format;
[0068] S3: The output of the optimized YOLOv8n model is passed to the DeepSeek inference engine. The DeepSeek inference engine performs semantic analysis and compliance judgment on the detection results of the optimized YOLOv8n model. In other words, the DeepSeek inference engine performs contextual reasoning on the detection results and outputs the risk type and treatment suggestions.
[0069] S4: A hidden danger warning mechanism is proposed based on the DeepSeek reasoning engine. Combined with the generated rule base, if a violation is detected in the results of the DeepSeek reasoning engine, a screen alarm will be triggered and processing suggestions will be generated (such as "it is recommended to separate raw and cooked foods into designated areas"). Violations include at least mixing raw and cooked foods, improper storage, and mixing special and ordinary foods.
[0070] The DeepSeek inference engine is DeepSeek-R1. DeepSeek-R1 uses DeepSeekV3 as its base model and adopts GRPO as the reinforcement learning (RL) framework to improve the model's inference performance. It focuses on efficient inference and low cost. Its technical architecture and processing logic are closely designed around complex tasks such as food safety testing. The core algorithm principles are shown below.
[0071] The core technical architectures of DeepSeek-R1 are the Mixture of Experts (MoE) and Multi-Head Latent Attention (MLA).
[0072] The Mixture of Experts (MoE) is the "brain" of dynamic computing power allocation. It is a distributed architecture with dynamic sparse activation. It decomposes complex tasks into multiple subtasks, which are independently processed by specialized "expert networks" and finally integrate the results through a gating mechanism. Its core idea is to "let professional models handle professional tasks" to avoid the performance degradation of a single model in multiple tasks, while greatly reducing computing power consumption through sparse computing. Through the structural diagram of the MoE of the present invention, it can be found that it is composed of Input Hidden u t 、Router(routing module),Experts(expert module),Output Hidden h' t It consists of four parts, such as Figure 9 shown.
[0073] (1)Input Hidden u t This is the hidden layer representation of the input data obtained after preliminary processing, containing input feature information. It is the starting point for model processing and provides the data foundation for subsequent expert selection and calculation. For example, in natural language processing, it may be a word embedding or a preliminary feature representation of a sentence; in image processing tasks, it may be a feature map extracted by the convolutional layer.
[0074] (2) The core function of Router (routing module) is to analyze the u of the input layer t , calculate the suitability of each expert for the current input. t Perform linear changes such as f(u t )=W×u t+b, and apply the Sigmoid activation function to obtain the relevance score of each expert, so as to select the TOP-K with the highest score r It can dynamically allocate computing resources to ensure that the model only activates experts related to the current input, achieving sparse computing and improving efficiency.
[0075] (3) Experts (Expert Network) is composed of multiple independent sub-models, divided into Shared Expert (shared expert, green mark) and Routed Expert (routing expert, blue mark). The green module is the basic expert shared by all inputs. It provides general processing capabilities and is responsible for common or basic features in the input. For example, in text tasks, it may process common grammatical structures or common semantics; the blue module (from 1-N E -2) Dedicated experts are dynamically selected based on the input. Each routing expert specializes in processing input features in a specific area, i.e., local expertise. For example, in the food safety identification of the present invention, one expert focuses on the characteristics of mixed raw and cooked foods, while another expert focuses on the characteristics of improper food storage.
[0076] (4)Output Hidden h' t This is the final output hidden layer of the model, which is composed of selected experts (SharedExpert and TOP-K r Routed Expert) input u t It is obtained by combining the processing results of different experts, including both common features (from Shared Expert) and input-specific features (from RoutedExpert), providing rich representation for subsequent tasks (such as generating text).
[0077] The other is the Multi-Headed Latent Attention (MLA) of the present invention, which is an attention mechanism that optimizes the traditional Multi-Headed Attention (MHA), aiming to reduce memory usage and computational complexity while maintaining or even improving the model's ability to process sequence data. t 、 and RoPE (Rotational Position Encoding), Concatenate (connection operation), Multi-Head Attention (multi-head attention module), Output Hidden u t Composition, such as Figure 10 shown.
[0078] (1)Input Hidden h t This is the hidden layer representation obtained after the input data has been pre-processed. It contains the basic feature information of the input and is the starting point of MLA processing, providing raw data for subsequent attention calculations.
[0079] (2) Used to generate the potential representation related to the query, h t Break down into contextually relevant parts (indicates the input feature content) and position-related parts (Capture the position information of the input element). Used to generate potential representations related to Key-Value, u t Decomposed into the content part of the Key The content of the Value And the location part of the Key
[0080] (3)RoPE (Rotational Position Encoding) and Rotational Position Encoding (RoPE) is a special position encoding method that preserves the relative relationship of position information in a low-dimensional space. This ensures that the model can accurately capture the positional relationships between elements when processing sequential data, avoiding semantic understanding errors caused by the loss of position information. For example, when processing text sequences or the position coordinates of objects in images, RoPE can effectively maintain the accuracy of position information.
[0081] (4) Concatenate (connection operation) connects the location representation processed by RoPE with the content representation, that is, and connect, and This is done to simultaneously utilize the content features and position features of the input in subsequent attention calculations, allowing the model to more comprehensively understand the input data.
[0082] (5) Multi-Head Attention (Multi-Head Attention) is the core of MLA. It performs calculations on multiple attention heads in parallel, with each head focusing on different aspects of the input data. In this way, the model can capture richer feature representations and relationships. The Multi-Head Attention module receives the concatenated query, key, and value representations, calculates the attention weights of each head, and performs a weighted sum on the value. Finally, the results of each head are combined to generate the final output representation.
[0083] (6)Output Hidden u tThis is the output hidden layer after MLA processing. It incorporates the content and location information of the input data and captures the complex relationships between input elements through a multi-head attention mechanism, providing rich feature representations for subsequent tasks (such as classification and generation). The "Cached During Inference" label in the figure indicates that some intermediate results are cached during the inference process to improve inference efficiency and avoid repeated calculations.
[0084] The following is a detailed and professional explanation of MLA from the aspects of technical principles, core improvements and application advantages:
[0085] Technical principle: (1) Low-rank compression of KV cache: In traditional MHA, each attention head maintains a complete Key-Value (KV) matrix, and the memory usage increases with the sequence length L. 2 ) growth. MLA compresses high-dimensional KV matrices into potential representations through methods such as low-rank approximation. For example, compressing a KV matrix of dimension d×L (d is the feature dimension) to d′×L (d′<<d) greatly reduces the amount of storage. Taking actual scenarios as an example, when processing long texts (such as legal provisions) or long video frame sequences, the KV cache memory usage can be reduced to 5%-13% of the traditional solution. (2) Decoupling and fusion of position coding: MLA improves the position coding processing and and Using technologies such as Rotational Position Encoding (RoPE), content features are decoupled from position features. When calculating attention, position encoding is processed separately to ensure that the compressed KV can still accurately convey the positional relationship between elements. For example, when analyzing the position of objects in food images (such as the coordinates of the detection box of "mixed raw and cooked food"), semantic understanding deviations caused by the loss of position information can be avoided. (3) Efficient attention calculation: The Query input in the multi-head attention module and the compressed KV are connected through the projection matrix W Q ,W K ,W V Perform linear transformation to generate Q′, K′, V′. Attention weight Output: Attention(Q,K,V) = αV′. By optimizing matrix operations, computational complexity is reduced, significantly improving inference speed.
[0086] Core improvements: (1) Memory optimization: Traditional MHA has huge memory overhead when processing long sequences. MLA compresses the KV cache to make memory growth approximately linear O(L) rather than exponential. For example, when processing a sequence of L=4096, MLA can run smoothly with 16GB of video memory, while traditional MHA may not be able to process it due to insufficient memory. (2) Improved computing efficiency: After reducing the dimension of the KV matrix, the amount of matrix multiplication operations is greatly reduced. The inference speed can be greatly improved, which is suitable for real-time scenarios (such as frame-by-frame analysis of food safety video streams, which can achieve a processing speed of more than 15FPS). (3) Long sequence processing capabilities: With memory and computing optimization, MLA can better handle long sequence data. For example, when analyzing a supermarket's full-day surveillance video (tens of thousands of frames), continuous inference can be performed without video memory overflow, while traditional solutions may not be able to process it due to memory limitations.
[0087] Application advantages: (1) Real-time tasks: In scenarios that require rapid response (such as real-time detection of illegal operations in the kitchen), MLA's low latency (single-frame processing time <800ms) ensures timely detection of problems (such as "uncovered storage in the refrigerator") and triggers alarms. (2) Multimodal fusion: When processing multimodal information such as images (detection frame coordinates), text (regulatory clauses), and structured data (temperature sensor values), MLA can efficiently fuse KV representations of different modalities. For example, in food safety testing, the "raw meat" detection frame (visual modality) and the KV compressed representation of "Salmonella transmission conditions" (textual modality) are combined to accurately assess risks (such as the probability of cross-contamination). (3) Edge device deployment: Due to memory and computing optimization, MLA is suitable for low-computing edge devices (such as ARM architecture surveillance cameras). In the local supervision system of small supermarkets or catering stores, the "detection-analysis-warning" closed loop can be achieved without relying on high-computing power in the cloud, reducing deployment costs.
[0088] Finally, the most important reinforcement learning framework in the training phase of this invention is the Group Relative Policy Optimization (GRPO) algorithm, which aims to solve the problem of excessive resource consumption of the traditional Proximal Policy Optimization (PPO) algorithm in large model training. It abandons the critic model, which is usually the same size as the policy model, and directly uses the output of the reward model to estimate the baseline. Specifically, for each question q, GRPO starts from the old policy Sampling a set of outputs {o1,o1,…o G}, and then use the reward model to score each output. For example, in mathematical reasoning, multiple answers are generated for the same problem, and the reward value r is given based on the accuracy, completeness, etc. iGRPO then calculates the relative advantage of each policy, that is, how its reward performs relative to other outputs in the group. Based on these relative advantages, GPRO adjusts the policy model so that better-performing outputs are more likely to be generated, while poorer-performing outputs are suppressed. This process is achieved by gradually optimizing the policy model to obtain the latest π θ .
[0089]
[0090]
[0091] The strategy ratio represents the ratio of the probability of a new strategy generating a certain output to the probability of the old strategy generating that output. To prevent the new strategy from deviating too far from the old strategy, the GRPO algorithm of the present invention clips the strategy ratio. This limits the range of variation in the new strategy, preventing the probability of the new strategy generating a certain output from being significantly higher or lower than that of the old strategy. This ensures a more stable training process.
[0092] The core idea of reward feedback is to adjust the model's strategy (i.e., the way to generate text) through reward signals, so that the model is more inclined to generate high-reward outputs. For example, when the present invention organizes the improved YOLOv8 model's food safety reasoning results into natural language and sends it to DeepSeek, the model will generate multiple outputs, including multiple different reasoning processes and answers. Then, for each output, the reward model of the present invention will give corresponding rewards based on the accuracy and format correctness of the language and the consistency of the language. For a group of outputs, the relative advantage of each output is calculated. The relative advantage indicates how the reward of a certain output performs relative to other outputs in the group. The strategy model adjusts its generation strategy based on the reward signal so that high-reward outputs are more likely to be generated. The calculation formula is as follows. Among them, r i is the reward of the ith output, mean and std are the mean and standard deviation of the rewards within the group, respectively.
[0093]
[0094] KL divergence is used to measure the difference between the new policy and the old policy. If the new policy differs too much from the old policy, the KL divergence will be large, and the policy model will be penalized, thereby generating a more accurate and reasonable policy output. The formula is as follows:
[0095]
[0096] The Backbone layer includes five CBS modules, four C2f modules, one SPPF module, and embedded GAM and SA attention mechanisms;
[0097] like Figure 3 As shown in the figure, the Backbone layer extracts features through convolution and pooling operations, and sets multiple jump connections (also called residual connections) to fuse features and reduce computational complexity.
[0098] The CBS module is an important feature extraction module. The module structure of CBS is as follows: Figure 4 As shown in Figure 2, the CBS module consists of a convolutional layer, a batch normalization layer (BatchNorm, BN), and an activation function (SiLU);
[0099] The convolutional layer, also known as the Conv2d layer, uses a convolution kernel with a stride of 2 to achieve downsampling, reducing the spatial dimension of the feature map and increasing the channel capacity, thus achieving hierarchical feature expression;
[0100] The BN layer following the convolutional layer is used to standardize the batch data, solving the problem of internal covariate drift and, to a certain extent, weakening the disturbance caused by the input distribution to the network.
[0101] At the same time, due to the introduction of the SiLU activation function with nonlinear mapping, the entire network has greatly improved its performance in learning more complex features. The multi-layer stacked modular backbone network extracts semantic information from the image layer by layer, obtains discriminative multi-scale feature representation, accelerates the network convergence speed and obtains better generalization performance. It is easier to achieve better results by using a modular deep neural network structure.
[0102] The C2f module is lighter than the C3 module in YOLOv5. It is used instead of the C3 module. This replacement change combines the ELAN concept in YOLOv7, enabling the YOLOv8 model to access richer gradient flows during the training phase.
[0103] The module structure of C2f is as follows Figure 5 As shown;
[0104] The processing of the C2f module includes at least the following steps:
[0105] After the input image is convolved by the CBS module, a Split operation is performed to divide the feature map with C channels into two feature maps with C / 2 channels to reduce the amount of calculation and the scale of parameters.
[0106] One of the feature maps will pass through X Bottleneck modules (the module structure is as follows Figure 5 As shown), the feature map will be further convolved during the process of X Bottleneck modules;
[0107] Then another feature map is residually connected to the feature map output by X Bottleneck modules, enabling the network to learn deeper feature representations;
[0108] Finally, a CBS module is used to restore the normal number of channels of the feature map to facilitate the next step;
[0109] While maintaining high performance, C2f achieves a simultaneous reduction in computational complexity and storage requirements, demonstrating dual optimization of performance and efficiency.
[0110] The SPPF module (Spatial Pyramid Pooling-Fast) is an improved version of the SPP structure. The two structures are as follows Figure 6 Designed to reduce computational effort and increase the speed of the YOLOv8 model;
[0111] The SPP module generates multi-scale feature maps by pooling the input feature maps with different kernel sizes (such as 1×1, 5×5, 9×9, etc.). By integrating features of different scales, the SPP module can capture more contextual information, enabling the YOLOv8 model to perform better when processing objects of different sizes. However, the SPPF module processes the input feature maps through multiple Maxpool layers in series. This structural adjustment reduces computational complexity and memory consumption, speeding up the YOLOv8 model while maintaining the SPP module's ability to process feature maps of different scales.
[0112] GAM (Global Attention Mechanism) aims to reduce information loss, enhance global interaction features, and improve the performance of deep neural networks;
[0113] It addresses the limitations of traditional attention mechanisms, which often only retain partial information in the channel or spatial dimension;
[0114] GAM adopts a sequential channel-spatial attention mechanism to reconfigure the CBAM submodules;
[0115] The channel attention submodule in GAM focuses on extracting "important features". After globally pooling the input feature map, the features of each channel are compressed into a global value representing the entire feature map. Then, a two-layer multi-layer perceptron (MLP) is used to process the summarized global information and assign corresponding weights to each channel. Finally, the Sigmoid activation function is applied to generate channel attention. The structure of the channel attention submodule is as follows: Figure 7 shown.
[0116] The spatial attention submodule in CAM focuses on the "important positions" of "important features". It first compresses the number of channels in the feature map, that is, uses average pooling or maximum pooling operations to compress the number of channels to 1, and then uses the convolution layer to operate on the feature map of spatial information. This process will produce a spatial attention weight map, and each point on the map represents the importance of the corresponding position; then, the spatial attention weight map is multiplied by the original feature map elements in the spatial dimension to achieve weighted spatial positions; finally, it is processed and output through the Sigmoid activation function; the structure of the spatial attention submodule is as follows Figure 7 shown.
[0117] SA attention mechanism (Shuffle Attention) is a new visual attention mechanism. SA attention mechanism combines spatial attention and channel attention through permutation units. Compared with the previous hybrid attention, it is more efficient and is a lightweight and efficient attention structure.
[0118] The structure of the SA attention mechanism surpasses the current SOTA attention models such as SE and SGE on benchmarks such as ImageNet and COCO, while having lower computational complexity and parameter count.
[0119] like Figure 8 As shown in Figure 2, the SA attention mechanism includes feature grouping (Group), mixed attention, and feature fusion;
[0120] Initially, the input feature map is processed by feature grouping and divided into multiple groups. Each group represents a sub-feature of the input feature map. The input feature map is divided into g groups along the channel dimension, represented as X = [X1, X2, X3, ..., X g ],X g ∈R C / g×H×W , where each sub-feature gradually captures a specific semantic information as it is trained;
[0121] Then, X g It is divided into two branches, still divided along the channel dimension, and the two sub-features are represented as X g1 , X g2 ∈R C / 2g×H×W , is divided into two branches by the Split operation, represented by green and blue boxes respectively;
[0122] The green branch implements channel attention to reveal the dependencies between channels, while the blue branch captures the spatial dependencies between features and generates a spatial attention map.
[0123] In this way, we can focus on both semantic and position information at the same time, and the results of the two attentions are concat- ed together, which is now consistent with the input size of the group.
[0124] Finally, the channel shuffle operation is used to ensure the interaction between each group of sub-features, and all SA units are randomly mixed to obtain the final output feature map.
[0125] S3 and S4 include at least the following steps:
[0126] After receiving the data, the DeepSeek inference engine uses multimodal input fusion technology to normalize the detection frame coordinates into relative position codes, convert category labels into high-dimensional semantic vectors, and inject scenario-related regulatory knowledge and historical case data.
[0127] Based on the hybrid expert architecture (MoE), the gated network dynamically activates dedicated experts (such as "raw and cooked mixed risk experts" and "cold chain storage regulations experts") according to the input, and efficiently processes long-sequence detection data through the low-rank compressed multi-head latent attention (MLA) mechanism to achieve cross-modal association between detection frame coordinates and regulatory clauses and case features. When specific data is detected exceeding the threshold, the corresponding risk modeling is triggered and matched with international clauses, triggering a screen alarm and generating processing suggestions; for example, when the overlap rate of raw and cooked food detection frames is detected to exceed the threshold, the microbial risk modeling is synchronously triggered to match the national standard clauses to generate structured results including risk level, violation basis and rectification suggestions.
[0128] During the reasoning process, the Group Relative Policy Optimization (GRPO) algorithm dynamically calibrates multiple candidate rectification plans, prioritizing the output of highly compliant and actionable suggestions (such as recommending separation containers that comply with the GB 9684 standard);
[0129] The final results are transmitted back through a standardized interface, highlighting risk areas and providing pop-up alerts for corrective actions on the user side. Detection logs are then stored synchronously in the regulatory database, supporting risk tracing and statistical analysis. This process significantly improves detection and reasoning efficiency and the implementation of recommendations through scenario-aware expert activation, lightweight multimodal computing, and a compliance-first reasoning strategy. While maintaining low latency (single-frame processing ≤800ms), it also increases the compliance rate of corrective actions to 91%, providing an efficient and reliable technical solution for intelligent food safety supervision.
[0130] Ultimately, the present invention will achieve the following goals: (1) Semanticization of detection results: The results of the improved YOLOv8 model detection, including the detection box (category, location, confidence, detection scene), are converted into natural language descriptions and sent to the DeepSeek model of the present invention. DeepSeek will use natural language processing (NIP) technology to parse food safety standards and local management regulations (such as the "National Food Safety Standard") to build a structured rule base (such as "raw and cooked foods must be stored separately" and "special foods must be sold in separate areas") to conduct compliance analysis. (2) Hidden danger warning mechanism: Combined with the generated rule base, if violations such as mixing raw and cooked foods, improper storage, and mixing special and ordinary foods are detected in the DeepSeek reasoning results of the present invention, a screen alarm will be triggered and processing suggestions will be generated (such as "it is recommended to separate raw and cooked foods to designated areas"). (3) Multimodal input support: Image detection, users upload JPG / PNG format images, the system automatically unifies the image size to 640×640, calls the improved YOLOv8n model for detection, marks the hidden danger area, and superimposes the confidence. Video detection: Frame-by-frame detection and keyframe caching. Deepseek batch inference is used to generate a risk timeline (e.g., "Improper storage detected continuously in frames 10-15 of the video; it is recommended to check the seal of the freezer"). Real-time camera detection: Using OpenCV to call a USB camera, low-latency detection of real-time streaming (frame rate ≥ 15FPS) is achieved. Detection results are rendered in real time to the web interface, triggering an audible alarm and improving system responsiveness through asynchronous processing.
[0131] Based on the above content, we further propose engineering deployment and scenario matching:
[0132] (1) Lightweight model design: Based on YOLOv8n, the basic framework is built. Through the optimization of the attention mechanism, mAP@0.5 is improved by 5%-7% while maintaining high real-time performance (FPS>85). A cross-scale knowledge distillation and lightweight collaborative optimization method is proposed to decouple the domain-specific reasoning modules such as "food risk assessment" and "food storage regulatory clause matching" from the large model's mixed expert architecture (MoE). The modules are injected into the lightweight model in the form of structured knowledge capsules to ensure lossless migration of the core logic. The core reasoning capabilities of the 671B large model are efficiently migrated to the 7B-30B small model to meet the low power consumption and high real-time performance requirements of edge devices. (2) Dataset enhancement strategy: A multi-scene dataset containing a large number of annotated images is constructed. Combined with random masking, adaptive brightness adjustment, and multi-angle rotation, the model's robustness to lighting changes and partial occlusion is improved. Complex environments such as supermarkets and kitchens are simulated to improve the model's generalization ability.
[0133] In summary:
[0134] The present invention breaks through the above technical bottlenecks by integrating the YOLOv8 optimization architecture of the dual attention mechanism (GAM+SA) and the DeepSeek multimodal reasoning engine: (1) Feature enhancement: GAM (global attention) and SA (channel-spatial mixed attention) are embedded in the backbone network to enhance the ability to extract fine-grained features such as food edges and textures. Experiments show that mAP@0.5 is improved by 5.1%; (2) Lightweight design: Through C2f module reconstruction and SPPF acceleration strategy, real-time detection of FPS85 is achieved on the RTX 3050Ti device, which is 37% faster than the original YOLOv8x; (3) Intelligent reasoning: The DeepSeek large model is integrated to perform semantic association analysis on the detection results, output violation level assessment and rectification suggestions, and improve the efficiency of regulatory decision-making.
[0135] It will be apparent to those skilled in the art that the present invention is not limited to the details of the exemplary embodiments described above and that the invention can be embodied in other specific forms without departing from the spirit or essential characteristics of the invention. Therefore, the embodiments should be considered in all respects as illustrative and non-restrictive, and the scope of the invention is defined by the appended claims, not the foregoing description, and all variations within the meaning and range of equivalents of the claims are intended to be included therein. Any reference sign in a claim should not be construed as limiting the claim to which it relates.
Claims
1. An intelligent food safety detection method based on an improved YOLOv8 model and DeepSeek reasoning, characterized by: At least the following steps are included: S1: Based on the YOLOv8 model as the model framework; S2: Optimize the structure of YOLOv8n's Backbone layer. Embed the GAM and SA attention mechanisms in the second and tenth layers of the YOLOv8n Backbone layer, respectively. This enhances YOLOv8n's ability to extract key regional features and improves its ability to accurately detect food safety hazard scenarios. This results in an optimized YOLOv8n model, which is then used to identify food safety hazards. S3: The output of the optimized YOLOv8n model is passed to the DeepSeek inference engine. The DeepSeek inference engine performs semantic analysis and compliance judgment on the detection results of the optimized YOLOv8n model. In other words, the DeepSeek inference engine performs contextual reasoning on the detection results and outputs the risk type and treatment suggestions. S4: A hidden danger warning mechanism is proposed based on the DeepSeek reasoning engine. Combined with the generated rule base, if a violation is detected in the results of the DeepSeek reasoning engine, a screen alarm is triggered and processing suggestions are generated. The violations include at least mixing raw and cooked food, improper storage, and mixing special and ordinary foods.
2. The food safety intelligent detection method based on the improved YOLOv8 model and DeepSeek reasoning according to claim 1, characterized in that: The Backbone layer includes five CBS modules, four C2f modules, one SPPF module, and embedded GAM and SA attention mechanisms; The Backbone layer extracts features through convolution and pooling operations, and sets multiple jump connections to fuse features and reduce computational complexity.
3. The food safety intelligent detection method based on the improved YOLOv8 model and DeepSeek reasoning according to claim 2, characterized in that: The CBS module is an important feature extraction module, which consists of a convolution layer, a batch normalization layer and an activation function; The convolutional layer, namely the Conv2d layer, uses a convolution kernel with a step size of 2 to achieve downsampling, reducing the spatial dimension of the feature map and increasing the channel capacity, thereby achieving hierarchical feature expression; The BN layer following the convolutional layer is used to standardize the batch data, solving the problem of internal covariate drift and, to a certain extent, weakening the disturbance caused by the input distribution to the network. At the same time, due to the introduction of the SiLU activation function with nonlinear mapping, the entire network has greatly improved its performance in learning more complex features. The multi-layer stacked modular backbone network extracts semantic information from the image layer by layer, obtains discriminative multi-scale feature representation, accelerates the network convergence speed and obtains better generalization performance. It is easier to achieve better results by using a modular deep neural network structure.
4. The food safety intelligent detection method based on the improved YOLOv8 model and DeepSeek reasoning according to claim 2, characterized in that: The C2f module is lighter than the C3 module of YOLOv5. The C2f module is used instead of the C3 module. This replacement change combines the ELAN concept in YOLOv7, enabling the YOLOv8 model to access richer gradient flows during the training phase. The processing of the C2f module includes at least the following steps: After the input image is convolved by the CBS module, a Split operation is performed to divide the feature map with C channels into two feature maps with C / 2 channels to reduce the amount of calculation and the scale of parameters. One of the feature maps will pass through X Bottleneck modules, and the feature map will be further convolved during the process of passing through X Bottleneck modules; Then another feature map is residually connected to the feature map output by X Bottleneck modules, enabling the network to learn deeper feature representations; Finally, a CBS module is used to restore the normal number of channels of the feature map to facilitate the next step.
5. The food safety intelligent detection method based on the improved YOLOv8 model and DeepSeek reasoning according to claim 2, characterized in that: The SPPF module is an improved version of the SPP structure, which aims to reduce the amount of calculation and increase the speed of the YOLOv8 model; The SPP module generates multi-scale feature maps by performing pooling operations on the input feature maps with different kernel sizes (such as 1×1, 5×5, 9×9, etc.). By integrating features of different scales, the SPP module can capture more contextual information, making the YOLOv8 model perform better when processing objects of different sizes; However, the SPPF module processes the input feature map through multiple Maxpool layers in series. This structural adjustment reduces the amount of computation and memory consumption, speeds up the YOLOv8 model, and maintains the ability of the SPP module to process feature maps of different scales.
6. The food safety intelligent detection method based on the improved YOLOv8 model and DeepSeek reasoning according to claim 2, characterized in that: The GAM is designed to reduce information loss, enhance global interaction features, and improve the performance of deep neural networks; The GAM adopts a sequential channel-spatial attention mechanism to reconfigure the CBAM submodules; The channel attention submodule in GAM focuses on extracting "important features". It compresses the features of each channel into a global value representing the entire feature map after global pooling of the input feature map. Then, a two-layer multi-layer perceptron processes the summarized global information and assigns corresponding weights to each channel. Finally, a sigmoid activation function is applied to generate channel attention. The spatial attention submodule in CAM focuses on the "important positions" of "important features". It first compresses the number of channels in the feature map, that is, uses average pooling or maximum pooling operations to compress the number of channels to 1. The resulting feature map of spatial information is then operated on by a convolutional layer. This process generates a spatial attention weight map, in which each point on the map represents the importance of the corresponding position. The spatial attention weight map is then multiplied by the original feature map elements in the spatial dimension to achieve weighted spatial positions. Finally, it is processed and output through the Sigmoid activation function.
7. The food safety intelligent detection method based on the improved YOLOv8 model and DeepSeek reasoning according to claim 2, characterized in that: The SA attention mechanism is a new type of visual attention mechanism. The SA attention mechanism combines spatial attention and channel attention through permutation units. Compared with the previous hybrid attention, it is more efficient and is a lightweight and efficient attention structure. The SA attention mechanism includes feature grouping, mixed attention and feature fusion; Initially, the input feature map is processed by feature grouping and divided into multiple groups. Each group represents a sub-feature of the input feature map. The input feature map is divided into g groups along the channel dimension, represented as X = [X1, X2, X3, ..., X g ],X g ∈R C / g×H×W , where each sub-feature gradually captures a specific semantic information as it is trained; Then, X g It is divided into two branches, still divided along the channel dimension, and the two sub-features are represented as X g1 , X g2 ∈R C / 2g×H×W , is divided into two branches by the Split operation, represented by green and blue boxes respectively; The green branch implements channel attention to reveal the dependencies between channels, while the blue branch captures the spatial dependencies between features and generates a spatial attention map. In this way, we can focus on both semantic and position information at the same time, and the results of the two attentions are concat- ed together, which is now consistent with the input size of the group. Finally, the channel shuffle operation is used to ensure the interaction between each group of sub-features, and all SA units are randomly mixed to obtain the final output feature map.
8. The food safety intelligent detection method based on the improved YOLOv8 model and DeepSeek reasoning according to claim 1, characterized in that: S3 and S4 at least include the following steps: After receiving the data, the DeepSeek inference engine uses multimodal input fusion technology to normalize the detection frame coordinates into relative position codes, convert category labels into high-dimensional semantic vectors, and inject scenario-related regulatory knowledge and historical case data. Based on a mixture of experts (MoE) architecture, the system efficiently processes long sequences of detection data through a low-rank compressed multi-head latent attention mechanism, achieving cross-modal association between detection box coordinates and regulatory provisions and case features. When specific data exceeds a threshold, it triggers corresponding risk modeling and matches it with international regulations, triggering on-screen alerts and generating action suggestions. During the reasoning process, the group's relative strategy optimization algorithm dynamically calibrates multiple candidate rectification plans, giving priority to outputting highly compliant and actionable suggestions; The final results are transmitted back through a standardized interface, and risk areas are highlighted and rectification suggestions are popped up on the user side. The detection logs are also stored synchronously in the regulatory database to support risk tracing and statistical analysis.
Citation Information
Patent Citations
Pseudosciaena crocea target detection method and system by using deep learning technology
CN118506103A
Construction method and device of food safety intelligent supervision large model
CN118917652A
Power contact network foreign matter detection method and system based on improved YOLOv8 neural network
CN119027401A