Tobacco impurity detection method based on improved YOLOv8

By using an improved lightweight YOLOv8 model, the accuracy issues of multi-dimensional feature analysis and category identification in tobacco impurity detection were resolved, achieving efficient and accurate detection of tobacco impurities.

CN120913031APending Publication Date: 2025-11-07CHONGQING UNIV OF POSTS & TELECOMM
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510962617.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-14
Publication Date
2025-11-07

AI Technical Summary

Technical Problem

Existing methods for detecting tobacco impurities cannot comprehensively analyze the multi-dimensional characteristics of impurities, nor can they accurately detect the types of impurities.

Method used

An improved lightweight YOLOv8 model is adopted, including the UniRepLKNetBlock module, an improved BiFPN feature fusion structure, and an improved attention module R-EMA. Through multi-branch dilated convolution, weighted multi-input fusion, and multi-scale convolution, the model's global perception of impurities and segmentation accuracy are improved.

Benefits of technology

It significantly improves the model's global perception of irregular structures and targets with blurred edges, enhances the detection capability of multi-dimensional features, and improves the accuracy of impurity category identification and segmentation precision.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120913031A_ABST
    Figure CN120913031A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of image recognition, in particular to a tobacco impurity detection method based on improved YOLOv8, which comprises the following steps: acquiring a tobacco leaf data set, and preprocessing the tobacco leaf data set to obtain a preprocessed data set; processing the pre-processed data set by adopting a pre-trained improved lightweight YOLOv8 model to obtain an impurity discrimination result, and outputting an impurity annotation image; the improved lightweight YOLOv8 model comprises a backbone network based on a UniRepLKNetBlock module, an improved BiFPN feature fusion structure, and a prediction output module based on an improved attention module; according to the method, the global perception capability of irregular structures and edge fuzzy targets, the discrimination capability of weak texture targets and the segmentation accuracy in the tobacco impurity detection process in the prior art are improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of image recognition, and particularly relates to a tobacco impurity detection method based on an improved YOLOv8. BACKGROUND

[0002] In the process of tobacco leaf collection, transportation and processing, various impurities are often mixed in, thereby affecting the quality of the cigarette.

[0003] The traditional impurity detection method mainly includes a color feature analysis detection method and an optical analysis detection method. The color feature analysis detection method filters the standard tobacco leaf color library based on the existing foreign matter color library to update the standard color library, and uses the updated standard color library to identify foreign matter. The optical analysis detection method is based on the difference in reflectivity or transmittance of light between impurities and tobacco leaves. When the tobacco leaf passes through the photoelectric sensor, the system judges whether there is impurity according to the collected optical signal. Both methods are based on simple color and optical characteristics, and cannot comprehensively analyze the multi-dimensional characteristics of impurities, and cannot accurately detect the category of tobacco impurities.

[0004] Therefore, there is an urgent need for a tobacco impurity detection method that can comprehensively analyze the multi-dimensional characteristics of impurities and has higher accuracy in detecting the category of impurities. SUMMARY

[0005] Therefore, the present application discloses a tobacco impurity detection method based on an improved YOLOv8 to solve the above problems, comprising:

[0006] Obtain a tobacco leaf dataset, preprocess the tobacco leaf dataset to obtain a preprocessed dataset, and use a pre-trained improved lightweight YOLOv8 model to process the preprocessed dataset to obtain an impurity discrimination result and output an impurity labeled image.

[0007] Further, the improved lightweight YOLOv8 model comprises a backbone network based on a UniRepLKNetBlock module, an improved BiFPN (Bidirectional Feature Pyramid Network) feature fusion structure, and a prediction output module based on an improved attention module. The prediction output module based on the improved attention module includes four branch outputs P2-P5.

[0008] The present application has the following advantages:

[0009] The UniRepLKNetBlock module is introduced into the backbone network, the Bottle neck structure in the C2f module is replaced, the re-parameterized large kernel convolution and the attention mechanism are fused, the feature modeling capability of the model is significantly improved, and the multi-branch dilated convolution structure is introduced, which expands the receptive field while maintaining the inference efficiency, and effectively enhances the global perception capability of the model to irregular structures and edge blurred targets;

[0010] The channel space attention mechanism is introduced into the UniRepLKNetBlock module, the importance of the channel response intensity and the spatial region is adaptively adjusted, and the discrimination capability for weak texture targets is improved.

