Steel workpiece surface defect detection method based on multiple attention mechanisms
By introducing SCSA collaborative attention and EMA multi-scale attention modules into the YOLOv8 framework, and combining dataset preprocessing and loss function optimization, the accuracy and robustness issues of surface defect detection of steel workpieces in complex industrial scenarios are solved, achieving efficient identification and accurate detection of minute defects.
Patent Information
- Application Number
- CN202610013689.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-01-07
- Publication Date
- 2026-02-06
AI Technical Summary
In complex industrial scenarios, existing technologies such as YOLO series algorithms suffer from insufficient feature extraction and inadequate detection accuracy for detecting surface defects in steel workpieces. This is especially true for complex workpiece surfaces with severe background interference, minute defects, and slender cracks, which are prone to missed or false detections.
A detection method based on a multi-attention mechanism is adopted, including the construction of the SCSA collaborative attention module and the EMA multi-scale attention module, which are integrated into the YOLOv8 framework. Combined with dataset preprocessing and targeted enhancement, a joint loss function is designed to improve the model's generalization ability and detection accuracy.
It effectively improves the model's ability to identify defects under complex background interference, enhances the detection accuracy and adaptability of defects such as micro-cracks, maintains good real-time detection performance, and is suitable for various actual detection scenarios of surface defects on steel workpieces.
Smart Images

