A glass bottle mouth detection method based on hierarchical weighted feature fusion network
By improving the YOLOv7 network model, introducing a CA attention mechanism and a hierarchical weighted feature fusion network, the problems of strong model specificity and low accuracy of small defect recognition in glass bottle defect detection are solved, and higher detection accuracy and robustness are achieved.
Patent Information
- Application Number
- CN202410674679.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-28
- Publication Date
- 2025-04-22
- Estimated Expiration
- 2044-05-28
AI Technical Summary
The existing glass bottle defect detection technology has problems such as excessive model specificity, low accuracy in identification of small defects, and easy to miss detection and miss detection. Especially when there are a large number of negative samples, the model will lose features and reduce the accuracy of detection.
The glass bottle defect detection method based on a hierarchical weighted feature fusion network is adopted, the YOLOv7 network model is improved, the CA attention mechanism and the S-ELAN module of the efficient layer aggregation network are introduced, and the feature fusion network in the Neck part is replaced as a hierarchically weighted feature fusion network HWFF, and the model is optimized using EIoU Loss and Focal Loss.
It improves the accuracy of glass bottle mouth defect detection, enhances the ability to identify small defects, reduces missed and missed detection, and improves the detection speed and robustness of the model. Through experiments, mAP@0.5 reached 85.8%, an increase of 4.6 percentage points compared with the original algorithm.
Smart Images

