Light-weight metal plate surface defect detection method based on improved YOLOv5n

By introducing the ADown module and the deformable attention Transformer (DAT) module in the YOLOv5n network model, the problem of poor model detection effect and large parameters in the visual inspection of metal sheets is solved, and efficient and high-precision defect detection is achieved.

CN119624879APending Publication Date: 2025-03-14JIANGSU UNIV OF TECH

Patent Information

Application Number
CN202411662412.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-24
Publication Date
2025-03-14

AI Technical Summary

Technical Problem

The prior art has problems in the field of visual detection of metal sheets, which are poor model detection effects and large model parameters.

Method used

The improved YOLOv5n network model is adopted, and the CBS module in the original downsampling process is replaced by introducing ADown modules in the backbone and neck networks, and the deformable attention Transformer (DAT) module is incorporated into the C3 module of the neck network to reduce the amount of model parameters and improve the computing efficiency.

Benefits of technology

It realizes that while maintaining a high inference speed, it improves the accuracy of surface defect detection of metal sheets, meets the needs of higher detection accuracy, and reduces the calculation amount of the model.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119624879A_ABST
    Figure CN119624879A_ABST
Patent Text Reader

Abstract

The invention discloses a lightweight metal plate surface defect detection method based on improved YOLOv5n, and relates to the field of machine vision and target detection, and the method comprises the following steps: S1, image preprocessing; s2, inputting the picture into the model; s3, model training; s4, data obtained after model training are analyzed and judged, the model is adjusted and optimized, and a final result is marked on an original image to serve as output of a detection result; and an improved YOLOv5n network model is adopted to carry out identification detection on the picture. The method has the advantages that the ADown module is introduced into the trunk network and the neck network to replace a CBS module in the original down-sampling process, the calculation efficiency is improved while the model parameter quantity is reduced, the deformable attention Transform is fused into the Bottleneck structure of the neck network C3 module, more feature information is obtained while the high reasoning speed is kept, and the method has the advantages of being high in reasoning speed, high in reasoning speed and high in reasoning speed. The method adapts to diversified defect characteristics of the surface of the metal plate, can meet the requirement for high detection precision, and meanwhile reduces the calculation amount of the model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of machine vision and target detection, and in particular to a method for detecting surface defects of lightweight metal plates based on improved YOLOv5n. Background Art

[0002] During the processing of metal products, burrs, streaks, holes, cracks, pits and other defects often appear on the surface. Therefore, it is necessary to detect surface defects of metal sheets. Traditional metal surface defect detection methods mainly rely on manual vision and simple instrument measurement. Manual detection is easily affected by subjective factors of the inspectors, is inefficient, and is difficult to achieve quality control for large-scale automated production.

[0003] Defect detection technology based on machine vision has shown significant advantages in metal surface defect detection and has gradually become a hot topic for research and application. The advantages of deep learning in the field of target detection, such as high speed, high precision, portability and robustness, make it show great application potential in industrial production, especially in metal sheet surface defect detection. Target detection algorithms based on deep learning are mainly divided into two categories: two-stage target detection algorithms and single-stage target detection methods. These two types of algorithms use different strategies when processing target detection in images: the two-stage algorithm first detects approximate areas and annotates candidate boxes, and then uses another method to more accurately determine the category and specific location of objects in these areas. The two-stage algorithm has a higher detection accuracy; the single-stage algorithm directly uses a method to simultaneously determine the objects in the image and their specific locations, which can be detected faster. Among them, the YOLO series of algorithms are typical representatives of single-stage target detection. This series of algorithms significantly improves the processing speed by merging the positioning and classification steps in the traditional two-stage detection process;

[0004] After searching, the Chinese patent application No. 202410341584.2 discloses a target recognition method based on the improved YOLOv5s smart orchard fruit. By replacing the convolution module in the backbone network, adding the attention mechanism, and modifying the loss function, the fruit target is accurately recognized. The YOLOv7-tiny and other networks are compared to verify the superiority of the improved algorithm in terms of accuracy indicators. However, the number of parameters of the improved network increases, which consumes more running time.

[0005] Application number 202311509438.8 discloses a construction site safety helmet detection method based on improved YOLOv5. By replacing the network pyramid structure and the loss function, it reduces small target misdetection, realizes safety helmet wearing detection, and improves small target detection accuracy, but lacks the calculation of network parameter quantities.

[0006] Application number 202410663373.0 is a steel surface defect detection method based on the improved YOLOv5 algorithm. By introducing the attention mechanism, the detection of steel surface defects is realized. The improved algorithm improves the detection accuracy of steel surface defects, but lacks comprehensive improvement in accuracy and parameter quantity.