[0011] The feature fusion structure is replaced by the improved BiFPN feature fusion structure, the weighted multi-input fusion mechanism is used for weighted fusion, the importance modeling and dynamic adjustment of each input feature map are realized, and the problem of multi-dimensional features is solved.

[0012] The improved attention module R-EMA is used in the prediction output module, the local context of the feature is enhanced through 3*3 and 5*5 multi-scale convolution, so that the response capability of the model to different scale texture patterns is improved, and finally the overall segmentation accuracy is improved. BRIEF DESCRIPTION OF DRAWINGS

[0013] Figure 1 FIG. 1 is a structural schematic diagram of the tobacco impurity detection method based on the improved YOLOv8 in the present application;

[0014] Figure 2 FIG. 2 is a structural schematic diagram of the C2f-UniRepLKNetBlock module in Embodiment 1 of the present application;

[0015] Figure 3 FIG. 3 is a structural schematic diagram of the improved BiFPN feature fusion structure in Embodiment 1 of the present application;

[0016] Figure 4 FIG. 4 is a structural schematic diagram of the improved lightweight YOLOv8 model in Embodiment 1 of the present application;

[0017] Figure 5 FIG. 5 is a pre-training process visualization image in Embodiment 2 of the present application;

[0018] Figure 6 FIG. 6 is a detection effect visualization comparison diagram of the model before and after improvement in Embodiment 2 of the present application;

[0019] Figure 7 FIG. 7 is an ablation experiment result in Embodiment 2 of the present application;

[0020] Figure 8The comparative experiment results of the mainstream detection model in Embodiment 2 of the present application. DETAILED DESCRIPTION

[0021] In order to make the purpose, technical scheme, characteristics and advantages of the present application more clear, and to make the technical personnel in the art better understand the technical scheme of the present application, the present application will be further described in detail below in combination with the drawings and embodiments.

[0022] Embodiment 1:

[0023] The embodiment provides a tobacco impurity detection method based on an improved YOLOv8, which comprises the following steps:

[0024] A tobacco dataset is acquired, and the tobacco dataset is preprocessed to obtain a preprocessed dataset; an improved lightweight YOLOv8 model pre-trained is used to process the preprocessed dataset to obtain an impurity discrimination result and output an impurity labeled image.

[0025] The improved lightweight YOLOv8 model comprises a backbone network based on a UniRepLKNetBlock module, an improved BiFPN feature fusion structure and a prediction output module based on an improved attention module; the prediction output module based on the improved attention module comprises four branch outputs P2-P5.

[0026] Specifically, the tobacco dataset is composed of real-time shooting images of tobacco on a tobacco production line, and the preprocessing comprises impurity labeling and dataset division of the tobacco dataset.

[0027] The structure of the standard YOLOv8 network comprises a backbone network (Backbone), a feature fusion structure (Neck) and a prediction output module (Head). Among them, the backbone network is used to acquire an original input image and extract multi-scale feature information from the original input image, the backbone network comprises a C2f module, the C2f module is used to realize efficient feature fusion and extraction, and comprises a Bottleneck structure; the feature fusion structure is used to complete bidirectional feature fusion from top to bottom and from bottom to top, enhance semantic complement and positioning compensation between different scales, and obtain fused features; the prediction output module performs target class recognition, boundary box regression and instance mask generation based on the fused features, and outputs a final image.

[0028] Based on the above standard YOLOv8 network, the improvements of the present application comprise: introducing a UniRepLKNetBlock module in the backbone network; replacing the feature fusion structure with an improved BiFPN feature fusion structure; and using an improved attention module R-EMA in the prediction output module.

[0029] Further, as Figure 4As shown in the structural schematic diagram of the improved lightweight YOLOv8 model in the embodiment, the backbone network based on the UniRepLKNetBlock module comprises the UniRepLKNetBlock module, and the UniRepLKNetBlock module is arranged in a C2f module of the backbone network and is used for replacing a Bottleneck structure in the C2f module to form a C2f-UniRepLKNetBlock module.

