A medicine packaging defect detection method and device based on an improved YOLOv11 model
Patent Information
- Application Number
- CN202511358589.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-23
- Publication Date
- 2026-08-21
- Estimated Expiration
- 2045-09-23
AI Technical Summary
但仍然存在以下问题:(1)局部特征不够突出,难以捕捉微小特征;(2)容易受到环境背景噪声干扰,难以突出缺陷特征;(3)特征融合模块特征融合不充分,多尺度目标检测性能受限;(4)下采样过程易出现特征丢失,目标检测精度不足
[0113]1、提高局部特征融合能力:与传统的YOLOv11模型相比,本发明该进的YOLOv11模型显著提高了局部特征融合能力,本发明在骨干网络中使用创新性的SPD-Conv模块代替原始骨干网络中的Conv模块,扩大了感受野,增强了图像的细粒度特征,避免了传统池化操作中的信息丢失。同时,在C2PSA模块中融合了多尺度扩张注意力(MSDA)模块,MSDA考虑了自注意力机制的局部性和稀疏性,以扩大的方式减少自我注意力的冗余;
Smart Images

Figure CN121190437B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a method and apparatus for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model, belonging to the fields of industrial automation defect detection and computer vision technology. Background Technology
[0002] Against the backdrop of continuously growing pharmaceutical consumption, the demand for efficient and high-volume pharmaceutical product defect detection devices is also constantly increasing. Compared to the traditional method of decocting Chinese medicine, the method of grinding Chinese medicine into powder and packaging it into bags for ready-to-eat consumption is highly favored. However, in the pharmaceutical packaging process, some companies still use manual sorting to remove defective bags of medicine, resulting in high production costs, low efficiency, insufficient industrial automation, and the current limitations of real-time performance and accuracy of defect detection devices. Therefore, addressing the technical challenges of pharmaceutical packaging defect detection has become a key factor in the Chinese pharmaceutical market. Effectively improving pharmaceutical packaging defect detection technology and increasing pharmaceutical production capacity is of paramount importance.
[0003] Current methods for detecting drug defects mainly fall into two categories: traditional manual inspection and machine vision-based inspection methods using deep learning. Currently, machine vision-based inspection technology has been applied in many industrial scenarios to replace manual inspection. This invention improves the defect detection field in the drug packaging process using deep learning-based computer vision inspection technology, proposing specific improvements to enhance pharmaceutical product output efficiency and pharmaceutical product defect detection technology.
[0004] Currently, deep learning-based surface defect detection technologies are mainly divided into two categories: one is based on two-stage object detection, such as R-CNN, Faster R-CNN, and Mask R-CNN; the other is based on single-stage object detection, such as the SSD series, RetinaNet, and YOLO series. Two-stage object detection models achieve high-precision detection through candidate region generation and precise localization, but due to their large computational load, slow inference speed, and weak real-time performance, they are not suitable for industrial inspection scenarios. In contrast, single-stage object detection models, through an end-to-end approach, do not require candidate region generation, and their simple structure makes them suitable for industrial applications. However, they still have the following problems: (1) local features are not prominent enough, making it difficult to capture minute features; (2) they are easily affected by environmental background noise, making it difficult to highlight defect features; (3) the feature fusion module does not fully fuse features, limiting the performance of multi-scale object detection; (4) feature loss is prone to occur during the downsampling process, resulting in insufficient object detection accuracy. Summary of the Invention
[0005] To address the shortcomings of the existing technologies, this invention proposes a method and apparatus for detecting drug packaging defects based on an improved YOLOv11 model. This method introduces the SPD-WTConv module, optimizes feature extraction, replaces the downsampling mechanism, and expands the probe module, thereby improving the detection efficiency of drug packaging defects.
[0006] The technical solution of this invention is: a method for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model, the method comprising:
[0007] Step 1: Collect images of defects in drug packaging to form an image dataset;
[0008] Step 2: Label the location, size, and category of defects in any image in the image dataset to obtain the location, size, and category labels of defects in any image;
[0009] Step 3: Preprocess the labeled image dataset, and then divide the preprocessed image dataset into training set, validation set and test set;
[0010] Step 4: Construct a drug packaging defect detection model based on the improved YOLOv11;
[0011] Step 5: Train the improved YOLOv11 drug packaging defect detection model using the training set, continuously evaluate the model performance during training using the validation set, and test it using the test set to obtain the model weight file with the best test results.
[0012] Step 6: Perform sparse training on the trained model weight file, iteratively prune the model, and output a lightweight weight file for the improved YOLOv1n drug packaging defect detection model.
[0013] Step 7: Deploy the lightweight weight file of the improved YOLOv11 drug packaging defect detection model into the inference model to build the final improved YOLOv11 drug packaging defect detection model. Use this model to detect the location, size, and confidence level of drug packaging defects and output the defect detection results.
[0014] Furthermore, in step 2, the categories of defects include: surface scratches, gaps in the medicine bag, poor heat sealing, blister pack damage, material aging, and blurred lettering;
[0015] The location and size of the defect. The calculation formula is as follows:
[0016]
[0017] in, Indicates the i-th defect in any image The center point of the actual bounding box where the location is located; Indicates the i-th defect in any image The width of the actual bounding box where the object is located; Indicates the i-th defect in any image The height of the actual bounding box at the location in the middle.
[0018] Furthermore, in step 3, the preprocessing includes normalizing the labeled image dataset; the preprocessing also includes dataset augmentation, which involves expanding the dataset by flipping, rotating, stitching, and translating to obtain an augmented dataset.
[0019] Furthermore, in step 4, the improved YOLOv11 drug packaging defect detection model improves the backbone network module, neck network module, and head detection head module included in the original YOLOv11 network model as follows:
[0020] (1) Replace the traditional convolution Conv in the backbone network module with SPD large receptive field wavelet convolution SPD-WTConv. The SPD-WTConv is a novel convolution operation module consisting of SPD and WTConv layers. The SPD layer reconstructs the spatial blocks of the input feature map to the depth dimension, increases the number of channels, and reduces the spatial resolution. WTConv is an innovative convolutional neural network layer that achieves deep and multi-scale analysis of the input data through wavelet transform technology, providing a larger receptive field for the convolutional neural network CNN.
[0021] (2) Improve the C2PSA module in the backbone network module to the C2PSA-MSDA module, replace the autonomous intention module in the C2PSA module with the multi-scale expanded attention module, replace the self-attention module with the multi-scale expanded attention module, set different expansion rates for different heads, extract multi-scale features, and thus realize multi-scale representation capability.
[0022] (3) In the neck network module, the Haar wavelet downsampling module is used to optimize the downsampling part of the YOLOv11 model. HWD explicitly increases the number of channels of the feature map and uses Haar wavelet transform to reduce its resolution. Then, convolution operation is used to learn representative features to filter redundant information.
[0023] (4) The Head part of the detection head module adopts the dynamic detection head DyHead. DyHead combines scale awareness, spatial awareness and task awareness attention mechanisms. By adding attention mechanism to the feature tensor dimension, the feature representation capability can be enhanced.
[0024] Furthermore, in step 4, the construction of the improved YOLOv11 drug packaging defect detection model includes, in sequence: a feature extraction network, a feature fusion network, and a dynamic detection head network;
[0025] 4.1 The feature extraction network includes one convolutional module (Conv), four SPD-WTConv convolutional modules, four C3K2 feature extraction modules, one fast spatial pyramid (SPPF) module, and a feature enhancement (C2PSA) module; wherein, one SPD-WTConv convolutional module and one C3K2 feature extraction module constitute a local feature extraction unit; for the i-th feature map The process yields a feature set consisting of n coarsely extracted features. ;
[0026] 4.2 The structure of the feature fusion network consists of a bottom-up upsampling module and a top-down downsampling module concatenated in reverse; the feature fusion network includes 4 feature fusion units, which process the i-th feature map. The feature set consisting of n coarsely extracted features is input into a feature fusion network to perform feature fusion and obtain the first fused feature. Second fusion feature The third fusion feature Fourth fusion feature ;
[0027] 4.3 The dynamic detection head network processes features in three dimensions—L, S, and C—using attention mechanisms in these three dimensions respectively. , , , The i-th feature map is obtained through processing. Category prediction labels and position size labels .
[0028] Furthermore, the processing flow of the feature extraction network includes:
[0029] Step 4.1.1, the convolution module Conv... Performing a convolution operation yields a size of initial convolutional features Where M is The width, N is of high;
[0030] Step 4.1.2, The feature set is obtained by sequentially processing n local feature extraction units, which correspond to the coarsely extracted features. ;
[0031] The operation process of the SPD-WTConv convolution module in the local feature extraction unit includes:
[0032] Input the i-th feature map The input features are decomposed into high-frequency and low-frequency features using the Haar wavelet transform (WT):
[0033]
[0034] in, For a low-pass filter, It is a set of high-pass filters; the convolution output has four channels, each channel has resolution :
[0035]
[0036] in, yes The low-frequency components, These are its horizontal, vertical, and diagonal high-frequency components:
[0037] The features are input into downsampling, and then low-kernel depthwise convolutions are performed at different frequencies:
[0038]
[0039] Where Y represents the features output by the small kernel depthwise convolution, Q is the input tensor, and W is... The weight tensor of a deep kernel, For convolution operations, this step is used to separate the convolutions between frequency components and also increases the receptive field. For inverse wavelet transform, WT Wavelet transform;
[0040] The features of different frequencies are processed, and the results are weighted and summed. The input features are then transformed using inverse wavelet transform (IWT). The output is :
[0041]
[0042] Since equation (1) forms an orthogonal normalized basis, the transpose convolution operation can be performed... Obtain inverse wavelet features ;
[0043] The cascaded wavelet decomposition is given by recursively decomposing the low-frequency components:
[0044]
[0045] in, i2 is the current level, which increases the resolution of low-frequency frequencies and decreases the spatial resolution;
[0046] After that, Decomposed into multiple sub-feature maps:
[0047]
[0048] Wherein, sub-mapping Including all elements , ( , ) represents an element At a specific location in a two-dimensional plane Indicates step size, sub-mapping The number depends on The size of the step, equation (6) is based on Taking 2 as an example, [,]express The width and height values range from 0 to 10. The set of four sub-maps is obtained by downsampling the original feature map using a scaling factor of 1 / 2. , , , ; , express The width and height in the (x,y) dimension, where x and y take values of 0 and 1 in the above formula;
[0049] The SPD layer maps these sub-features onto the channel dimension and concatenates them to generate new feature representations. This reduces the spatial dimension by an order of magnitude:
[0050]
[0051] in, Indicates the number of channels;
[0052] After the SPD feature transformation layer, when At that time, a convolutional layer with a stride of 1 is introduced to obtain the feature representation. :
[0053]
[0054] in, The number of channels obtained by introducing a convolutional layer with a stride of 1;
[0055] Step 4.1.3: Input the coarsely extracted features obtained from the nth local feature extraction unit into the Fast Spatial Pyramid Pooling (SPPF) layer for feature enhancement to obtain the enhanced features. ;
[0056] Step 4.1.4, then, Further enhancement is achieved by entering the C2PSA-MSDA module, resulting in enhanced features. The operating principle of the C2PSA-MSDA module includes:
[0057] right conduct The convolution operation divides the channel into two equal parts. and ;
[0058] Then The number of channels is divided into ,get and , The input is the PSA-MSDA module, which consists of a multi-scale extended attention MSDA module and a feedforward network FFN module;
[0059] Part The feature map is input into the MSDA module. First, the channel is divided into different heads, and different dilation rates are used in different heads. Then, a sliding window expansion attention (SWDA) operation is performed in each head.
[0060] The Sliding Window Expanded Attention (SWDA) is described as follows:
[0061]
[0062] in, for Output features: Q, K, and V represent the query, key, and value matrix, respectively, and r represents the expansion rate, which controls the sparsity.
[0063] For position ( , Output Corresponding components :
[0064]
[0065] Where H and W2 represent the height and width of the feature map, and This represents the keys and values selected from the feature map. Indicates the position ( , The query vector at () express Scaling factor for vector dimension This represents the attention mechanism. express Function; for position ( , ) query at, select a set of coordinates Self-attention is performed on the keys and values at the specified locations:
[0066]
[0067] in, , This indicates the coordinate offset of the window relative to the query. Indicates the size of the sliding window;
[0068] Then, the different features are concatenated together and fed into a linear layer:
[0069]
[0070]
[0071] in, As the expansion rate of the i3th head, Represents the feature map slice of the i3th head, output connect After being combined, it is sent to the linear layer. Perform feature aggregation to obtain aggregated features G;
[0072] Processed by the MSDA module and Feature fusion to form part , Features are obtained after processing by a feedforward network (FFN). ;
[0073] The output feature of the nth PSA-MSDA module ,Will and Features are obtained by performing a concat operation. .
[0074] Furthermore, the feature fusion network performs feature fusion to obtain a first fused feature. Second fusion feature The third fusion feature Fourth fusion feature Specifically, it includes:
[0075] Step 4.2.1: The first fused feature is extracted from the coarse features. and the third convolution module The features obtained from the third upsampling module and the C3K2 feature extraction module are combined; the coarsely extracted features are then processed. Features obtained from the third convolutional module The fused features are obtained after processing by the third upsampling module. , The first fused feature is obtained after feature extraction by the C3K2 feature extraction module. ;
[0076] Step 4.2.2: The second fused feature is generated by the third convolutional module. First downsampling module The features obtained from the C3K2 feature extraction module are combined; the features are then... Features obtained from the first downsampling module The splicing results in fusion features , The second fused feature is obtained after feature extraction by the C3K2 feature extraction module. ;
[0077] Step 4.2.3: The third fusion feature is generated by the second convolutional module. Second downsampling module The features obtained from the C3K2 feature extraction module are combined with the features obtained from the second convolution module. The obtained features Features obtained by the second downsampling module The splicing results in fusion features , The third fused feature is obtained after feature extraction by the C3K2 feature extraction module. ;
[0078] Step 4.2.4, the fourth fusion feature is composed of features and the third downsampling module The features obtained from the C3K2 feature extraction module constitute the feature composition; the features are then processed. Features obtained from the third downsampling module The splicing results in fusion features , The fourth fused feature is obtained after feature extraction by the C3K2 feature extraction module. ;
[0079] The downsampling module operates as follows:
[0080] The Haar wavelet downsampling module (HWD) consists of two modules: (1) a lossless feature encoding module and (2) a feature representation learning module. The lossless feature encoding module is responsible for transforming features and reducing spatial resolution. The feature representation learning module is used to extract discriminative features.
[0081] The lossless feature encoding module utilizes a Haar wavelet transform layer to reduce the spatial resolution of the feature map, wherein the wavelet basis functions of the first-order one-dimensional Haar transform are... and scaling function The formula is as follows:
[0082]
[0083] in, The definition is as follows:
[0084]
[0085] The first-order Haar transform is represented using the 0th-order Haar basis functions:
[0086]
[0087] When the Haar wavelet transform is applied to process two-dimensional signals of image features, four components are generated, each with the spatial resolution of the original two-dimensional signal of image features. ; This represents the position index used to define wavelet functions at different scales and directions during the Haar wavelet transform process, where j2 and k1 represent the stage and order of the Haar basis functions, respectively.
[0088] The four components generated by the lossless feature encoding module are input into the feature representation learning module to extract discriminative features, resulting in a set of i4 downsampled features. .
[0089] Furthermore, the processing flow of the dynamic detection head network includes:
[0090] Step 4.3.1: The dynamic head DyHead in the dynamic head network transforms the attention function into three sequential attentions, each focusing on a viewpoint:
[0091]
[0092] in, , and These are attention functions applied to three different dimensions: scale dimension L, spatial dimension S, and task dimension C; F is a given feature tensor.
[0093] Apply a scale-aware attention module along the L dimension to fuse features at different scales based on semantic importance:
[0094]
[0095] in, It is a by A linear function approximated by a convolutional layer;
[0096] Applying a spatially aware attention module along the S dimension focuses on the discriminative region where spatial location and feature level always coexist:
[0097]
[0098] Where K is the number of sparse sampling locations. It is a self-learning space offset movement position It is a location The importance scalar of self-learning at a given location This indicates the number of levels in the pyramid. Indicates the first The weight value of the k-th spatial location in the feature map of the layer. Indicates at the level Position after offset ,aisle Eigenvalue functions at;
[0099] In dimension C, a task-aware attention module is applied to dynamically switch the ON and OFF channels of features to support different tasks:
[0100]
[0101] in, It is a feature slice of the c-th channel. It is a hyperfunction that learns to control the activation threshold;
[0102] Step 4.3.2: Apply the above three types of attention in sequence, nesting them multiple times to combine multiple... , and They are stacked together to form a dynamic detection head.
[0103] Further, step 6 includes:
[0104] A layer-adaptive amplitude-based pruning LAMP was used to lightweight the improved YOLOv11-based drug packaging defect detection model.
[0105] LAMP evaluates the importance of each layer's connection based on the square of the weight magnitude, and determines the pruning strategy based on the LAMP score calculated for each layer; connections with low scores are removed during model pruning; LAMP score. The calculation formula is as follows:
[0106]
[0107] Where u and v represent the indices of the weight vector, and W[u] and W[v] represent the weight values of indices u and v of the weight vector, respectively. This represents the squared weight of u. This represents the sum of squares of the weights from the index u to the subsequent weights;
[0108] The weight tensors of each layer are converted into one-dimensional vectors for standardization. Then, the weights are sorted in ascending order according to the index mapping. The LMAP scoring criteria are shown in the following formula:
[0109]
[0110] According to formula (22), assuming Then the LAMP score This indicates that the model performs well with connection pairs that have high weights and are preserved during pruning.
[0111] The present invention also provides a pharmaceutical packaging defect detection device based on an improved YOLOv11 model, the device comprising: a module for performing the aforementioned pharmaceutical packaging defect detection method based on an improved YOLOv11 model.
[0112] The beneficial effects of this invention are:
[0113] 1. Improved Local Feature Fusion Capability: Compared with the traditional YOLOv11 model, the improved YOLOv11 model of this invention significantly improves the local feature fusion capability. This invention uses an innovative SPD-Conv module in the backbone network to replace the Conv module in the original backbone network, expanding the receptive field, enhancing fine-grained image features, and avoiding information loss in traditional pooling operations. Simultaneously, a multi-scale expanded attention (MSDA) module is fused into the C2PSA module. MSDA considers the locality and sparsity of the self-attention mechanism, reducing redundancy of self-attention in an expanded manner.
[0114] 2. The network structure of YOLOv11 has been optimized: a Haar wavelet downsampling module has been introduced, which increases the number of channels for feature mapping and replaces the traditional downsampling operation of the YOLOv11 model. This downsampling operation effectively solves the problem of loss of important feature information in the downsampling operation, improves the model's ability to detect defects of different sizes, and effectively solves the problem of inaccurate feature extraction.
[0115] 3. Precise Defect Location: In the backbone network, an innovative SPD-WTConv module is introduced to increase the receptive field and enhance fine-grained image features. In addition, the multi-head self-attention in C2PSA is replaced with multi-scale expanded attention (MSDA), which significantly enhances the feature extraction capability of the backbone network. Furthermore, this invention introduces a dynamic detection head (Dy Head), which transforms the attention function into three sequential attentions, each focusing on a viewpoint. This attention mechanism reduces computational complexity while fusing the three attention mechanisms at multiple scales, significantly improving the defect location accuracy.
[0116] 4. Lightweight Model: Compared with the traditional YOLOv11 model, this invention performs LAMP model pruning on the improved YOLOv11 model. This LAMP model pruning involves sparse training and model pruning, automatically replacing redundant neurons, and finally generating a lightweight weight file. This effectively reduces redundant information in the model, improves the model's detection performance, and makes the model more efficient and of higher quality. Attached Figure Description
[0117] The accompanying drawings are provided to further illustrate the invention and form part of the specification. They are used together with the embodiments of the invention to explain the invention and do not constitute a limitation thereof.
[0118] Figure 1 This is a schematic diagram of the method flow proposed in this invention;
[0119] Figure 2 This is a schematic diagram of the improved YOLOv11 overall network structure proposed in this invention.
[0120] Figure 3 This is a schematic diagram of the SPD-WTConv module structure proposed in the local feature extraction unit of this invention;
[0121] Figure 4 This is a schematic diagram of the C2PSA-MSDA module structure proposed in the local feature extraction unit of this invention;
[0122] Figure 5 This is a schematic diagram of the HWD module structure proposed in the neck network downsampling unit module of the present invention;
[0123] Figure 6 This is a schematic diagram of the dynamic detection head (Dy Head) module proposed in the detection head module of this invention. Detailed Implementation
[0124] The present invention will be further described below with reference to the accompanying drawings. It should be noted that the following embodiments are only used to illustrate the technical solutions of the present invention and should not be used to limit the scope of protection of the present invention.
[0125] Existing defect detection methods based on existing defect detection models suffer from problems such as weak detection capability for small targets, insufficient real-time target detection, and difficulty in accurately identifying and locating similar defect parts of the detected targets in complex industrial scenarios. This invention is proposed to address these issues.
[0126] This invention improves the YOLOv11 model by modifying its backbone, neck network, and detector head. It replaces the traditional Conv convolution in the backbone network with an SPD module and Large Perception Wavelet Convolution (SPD-WTConv), avoiding information loss in traditional stride convolution and pooling operations. The C2PSA module is improved using Multi-Scale Dilated Attention (MSDA), reducing computational redundancy and enhancing model learning ability and efficiency. A Haar wavelet downsampling module replaces the existing traditional downsampling convolution, reducing information loss during downsampling. To address the problem of complex, dense, and highly similar small targets, this invention uses a dynamic small target detection head to reduce environmental noise interference and enhance feature representation capabilities. Through this improved YOLOv11 model, the challenge of detecting defects with high complexity and similarity of small targets is effectively overcome, improving the robustness of image quality detection methods and increasing the detection speed and accuracy of the device.
[0127] The construction idea of this invention is as follows: obtain six types of drug packaging defect samples from pharmaceutical companies, preprocess the defective drug images to obtain a defect image dataset, preprocess the data images and divide the dataset into a training set, a validation set and a test set in a 7:2:1 ratio, input the training set into the improved YOLOv11 algorithm model of this invention for training to obtain the model weight file, then perform model pruning operation to sparse the model training, and continuously iterate to obtain the model weight file with the best test performance and deploy it to the inference module to perform the drug packaging defect detection task.
[0128] Example 1: As Figures 1-6 As shown, a method for detecting defects in drug packaging based on an improved YOLOv11 model is presented in this embodiment. A pharmaceutical manufacturing company is used as an example to demonstrate the application of this method. The specific steps are as follows:
[0129] Step 1: Collect images of drug packaging defects from a pharmaceutical manufacturing company using an industrial camera to create an image dataset IMD; let any image in the image dataset IMD be denoted as... The defective image has 3 input channels and a resolution of 640*640.
[0130] Step 2: For any image in the image dataset IMD The location, size, and category of n defects in the image are labeled to obtain an arbitrary image. The location, size, and category label of the defect; The Each defect label is recorded as ;make The size of the i-th defect is ;make The i-th defect category is ;
[0131] Furthermore, in step 2, the categories of defects include: surface scratches, gaps in the medicine bag, poor heat sealing, blister pack damage, material aging, and blurred lettering;
[0132] The location and size of the defect. The calculation formula is as follows:
[0133]
[0134] in, Indicates the i-th defect in any image The center point of the actual bounding box where the location is located; Indicates the i-th defect in any image The width of the actual bounding box where the object is located; Indicates the i-th defect in any image The height of the actual bounding box at the location in the middle.
[0135] Step 3: Preprocess the labeled image dataset IDM, and then divide the preprocessed image dataset into training and training sets. Validation set and test set ;
[0136] Furthermore, in step 3, the preprocessing includes normalizing the labeled image dataset; the preprocessing also includes dataset augmentation, which involves expanding the dataset by flipping, rotating, stitching, and translating to obtain an augmented dataset.
[0137] Step 4: Construct a drug packaging defect detection model based on the improved YOLOv11;
[0138] The original YOLOv11 network model mainly includes a backbone module, a neck module, and a head module. The backbone module consists of 5 standard convolutional Conv modules, 4 C3k2 modules, 1 SPPF module, and 1 C2PSA module. The backbone is the core feature extraction network of YOLOv11n, responsible for extracting multi-level semantic information from the input image, providing rich feature representations for subsequent object detection tasks. The neck module plays a role in multi-scale feature fusion and optimization in object detection tasks. By integrating feature maps from different levels, it enhances the model's ability to capture object details and organically integrates the backbone network layer and the detection layer. The head module is used to predict targets of different sizes on the feature map. The multi-scale feature map processed by the backbone and neck is converted into the final detection result. By integrating feature information at different scales, it achieves the localization and classification of various targets.
[0139] Furthermore, in step 4, the improved YOLOv11 drug packaging defect detection model improves the backbone network module, neck network module, and head detection head module included in the original YOLOv11 network model as follows:
[0140] (1) Replace the traditional convolution Conv in the backbone network module with SPD large receptive field wavelet convolution SPD-WTConv. The SPD-WTConv is a novel convolution operation module consisting of SPD and WTConv layers. The SPD layer reconstructs the spatial blocks of the input feature map to the depth dimension, increases the number of channels, and reduces the spatial resolution. WTConv is an innovative convolutional neural network layer that achieves deep and multi-scale analysis of the input data through wavelet transform technology, providing a larger receptive field for the convolutional neural network CNN.
[0141] (2) The C2PSA module in the backbone network module is improved to the C2PSA-MSDA module. The autonomous attention module in the C2PSA module is replaced with the multi-scale expanded attention (MSDA) module. The self-attention module in the C2PSA module lacks cross-head information interaction, which may limit the feature fusion effect. Moreover, it has high requirements for the diversity and labeling quality of training data. This invention proposes to replace the self-attention module with the multi-scale expanded attention module, set different expansion rates for different heads, extract multi-scale features, thereby achieving multi-scale representation capability without additional parameters and computational cost.
[0142] (3) In the neck network module, the Haar wavelet downsampling module (HWD) is used to optimize the downsampling part of the YOLOv11 model. HWD explicitly increases the number of channels of the feature map and uses Haar wavelet transform to reduce its resolution. Then, convolution operation is used to learn representative features to filter redundant information.
[0143] (4) The Head part of the detection head module adopts the dynamic detection head DyHead. DyHead combines scale awareness, spatial awareness and task awareness attention mechanisms. By adding attention mechanism to the feature tensor dimension, the feature representation capability can be enhanced.
[0144] Furthermore, in step 4, the construction of the improved YOLOv11 drug packaging defect detection model includes, in sequence: a feature extraction network, a feature fusion network, and a dynamic detection head network;
[0145] 4.1 The feature extraction network includes one convolutional module (Conv), four SPD-WTConv convolutional modules, four C3K2 feature extraction modules, one fast spatial pyramid (SPPF) module, and a feature enhancement (C2PSA) module; wherein, one SPD-WTConv convolutional module and one C3K2 feature extraction module constitute a local feature extraction unit; for the i-th feature map The process yields a feature set consisting of n coarsely extracted features. ;
[0146] 4.2 The structure of the feature fusion network consists of a bottom-up upsampling module and a top-down downsampling module concatenated in reverse; the feature fusion network includes 4 feature fusion units, which process the i-th feature map. The feature set consisting of n coarsely extracted features is input into a feature fusion network to perform feature fusion and obtain the first fused feature. Second fusion feature The third fusion feature Fourth fusion feature ;
[0147] 4.3 The dynamic detection head network processes features in three dimensions—L, S, and C—using attention mechanisms in these three dimensions respectively. , , , The i-th feature map is obtained through processing. Category prediction labels and position size labels .
[0148] Furthermore, the processing flow of the feature extraction network includes:
[0149] Step 4.1.1, the convolution module Conv... Performing a convolution operation yields a size of initial convolutional features Where M is The width, N is of high;
[0150] Step 4.1.2, The feature set is obtained by sequentially processing n local feature extraction units, which correspond to the coarsely extracted features. ;
[0151] The operation process of the SPD-WTConv convolution module in the local feature extraction unit includes:
[0152] Input the i-th feature map The input features are decomposed into high-frequency and low-frequency features using the Haar wavelet transform (WT):
[0153]
[0154] in, For a low-pass filter, It is a set of high-pass filters; the convolution output has four channels, each channel has resolution :
[0155]
[0156] in, yes The low-frequency components, These are its horizontal, vertical, and diagonal high-frequency components:
[0157] The features are input into downsampling, and then low-kernel depthwise convolutions are performed at different frequencies:
[0158]
[0159] Where Y represents the features output by the small kernel depthwise convolution, Q is the input tensor, and W is... The weight tensor of a deep kernel, For convolution operations, this step is used to separate the convolutions between frequency components and also increases the receptive field. For inverse wavelet transform, WT Wavelet transform;
[0160] The features of different frequencies are processed, and the results are weighted and summed. The input features are then transformed using inverse wavelet transform (IWT). The output is :
[0161]
[0162] Since equation (1) forms an orthogonal normalized basis, the transpose convolution operation can be performed... Obtain inverse wavelet features ;
[0163] The cascaded wavelet decomposition is given by recursively decomposing the low-frequency components:
[0164]
[0165] in, i2 is the current level, which increases the resolution of low-frequency frequencies and decreases the spatial resolution;
[0166] After that, Decomposed into multiple sub-feature maps:
[0167]
[0168] Wherein, sub-mapping Including all elements , ( , ) represents an element At a specific location in a two-dimensional plane Indicates step size, sub-mapping The number depends on The size of the step, equation (6) is based on Taking 2 as an example, [,]express The width and height values range from 0 to 10. The set of four sub-maps is obtained by downsampling the original feature map using a scaling factor of 1 / 2. , , , ; , express The width and height in the (x,y) dimension, where x and y take values of 0 and 1 in the above formula;
[0169] The SPD layer maps these sub-features onto the channel dimension and concatenates them to generate new feature representations. This reduces the spatial dimension by an order of magnitude:
[0170]
[0171] in, Indicates the number of channels;
[0172] After the SPD feature transformation layer, when At that time, a convolutional layer with a stride of 1 is introduced to obtain the feature representation. :
[0173]
[0174] in, The number of channels obtained by introducing a convolutional layer with a stride of 1;
[0175] Step 4.1.3: Input the coarsely extracted features obtained from the nth local feature extraction unit into the Fast Spatial Pyramid Pooling (SPPF) layer for feature enhancement to obtain the enhanced features. ;
[0176] Step 4.1.4, then, Further enhancement is achieved by entering the C2PSA-MSDA module, resulting in enhanced features. The operating principle of the C2PSA-MSDA module includes:
[0177] right conduct The convolution operation divides the channel into two equal parts. and ;
[0178] Then The number of channels is divided into ,get and , The input is the PSA-MSDA module, which consists of a multi-scale extended attention MSDA module and a feedforward network FFN module;
[0179] Part The feature map is input into the MSDA module. First, the channel is divided into different heads, and different dilation rates are used in different heads. Then, a sliding window expansion attention (SWDA) operation is performed in each head.
[0180] The Sliding Window Expanded Attention (SWDA) is described as follows:
[0181]
[0182] in, for Output features: Q, K, and V represent the query, key, and value matrix, respectively, and r represents the expansion rate, which controls the sparsity.
[0183] For position ( , Output Corresponding components :
[0184]
[0185] Where H and W2 represent the height and width of the feature map, and This represents the keys and values selected from the feature map. Indicates the position ( , The query vector at () express Scaling factor for vector dimension This represents the attention mechanism. express Function; for position ( , ) query at, select a set of coordinates Self-attention is performed on the keys and values at the specified locations:
[0186]
[0187] in, , This indicates the coordinate offset of the window relative to the query. Indicates the size of the sliding window;
[0188] Then, the different features are concatenated together and fed into a linear layer:
[0189]
[0190]
[0191] in, As the expansion rate of the i3th head, Represents the feature map slice of the i3th head, output connect After being combined, it is sent to the linear layer. Perform feature aggregation to obtain aggregated features G;
[0192] Processed by the MSDA module and Feature fusion to form part , Features are obtained after processing by a feedforward network (FFN). ;
[0193] The output feature of the nth PSA-MSDA module ,Will and Features are obtained by performing a concat operation. .
[0194] Furthermore, the feature fusion network performs feature fusion to obtain a first fused feature. Second fusion feature The third fusion feature Fourth fusion feature Specifically, it includes:
[0195] Step 4.2.1: The first fused feature is extracted from the coarse features. and the third convolution module The features obtained from the third upsampling module and the C3K2 feature extraction module are combined; the coarsely extracted features are then processed. Features obtained from the third convolutional module The fused features are obtained after processing by the third upsampling module. , The first fused feature is obtained after feature extraction by the C3K2 feature extraction module. ;
[0196] Step 4.2.2: The second fused feature is generated by the third convolutional module. First downsampling module The features obtained from the C3K2 feature extraction module are combined; the features are then... Features obtained from the first downsampling module The splicing results in fusion features , The second fused feature is obtained after feature extraction by the C3K2 feature extraction module. ;
[0197] Step 4.2.3: The third fusion feature is generated by the second convolutional module. Second downsampling module The features obtained from the C3K2 feature extraction module are combined with the features obtained from the second convolution module. The obtained features Features obtained by the second downsampling module The splicing results in fusion features , The third fused feature is obtained after feature extraction by the C3K2 feature extraction module. ;
[0198] Step 4.2.4, the fourth fusion feature is composed of features and the third downsampling module The features obtained from the C3K2 feature extraction module constitute the feature composition; the features are then processed. Features obtained from the third downsampling module The splicing results in fusion features , The fourth fused feature is obtained after feature extraction by the C3K2 feature extraction module. ;
[0199] The downsampling module operates as follows:
[0200] The Haar wavelet downsampling module (HWD) consists of two modules: (1) a lossless feature encoding module and (2) a feature representation learning module. The lossless feature encoding module is responsible for transforming features and reducing spatial resolution. The feature representation learning module is used to extract discriminative features.
[0201] The lossless feature encoding module utilizes a Haar wavelet transform layer to reduce the spatial resolution of the feature map, wherein the wavelet basis functions of the first-order one-dimensional Haar transform are... and scaling function The formula is as follows:
[0202]
[0203] in, The definition is as follows:
[0204]
[0205] The first-order Haar transform is represented using the 0th-order Haar basis functions:
[0206]
[0207] When the Haar wavelet transform is applied to process two-dimensional signals of image features, four components are generated, each with the spatial resolution of the original two-dimensional signal of image features. ; This represents the position index used to define wavelet functions at different scales and directions during the Haar wavelet transform process, where j2 and k1 represent the stage and order of the Haar basis functions, respectively.
[0208] The four components generated by the lossless feature encoding module are input into the feature representation learning module to extract discriminative features, resulting in a set of i4 downsampled features. .
[0209] Furthermore, the processing flow of the dynamic detection head network includes:
[0210] Step 4.3.1: The dynamic head DyHead in the dynamic head network transforms the attention function into three sequential attentions, each focusing on a viewpoint:
[0211]
[0212] in, , and These are attention functions applied to three different dimensions: scale dimension L, spatial dimension S, and task dimension C; F is a given feature tensor.
[0213] Apply a scale-aware attention module along the L dimension to fuse features at different scales based on semantic importance:
[0214]
[0215] in, It is a by A linear function approximated by a convolutional layer;
[0216] Applying a spatially aware attention module along the S dimension focuses on the discriminative region where spatial location and feature level always coexist:
[0217]
[0218] Where K is the number of sparse sampling locations. It is a self-learning space offset movement position It is a location The importance scalar of self-learning at a given location This indicates the number of levels in the pyramid. Indicates the first The weight value of the k-th spatial location in the feature map of the layer. Indicates at the level Position after offset ,aisle Eigenvalue functions at;
[0219] In dimension C, a task-aware attention module is applied to dynamically switch the ON and OFF channels of features to support different tasks:
[0220]
[0221] in, It is a feature slice of the c-th channel. It is a hyperfunction that learns to control the activation threshold;
[0222] Step 4.3.2: Apply the above three types of attention in sequence, nesting them multiple times to combine multiple... , and They are stacked together to form a dynamic detection head.
[0223] Step 5: Use the training set A modified YOLOv11-based drug packaging defect detection model was trained and validated using a validation set. The model performance is continuously evaluated during training using a test set. Conduct tests to obtain the model weight file that yields the best test results;
[0224] Step 6: Perform sparse training on the trained model weight file, iteratively prune the model, and output a lightweight weight file for the improved YOLOv1n drug packaging defect detection model.
[0225] Further, step 6 includes:
[0226] Layer-Adaptive Magnitude-based Pruning (LAMP) is used to lightweight the improved YOLOv11-based drug packaging defect detection model.
[0227] LAMP evaluates the importance of each layer's connection based on the square of the weight magnitude, and determines the pruning strategy based on the LAMP score calculated for each layer; connections with low scores are removed during model pruning; LAMP score. The calculation formula is as follows:
[0228]
[0229] Where u and v represent the indices of the weight vector, and W[u] and W[v] represent the weight values of indices u and v of the weight vector, respectively. This represents the squared weight of u. This represents the sum of squares of the weights from the index u to the subsequent weights;
[0230] To unify the LAMP scores between fully connected layers and convolutional layers, the weight tensors of each layer are converted into one-dimensional vectors for consistency processing. Then, the weights are sorted in ascending order according to the index mapping. The LAMP scoring criteria are shown in the following formula:
[0231]
[0232] According to formula (22), assuming Then the LAMP score This indicates that the connection pairs with large weights have high model performance and are preserved during pruning; this quantization method enables the LAMP score to effectively balance the importance of local weights and local distributions, ensuring an effective pruning strategy.
[0233] Step 7: Deploy the lightweight weight file of the improved YOLOv11 drug packaging defect detection model into the inference model to build the final improved YOLOv11 drug packaging defect detection model. Use this model to detect the location, size, and confidence level of drug packaging defects and output the defect detection results.
[0234] The present invention also provides a pharmaceutical packaging defect detection device based on an improved YOLOv11 model, the device comprising: a module for performing the aforementioned pharmaceutical packaging defect detection method based on an improved YOLOv11 model.
[0235] For the specific functions of each of the above modules, please refer to the specific method content in Example 1.
[0236] It should be noted that those skilled in the art will understand that the embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can be implemented as a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can be implemented as a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0237] It should be noted that in this application, the terms "comprising," "including," or otherwise are intended to cover non-exclusive inclusion, such that a process, method, article, or system that comprises a list of elements includes not only those elements but also other elements not expressly listed. Unless otherwise specified, the phrase "comprising" implies inclusion. "The definition of a particular element does not preclude the existence of another identical element in the process, method, item, or system of that element."
[0238] Although the present invention has been disclosed above with reference to preferred embodiments, the embodiments are merely illustrative examples and are not intended to limit the present invention. Those skilled in the art can make any changes and modifications without departing from the spirit and scope of the present invention. The scope of protection claimed by the present invention should be determined by the claims.
Claims
1. A method for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model, characterized in that: The method includes: Step 1: Collect images of defects in drug packaging to form an image dataset; Step 2: Label the location, size, and category of defects in any image in the image dataset to obtain the location, size, and category labels of defects in any image; Step 3: Preprocess the labeled image dataset, and then divide the preprocessed image dataset into training set, validation set and test set; Step 4: Construct a drug packaging defect detection model based on the improved YOLOv11; Step 5: Train the improved YOLOv11 drug packaging defect detection model using the training set, continuously evaluate the model performance during training using the validation set, and test it using the test set to obtain the model weight file with the best test results. Step 6: Perform sparse training on the trained model weight file, iteratively prune the model, and output a lightweight weight file for the improved YOLOv1n drug packaging defect detection model. Step 7: Deploy the lightweight weight file of the improved YOLOv11 drug packaging defect detection model into the inference model to build the final improved YOLOv11 drug packaging defect detection model. Use this model to detect the location, size, and confidence of drug packaging defects and output the defect detection results. In step 4, the improved YOLOv11 drug packaging defect detection model improves the backbone, neck, and head network modules included in the original YOLOv11 network model as follows: (1) Replace the traditional convolution Conv in the backbone network module with SPD large receptive field wavelet convolution SPD-WTConv. The SPD-WTConv is a novel convolution operation module consisting of SPD and WTConv layers. The SPD layer reconstructs the spatial blocks of the input feature map to the depth dimension, increases the number of channels, and reduces the spatial resolution. WTConv is an innovative convolutional neural network layer that achieves deep and multi-scale analysis of the input data through wavelet transform technology, providing a larger receptive field for the convolutional neural network CNN. (2) Improve the C2PSA module in the backbone network module to the C2PSA-MSDA module, replace the autonomous intention module in the C2PSA module with the multi-scale expanded attention module, replace the self-attention module with the multi-scale expanded attention module, set different expansion rates for different heads, extract multi-scale features, and thus realize multi-scale representation capability. (3) In the neck network module, the Haar wavelet downsampling module is used to optimize the downsampling part of the YOLOv11 model. HWD explicitly increases the number of channels of the feature map and uses Haar wavelet transform to reduce its resolution. Then, convolution operation is used to learn representative features to filter redundant information. (4) The Head part of the detection head module adopts the dynamic detection head DyHead. DyHead combines scale awareness, spatial awareness and task awareness attention mechanisms. By adding attention mechanism to the feature tensor dimension, the feature representation capability can be enhanced.
2. The method for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model according to claim 1, characterized in that: In step 2, the categories of defects include: surface scratches, gaps in the medicine bag, poor heat sealing, blister pack damage, material aging, and blurred lettering; The location and size of the defect. The calculation formula is as follows: ; in, Indicates the i-th defect in any image The center point of the actual bounding box where the location is located; Indicates the i-th defect in any image The width of the actual bounding box where the object is located; Indicates the i-th defect in any image The height of the actual bounding box at the location in the middle.
3. The method for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model according to claim 1, characterized in that: In step 3, the preprocessing includes normalizing the labeled image dataset; the preprocessing also includes augmenting the dataset by using flipping, rotating, stitching, and translating methods to augment the dataset to obtain an augmented dataset.
4. The method for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model according to claim 1, characterized in that: In step 4, the construction of the improved YOLOv11 drug packaging defect detection model includes, in sequence: a feature extraction network, a feature fusion network, and a dynamic detection head network. The feature extraction network includes one convolutional module (Conv), four SPD-WTConv convolutional modules, four C3K2 feature extraction modules, one fast spatial pyramid (SPPF) module, and a feature enhancement (C2PSA) module; wherein, one SPD-WTConv convolutional module and one C3K2 feature extraction module constitute a local feature extraction unit; for the i-th feature map... The process yields a feature set consisting of n coarsely extracted features. ; The feature fusion network is composed of a bottom-up upsampling module and a top-down downsampling module concatenated in reverse. The feature fusion network includes four feature fusion units, which process the i-th feature map. The feature set consisting of n coarsely extracted features is input into a feature fusion network to perform feature fusion and obtain the first fused feature. Second fusion feature The third fusion feature Fourth fusion feature ; The dynamic detection head network processes features in three dimensions—L, S, and C—using attention mechanisms in these three dimensions respectively. , , , The i-th feature map is obtained through processing. Category prediction labels and position size labels .
5. The method for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model according to claim 4, characterized in that: The processing flow of the feature extraction network includes: Step 4.1.1, the convolution module Conv... Performing a convolution operation yields a size of initial convolutional features Where M is The width, N is of high; Step 4.1.2, The feature set is obtained by sequentially processing n local feature extraction units, which correspond to the coarsely extracted features. ; The operation process of the SPD-WTConv convolution module in the local feature extraction unit includes: Input the i-th feature map The input features are decomposed into high-frequency and low-frequency features using the Haar wavelet transform (WT): ; in, For a low-pass filter, It is a set of high-pass filters; the convolution output has four channels, each channel has resolution : ; in, yes The low-frequency components, These are its horizontal, vertical, and diagonal high-frequency components: The features are input into downsampling, and then low-kernel depthwise convolutions are performed at different frequencies: ; Where Y represents the features output by the small kernel depthwise convolution, Q is the input tensor, and W is... The weight tensor of a deep kernel, For convolution operations, this step is used to separate the convolutions between frequency components and also increases the receptive field. For inverse wavelet transform, WT Wavelet transform; The features of different frequencies are processed, and the results are weighted and summed. The input features are then transformed using inverse wavelet transform (IWT). The output is : ; Since equation (1) forms an orthogonal normalized basis, the transpose convolution operation can be performed... Obtain inverse wavelet features ; The cascaded wavelet decomposition is given by recursively decomposing the low-frequency components: ; in, i2 is the current level, which increases the resolution of low-frequency frequencies and decreases the spatial resolution; After that, Decomposed into multiple sub-feature maps: ; Wherein, sub-mapping Including all elements , ( , ) represents an element At a specific location in a two-dimensional plane Indicates step size, sub-mapping The number depends on The size of the step size, equation (6) is based on Taking 2 as an example, [,]express The width and height values range from 0 to 10. The set of four sub-maps is obtained by downsampling the original feature map using a scaling factor of 1 / 2. , , , ; , express The width and height in the (x,y) dimension, where x and y take values of 0 and 1 in the above formula; The SPD layer maps these sub-features onto the channel dimension and concatenates them to generate new feature representations. This reduces the spatial dimension by an order of magnitude: ; in, Indicates the number of channels; After the SPD feature transformation layer, when At that time, a convolutional layer with a stride of 1 is introduced to obtain the feature representation. : ; in, The number of channels obtained by introducing a convolutional layer with a stride of 1; Step 4.1.3: Input the coarsely extracted features obtained from the nth local feature extraction unit into the Fast Spatial Pyramid Pooling (SPPF) layer for feature enhancement to obtain the enhanced features. ; Step 4.1.4, then, Further enhancement is achieved by entering the C2PSA-MSDA module, resulting in enhanced features. The operating principle of the C2PSA-MSDA module includes: right conduct The convolution operation divides the channel into two equal parts. and ; Then The number of channels is divided into ,get and , The input is the PSA-MSDA module, which consists of a multi-scale extended attention MSDA module and a feedforward network FFN module; Part The feature map is input into the MSDA module. First, the channel is divided into different heads, and different dilation rates are used in different heads. Then, a sliding window expansion attention (SWDA) operation is performed in each head. The Sliding Window Expanded Attention (SWDA) is described as follows: ; in, for Output features: Q, K, and V represent the query, key, and value matrix, respectively, and r represents the expansion rate, which controls the sparsity. For position ( , Output Corresponding components : ; Where H and W2 represent the height and width of the feature map, and This represents the keys and values selected from the feature map. Indicates the position ( , The query vector at () express Scaling factor for vector dimension This represents the attention mechanism. express Function; for position ( , ) query at, select a set of coordinates Self-attention is performed on the keys and values at the specified locations: ; in, , This indicates the coordinate offset of the window relative to the query. Indicates the size of the sliding window; Then, the different features are concatenated together and fed into a linear layer: ; ; in, As the expansion rate of the i3th head, Represents the feature map slice of the i3th head, output connect After being combined, it is sent to the linear layer. Perform feature aggregation to obtain aggregated features G; Processed by the MSDA module and Feature fusion to form part , Features are obtained after processing by a feedforward network (FFN). ; The output feature of the nth PSA-MSDA module ,Will and Features are obtained by performing a concat operation. .
6. The method for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model according to claim 4, characterized in that: The feature fusion network performs feature fusion to obtain the first fused feature. Second fusion feature The third fusion feature Fourth fusion feature ; Specifically, it includes: Step 4.2.1: The first fused feature is extracted from the coarse features. and the third convolution module The features obtained from the third upsampling module and the C3K2 feature extraction module are combined; the coarsely extracted features are then processed. Features obtained from the third convolutional module The fused features are obtained after processing by the third upsampling module. , The first fused feature is obtained after feature extraction by the C3K2 feature extraction module. ; Step 4.2.2: The second fused feature is generated by the third convolutional module. First downsampling module The features obtained from the C3K2 feature extraction module are combined; the features are then... Features obtained from the first downsampling module The splicing results in fusion features , The second fused feature is obtained after feature extraction by the C3K2 feature extraction module. ; Step 4.2.3: The third fusion feature is generated by the second convolutional module. Second downsampling module The features obtained from the C3K2 feature extraction module are combined with the features obtained from the second convolution module. The obtained features Features obtained by the second downsampling module The splicing results in fusion features , The third fused feature is obtained after feature extraction by the C3K2 feature extraction module. ; Step 4.2.4, the fourth fusion feature is composed of features and the third downsampling module The features obtained from the C3K2 feature extraction module constitute the feature composition; the features are then processed. Features obtained from the third downsampling module The splicing results in fusion features , The fourth fused feature is obtained after feature extraction by the C3K2 feature extraction module. ; The downsampling module operates as follows: The Haar wavelet downsampling module (HWD) consists of two modules: (1) a lossless feature encoding module and (2) a feature representation learning module. The lossless feature encoding module is responsible for transforming features and reducing spatial resolution. The feature representation learning module is used to extract discriminative features. The lossless feature encoding module utilizes a Haar wavelet transform layer to reduce the spatial resolution of the feature map, wherein the wavelet basis functions of the first-order one-dimensional Haar transform are... and scaling function The formula is as follows: ; in, The definition is as follows: ; The first-order Haar transform is represented using the 0th-order Haar basis functions: ; When the Haar wavelet transform is applied to process two-dimensional signals of image features, four components are generated, each with the spatial resolution of the original two-dimensional signal of image features. ; This represents the position index used to define wavelet functions at different scales and directions during the Haar wavelet transform process, where j2 and k1 represent the stage and order of the Haar basis functions, respectively. The four components generated by the lossless feature encoding module are input into the feature representation learning module to extract discriminative features, resulting in a set of i4 downsampled features. .
7. The method for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model according to claim 4, characterized in that: The processing flow of the dynamic detection head network includes: Step 4.3.1: The dynamic head DyHead in the dynamic head network transforms the attention function into three sequential attentions, each focusing on a viewpoint: ; in, , and These are attention functions applied to three different dimensions: scale dimension L, spatial dimension S, and task dimension C; F is a given feature tensor. Apply a scale-aware attention module along the L dimension to fuse features at different scales based on semantic importance: ; in, It is a by A linear function approximated by a convolutional layer; Applying a spatially aware attention module along the S dimension focuses on the discriminative region where spatial location and feature level always coexist: ; Where K is the number of sparse sampling locations. It is a self-learning space offset movement position It is a location The importance scalar of self-learning at a given location This indicates the number of levels in the pyramid. Indicates the first The weight value of the k-th spatial location in the feature map of the layer. Indicates at the level Position after offset ,aisle Eigenvalue functions at; In dimension C, a task-aware attention module is applied to dynamically switch the ON and OFF channels of features to support different tasks: ; in, It is a feature slice of the c-th channel. It is a hyperfunction that learns to control the activation threshold; Step 4.3.2: Apply the above three types of attention in sequence, nesting them multiple times to combine multiple... , and They are stacked together to form a dynamic detection head.
8. The method for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model according to claim 1, characterized in that: Step 6 includes: A layer-adaptive amplitude-based pruning LAMP was used to lightweight the improved YOLOv11-based drug packaging defect detection model. LAMP evaluates the importance of each layer's connection based on the square of the weight magnitude, and determines the pruning strategy based on the LAMP score calculated for each layer; connections with low scores are removed during model pruning; LAMP score. The calculation formula is as follows: ; Where u and v represent the indices of the weight vector, and W[u] and W[v] represent the weight values of indices u and v of the weight vector, respectively. This represents the squared weight of u. This represents the sum of squares of the weights from the index u to the subsequent weights; The weight tensors of each layer are converted into one-dimensional vectors for standardization. Then, the weights are sorted in ascending order according to the index mapping. The LMAP scoring criteria are shown in the following formula: ; According to formula (22), assuming Then the LAMP score This indicates that the model performs well with connection pairs that have high weights and are preserved during pruning.
9. A pharmaceutical packaging defect detection device based on an improved YOLOv11 model, characterized in that, The apparatus includes a module for performing a method for detecting defects in pharmaceutical packaging based on an improved YOLOv11 model as described in any one of claims 1 to 8.
Citation Information
Patent Citations
Turning tool workpiece defect detection method based on YOLOv5n model
CN118469973A
Improved defect detection method for medicine bubble cap plate by using YOLOv12
CN120580563A