YOLOv7 plaid defect detection method, system and device
By improving the YOLOv7 model and introducing the SK-ELAN module, SKnet module, SimAM attention mechanism, and deformable convolution, the problems of accuracy and speed in detecting defects in patterned fabrics were solved, and efficient multi-defect detection was achieved in complex backgrounds.
Patent Information
- Application Number
- CN202410554628.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-07
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2044-05-07
AI Technical Summary
Existing defect detection network models suffer from low detection accuracy and slow speed on patterned fabrics, especially in complex backgrounds where they struggle to effectively detect multiple defects.
An improved YOLOv7 model is adopted, which improves the model's receptive field adaptation ability and object detection performance by introducing the SK-ELAN module, SKnet module, SimAM attention mechanism and deformable convolution.
It improves the accuracy and speed of defect detection in patterned fabrics, enabling rapid detection of multiple defects against complex backgrounds. It is suitable for the inspection of simple solid-color fabrics and textiles, and reduces detection errors caused by complex backgrounds.
Smart Images

Figure CN118485628B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a method, system, and device for detecting defects in patterned fabrics, and more particularly to a YOLOv7 method, system, and device for detecting defects in patterned fabrics, belonging to the field of target detection technology. Background Technology
[0002] Fabric is an essential part of daily life, and most of our everyday clothing is made directly or indirectly from it. However, during the fabric production process, due to errors from machinery or humans, various defects are inevitably produced. A piece of fabric containing defects will have its value reduced by about half. Therefore, to ensure the quality of the produced fabric, defect detection is an indispensable part of the fabric production process.
[0003] Fabric defects are categorized into solid-color and patterned fabric defects. For patterned fabric defects, the complex background pattern can hinder detection. Currently, representative two-stage deep neural network models for defect detection include R-CNN, Fast R-CNN, and Mask R-CNN, while representative one-stage deep neural networks include the YOLO series and SSD series. Two-stage network models typically offer higher accuracy but are slower, while one-stage network models are generally faster than two-stage algorithms but suffer some accuracy loss. In recent years, the YOLO series has reached YOLOv8, representing a significant breakthrough in both accuracy and speed. Summary of the Invention
[0004] Purpose of the invention: The purpose of this invention is to provide a YOLOv7 method, system, and device for rapidly extracting global information of patterned fabrics and better detecting various defects in complex backgrounds.
[0005] Technical solution: The present invention provides a method for detecting defects in YOLOv7 patterned fabric, comprising the following steps:
[0006] S1: Obtain data from a public dataset, preprocess the data to obtain a dataset of patterned fabrics, and divide the dataset into a training set, a validation set, and a test set;
[0007] S2: Based on the YOLOv7 model, the backbone network and attention mechanism are improved to obtain the improved model; the improvements include: replacing the ELAN module with the SK-ELAN module, introducing the SKnet module, adding the SimAM attention mechanism in the Neck part, and adding deformable convolution in the backbone part.
[0008] S3: Input the training set into the improved model for training to obtain a trained detection model;
[0009] S4: Use the detection model to complete the defect detection of the patterned fabric.
[0010] Furthermore, the method for preprocessing the data in step S1 is as follows: changing the geometry and visual appearance of the image, wherein changing the geometry includes rotation, translation, and cropping; and changing the visual appearance includes noise injection, flipping, cropping, and resizing and color space adjustment.
[0011] Furthermore, the SK-ELAN module described in step S2 adds an SKnet module to the branch of the ELAN module structure. Through a dynamic selection mechanism, each neuron in the CNN adaptively adjusts its receptive field size according to the scale of multiple input information, thereby extracting important features.
[0012] Furthermore, the SKnet module in step S2 extracts features through different convolutional kernels, and then fuses them through softmax composed of different information guided by each branch; each SK unit in the SKnet module consists of a 1x1 convolution, an SK convolution, and a 1x1 convolution, and the SK unit includes Split, Fuse, and elect.
[0013] Furthermore, the specific implementation process of the SKnet module in step S2 includes:
[0014] First, for any input patterned fabric defect feature map, perform two transformations to obtain two different convolutional feature maps. and The convolutional kernel sizes are 3x3 and 5x5, respectively, and the dimensions are both H×W×C, where H is the feature map height, W is the feature map width, and C is the number of channels;
[0015] The outputs of different branches are fused, and the elements with the same size and number of channels are added element by element to obtain the feature map U:
[0016]
[0017] Perform global average pooling on the two outputs to obtain global information for each channel:
[0018]
[0019] Among them, F gp () represents the channel's statistical information, U C () indicates feature maps of different sizes;
[0020] Global information S on each channel is accessed through a fully connected layer.C We perform dimensionality reduction to determine the proportion of each channel:
[0021]
[0022] In the formula, Z and F fc () represents the statistical information after dimensionality reduction. For a fully connected layer, δ is the ReLU function, and β represents batch regularization.
[0023] A decay rate γ is introduced to verify the effect of the vector d generated by the fully connected layer, using the following equation:
[0024]
[0025] In the formula, L is the minimum value of d, and C represents the number of channels;
[0026] The feature maps with channel discrimination are obtained and summed together to obtain the output feature map V. C As feature maps obtained by adapting to different receptive fields:
[0027]
[0028] In the formula, a C and b C It is a matrix.
[0029] Furthermore, step S2 describes adding a SimAM attention mechanism to the Neck section. After introducing the SimAM attention mechanism, two trainable parameters are introduced to calculate the weights and biases of the attention map.
[0030] Furthermore, the specific implementation process of the SimAM attention mechanism is as follows:
[0031] Measuring the linear separability between a target neuron and other neurons; each neuron t is defined by the following energy function e t :
[0032]
[0033] In the formula, w t b represents the weights of the linear transformation. t For the bias of the linear transformation, y, y0, and y t x is a constant i This represents the remaining neurons, where M is the number of neurons in the channel. This represents a linear change in t. x represents i Linear change;
[0034] Using binary labels and adding a regularization term, the energy function is redefined as follows:
[0035]
[0036] In the formula, λ is a hyperparameter; analyzing the above formula, we obtain:
[0037]
[0038]
[0039] In the formula, μ t The mean of the target neuron. Let V be the variance of the target neuron, then the minimum energy is as follows:
[0040]
[0041] In the formula, This represents a linear change in the mean. σ 2 The linear change, according to the above formula, the lower the energy, the greater the difference between the neuron and the surrounding neurons, the higher the importance, and the better the result of the input feature X enhancement. As shown in the following formula:
[0042]
[0043] In the formula, sigmoid() is the activation function, E represents the minimum energy, and ⊙ represents differentiation.
[0044] Furthermore, the deformable convolution includes an offset layer. The number of output channels of the offset layer controls the offset in the direction of the input feature map g and the output feature map h, enabling the network to dynamically adjust the sampling position of the convolution kernel on the input. A 3x3 convolution operation is used, and each position on the output feature value h satisfies the following formula:
[0045]
[0046] Where ω is the weight of the convolution kernel, p i Δp represents the position within the receptive field region R. i This represents the offset that can be learned at each feature location;
[0047] The offset pixel value is obtained using bilinear interpolation, as shown in the following formula:
[0048]
[0049] Where p is any position, q is any position in the integration space of h, g(p) represents the sampled value at position p on feature map g, g(q) represents the sampled value at position q on feature map g, and G(q, p) represents the bilinear interpolation kernel.
[0050] Based on the same inventive concept, the present invention also provides a YOLOv7 patterned fabric defect detection system, comprising:
[0051] The preprocessing module is used to obtain data from a public dataset, preprocess the data to obtain a dataset of patterned fabrics, and divide the dataset into a training set, a validation set, and a test set.
[0052] The model improvement module is used to improve the backbone network and attention mechanism based on the YOLOv7 model to obtain an improved model. The improvements include: replacing the ELAN module with the SK-ELAN module, introducing the SKnet module, adding the SimAM attention mechanism in the Neck part, and adding deformable convolution in the backbone part.
[0053] The model training module is used to input the training set into the improved model for training to obtain a trained detection model.
[0054] The detection module is used to perform defect detection on the patterned fabric using the detection model.
[0055] Based on the same inventive concept, the present invention also provides a computing device, comprising: one or more processors, one or more memories, and one or more programs, the programs being stored in the memory and configured to be executed by the processor, wherein when the programs are loaded onto the processor, they implement the steps of the YOLOv7 patterned fabric defect detection method according to any of the preceding claims.
[0056] Beneficial effects: Compared with the prior art, the present invention reduces the impact of complex backgrounds on the detection of fabric defects in patterned fabrics, improves the detection accuracy, and provides a foundation for subsequent fabric defect detection; it improves the detection of small defects relative to the background pattern, and the added attention mechanism can quickly detect small defects and detect multiple types of defects. It can be applied to relatively simple defect detection of solid-color fabrics, textile inspection, and headlight scratch detection. Attached Figure Description
[0057] Figure 1 This is a flowchart of the method of the present invention;
[0058] Figure 2 This is a schematic diagram of the SK-ELAN module according to an embodiment of the present invention;
[0059] Figure 3 This is a schematic diagram of the improved YOLOv7 backbone according to an embodiment of the present invention;
[0060] Figure 4 This is a schematic diagram of the improved YOLOv7 network structure according to an embodiment of the present invention. Detailed Implementation
[0061] The technical solution of the present invention will be further described below with reference to the accompanying drawings and embodiments.
[0062] As attached Figure 1 As shown, the YOLOv7 patterned fabric defect detection method of this embodiment includes the following steps:
[0063] S1: Obtain data from a public dataset, preprocess the data to obtain a dataset of patterned fabrics, and divide the dataset into a training set, a validation set, and a test set;
[0064] S2: Based on the YOLOv7 model, the backbone network and attention mechanism are improved to obtain the improved model; the improvements include: replacing the ELAN module with the SK-ELAN module, introducing the SKnet module, adding the SimAM attention mechanism in the Neck part, and adding deformable convolution in the backbone part.
[0065] S3: Input the training set into the improved model for training to obtain a trained detection model;
[0066] S4: Use the detection model to complete the defect detection of the patterned fabric.
[0067] Step S1 selects several images and performs data augmentation on them using methods that modify the image geometry, such as rotation, translation, and cropping. Non-geometric data augmentation focuses on the visual appearance of the image rather than its geometry, such as noise injection, flipping, cropping, resizing, and color space manipulation.
[0068] like Figure 3 As shown, step S2 is an improvement on YOLOv7. In the improved YOLOv7 network, the ELAN design aims to achieve efficient learning and convergence of deeper networks. Therefore, this paper proposes the SK-ELAN module, as follows: Figure 2 As shown, a new backbone feature extraction network is constructed. A challenge in patterned fabric is the small size and variety of defects, making them difficult to detect. The original ELAN module is replaced with an SK-ELAN module, and a dynamic selection mechanism is proposed. This allows each neuron in the CNN to adaptively adjust its receptive field size based on the scale of multiple input information, thus enabling selective extraction of important features. Its strategy is based on the ELAN module structure, adding SKnet modules to the branches, primarily to improve the model's adaptability to the receptive field and select more important convolutional kernel sizes.
[0069] Step 2 uses an improved YOLOv7 network with the SKnet module. SK units extract features using different convolutional kernels, and then fuse them through a softmax function composed of different information guided by each branch. Each SK unit consists of a 1x1 convolution, an SK convolution, and a 1x1 convolution. All convolutional kernels with larger sizes in the original network are replaced with SK convolutions, allowing the network to select an appropriate receptive field size.
[0070] The SK unit comprises three aspects: Split (convolving the original image using different convolutional kernels), Fuse (generating global selection weights by aggregating information at different scales), and Select (aggregating feature maps of kernels of different sizes based on the selection weights). SK convolutions can be embedded into mainstream convolutional neural networks with minimal computational cost.
[0071] The specific experimental process of SKnet includes:
[0072] 1. First, for any input patterned fabric defect feature map, perform two transformations to obtain... and They are identical except for the kernel size (3x3 and 5x5 respectively), and their dimensions are both H×W×C.
[0073] 2. Merge the outputs of different branches, i.e., add them element by element (the output size and number of channels must be the same);
[0074]
[0075] 3. Perform global average pooling on the two outputs to obtain global information for each channel;
[0076]
[0077] 4. To achieve a more compact representation, s is reduced in dimensionality using a fully connected layer to determine the weight of each channel;
[0078]
[0079] δ is the ReLU function, and β represents batch regularization.
[0080] 5. To verify the role of d in W, an attenuation rate γ is introduced, as follows, where C represents the number of channels.
[0081]
[0082] Points 2 to 5 above mainly involve selectively filtering the output of the previous layer, so that each branch carries a different information flow into the next neuron.
[0083] 6. Obtain feature maps with channel discrimination, and then add them together to obtain the output feature map V. This feature map is the feature map obtained by adaptively changing different receptive fields.
[0084]
[0085] In step S2, the SimAM attention mechanism is added to the Neck part of the YOLOv7 model. Introducing SimAM introduces two additional trainable parameters to YOLOv7, used to calculate the weights and biases of the attention map. However, the biggest problem in detecting defects in patterned fabric is the complex background. The background is large while the defects are small and easily overlap, making them difficult to detect. The SimAM attention mechanism can effectively improve the performance of object detection, especially when dealing with small objects. Therefore, although the number of parameters and computational cost increases, the model performance is also improved. It also reduces the error caused by the difficulty in detecting defects in complex backgrounds. Moreover, it has superior characteristics compared to the current two-dimensional attention mechanism. SimAM can directly calculate the three-dimensional attention weights, meaning it can fully utilize the information in the three-dimensional data, enabling the model to better capture the features of the target.
[0086] The SimAM attention mechanism operates as follows:
[0087] To achieve better attention, it's necessary to assess the importance of each neuron. Activating a neuron typically inhibits surrounding neurons, a phenomenon known as spatial inhibition. The simplest implementation is to measure the linear separability of a target neuron from other neurons. Each neuron defines the following energy function:
[0088]
[0089] Minimizing the above formula is equivalent to achieving linear separability between neuron t and other neurons within the same channel during training. For simplicity, we use binary labels and add a regularization term; the final energy function is defined as follows:
[0090]
[0091] In theory, each channel has an energy function. Fortunately, the above formula has the following analytical solution:
[0092]
[0093]
[0094] Therefore, the minimum energy can be obtained by the following formula:
[0095]
[0096] The above formula means that the lower the energy, the greater the difference between neuron t and surrounding neurons, and the higher its importance. The output result is shown in the following formula:
[0097]
[0098] Step 2 introduces deformable convolution, adding it to the backbone of the YOLOv7 network to improve the extraction of features from the target's own regions and edges. Different batches of fabric have different complex textures and patterns, greatly increasing the difficulty of defect detection. Traditional convolution cannot flexibly adapt to the irregular shape of the target, thus its receptive field is fixed. However, defects on patterned fabrics, such as oil stains and missing pieces, vary greatly in scale, and their proportion is small compared to the overall fabric. Therefore, a deformable convolutional neural network model is proposed. Deformable convolution has a flexible receptive field, covering a larger area and better matching the shape of defects, thereby improving convolution efficiency. Furthermore, to obtain feature information from distant locations in the image while minimizing the acquisition of texture information from the fabric, thus reducing defect detection errors, deformable convolution can be used to improve the semantic capabilities of the features.
[0099] An offset layer is set up, and the number of output channels of the offset layer controls the offset in the learning g and h directions, enabling the network to dynamically adjust the sampling position of the convolution kernel on the input. This method allows the network to better adapt to the deformation of the target, thereby improving the ability to recognize irregular targets. A 3x3 convolution operation is used, and each position of the output feature value h satisfies the following formula:
[0100]
[0101] Where ω is the weight of the convolution kernel, g is the input feature map, and p i Indicates the position in R, Δp i This represents the offset that can be learned at each feature location. When there is no Δp... i When the time is right, it is a regular convolution.
[0102] Since the position after adding the offset is generally a decimal and does not correspond to the actual pixel point on the input feature map, interpolation is needed to obtain the offset pixel value. Bilinear interpolation is usually used, and the formula is as follows:
[0103]
[0104] Where p is an arbitrary position; q represents all integral spatial positions in the feature map h; and G(q, p) represents the bilinear interpolation kernel. The improved network is as follows: Figure 4 As shown.
[0105] Based on the same inventive concept, this embodiment also provides a YOLOv7 patterned fabric defect detection system, including:
[0106] The preprocessing module is used to obtain data from a public dataset, preprocess the data to obtain a dataset of patterned fabrics, and divide the dataset into a training set, a validation set, and a test set.
[0107] The model improvement module is used to improve the backbone network and attention mechanism based on the YOLOv7 model to obtain an improved model. The improvements include: replacing the ELAN module with the SK-ELAN module, introducing the SKnet module, adding the SimAM attention mechanism in the Neck part, and adding deformable convolution in the backbone part.
[0108] The model training module is used to input the training set into the improved model for training to obtain a trained detection model.
[0109] The detection module is used to perform defect detection on the patterned fabric using the detection model.
[0110] Based on the same inventive concept, this embodiment also provides a computing device, including: one or more processors, one or more memories, and one or more programs, the programs being stored in the memory and configured to be executed by the processor, the programs being loaded onto the processor to implement the steps of the YOLOv7 patterned fabric defect detection method according to any of the preceding claims.
[0111] The improved YOLOv7 network model used in this method adds the SKnet module to the old ELAN module. Since the SK unit can fuse feature maps obtained from convolutional kernels of different sizes to adapt to targets of different scales, the SKNet network gains the ability to adaptively adjust the receptive field, thereby making the extraction of target feature information more accurate.
[0112] The improved YOLOv7 network model incorporates the SimAM attention mechanism to enhance object detection performance, particularly when handling small objects. It also reduces errors caused by complex backgrounds making it difficult to detect defects, enabling the model to better capture object features.
[0113] This invention adds deformable convolutions to the backbone of YOLOv7, which can be used to improve the extraction capability of features of the target's own region and edges. This makes the receptive field more flexible, covers a larger area, and better matches the shape of imperfections, thereby improving convolution efficiency.
Claims
1. A method for detecting defects in YOLOv7 patterned fabric, characterized in that, Includes the following steps: S1: Obtain data from a public dataset, preprocess the data to obtain a dataset of patterned fabrics, and divide the dataset into a training set, a validation set, and a test set; S2: Based on the YOLOv7 model, the backbone network and attention mechanism are improved to obtain the improved model; the improvements include: replacing the ELAN module with the SK-ELAN module, introducing the SKnet module, adding the SimAM attention mechanism in the Neck part, and adding deformable convolution in the backbone part. The SK-ELAN module, based on the ELAN module structure, adds an SKnet module to the branch. Through a dynamic selection mechanism, each neuron in the CNN adaptively adjusts its receptive field size according to the scale of multiple input information, thereby extracting important features. The specific implementation process of the SKnet module includes: First, for any input patterned fabric defect feature map, perform two transformations to obtain two different convolutional feature maps. and The convolutional kernel sizes are 3x3 and 5x5, respectively, and the dimension is 1. , For feature map height, The width of the feature map. Number of channels; The outputs of different branches are fused, and the elements with the same size and number of channels are added element by element to obtain the feature map. : ; Perform global average pooling on the two outputs to obtain global information for each channel: ; in, This indicates statistical information about the channel. Feature maps representing different sizes; Global information on each channel is accessed through a fully connected layer. We perform dimensionality reduction to determine the proportion of each channel: ; In the formula, and This represents the statistical information after dimensionality reduction. It is a fully connected layer. It's the ReLU function. This indicates batch processing; An attenuation rate was introduced. Verify the vectors generated by the fully connected layer Its function is achieved through the following formula: ; In the formula, for The minimum value, Indicates the number of channels; The feature maps with channel discrimination are obtained and summed together to obtain the output feature map. As feature maps obtained by adapting to different receptive fields: ; In the formula, and It is a matrix; The deformable convolution includes an offset layer, and the number of output channels of the offset layer controls the learning of the input feature map. and output feature map The directional offset allows the network to dynamically adjust the sampling position of the convolution kernel on the input; a 3x3 convolution operation is used to output feature values. Each position satisfies the following formula: ; in, The weights of the convolution kernel, This indicates the location within the receptive field region R. This represents the offset that can be learned at each feature location; The offset pixel value is obtained using bilinear interpolation, as shown in the following formula: ; in, For any position, for All integral space locations, Representation of feature map Upper position The sampled values, Representation of feature map Upper position The sampled values, Represented as a bilinear interpolation kernel; S3: Input the training set into the improved model for training to obtain a trained detection model; S4: Use the detection model to complete the defect detection of the patterned fabric.
2. The YOLOv7 patterned fabric defect detection method according to claim 1, characterized in that, The method for preprocessing data in step S1 is as follows: changing the geometry and visual appearance of the image, wherein changing the geometry includes rotation, translation and cropping; and changing the visual appearance includes noise injection, flipping, cropping and resizing and color space adjustment.
3. The YOLOv7 patterned fabric defect detection method according to claim 1, characterized in that, The SKnet module in step S2 extracts features through different convolution kernels, and then fuses them through softmax composed of different information guided by each branch; each SK unit in the SKnet module consists of a 1x1 convolution, an SK convolution, and a 1x1 convolution, and the SK unit includes Split, Fuse, and elect.
4. The YOLOv7 patterned fabric defect detection method according to claim 1, characterized in that, Step S2 describes adding a SimAM attention mechanism to the Neck section. After introducing the SimAM attention mechanism, two trainable parameters are introduced to calculate the weights and biases of the attention map.
5. The YOLOv7 patterned fabric defect detection method according to claim 1, characterized in that, The specific implementation process of the SimAM attention mechanism is as follows: Measuring the linear separability between a target neuron and other neurons; each neuron The following energy function is defined. : ; In the formula, The weights are those for the linear transformation. For the bias of the linear transformation, , and It is a constant. Representing the remaining neurons, This represents the number of neurons in the channel. express linear change, express Linear change; Using binary labels and adding a regularization term, the energy function is redefined as follows: ; In the formula, Let be a hyperparameter; analyzing the above equation, we obtain: ; In the formula, The mean of the target neuron. Let V be the variance of the target neuron, then the minimum energy is as follows: ; In the formula, This represents a linear change in the mean. express The linear change of the input features, according to the above formula, means that the lower the energy, the greater the difference between the neuron and its surrounding neurons, and the higher its importance. Enhanced results As shown in the following formula: ; In the formula, For activation function, Represents the minimum energy. This indicates element-wise multiplication.
6. A YOLOv7 patterned fabric defect detection system using the YOLOv7 patterned fabric defect detection method as described in any one of claims 1 to 5, characterized in that, include: The preprocessing module is used to obtain data from a public dataset, preprocess the data to obtain a dataset of patterned fabrics, and divide the dataset into a training set, a validation set, and a test set. The model improvement module is used to improve the backbone network and attention mechanism based on the YOLOv7 model to obtain an improved model. The improvements include: replacing the ELAN module with the SK-ELAN module, introducing the SKnet module, adding the SimAM attention mechanism in the Neck part, and adding deformable convolution in the backbone part. The model training module is used to input the training set into the improved model for training to obtain a trained detection model. The detection module is used to perform defect detection on the patterned fabric using the detection model.
7. A computing device, characterized in that, include: One or more processors, one or more memories, and one or more programs, said programs being stored in the memory and configured to be executed by the processor, said programs being loaded onto the processor to implement the steps of the YOLOv7 patterned fabric defect detection method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Fabric defect detection method based on YOLO v4 improved algorithm
CN113192040A
Fabric defect detection method and device based on improved YOLOv5
CN116740050A