[0030] As shown in the structural schematic diagram of the C2f-UniRepLKNetBlock module, n connected RepLKNetBlock modules in the figure represent the UniRepLKNetBlock module. The UniRepLKNetBlock module introduces a multi-branch dilated convolution structure, expands the receptive field while maintaining the inference efficiency, effectively enhances the global perception ability of the model to irregular structures and edge fuzzy targets, and is especially suitable for detection scenes in which small foreign matters and complex texture backgrounds coexist. The UniRepLKNetBlock module fuses the reparameterization large kernel convolution and the attention mechanism, and significantly improves the feature modeling ability of the model. Figure 2

[0031] To better represent the spatial structure and channel response relationship, the application further arranges a lightweight channel space attention mechanism SE (Squeeze-and-Excitation) module in the UniRepLKNetBlock module, adjusts the importance of the channel response intensity and the spatial region adaptively, and improves the discrimination ability to weak texture targets. The SE module guides the network to focus on the key structure region in the feature extraction stage, and improves the detection and segmentation accuracy. The SE attention mechanism comprises: Squeeze (compression): compressing the spatial dimension feature information into a global statistic quantity in the channel dimension, capturing the global information of each channel; Excitation (excitation): learning the dependency relationship between channels through a full connection layer and an activation function, and outputting the weight of each channel; Scale (scaling): multiplying the learned weight with the original feature channel, enhancing the features of important channels, and suppressing the features of unimportant channels.

[0032] Further, the improved lightweight YOLOv8 model comprises four output branches, respectively corresponding to different scale feature maps P2, P3, P4 and P5 from bottom to top. The image resolutions processed by P2 to P5 are from high to low, the features are from shallow to deep, and the target boundary perception ability is from small to large. Each branch processes the corresponding scale fusion feature map, and is used for predicting impurity targets at different scales.

[0033] ​The improved BiFPN feature fusion structure enhances the fusion ability between shallow detail features and deep semantic features by constructing an up-down bidirectional feature flow path and fusing the output nodes with the input nodes, thereby improving the model's detection ability for multi-scale impurities. Figure 3 As shown in FIG. 3, which is a structural diagram of the improved BiFPN feature fusion structure. The white nodes on the left side represent the input nodes of different output branches of the prediction output module, representing feature information from different sources. The nodes of different colors on the right side represent fusion nodes, and the arrows represent the flow direction of the feature information. The arrows from the input nodes to the fusion nodes represent the fusion of different input features at the fusion nodes according to the arrow paths. Through the improved BiFPN feature fusion structure, multi-source features are integrated and converged to obtain more expressive fusion features. On the basis of maintaining the original middle-high layer feature (P3-P5) output, the shallow high-resolution feature P2 is introduced, which can effectively solve the problems of missing shallow information and easy loss of boundary details in the traditional structure by supplementing the perception of the boundary and structure of the tiny foreign matter.

[0034] Specifically, the improved BiFPN feature fusion structure adopts a weighted multi-input fusion mechanism (Weighted Concat) for weighted fusion. On the basis of the traditional channel-level concatenation (Concat) operation, a learnable weight parameter is introduced to model and dynamically adjust the importance of each input feature map. The forward propagation process of Weighted Concat includes:

[0035] Step 1, receive feature map input {F1, F2, … F N} from different scale branches.

[0036] Step 2, introduce a learnable scalar weight ω i for each scale branch, and construct a weight vector w = [ω1, ω2, …, ω N ].

[0037] Step 3, normalize the weight vector to obtain the normalized weight, and the normalization strategy adopted is:

[0038]

[0039] where α i represents the normalized weight, and ε is a small value set to prevent the denominator from being zero. The normalization process is used to avoid unstable or excessive bias of the weight.

[0040] Step 4, weight the feature map input using the normalized weight to obtain the weighted feature map The formula is:

[0041]

[0042] Step 5, the weighted feature maps are spliced in the channel dimension to form the final fused feature F fused , the formula is:

[0043]

[0044] wherein Concat() represents splicing.

[0045] Through weighted fusion, the dynamic collaboration of cross-level information is realized, the problem of fusion imbalance between different scale features is solved, and the perception ability and segmentation accuracy of the model for irregular shape, small scale or adhesion type impurity targets are improved. While maintaining efficient inference, deep coupling and efficient transmission of cross-layer information are realized. The improved BiFPN feature fusion structure improves the recognition robustness of the model for fine-grained, small-scale and adhesion complex impurities, enhances the overall segmentation performance, and solves the problem of multi-dimensional features.