Figure CN121482033A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of defect detection, in particular to a steel workpiece surface defect detection method based on a multiple attention mechanism. BACKGROUND
[0002] As the most basic and core component in the manufacturing process, the processing quality of steel workpieces directly determines the overall manufacturing foundation capability and product level. In actual production, due to factors such as manufacturing process, forging technology and raw material performance, small cracks, scratches, bubbles and depressions often appear on the surface of steel workpieces. These defects not only damage the structural integrity and service life of the workpiece itself, but also may pose a potential threat to the performance of the final product, thereby affecting the stability and efficiency of the production system. Therefore, realizing the rapid and accurate identification of steel workpiece surface defects has become a key technical problem to be solved in the current manufacturing industry.
[0003] In recent years, with the rapid development of deep learning technology, target detection algorithms based on convolutional neural networks (CNN) have become a research hotspot in industrial visual detection. Among them, the YOLO series single-stage algorithm models the detection task as a regression problem, realizing efficient detection from end to end, improving the inference speed while maintaining high accuracy, and has become one of the widely used frameworks in industrial defect detection. However, as the industrial detection scene becomes increasingly complex and the defect morphology becomes more diverse, the YOLO series algorithm still has problems of insufficient feature extraction and insufficient detection accuracy when dealing with complex workpiece surfaces with serious background interference, small defects, long cracks or low contrast areas. SUMMARY
[0004] The purpose of the present application is to solve the problem of missed detection and false detection caused by complex industrial scene interference, small or long targets in the prior art, and to realize accurate and robust detection of steel workpiece surface defects.
[0005] To achieve the above purpose, the present application provides a steel workpiece surface defect detection method based on a multiple attention mechanism, comprising the following steps: S1, data set preprocessing: constructing a steel workpiece surface defect data set, dividing it into a training set, a validation set and a test set, and preprocessing the image samples in the data set; S2, model construction and improvement: designing a C2f_SCSA module, integrating the C2f_SCSA module into YOLOv8 to obtain a YOLOv8-SCSA steel workpiece surface defect detection model; YOLOv8 is a prior art; S3, further improvement of the model: constructing an EMA multi-scale attention module, integrating the EMA multi-scale attention module into the YOLOv8-SCSA model to obtain a YOLOv8-SCSA-EMA model; S4, loss function: construct a joint loss function; S5, model training: input the preprocessed training set into the YOLOv8-SCSA-EMA model to complete the training of the model; S6, defect detection: using the trained YOLOv8-SCSA-EMA model to detect the surface defects of the steel workpiece, and generating a detection frame set.
[0006] Further, step S1 specifically comprises: S11, data set selection and division: selecting a suitable steel workpiece surface defect data set according to the steel workpiece to be detected, the surface defects in the data set including cracks, inclusions, patches, pitting surfaces, scales and scratches; the size of the original image in the data set is uniformly expanded to the same size by bilinear interpolation, and the data set is divided into training set, validation set and test set in the ratio of 8:1:1; S12, preprocessing: preprocessing the samples in the training set by using data enhancement technology; the data enhancement technology includes Mosaic splicing, image geometric transformation including random scaling, translation and flipping, and color attribute adjustment including HSV disturbance, brightness and contrast change; S13, targeted data enhancement: targeted data enhancement is performed on the defects with insufficient samples in the training set; geometric and image filter transformation enhancement strategies including horizontal and vertical flipping, random rotation, Gaussian blur and limited contrast adaptive histogram equalization are specifically used.
[0007] Further, in step S2, the C2f_SCSA module specifically comprises: constructing an SCSA collaborative attention module, embedding SCSA into the Bottlneck unit in the C2f module to obtain the C2f_SCSA module; The Bottleneck unit of the C2f_SCSA module includes 2 CBS, 1 SCSA and 1 residual connection, the SCSA is located between the two CBS, and the residual connection is located at the end.
[0008] Further, the SCSA collaborative attention module is composed of shared multi semantic space attention SMSA and progressive channel self-attention PCSA, specifically: SMSA first performs global average pooling on the input features respectively in the height dimension and the width dimension to obtain and ; then, along the channel dimension, divide and into 4 independent sub-features and ; then, sent into the MS-DWConv1d module, which uses four one-dimensional convolution kernels with lengths of 3, 5, 7, and 9 , respectively, to convolve the sub-features and along the channel direction, to obtain sub-features and containing spatial structure information ; subsequently, the feature maps and are spliced along the channel dimension, and the spliced result is applied with group normalization GroupNorm-4 with a group number of 4 and activated with a Sigmoid function, to generate spatial attention weight matrices and ; finally, the and are point multiplied with the input feature , to obtain the SMSA shared multi-semantics spatial attention enhanced feature : ; PCSA first performs average pooling AvgPool on the output of SMSA using a pooling kernel with a size of and a step of 7, and performs group normalization GroupNorm-1 on the pooled result with a group number of 1; then, the normalized result is linearly mapped in the channel dimension to query Q, key K, and value V through three independent linear mapping matrices , and ; then, the channel similarity between the query Q and the key K is modeled using channel single-head self-attention CA-MHSA, and the value is enhanced in the channel dimension based on the similarity, to obtain the enhanced result : ; The is subjected to spatial global average pooling AvgPool and Sigmoid function activation, to obtain the channel attention weight matrix ; the is point multiplied with the channel attention weight matrix , to obtain the final output of the SCSA collaborative attention module.
[0009] Further, in step S2, the integration of the C2f_SCSA module into YOLOv8 is specifically: replacing the four C2f modules in the YOLOv8 backbone network Backbone and the four C2f modules in the feature pyramid network Neck with the C2f_SCSA module in sequence.
[0010] Further, in step S3, the EMA multi-scale attention module is composed of channel grouping, multi-branch parallel subnetwork, and cross-space learning module, specifically: The channel grouping first divides the input feature into 3 sub-features along the channel direction, and combines the 3 sub-features along the batch direction to obtain the combined feature . In the multi-branch parallel subnetwork, the first and second branches respectively perform global average pooling on the combined feature along the height and width dimensions, and then use convolution kernels to perform channel convolution on the two pooling results, and then generate attention weight matrices in the height direction and the width direction through the Sigmoid activation function and . After multiplying the two attention weight matrices and with the combined feature , point multiplication is performed, and the point multiplication result is processed by group normalization with a group number of 1 to obtain the enhanced feature ; at the same time, the third branch uses a convolution kernel with a size of to perform spatial convolution on the combined feature , and the convolution result is . The cross-space learning module first performs spatial global average pooling and Softmax nonlinear activation on the outputs and of the multi-branch parallel subnetwork in parallel to generate attention weight matrices and ; then, respectively use and , and to perform point multiplication operations to enhance the features and , and add the enhanced features and together and then pass through the Sigmoid activation function again to fuse into the final weight matrix ; and perform point multiplication on the combined feature , and reshape the dimension of the point multiplication result to be the same as the input featureConsistent, get the output of the EMA multi-scale attention module .
[0011] Further, in step S3, the EMA multi-scale attention module is integrated into the YOLOv8-SCSA model, specifically: the EMA multi-scale attention module is inserted into the three detection heads of the YOLOv8-SCSA model, respectively, before the three detection heads.
[0012] Further, the construction of the joint loss function in step S4 is as follows: The joint loss function is constructed by CIoU function loss , cross-entropy loss and distribution focus loss : ; Among them, Considering the overlapping area, center point distance and aspect ratio difference between the predicted frame and the real frame, measure the difference between the predicted class label and the real class label, convert the bounding box regression problem into a discrete probability distribution learning problem for its coordinate values.
[0013] Further, the model training in step S5 is as follows: S51, set the hyperparameters including learning rate, training rounds, and batch size; S52, input the preprocessed training set into the YOLOv8-SCSA-EMA network for N +10 rounds of iterative training, in each round of training before N , the Mosaic data augmentation strategy is used for dynamic data augmentation for each batch; after the model forward inference, the loss is calculated by comparing the predicted result with the real label, and then the network parameters are updated by back propagation; S53, the whole training process continuously evaluates the model performance through the validation set.
[0014] Further, the model training uses the stochastic gradient descent (SGD) optimizer, with an initial learning rate of 0.005, a momentum of 0.937, a weight decay coefficient of 0.0005, a training round of 230 rounds, and a batch size of 64.
[0015] The steel workpiece surface defect detection method based on the above multi-attention mechanism has the following beneficial effects: (1) The present application effectively improves the generalization ability and defect recognition of the model by systematically preprocessing and targetedly enhancing the data set. (2) The SCSA cooperative attention module is embedded into the YOLOv8 framework to construct a YOLOv8-SCSA model, the SMSA focuses on the defect significant area and suppresses the complex background interference, the PCSA strengthens the expression of the key feature channel, and the cooperation of the two can make the model more effectively capture the difficult-to-identify defect features such as micro cracks under complex background interference; (3) The EMA multi-scale attention module is further introduced to construct a YOLOv8-SCSA-EMA detection model, different receptive field features are captured through a parallel multi-branch structure, cross-dimension interaction is established, the model can effectively detect different scale defects such as micro scratches and larger depressions at the same time, and the adaptability of the model to micro targets is effectively improved; (4) The model composite total loss function composed of CIoU loss, binary cross entropy (BCE) and distribution focal loss (DFL) is designed, and the balance between detection accuracy and model stability is realized. BRIEF DESCRIPTION OF DRAWINGS
[0016] Figure 1 is a flowchart of an embodiment of the present application; Figure 2 is an effect diagram of training set enhancement of an embodiment of the present application; Figure 3 is a network structure of a YOLOv8 model of an embodiment of the present application; Figure 4 is a structure of an SCSA cooperative attention module of an embodiment of the present application; Figure 5 is a structure of a C2f_SCSA module and a bottleneck unit thereof of an embodiment of the present application; Figure 6 is a structure of an EMA multi-scale attention module of an embodiment of the present application; Figure 7 is a whole structure of a YOLOv8-SCSA-EMA model of an embodiment of the present application; Figure 8 is a defect detection effect and feature heat map of an embodiment of the present application. DETAILED DESCRIPTION
[0017] To make the purpose, technical scheme and advantages of the present application clearer and more intelligible, the present application is further described in detail below in combination with specific embodiments and with reference to the drawings. It should be understood that these descriptions are only exemplary and are not intended to limit the scope of the present application. In addition, in the following description, the description of well-known structures and technologies is omitted to avoid unnecessary confusion of the concept of the present application.
[0018] As Figure 1As shown, the steel workpiece surface defect detection method based on the multiple attention mechanism of the embodiment of the application comprises: Device: CPU is 16 vCPU Intel(R) Xeon(R) Platinum 8481C, GPU is NVIDA GeForce RTX 4090 (24GB), memory is 80GB, Python version is Python3.9, deep learning framework is Pytorch2.0.0, and CUDA version is 11.8.
[0019] S1, select the data set and pretreat: The steel workpiece surface defect image data set is constructed, divided into a training set, a validation set and a test set according to a preset ratio, and the training set is pretreated and subjected to targeted data enhancement processing.
[0020] S11, data set selection and division: select the NEU-DET steel workpiece surface defect data set, which contains 6 typical hot-rolled steel strip surface defects of cracks, inclusions, patches, pitting surfaces, scales and scratches; the size of the original image in the data set is uniformly expanded from 200x200 to 768x768 through bilinear interpolation, and the data set is divided into a training set, a validation set and a test set according to a ratio of 8:1:1; S12, pretreatment: a plurality of data enhancement techniques including Mosaic splicing, image random scaling, translation, flipping and other geometric transformations, and HSV disturbance, brightness and contrast change and other color attribute adjustments are used to pretreat the samples in the training set; S13, targeted data enhancement: for the crack defects in the training set with small sample quantity, scattered distribution and fuzzy boundary, targeted data enhancement is performed; specifically, horizontal and vertical flipping, random rotation, Gaussian blur, limited contrast adaptive histogram equalization and other geometric and image filtering transformation enhancement strategies are used, and the enhancement effect is as shown in Figure 2 After this processing, a total of 756 high-quality crack defect samples are generated, and the training set is expanded with them.
[0021] S2, model construction and improvement: A basic detection model with YOLOv8 as the core framework is built, a C2f_SCSA module is designed, the C2f_SCSA module is integrated into YOLOv8, and a YOLOv8-SCSA steel workpiece surface defect detection model is obtained.
[0022] S21, build a YOLOv8 network model, which mainly includes an input layer, a backbone network, a feature pyramid network (Neck) and a detection head (Head), as shown in Figure 3 .
[0023] The backbone network adopts an alternating stack structure of 5 CBS modules and 4 C2f modules: the CBS module is a basic feature extraction unit that sequentially performs convolution, batch normalization, and Sigmoid function activation operations. The C2f module is a lightweight feature extraction structure composed of two CBS convolution layers and a Bottlneck unit. The first CBS compresses the input channel number to , and splits the features into two branches branch1 and branch2 along the channel direction. Branch1 remains unchanged, and branch2 flows through the Bottleneck unit to extract deep features. Finally, the features of the two branches are spliced in the channel dimension, and the channel number of the spliced features is adjusted to be consistent with that of the C2f module through the last CBS convolution layer. The SPPF module is used at the end of the backbone network to expand the receptive field through serial pooling and multi-scale fusion operations.
[0024] The feature pyramid network (Neck) adopts an improved PANet architecture. First, the deep features containing global semantic information output by the SPPF module of the backbone network are up-sampled through the "top-down" path, and gradually fused with the shallow features output by the third and second C2f modules of the backbone network and , enabling the model to utilize context semantic information to infer "fuzzy boundary" defects such as cracks. Then, through the "bottom-up" path, the shallow features fused with details are transmitted upwards to enhance the positioning accuracy of deep features for small defects. Finally, three feature layers fused with multi-scale information , and are output.
[0025] The three detection heads (Head) of the detection end all adopt a decoupled anchor-free lightweight structure, connected to the three scale features , and output by the feature pyramid network Neck. They process the type judgment and positioning of defects through two independent and parallel classification and regression tasks, respectively, to realize the synchronous prediction of small, medium, and large targets.
[0026] S22, a SCSA collaborative attention module is constructed, which is composed of a shared multi-semantic spatial attention SMSA and a progressive channel self-attention PCSA, as shown in Figure 4 , specifically: SMSA first performs global average pooling on the input features in the height and width dimensions, respectively, to obtain With ; then, respectively along the channel dimension With is divided into 4 independent sub-features and ; then, sent into the MS-DWConv1d module, the MS-DWConv1d module uses four one-dimensional convolution kernels with lengths of 3, 5, 7, and 9 , respectively, to perform convolution on the sub-features and along the channel direction, to obtain sub-features and containing spatial structure information ; subsequently, respectively concatenate the feature maps and along the channel dimension, apply group normalization GroupNorm-4 with a group number of 4 to the concatenation result, and use the Sigmoid function activation to generate spatial attention weight matrices and ; finally, perform point multiplication operation on and and the input feature , to obtain the SMSA shared multi-semantics spatial attention enhanced feature : ; PCSA first performs average pooling AvgPool on the output of SMSA using a pooling kernel with a size of and a step of 7, and performs group normalization GroupNorm-1 with a group number of 1 on the pooled result ; then, linearly map the normalized result in the channel dimension to query Q, key K, and value V through three independent linear mapping matrices , and ; then, model the channel similarity of query Q and key K using channel single-head self-attention CA-MHSA, and perform channel enhancement on value based on the similarity, to obtain the enhanced result : ; perform spatial global average pooling AvgPool and Sigmoid function activation on , to obtain a channel attention weight matrix ; perform point multiplication operation on and the channel attention weight matrix , to obtain the final output of the SCSA collaborative attention module .
[0027] S23, embedding the constructed SCSA cooperative attention module into the Bottleneck unit in the C2f module to obtain a C2f_SCSA module; As shown in Figure 5 , the Bottleneck unit of the C2f_SCSA includes two CBSs, one SCSA, and one residual connection. The first CBS compresses the number of input feature channels to of the original number by channel convolution with a convolution kernel size of The SCSA cooperative attention module enhances the spatial and channel attention of the output features of the first CBS. The second CBS extracts local spatial information from the SCSA attention-enhanced features by spatial convolution with a convolution kernel size of and a step size of 1. The residual connection element-wise adds the input of the Bottleneck to the output of the second CBS to obtain the output of the improved Bottleneck unit.
[0028] S24, replacing the four C2f modules in the backbone network of the YOLOv8 constructed in step S21 and the four C2f modules in the feature pyramid network Neck with the constructed C2f_SCSA module to obtain an improved YOLOv8-SCSA steel workpiece surface defect detection model.
[0029] S3, model further improvement: Constructing an EMA multi-scale attention module, integrating the EMA multi-scale attention module into the YOLOv8-SCSA model to obtain a YOLOv8-SCSA-EMA model.
[0030] S31, constructing an EMA multi-scale attention module: the EMA multi-scale attention module is composed of channel grouping, multi-branch parallel subnetwork, and cross-space learning module, as shown in Figure 6 , specifically: The channel grouping first divides the input feature into three sub-features along the channel direction, and combines the three sub-features along the batch direction to obtain a combined feature In the multi-branch parallel subnetwork, the first and second branches perform global average pooling on the combined feature along the height and width dimensions, respectively, and then perform channel convolution on the two pooling results using convolution kernels, and then generate attention weight matrices and in the height direction and the width direction through the Sigmoid activation function, respectively. and matrix multiplication and group normalization with a group size of 1 Point multiplication is performed, and the point multiplication result is group normalized with a group size of 1 to obtain enhanced features ; at the same time, the third branch uses a convolution kernel with a size of to perform spatial convolution on the combined features , and the convolution result is ; The cross-space learning module first performs spatial global average pooling and Softmax nonlinear activation on the outputs of the multi-branch parallel subnetworks and respectively, and generates attention weight matrices and in parallel; then, point multiplication is performed using and , and to enhance features and , and the enhanced features and are added and then passed through a Sigmoid activation function again to be fused into the final weight matrix ; and and the combined features are multiplied, and the dimension of the multiplication result is reshaped to be consistent with the input features , to obtain the output of the EMA multi-scale attention module .
[0031] S32, the EMA multi-scale attention module is inserted into the three detection heads Head of the YOLOv8-SCSA model constructed in step 2 respectively, to obtain the final YOLOv8-SCSA-EMA steel workpiece surface defect detection model, and the overall structure is shown in Figure 7 .
[0032] S4, loss function: a joint loss function composed of CIoU function loss , cross-entropy loss and distribution focus loss : : ; wherein considering the overlapping area, center point distance and length-width ratio difference between the predicted box and the real box, measuring the difference between the predicted class label and the real class label, transforming the bounding box regression problem into a discrete probability distribution learning problem for its coordinate values.
[0033] S5、Model training: The preprocessed training set is input into the YOLOv8-SCSA-EMA model, and the learning rate, batch size, and other hyperparameters are set to complete the end-to-end training of the model. Specifically: The model training uses the stochastic gradient descent (SGD) optimizer, with an initial learning rate of 0.005, a momentum of 0.937, a weight decay coefficient of 0.0005, a training round number of 230, and a batch size of 64.
[0034] During training, the preprocessed training set from step S1 is input into the YOLOv8-SCSA-EMA network for 230 rounds of iterative training. In the first 220 rounds of training, the Mosaic data augmentation strategy is used to dynamically augment each batch to improve the model's generalization ability. After the model's forward inference, the loss is calculated by comparing the predicted results with the true labels, and then the network parameters are updated through backpropagation.
[0035] The entire training process continuously evaluates the model's performance using the validation set, and ultimately trains a well-performing YOLOv8-SCSA-EMA detection model.
[0036] S6、Model detection effect: The YOLOv8-SCSA-EMA model trained in step S5 is used to detect defects in the test data divided in step S1, and the detection results are comprehensively evaluated and visually analyzed.
[0037] S61、The trained YOLOv8-SCSA-EMA model is used to infer the test data to generate the final detection frame, specifically: As shown in Figure 7 , the model extracts the multi-scale features 、 and of the input image through the backbone network Backbone, the feature pyramid network Neck, and the attention mechanism module EMA, with resolutions from high to low, respectively used to detect small, medium, and large targets; then, the feature maps of each scale are parallelly passed through 3 detection heads Head, each of which generates a batch of candidate boxes containing bounding box coordinates, target confidence, and class probability, and all candidate boxes are combined into a unified set; then, the model discards the candidate boxes with a confidence lower than the preset threshold of 0.25 in the set, and further removes the candidate boxes with an overlap exceeding the preset threshold of 0.7 using non-maximum suppression (NMS), to obtain the final detection frame set.
[0038] S62, based on the inference results on the test set in step S61, calculate multiple performance indicators to objectively and quantitatively evaluate the comprehensive detection performance and inference efficiency of the model, as follows: Select precision, recall, mean average precision (mAP), and frames per second (FPS) as four quantitative indicators to comprehensively evaluate the detection effect of the model. Precision measures the proportion of true positive samples among all samples predicted as positive by the model; recall measures the proportion of correctly identified samples among all positive samples; mean average precision (mAP) is the arithmetic mean of the average precision of each class, used to evaluate the overall performance of the model; and frames per second (FPS) is the number of images processed per second, reflecting the inference speed of the model.
[0039] The inference results are evaluated according to the four objective indicators in parallel, and compared with other models, and the specific indicator comparison results are shown in Table 1.
[0040] Table 1 Comparison of detection results of YOLOv8-SCSA-EMA and other models on NEU-DET dataset
[0041] According to the results in Table 1, under the condition of inference speed of 109 frames per second, the performance indicators of YOLOv8-SCSA-EMA model are significantly better than those of other models. Specifically, the indicators Precision, Recall, and mAP@0.5 are improved by 2.1%, 6.7%, and 6.6% respectively compared with the baseline model YOLOv8, and by 2.1%, 8.0%, and 4.3% respectively compared with the improved model YOLOv8-SCSA.
[0042] S63, visualization analysis of model prediction results and decision basis: The bounding boxes obtained in step S61 are superimposed on the original test image to generate intuitive detection effect diagram, clearly showing the positioning and classification results of the model for each type of target. At the same time, based on gradient weighted class activation mapping (Grad-CAM), a feature heat map is generated to intuitively show the key areas of the model's defect detection, and to compare and analyze the effectiveness of the model. The detection effect and feature heat map are shown in Figure 8 .
[0043] from Figure 8The visualization results show that the YOLOv8-SCSA-EMA model performs better than other models in both the prediction result map and the heat map. Specifically, the YOLOv8-SCSA-EMA model has a higher confidence in the prediction box, successfully detects the missed targets in some defect samples, and the generated heat map has a higher degree of fit with the defect area.
[0044] The comprehensive results show that the YOLOv8-SCSA-EMA model significantly improves the detection accuracy and effectively reduces the missed detection rate while maintaining good real-time detection performance, achieving a better balance between accuracy and efficiency, and having high engineering application value, suitable for various steel workpiece surface defect detection scenarios.
[0045] Therefore, the steel workpiece surface defect detection method based on the multiple attention mechanism is adopted, the generalization ability and defect recognition of the model are effectively improved by systematic preprocessing and targeted enhancement of the data set, the SCSA collaborative attention module is embedded into the YOLOv8 framework to build the YOLOv8-SCSA model, the SMSA focuses on the defect saliency region and suppresses the complex background interference, the PCSA strengthens the expression of the key feature channel, and the two can cooperate to make the model more effectively capture the defect features such as small cracks that are difficult to identify under complex background interference; further introduce the EMA multi-scale attention module to build the YOLOv8-SCSA-EMA detection model, capture different receptive field features through parallel multi-branch structure, establish cross-dimensional interaction, so that the model can effectively detect defects of different scales such as small scratches and large depressions at the same time, effectively improve the adaptability of the model to small targets; a model composite total loss function composed of CIoU loss, binary cross entropy (BCE) and distribution focal loss (DFL) is designed to balance the detection accuracy and model stability. It has high engineering application value and is suitable for various steel workpiece surface defect detection scenarios.
[0046] Finally, it should be noted that: the above examples are only used to illustrate the technical solutions of the present application and not to limit them, although the present application has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solutions of the present application can still be modified or replaced by equivalents, and these modifications or equivalent replacements cannot make the modified technical solutions deviate from the spirit and scope of the technical solutions of the present application.
Claims
1. A method for detecting surface defects in steel workpieces based on a multiple attention mechanism, characterized in that, Includes the following steps: S1. Dataset preprocessing: Construct a dataset of surface defects on steel workpieces, divide it into training set, validation set and test set, and preprocess the image samples in the dataset. S2. Model Construction and Improvement: The C2f_SCSA module was designed and integrated into YOLOv8 to obtain the YOLOv8-SCSA steel workpiece surface defect detection model; YOLOv8 is the existing technology. S3. Further model improvement: Construct an EMA multi-scale attention module and integrate it into the YOLOv8-SCSA model to obtain the YOLOv8-SCSA-EMA model; S4. Loss Function: Construct the joint loss function; S5. Model Training: Input the preprocessed training set into the YOLOv8-SCSA-EMA model to complete the training of the model; S6. Defect Detection: Use the trained YOLOv8-SCSA-EMA model to detect surface defects in steel workpieces and generate a set of detection boxes.
2. The method for detecting surface defects in steel workpieces based on a multiple attention mechanism according to claim 1, characterized in that, Step S1 specifically includes: S11. Dataset Selection and Division: Select a suitable dataset of surface defects of steel workpieces to be inspected. The surface defects in the dataset include cracks, inclusions, patches, pitting, oxide scale and scratches. The original images in the dataset are uniformly expanded to the same size by bilinear interpolation, and the dataset is divided into training set, validation set and test set in a ratio of 8:1:
1. S12. Preprocessing: Data augmentation techniques are used to preprocess the samples in the training set; the data augmentation techniques include: Mosaic stitching, including image geometric transformations such as random scaling, translation, and flipping of images, and color attribute adjustments including HSV perturbation, brightness and contrast changes; S13. Targeted Data Augmentation: Targeted data augmentation is performed to address the deficiency of insufficient samples in the training set; specifically, geometric and image filtering transformation enhancement strategies are adopted, including horizontal and vertical flipping, random rotation, Gaussian blur, and contrast-limited adaptive histogram equalization.
3. The method for detecting surface defects in steel workpieces based on a multiple attention mechanism according to claim 1, characterized in that, In step S2, the C2f_SCSA module specifically involves: constructing an SCSA collaborative attention module, embedding SCSA into the Bottelneck unit in the C2f module, and obtaining the C2f_SCSA module; The Bottleneck unit of the C2f_SCSA module contains two CBSs, one SCSA, and one residual connection. The SCSA is located between the two CBSs, and the residual connection is located at the end.
4. The method for detecting surface defects in steel workpieces based on a multiple attention mechanism according to claim 3, characterized in that, The SCSA collaborative attention module consists of shared multi-semantic space attention (SMSA) and progressive channel self-attention (PCSA), specifically: SMSA first processes the input features Global average pooling is performed on both the height and width dimensions to obtain... and Next, along the channel dimension, respectively and Divided into 4 independent sub-features and Then, it is fed into the MS-DWConv1d module, which uses four one-dimensional convolutional kernels with lengths of 3, 5, 7, and 9. For each sub-feature and Convolution along the channel direction yields sub-features containing spatial structure information. and , ; Subsequently, the feature maps were respectively and The concatenation is performed along the channel dimension, and a group normalization of 4 groups (GroupNorm-4) is applied to the concatenation result. Then, the sigmoid function is used for activation to generate a spatial attention weight matrix. and Finally, and Input features Perform a dot product operation to obtain the features enhanced by shared multi-semantic space attention in SMSA. : ; PCSA first adopts a size of The output of the pooling kernel with a step size of 7 for SMSA Perform average pooling (AvgPool) and analyze the pooling results. Perform group normalization (GroupNorm-1) with a group size of 1; then, use three independent linear mapping matrices. , and The normalized results are linearly mapped onto the channel dimension as query Q, key K, and value V. Then, channel-based single-head self-attention CA-MHSA is used to model the channel similarity between query Q and key K, and the value is then mapped based on this similarity. Channel enhancement is performed to obtain the enhanced result. : ; right Spatial global average pooling (AvgPool) and sigmoid activation are performed to obtain the channel attention weight matrix. ;Will With channel attention weight matrix Perform a dot product operation to obtain the final output of the SCSA collaborative attention module. .
5. The method for detecting surface defects in steel workpieces based on a multiple attention mechanism according to claim 1, characterized in that, In step S2, the integration of the C2f_SCSA module into YOLOv8 specifically involves replacing the four C2f modules in the YOLOv8 backbone network and the four C2f modules in the feature pyramid network Neck with the C2f_SCSA module in sequence.
6. The method for detecting surface defects in steel workpieces based on a multiple attention mechanism according to claim 1, characterized in that, In step S3, the EMA multi-scale attention module consists of three parts: channel grouping, multi-branch parallel sub-network, and cross-space learning module, specifically: Channel grouping first divides the input features The feature is divided into three sub-features along the channel direction, and these three sub-features are combined along the batch direction to obtain the combined feature. ; In the multi-branch parallel subnetwork, the first and second branches combine features along the height and width dimensions, respectively. Perform global average pooling, then utilize The convolution kernel performs channel convolution on the two pooling results, and then generates attention weight matrices in the height and width directions through the sigmoid activation function. and These two attention weight matrices and Matrix multiplication and combination features Perform a dot product and then normalize the result by grouping it into groups of 1 to obtain the enhanced features. Meanwhile, the third branch uses a size of Convolution kernel pairs combine features Performing spatial convolution yields the following convolution result: ; The cross-space learning module first processes the output of the multi-branch parallel sub-network. and Spatial global average pooling and Softmax nonlinear activation are performed separately to generate attention weight matrices in parallel. and Subsequently, they used... and , and Perform dot product operations to enhance features. and This will enhance the features. and After addition, the weights are passed through the Sigmoid activation function again to merge them into the final weight matrix. ;Will and combined features Perform a dot product and reshape the dimension of the result to match the input features. Consistent, the output of the EMA multiscale attention module is obtained. .
7. The method for detecting surface defects in steel workpieces based on a multiple attention mechanism according to claim 1, characterized in that, In step S3, integrating the EMA multi-scale attention module into the YOLOv8-SCSA model specifically involves inserting the EMA multi-scale attention module before the heads of the three detection heads of the YOLOv8-SCSA model.
8. The method for detecting surface defects in steel workpieces based on a multiple attention mechanism according to claim 1, characterized in that, Step S4, which involves constructing the joint loss function, specifically involves: Constructing the loss function based on CIoU Cross-entropy loss and distribution focus loss Joint loss function : ; in, Taking into account the overlap area between the predicted bounding box and the ground truth bounding box, the distance between their center points, and the difference in aspect ratio, Measure the difference between the predicted category label and the true category label. The bounding box regression problem is transformed into a problem of learning the discrete probability distribution of its coordinate values.
9. The method for detecting surface defects in steel workpieces based on a multiple attention mechanism according to claim 1, characterized in that, The model training described in step S5 specifically involves: S51. Set hyperparameters including learning rate, number of training epochs, and batch size; S52. Input the preprocessed training set into the YOLOv8-SCSA-EMA network for further processing. N +10 rounds of iterative training, in the previous N In each round of training, the Mosaic data augmentation strategy is used to dynamically augment the data for each batch; after the model performs forward inference, the loss is calculated by comparing the predicted results with the true labels, and then backpropagation is performed to update the network parameters. S53. Throughout the training process, the model performance is continuously evaluated using the validation set.
10. The method for detecting surface defects in steel workpieces based on a multiple attention mechanism according to claim 9, characterized in that, The model was trained using the stochastic gradient descent (SGD) optimizer with an initial learning rate of 0.005, momentum of 0.937, weight decay coefficient of 0.0005, 230 training epochs, and a batch size of 64.
Citation Information
Patent Citations
YOLOX-based steel surface defect detection method, system, equipment and medium
CN117392052A
Industrial part surface defect detection method and device based on YOLOv8
CN118333940A
Inspection robot detection system based on image recognition
CN121061853A
Cited By
Self-adaptive slicing multi-target screw bending defect detection method
CN122134727A
A multi-target screw bending defect detection method with adaptive slicing
CN122134727B