A method for detecting defects in vials based on improved YOLOv8
By introducing the spatially separable pooling attention module (SSPA) and the feature enhancement module (MMFF) into the YOLOv8 model, the problem of low accuracy in detecting defects in vials of syringes is solved, and the ability to detect subtle and complex defects is improved.
Patent Information
- Application Number
- CN202510804742.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-17
- Publication Date
- 2025-09-19
- Estimated Expiration
- 2045-06-17
AI Technical Summary
The existing YOLOv8 model has low accuracy in detecting defects on glass vials, and it is difficult to effectively detect complex defects such as scratches, dirt, and abnormal filling volume on the bottle.
The spatially separable pooling attention module SSPA and the feature enhancement module MMFF are introduced into the backbone network of the YOLOv8 model. Through improved feature extraction and fusion methods, the detection capability of subtle and complex defects is enhanced.
The accuracy of vial defect detection has been improved, especially the detection capabilities of subtle features such as scratches, dirt and abnormal filling volume against complex backgrounds, achieving higher detection accuracy and efficiency.
Smart Images

Figure CN120318607B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer vision technology, and in particular to a method for detecting defects in vials based on an improved YOLOv8. Background Art
[0002] With the rapid development of the medical field, the requirements for defect detection in medical products are increasing, and computer vision-based defect detection methods are gradually becoming mainstream. Traditional object detection algorithms are based on hand-designed feature extractors and machine learning algorithms. They use manually defined features combined with classifiers to detect and classify objects. This process requires extensive expertise and experience, and is relatively complex. In contrast, object detection methods based on deep learning have performed better in production applications. Deep learning neural networks learn target features from raw images through end-to-end training, completing detection tasks in the corresponding context, reducing manual intervention and improving detection accuracy and efficiency in complex environments.
[0003] Vials, glass pharmaceutical containers, are common medical packaging products. The integrity of the vial's exterior and the condition of its interior directly or indirectly impact the quality and safety of the medication contained within. This is crucial for patient health, and ensuring product quality and safety is paramount. During the bottling process, various issues can arise, such as foreign matter, bottle damage, incomplete seals, and medication re-dissolution. These challenges pose challenges to efficient and accurate vial quality inspection.
[0004] The production line inspection process for vials of pharmaceuticals is divided into four parts: plastic cap inspection, aluminum cap inspection, top of the vial, and glass bottle inspection. Timely and accurate detection and screening of defective products during this process effectively reduces waste of production resources and lowers economic costs. The practical application of the YOLO series of models can achieve a balanced approach to inspection speed and accuracy, making it more suitable for real-time inspection of vial production lines. The YOLOv8-n basic model can achieve high-precision inspection of the plastic cap, aluminum cap, and top of the vial. However, defects in the glass vial body are complex: scratches on the bottle body are not obvious; dirt varies in size and is discretely distributed; and defects caused by abnormal filling levels have irregular shapes. These phenomena increase the difficulty of detection, resulting in low overall accuracy of the basic network for glass vial inspection. Summary of the Invention
[0005] In order to solve the above technical problems, the present invention provides a vial defect detection method based on improved YOLOv8 with a simple algorithm and high detection accuracy.
[0006] The technical solution of the present invention to solve the above technical problems is: a method for detecting vial defects based on improved YOLOv8, comprising the following steps:
[0007] S1: Obtain and preprocess vial defect data to obtain training and test sets;
[0008] S2: Establish an improved YOLOv8 defect target detection model;
[0009] Using YOLOv8 as the basic framework, the spatially separable pooling attention module (SSPA) is introduced in the shallow layer of the backbone network. The feature enhancement module (MMFF) is introduced in the neck to replace the C2f module. Convolution kernels of different sizes are used to extract defect features and stack them to improve feature fusion. The detection head outputs defect targets.
[0010] S3: Use the training set to train the defect target detection model, optimize the loss function, and update the model weight parameters until the loss function converges;
[0011] S4: Use the test set to test the defect target detection model.
[0012] In the above-mentioned method for detecting vial defects based on the improved YOLOv8, the specific process of step S1 is as follows:
[0013] S11: The vials enter the image acquisition area via a conveyor belt. The sensor is triggered as the vials are inspected at a constant speed. The industrial camera samples images of the vials from multiple angles.
[0014] S12: Perform target area positioning, image segmentation, defect annotation, and data enhancement on the collected image samples to complete image preprocessing, and divide the training set and test set into proportions.
[0015] In the above-mentioned method for detecting defects in vials based on the improved YOLOv8, the specific process of target area positioning, image segmentation, and data enhancement in step S12 is as follows:
[0016] Target area positioning: Binarize the original image to show the bottle outline against a white background. Calculate the center of mass of the white background area and locate the bottle wall and bottom along the horizontal and vertical grayscale changes. Draw a rectangular frame symmetrical about the center of mass to locate the target area.
[0017] Image segmentation: Fine-tune and scale the rectangular frame, segment the image, and remove the background area to obtain the bottle area to be inspected, making it easier to detect defects within the area; batch processing is performed to obtain samples to be inspected;
[0018] Data enhancement: For the processed samples to be tested, data enhancement operations are performed to expand the data volume.
[0019] In the above-mentioned method for detecting defects in vials based on the improved YOLOv8, in step S2, the spatially separable pooled attention module (SSPA) includes two parts: attention weight calculation and information aggregation. The attention weight calculation includes three parts: local feature extraction, global feature generation, and spatial separation features. The information aggregation includes merging information between different feature groups after adjusting the attention weights of multiple feature groups.
[0020] If the input features are given , , represents the field of real numbers, Represent the three dimensions of channel number, height and width respectively. Divide Group, represented by ,in express The Group features, , / / represents integer division operation; each set of features is extracted and fused through three-part calculation, and finally the features of each set are aggregated.
[0021] In the above-mentioned method for detecting defects in vials based on the improved YOLOv8, the process of calculating the attention weight in step S2 includes:
[0022] Local feature extraction: For each set of input features, deploy them horizontally 、 、 The pooling window of size is used for global average pooling and deployed in the vertical direction. 、 、 The pooling windows of different sizes are used for global average pooling; the deployment of pooling windows in a single direction embeds the spatial position information into the channel dimension; pooling windows of different sizes balance the short-distance dependencies when establishing long-distance dependencies in discrete areas, and realize the capture of multi-scale features under different distance dependencies, and obtain two local feature maps. as follows:
[0023] ;
[0024] ;
[0025] in, 、 Respectively represent the splicing operations along the horizontal and vertical directions, Represent the horizontal and vertical coordinates of the pixel respectively, is the size of the global average pooling window, , ;
[0026] Global feature generation: using 1×1 standard convolution processing and , and obtain feature maps respectively and feature maps , and Perform matrix multiplication operations between them to generate global feature maps :
[0027] ;
[0028] ;
[0029] ;
[0030] in, represents a 1×1 convolution operation, Represents matrix multiplication operation;
[0031] Global feature map Apply 1×1 standard convolution for information fusion and pass the Sigmoid function to obtain the global feature attention weight :
[0032] Spatial separation features: and Upsample to the original feature map size Get the feature maps respectively and feature maps , and conduct Dimensions and Transposition between dimensions;
[0033] ;
[0034] ;
[0035] in, represents the transpose operation, Represents an upsampling operation;
[0036] and exist Splicing in the dimension direction The feature map is then extracted through a 3×3 standard convolution to obtain a feature enhancement map. , realizing the reorganization of local features across spatial dimensions, so that the feature information in the horizontal and vertical directions can be transmitted complementary;
[0037] ;
[0038] in, represents a 3×3 standard convolution operation, is the merge function, Represents the third dimension;
[0039] After a 1×1 standard convolution, the new weights are generated by the Sigmoid function; the new weights and Separation operations are performed along the H dimension to obtain the local feature weights in the horizontal direction , local feature map in the horizontal direction , local feature weight in the vertical direction , local feature map in the vertical direction ;
[0040] ;
[0041] ;
[0042] ;
[0043] ;
[0044] in, For separation operation, is the Sigmoid function;
[0045] Multiply the local feature map and the local feature weight accordingly, perform independent separation and adjustment of the features in different directions in the spatial dimension, and generate the attention weight through the sigmoid function ;
[0046] ;
[0047] in, Represents element-wise multiplication operation.
[0048] In the above-mentioned method for detecting defects in vials based on the improved YOLOv8, in step S2, the information aggregation process is as follows:
[0049] For the generated and ,Will and 、 Multiply them sequentially and perform spatial weighting to obtain enhanced feature maps As follows:
[0050] ;
[0051] Among them, the enhanced feature map ;
[0052] Similarly, processing Each group of features in the group, get After grouping the enhanced features, perform information aggregation and restore the size to , and get the output ;
[0053] ;
[0054] in, is the batch normalization function; is a merging function used to merge g groups of features in the channel dimension.
[0055] In the above-mentioned method for detecting vial defects based on the improved YOLOv8, in step S2, the feature enhancement module MMFF uses convolution kernels of different sizes to extract features from defects and stacks them to improve the feature fusion effect, as follows:
[0056] The MMFF input is , the output is , MMFF captures spatial feature information and performs feature fusion through two parallel branches. The first branch contains a convolution layer Conv1×1-batch normalization layer BN-activation function layer SiLU, and the convolution layer Conv1×1-batch normalization layer BN-activation function layer SiLU is recorded as CBS. All channels at the same position are weighted summed to fuse channel features. The first branch input Generate Output The second branch contains a multi-scale depth-separable convolution module MDSConv and N re-parameterized convolution modules RepConv-Block, which use convolution kernels of different sizes to capture defect features, help the network focus on local details and context information, and stack multiple RepConv-Blocks to enhance the representation ability of features. The second branch input Generate Output ; and After completing the element-by-element addition operation, the final output is obtained through additional CBS , the number of channels does not change.
[0057] In the above-mentioned method for detecting defects in vials based on the improved YOLOv8, in step S2, RepConv-Block uses a multi-branch convolutional layer in the training phase, then merges multiple computing modules into one in the inference phase, and reparameterizes the branch parameters to the main branch; RepConv-Block extracts and enhances target features by repeatedly applying convolutional layers and activation functions; MDSConv includes 3×3, 5×5, and 7×7 depth-wise convolutions and 1×1 point-wise convolutions. The input feature map undergoes parallel multi-scale depth-wise convolutions, and group convolutions are performed in the channel dimension to extract spatial features of different sizes; after element-by-element addition operations are performed on multiple branches, feature aggregation is achieved by point-by-point convolution.
[0058] In the above-mentioned improved YOLOv8-based method for detecting defects in vials, in step S3, the training set images are input into the defect target detection model. After backbone network feature extraction and neck feature fusion, the detection head outputs the predicted box of the defect target.
[0059] The error function between the target prediction result and the true label By bounding box regression loss , target category loss and confidence loss The weighted composition is as follows:
[0060] ;
[0061] in, is the weighted coefficient of the bounding box regression loss, is the weighted coefficient of target category loss, is the weighted coefficient of confidence loss;
[0062] Bounding box regression loss It is used to measure the position and shape of the predicted box and the real box, as follows:
[0063] ;
[0064] ;
[0065] ;
[0066] in: represents the intersection-and-union ratio, is the prediction box, is the real frame, is the center point of the prediction box, is the center point of the ground truth box; yes and The Euclidean distance of is the diagonal length of the smallest rectangle that encloses the predicted box and the real box; is the difference in aspect ratio between the predicted box and the real box; is the adjustment factor; target category loss function As follows:
[0067] ;
[0068] in, is the total number of categories; is the category index; is the true category label; is the predicted class probability;
[0069] Confidence loss As follows:
[0070] ;
[0071] in, is the true confidence, is the confidence value of the network prediction;
[0072] optimization , the model parameters are iteratively updated so that the model parameters converge to the minimum value and the model performance reaches the optimal value;
[0073] After the training is completed, the weight with the highest accuracy is selected to obtain the optimal model for vial defect detection.
[0074] In the above-mentioned method for detecting defects in vials based on the improved YOLOv8, in step S4, the performance of the defect target detection model for detecting defects in vials is evaluated by using evaluation indicators. The evaluation indicators include 、 , precision , recall rate , Represents the average precision of a single category; All categories The average value of ; calculated as follows:
[0075] ;
[0076] ;
[0077] ;
[0078] ;
[0079] in, represents the number of correctly detected targets, which are true positives; Indicates the number of targets that were misdetected, which are false positives; represents the number of true targets that were not detected, which are false negatives; Indicates accuracy , Recall ; Indicates that the recall rate is , The recall rate is The accuracy of is the total number of categories; is the category index, Indicates the categories value.
[0080] The beneficial effects of the present invention are as follows: the present invention can solve the problem of difficulty and low accuracy in detecting defects on the body of a vial, which is specifically manifested as follows:
[0081] 1. Introducing the Spatial Separable Pooling Attention Module (SSPA) to enhance the network’s ability to perceive subtle features such as scratches on bottles;
[0082] 2. The feature enhancement module MMFF is introduced, and convolution kernels of different sizes are used to extract defect features of different sizes such as dirt, scratches, and abnormal loading under complex backgrounds, and the feature fusion effect is improved by module stacking. BRIEF DESCRIPTION OF THE DRAWINGS
[0083] Figure 1 It is the overall flow chart of the present invention.
[0084] Figure 2 This is the overall framework diagram of the defect target detection model of the present invention.
[0085] Figure 3 These are sample images of defects in the vial of the present invention, where the left, middle, and right images are sample images of defects such as stain, scratch, and load anomaly, respectively.
[0086] Figure 4 This is a working principle diagram of the spatially separable pooling attention module SSPA of the present invention.
[0087] Figure 5 It is a structural diagram of the feature enhancement module MMFF of the present invention.
[0088] Figure 6 Schematic diagram of the structure of RepConv-Block of the present invention.
[0089] Figure 7 Schematic diagram of the structure of MDSConv of the present invention. DETAILED DESCRIPTION
[0090] The present invention will be further described below with reference to the accompanying drawings and embodiments.
[0091] like Figure 1 As shown, a method for detecting vial defects based on improved YOLOv8 includes the following steps:
[0092] S1: Obtain and preprocess vial defect data to obtain training and test sets.
[0093] The specific process of step S1 is:
[0094] S11: Vials enter the image acquisition area via a conveyor belt. As the vials are inspected at a constant speed, the sensor is triggered. The industrial camera samples the vials from multiple angles, capturing 1,241 original images at a resolution of 2,448 × 2,048 pixels.
[0095] S12: Perform target area positioning, image segmentation, defect annotation, and data enhancement on the collected image samples to complete image preprocessing, and divide the training set and test set into proportions.
[0096] The specific process of target area positioning, image segmentation, and data enhancement is as follows:
[0097] Target area positioning: Binarize the original image to show the bottle outline against a white background. Calculate the center of mass of the white background area and locate the bottle wall and bottom along the horizontal and vertical grayscale changes. Draw a rectangular frame symmetrical about the center of mass to locate the target area.
[0098] Image segmentation: In actual production lines, the position of vials may be slightly offset. Therefore, the rectangular frame is fine-tuned and scaled, the image is segmented, and the background area is removed to obtain the bottle area to be inspected. This facilitates the detection of possible dirt, scratches, and abnormal filling volume. Batch processing is then performed to obtain the samples to be inspected.
[0099] Data augmentation: We performed data augmentation on the processed sample images by applying shear transformation (±10° horizontally, ±10° vertically), adding noise (1.05%), and hue transformation (between -15° and +15°) to expand the data volume to 3723 images.
[0100] like Figure 3 As shown in the figure, there are three types of defects on the vial body: stain, scratch, and load anomaly.
[0101] S2: Establish an improved YOLOv8 defect target detection model, such as Figure 2 As shown;
[0102] Using YOLOv8 as the basic framework, the spatially separable pooling attention module (SSPA) is introduced in the shallow layer of the backbone network to enhance the network's perception of subtle features such as scratches on the bottle body. The feature enhancement module (MMFF) is introduced in the neck to replace the C2f module. Convolution kernels of different sizes are used to extract features of defects such as dirt, scratches, and abnormal loading, and the features are stacked to improve the feature fusion effect. The detection head outputs defect targets.
[0103] Backbone consists of the Conv-BN-SiLU (Convolutional Layer-Batch Normalization Layer-SiLU Activation Function) module, the C2f (Feature Enhancement) module, the Spatial Separable Pooling Attention (SSPA) module, and the Spatial Pyramid Pooling (SPPF) module. Backbone receives the original image and alternately stacks the CBS and C2f modules to gradually extract high-level semantic features. SSPA performs feature enhancement, and finally the SPPF module aggregates multi-scale contextual information in the deep network to generate a multi-scale feature map.
[0104] The CBS module performs preliminary feature extraction through a 3×3 standard convolution block, and then passes through a batch normalization layer BN and SiLU activation function to accelerate training convergence and improve computational efficiency.
[0105] The C2f module is divided into three parts: segmentation, processing, and splicing, preserving shallow details and deep semantics. The C2f module receives the feature map output from the CBS module as input and evenly splits the input into two parts along the channel dimension. The two parts are connected using a residual connection. The first part is directly passed, while the second part is processed by a stacked feature fusion bottleneck structure called Bottleneck. The two processed feature maps are spliced along the channel dimension and then adjusted for the number of channels using a 1×1 convolution module to obtain the final output feature map. The Bottleneck structure consists of a 1×1 convolution module, a 3×3 convolution module, and a residual connection. The 1×1 convolution module compresses the number of feature map channels, reducing the amount of computation; the 3×3 convolution module extracts spatial features; and the residual connection adds the input features to the extracted features to prevent gradient vanishing.
[0106] like Figure 4 As shown, in step S2, the spatially separable pooled attention module SSPA includes two parts: attention weight calculation and information aggregation. The attention weight calculation includes three parts: local feature extraction, global feature generation, and spatial separation feature. The information aggregation includes merging the information between different feature groups after adjusting the attention weights of multiple groups of features.
[0107] If the input features are given , , represents the field of real numbers, Represent the three dimensions of channel number, height and width respectively. Divide Group, represented by ,in express The Group features, , / / represents integer division operation; each set of features is extracted and fused through three-part calculation. In this process, the channel dimension does not change but the amount of calculation is reduced. Finally, each set of features is aggregated to enhance the network's ability to capture various defect features such as dirt, scratches, and abnormal loading.
[0108] The process of attention weight calculation includes:
[0109] Local feature extraction: For each set of input features, deploy them horizontally 、 、 The pooling window of size is used for global average pooling and deployed in the vertical direction. 、 、 The pooling windows of different sizes are used for global average pooling; the deployment of pooling windows in a single direction embeds the spatial position information into the channel dimension; pooling windows of different sizes balance the short-distance dependencies when establishing long-distance dependencies in discrete areas, and realize the capture of multi-scale features under different distance dependencies, and obtain two local feature maps. as follows:
[0110] ;
[0111] ;
[0112] in, 、 Respectively represent the splicing operations along the horizontal and vertical directions, Represent the horizontal and vertical coordinates of the pixel respectively, is the size of the global average pooling window, , ;
[0113] Global feature generation: using 1×1 standard convolution processing and , and obtain feature maps respectively and feature maps , and Perform matrix multiplication operations between them to generate global feature maps :
[0114] ;
[0115] ;
[0116] ;
[0117] in, represents a 1×1 convolution operation, Represents matrix multiplication operation; and Two feature maps, each pixel represents the local refined feature in the current direction. The two perform matrix multiplication operations to interact cross-dimensional information between local features of different spatial dimensions and better establish the correlation of global feature information.
[0118] Global feature map Apply 1×1 standard convolution for information fusion and pass the sigmoid function to obtain the global feature attention weight , global feature attention weight Expressed as:
[0119] ;
[0120] Spatial separation features: and Upsample to the original feature map size Get the feature maps respectively and feature maps , and conduct Dimensions and Transposition between dimensions;
[0121] ;
[0122] ;
[0123] in, represents the transpose operation, Represents an upsampling operation;
[0124] and exist Splicing in the dimension direction The feature map is then extracted through a 3×3 standard convolution to obtain a feature enhancement map. , realizing the reorganization of local features across spatial dimensions, so that the feature information in the horizontal and vertical directions can be transmitted complementary;
[0125] ;
[0126] in, represents a 3×3 standard convolution operation, is the merge function, Represents the third dimension;
[0127] After a 1×1 standard convolution, the sigmoid function generates new weights; the new weights and Separation operations are performed along the H dimension to obtain the local feature weights in the horizontal direction , local feature map in the horizontal direction , local feature weight in the vertical direction , local feature map in the vertical direction ;
[0128] ;
[0129] ;
[0130] ;
[0131] ;
[0132] in, For separation operation, is the Sigmoid function;
[0133] Multiply the local feature map and the local feature weight accordingly, perform independent separation and adjustment of the features in different directions in the spatial dimension, and generate the attention weight through the sigmoid function ;
[0134] ;
[0135] in, Represents element-wise multiplication operation.
[0136] The process of information aggregation is:
[0137] For the generated and ,Will and 、 Multiply them sequentially and perform spatial weighting to obtain enhanced feature maps As follows:
[0138] ;
[0139] Among them, the enhanced feature map ;
[0140] Similarly, processing Each group of features in the group, get After grouping the enhanced features, perform information aggregation and restore the size to , and get the output ;
[0141] ;
[0142] in, is the batch normalization function; is a merging function used to merge g groups of features in the channel dimension.
[0143] The SPPF module, located at the end of the backbone network, is primarily responsible for multi-scale feature fusion. It first compresses the number of channels in the input feature map using 1×1 convolutions. It then performs maximum pooling using three cascaded 5×5 pooling windows. The original feature map and the three pooling results are concatenated along the channel dimension, and the number of channels is restored using 1×1 convolutions.
[0144] Neck adopts FPN-PAN bidirectional structure, including upsampling module , 3×3 convolution module, C2f module, Concat splicing module and feature enhancement module MMFF. Neck receives the output of the backbone network as input. First, it passes through the upsampling module in the feature pyramid network FPN The feature map size is enlarged, and the Concat splicing module is used to perform feature splicing and the MMFF is used to enhance the features. Secondly, the feature map size is reduced by the 3×3 convolution module of the path aggregation network PAN, and the Concat splicing module is used to perform feature splicing and the C2f module is used to fuse features of different scales to enhance semantic and spatial information.
[0145] FPN structure: A top-down transfer path. The deep feature map is upsampled and transferred to the shallow feature map, the feature map is concatenated in the channel dimension, and the feature is enhanced by MMFF.
[0146] The feature enhancement module MMFF uses convolution kernels of different sizes to extract defect features and stack them to improve the feature fusion effect. The details are as follows:
[0147] like Figure 5 As shown, the MMFF input is , the output is , MMFF captures spatial feature information and performs feature fusion through two parallel branches. The first branch contains a convolution layer Conv1×1-batch normalization layer BN-activation function layer SiLU, and the convolution layer Conv1×1-batch normalization layer BN-activation function layer SiLU is recorded as CBS. All channels at the same position are weighted summed to fuse channel features. The first branch input Generate Output The second branch consists of a multi-scale depth-separable convolution module MDSConv and N re-parameterized convolution modules RepConv-Block, which use convolution kernels of different sizes to capture defect features, helping the network focus on local details and a wider range of contextual information. At the same time, stacking multiple RepConv-Blocks enhances the representation ability of features and extracts more complex and high-level features. The second branch input Generate Output ; and After completing the element-by-element addition operation, the final output is obtained through additional CBS , the number of channels does not change. CBS consists of a 1×1 standard convolution, BN, and SiLU activation function.
[0148] like Figure 6 As shown in the figure, RepConv-Block uses multi-branch convolutional layers during training. During inference, it merges multiple computational modules into one and reparameterizes the branch parameters onto the main branch, reducing computational effort and memory consumption while improving inference speed. RepConv-Block extracts and enhances target features by repeatedly applying convolutional layers and activation functions.
[0149] like Figure 7 As shown in the figure, MDSConv includes 3×3, 5×5, 7×7 depth-wise convolutions and 1×1 point-wise convolutions. The input feature map undergoes parallel multi-scale depth-wise convolutions, and group convolutions are performed in the channel dimension to extract spatial features of different sizes, reducing the amount of computation. The residual connection helps the information flow of the deep network, avoids gradient disappearance, and enhances the expressiveness of features. After the multi-branch element-by-element addition operation, feature aggregation is achieved by point-by-point convolution.
[0150] PAN architecture: A bottom-up transfer path. Shallow feature maps are downsampled using a 3×3 convolutional module with stride 2. This information is then transferred to deeper feature maps, which are then concatenated in the channel dimension. The C2f module then performs feature fusion. Neck utilizes a bidirectional path structure, combining feature maps from different levels. This avoids the problem of missing spatial details in deep features and insufficient semantic information in shallow features, enhancing the effectiveness of multi-scale feature fusion.
[0151] Head: Contains a decoupling head and an Anchor-Free mechanism. The decoupling head separates the classification task and the regression task into two independent branches to avoid interference between tasks and improve classification accuracy and the stability of regression positioning. It adopts the Anchor-Free mechanism and consists of three detection layers. Feature maps of different scales are used to detect target objects of different sizes. Each detection layer outputs a corresponding vector through the three branches of classification, confidence, and regression, and filters it through non-maximum suppression (NMS). Finally, it generates the predicted bounding box and category of the target in the original image and marks it. The head is mainly responsible for converting the multi-scale feature map output by Neck into the target detection result. The head outputs a vector that contains the target category probability, target confidence, and bounding box information.
[0152] S3: Use the training set to train the defect target detection model, optimize the loss function, and update the model weight parameters until the loss function converges.
[0153] In this paper, because the vial defect dataset was custom constructed and lacked pre-trained weights, the network was trained from scratch. A stochastic gradient descent (SGD) optimizer was used for 100 epochs, with an initial learning rate of 0.01, a momentum of 0.937, and a weight decay of 0.0005. Training was performed using a warm-up method with a warm-up period of three epochs, an input image size of 640×640, and a batch size of 16.
[0154] In step S3, the training set image is input into the defect target detection model. After backbone network feature extraction and neck feature fusion, the detection head outputs the prediction box of the defect target.
[0155] The error function between the target prediction result and the true label By bounding box regression loss , target category loss and confidence loss The weighted composition is as follows:
[0156] ;
[0157] in, is the weighted coefficient of the bounding box regression loss, is the weighted coefficient of target category loss, is the weighted coefficient of confidence loss;
[0158] Bounding box regression loss It is used to measure the position and shape of the predicted box and the real box, as follows:
[0159] ;
[0160] ;
[0161] ;
[0162] in: represents the intersection-and-union ratio, is the prediction box, is the real frame, is the center point of the prediction box, is the center point of the ground truth box; yes and The Euclidean distance of is the diagonal length of the smallest rectangle that encloses the predicted box and the real box; is the difference in aspect ratio between the predicted box and the real box; is the adjustment factor;
[0163] Target category loss function As follows:
[0164] ;
[0165] in, is the total number of categories; is the category index; is the true category label; is the predicted class probability;
[0166] Confidence loss As follows:
[0167] ;
[0168] in, is the true confidence, is the confidence value of the network prediction;
[0169] optimization , the model parameters are iteratively updated so that the model parameters converge to the minimum value and the model performance reaches the optimal value;
[0170] After 100 rounds of training, the weight with the highest accuracy is selected to obtain the optimal model for vial defect detection.
[0171] S4: Use the test set to test the defect target detection model.
[0172] The performance of the defect detection model for detecting defects in vials is evaluated through evaluation indicators. The evaluation indicators include 、 , precision , recall rate , Represents the average precision of a single category; All categories The average value of ; calculated as follows:
[0173] ;
[0174] ;
[0175] ;
[0176] ;
[0177] in, represents the number of correctly detected targets, which are true positives; Indicates the number of targets that were misdetected, which are false positives; represents the number of true targets that were not detected, which are false negatives; Indicates accuracy , Recall ; Indicates that the recall rate is , The recall rate is The accuracy of is the total number of categories; is the category index, Indicates the categories value.
[0178] The present invention is trained and tested, and the existing advanced target detection model is compared with the present invention. and overall @50As shown in Table 1, Stain, Scratch, and Load represent the defect categories of dirt, scratches, and abnormal loading, respectively; @50 is A specific form of The predicted box is considered a correct detection.
[0179]
[0180] Among them, Size represents the input image size.
[0181] As can be seen from Table 1, the detection accuracy of the present invention for various types of defects on the vial body is improved compared with the basic network, and the overall detection effect is also better than that of the existing advanced object detection network.
Claims
1. A method for detecting vial defects based on improved YOLOv8, characterized in that: The following steps are involved: S1: Obtain and preprocess vial defect data to obtain training and test sets; S2: Establish an improved YOLOv8 defect target detection model; Using YOLOv8 as the basic framework, the spatially separable pooling attention module (SSPA) is introduced in the shallow layer of the backbone network. The feature enhancement module (MMFF) is introduced in the neck to replace the C2f module. Convolution kernels of different sizes are used to extract defect features and stack them to improve feature fusion. The detection head outputs defect targets. In step S2, the spatially separable pooled attention module SSPA includes two parts: attention weight calculation and information aggregation. The attention weight calculation includes three parts: local feature extraction, global feature generation, and spatial separation feature. Information aggregation includes merging information between different feature groups after adjusting the attention weights of multiple feature groups. If the input features are given , , represents the field of real numbers, Represent the three dimensions of channel number, height and width respectively. Divide Group, represented by ,in express The Group features, , / / represents integer division operation; Each set of features is extracted and fused through three parts of calculation, and finally the features of each set are aggregated; S3: Use the training set to train the defect target detection model, optimize the loss function, and update the model weight parameters until the loss function converges; S4: Use the test set to test the defect target detection model.
2. The method for detecting vial defects based on improved YOLOv8 according to claim 1, characterized in that: The specific process of step S1 is: S11: The vials enter the image acquisition area via a conveyor belt. The sensor is triggered as the vials are inspected at a constant speed. The industrial camera samples images of the vials from multiple angles. S12: Perform target area positioning, image segmentation, defect annotation, and data enhancement on the collected image samples to complete image preprocessing, and divide the training set and test set into proportions.
3. The method for detecting vial defects based on improved YOLOv8 according to claim 2, characterized in that: In step S12, the specific process of target area positioning, image segmentation, and data enhancement is as follows: Target area positioning: Binarize the original image to show the bottle outline against a white background. Calculate the center of mass of the white background area and locate the bottle wall and bottom along the horizontal and vertical grayscale changes. Draw a rectangular frame symmetrical about the center of mass to locate the target area. Image segmentation: Fine-tune and scale the rectangular frame, segment the image, and remove the background area to obtain the bottle area to be inspected, making it easier to detect defects within the area; batch processing is performed to obtain samples to be inspected; Data enhancement: For the processed samples to be tested, data enhancement operations are performed to expand the data volume.
4. The method for detecting vial defects based on improved YOLOv8 according to claim 3, characterized in that: In step S2, the process of calculating the attention weight includes: Local feature extraction: For each set of input features, deploy them horizontally 、 、 The pooling window of size is used for global average pooling and deployed in the vertical direction. 、 、 The pooling windows of different sizes are used for global average pooling; the deployment of pooling windows in a single direction embeds the spatial position information into the channel dimension; pooling windows of different sizes balance the short-distance dependencies when establishing long-distance dependencies in discrete areas, and realize the capture of multi-scale features under different distance dependencies, and obtain two local feature maps. as follows: ; ; in, 、 Respectively represent the splicing operations along the horizontal and vertical directions, Represent the horizontal and vertical coordinates of the pixel respectively, is the size of the global average pooling window, , ; Global feature generation: using 1×1 standard convolution processing and , and obtain feature maps respectively and feature maps , and Perform matrix multiplication operations between them to generate global feature maps : ; ; ; in, represents a 1×1 convolution operation, Represents matrix multiplication operation; Global feature map Apply 1×1 standard convolution for information fusion and pass the Sigmoid function to obtain the global feature attention weight : Spatial separation features: and Upsample to the original feature map size Get the feature maps respectively and feature maps , and conduct Dimensions and Transposition between dimensions; ; ; in, represents the transpose operation, Represents an upsampling operation; and exist Splicing in the dimension direction The feature map is then extracted through a 3×3 standard convolution to obtain a feature enhancement map. , realizing the reorganization of local features across spatial dimensions, so that the feature information in the horizontal and vertical directions can be transmitted complementary; ; in, represents a 3×3 standard convolution operation, is the merge function, Represents the third dimension; After a 1×1 standard convolution, the new weights are generated by the Sigmoid function; the new weights and Separation operations are performed along the H dimension to obtain the local feature weights in the horizontal direction , local feature map in the horizontal direction , local feature weight in the vertical direction , local feature map in the vertical direction ; ; ; ; ; in, For separation operation, is the Sigmoid function; Multiply the local feature map and the local feature weight accordingly, perform independent separation and adjustment of the features in different directions in the spatial dimension, and generate the attention weight through the sigmoid function ; ; in, Represents element-wise multiplication operation.
5. The method for detecting vial defects based on improved YOLOv8 according to claim 4, characterized in that: In step S2, the process of information aggregation is as follows: For the generated and ,Will and 、 Multiply them sequentially and perform spatial weighting to obtain enhanced feature maps As follows: ; Among them, the enhanced feature map ; Similarly, processing Each group of features in the group, get After grouping the enhanced features, perform information aggregation and restore the size to , and get the output ; ; in, is the batch normalization function; is a merging function used to merge g groups of features in the channel dimension.
6. The method for detecting vial defects based on improved YOLOv8 according to claim 5, characterized in that: In step S2, the feature enhancement module MMFF uses convolution kernels of different sizes to extract features from defects and stack them to improve the feature fusion effect, as follows: The MMFF input is , the output is , MMFF captures spatial feature information and performs feature fusion through two parallel branches. The first branch contains a convolution layer Conv1×1-batch normalization layer BN-activation function layer SiLU, and the convolution layer Conv1×1-batch normalization layer BN-activation function layer SiLU is recorded as CBS. All channels at the same position are weighted summed to fuse channel features. The first branch input Generate Output The second branch contains a multi-scale depth-separable convolution module MDSConv and N re-parameterized convolution modules RepConv-Block, which use convolution kernels of different sizes to capture defect features, help the network focus on local details and context information, and stack multiple RepConv-Blocks to enhance the representation ability of features. The second branch input Generate Output ; and After completing the element-by-element addition operation, the final output is obtained through additional CBS , the number of channels does not change.
7. The method for detecting vial defects based on improved YOLOv8 according to claim 6, characterized in that: In step S2, RepConv-Block uses a multi-branch convolution layer in the training phase, and then merges multiple computing modules into one in the inference phase, and reparameterizes the branch parameters to the main branch; RepConv-Block extracts and enhances target features by repeatedly applying convolution layers and activation functions; MDSConv includes 3×3, 5×5, 7×7 depth-wise convolutions and 1×1 point-wise convolutions. The input feature map undergoes parallel multi-scale depth-wise convolutions, and group convolutions are performed in the channel dimension to extract spatial features of different sizes; after the multiple branches undergo element-by-element addition operations, feature aggregation is achieved by point-by-point convolution.
8. The method for detecting vial defects based on improved YOLOv8 according to claim 6, characterized in that: In step S3, the training set image is input into the defect target detection model. After backbone network feature extraction and neck feature fusion, the detection head outputs the prediction box of the defect target. The error function between the target prediction result and the true label By bounding box regression loss , target category loss and confidence loss The weighted composition is as follows: ; in, is the weighted coefficient of the bounding box regression loss, is the weighted coefficient of target category loss, is the weighted coefficient of confidence loss; Bounding box regression loss It is used to measure the position and shape of the predicted box and the real box, as follows: ; ; ; in: represents the intersection-and-union ratio, is the prediction box, is the real frame, is the center point of the prediction box, is the center point of the ground truth box; yes and The Euclidean distance of is the diagonal length of the smallest rectangle that encloses the predicted box and the real box; is the difference in aspect ratio between the predicted box and the real box; is the adjustment factor; Target category loss function As follows: ; in, is the total number of categories; is the category index; is the true category label; is the predicted class probability; Confidence loss As follows: ; in, is the true confidence, is the confidence value of the network prediction; optimization , the model parameters are iteratively updated so that the model parameters converge to the minimum value and the model performance reaches the optimal value; After the training is completed, the weight with the highest accuracy is selected to obtain the optimal model for vial defect detection.
9. The method for detecting vial defects based on improved YOLOv8 according to claim 1, characterized in that: In step S4, the performance of the defect target detection model for detecting defects in the vial is evaluated by using evaluation indicators. The evaluation indicators include: 、 , precision , recall rate , Represents the average precision of a single category; All categories The average value of ; calculated as follows: ; ; ; ; in, represents the number of correctly detected targets, which are true positives; Indicates the number of targets that were misdetected, which are false positives; represents the number of true targets that were not detected, which are false negatives; Indicates accuracy , Recall ; Indicates that the recall rate is , The recall rate is The accuracy of is the total number of categories; is the category index, Indicates the categories value.
Citation Information
Patent Citations
High body seriola quinqueradiata detection method based on YOLOv8 network structure
CN118279935A