[0046] Further, the prediction output module based on the improved attention module includes an improved attention module R-EMA, which is embedded in P2-P5. The improved attention module R-EMA uses 3x3 and 5x5 multi-scale convolution to enhance the local context of the features, thereby improving the response ability of the model to different scale texture patterns.

[0047] Specifically, R-EMA is used to output different scale feature segments. In the P2 and P3 branches, the R-EMA module can significantly improve the response strength of the model to high-resolution small impurities such as insect eggs and plant leaves; in the P4 and P5 branches, the module enhances the model's ability to identify edge-bloated targets in complex texture backgrounds. The R-EMA module improves the perception accuracy and stability of the model in multi-scale, low-contrast and boundary confusion difficult scenarios, and provides stronger target focusing ability for instance segmentation tasks. Through explicit modeling on different scale feature maps, redundant information suppression and target region enhancement are synergized, and through feature complementation and discriminative information fusion between multi-scale branches, the model has better feature focusing and foreign matter segmentation performance at each output scale, ultimately improving the overall segmentation accuracy.

[0048] The non-maximum suppression and multi-scale feature fusion in the standard YOLOv8 network are used to process different scale feature segments, and output impurity labeled images.

[0049] Embodiment 2:

[0050] The embodiment provides a tobacco impurity detection method based on an improved YOLOv8, which comprises the following steps:

[0051] The tobacco dataset is obtained, preprocessed to obtain a preprocessed dataset, and the preprocessed dataset is processed by using the improved lightweight YOLOv8 model to obtain an impurity discrimination result and output an impurity labeled image.

[0052] The improved lightweight YOLOv8 model comprises a backbone network based on a UniRepLKNetBlock module, an improved BiFPN feature fusion structure, and a prediction output module based on an improved attention module.

[0053] In this embodiment, the resolution of each image in the tobacco dataset is 2048x2048, and the impurity types include plant leaves, tobacco heads, carton boards, wood blocks, glass, insect eggs, blue plastic, yellow plastic, green plastic, black plastic, metal, gray plastic, white foam, red foam, feathers, rubber, and gray paperboard.

[0054] The tobacco dataset is labeled for impurities using the labelme tool, and the training set and the validation set are divided in a ratio of 9:1, the training set is used to pretrain the improved lightweight YOLOv8 model, and the validation set is used to test the improved lightweight YOLOv8 model. The final image obtained in this embodiment contains 2569 plant leaves, 68 tobacco heads, 121 carton boards, 57 wood blocks, 99 glass, 757 insect eggs, 143 blue plastic, 33 rubber, 102 yellow plastic, 92 green plastic, 109 metal, 81 gray plastic, 52 white foam, 48 red foam, 139 feathers, 120 gray paperboard, and 55 black plastic.

[0055] Further, the backbone network based on the UniRepLKNetBlock module comprises a UniRepLKNetBlock module, and the forward propagation process of the UniRepLKNetBlock module comprises:

[0056] Step 1, performing deep convolution operation on the input feature map of the UniRepLKNetBlock module to obtain down-sampling feature X agg .

[0057] Specifically, the deep convolution operation is performed by using the Dilated Reparam Block with different expansion rates and convolution kernel size branches, and the outputs of the multiple branches are added, and the formula is:

[0058]

[0059] wherein BN i represents batch normalization, denotes a depthwise convolution, r i denotes the dilation rate of the depthwise convolution, k i denotes the convolution size of the depthwise convolution, denotes the input feature map, denotes the real set, C, H, and W represent the number of channels, height, and width, respectively, Y i denotes the output of a single branch.

[0060] Step 2, structural reparameterization of the UniRepLKNetBlock module according to the down-sampling feature. The formula is as follows:

[0061] X agg ≈Conv K (X),

[0062] where Conv K denotes the convolution after structural reparameterization, and K denotes the kernel size after structural reparameterization.

[0063] Step 3, normalization processing is performed on the down-sampling feature X agg , and the SE attention mechanism is used for enhancement to obtain the attention-enhanced feature X se . The formula is as follows:

[0064] X se =SE(Norm(X agg ))

[0065] where Norm( ) denotes normalization processing, and SE() denotes the SE attention mechanism.