Figure CN118710580B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of target detection, and in particular relates to a glass bottle defect detection method based on a hierarchical weighted feature fusion network. Background Art
[0002] In the field of defect detection, manual visual inspection originated the earliest and is widely used in small and medium-sized enterprises. According to statistics, more than 80% of industrial surface defects still rely on manual inspection. However, with the emergence of problems such as labor difficulties, there are many problems with manual inspection, and the biggest problems are high labor intensity, poor stability, and low production efficiency. Due to the complexity of different types of defects and the large differences in defects of the same type, the appearance of glass bottles is complex and diverse, and many detection algorithms are not adaptable. It is necessary to develop detection algorithms separately for defects, and the development complexity is extremely high. In addition, it is difficult to separate defects from backgrounds in production scenarios, which affects the accuracy of the algorithm.
[0003] Traditional mathematical methods include histogram equalization, filtering, secondary filtering, grayscale binarization and other methods to pre-process the obtained images. Then, mathematical morphology, Fourier transform, Gabor transform, wavelet transform and other methods are used to detect and mark the defective parts of the image. This method has high accuracy and does not require a high number of samples. It solves the problems of slow speed and unstable accuracy of traditional manual detection, but the problem is that the model is too specific, and the entire model may need to be readjusted every time a new material or new part is changed. The robustness is also poor, and when the production environment changes, the accuracy changes greatly.
[0004] With the development of technology, direct contact between operators and glass bottles and related equipment has been completely avoided. This advantage has made deep learning algorithm detection widely recognized in the detection industry, and it is more adaptable than mathematical methods. However, the recognition network used in the current glass bottle defect recognition technology has a low recognition accuracy for small defects, resulting in missed detection and false detection. In the case of a large number of negative samples, the model will lose features, reducing the accuracy of detection.
[0005] The patent with publication number CN115330754B, entitled Glass Bottle Mouth Defect Detection Method, Device, Equipment and Storage Medium, describes a method of constructing a detection model based on a multi-scale multi-attention convolutional neural network MSMA-CNN module; detecting the glass bottle mouth defect image of the glass bottle to be inspected through the detection model to obtain a defect image detection result; determining the glass bottle mouth defect position based on the defect image detection result, and being able to perform glass bottle mouth defect detection based on a multi-scale multi-attention convolutional neural network, but the detection accuracy of small defects is low.
[0006] The paper "Willis, Yu Zehui. Glass Bottle Mouth Defect Detection Based on YOLOv5 [J]. Yangtze River Information Communication, 2023, 36(01): 9-11." proposes a method for directly using the YOLOv5 algorithm to detect defects in glass bottle mouths. This method simultaneously photographs the glass bottle mouth in three directions. If a defect appears in one picture, it is determined that the bottle mouth has a defect. This improves the accuracy of detection. However, when the number of negative samples is large, the model will lose defect features, resulting in false detection.
[0007] The above defect detection method has the following problems:
[0008] Mathematical method: The model is too specific. When a new material is used, the entire model must be readjusted. When the environment changes, the accuracy changes greatly.
[0009] Deep learning technology: The current glass bottle defect detection technology has a low accuracy rate, and there are a large number of small defects, which are prone to under-detection and false detection. The detection speed is slow. When a large number of negative samples appear, the accuracy of model detection is seriously affected. Summary of the invention
[0010] Purpose of the invention: In response to the problems pointed out in the background technology, the present invention discloses a glass bottle defect detection method based on a hierarchical weighted feature fusion network, which improves the accuracy of deep learning technology in detecting glass bottle mouth defects and speeds up the detection speed.
[0011] Technical solution: The present invention discloses a glass bottle defect detection method based on a hierarchical weighted feature fusion network, comprising the following steps:
[0012] Step 1: Obtain glass bottle mouth image data with different defects, annotate the data, preprocess the acquired glass bottle mouth defect image data, and divide the image data to obtain a training set and a test set;
[0013] Step 2: Build the original YOLOv7 network model, improve the network structure of YOLOv7, introduce the CA attention mechanism in the backbone feature extraction network, build an efficient layer aggregation network S-ELAN module to replace the original ELAN; replace the feature fusion network of the Neck part with the hierarchical weighted feature fusion network HWFF, perform feature fusion on the shallow feature map obtained by the backbone network, and select EIoU as the loss function for model optimization;
[0014] Step 3: Input the training set into the improved YOLOv7 model for training, thereby obtaining a glass bottle mouth defect detection model;
[0015] Step 4: Input the validation set into the glass bottle mouth defect detection model trained in step 1, evaluate the model based on the test results, and finally use the trained glass bottle mouth defect detection model to perform defect detection.
[0016] Furthermore, the pretreatment process in step 1 is as follows:
[0017] Step 1.1: Obtain open-source glass bottle mouth defect image data, including four typical defects: gap Ga, contamination Po, crack Cr, and bubble Bu, and use bilateral filtering to denoise the image;
[0018] Step 1.2: Use Labelimg to select YOLO format to annotate the image data, output the results to the corresponding file, and divide the data set into a training set and a validation set in a ratio of 8:2.
[0019] Furthermore, in step 2, the efficient layer aggregation network S-ELAN module in the improved YOLOv7 backbone network replaces the original ELAN, as follows:
[0020] Based on ELAN, the residual link of ResNet and the SE module of SENet are introduced, the input is directly added to the output, the short-circuit connection is added, and the FC layer in the middle of the SE module is removed.
[0021] Furthermore, in step 2, the CA attention mechanism is embedded into the backbone network of YOLOv7, and the position information of the actual bottle mouth defect is embedded into the attention channel, so that the generated feature information has the coordinate information corresponding to the defect, as follows:
[0022] CoordAttention decomposes channel attention into two 1D feature encoding processes, so that features can be aggregated in different spatial directions. It is divided into two steps: coordinate information embedding and coordinate attention generation;
[0023] First, given the input feature X, each channel is encoded using a pooling kernel of size (H, 1) or (1, W). This encoding step is performed in the horizontal and vertical directions respectively. Through this encoding process, the output of two channels is obtained. These two transformations enable the attention module to obtain long-term dependencies along one spatial direction and preserve precise position information along another spatial direction.
[0024] The output of the cth channel with height h is expressed as:
[0025]
[0026] The output of the cth channel with width w is expressed as:
[0027]
[0028] Then the above transformation is concatenated, and then transformed using the 1×1 convolution transformation function F1, as shown below:
[0029] f=δ(F1([z h ,z w ]))
[0030] δ is a nonlinear activation function, and f is an intermediate feature map that encodes spatial information in the horizontal and vertical directions;
[0031] Split f into two separate tensors f along the spatial dimension h and f w , using two 1×1 convolution transformations F h and F w f h and f w Transformed to g with the same number of channels h and g w Input to X, the display is as follows:
[0032] g h =σ(F h (f h ))
[0033] g w =σ(F w (f w ))
[0034] σ is the sigmoid activation function;
[0035] The output formula of Coordinate Attention Block is:
[0036]
[0037] Furthermore, in step 2, the feature fusion network of the Neck part is replaced by a hierarchical weighted feature fusion network HWFF, specifically:
[0038] Use the path aggregation feature fusion network PANet in the Neck module of YOLOv7, and input an autonomous learning weight W to all features in the feature fusion stage:
[0039]
[0040] Among them, w is the self-learning weight, and ε is a very small number to ensure that the weight range is 0-1.
[0041] Furthermore, in step 2, the CIoU Loss in yolov7 is replaced by EIoU Loss, including overlap loss, center distance loss, width and height loss. The loss function of EIoU to penalize the prediction results of width and height is:
[0042]
[0043] Among them, L IoU is the IoU loss, L dis is the distance loss, L asp is the aspect ratio loss, IoU is the intersection over union ratio, ρ 2 (b,b gt ) / c 2 is the Euclidean distance between the ground truth center and the anchor box center, is the Euclidean distance between the ground truth width and the anchor box width, is the Euclidean distance between the ground truth height and the anchor box height, c W and c h is the width and height of the minimum bounding box covering the two bboxes;
[0044] Furthermore, Focal Loss is introduced on the basis of EIoU to optimize the sample imbalance problem in the bounding box regression task, that is, to reduce the contribution of the prediction box with a small overlapping area with the target box to the bbox optimization, and to separate the high-quality anchor box from the low-quality anchor box. The Focal-EIoU loss formula is:
[0045] L Focal-EIOU =IOU γ L EIOU
[0046] Among them, γ is a hyperparameter used to control the curvature of the curve.
[0047] Furthermore, in step 4, the evaluation indicators used for training and verification include: mAP mean average precision, which is expressed as:
[0048] Average precision value:
[0049]
[0050] in,
[0051]
[0052] In the formula, P represents precision, R represents recall, TP represents the number of positive predictions that are actually positive, FP represents the number of positive predictions that are actually negative, FN represents the number of negative predictions that are actually positive, and AP represents the average precision.
[0053] Beneficial effects:
[0054] The present invention proposes a new network YOLOv7-CSH suitable for glass bottle mouth defect detection, in which the CA attention mechanism is introduced into the backbone feature extraction network, which can capture the location information of the defect, improve the accuracy of defect positioning, and make the detection result more accurate. The S-ELAN module is constructed to replace the original ELAN to enhance the network's learning ability for small differences between different defects. The feature fusion network of the Neck part is replaced with a hierarchical weighted feature fusion network (HWFF), so that features of different sizes can contribute differently in the detection, solving the problem that when a large number of small defects appear, the loss of feature information affects the detection accuracy. And on this basis, the Focal Loss focal loss is introduced. The problem of sample imbalance in the bounding box regression task is optimized, the network's regression ability for the defect position is improved, and the detection performance of the detection network is further improved. Through experiments, mAP@0.5 reached 85.8%, which is 4.6 percentage points higher than the original algorithm, and in the recognition of small defects, it is more accurate than the original algorithm. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] Figure 1 It is the structural diagram of ELAN and S-ELAN;
[0056] Figure 2 This is the YOLOv7-CSH network structure diagram of the present invention;
[0057] Figure 3 This is the structural diagram of the CA coordinate attention mechanism of the present invention;
[0058] Figure 4 This is a HWFF network structure diagram of the present invention;
[0059] Figure 5 The figure is a flow chart of the glass bottle mouth detection method of the present invention. DETAILED DESCRIPTION
[0060] The present invention will be further described below in conjunction with the accompanying drawings. The following embodiments are only used to more clearly illustrate the technical solution of the present invention, and cannot be used to limit the protection scope of the present invention.
[0061] The present invention discloses a glass bottle defect detection method based on a hierarchical weighted feature fusion network, comprising the following steps:
[0062] Step 1: Obtain glass bottle mouth image data with different defects and annotate the data. Preprocess the acquired glass bottle mouth defect image data and divide the image data to obtain a training set and a test set.
[0063] Step 1.1: Obtain open source glass bottle mouth defect image data, including four typical defects: gap (Ga), pollution (Po), crack (Cr), and bubble (Bu), and use bilateral filtering to denoise the image.
[0064] Step 1.2: Use Labelimg to select YOLO format to annotate the image data and output the results to the corresponding file. Divide the data set into a training set and a validation set in a ratio of 8:2.
[0065] Step 2: Build the original YOLOv7 network model, and reasonably improve the network structure of YOLOv7, and propose a network YOLOv7-CSH suitable for glass bottle mouth defect detection. The network structure diagram is as follows Figure 2 As shown. The improvements include: introducing the CA attention mechanism in the backbone feature extraction network, embedding the vertical and horizontal information of the defect into the feature channel, and improving the network's ability to detect small targets. Constructing a larger efficient layer aggregation network (S-ELAN) module to replace the original ELAN to enhance the network's learning ability for small differences between different defects. The feature fusion network of the Neck part is replaced by a hierarchical weighted feature fusion network (HWFF) to effectively utilize shallow location information and deep semantic information, and perform feature fusion on the shallow feature map obtained by the backbone network to solve the problem of poor detection of small defects. EIoU is selected as the loss function for model optimization to improve the algorithm's feature extraction ability and detection accuracy.
[0066] Step 2.1: Configure the deep learning environment and build the original YOLOv7 model. The software environment used in the experiment is Windows 10 system, python 3.10, Pytorch 1.12, and CUDA version 10.0. The IDE used is pycharm, and the environment management used is anaconda. Go to the open source website to download the YOLOv7 source code.
[0067] Step 2.2: Improve the ELAN module in the YOLOv7 backbone network. Since the types of defects on the bottle mouth surface are not very different, they are not easy to distinguish, which always causes false detection. In order to enhance the network's ability to learn small differences between different defects, the residual link of ResNet and the SE module of SENet are introduced on the basis of ELAN, the input is directly added to the output, a short-circuit connection is added, and the FC layer in the middle of the SE module is removed. This improved ELAN module is named S-ELAN module. Compared with ELAN, S-ELAN not only enhances the learning ability of different defect features, but also can train deeper networks. Figure 1 Schematic diagram before and after improvement.
[0068] Step 2.3: The detection capability of the surface defect detection algorithm of YOLOv7 is limited. It is necessary to enhance the location information of defect features and improve the network's attention to defects. The CA attention mechanism is embedded into the backbone network of YOLOv7, and the location information of the actual bottle mouth defect is embedded into the attention channel, so that the generated feature information has the coordinate information corresponding to the defect. This helps the model locate and identify defects more accurately.
[0069] CoordAttention decomposes channel attention into two 1D feature encoding processes, so that features can be aggregated in different spatial directions, such as Figure 3 shown.
[0070] It is specifically divided into two steps: coordinate information embedding and coordinate attention generation.
[0071] Specifically, first, given the input feature X, we encode each channel using a pooling kernel of size (H, 1) or (1, W). This encoding step is performed in the horizontal and vertical directions respectively. Through this encoding process, we obtain the output of two channels. These two transformations enable the attention module to obtain long-term dependencies along one spatial direction and preserve precise position information along another spatial direction.
[0072] The output of the cth channel with height h can be expressed as:
[0073]
[0074] The output of the cth channel with width w can be expressed as:
[0075]
[0076] Then the above transformation is concatenated, and then transformed using the 1×1 convolution transformation function F1, as shown below:
[0077] f=δ(F1([z h ,z w ]))
[0078] δ is a nonlinear activation function, and f is an intermediate feature map that encodes spatial information in the horizontal and vertical directions.
[0079] Split f into two separate tensors f along the spatial dimension h and f w , using two 1×1 convolution transformations F h and F w fh and f w Transformed to g with the same number of channels h and g w Input to X, the display is as follows:
[0080] g h =σ(F h (f h ))
[0081] g w =σ(F w (f w ))
[0082] σ is the sigmoid activation function.
[0083] The output formula of Coordinate Attention Block is:
[0084]
[0085] Step 2.4: The path aggregation feature fusion network PANet is used in the Neck module of YOLOv7. It treats all feature information equally. Since there are many small defects in the bottle mouth in the data set, the defect feature information will be missing, which will eventually affect the detection results.
[0086] To prevent this phenomenon, it is necessary to weight the feature fusion to obtain different contributions from features of different sizes. In the feature fusion stage, a self-learning weight (W) is input to all features.
[0087]
[0088] Among them, w is the self-learning weight, and ε is a very small number to ensure that the weight range is 0-1.
[0089] The deep semantic information is mainly used. The shallow position information is integrated to reduce the loss of feature information of small defects. This improved network structure is named Hierarchical Weighted Feature Fusion Network (HWFF). Figure 4 Take input P5 to R5 as an example to get the formula:
[0090]
[0091]
[0092] Among them, w is the self-learning weight, ε is a very small number, and Resize is a downsampling or upsampling operation.
[0093] Step 2.5: The original loss function CIoU Loss used by YOLOv7 takes into account the overlapping area, center point distance, and aspect ratio of bounding box regression. However, its aspect ratio describes a relative value, not the actual difference between width and height and its confidence. When the width and height of the predicted box meet certain conditions, the relative proportion penalty term will become invalid. Therefore, the CIoU Loss in yolov7 is replaced by EIoU Loss. Its loss function includes overlap loss, center distance loss, and width and height loss. Although the first two parts are the same as CIoU Loss, the aspect ratio loss term is split into the difference between the predicted width and height and the minimum external frame width and height in the width and height loss to accelerate convergence. The loss function of EIoU that penalizes the prediction results of width and height is:
[0094]
[0095] L IoU is the IoU loss, L dis is the distance loss, L asp is the aspect ratio loss. IoU is the intersection over union ratio, ρ 2 (b,b gt ) / c 2 is the Euclidean distance between the ground truth center and the anchor box center, is the Euclidean distance between the ground truth width and the anchor box width, is the Euclidean distance between the ground truth height and the anchor box height. W and c h is the width and height of the smallest bounding box covering both bboxes
[0096] Considering that there is also an imbalanced training sample problem in the regression of the bbox, the number of prediction boxes with a small overlap area with the target box is much greater than the low-quality samples with a large overlap area, which ultimately affects the training process. FocalLoss is introduced on the basis of EIoU to optimize the sample imbalance problem in the bounding box regression task, that is, to reduce the contribution of the prediction box with a small overlap area with the target box to the bbox optimization, and to separate the high-quality anchor boxes from the low-quality anchor boxes. The Focal-EIoU loss formula is:
[0097] L Focal-EIOU =IOU γ L EIOU
[0098] γ is a hyperparameter used to control the curvature of the curve.
[0099] The Focal-EIoU loss function improves the network’s ability to regress defect locations and further improves the prediction performance of the detection network.
[0100] Step 2.6: In step 4, the evaluation indicators used for training and verification include: mAP mean average precision, which is expressed as:
[0101] Average precision value:
[0102]
[0103] in,
[0104]
[0105] In the formula, P represents precision, R represents recall, TP represents the number of positive predictions that are actually positive, FP represents the number of positive predictions that are actually negative, FN represents the number of negative predictions that are actually positive, and AP represents the average precision.
[0106] Step 3: Input the training set in the dataset into the improved YOLOv7 model for training to obtain a glass bottle mouth defect detection model.
[0107] Step 3.1: First, copy the coco.yaml file in the data folder to create the bottle.yaml file, and modify the parameters of train, test, nc, and names. Next, find the yolo.py file in the models folder, modify the nc parameter to form the original yolov7 model, and then copy a copy to yolov7-CSH.yaml, and configure it according to the improved method of the model in step 2.
[0108] Step 3.2: Download the pre-trained weights yolov7.pt from the yolov7 open source website, change the weigts parameter in the train.py file to yolov7.pt, change cfg to the yolov7-CSH.yaml path, change data to the bottle.yaml path, change the number of epoch training rounds to 300, change batch-size to 16, and change workers to 16. After the modification is completed, use the data set in step 1 to start training.
[0109] Step 4: Input the validation set in the data set into the glass bottle mouth defect detection model trained in step 1, and evaluate the model based on the test results.
[0110] The present invention sets up three experimental groups for comparison and explanation. Using the same experimental equipment data set and parameters, YOLOv5, the original improved YOLOv7 and the improved model YOLOv7-HSE model are compared respectively. The comparison results are shown in the following table:
[0111] Network Model mAP / % Parameter quantity / M FPS(frames / s) YOLOv5 80.8 7.4 42.8 YOLOv7 81.2 9.1 78.9 YOLOv7-CSH 85.8 11.2 77.4
[0112] Although the number of parameters of the present invention has increased, the average accuracy is 4.6 higher than that of the original YOLOv7 model. The overall network performance is enhanced, the possibility of missing small defects when detecting defects is reduced, and features can be extracted more completely.
[0113] The present invention introduces the CA attention mechanism into the backbone feature extraction network, which can capture the location information of the defects, improve the accuracy of defect positioning, and make the detection results more accurate. The S-ELAN module is constructed to replace the original ELAN to enhance the network's learning ability for small differences between different defects. The feature fusion network of the Neck part is replaced with a hierarchical weighted feature fusion network (HWFF), so that features of different sizes can contribute differently in the detection, solving the problem that when a large number of small defects appear, the loss of feature information affects the detection accuracy. And on this basis, the Focal Loss focal loss is introduced. The problem of sample imbalance in the bounding box regression task is optimized, the network's regression ability for the defect position is improved, and the detection performance of the detection network is further improved. Through experiments, mAP@0.5 reached 85.8%, which is 4.6 percentage points higher than the original algorithm, and in the recognition of small defects, it is more accurate than the original algorithm.
[0114] The above embodiments are only for illustrating the technical concept and features of the present invention, and their purpose is to enable people familiar with the technology to understand the content of the present invention and implement it accordingly, and they cannot be used to limit the protection scope of the present invention. Any equivalent transformation or modification made according to the spirit of the present invention should be included in the protection scope of the present invention.
Claims
1. A glass bottle defect detection method based on a hierarchical weighted feature fusion network, characterized in that: The steps include: Step 1: Obtain glass bottle mouth image data with different defects, annotate the data, preprocess the acquired glass bottle mouth defect image data, and divide the image data to obtain a training set and a test set; Step 2: Build the original YOLOv7 network model, improve the network structure of YOLOv7, introduce the CA attention mechanism in the backbone feature extraction network, build an efficient layer aggregation network S-ELAN module to replace the original ELAN; replace the feature fusion network of the Neck part with the hierarchical weighted feature fusion network HWFF, perform feature fusion on the shallow feature map obtained by the backbone network, and select EIoU as the loss function for model optimization; The CA attention mechanism is inserted after the three CBS modules in the backbone feature extraction network and after the first S-ELAN module; The specific structure of the hierarchical weighted feature fusion network HWFF is as follows: the shallow feature output is downsampled and fused with P4 to generate Q4. Similarly, Q4 is fused with P5 to generate Q5, and further downsampled and fused with P6 and P7 to generate Q6 and Q7 respectively. The feature information Q5, Q6 and Q7 generated after fusion are fused with P5, P6 and P7 respectively, and then fused with the adjacent upsampled output to generate R5, R6 and R7. The mathematical model expression of the HWFF network is shown in the formula. When inputting P5 to outputting R5: Among them, ω is the autonomous learning weight, ε is a very small number, and Resize is a downsampling or upsampling operation; The high-efficiency layer aggregation network S-ELAN module replaces the original ELAN, as follows: Based on ELAN, the residual link of ResNet and the SE module of SENet are introduced, the input is directly added to the output, a short-circuit connection is added, and the FC layer in the middle of the SE module is removed; Step 3: Input the training set into the improved YOLOv7 model for training, thereby obtaining a glass bottle mouth defect detection model; Step 4: Input the validation set into the glass bottle mouth defect detection model trained in step 1, evaluate the model based on the test results, and finally use the trained glass bottle mouth defect detection model to perform defect detection.
2. The glass bottle defect detection method based on hierarchical weighted feature fusion network according to claim 1 is characterized in that: The pretreatment process in step 1 is as follows: Step 1.1: Obtain open-source glass bottle mouth defect image data, including four typical defects: gap Ga, contamination Po, crack Cr, and bubble Bu, and use bilateral filtering to denoise the image; Step 1.2: Use Labelimg to select YOLO format to annotate the image data, output the results to the corresponding file, and divide the data set into a training set and a validation set in a ratio of 8:
2.
3. The glass bottle defect detection method based on hierarchical weighted feature fusion network according to claim 1 is characterized in that: In step 2, the CA attention mechanism is embedded into the backbone network of YOLOv7, and the position information of the actual bottle mouth defect is embedded into the attention channel, so that the generated feature information has the coordinate information corresponding to the defect, as follows: CoordAttention decomposes channel attention into two 1D feature encoding processes, so that features can be aggregated in different spatial directions. It is divided into two steps: coordinate information embedding and coordinate attention generation; First, given the input feature X, each channel is encoded using a pooling kernel of size (H, 1) or (1, W). This encoding step is performed in the horizontal and vertical directions respectively. Through this encoding process, the output of two channels is obtained. These two transformations enable the attention module to obtain long-term dependencies along one spatial direction and preserve precise position information along another spatial direction. The output of the cth channel with height H is expressed as: The output of the c-th channel with width W is expressed as: Then the above transformation is concatenated, and then transformed using the 1×1 convolution transformation function F1, as shown below: f=δ(F1([z h ,z w ])) δ is a nonlinear activation function, and f is an intermediate feature map that encodes spatial information in the horizontal and vertical directions; Split f into two separate tensors f along the spatial dimension h and f w , using two 1×1 convolution transformations F h and F w f h and f w Transformed to g with the same number of channels h and g w Input to X, the display is as follows: g h =σ(F h (f h )) g w =σ(F w (f w )) σ is the sigmoid activation function; The output formula of Coordinate Attention Block is:
4. The method for glass bottle defect detection based on hierarchical weighted feature fusion network according to claim 1, characterized in that: In step 2, the feature fusion network of the Neck part is replaced by a hierarchical weighted feature fusion network HWFF, specifically: Use the path aggregation feature fusion network PANet in the Neck module of YOLOv7, and input an autonomous learning weight W to all features in the feature fusion stage: Among them, w is the self-learning weight, and ε is a very small number to ensure that the weight range is 0-1.
5. The method for glass bottle defect detection based on hierarchical weighted feature fusion network according to claim 1, characterized in that: In step 2, the CIoU Loss in yolov7 is replaced by EIoU Loss, including overlap loss, center distance loss, width and height loss. The loss function of EIoU to penalize the prediction results of width and height is: Among them, L IoU is the IoU loss, L dis is the distance loss, L asp is the aspect ratio loss, IoU is the intersection over union ratio, ρ 2 (b,b gt ) / c 2 is the Euclidean distance between the ground truth center and the anchor box center, is the Euclidean distance between the ground truth width and the anchor box width, is the Euclidean distance between the ground truth height and the anchor box height, c W and c h is the width and height of the smallest bounding box that covers both bboxes.
6. The method for glass bottle defect detection based on hierarchical weighted feature fusion network according to claim 5, characterized in that: Based on EIoU, Focal Loss is introduced to optimize the sample imbalance problem in the bounding box regression task, that is, to reduce the contribution of the prediction box with a small overlapping area with the target box to the bbox optimization, and to separate the high-quality anchor box from the low-quality anchor box. The Focal-EIoU loss formula is: L Focal-EIOU =IOUγL EIOU Among them, γ is a hyperparameter used to control the curvature of the curve.
7. A glass bottle defect detection method based on a hierarchical weighted feature fusion network according to any one of claims 1 to 6, characterized in that: In step 4, the evaluation indicators used for training and verification include: mAP average precision, which is expressed as: Average precision value: in, In the formula, P represents precision, R represents recall, TP represents the number of positive predictions that are actually positive, FP represents the number of positive predictions that are actually negative, FN represents the number of negative predictions that are actually positive, and AP represents the average precision.
Citation Information
Patent Citations
Methods, apparatus, equipment and storage media for detecting defects at the mouth of glass bottles
CN115330754B
Insulator defect detection method in foggy day scene based on improved YOLOv7 algorithm
CN116843636A
PCB surface defect detection method based on improved YOLOv7
CN118071715A