[0007] After analysis, it is found that the above-mentioned prior art still has the problems of poor model detection effect and large number of model parameters in the field of metal sheet visual inspection. Based on this, the present invention designs a lightweight metal sheet surface defect detection method based on improved YOLOv5n. Summary of the invention

[0008] The purpose of the present invention is to solve the problems in the prior art and propose a lightweight metal sheet surface defect detection method based on improved YOLOv5n.

[0009] A method for detecting surface defects of lightweight metal sheets based on improved YOLOv5n comprises the following steps:

[0010] S1: Image preprocessing;

[0011] S2: Input the image into the model, wherein the model adopts the YOLOv5n network model;

[0012] S3: Model training. The training process of the model goes through the backbone network, the neck network and the head network in sequence. The prediction results of the model and the true labels are used as inputs to calculate the loss function. Through the back propagation mechanism, the model can self-adjust the parameters of each part of the network model to achieve the expected effect.

[0013] S4: Analyze and judge the data after model training, adjust and optimize the model, retain the optimal model weight, conduct experimental tests on the trained model, perform non-maximum suppression on the prediction results, and mark the final results on the original image as the output of the detection result;

[0014] An improved YOLOv5n network model is used to identify and detect images, wherein in the model, the original downsampling CBS module in the backbone network and the neck network is replaced with an ADown module to reduce the overall resource consumption of the model, wherein the ADown module is a downsampling convolution module in the YOLOv9 network model.

[0015] In the above-mentioned lightweight metal sheet surface defect detection method based on improved YOLOv5n, in step S2, a DAT module is added to the Bottleneck module in the C3 component of the neck network. In the design of the Bottleneck module, the number of input channels is effectively reduced through the two preceding convolutional layers and the original convolutional layers of the C3 module. The DAT module adopts a dynamic deformable attention mechanism. The DAT module is an improvement based on the DETR model, which introduces deformable DETR into the DETR model. By adopting the deformable attention mechanism, the focus on key points around the reference point is enhanced, thereby improving the detection capability of small objects and significantly shortening the training time. The working process of the DAT module includes:

[0016] First, the input feature is converted to q through a linear mapping, and then a lightweight network is used to generate an offset Δp, which will be used to adjust the position of the key point to make it closer to the target area;

[0017] Coordinate normalization and amplitude scaling operations are used to prevent the offset from being too large to ensure the stability of the training process;

[0018] On this basis, through the multi-head attention mechanism, these adjusted keys and values ​​are accurately focused and weighted, enhancing the model's ability to capture key areas;

[0019] In addition, the relative position bias is considered to further enhance the expressiveness of the attention pattern, and the final output is obtained through a linear transformation.

[0020] In the above-mentioned lightweight metal sheet surface defect detection method based on improved YOLOv5n, in the DAT module, in order to achieve accurate feature positioning and dynamic sampling of key points, an offset calculation method is used to generate the key points required in the DAT module:

[0021] The specific method includes: given the input features in the H×W×C space range:

[0022] x∈R H×W×C

[0023] Generate a series of reference points:

[0024]

[0025] To obtain the offset of each reference point, the feature map is linearly projected onto the query, labeled as q, and then fed to the subnetwork θoffset to generate the offset Δp and scale it. The input feature, reference point and its offset are then combined to generate the deformed feature through the sampling function φ(·) The process can be expressed by the following formula:

[0026]

[0027] in, and Respectively represent the position sampling feature generation key and value of the reference point after offset, W q , W k and W v The projection matrix of the keys and values ​​generated by sampling features of the marker point q and the reference point position after offset is the sampling function φ(·) which is bilinear interpolation and is calculated as follows:

[0028]

[0029] Among them, (r x ,r y ) index z∈R H×W×C All positions on;

[0030] (p x ,p y ) is the coordinate of the reference point p+Δp after the offset;

[0031] g(p x ,r x )=max(0,1-|p x -r x |)

[0032] g(p y ,r y )=max(0,1-|p y -r y |)

[0033] Then, a multi-head attention mechanism is used, combined with the relative position offset R, to output the features.

[0034]

[0035] Where m is the number of attention heads, σ represents the normalized exponential function, and d is the size of each attention head. The bias matrix of the relative position deviation of each head is connected together and projected to obtain the final output z.

[0036] In the above-mentioned lightweight metal sheet surface defect detection method based on improved YOLOv5n, in the structure of the sub-network θoffset, a depthwise separable convolution is first performed, and then, based on the depthwise separable convolution, the network is subjected to a GELU nonlinear activation function to further enhance the feature representation capability. Finally, a 1×1 convolution layer is used to generate the final offset. In order to prevent the reference point offset from being forced to move, the bias term of the 1×1 convolution layer is deleted in the θoffset network structure.