[0066] Step 4, the FFN with GRN (Feed-Forward Network with Global Response Normalization) structure is used to extract the global feature of X se .

[0067] Specifically, the channel dimension of X se is transformed into the dimension of FFN, and after nonlinear mapping, the obtained feature is mapped back to the dimension of the attention-enhanced feature. The nonlinear mapping includes GELU mapping and GRN mapping; the formula used to extract the global feature of the attention-enhanced feature is as follows:

[0068]

[0069] Wherein, Y represents a global feature, Linear1() and Linear2() represent two different channel dimension transformation operations, GELU() represents a GELU mapping, and GRN() represents a GRN mapping.

[0070] Step 5, a learnable scaling factor γ is set, and the global feature is connected with the input feature in residual connection according to the learnable scaling factor, to obtain a forward propagation output of the UniRepLKNetBlock module, and the formula is:

[0071] Z = X + DropPath(γ·Y)

[0072] Wherein, Z represents the forward propagation output of the UniRepLKNetBlock module, DropPath() represents a path-level regularization processing, which is used to randomly discard the residual branch to enhance the robustness of the network.

[0073] Further, the prediction output module based on the improved attention module includes an improved attention module R-EMA, which is embedded in P2-P5, and the forward propagation process of the R-EMA module includes:

[0074] Step 1, the input feature map of the improved attention module is divided into G group features, each group feature containing C / G channels, and the formula is:

[0075]

[0076] Wherein, represents the input feature map, B represents the batch size, x g represents the group feature, G represents the number of groups, and G is 32 in this embodiment, which is divided into G group features to realize group attention features, thereby reducing the calculation amount, maintaining the calculation efficiency, and enhancing the attention ability of the model to different features.

[0077] Step 2, transverse pooling and longitudinal pooling are respectively performed on each group feature, and the two pooling direction features are spliced, and 1×1 convolution is adopted to compress into joint spatial weight, and the formula is:

[0078] h w =Conv 1×1 (Concat(x g ,x w ))

[0079] Wherein, h w represents the joint spatial weight, Concat() represents the splicing operation, and Conv h () represents 1×1 convolution.

[0080] Step 3, split the joint spatial weight into attention weights of two directions, formula is:

[0081] x′ g ,x′ w =Split(h w )

[0082] wherein x′ h ,x′ w represent attention weights of two directions (height and width), Split( ) represents split operation.

[0083] Step 4, generate spatial weighted output according to grouping feature and attention weights of two directions, formula is:

[0084] x attn =GN(x g ·σ(x′ h )·σ(x′ w T ))

[0085] wherein x attn represents spatial weighted output, σ( ) represents Sigmoid activation function, GN( ) represents group normalization.

[0086] Step 5, respectively perform feature extraction on grouping features by using convolution with different kernel sizes, splice and fuse by using 1×1 convolution, obtain fused feature, formula is:

[0087] x multi =Conv 1×1 (Concat(Conv 3×3 (x g ),Conv 5×5 (x g )))

[0088] wherein x multi represents fused feature, Conv 1×1 represents 1×1 convolution, Conv 3×3 ( ) represents 3×3 convolution, Conv 5×5 ( ) represents 5×5 convolution.

[0089] Step 6, respectively perform GAP and SoftMax operation on spatial weighted output and fused feature, obtain attention vector, formula is:

[0090] w1=SoftMax(GAP(x attn ))

[0091] w2=SoftMax(GAP(x multi ))

[0092] wherein w1 and w2 represent attention vectors.

[0093] Step 7, matrix multiplication is performed on the spatially weighted output and the fused feature respectively using the attention vectors to obtain channel perception coefficients, and the formula is:

[0094] a1 = w1 Flatten(x multi )

[0095] a2 = w2 Flatten(x attn )

[0096] wherein a1 and a2 represent channel perception coefficients.

[0097] Step 8, the spatial dimensions of the channel perception coefficients a1 and a2 are reshaped to obtain attention weights, and the formula is:

[0098]

[0099] wherein W represents attention weights, σ() represents a sigmoid function, and reshape() represents a reshaping operation.

[0100] Step 9, the grouped features are weighted using the attention weights, and the number of channels is restored to the number of input features Figure 1 , to obtain the output vector of the improved attention module R-EMA, and the formula is:

[0101]

