Ampoule bottle head defect detection method and system based on multi-scale feature fusion
By introducing a full-dimensional dynamic convolution and multi-scale feature fusion module, combined with CARAFE upsampling and WIoU loss function optimization, the problems of difficult feature extraction and feature loss of small targets in ampoule bottle head defect detection are solved, and more efficient multi-scale defect detection is achieved.
Patent Information
- Application Number
- CN202511681549.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-17
- Publication Date
- 2026-02-10
AI Technical Summary
Existing ampoule bottle head defect detection models have difficulty in feature extraction and suffer from severe feature loss in the detection of carbonization defects in small targets. They also lack detection accuracy and robustness, especially when dealing with small-scale defects and complex-shaped defects.
We employ full-dimensional dynamic convolution (ODConv) to enhance feature extraction capabilities, combine multi-scale feature fusion module (MFFM) and CARAFE upsampling technology, introduce WIoU loss function to optimize bounding box regression, and construct OCMW-YOLOv11n model to achieve multi-scale feature adaptive fusion and detail recovery.
It significantly improves the model's detection accuracy and robustness for small targets and multi-scale defects, enhances detection efficiency and stability, and strengthens its adaptability to complex scenarios.
Smart Images

Figure CN121505344A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of ampoule bottle head defect detection technology, and in particular to an ampoule bottle head defect detection method and system based on multi-scale feature fusion. Background Technology
[0002] In actual production, the two most common defects in ampoule heads are carbonization defects and head deformity defects. Carbonization defects mainly originate from incomplete glass combustion or insufficient local melting during the high-temperature sealing process, usually manifesting as black spots at the bottle mouth. This not only affects the visual appearance of the product but may also lead to oxidation of the medicine inside the bottle. Head deformity defects are often caused by mold wear, uneven sealing pressure, or excessively high temperature control. These defects manifest as abnormal shapes such as pointed, bulging, crooked, or sprout-like heads, directly affecting the use of the ampoule and subsequent packaging and transportation.
[0003] Early on, ampoule surface defect detection relied primarily on traditional machine vision methods, such as those based on geometric symmetry, gradient vectors, edge analysis, or least squares methods. These methods often utilize the symmetry features, edge information, or local structure of the ampoule's outer contour to model and detect defects, achieving some success in identifying common appearance anomalies. However, these methods are typically designed for specific defect types, lacking versatility and robustness; furthermore, their detection efficiency is low, making it difficult to meet the speed and adaptability requirements of modern production lines for ampoule inspection.
[0004] Currently, deep neural networks have made significant progress in complex tasks such as object detection, instance segmentation, and natural language processing, providing strong technical support for industrial surface defect detection. Especially in object detection, neural network models are widely used for defect localization and classification, with mainstream methods broadly categorized into two types: two-stage models and single-stage models. Two-stage detection models, such as Faster R-CNN, offer high accuracy but suffer from slow inference speed and high computational resource consumption. Single-stage models, such as the YOLO series, have significant advantages in detection speed and deployment efficiency, but perform poorly in small-object defect detection tasks. Furthermore, the Transformer architecture, represented by DETR which introduces a global modeling mechanism, and Mamba, which achieves efficient sequence modeling based on a state-space model, are increasingly being applied to detection tasks, with Vision Mamba being a prime example. However, methods like DETR and Mamba still suffer from insufficient generalization ability and high computational overhead in small-size, high-density industrial defect scenarios. In comparison, the lightweight YOLO series models remain the mainstream choice for industrial inspection.
[0005] In summary, the YOLO series of object detection models are currently the mainstream choice in the field of defect detection and can achieve basic ampoule bottle head defect detection tasks. However, due to the inherent characteristics of ampoule bottle defects, such as large scale differences, complex shapes, and uneven distribution, as well as the limitations of current detection technologies in feature extraction, scale adaptability, and generalization ability, there is still room for improvement in detection performance. Specifically, although existing models can meet the basic requirements for ampoule bottle head defect detection, the following difficulties and challenges still exist:
[0006] The challenge of detecting small carbonized defects: Carbonized defects typically exist as tiny black dots, occupying a very small percentage of pixels in the overall ampoule head image, exhibiting typical characteristics of small targets. During feature extraction, this information is easily weakened or even obscured, making it difficult for the model to form a stable representation. Furthermore, the texture details of carbonized defects are subtle, exhibiting high similarity to the background area, further increasing the difficulty of feature extraction and discrimination. Therefore, improving the model's ability to perceive small carbonized defects has become a significant challenge in ampoule head defect detection.
[0007] Feature loss during feature extraction: YOLO models typically reduce feature map resolution through multiple convolutional layers to obtain high-level semantic information. However, this sequential feature extraction process inevitably leads to the loss of shallow, fine-grained feature information. For small-scale defects, this information loss means insufficient saliency, which is easily masked by subsequent feature fusion stages; for large-scale defects, it makes it difficult to fully express edge contours and subtle deformations, thus limiting the accuracy and robustness of the detection model for defects of different morphologies. Summary of the Invention
[0008] To address the aforementioned problems, this invention provides a method and system for detecting defects in ampoule bottle heads based on multi-scale feature fusion.
[0009] In a first aspect, the present invention provides a method for detecting defects in ampoule bottle heads based on multi-scale feature fusion, which adopts the following technical solution: A method for detecting ampoule bottle head defects based on multi-scale feature fusion includes: Acquire image data of the ampoule bottle head; Image preprocessing is performed on the acquired ampoule bottle head image data; A multi-scale feature fusion ampoule bottle head defect detection model OCMW-YOLOv11n based on YOLOv11n is constructed. The model backbone is introduced into the full-dimensional dynamic convolution ODConv and integrated into the C3K2 module. Contextual information is fused at the model neck using the multi-scale feature fusion module MFFM. CARAFE upsampling is used to replace the neck upsampling stage. The WIoU loss function is employed to improve the model's localization accuracy in detecting defects in small targets. Train the constructed OCMW-YOLOv11n model; The trained model was used to detect defects in ampoule bottle heads, and the detection results were evaluated.
[0010] Furthermore, the image preprocessing of the acquired ampoule bottle head image data includes, at the input end, using Mosaic data augmentation to preprocess the image, generating new training samples by randomly cropping an image and splicing it with another randomly selected image, enriching background information according to batch size, increasing the number of small targets, and improving the diversity of the dataset and the robustness of the model.
[0011] Furthermore, the process of introducing the full-dimensional dynamic convolution ODConv into the core of the model and integrating it into the C3K2 module includes incorporating ODConv into the C3K2 module to replace the standard convolution operator Conv in Bottleneck, ultimately constructing a C3K2-ODConv module. This module dynamically adjusts the convolution kernel weights through a multi-dimensional attention mechanism, allowing the convolution operator to adaptively update as the input features change. ODConv learns attention weights in parallel across the four dimensions of the convolution kernel. First, ODConv introduces four lightweight attention modules for each convolution kernel, corresponding to the four dimensions respectively. Then, dynamic weight generation is performed. For a standard convolutional layer, dynamic weights are generated using ODConv, expressed as follows: , in: It is a basic static convolution kernel. These are the attention scalars learned along the spatial dimension, input channel dimension, output channel dimension, and convolutional kernel dimension, respectively. This indicates element-wise multiplication. Finally, the core of the model is introduced into the full-dimensional dynamic convolution ODConv and integrated into the C3K2 module. It also includes an improvement to the Bottleneck structure in the C3K2 module. The second 3×3 standard conv in the two standard conv convolutions contained in Bottleneck is replaced with an ODConv layer of the same kernel size, so that when information undergoes non-linear transformation within Bottleneck, adaptive feature extraction is performed through ODConv.
[0012] Furthermore, the multi-scale feature fusion module (MFFM) is used to fuse contextual information in the neck region of the model. This addresses the issue of inaccurate spatial information handling in the neck network of YOLOv11, which uses path aggregation network (PAN) and feature pyramid network (FPN). The MFFM module enhances the model's feature extraction capabilities at different scales and in different contexts by fusing shallow, corresponding, and deep feature maps. The MFFM construction employs a hierarchical processing architecture, achieving multi-scale feature integration through four stages: spatial alignment, channel unification, adaptive weighted fusion, and multi-receptive field enhancement. The input feature map contains three scales: low-level features C1, mid-level features C2, and high-level features C3. First, the shallow feature map is downsampled, and the deep feature map is upsampled to obtain three feature maps with the same resolution. Then, pointwise convolution is performed to unify the channel dimensions, as shown below: , , After processing, the three features are unified to dimensions B×Cm×H×W. Then, attention weights are generated by using three independent 1×1 convolutions and CBAM attention modules to process the three features separately, generating attention weight feature maps for each feature. These three weight feature maps are then concatenated, and the normalized weights for the three features are obtained through a fusion convolution, layer normalization, and softmax function. , , , The weighted fusion process is represented as follows: , Where W = [w1, w2, w3] are the normalized weight coefficients, and the feature fusion based on attention weights is: , in It is the residual of the corresponding layer feature map, and finally the feature representation capability is improved through the multi-receptive field enhancement module: .
[0013] Furthermore, the replacement of the model neck upsampling stage with CARAFE upsampling includes the model using the CARAFE upsampling operator to adaptively utilize neighborhood feature information to generate high-resolution feature maps through a content-aware feature recombination mechanism. The CARAFE upsampling process consists of two key components: first, an upsampling kernel prediction module performs channel compression on the input feature map to reduce computational cost; then, a convolutional layer predicts a target location for each upsampling target. The convolution kernel, which is generated based on local regions of the input feature map ( Then, the content-aware feature reorganization module uses the predicted upsampling kernel to perform weighted reorganization on the corresponding local regions in the input feature map to generate a high-resolution output feature map. Based on CARAFE, upsampling is adaptively performed according to the local content of the image: in edge and texture-rich regions, a sharp kernel is generated to preserve details; in smooth regions, a smooth kernel is generated to avoid introducing noise.
[0014] Furthermore, the method of using the WIoU loss function to improve the localization accuracy of the model in small target defect detection includes optimizing the bounding box regression loss function, introducing the WioU v3 loss based on a dynamic non-monotonic focusing mechanism, comprehensively considering the overlapping area, center point distance, and aspect ratio, and performing gradient competition imbalance suppression for samples with high and low IoU. IoU is expressed as: In this equation, the numerator represents the intersection area of the predicted bounding box and the ground truth bounding box, and the denominator is the union area of the two. Based on this, CIoU introduces additional constraints on center point distance and aspect ratio during the optimization process. Its loss function is expressed as: , Where ρ(bp,bg) is the Euclidean distance between the center points of the predicted box and the ground truth box, c is the length of the diagonal of the minimum bounding rectangle containing the two boxes, and αv is used to measure the consistency of aspect ratio. Then, the outlier measure is constructed using the WIoUv3 form and WioU v1, and the gradient gain is dynamically adjusted to achieve differentiated focusing on samples of different quality.
[0015] Furthermore, the method of constructing an outlier metric using WIoUv3 and WioUv1 and dynamically adjusting the gradient gain to achieve differentiated focusing on samples of different quality includes firstly constructing a loss function that emphasizes distance metrics using WIoUv1: , in and These are the center coordinates of the predicted bounding box and the ground truth bounding box, respectively. and These are the width and height of the smallest bounding rectangle. The normalization coefficients are then used, and a dynamic non-monotonic focusing mechanism is introduced in WIoU v3 to construct an outlier factor. To assess sample quality, it is represented as: , in ,and It is within the current sliding window The mean; finally, the WIoU v3 loss function is defined as: , To and The relevant gradient gain coefficients, when High time, For high-quality samples, the gradient gain is large; conversely, for low-quality samples, a small gradient gain is assigned.
[0016] Furthermore, the training using the improved model includes setting the model training epochs to 300, the initial learning rate to 0.01, using the SGD optimizer, and setting the batch size to 32. Specifically, the image is input into the model network, and through the feature processing stages of feature extraction and feature fusion, three feature maps of different scales, P3, P4, and P5, are obtained. These are then passed to the detection head to enable the model to locate and classify defects in the image. The overall loss is obtained by measuring the predicted bounding box and the ground truth bounding box, and the network parameters are updated through backpropagation to reduce the overall loss. After 300 epochs of training, the model converges as much as possible to obtain the optimal weights.
[0017] Furthermore, the method of using the trained model to detect ampoule bottle head defects and evaluating the detection results includes quantitative evaluation of the model based on evaluation metrics, including precision P, recall R, computational complexity, mean precision mAP, and number of parameters. Among these, precision P is used to calculate the proportion of samples that are actually positive among those predicted as positive samples, and is expressed as follows: Recall R is used to calculate the proportion of all true positive samples that are correctly predicted by the model, and is expressed as: AP is obtained by plotting the precision-recall curve and calculating the area under the curve, and is represented as: Then, take the average of the AP values for all categories to get: , where TP, FP, and FN are the number of true positives, false positives, and false negatives, respectively, and N is the number of categories.
[0018] Secondly, an ampoule bottle head defect detection system based on multi-scale feature fusion includes: The data acquisition module is configured to acquire image data of the ampoule bottle head; The preprocessing module is configured to perform image preprocessing on the acquired ampoule bottle head image data; The model building module is configured to construct an OCMW-YOLOv11n multi-scale feature fusion ampoule bottle head defect detection model based on YOLOv11n. Specifically, the model backbone is introduced into the full-dimensional dynamic convolution ODConv and integrated into the C3K2 module; context information is fused at the model neck using the multi-scale feature fusion module MFFM; CARAFE upsampling is used to replace the model neck upsampling stage; and the WIoU loss function is employed to improve the model's localization accuracy in small target defect detection. The model training module is configured to train the constructed OCMW-YOLOv11n model. The detection module is configured to use a trained model to detect defects in ampoule bottle heads and evaluate the detection results.
[0019] Thirdly, the present invention provides a computer-readable storage medium storing a plurality of instructions adapted for loading and execution by a processor of a terminal device of the aforementioned method for detecting ampoule bottle head defects based on multi-scale feature fusion.
[0020] Fourthly, the present invention provides a terminal device, including a processor and a computer-readable storage medium, wherein the processor is used to implement various instructions; the computer-readable storage medium is used to store multiple instructions, the instructions being adapted to be loaded and executed by the processor to provide the ampoule bottle head defect detection method based on multi-scale feature fusion.
[0021] In summary, the present invention has the following beneficial technical effects: This invention introduces full-dimensional dynamic convolution (ODConv) into the C3K2 module of the YOLOv11 network. By simultaneously introducing dynamic weight modulation mechanisms in the spatial, input channel, output channel, and kernel dimensions, it achieves full-dimensional adaptive modeling of the convolution kernel parameters. This structure can dynamically adjust the response mode of the convolution kernel based on the local semantics and contextual information of the input features, thereby establishing more targeted feature associations between features in different spatial locations and different channels. This mechanism can effectively improve the feature representation and discrimination capabilities of the network without significantly increasing computational complexity, enabling the model to exhibit stronger adaptability and accuracy in ampoule bottle defect feature extraction.
[0022] The Multi-Scale Feature Fusion Module (MFFM) designed in this invention introduces multi-receptive field convolution enhancement, adaptive attention weight adjustment, and a weighted fusion mechanism simultaneously in the feature fusion stage. The module first performs scale alignment and channel unification operations on three feature maps from different levels. High-level features are upsampled using CARAFE, while low-level features are spatially compressed using an adaptive downsampling module (ADown), thus achieving consistency in the feature space. Subsequently, the module uses the CBAM attention mechanism to generate a spatial weight map of multi-scale features. After weighted concatenation of the three scale features, pixel-wise fusion weights are generated through 1×1 convolution, LayerNorm, and Softmax functions, achieving dynamic weighting of cross-scale features. In the fusion output stage, multi-branch depthwise separable convolutions (kernel sizes of 3, 5, 7, and 9) are used to extract multi-receptive field information, and residual connections are combined for feature integration. This structure can adaptively balance the weight distribution of shallow detail features and deep semantic features, significantly improving the model's robustness to detecting defects at different scales and the stability of feature representation.
[0023] This invention employs CARAFE in the feature upsampling stage, which adaptively generates a reconstruction kernel based on input features, achieving detailed restoration of high-resolution features and preserving boundary continuity, thereby improving the model's ability to reconstruct edges of minor defects. Simultaneously, a WIoU mechanism is introduced into the loss function to adjust the impact of images of different qualities on the model's regression loss. The model significantly improves convergence speed, localization accuracy, and overall detection stability while maintaining computational efficiency. Attached Figure Description
[0024] Figure 1 This is a schematic diagram of the OCMW-YOLOv11n model structure of Embodiment 1 of the present invention; Figure 2 This is a schematic diagram of the C3K2-ODConv structure of Embodiment 1 of the present invention; Figure 3 This is a schematic diagram of the ODConv structure of Embodiment 1 of the present invention; Figure 4 This is a schematic diagram of the MFFM structure of Embodiment 1 of the present invention; Figure 5 This is a schematic diagram of the CARAFE structure in Embodiment 1 of the present invention; Figure 6 These are images of ampoule bottle heads from Embodiment 1 of the present invention. Figure 7 This is a schematic diagram of the dataset annotation in Embodiment 1 of the present invention; Figure 8 This is a PR curve diagram of the YOLOv11 model in Embodiment 1 of the present invention; Figure 9This is the PR curve diagram of the OCMW-YOLOv11n model in Embodiment 1 of the present invention; Figure 10 This is a diagram showing the detection results of the YOLOv11 model in Embodiment 1 of the present invention; Figure 11 This is a graph showing the detection results of the OCMW-YOLOv11n model in Embodiment 1 of the present invention. Detailed Implementation
[0025] The present invention will be further described in detail below with reference to the accompanying drawings.
[0026] Example 1 Reference Figure 1 To address the challenges of identifying small target defects and losing shallow information during feature extraction on the ampoule head dataset, this embodiment proposes targeted improvements to YOLOv11n. For example... Figure 1 As shown, the main optimization design includes the following three aspects: First, a full-dimensional dynamic convolution (ODConv) is introduced into the C3K2 module of the backbone network. By achieving dynamic adaptive modeling in multiple dimensions such as space, channels, and convolution kernels, the expressive power of feature extraction is effectively improved, thereby achieving feature enhancement. Second, an adaptive multi-scale feature fusion module (MFFM) is proposed, which can efficiently fuse shallow, medium, and deep features, preserving fine-grained information and strengthening cross-level feature interactions, thus alleviating the information loss problem caused by downsampling. At the same time, CARAFE is applied to the upsampling stage to achieve more refined feature recombination, thereby improving the model's ability to restore resolution. Finally, the original regression loss is replaced with weighted IoU (WIoU). Through a dynamic weighting mechanism, the model is guided to pay more attention to low-quality samples and images containing small target defects, further improving the localization accuracy and robustness of small targets. Through the above improvements, the proposed model significantly enhances the detection capability of small targets and multi-scale defects while maintaining lightweight characteristics. The improved model is named OCMW-YOLOv11n (ODConv-CARAFE-MFFM-WIoU) in this paper. Its overall network structure consists of four parts: input, backbone, neck and head.
[0027] At the input end, Mosaic data augmentation is used to preprocess the images. This method generates new training samples by randomly cropping an image and stitching it with another randomly selected image, according to batch size. This enriches the background information, increases the number of small objects, and significantly improves the diversity of the dataset and the robustness of the model.
[0028] In the backbone network, the model integrates the C3K2-ODConv module with a regular convolutional module. By introducing full-dimensional dynamic convolution (ODConv), it effectively enhances the expressive power of the feature extraction stage and the ability to perceive small targets and complex defects. In addition, the SPPF module further improves the model's feature abstraction ability through multi-scale pooling operations, while the C2PSA module enhances the network's attention mechanism, thereby maintaining stable detection performance in various defect scenarios.
[0029] In the neck network, the multi-scale feature fusion module MFFM designed in this paper is introduced. This module fully preserves the semantic and detailed information of feature maps at different scales through cross-scale information interaction and weight adaptive fusion mechanism, which significantly improves the efficiency and robustness of feature fusion. In addition, upsampling is replaced with CARAFE.
[0030] In the detection head section, the model comprises three prediction layers, corresponding to feature maps of different scales from the neck (80×80×256, 40×40×512, and 20×20×1024), to meet the detection requirements of small, medium, and large targets. Each feature map is divided into grid cells, and three different aspect ratios are preset for each cell to complete target classification and bounding box regression.
[0031] Furthermore, during the training phase, this paper replaces the traditional IoU loss function with the WIoU (Wise IoU) loss function. This method can dynamically adjust the weight distribution of positive and negative samples, suppress the adverse effects of low-quality candidate boxes on the optimization process, thereby improving the model's localization accuracy and overall detection performance while ensuring convergence stability.
[0032] 3.2 Enhanced Feature Extraction Module Based on ODConv In object detection models, the feature extraction capability of the backbone network directly determines the performance of subsequent detection heads. The C3K2 module in YOLOv11 is a key component of backbone feature extraction. Its structure is based on the CSPNet (Cross Stage Partial Network) concept, effectively mitigating the gradient vanishing and information redundancy problems that occur during network deepening through cross-stage feature branching and aggregation design. Specifically, C3K2 divides the input features into two branches: one branch extracts features through multiple lightweight Bottleneck units (internally using a combination of 3×3 and 1×1 convolutions), while the other branch acts as a shortcut path for direct transmission. Finally, the two feature branches are concatenated along the channel dimension and fused using a 1×1 convolution. This structure can achieve complementarity and enhancement of deep and shallow features while ensuring computational efficiency. However, its internal convolutional kernels are static, sharing the same weights for all input samples, which limits the model's adaptability to targets of different scales and appearances.
[0033] Therefore, this paper will use ODConv (Omni-Dimensional Dynamic Convolution, such as...) Figure 4 This technology, ODConv, is integrated into the C3K2 module to replace the standard convolution operator Conv in its Bottleneck, ultimately forming the C3k2-ODConv module. The core idea of ODConv is to dynamically adjust the convolution kernel weights through a multi-dimensional attention mechanism, enabling the convolution operator to adaptively update as the input features change. As an advanced full-dimensional dynamic convolution technique, ODConv achieves true full-dimensional dynamic perception by learning attention weights in parallel across the four dimensions of the convolution kernel (spatial, input channel, output channel, and kernel dimension). Its core mechanism is as follows: Multi-dimensional attention: ODConv introduces four lightweight attention modules for each convolutional kernel, corresponding to the four dimensions mentioned above. These modules work together to generate adaptive weights for each convolutional kernel at different locations, different input / output channels, and different kernel samples.
[0034] Dynamic weight generation: For a standard convolutional layer, ODConv generates dynamic weights in the following way: , in: It is a basic static convolution kernel. These are the attention scalars (or vectors) learned along the spatial dimension, input channel dimension, output channel dimension, and convolutional kernel dimension, respectively. This indicates element-wise multiplication.
[0035] This design allows ODConv to dynamically adjust the importance of each convolutional kernel based on the input content, giving the model stronger feature representation and generalization capabilities, while introducing only a small number of additional parameters. Compared to traditional static convolution, ODConv gives convolutional kernels the ability to adaptively adjust according to input features, thus enabling better modeling of multi-scale, irregular, and complex morphological defects.
[0036] In this design, we specifically improved the Bottleneck structure in the C3k2 module. For example... Figure 2 As shown, the original Bottleneck contains two standard conv convolutions. This paper replaces the second 3×3 standard conv with an ODConv layer of the same kernel size. This replacement allows for more intelligent and adaptive feature extraction via ODConv when information undergoes non-linear transformations within the Bottleneck. It enhances the model's ability to capture key features in complex scenes, particularly improving robustness to detecting targets with large scale variations and diverse appearances. This improvement is applied to all C3k2 modules in the feature extraction stage of the baseline model as a key technique for building a more robust backbone network. The specific structure diagram is shown below. Figure 2 As shown: 3.3 Multi-scale Feature Fusion Module (MFFM) In YOLOv11, the neck network employs a path aggregation network (PAN) and a feature pyramid network (FPN), and uses spatial pyramid pooling (SPPF) to obtain hierarchical information. However, this information extraction method is not precise enough when processing spatial information, especially in the backbone network. The loss of detail information in the upper-layer feature maps due to their low resolution affects the detection performance of small targets. Traditional feature fusion methods typically only upsample deep feature maps, ignoring important details in shallow layers, which further impacts the detection accuracy of small targets.
[0037] To address these issues, this paper proposes a novel module—the Multi-Scale Feature Fusion Module (MFFM). This module aims to enhance the model's feature extraction capabilities at different scales and under complex backgrounds by fusing feature maps from shallow, corresponding, and deep layers, thereby improving the performance of ampoule bottle head defect detection.
[0038] like Figure 4 As shown, to effectively fuse feature information from different scales, this paper designs a multi-scale feature fusion module (MFFM). This module adopts a hierarchical processing architecture and achieves efficient integration of multi-scale features through four stages: spatial alignment, channel unification, adaptive weighted fusion, and multi-receptive field enhancement.
[0039] The input feature map contains three scales: low-level feature C1 (size B×Cl×2H×2W), mid-level feature C2 (size B×Cm×H×W), and high-level feature C3 (size B×Ch×H / 2×W / 2). First, the shallow feature map is downsampled, and the deep feature map is upsampled to obtain three feature maps with the same resolution. Then, pointwise convolution is performed to unify the channel dimensions. , , , After processing, the three features are unified to dimensions B×Cm×H×W. Then, attention weights are generated: three independent 1×1 convolutions and CBAM attention modules are used to process the three features separately, generating attention weight feature maps for each feature. These three weight feature maps are then concatenated, and the normalized weights for the three features are obtained through a fusion convolution, layer normalization, and softmax function. , , , The weighted fusion process is represented as follows: , Where W = [w1, w2, w3] are the normalized weight coefficients. Feature fusion based on attention weights is as follows: , in It is the residual of the corresponding layer feature map.
[0040] Finally, the feature representation capability is improved through a multi-receptive field enhancement module: , This module adaptively balances the contribution of features at different scales, significantly improving the model's ability to perceive multi-scale defects while maintaining parameter efficiency. It also enhances the model's ability to represent small targets while preserving multi-scale contextual information. Furthermore, the multi-receptive field enhancement operation further expands the model's receptive field of view, improving detection robustness and accuracy.
[0041] 3.4 CARAFE Upsampling Module In object detection networks, upsampling operations are typically used to restore low-resolution feature maps to high resolution for multi-scale fusion with higher-level features, thereby improving the model's ability to perceive targets at different scales. In surface defect detection tasks, defects are often small in size, complex in shape, and subject to significant background interference; therefore, multi-scale feature fusion plays a crucial role in improving the detection accuracy of minute defects. Traditional upsampling operations (such as bilinear interpolation) are static and content-independent. They use the same interpolation kernel for all regions and cannot adaptively adjust based on the content of the feature map. This often leads to information loss when processing complex textures and detailed structures, which negatively impacts the model's detection accuracy.
[0042] To address the aforementioned issues, this paper replaces upsampling with the CARAFE (Content-Aware ReAssembly of Features) upsampling operator in the model. The core idea of CARAFE is to adaptively utilize neighborhood feature information to generate high-resolution feature maps through a content-aware feature reconstruction mechanism, rather than simply relying on fixed interpolation methods. Specifically, CARAFE dynamically adjusts the feature fusion weights at each location during the upsampling process, thereby more accurately reconstructing high-resolution features, preserving fine-grained information in key defect regions, and enhancing the context-awareness of the feature maps. CARAFE consists of two key components: Upsampling kernel prediction module: This module first performs channel compression on the input feature map to reduce computational cost, and then predicts a kernel for each upsampled target location through a convolutional layer. The convolutional kernel. The generation of this kernel depends on a large local region in the input feature map ( This enables it to perceive semantic content.
[0043] Content-aware feature recombination module: This module uses the predicted upsampling kernel to perform weighted recombination on the corresponding local regions in the input feature map, thereby generating a high-resolution output feature map.
[0044] This mechanism allows CARAFE to adaptively upsample based on the local content of the image: in edge and texture-rich areas, it generates a sharper kernel to preserve details; in smooth areas, it generates a smooth kernel to avoid introducing noise.
[0045] In our implementation, we replaced the upsampling in the neck network of the model, and also used the same upsampling operator in the multi-scale feature fusion module MFFM mentioned earlier. This module learns end-to-end during training, introducing only a small number of additional parameters and computational overhead, yet significantly improving the model's ability to reconstruct detailed information, especially beneficial for the detection and localization accuracy of small targets.
[0046] 3.5 Improvement of Bounding Box Regression Loss Function Based on WIoU To further improve the regression accuracy of the bounding box loss, especially for difficult samples such as small targets, this paper optimizes the bounding box regression loss function. This paper abandons the CIoU loss used in the baseline model and introduces the WioU v3 loss based on a dynamic non-monotonic focusing mechanism. This loss function is the third version of the WioU loss function.
[0047] While CIoU loss takes into account overlap area, center distance, and aspect ratio, its aspect ratio term is not accurate enough in some cases and treats all samples equally. This leads to an imbalance in gradient competition between high-quality (high IoU) and low-quality (low IoU) samples, with gradients from a large number of simple samples dominating the training process, thus limiting optimization for difficult samples.
[0048] The basic definition of IoU is: , Where the numerator represents the intersection area of the predicted bounding box and the ground truth bounding box, and the denominator is the union area of the two. Based on this, CIoU introduces additional constraints on center point distance and aspect ratio during the optimization process, and its loss function can be expressed as: , Where ρ(bp,bg) is the Euclidean distance between the center points of the predicted bounding box and the ground truth bounding box, c is the length of the diagonal of the minimum bounding rectangle containing both boxes, and αv is used to measure aspect ratio consistency.
[0049] To overcome the aforementioned problems, this paper replaces the regression loss of the YOLO model with WIoU (Wise-IoU). The core idea of WIoU is to design a dynamic weighting mechanism that adaptively allocates gradient weights based on the matching difficulty between the predicted and ground truth boxes. WIoU has three forms; this paper uses form v3. Its core lies in building upon WIoU v1 to construct an "outlier" metric and dynamically adjust the gradient gain accordingly, achieving differentiated focusing on samples of varying quality. The definition of WIoU v3 is as follows: First, WIoU v1 constructs a loss function that emphasizes distance metrics: , here, and These are the center coordinates of the predicted bounding box and the ground truth bounding box, respectively. and It is the width and height of the smallest bounding rectangle. These are the normalization coefficients, which are separated during the calculation to prevent hindering model convergence.
[0050] Building upon this, WIoU v3 introduces a dynamic non-monotonic focusing mechanism. This is achieved by constructing an "outlier" factor. To assess sample quality: , in ,and It is within the current sliding window The mean.
[0051] Finally, the WIoU v3 loss function is defined as: , This constructs a... Related gradient gain coefficients .when When the sample quality is high (i.e., the sample quality is poor, and the sample is an outlier), It is also larger, thus allocating a larger gradient gain to difficult samples; conversely, for high-quality samples ( If the sample size is low, a smaller gradient gain is assigned. This dynamic focusing mechanism effectively reduces the monopoly of ordinary quality samples on regression training, guiding the model to focus more on improving the regression accuracy of difficult samples.
[0052] In our implementation, we directly replaced the CIoU loss calculation module in the original detector head, using WIoU v3 as the sole loss function for bounding box regression. This improvement does not incur additional computational overhead, yet significantly optimizes the model's performance in target localization within complex scenes.
[0053] Model training The training cycle of the ampoule bottle head defect detection model described in this invention includes multiple stages such as data input, feature extraction, feature fusion, target localization and classification, loss calculation, and parameter optimization.
[0054] In each training iteration, the training set and validation machine from the ampoule head dataset are first used to train the model weights. The processed image is then input into the model's backbone network, where multi-layer convolutional downsampling and non-linear activation operations are used to extract feature information at different levels. This invention optimizes the backbone network by introducing full-dimensional dynamic convolution into the C3k2 module. This operation allows the model to extract richer and more detailed defect features during the feature extraction stage. Shallow features mainly contain ampoule edge and surface texture information, while deeper features contain semantic expressions of defects. After feature extraction, the output consists of multiple feature maps of different scales. For an image of size 640◊640, feature extraction will yield feature maps of sizes P1(320◊320), P2(160◊160), P3(80◊80), P4(40◊40), and P(20◊20).
[0055] Next, the P3, P4, and P5 features obtained from the feature extraction stage are fused and fed into the multi-scale feature fusion module MFFM. This module adaptively fuses high-level semantic features with low-level texture features through strategies such as channel unification, scale alignment, and dynamic weighting, achieving collaborative modeling of global perception and local details. During the fusion process, CBAM attention and multi-scale deep convolution are used to enhance local response capabilities, enabling the model to more effectively focus on the small defect area of the ampoule bottle head. The fused unified feature map is then subjected to content adaptive reconstruction by the upsampling module CARAFE. CARAFE learns a reconstruction kernel related to feature content to achieve fine edge restoration of high-resolution features, significantly improving the localization accuracy and boundary integrity of small targets. After the above processing, the features are then input into other modules for further fusion processing, finally obtaining P3, P4, and P5 feature maps containing richer feature information.
[0056] Next, the feature maps at the three scales are fed into the model's detection head. In the detection head stage, the model performs object classification and bounding box regression prediction on the fused multi-scale feature maps. This stage is crucial for the model to complete the transition from feature representation to explicit detection output. The detection head consists of several sets of 1×1 and 3×3 convolutions. After processing by the detection head, it outputs three types of prediction results: objectness indicating whether an object exists at the current grid location; classification, which predicts the defect type of the detected object; and regression, which accurately regresses the center point coordinates and width and height parameters of the target region. In the prediction stage, the model uses the Sigmoid and Softmax functions to enable spatial localization and category determination of defects.
[0057] During training, the model uses loss functions to measure the difference between the predicted results and the ground truth bounding boxes. These loss functions include bounding box regression loss (ensuring the model can accurately locate the target), confidence loss (optimizing the confidence of the predicted bounding boxes and improving the model's ability to distinguish the presence or absence of the target), and classification loss (ensuring the model can correctly identify which category the object in the image belongs to). To improve the performance of small target detection and regression stability, this invention introduces the WIoU loss function in the bounding box regression part, replacing the CIoU used in the original YOLOv11 framework. This allows the model to adaptively focus on hard-spot regions with large localization errors during training, thereby improving the regression accuracy of the bounding boxes and the model's convergence speed. The model then updates and optimizes the network parameters based on the value of the loss function. After each forward propagation, the model calculates the overall loss for that batch of samples. Then, the backpropagation algorithm calculates the gradient of the loss with respect to each parameter layer by layer according to the chain rule, thereby guiding the optimizer to update. The optimizer fine-tunes each parameter based on factors such as gradient direction and learning rate, so that the overall loss tends to decrease. Finally, through a large number of iterations, the parameters are continuously updated and optimized by gradients, and the model learns the optimal feature representation and detection strategy, achieving high-precision identification and location of defects on the surface of ampoule bottle heads.
[0058] Throughout the 300 training epochs, a learning rate of 0.01 was set, and the SGD optimizer was used. The model continuously optimized its parameters on the training set and calculated key performance metrics such as P, R, and mAP on the validation set, dynamically monitoring the model's convergence status and generalization performance. As the epochs progressed, the model gradually converged, and the various metrics improved and stabilized. The final optimal model weights were then used to verify the performance on the test set and applied in practice.
[0059] Experimental verification To verify the performance of the proposed model, extensive experiments were conducted on a self-made ampoule bottle head dataset. The following sections first describe the creation and preprocessing of this dataset. Then, the experimental setup and relevant evaluation metrics are introduced. Finally, the experimental results are presented and analyzed.
[0060] 4.1 Dataset This paper constructs a relatively general dataset for ampoule bottle head defect detection, with the main goal of automating the identification and detection of key defects in ampoule bottle heads. The defect samples in the dataset are all from a collaboration with a manufacturer, and the image resolution is uniformly adjusted to 640 × 640 to ensure the comparability of experimental results and the stability of the training process. The dataset contains two main types of defects: carbonization defects and head defects. The defect categories are shown in the figure. Carbonization defects typically appear as tiny black dots, while head defects often have larger areas and complex shapes. The dataset annotation is shown in the figure. Although the number of head defects is relatively small, their impact on the final model performance is negligible based on the results.
[0061] During data collection, this paper collected a large number of ampoule bottle head images from the factory production line. During the data processing phase, a rigorous screening process was implemented, prioritizing samples with relatively clean surfaces and containing only a single type of defect. This avoided training bias and performance degradation caused by background noise or the superposition of multiple defects. Ultimately, 716 ampoule bottle head images containing two types of defects were selected.
[0062] In terms of data partitioning, this paper divides the dataset into a training set (500), a validation set (107), and a test set (109) in a ratio of 7:1.5:1.5. The training set (70%) is used for the model to learn the main features of the data, the validation set (15%) is used to dynamically evaluate the convergence and performance of the model during training, and the test set (15%) is used to objectively evaluate the model's generalization ability on unseen data after training. This partitioning ratio is a common practice in deep learning experiments, ensuring sufficient training data while reserving enough validation and test samples, thereby effectively reducing the risk of overfitting and improving the reliability and practicality of experimental conclusions.
[0063] In deep learning tasks, data augmentation is often considered a common method to improve model robustness and generalization performance. However, this paper did not introduce additional data augmentation methods to expand the sample size when constructing the ampoule bottle head defect detection dataset. The reasons are mainly reflected in the following three aspects: First, carbonization and head defects have certain regularities in spatial distribution and morphology. If augmentation is performed by means of rotation, scaling, or flipping, it may destroy the natural characteristics of the defects and produce pseudo-samples that do not match reality. Second, although the data scale is limited, the collected and selected images have covered the typical manifestations of the two types of defects, and the category distribution is relatively balanced, which can meet the needs of model training. Finally, in small object detection tasks, unreasonable augmentation often introduces noise or changes the defect boundary features, causing the model to learn invalid or even harmful information. Based on the above considerations, this paper chooses to maintain the original authenticity of the data and ensure the reliability and interpretability of experimental results through strict data screening and reasonable data partitioning, rather than relying on data augmentation for expansion.
[0064] 4.2 Evaluation Indicators To fully verify the effectiveness of the proposed improved model, this paper uses mainstream evaluation metrics commonly used in the field of object detection to quantitatively evaluate the model, including: precision (P), recall (R), computational complexity (GFloaps), mean average precision (mAP), and number of parameters. The definitions of each metric are as follows: • Accuracy (P): The proportion of samples predicted as positive that are actually positive, used to measure the reliability of the model's prediction results; , • Recall (R): The proportion of all true positive samples that are correctly predicted by the model, used to measure the model's detection capability; , • AP (Average Precision): Calculated by plotting the precision-recall curve and calculating the area under the curve; , • mAP (mean Average Precision): The average AP of all categories. The higher the value, the better the model performs in terms of overall detection accuracy. , • Number of parameters: Reflects the size and complexity of the model. The smaller the number of parameters, the lower the computational and storage overhead of the model, and the more suitable it is for practical deployment.
[0065] By comprehensively evaluating the above multi-dimensional indicators, we can compare the overall performance of different models in ampoule defect detection tasks from different perspectives such as detection accuracy, detection speed, and model complexity, ensuring the objectivity and comprehensiveness of the experimental conclusions. The specific calculation formula is shown below: Where TP, FP, and FN are the number of true positives, false positives, and false negatives, respectively, and N is the number of categories.
[0066] 4.3 Experimental Setup All experiments in this paper were conducted under the Windows 11 operating system. The hardware platform consisted of a computer equipped with an Intel(R) Core(TM) i9-10900K processor and an NVIDIA GeForce RTX 3090 graphics card with 24GB of video memory. For the software environment, Python 3.9 was used as the primary programming language, and the model was built and trained based on the PyTorch 2.4.0 deep learning framework. The underlying CUDA version was 11.8 to fully utilize the parallel computing capabilities of the GPU.
[0067] To ensure the fairness and consistency of the comparative experimental results, all experiments were conducted with the same initial parameter configuration and without introducing any form of pre-training weights. This ensures that performance differences stem solely from the model structure and improvement methods themselves, rather than external factors. This experimental setup provides reliable comparability and persuasiveness for subsequent ablation and comparative experiments.
[0068] Table 1. Introduction to Model Parameters Epochs 300 Batch size 32 Learning rate 0.01 Optimizer SGD IoU 0.7 4.5 Comparative Experiment To further verify the effectiveness and robustness of the proposed improved algorithm in ampoule bottle defect detection, this paper selects several representative models in the current object detection field for comparative experiments, including YOLOv5, YOLOv8, YOLOv9, YOLOv10, YOLOv11, YOLOv12, YOLOv13, and RT-DETR. These models are all advanced algorithms in the object detection field in recent years, possessing high detection accuracy, excellent generalization ability, and strong adaptability, and have been widely used in various industrial inspection scenarios. Among them, YOLOv5 and YOLOv8, as benchmark models widely used in most defect detection fields, have good real-time detection performance in actual production environments; YOLOv9 to YOLOv13 represent the latest advancements in the YOLO series, achieving significant improvements in overall performance; RT-DETR, as a representative Transformer-based model, provides a detection paradigm different from traditional CNN architectures for comparison.
[0069] Introduction to the comparative model: YOLOv5: As a representative model of single-stage detectors, YOLOv5 achieves a good balance between detection speed and accuracy. With its lightweight design and engineering advantages, it has been widely used in various industrial defect detection tasks, and therefore has high comparative reference value.
[0070] YOLOv8: As an upgraded version of the YOLO series, YOLOv8 has made systematic improvements in feature extraction, network structure and training strategy. It performs better in small object detection and complex scene adaptability, and is suitable as a strong baseline model for evaluating defect detection performance.
[0071] YOLOv9: YOLOv9 introduces the PGI and GELAN architectures, and by optimizing the gradient information flow and feature aggregation methods, it significantly improves the detection accuracy of small targets and complex scenes while maintaining efficient inference speed.
[0072] YOLOv10: It adopts a unified and efficient detection framework, and achieves structured optimization and redundancy suppression without post-processing (NMS-free), effectively balancing detection speed and accuracy, and further improving the real-time performance and stability of the model in industrial defect detection tasks.
[0073] YOLOv12: YOLOv12 is a real-time target detection model with attention mechanism at its core. By introducing Area Attention and efficient layer aggregation structure (R-ELAN), it significantly enhances the ability of feature extraction and multi-scale feature fusion, improving the recognition accuracy of defective targets while ensuring detection speed.
[0074] YOLOv13: Building upon YOLOv12, YOLOv13 introduces the HyperGraph Enhancement Adaptive Association (HyperACE) mechanism and the Full-Process Feature Allocation (FullPAD) paradigm, which can capture higher-order feature dependencies and achieve more accurate semantic modeling and localization of complex defect targets.
[0075] RT-DETR: An end-to-end detection model based on Transformer, combining global modeling capabilities with high real-time performance. It excels in efficient feature modeling and accurate target localization, providing a novel paradigm for defect detection that differs from traditional CNNs.
[0076] By comparing the proposed algorithm with the aforementioned mainstream models, the overall performance of the improved algorithm in the ampoule bottle defect detection task can be comprehensively evaluated from multiple dimensions, including detection accuracy, recall, and detection speed. To ensure the fairness of the experiment, all models were trained and tested on the same dataset and in the same experimental environment. The experimental results are shown in the table. The comparative experimental results show that the proposed improved algorithm outperforms the mainstream models in the ampoule bottle defect detection task. The proposed method achieved optimal performance in terms of metrics and maintained high real-time performance in terms of detection speed, fully demonstrating its effectiveness and practical application value.
[0077] Table 2 presents quantitative benchmark tests of OCMW-YOLOv11n and several state-of-the-art (SOTA) object detection models. Experimental results confirm that the proposed method successfully achieves object optimization. The improved model achieves a maximum mAP of 95.6%, demonstrating its superior detection accuracy. Compared directly with the YOLOv11n base model, it shows simultaneous enhancements in all key metrics: precision, recall, and mAP improve by 4%, 2%, and 4%, respectively. Despite the small number of parameters (… While the computational complexity has improved somewhat, it can still meet the deployment requirements of actual production tasks.
[0078] Table 2 Comparative test results Model P R <![CDATA[mAP 50 ]]> Parameters GFloaps RE-DETR 0.888 0.897 0.907 31.98 103.4 YOLOv5 0.928 0.93 0.931 2.2 6.0 YOLOv8 0.932 0.934 0.937 2.7 6.8 YOLOv9 0.926 0.919 0.918 1.73 6.4 YOLOv10 0.883 0.847 0.914 2.7 8.2 YOLOv11 0.877 0.911 0.916 2.6 6.3 YOLOv12 0.923 0.907 0.939 2.5 6.2 YOLOv13 0.894 0.852 0.926 2.45 6.2 Ours 0.92 0.93 0.956 3.5 7.8 Figure 8 , Figure 9 The precision and recall curves shown visually demonstrate the overall detection performance of OCMW-YOLOv11n, achieving the highest accuracy. Compared to YOLOv11n, it exhibits a better precision-recall balance and has a significant advantage in the carbon category.
[0079] Figure 10 , Figure 11The results show the actual performance of the improved model on the same dataset before and after testing. As shown in the figure, the improved model significantly improves the detection performance of ampoule bottle head defects. The comparative experimental results and model detection results above demonstrate that the improved model presented in this paper is of great significance in the field of ampoule bottle head defect detection, and basically meets the requirements for high-precision and high-efficiency detection in product manufacturing scenarios.
[0080] Furthermore, this paper validates the model's generalization ability on a test set by using the weights obtained from training the aforementioned comparative models and the improved model in this paper for validation on the test set. The improved model in this paper also achieved the best mAP_50 metric, reaching 89.7%, which is 3.4% higher than the baseline model YOLOv11n's 86.3%.
[0081] Table 3. Experimental Results on the Test Set Model <![CDATA[mAP 50 ]]> RE-DETR 0.772 YOLOv5 0.822 YOLOv8 0.849 YOLOv9 0.887 YOLOv10 0.794 YOLOv11 0.863 YOLOv12 0.88 YOLOv13 0.839 Ours 0.897 4.6 Ablation Experiment In this experimental section, systematic ablation experiments were conducted on the proposed key improvement strategies—the C3K2-ODConv module, the MFFM module, the CARAFE upsampling operator, and the WIoU loss function—to verify the effectiveness and contribution of each improvement method to the overall improvement in detection performance. The experimental results are shown in Table 5. In the table, "-" indicates that the corresponding module was not introduced, and the model retains its original structure; while "√" indicates that the module has been enabled and integrated into the model. By comparing the experimental results under different combinations, the independent role of a single module and the impact of the synergistic effect of multiple modules on the final performance of the model can be intuitively evaluated, thus providing strong support for the rationality and effectiveness of the improvement methods.
[0082] Table 4 Ablation Experiment Results Base ODConv MFFM CARAFE WIoU P R <![CDATA[mAP 50 ]]> Parameters GFloaps √ - - - - 0.87 0.91 0.916 2.6 6.3 √ √ - - - 0.908 0.913 0.948 2.88 6.1 √ - √ - - 0.88 0.94 0.94 3 7.7 √ - - √ - 0.92 0.89 0.94 2.7 6.6 √ - - - √ 0.93 0.917 0.953 2.6 6.3 √ √ √ √ √ 0.92 0.93 0.956 3.5 7.8 The experimental results table shows the basic model. The value is 0.916. When only the ODConv module is included, The accuracy improved to 0.948, with slight increases in both precision and recall, indicating that ODConv effectively enhances feature extraction capabilities. After introducing the MFFM module, The recall rate improved to 0.94, reaching 0.94, indicating that this module has a positive effect on the fusion of multi-scale features. When the CARAFE module is introduced into the base model, The value reached 0.94, and the P-value reached 0.92, indicating that it helps to improve the attentional representation of features.
[0083] In the loss function section, after introducing WIoU, The loss function was further improved to 0.953, with P and R reaching 0.93 and 0.917 respectively, indicating that this loss form can better optimize the localization accuracy of the detection boxes. Finally, when the four modules are used together, the model achieves [performance / performance / etc.]. The highest performance was achieved at 0.956, a 4 percentage point improvement over the base model. Although the number of parameters and FLOPs increased slightly (from 2.6M to 3.3M, and FLOPs from 6.3G to 7.8G), the overall detection performance was significantly improved, validating the effectiveness and rationality of the design of each module.
[0084] 5. Conclusion In summary, this paper proposes a novel model, OCMW-YOLOv11n, for detecting ampoule bottle head defects. This model aims to address challenges such as low accuracy in recognizing small carbonized targets in ampoule bottle head images, significant variations in head defects, and feature loss during the feature processing stage of benchmark models. First, this paper designs the C3K2-ODConv module to more effectively extract ampoule bottle head defect features. Second, this paper proposes a multi-scale feature fusion module, MFFM, which fully integrates feature information from different scales during the model's feature fusion stage, compensating for lost feature information to some extent, enhancing the model's ability to detect targets at different scales, and contributing to the accurate identification of small targets. Next, this paper introduces the CARAFE upsampling operator. Finally, this paper adjusts the loss function to WIoU v3, enabling the model to consider the impact of low-quality images on the loss. Compared with other target detection models, this method achieves higher accuracy in detecting ampoule bottle head defects.
[0085] Despite these encouraging results, further research is necessary, primarily focusing on the model's stability under a wider range of production conditions and defect types. Future research will concentrate on two directions: First, collecting a broader collection of higher-quality, more diverse ampoule bottle head defect images to further improve the generalization ability of the OCMW-YOLOv11n model, making it applicable to a wider range of detection environments. Second, while ensuring the model's detection accuracy, achieving maximum model lightweighting to improve deployment and detection efficiency.
[0086] A computer-readable storage medium storing a plurality of instructions adapted for loading and execution by a processor of a terminal device, the aforementioned method for detecting ampoule bottle head defects based on multi-scale feature fusion.
[0087] A terminal device includes a processor and a computer-readable storage medium, the processor being configured to implement various instructions; the computer-readable storage medium being configured to store multiple instructions adapted for loading and execution by the processor of the aforementioned method for detecting ampoule bottle cap defects based on multi-scale feature fusion.
[0088] The above are all preferred embodiments of the present invention and are not intended to limit the scope of protection of the present invention. Therefore, all equivalent changes made in accordance with the structure, shape and principle of the present invention should be covered within the scope of protection of the present invention.
Claims
1. A method for detecting defects in ampoule bottle heads based on multi-scale feature fusion, characterized in that, include: Acquire image data of the ampoule bottle head; Image preprocessing is performed on the acquired ampoule bottle head image data; A multi-scale feature fusion ampoule bottle head defect detection model OCMW-YOLOv11n based on YOLOv11n is constructed. The model backbone is introduced into the full-dimensional dynamic convolution ODConv and integrated into the C3K2 module. Contextual information is fused at the model neck using the multi-scale feature fusion module MFFM. CARAFE upsampling is used to replace the neck upsampling stage. The WIoU loss function is employed to improve the model's localization accuracy in detecting defects in small targets. Train the constructed OCMW-YOLOv11n model; The trained model was used to detect defects in ampoule bottle heads, and the detection results were evaluated.
2. The method for detecting ampoule bottle head defects based on multi-scale feature fusion according to claim 1, characterized in that, The image preprocessing of the acquired ampoule bottle head image data includes, at the input end, using Mosaic data augmentation to preprocess the image, generating new training samples by randomly cropping an image and stitching it with another randomly selected image, enriching background information according to batch size, increasing the number of small targets, and improving the diversity of the dataset and the robustness of the model.
3. The method for detecting ampoule bottle head defects based on multi-scale feature fusion according to claim 2, characterized in that, The process involves introducing the full-dimensional dynamic convolution ODConv into the core of the model and integrating it into the C3K2 module. This includes incorporating ODConv into the C3K2 module to replace the standard convolution operator Conv in Bottleneck, ultimately constructing a C3K2-ODConv module. This module dynamically adjusts the convolution kernel weights through a multi-dimensional attention mechanism, allowing the convolution operator to adaptively update as the input features change. ODConv learns attention weights in parallel across the four dimensions of the convolution kernel. First, ODConv introduces four lightweight attention modules for each convolution kernel, corresponding to the four dimensions respectively. Then, dynamic weight generation is performed. For a standard convolutional layer, dynamic weights are generated using ODConv, expressed as follows: , in: It is a basic static convolution kernel. These are the attention scalars learned along the spatial dimension, input channel dimension, output channel dimension, and convolutional kernel dimension, respectively. This indicates element-wise multiplication. Finally, the core of the model is introduced into the full-dimensional dynamic convolution ODConv and integrated into the C3K2 module. It also includes an improvement to the Bottleneck structure in the C3K2 module. The second 3×3 standard conv in the two standard conv convolutions contained in Bottleneck is replaced with an ODConv layer of the same kernel size, so that when information undergoes non-linear transformation within Bottleneck, adaptive feature extraction is performed through ODConv.
4. The method for detecting ampoule bottle head defects based on multi-scale feature fusion according to claim 3, characterized in that, The method described above integrates contextual information in the model neck region using the Multi-Scale Feature Fusion Module (MFFM). This addresses the issue of inaccurate spatial information handling in the YOLOv11 neck network, which employs Path Aggregation Network (PAN) and Feature Pyramid Network (FPN) structures. The MFFM module enhances the model's feature extraction capabilities across different scales and contexts by fusing shallow, corresponding, and deep feature maps. The MFFM architecture employs a hierarchical processing approach, integrating multi-scale features through four stages: spatial alignment, channel unification, adaptive weighted fusion, and multi-receptive field enhancement. The input feature map comprises three scales: low-level features C1, mid-level features C2, and high-level features C3. First, the shallow feature map is downsampled, and the deep feature map is upsampled to obtain three feature maps with the same resolution. Then, pointwise convolution is performed to unify the channel dimensions, as shown below: , , After processing, the three features are unified to dimensions B×Cm×H×W. Then, attention weights are generated by using three independent 1×1 convolutions and CBAM attention modules to process the three features separately, generating attention weight feature maps for each feature. These three weight feature maps are then concatenated, and the normalized weights for the three features are obtained through a fusion convolution, layer normalization, and softmax function. , , , The weighted fusion process is represented as follows: , Where W = [w1, w 2, [w3] represents the normalized weight coefficients. Feature fusion based on attention weights is as follows: , in It is the residual of the corresponding layer feature map, and finally the feature representation capability is improved through the multi-receptive field enhancement module: 。 5. The method for detecting ampoule bottle head defects based on multi-scale feature fusion according to claim 4, characterized in that, The replacement of the model's neck upsampling stage with CARAFE upsampling involves the model using the CARAFE upsampling operator to adaptively utilize neighborhood feature information to generate high-resolution feature maps through a content-aware feature recombination mechanism. The CARAFE upsampling process consists of two key components: first, an upsampling kernel prediction module performs channel compression on the input feature map to reduce computational cost; then, a convolutional layer predicts a target location for each upsampling target. The convolution kernel, which is generated based on local regions of the input feature map ( Then, the content-aware feature reorganization module uses the predicted upsampling kernel to perform weighted reorganization on the corresponding local regions in the input feature map to generate a high-resolution output feature map. Based on CARAFE, upsampling is adaptively performed according to the local content of the image: in edge and texture-rich regions, a sharp kernel is generated to preserve details; in smooth regions, a smooth kernel is generated to avoid introducing noise.
6. The method for detecting ampoule bottle head defects based on multi-scale feature fusion according to claim 5, characterized in that, The method of using the WIoU loss function to improve the localization accuracy of the model in small target defect detection includes optimizing the bounding box regression loss function, introducing the WioU v3 loss based on a dynamic non-monotonic focusing mechanism, comprehensively considering the overlapping area, center point distance, and aspect ratio, and suppressing gradient competition imbalance between high and low IoU samples. IoU is expressed as: In this equation, the numerator represents the intersection area of the predicted bounding box and the ground truth bounding box, and the denominator is the union area of the two. Based on this, CIoU introduces additional constraints on center point distance and aspect ratio during the optimization process. Its loss function is expressed as: , Where ρ(bp,bg) is the Euclidean distance between the center points of the predicted box and the ground truth box, c is the length of the diagonal of the minimum bounding rectangle containing the two boxes, and αv is used to measure the consistency of aspect ratio. Then, the outlier measure is constructed using the WIoUv3 form and WioU v1, and the gradient gain is dynamically adjusted to achieve differentiated focusing on samples of different quality.
7. The method for detecting ampoule bottle head defects based on multi-scale feature fusion according to claim 6, characterized in that, The method employs WIoUv3 and WioU v1 to construct an outlier metric and dynamically adjusts the gradient gain to achieve differentiated focusing on samples of different quality. This includes firstly constructing a loss function that emphasizes distance metrics using WIoU v1: , in and These are the center coordinates of the predicted bounding box and the ground truth bounding box, respectively. and These are the width and height of the smallest bounding rectangle. The normalization coefficients are then used, and a dynamic non-monotonic focusing mechanism is introduced in WIoU v3 to construct an outlier factor. To assess sample quality, it is represented as: , in ,and It is within the current sliding window The mean; finally, the WIoUv3 loss function is defined as: , To and The relevant gradient gain coefficients, when High time, For high-quality samples, the gradient gain is large; conversely, for low-quality samples, a small gradient gain is assigned.
8. The method for detecting ampoule bottle head defects based on multi-scale feature fusion according to claim 7, characterized in that, The training of the constructed OCMW-YOLOv11n model includes setting the number of training epochs to 300, the initial learning rate to 0.01, using the SGD optimizer, and setting the batch size to 32. Specifically, the image is input into the model network, and through the feature processing stages of feature extraction and feature fusion, three feature maps of different scales, P3, P4, and P5, are obtained. These feature maps are then passed to the detection head to enable the model to locate and classify defects in the image. The overall loss is obtained by measuring the predicted bounding box and the ground truth bounding box, and the network parameters are updated through backpropagation to reduce the overall loss. After 300 epochs of training, the model converges as much as possible to obtain the optimal weights.
9. The method for detecting ampoule bottle head defects based on multi-scale feature fusion according to claim 8, characterized in that, The method of using the trained model to detect defects in ampoule bottle heads and evaluating the detection results includes quantitative evaluation of the model based on evaluation metrics, including precision P, recall R, computational complexity, mean precision mAP, and number of parameters. Among them, precision P is used to calculate the proportion of samples that are actually positive samples among those predicted as positive samples, and is expressed as: Recall R is used to calculate the proportion of all true positive samples that are correctly predicted by the model, and is expressed as: AP is obtained by plotting the precision-recall curve and calculating the area under the curve, and is represented as: Then, take the average of the AP values for all categories to get: , where TP, FP, and FN are the number of true positives, false positives, and false negatives, respectively, and N is the number of categories.
10. A defect detection system for ampoule bottle heads based on multi-scale feature fusion, characterized in that, include: The data acquisition module is configured to acquire image data of the ampoule bottle head; The preprocessing module is configured to perform image preprocessing on the acquired ampoule bottle head image data; The model building module is configured to construct an OCMW-YOLOv11n multi-scale feature fusion ampoule bottle head defect detection model based on YOLOv11n. Specifically, the model backbone is introduced into the full-dimensional dynamic convolution ODConv and integrated into the C3K2 module; context information is fused at the model neck using the multi-scale feature fusion module MFFM; CARAFE upsampling is used to replace the model neck upsampling stage; and the WIoU loss function is employed to improve the model's localization accuracy in small target defect detection. The model training module is configured to train the constructed OCMW-YOLOv11n model. The detection module is configured to use a trained model to detect defects in ampoule heads and evaluate the detection results.
Citation Information
Patent Citations
High-precision intelligent detection method for surface defects of bearing ring
CN116645328A
End-to-end infrared small target detection method based on Transform decoder network
CN118505965A
BFS product detection method and system
CN119000713A
Penicillin bottle body defect detection method based on improved YOLOv8
CN120318607A
Vehicle detection method in severe weather
CN120375301A
Cited By
Underwater sonar image target detection method and device
CN121746898A
Trademark defect detection method based on improved YOLOv11 and generative network
CN122023414A