[0037] In the above-mentioned lightweight metal sheet surface defect detection method based on improved YOLOv5n, in step S1, the image preprocessing includes three parts: data set loading, data enhancement, and prior frame setting. First, the metal sheet defect sample data set is loaded into the memory. Then, in order to enhance the adaptability and generalization of the model to the metal sheet defect environment, the image preprocessing operation is performed, the input image is enhanced by Mosaic data, and multiple images that have been randomly scaled, cropped, and color adjusted are randomly spliced ​​to improve the generalization ability of the model. Finally, the initially set anchor frame is verified. If the recall rate of the anchor frame is lower than 0.98, the anchor frame size is recalculated.

[0038] In the above-mentioned lightweight metal sheet surface defect detection method based on improved YOLOv5n, the ADown module first reduces the spatial dimension of the input feature map through the average pooling layer, and then divides the feature map into two parts, each of which is processed independently to capture different features. The first part extracts more detailed features through a 3×3 convolution layer with a step size of 2. The second part strengthens the extraction of key features through the maximum pooling operation, and adjusts the channel dimension through 1×1 convolution to further optimize information transmission. Finally, the feature maps processed by the two parts are merged to ensure that the output feature map contains both local features and retains the original information to a certain extent.

[0039] Compared with the prior art, the present invention has the following advantages:

[0040] 1. Methods: ADown modules were introduced into the backbone and neck networks to replace the CBS modules in the original downsampling process, which reduced the number of model parameters and improved computational efficiency.

[0041] 2. Integrate the deformable attention Transformer into the Bottleneck structure of the C3 module of the neck network to obtain more feature information while maintaining a high inference speed to adapt to the diverse defect characteristics of the metal sheet surface. This method can meet the needs of higher detection accuracy while reducing the computational complexity of the model. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] Figure 1 This is a diagram of the YOLO model training process in the lightweight metal sheet surface defect detection method based on improved YOLOv5n proposed in the present invention.

[0043] Figure 2 This is the YOLO model evaluation process in the lightweight metal sheet surface defect detection method based on improved YOLOv5n proposed in the present invention.

[0044] Figure 3This is the YOLO model detection process in the lightweight metal sheet surface defect detection method based on improved YOLOv5n proposed in the present invention.

[0045] Figure 4 This is the YOLOv5n+ADown+C3_DAT structure diagram in the lightweight metal sheet surface defect detection method based on improved YOLOv5n proposed in the present invention.

[0046] Figure 5 This is the ADown structure diagram of the lightweight metal sheet surface defect detection method based on improved YOLOv5n proposed in the present invention.

[0047] Figure 6 This is a structural diagram of the DAT module in the lightweight metal sheet surface defect detection method based on improved YOLOv5n proposed in the present invention.

[0048] Figure 7 This is the θoffset network structure in the lightweight metal sheet surface defect detection method based on improved YOLOv5n proposed by the present invention.

[0049] Figure 8 This is the C3_DAT module in the lightweight metal sheet surface defect detection method based on improved YOLOv5n proposed in the present invention.

[0050] Fig. 9 This is the NEU-DET data set image in the lightweight metal sheet surface defect detection method based on improved YOLOv5n proposed in this invention.

[0051] Fig.10 This is the GC10-DET data set image in the lightweight metal sheet surface defect detection method based on improved YOLOv5n proposed in this invention.

[0052] Figure 11(a) is the PR curve of YOLOv5n in the present invention on the NEU-DET test set.

[0053] Figure 11(b) is the PR curve of YOLOv5n+ADown on the NEU-DET test set in the present invention.

[0054] Figure 11(c) is the PR curve of YOLOv5n+C3_DAT on the NEU-DET test set in the present invention.

[0055] Figure 11(d) is the PR curve of YOLOv5n+C3_DAT+ADown on the NEU-DET test set in the present invention. DETAILED DESCRIPTION

[0056] Reference Figure 1~1 1. The present invention provides a method for detecting surface defects of lightweight metal sheets based on improved YOLOv5n. The method introduces ADown modules in the backbone and neck networks to replace the CBS modules in the original downsampling process, thereby reducing the number of model parameters and improving the computational efficiency. The deformable attention Transformer is integrated into the Bottleneck structure of the C3 module of the neck network to obtain more feature information while maintaining a high inference speed, so as to adapt to the diverse defect characteristics of the metal sheet surface. This method can meet the requirements of higher detection accuracy while reducing the computational complexity of the model.