[0102] wherein x out represents the forward propagation output of the R-EMA module, i.e., the feature fragment of the scale branch.

[0103] The non-maximum suppression in the standard YOLOv8 network and the multi-scale feature fusion are used to process the feature fragments of different scales to output impurity labeled images.

[0104] Further, the improved YOLOv8 network in the embodiment is trained and evaluated, and the experimental conditions include hardware and software environments and training parameter settings. In the embodiment, the hardware and software environment configurations are as follows: CPU: 14 cores, Xeon(R) Platinum 8362C; GPU: NVIDIA GeForce RTX 4090D (24 GB); operating system: Ubuntu 20.04; Python: 3.8; PyTorch: 2.0.0; CUDA: 11.8; cuDNN: 8.0.4. The training parameter settings are as follows: input image size: 640x640; Batch Size: 16; Epoch: 300; Early Stopping: 50; Weight Decay: 0.0005; Momentum: 0.937; Mosaic Enhancement: True; Mixup: False.

[0105] The validation set and the test set of the data set are used to pre-train the model in the embodiment, and various indicators are recorded and analyzed, including mean accuracy (mAP), precision (Precision), recall (Recall), parameter quantity (Params), and other indicators, to verify the improvement effect. The evaluation index calculation formula is:

[0106]

[0107]

[0108] Among them, TP is a true example, which represents the number of correct positive samples; FP is a false positive, which represents the number of incorrect negative samples; and FN is a false negative, which represents the number of incorrect positive samples.

[0109] The pre-training process is visualized as shown in Figure 5 The horizontal axis represents Recall and the vertical axis represents Precision.

[0110] As shown in Figure 6 The detection effects before and after the improvement are visualized, and the figure shows the visualization comparison of the detection effects of the model before and after the improvement. It can be seen that in different complex image scenes such as complex impurity morphology, overlapping and dense tobacco, fuzzy impurity boundary, and strong background interference, the tobacco impurity detection method based on the improved YOLOv8 in the application can more comprehensively analyze the multi-dimensional characteristics of impurities and improve the tobacco impurity detection effect.

[0111] In order to evaluate the improvement effect of the three improvements on the model performance, an ablation experiment is performed on the improved lightweight YOLOv8 model in the embodiment, and the ablation experiment results are as follows: Figure 7As shown, after the improved C2f-UniRepLK, the improved BiFPN, R-EMA and mutual combination are introduced into the standard YOL Ov8 respectively and individually, the various indexes of the model are improved significantly. The average precision of the original model is 85.8%, the average precision of the improved model is 89.9%, which is increased by 4.4 percentage points; the precision is increased from 85.5% to 88.9%, which is increased by 3.4 percentage points; the recall rate is increased from 80.2% to 84.4%, which is increased by 4.2 percentage points.

[0112] Further, compared with other mainstream detection models, the comparison results of the mainstream detection models are as shown in Figure 8 It can be seen that the method has the highest mAP, Precision and Recall, and the model complexity is relatively low, and has the characteristics of light weight.

[0113] The present application shows significant performance improvement on the data set. And through the ablation experiment, the comparison experiment of different networks and the comparison of detection effect before and after improvement, the present application effectively solves the above-mentioned challenges, and performs well in the face of complex practical application environment, and has wide application prospect.

[0114] Finally, it should be noted that the above only describes some embodiments of the present application, and for those skilled in the art, various changes, modifications, replacements and deformations of these embodiments can be made without departing from the principles and spirits of the present application, the protection scope of the present application is defined by the appended claims and their equivalents, and the above behaviors should be covered within the protection scope of the present application.

Claims

1. A tobacco impurity detection method based on improved YOLOv8, characterized in that, The method comprises the following steps: obtaining a tobacco dataset, preprocessing the tobacco dataset to obtain a preprocessed dataset; processing the preprocessed dataset by using a pre-trained improved lightweight YOLOv8 model to obtain an impurity discrimination result and output an impurity labeled image; the improved lightweight YOLOv8 model comprises a backbone network based on a UniRepLKNetBlock module, an improved BiFPN feature fusion structure, and a prediction output module based on an improved attention module; the prediction output module based on the improved attention module comprises four branch outputs P2-P5.