[0057] The technical solution adopted by the present invention comprises the following steps:

[0058] S1: Image preprocessing: includes three parts: data set loading, data enhancement, and prior frame setting. First, the metal sheet defect sample data set is loaded into the memory. Subsequently, in order to enhance the model's adaptability and generalization to the metal sheet defect environment, a series of image preprocessing operations are performed, Mosaic data enhancement is performed on the input image, and four images that have been randomly scaled, cropped, and color adjusted are randomly spliced ​​to improve the generalization ability of the model. Finally, the initially set anchor frame is verified. If the recall rate of the anchor frame is lower than 0.98, it is considered that it is not suitable for the current data set configuration, and the anchor frame size is recalculated to ensure that it better adapts to the characteristics of the data set and provides an accurate prior frame for the subsequent calculation of the loss function.

[0059] S2: Input the image into the improved YOLOv5n network model: In the YOLOv5n model, the backbone network and the neck network are responsible for feature extraction and information fusion. In the backbone network and the neck network, the original downsampling CBS module is replaced with the ADown module with fewer parameters and more efficient calculation to reduce the overall resource consumption of the model. In order to improve the adaptability and detection accuracy of the model to dynamic environments, a new deformable attention transformer (DAT) is introduced in the C3 module of the neck network, named C3_DAT. With the introduction of DAT, the model can focus on key areas more flexibly and improve the accuracy of multi-scale defect detection on the surface of metal sheets.

[0060] S3: Model training: Use the improved YOLOv5n model to train and evaluate the metal sheet surface defect dataset, take the model's prediction results and true labels as input, and calculate the loss function. Through the back-propagation mechanism, the model can self-adjust the parameters of each part of the network model to achieve the expected effect.

[0061] S4: Experimental test: Analyze and judge the data after model training, and adjust and optimize the model. Keep the optimal model weights and conduct experimental tests on the trained model. The model performs non-maximum suppression on the prediction results and annotates the final results on the original image as the output of the detection results.

[0062] Further, in step S3, the ADown module is used as a downsampling convolution module in YOLOv9 to improve the performance and efficiency of the model by optimizing the processing of feature maps. Downsampling is a common technique in deep learning models that is used to reduce the spatial dimension of feature maps, helping the model capture key features of the image at a higher level while significantly reducing the computational burden. The ADown module achieves this goal in an efficient way that has little impact on model performance. Although the YOLOv9 model has high processing power, its model has a large number of parameters and may encounter performance bottlenecks when running on devices with limited resources. In resource-constrained environments, YOLOv5 is more suitable for lightweight applications due to its lower complexity and parameter requirements. Therefore, the CBS module used for downsampling in YOLOv5 is replaced by the ADown module, combining the efficient characteristics of ADown with the lightweight advantages of YOLOv5 to achieve optimal performance in resource-constrained environments. The ADown module first reduces the spatial dimension of the input feature map through an average pooling layer, and then divides the feature map into two equal parts, each of which is processed independently to capture different features. The first part extracts more detailed features, such as edge or texture information, through a 3×3 convolution layer with a stride of 2. The second part strengthens the extraction of key features through the maximum pooling operation, and adjusts the channel dimension through 1×1 convolution to further optimize information transfer. Finally, the feature maps processed by the two parts are merged to ensure that the output feature map contains both local features and retains the original information to a certain extent.

[0063] Furthermore, in step S3, the deformable attention transformer (DAT) adopts a dynamic deformable attention mechanism, which can adaptively select key points and corresponding values ​​according to the importance of the data, strengthen the model's attention to the relevant area, and effectively capture more information while maintaining global perception. DAT is improved from the deformable DETR. The DETR model has strong competitiveness in the field of target detection. It introduces the transformer mechanism to obtain a wider receptive field by calculating the correlation between each pixel and all other pixels. However, the DETR model has some limitations, such as slow convergence speed and insufficient detection performance for small objects. To solve the above problems, the deformable DETR is introduced into the DETR model. By adopting the deformable attention mechanism, the attention to the key points around the reference point is enhanced, thereby improving the detection ability of small objects and significantly shortening the training time. DAT further proposes a dynamic deformable attention mechanism, which not only overcomes the shortcomings of the traditional DETR model in slow convergence and small-sized object detection, but also provides a resource-efficient and high-performance solution with its innovative deformable attention mechanism, making DAT an ideal choice for visual tasks under resource-constrained conditions. In order to achieve accurate feature localization and dynamic sampling of key points, an efficient offset calculation method is used to generate the key points required in DAT. Given the input features in the H×W×C space range:

[0064] x∈R H×W×C

[0065] Generate a series of reference points:

[0066]

[0067] To obtain the offset of each reference point, the feature map is linearly projected onto the query, labeled as q, and then fed into a lightweight subnetwork θoffset to generate an offset Δp and scale it. The input features, reference points and their offsets are then combined to generate the deformed features through a sampling function φ(·) The process can be expressed by the following formula:

[0068]

[0069]

[0070] in, and Respectively represent the position sampling feature generation key and value of the reference point after offset, W q , W k and Wv The projection matrix of the keys and values ​​generated by sampling features of the marker point q and the reference point position after offset is the sampling function φ(·) which is bilinear interpolation and is calculated as follows:

[0071]

[0072] Among them, (r x ,r y ) index z∈R H×W×C All positions on (p x ,p y ) is the coordinate of the reference point p+Δp after the offset,

[0073] g(p x ,r x )=max(0,1-|p x -r x |)

[0074] g(p y ,r y )=max(0,1-|p y -r y |)

[0075] Using a multi-head attention mechanism, combined with relative position offset R, output feature z (m) :

[0076]

[0077] Where m is the number of attention heads, σ represents the normalized exponential function, and d is the size of each attention head. is the bias matrix of the relative position deviation of each head. Each head feature is connected together and projected to get the final output z.

[0078] The offset subnetwork θoffset used is composed of two convolution modules with nonlinear activation. The GELU activation function and 1×1 convolution are used to obtain a two-dimensional offset to achieve the perception of local features. The input feature is captured by a 5×5 deep convolution, and then the GELU activation and 1×1 convolution are used to obtain a two-dimensional offset. In order to prevent the reference point offset from being forced to move, the bias term of the 1×1 convolution layer is deleted in the θoffset network structure to achieve a reasonable offset of the position point.

[0079] The software and hardware used in the experimental platform of this embodiment are shown in Table 1.

[0080] Table 1 Experimental software and hardware configuration table

[0081]

[0082] In this embodiment, evaluation indicators widely recognized in the field of target detection are used to quantify model performance, including precision (Precision, P), recall (Recall, R), mean average precision (MeanAverage Precision, mAP) and floating point operations (Floating point operations, FLOPs).

[0083] Precision: It indicates the ratio of the number of samples predicted by the model to the number of samples predicted by the model to be positive. The calculation formula is:

[0084]

[0085] (2) Recall rate: It indicates the ratio of the number of positive samples predicted by the model to the total number of positive samples in the sample. The calculation formula is:

[0086]

[0087] Among them, P and N represent the model prediction results as positive samples and negative samples respectively, while T and F indicate whether the prediction results are consistent with the true values. True Positive (TP) is a given sample whose actual label is the true class, and the model predicts that the sample is also the true class, which is considered a true positive. True Negative (TN) is a given sample whose actual label is the false class, and the model predicts that the sample is also the false class, which is considered a true negative. False Positive (FP) is a given sample whose actual label is the false class, but the model predicts that the sample is the true class, which is considered a false positive. False Negative (FN) is a given sample whose actual label is the true class, but the model predicts that the sample is the false class, which is considered a false negative.

[0088] (3) PR curve and AP: PR reflects the trade-off between precision and recall. This curve is used to evaluate the performance of classification models under different thresholds and is particularly useful when dealing with unbalanced data sets. The curve takes P as the vertical axis and R as the horizontal axis. It is drawn as follows: set a threshold, and values ​​greater than this threshold are classified as 1, and values ​​less than this threshold are classified as 0. Set the threshold from [0,1], and each threshold obtains (Presession, Recall). Connect all (Presession, Recall) to draw a PR curve. AP is the area enclosed by the PR curve and the coordinate axis, and its calculation formula is:

[0089]

[0090] Where P(R) represents the function of P with R as parameter.

[0091] (4) Mean average precision: represents the average value of AP of all categories. The calculation formula of mAP is:

[0092]

[0093] Floating point operations: The amount of calculation that reflects the complexity of the model. The calculation formula for FLOPs is:

[0094] FLOPs = (2C in K 2 +1)HWC out

[0095] This embodiment uses GFLOPs as an evaluation indicator to reflect the computational complexity of the model, which represents the total number of floating-point operations in units of 1 billion.

[0096] 1GFLOPs = 1×10 9 FLOPs