2. The improved YOLOv8-based tobacco impurity detection method according to claim 1, characterized in that, The backbone network based on the UniRepLKNetBlock module comprises a UniRepLKNetBlock module, and the forward propagation process of the UniRepLKNetBlock module comprises the following steps: Step 1, perform deep convolution operation on the input feature map of the UniRepLKNetBlock module to obtain down-sampling feature X agg ; Step 2, structure reparameterization of the UniRepLKNetBlock module according to the down-sampling feature; Step 3, normalize the down-sampled features and enhance them based on the SE attention mechanism to obtain attention-enhanced features X se ; Step 4, extracting global features of the attention-enhanced features by using a channel attention-enhanced feedforward network structure; Step 5, setting a learnable scaling factor, and performing residual connection between the global features and the input features according to the learnable scaling factor to obtain the forward propagation output of the UniRepLKNetBlock module.

3. The improved YOLOv8-based tobacco impurity detection method according to claim 2, characterized in that, The formula for structure reparameterization of the UniRepLKNetBlock module is as follows: where X represents the input feature map of the UniRepLKNetBlock module, Conv K represents the convolution after structure reparameterization, K represents the convolution kernel size after structure reparameterization, r i represents the expansion rate of the depth convolution, k i represents the convolution size of the depth convolution.

4. The improved YOLOv8-based tobacco impurity detection method according to claim 2, characterized in that, The down-sampling feature is normalized and enhanced based on the SE attention mechanism, and the formula used is as follows: X se = SE(Norm(X agg )) wherein Norm() represents normalization processing, and SE() represents the SE attention mechanism.

5. The improved YOLOv8-based tobacco impurity detection method according to claim 2, characterized in that, The global features of the attention-enhanced features comprise: transforming the channel dimension of the attention-enhanced features into the dimension of the FFN, performing nonlinear mapping, and then mapping the obtained features back to the dimension of the attention-enhanced features; the nonlinear mapping comprises GELU mapping and GRN mapping; and the formula used for extracting the global features of the attention-enhanced features is as follows: Y = Linear2(GRN(GELU(Linear1(X se )))) wherein Y represents the global features, Linear1() and Linear2() represent two different channel dimension transformation operations, GELU() represents GELU mapping, and GRN() represents GRN mapping.

6. The improved YOLOv8-based tobacco impurity detection method according to claim 1, characterized in that, The improved BiFPN feature fusion structure adopts a weighted multi-input fusion mechanism for weighted fusion.

7. The improved YOLOv8-based tobacco impurities detection method according to claim 1, wherein, The prediction output module based on the improved attention module comprises an improved attention module, which is embedded in P2-P5, and the forward propagation process of the improved attention module comprises the following steps: Step 1, dividing the input feature map of the improved attention module into G group features, each group feature comprising C / G channels, wherein C represents the number of channels of the input feature map; Step 2, performing horizontal and vertical pooling on each group feature respectively, splicing the two pooling direction features, and compressing them into joint spatial weights by using a 1x1 convolution; Step 3, splitting the joint spatial weights into attention weights in two directions; Step 4, generating spatial weighted output according to the group features and the attention weights in two directions. Step 5, feature extraction is performed on the grouped features respectively by using convolutions with different kernel sizes, the features are spliced and fused by using 1x1 convolution, and the fused features are obtained; Step 6, GAP and SoftMax operations are performed on the spatial weighted output and the fused features respectively, and the attention vector is obtained; Step 7, matrix multiplication is performed on the spatial weighted output and the fused features respectively by using the attention vector, and the channel perception coefficient is obtained; Step 8, the spatial dimension of the channel perception coefficient is reshaped, and the attention weight is obtained; Step 9, the grouped features are weighted by using the attention weight, and the channel number is restored to be consistent with the input feature map, and the forward propagation output of the improved attention module is obtained.

8. The improved YOLOv8-based tobacco impurity detection method according to claim 7, characterized in that, The formula used to generate the spatial weighted output is: x attn = GN(x g · σ(x′ h ) · σ(x′ w T )) where x attn represents the spatially weighted output, x g represents the group features, x′ h ,x′ w represents the attention weights in two directions, σ() represents the Sigmoid activation function, and GN() represents group normalization.

9. The improved YOLOv8-based tobacco impurity detection method according to claim 7, characterized in that, The convolutions with different kernel sizes include 3x3 convolution and 5x5 convolution.