[0097] In order to verify the effectiveness of the module and explore the impact of the number of modules on network performance, a series of ablation experiments were conducted on the YOLOv5n model on the NEU-DET hot-rolled strip surface defect dataset. The dataset was divided into training and test sets at a ratio of 0.85:0.15. There were 1530 images in the training set and 270 images in the training set. The input image size was set to 224×224. In the experiment, by adding different numbers of ADown and DAT modules to the backbone network and neck network of the model, the performance of the model under different configurations was evaluated, including accuracy, recall rate and mean average precision (mAP). This was to explore the impact of ADown and DAT modules on model performance and computational efficiency in the YOLOv5n model. It is worth noting that the ADown module is added to both the backbone network and the neck network, among which ADown*1~ADown*4 are gradually added in the backbone network, while ADown*5 and ADown*6 are added to the neck network on the basis of ADown*4. Specifically, the Conv modules of the 1st, 3rd, 5th, and 7th layers of the backbone network and the 18th and 21st layers of the neck network are replaced with ADown modules layer by layer, and a total of 6 Conv modules are replaced. The purpose of replacing ADown layer by layer is to gradually replace the downsampling role played by the CBS module in YOLOv5n. The DAT module only replaces the C3 module with the C3_DAT module layer by layer in the 13th, 17th, 20th, and 23rd layers of the neck network to enhance the feature expression ability of the model. The total number of iterations of the ablation experiment is 500 rounds, and its performance and computational cost are shown in Table 2 below.

[0098] Table 2 Ablation experiment

[0099]

[0100] From the experimental results, it can be seen that as the number of ADown modules increases, the GFLOPs of the model continues to decrease, showing a significant reduction in computational cost. However, the precision, recall rate and mAP generally show a downward trend after adding more ADown modules. This is especially obvious in the configuration with more ADown modules. The reason may be that compared with the original CBS module, the ADown module uses Chunk to evenly divide the number of input channels and finally performs splicing. Although this strategy greatly reduces the number of parameters and floating-point calculations, it may lead to over-simplification of the model, resulting in the loss of input features. The ADown*2 configuration performs well in accuracy, but its lower recall rate may reflect the model's inadequacy in covering all positive samples, which indicates that while reducing false detections, the model may not effectively capture all relevant targets. After adding more ADown modules, the mAP reaches the lowest 0.722, further verifying the information loss caused by model simplification. Its reduced performance also suggests that excessive reduction in calculations and parameters may sacrifice the effectiveness of the model. After the C3_DAT module is introduced on the basis of YOLOv5n, the mAP of the model is significantly improved, especially in the C3_DAT*4 configuration, the mAP reaches 0.815, which proves that the C3_DAT module can significantly enhance the detection accuracy of the model. Compared with the ADown module, the C3_DAT module maintains a high accuracy while improving the recall rate, which shows that the C3_DAT module can better identify positive samples while reducing false positives, further proving its dual advantages in reducing false positives and enhancing positive sample recognition. Although the introduction of the C3_DAT module leads to an increase in GFLOPs, since C3_DAT introduces a deformable attention mechanism, compared with the traditional self-attention mechanism, its parameter amount and floating point number growth are greatly reduced, and GFLOPs only increase from 4.2 to 4.5. The present invention adopts the replacement of 6 ADown modules and 4 C3_DAT in the YOLOv5n network model as the final optimization scheme.

[0101] In this embodiment, under the same environment, four models of YOLOv5n, YOLOv5n+ADown, YOLOV5n+C3_DAT and YOLOv5n+ADown+C3_DAT are compared based on the NEU-DET data set. Taking YOLOv5n as the baseline model, YOLOv5n+ADown is to add six ADown modules to the baseline model. YOLOv5n+C3_DAT is to add four C3_DAT modules to the baseline model. Finally, YOLOv5n+ADown+C3_DAT is to add six ADown and four C3_DAT modules to the baseline model at the same time. The comparison results of these four models on the NEU-DET validation set are shown in Table 3. This experiment continues to train on the basis of the ablation experiment, using the weights of each model trained in the ablation experiment, freezing the network backbone layer, and iterating 1500 times to achieve the best experimental results.

[0102] Table 3 Comparative experiments on the NEU-DET dataset

[0103]

[0104] Figure 11 shows the PR curves of different models on the NEU-DET test set. The baseline model YOLOv5n has an AP value of 0.395 for crack defects and an overall mAP of 0.805. It can be seen from the PR curve of the YOLOv5n model that as the recall rate increases, the detection accuracy of most defect types gradually decreases. This shows that the model sacrifices a certain degree of accuracy in pursuit of higher coverage. The ADown module is introduced to reduce the model floating point calculation and model parameter quantity. As can be seen from Figure 11(b), its AP value for crack defects increases from 0.395 to 0.415, but the other defect categories all decrease, and the overall mAP of the model drops to 0.752. The C3_DAT module is used to introduce the attention strategy, and its AP value for crack defects increases from 0.395 to 0.429, and the overall mAP increases to 0.816. The YOLOv5n+C3_DAT model maintains high accuracy on most defect types, especially under high recall conditions. This shows that C3_DAT significantly improves the model's ability to detect defects. Combining the composite enhancement strategy of C3_DAT and ADown, the AP value of crack defects is further improved to 0.502, and the overall mAP is 0.814. Experiments show that the YOLOv5n+C3_DAT and YOLOv5n+ADown+C3_DAT models show higher precision and recall, which proves that the C3_DAT module significantly improves the recognition ability of the model. Especially in terms of the mAP indicator, both configurations exceed the baseline model, with YOLOv5n+C3_DAT achieving the highest average accuracy of 0.816. Adding the ADown module significantly reduces GFLOPs from the original 4.2 to 3.3, reducing the amount of computation by nearly 21%. Using the C3_DAT module alone increases the computational cost (GFLOPs increases to 4.5). The configuration that combines the ADown and C3_DAT modules improves performance while keeping GFLOPs at 3.7. Its detection accuracy mAP only loses 0.02 compared to YOLOv5n+C3_DAT. The YOLOv5n+ADown+C3_DAT configuration effectively balances the computational cost of the model while maintaining a high mAP. This finding highlights the complementarity of the ADown and C3_DAT modules, proving that this configuration is very practical when equipment conditions are limited.

[0105] In order to verify the universality of the improved model of this topic and the above experimental conclusions, this section will use the GC10-DET industrial metal surface defect dataset for verification. Due to the lack of labels in the GC10-DET dataset, a total of 2292 labels were collected in this experiment. The redundant photos are now deleted to 2292 photos corresponding to the labels. The various defect data of GC10-DET are uneven, and cross sampling is adopted. The experimental hardware and software are shown in Table 1. Since the image size of the DET dataset is about 1000*2048, and the existing equipment resources and video memory are limited, the input image size is adjusted to 640×640. The experimental results are shown in Table 4.

[0106] Table 4 Comparative experiments on the GC10-DET dataset

[0107]

[0108] The experimental results prove the effectiveness of the module and the versatility of the improved model. It is worth noting that in this experiment, the YOLOv5n+ADown model performed very well. The parameter GFLOPs dropped from 4.2 to 3.3, but the mAP only lost 0.08. This further proves that although the ADown module reduces the number of parameters and the amount of floating-point operations, the lost information may not be much. This method can be adopted when resources are limited. In the YOLOv5n+C3_DAT model, its advantage is not as obvious as the above experiment. The recall rate is only 0.581, which may be caused by insufficient equipment and iterations. The YOLOv5+ADown+C3_DAT model still maintains good performance and achieves a good balance between accuracy and floating-point operations.

[0109] In this study, by comparing the detection performance of the YOLOv5n+ADown+C3_DAT model and the YOLOv5n model, it was found that the former has significant advantages in defect classification and prediction of the detection frame position. In summary, the present invention proposes a lightweight metal sheet surface defect detection method based on improved YOLOv5n.

[0110] It is known from common technical knowledge that the present invention can be implemented by other embodiments that do not deviate from its spirit or essential features. Therefore, the above disclosed embodiments are only illustrative in all respects and are not exclusive. All changes within the scope of the present invention or within the scope equivalent to the present invention are included in the present invention.

Claims

1. A method for detecting surface defects of lightweight metal sheets based on improved YOLOv5n, comprising the following steps: S1: Image preprocessing; S2: Input the image into the model, wherein the model adopts the YOLOv5n network model; S3: Model training. The training process of the model goes through the backbone network, the neck network and the head network in sequence. The prediction results of the model and the true labels are used as inputs to calculate the loss function. Through the back propagation mechanism, the model can self-adjust the parameters of each part of the network model to achieve the expected effect. S4: Analyze and judge the data after model training, adjust and optimize the model, retain the optimal model weight, conduct experimental tests on the trained model, perform non-maximum suppression on the prediction results, and mark the final results on the original image as the output of the detection result; The invention is characterized in that an improved YOLOv5n network model is used to identify and detect images, wherein in the model, the original downsampling CBS module in the backbone network and the neck network is replaced by an ADown module to reduce the overall resource consumption of the model, wherein the ADown module is a downsampling convolution module in the YOLOv9 network model.

2. A method for detecting surface defects of lightweight metal sheets based on improved YOLOv5n as claimed in claim 1, characterized in that: In step S2, a DAT module is added to the Bottleneck module in the C3 component of the neck network. In the design of the Bottleneck module, the number of input channels is effectively reduced through the two preceding convolutional layers and the original convolutional layers of the C3 module. The DAT module introduces deformable DETR into the DETR model and enhances the focus on key points around the reference point by adopting a deformable attention mechanism, thereby improving the detection capability of small objects and shortening the training time. The working process of the DAT module includes: First, the input feature is converted to q through a linear mapping, and then a lightweight network is used to generate an offset Δp, which will be used to adjust the position of the key point to make it closer to the target area; Coordinate normalization and amplitude scaling operations are used to prevent the offset from being too large to ensure the stability of the training process; On this basis, through the multi-head attention mechanism, these adjusted keys and values ​​are accurately focused and weighted, enhancing the model's ability to capture key areas; In addition, the relative position bias is considered to further enhance the expressiveness of the attention pattern, and the final output is obtained through a linear transformation.

3. A method for detecting surface defects of lightweight metal sheets based on improved YOLOv5n as claimed in claim 2, wherein in the DAT module, in order to achieve accurate feature positioning and dynamic sampling of key points, an offset calculation method is used to generate the key points required in the DAT module: Specific methods include: Given input features in the H×W×C space: x∈R H×W×C Generate a series of reference points: To obtain the offset of each reference point, the feature map is linearly projected to the query, labeled q, and then fed to the subnetwork θ offset Generate an offset Δp and scale it, then combine the input features, reference points and their offsets to generate the deformed features through the sampling function φ(·) The process can be expressed by the following formula: in, and Respectively represent the position sampling feature generation key and value of the reference point after offset, W q , W k and W v The projection matrix of the keys and values ​​generated by sampling features of the marker point q and the reference point position after offset is the sampling function φ(·) which is bilinear interpolation and is calculated as follows: Among them, (r x ,r y ) index z∈R H×W×C All positions on; (p x ,p y ) is the coordinate of the reference point p+Δp after the offset; g(p x ,r x )=max(0,1-|p x -r x |) g(p y ,r y )=max(0,1-|p y -r y |) Then, a multi-head attention mechanism is used, combined with the relative position offset R, to output the feature z (m) : Where m is the number of attention heads, σ represents the normalized exponential function, and d is the size of each attention head. The bias matrix of the relative position deviation of each head is connected together and projected to obtain the final output z.

4. A method for detecting surface defects of lightweight metal sheets based on improved YOLOv5n as claimed in claim 3, characterized in that: The subnetwork θ offset In the structure of , it first passes through a depthwise separable convolution. Then, based on the depthwise separable convolution, the network passes through a GELU nonlinear activation function to further enhance the feature representation capability. Finally, a 1×1 convolution layer is used to generate the final offset. To prevent the reference point offset from being forced to move, θ offset The bias term of the 1×1 convolutional layer is deleted from the network structure.

5. The method for detecting surface defects of lightweight metal sheets based on improved YOLOv5n according to claim 1, characterized in that: In step S1, the image preprocessing includes three parts: data set loading, data enhancement, and prior frame setting. First, the metal sheet defect sample data set is loaded into the memory. Then, in order to enhance the adaptability and generalization of the model to the metal sheet defect environment, the image preprocessing operation is performed. Mosaic data enhancement is performed on the input image, and multiple images that have been randomly scaled, cropped, and color adjusted are randomly spliced ​​to improve the generalization ability of the model. Finally, the initially set anchor frame is verified. If the recall rate of the anchor frame is lower than 0.98, the anchor frame size is recalculated.

6. The method for detecting surface defects of lightweight metal sheets based on improved YOLOv5n according to claim 1, characterized in that: The ADown module first reduces the spatial dimension of the input feature map through an average pooling layer, and then divides the feature map into two equal parts. Each part is processed independently to capture different features. The first part extracts more detailed features through a 3×3 convolution layer with a step size of 2. The second part strengthens the extraction of key features through a maximum pooling operation and adjusts the channel dimension through a 1×1 convolution to further optimize information transmission. Finally, the two processed feature maps are merged to ensure that the output feature map contains both local features and retains the original information to a certain extent.

Citation Information

Patent Citations

  • Construction site safety helmet detection method based on improved YOLOv5

    CN117423062A

  • Steel surface defect detection method based on improved YOLOv5 algorithm

    CN118396979A

  • Target identification method based on improved YOLOv5s intelligent orchard fruits

    CN118447386A

Cited By

  • Metal material intelligent sensing and positioning method based on deep learning, terminal equipment and storage medium

    CN120726310A

  • Perovskite cell panel surface defect detection method based on improved Yolov9

    CN121616527A

  • Event detection method, system and device for optical cable detection and storage medium

    CN121808706A

  • Event detection methods, systems, devices, and storage media for optical cable detection

    CN121808706B