A defect detection method, system, device and medium based on improved YOLOv5

By improving the YOLOv5 model and adopting a lightweight multi-scale feature extractor and differential attention module, the problem of balancing training speed and accuracy in industrial parts inspection is solved, and fast and efficient defect detection is achieved.

CN120543543BActive Publication Date: 2025-10-17ZHEJIANG LINYAN PRECISION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511030144.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-07-25
Publication Date
2025-10-17
Estimated Expiration
2045-07-25

AI Technical Summary

Technical Problem

Existing defect detection methods have the problem of balancing training speed and accuracy in industrial parts inspection, especially when inspecting large-size parts, where the training cost is high and the cycle is long. At the same time, it is difficult to achieve millisecond-level real-time response in high-speed continuous operation scenarios.

Method used

An improved YOLOv5 model is used to replace the original feature extraction module by building a lightweight multi-scale feature extractor. It is combined with lightweight differential attention to enhance the feature representation of defect targets, reduce computing requirements and improve training performance.

Benefits of technology

It speeds up model training, improves detection accuracy, meets high requirements for target detection models, and achieves real-time response in industrial parts detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120543543B_ABST
    Figure CN120543543B_ABST
Patent Text Reader

Abstract

The present application relates to the technical field of defect detection, in particular to a defect detection method, system, device and medium based on improved YOLOv5; first, image data of industrial parts in the production process is collected through a high-definition camera, image preprocessing is performed according to the characteristics of the surface defects of the parts, key frames are extracted and defect regions are labeled, and a defect detection dataset is constructed; second, a training environment is configured; then, taking the YOLOv5 network as the basic network, a CED-YOLO network model is constructed, the C3 module in the backbone network of the YOLOv5 network is replaced with a CEMS module as a feature extraction module, the C3 module in the neck network connection detection layer part is replaced with a differential attention DA, and the extracted features are enhanced; finally, the CED-YOLO network model is trained and verified by using the data set after data enhancement, the calculation amount is effectively reduced, the detection efficiency is improved, and the detection performance of the model is greatly improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of defect detection, in particular to a defect detection method, system, device and medium based on improved YOLOv5. BACKGROUND

[0002] Industrial parts, as the core component of mechanical equipment, directly affect the running stability and reliability of the equipment. Defects such as cracks, deformation and size deviation on the surface of the parts may cause equipment failure and even safety accidents. Traditional detection methods rely on manual visual inspection or contact measurement tools, which can identify some obvious defects, but have problems such as low detection efficiency, non-uniform standards, and insufficient sensitivity to micro-defects, which are difficult to meet the needs of modern intelligent manufacturing for high-precision quality control.

[0003] In recent years, automated detection technology based on machine vision has provided a new way for industrial part defect detection. By capturing part surface images with high-resolution industrial cameras and combining image processing algorithms for non-contact detection, the detection speed and consistency have been significantly improved. However, existing visual detection systems still face key challenges in industrial field applications:

[0004] Industrial parts are of various types and have significantly different defect morphologies. Defects such as cracks, burrs and rust have complex and variable texture features on different material surfaces (such as metal / plastic), making it difficult for traditional algorithms to establish a universal detection model through feature engineering.

[0005] Micro-defect detection of precision parts requires sub-pixel level recognition accuracy, while interference factors such as metal reflection and casting surface roughness can easily cause local overexposure or feature loss in images, severely affecting defect segmentation accuracy.

[0006] In high-speed continuous operation scenarios on production lines, existing detection systems face a conflict between data processing speed and computing resource occupation. The parallelization capability of algorithms for multi-camera collaborative detection is insufficient, making it difficult to achieve millisecond-level real-time response.

[0007] With the breakthrough of deep learning, algorithms based on region convolutional network (R-CNN) and single-stage detection framework have provided a new paradigm for industrial detection. The latest algorithms such as YOLO optimize feature reuse efficiency through cross-stage local network, improving small target detection capability while maintaining real-time performance. However, there is a significant problem in directly applying existing models for industrial part detection: for large-size part training data, the training cost is high and the training period is long. Using a smaller model size will result in insufficient accuracy. Ultimately, the problem of balancing training speed and accuracy is difficult to solve. SUMMARY

[0008] The present application aims at the problem that the training speed and accuracy of existing defect detection methods are difficult to balance, and proposes a defect detection method, system, device and medium based on improved YOLOv5; by constructing a lightweight multi-scale feature extractor to replace the original feature extraction module, the high computational demand of the model is reduced, and the model training speed is accelerated; at the same time, the feature representation of the defect target is strengthened through lightweight differential attention, and the training performance of the model is enhanced; and the high requirements of the target detection model training speed and accuracy are met.

[0009] The present application is specifically implemented as follows:

[0010] A defect detection method based on improved YOLOv5, specifically comprising the following steps:

[0011] Step S1: collect production process image data, extract key frames and label defect regions, and construct a defect image dataset;

[0012] Step S2: improve the network framework according to the YOLOv5 model to obtain the improved CED-YOLO model, and input the defect image dataset into the CED-YOLO model to obtain an enhanced defect image dataset;

[0013] The improved network framework includes replacing the C3 module in the backbone network with a CEMS multi-scale feature extraction module, and replacing the C3 module connected to the detection layer in the neck network with a DA spatial differential attention module;

[0014] Step S3: train the CED-YOLO model according to the enhanced defect image dataset;

[0015] Step S4: input the image to be detected into the trained CED-YOLO model to obtain a defect detection result.

[0016] In order to better realize the present application, further, the step S2 specifically comprises the following steps:

[0017] Step S21: according to the channel-by-channel convolution and point-by-point convolution, a CEMS multi-scale feature extraction module is constructed, the C3 module in the backbone network is replaced with the CEMS multi-scale feature extraction module, and multi-scale features are extracted and fused according to the CEMS multi-scale feature extraction module;

[0018] Step S22: replace the C3 module connected to the detection layer in the neck network with a DA spatial differential attention module.

[0019] In order to better realize the present application, further, the step S21 specifically comprises the following steps:

[0020] Step S211: Obtain an input feature map from a production process image dataset, and split the input feature map according to a channel dimension to obtain a first feature map and a second feature map;

[0021] Step S212: Divide the second feature map into a first branch map and a second branch map, call a CBS module to process the first branch map, and obtain a feature map Y0.

[0022] Step S213: Call the CBS module to process the second branch map, perform multi-dimensional convolution, integrate and extract the obtained multi-dimensional features, and normalize to obtain a feature map Y1.

[0023] Step S214: Concatenate the feature map Y0 and the feature map Y1 in the channel dimension, use a residual connection, sum the second feature map and the concatenated feature map, concatenate the first feature map of another branch in the channel dimension, and finally use the CBS module to integrate the channels of the concatenated feature map to obtain a multi-scale fusion feature map.

[0024] To better implement the present application, further, the CBS module comprises a 1*1 convolution module, a batch normalization module, and a SiLU activation function module connected in sequence.

[0025] To better implement the present application, further, the step S22 specifically comprises the following steps:

[0026] Step S221: Call the CBS module to process the multi-scale fusion feature map, call a 1*1 point convolution to integrate the channels, and compress the channel dimension again.

[0027] Step S222: Call a 3*3 convolution to provide spatial information and generate a foreground score and a background score.

[0028] Step S223: Calculate the difference between the foreground score and the background score to enlarge the score difference between the target and the background position.

[0029] Step S224: Call a Sigmoid activation function to weight the obtained score difference to obtain an attention weight map.

[0030] Step S225: According to the attention weight map, weight the attention weight to the multi-scale fusion feature map to enhance the target position feature representation and weaken the background position feature representation to obtain a differential attention feature map.

[0031] To better implement the present application, further, a loss function is added in the DA module, target position information of the current batch input is obtained according to label information in the input data, and a corresponding standard foreground score map and a background score map are generated, and the foreground score and the background score generated in the DA module are subtracted to calculate the cross-entropy loss of the difference value to obtain a final loss value.

[0032] To better implement the present application, further, the step S3 specifically comprises the following steps:

[0033] Step S31: dividing the processed defect image dataset into a training set and a test set according to 8:2;

[0034] Step S32: inputting the divided training set into CED-YOLO for training according to the set CED-YOLO training hyperparameters;

[0035] Step S33: inputting the test set into the trained CED-YOLO model to obtain a detection result.

[0036] Based on the above proposed defect detection method based on improved YOLOv5, in order to better implement the present application, further, a defect detection system based on improved YOLOv5 is proposed, which is used to execute the above-mentioned defect detection method based on improved YOLOv5; comprising a collection and preprocessing unit, a model optimization unit, a training unit and a detection unit;

[0037] The collection and preprocessing unit is used to collect production process image data, extract key frames and label defect regions, and construct a defect image dataset;

[0038] The model optimization unit is used to improve the network framework according to the YOLOv5 model to obtain an improved CED-YOLO model, and input the defect image dataset into the CED-YOLO model to obtain an enhanced defect image dataset;

[0039] The improved network framework comprises replacing the C3 module in the backbone network with a CEMS multi-scale feature extraction module, and replacing the C3 module connected by the detection layer in the neck network with a DA spatial difference attention module;

[0040] The training unit is used to train the CED-YOLO model according to the enhanced defect image dataset;

[0041] The detection unit is used to input the image to be detected into the trained CED-YOLO model to obtain a defect detection result.

[0042] Based on the above proposed defect detection method based on improved YOLOv5, in order to better implement the present application, further, an electronic device is proposed, comprising a memory and a processor; the memory has a computer program stored thereon; when the computer program runs on the processor, the above-mentioned defect detection method based on improved YOLOv5 is implemented.

[0043] Based on the defect detection method based on the improved YOLOv5 proposed above, in order to better realize the present application, further, a computer readable storage medium is provided, and computer instructions are stored on the computer readable storage medium;When the computer instructions are executed on the electronic device described above, the defect detection method based on the improved YOLOv5 is realized.

[0044] The present application has the following beneficial effects:

[0045] (1) The present application sets up a CEMS multi-scale feature extraction module, and fuses it into the backbone network to enhance the multi-scale feature extraction capability of the model, and at the same time, by replacing the ordinary convolution with the channel-by-channel convolution and the point-by-point convolution, the calculation amount of the model is greatly reduced, and the training speed of the model is accelerated.

[0046] (2) The present application sets up a lightweight differential attention model DA, and fuses it into the neck network, which is connected before the detection layer, and enhances the features of the important information extracted, while weakening the non-significant features, helping the model to focus on important local feature information and improving the detection performance. BRIEF DESCRIPTION OF DRAWINGS

[0047] Figure 1 The present application provides a YOLO industrial quality inspection model flowchart based on multi-scale features and differential attention.

[0048] Figure 2 The present application provides a model diagram of CED-YOLO.

[0049] Figure 3 The present application provides a CEMS module diagram.

[0050] Figure 4 The present application provides a DA module diagram. DETAILED DESCRIPTION

[0051] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application, it should be understood that the described embodiments are only a part of the embodiments of the present application, not all the embodiments, therefore should not be regarded as limiting the scope of protection. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor are within the scope of protection of the present application.

[0052] In the description of the present application, it should be noted that, unless otherwise explicitly specified and limited, the terms "arrangement", "connection", "connection" should be understood broadly, for example, it can be fixedly connected, or it can be detachably connected, or integrally connected; it can be mechanically connected, or it can be electrically connected; it can be directly connected, or it can be indirectly connected through an intermediate medium, or it can be the internal communication of two elements. For those skilled in the art, the specific meaning of the above terms in the present application can be understood according to the specific circumstances.

[0053] Embodiment 1:

[0054] A defect detection method based on improved YOLOv5, specifically comprising the following steps:

[0055] Step S1: Collecting production process image data, extracting key frames and labeling defect regions, and constructing a defect image dataset;

[0056] Step S11: Real-time acquisition of multi-period production process image data, hierarchical processing and labeling;

[0057] The step S11 specifically comprises the following steps:

[0058] Step S111: Real-time acquisition of multi-period production process image data, calling image recognition algorithm to filter production process image data, and calling adaptive enhancement technology to generate simulation data;

[0059] Step S112: Establishing a labeling system according to the defect type, labeling performance indicators and working condition parameters according to the labeling system, and associating multi-modal filtered production process image data;

[0060] Step S113: Clustering the associated production process image data, constructing an intelligent partitioning model, and calling a dynamic weighted sampling method to divide the clustered production process image data into a training set and a test set.

[0061] Step S12: Establishing a data quality evaluation mechanism according to the hierarchical data, and constructing a defect image dataset.

[0062] The step S12 specifically comprises the following steps:

[0063] Step S121: Establishing a data quality evaluation mechanism, monitoring data set deviation according to feature distribution thermodynamic map, and calling an adversarial generative network to supplement scarce fault samples;

[0064] Step S122: Recording the space-time attributes and environmental variables of each data iteration to form a full-cycle industrial part intelligent detection database, and constructing a defect image dataset.

[0065] Step S2: improving the network framework according to the YOLOv5 model to obtain an improved CED-YOLO model, and inputting the defect image dataset into the CED-YOLO model to obtain an enhanced defect image dataset;

[0066] The step S2 specifically comprises the following steps:

[0067] Step S21: constructing a CEMS multi-scale feature extraction module according to the channel-by-channel convolution and the point-by-point convolution, replacing the C3 module in the backbone network with the CEMS multi-scale feature extraction module, and extracting and fusing multi-scale features according to the CEMS multi-scale feature extraction module;

[0068] The step S21 specifically comprises the following steps:

[0069] Step S211: obtaining an input feature map from the production process image dataset, and segmenting the input feature map according to the channel dimension to obtain a first feature map and a second feature map;

[0070] Step S212: dividing the second feature map into a first branch map and a second branch map, calling a CBS module to process the first branch map to obtain a feature map Y0;

[0071] Step S213: calling the CBS module to process the second branch map, performing multi-dimensional convolution, integrating and extracting multi-dimensional features, and normalizing to obtain a feature map Y1;

[0072] Step S214: splicing the feature map Y0 and the feature map Y1 in the channel dimension, using residual connection, summing the second feature map and the spliced feature map, splicing the first feature map of another branch in the channel dimension, and finally using the CBS module to integrate the channels of the spliced feature map to obtain a multi-scale fusion feature map.

[0073] The improved network framework comprises replacing the C3 module in the backbone network with the CEMS multi-scale feature extraction module, and replacing the C3 module connected to the detection layer in the neck network with the DA spatial difference attention module;

[0074] Step S22: replacing the C3 module connected to the detection layer in the neck network with the DA spatial difference attention module.

[0075] The step S22 specifically comprises the following steps:

[0076] Step S221: calling the CBS module to process the input feature map, calling a 1*1 point convolution to integrate the channels, and compressing the channel dimension again;

[0077] Step S222: calling a 3*3 convolution to provide spatial information and generate a foreground score and a background score;

[0078] Step S223: calculate the difference between the foreground score and the background score, and expand the score difference between the target and the background position;

[0079] Step S224: call the Sigmoid activation function to weight the obtained score difference, and obtain an attention weight map;

[0080] Step S225: weight the attention weight to the input feature map, enhance the target position feature representation, weaken the background position feature representation, and obtain a differential attention feature map.

[0081] The CBS module comprises a 1*1 convolution module, a batch normalization module and a SiLU activation function module connected in sequence.

[0082] In the DA module, a loss function is added, target position information of the current batch input is obtained according to label information in the input data, and a standard foreground score map and a background score map corresponding to the DA module are generated, and the foreground score and the background score generated in the DA module are subtracted, and a cross-entropy loss is calculated according to the difference, and a final loss value is obtained.

[0083] Step S3: training the CED-YOLO model according to the enhanced defect image data set;

[0084] The step S3 specifically comprises the following steps:

[0085] Step S31: dividing the processed defect image data set into a training set and a test set according to 8:2;

[0086] Step S32: inputting the divided training set into the CED-YOLO for training according to the set CED-YOLO training hyperparameters;

[0087] Step S33: inputting the test set into the trained CED-YOLO model to obtain a detection result.

[0088] Step S4: inputting the image to be detected into the trained CED-YOLO model to obtain a defect detection result.

[0089] Working principle: first, the high-definition camera collects image data of industrial parts in the production process, pre-processes the image according to the characteristics of the part surface defects, extracts key frames and labels the defect area, and constructs a defect detection dataset; second, configure the training environment; then build the CED-YOLO network model, the CED-YOLO network model takes YOLOv5 network as the basic network, replaces the C3 module in the backbone network of YOLOv5 network with CEMS module as the feature extraction module, replaces the C3 module in the neck network connection detection layer part with differential attention DA, and enhances the extracted features; finally, the data set after data enhancement is applied to train and verify the CED-YOLO network model. The embodiment effectively reduces the calculation amount, improves the detection efficiency, and greatly improves the detection performance of the model.

[0090] As shown in Figure 2 Preprocessing is first performed, the image is adaptively scaled to the specified pixel HxW, the aspect ratio is maintained while deformation is avoided by filling the gray side, the pixel value is normalized to the [0, 1] interval, then converted to PyTorch tensor format, forming (Bx3xHxW) input data, and then input into the CED-YOLO model.

[0091] In the backbone network, the preprocessed image is input into the backbone network, first using two consecutive convolution blocks to reduce the image size to 1 / 4 of the original, while expanding the channel dimension, then using the multi-scale feature extraction block and the convolution block three times, after extracting the multi-scale features, using the convolution block to downsample, and finally using the fast pyramid pooling layer and the multi-scale feature extraction block to fuse the multi-scale features.

[0092] In the neck network, after obtaining the output feature map of the last layer of the backbone network, first, in the up-sampling block, the channel dimension is reduced through the convolution block, and the spatial size is expanded through up-sampling, the obtained feature map is channel spliced with the output feature map of the third multi-scale feature extraction block, and then the channel dimension is integrated through the point convolution block, secondly, the obtained output feature map is input into the up-sampling block again, the channel dimension is reduced through the convolution block, and then the spatial size is expanded through up-sampling, and the output is channel spliced with the output of the second multi-scale feature extraction block, the output feature map is input into the differential attention module for feature enhancement; after obtaining the enhanced feature map, the output feature map is downsampled through the convolution block, and then the output feature map is channel spliced with the output of the point convolution block, and then input into the differential attention for feature enhancement; then the convolution block is used again to downsample, the output feature map is channel spliced with the fourth multi-scale feature extraction block, and finally the differential attention is used again for feature enhancement.

[0093] In the head network, the outputs of three-layer difference attention modules are taken in sequence from front to back, and are input into a small target detection head, a medium target detection head and a large target detection head respectively. The target position, confidence and class probability are determined according to the output vectors. The redundant target information is removed according to the confidence threshold and the IoU threshold. Finally, the actual target detection information is obtained.

[0094] Embodiment 2

[0095] This embodiment is based on the above-mentioned embodiment 1 and is described in a specific embodiment, which specifically includes the following steps.

[0096] Step S1: Collect image data of industrial parts in the production process by using a high-definition camera, and label the collected data to generate a data set.

[0097] Step S2: Improve the network framework based on the YOLOv5 model to obtain an improved CED-YOLO model; the specific improvements of the CED-YOLO model are as follows:

[0098] 1) In the backbone network, a CEMS multi-scale feature extraction module is constructed by using channel-wise convolution and point-wise convolution to replace the original C3 module of the YOLOv5 model;

[0099] 2) In the neck network, a spatial difference attention DA is used to replace the C3 module connected in front of the detection layer;

[0100] Step S3: Train and test the CED-YOLO model on the processed part data set for defect detection.

[0101] Further, the CEMS module extracts multi-scale features in the spatial dimension and fuses them, specifically as follows:

[0102] First, the input feature map X is divided into two parts X1 and X2 along the channel dimension, and X1 remains unchanged, and X2 performs the following operations. input

[0103] X2 is processed in two branches, the first branch uses a CBS module for processing, and the CBS module is composed of a 1*1 convolution, a batch normalization and a SiLU activation function in series, which reduces the channel dimension to half of the original, and obtains a feature map Y0.

[0104] ​The second branch is also processed using the CBS module first, reducing the channel dimension to half of the original, extracting a 1*1 receptive field feature, and then connecting two 3*3 channel-by-channel convolutions in series to obtain spatial features in 3*3 and 5*5 receptive fields, respectively. Then, 1*1 convolution is used to integrate the channel information of the extracted 1*1, 3*3, and 5*5 receptive field features. Finally, the three output feature maps are superimposed and averaged, and then processed using batch normalization and SiLU activation function to obtain the feature map Y1.

[0105] The feature maps Y0 and Y1 are spliced in the channel dimension, and then the residual connection is used to sum X2 and the spliced feature map. Then, the original input X1 of the other branch is spliced in the channel dimension, and finally the CBS module is used to integrate the channel of the spliced feature map to obtain the final output.

[0106] Further, the feature enhancement performed by the differential attention DA is as follows:

[0107] First, the input feature map is processed using the CBS module, reducing the channel dimension to half of the input. Then, 1*1 point convolution is used to integrate the channel, and the channel dimension is compressed again. At this time, the output channel dimension is 1 / 4 of the original input feature map. Then, 3*3 convolution is used to extract spatial information while generating foreground and background scores. Then, the difference between the two score maps obtained is calculated by subtracting the background score from the foreground score to expand the score difference between the target and background positions. Finally, the Sigmoid activation function is used to weight the score difference to obtain the attention weight map. Finally, the attention weight is weighted to the input feature map to enhance the target position feature representation and weaken the background position feature representation to obtain the final output.

[0108] Meanwhile, the loss function is increased. The target position information of the current batch input is obtained according to the label information in the input data, and the corresponding standard foreground and background score maps are generated according to the target position information. The difference between the standard foreground and background score maps and the foreground and background scores generated in the DA module is calculated, and the cross-entropy loss of the difference is calculated to obtain the final loss value. With this loss function, the DA module can generate more accurate foreground and background score maps, optimize the feature enhancement effect, and accurately enhance the position information of the region.

[0109] Further, step S3 specifically includes the following operations:

[0110] 1) The processed industrial part defect image dataset is divided into a training set and a test set according to an 8:2 ratio.

[0111] 2) Set the CED-YOLO training hyperparameters: the batch size is 16, the initial learning rate is set to 0.01, the final learning rate is 0.0001, the momentum parameter is set to 0.937, the weight decay is 0.0005, the random gradient descent optimizer is used, and the input image preprocessing size is set to the original image size.

[0112] 3) The divided training set is input into the improved CED-YOLO based on YOLO for training;

[0113] 4) The test set is input into the trained CED-YOLO model to test its detection ability on new data.

[0114] Working principle: In the algorithm of the embodiment, a CEMS multi-scale feature extraction module is designed and fused into the backbone network to enhance the multi-scale feature extraction capability of the model. At the same time, by replacing the ordinary convolution with the channel-by-channel convolution and the point-by-point convolution, the computational amount of the model is greatly reduced, and the training speed of the model is accelerated. In the algorithm of the embodiment, a lightweight differential attention DA is designed and fused into the neck network, connected before the detection layer, to enhance the important information extracted, and weaken the non-significant features, helping the model focus on important local feature information and improving the detection performance.

[0115] The other parts of the embodiment are the same as those of the above-mentioned embodiment 1, and will not be described again.

[0116] Embodiment 3:

[0117] On the basis of any one of the above-mentioned embodiments 1-2, as shown in embodiments 3-1 and 3-2, the detection of industrial part defects is taken as an example for detailed description. Figure 1 、 Figure 2 、 Figure 3 、 Figure 4

[0118] Step S1: Constructing an industrial part dataset.

[0119] In the production scene of industrial parts, a high-definition monitoring system is deployed to collect multi-period data in real time, covering the peak, trough, equipment maintenance, shutdown and other states of conventional operation. The monitoring data includes images and videos, which need to record the standard production process, abnormal working conditions (such as mechanical failure, part defects) and sudden interference events, and cover different climate conditions and production environments to enhance the generalization ability of the subsequent AI model. At the same time, combined with intelligent crawling technology, industry public data is obtained, and the design parameters, service performance and failure cases of parts of different brands, specifications and materials are integrated, focusing on collecting feature data such as deformation and fatigue fracture of parts under extreme working conditions, and through automatic cleaning, low-resolution, repeated and irrelevant content is removed to construct a multi-dimensional part feature library.

[0120] ​The raw data is processed in a hierarchical manner: first, an image recognition algorithm is used to automatically filter out invalid data such as blur and distortion, and adaptive enhancement technology is used to generate simulated data such as illumination changes, angle offset, and surface wear. Secondly, a professional annotation system is established to finely annotate defect types such as part scratches, deformation, and corrosion, as well as performance indicators such as assembly accuracy and fit clearance. Simultaneously, operating parameters such as equipment vibration spectrum and temperature load are annotated to form multimodal data associations. Finally, an intelligent partitioning model is constructed to cluster data based on dimensions such as part material properties and failure mechanisms. Dynamic weighted sampling is used to divide the dataset into a training set (80%) and a test set (20%) to ensure a balanced distribution of various failure modes.

[0121] Establish a data quality assessment mechanism, monitor dataset deviations through feature distribution heatmaps, and utilize generative adversarial networks to supplement scarce fault samples. Develop a data version management system to record the spatiotemporal attributes and environmental variables of each data iteration, providing a traceable data base for part quality analysis across different production lines. Ultimately, establish an intelligent inspection database for industrial parts covering the entire design, manufacturing, and service lifecycle.

[0122] Step S2: Improve the YOLOv5 model;

[0123] Based on the existing YOLOv5 algorithm network structure, the optimized CED-YOLO target detection model is obtained, as shown in Figure 2 shown.

[0124] The present invention designs a CSP_EfficientMulti-Scale Module (CEMS) multi-scale feature extraction module in the backbone network, which has stronger feature extraction capability and less computational complexity.

[0125] like Figure 3 As shown, the CEMS module specifically performs the following operations:

[0126] ① Input feature segmentation;

[0127] Input feature map X In ∈R C×H×W Split along the channel dimension into X1∈R C / 2×H×W and X2∈R C / 2×H×W , where X1 retains the original feature information and X2 enters the multi-branch processing flow.

[0128] ②Multi-scale feature extraction;

[0129] Branch 1 (local feature compression);

[0130] Through the CBS module (Conv 1×1 -BN-SiLU) maps X2 to a low-dimensional space:

[0131] Y0= SiLU(BN(Conv1x1(X2))) e R C / 4×H×W ;

[0132] Branch two (hierarchical receptive field expansion);

[0133] a. Channel compression:

[0134] Z0= SiLU(BN(Conv1x1(X2))) e R C / 4×H×W ;

[0135] b. Spatial feature extraction:

[0136] First level: Z1= DWConv 3×3 (Z0) (get 3x3 receptive field features);

[0137] Second level: Z2= DWConv 3×3 (Z1) (receptive field expansion to 5x5);

[0138] c. Multi-scale feature fusion:

[0139] ;

[0140] ③ Feature fusion and reconstruction;

[0141] a. Feature splicing Y cat = Concat(Y0, Y1) e R C / 2×H×W ;

[0142] b. Residual reinforcement Y res = X2 o Y cat (element-level addition);

[0143] c. Cross-domain integration X out = CBS(Concat(X1, Y res )) e R C×H×W ;

[0144] The CEMS module utilizes a triple-innovative design to achieve the synergistic optimization of local feature compression and global context capture through a double-path parallel structure. Secondly, it uses stacked per-channel convolution to construct a 5x5 equivalent receptive field and perform weighted fusion with the 1x1 and 3x3 receptive field features of the previous two layers to enhance spatial feature expression, obtaining multi-scale fused feature maps.

[0145] In the neck network, a lightweight differential attention DeltaAttention (DA) module is designed to enhance spatial features.

[0146] As shown in Figure 4 , the DA module specifically performs the following operations:

[0147] ①Feature compression and space modeling;

[0148] Multi-scale fusion feature map X out Channel compression by CBS module (Conv1x1-BN-SiLU):

[0149] Z0'=SiLU(BN(Conv 1x1 (X out ))) ∈ R C / 2×H×W ;

[0150] Continue to reduce dimensionality by point convolution:

[0151] Z1'=Conv 1x1 (Z0') ∈ R C / 4×H×W ;

[0152] ②Dual-domain probability map generation;

[0153] Generate spatial probability distribution using dual-channel 3x3 deep convolution:

[0154] M dual =DWConv 3x3 (Z1') ∈R 2×H×W

[0155] Decouple foreground probability map M f = M dual [0,:,:] and background probability map M b =M dual [1,:,:]

[0156] ③Difference enhancement mechanism;

[0157] a. Probability difference calculation:

[0158] ΔM=M f (M b +ε) ( Indicates element-wise division, to avoid division by zero error, set smoothing factor ε=1e-5)

[0159] b. Dynamic range compression:

[0160] W att =Sigmoid(α*ΔM) (get attention weight map W att , α=5.0 is the scale factor)

[0161] ④Feature recalibration;

[0162] Implement feature modulation through spatial attention weight:

[0163] X out' = X out ⊙(W att +0.5) ∈R C×H×W (⊙ represents element-wise multiplication of channel broadcasting);

[0164] The DA module adopts a double probability map quotient space mapping to replace the traditional subtraction operation, enhances the small target distinguishability, and attention score weight is added with an alpha scale factor to control the attention influence range. A weak residual weighted strategy (W att +0.5) retains the original feature distribution to a certain extent, while regulating the weight range, distinguishing the feature weight values that need to be enhanced and weakened, and enhancing the features with distinguishability to obtain differential attention feature maps.

[0165] Step S3: training the CED-YOLO model;

[0166] The training of the CED-YOLO model is based on a high-precision part defect dataset, covering typical industrial defects such as cracks, scratches, and deformations. All samples are pixel-level labeled to ensure data quality and diversity. In the early stage of training, the backbone network is initialized with pre-trained weights, combined with dynamic weight distribution technology to enhance the detection sensitivity of small defect targets, and through the CosineWarmup learning rate strategy to gradually improve the initial learning rate (0.001→0.01), balance the convergence speed and stability.

[0167] During the training process, in order to save training time, the first 1000 rounds are verified on the validation set every round, adjusted to verify once every 50 rounds, while enabling the early stopping mechanism (automatic termination of training if the fitness does not improve for 50 consecutive rounds), and integrating the EMA weight smoothing technology to retain the best model weight (best.pt).

[0168] To improve the generalization ability, multi-dimensional anti-overfitting strategies are integrated during training: data augmentation introduces random occlusion, illumination disturbance and local affine transformation to simulate complex industrial scenes; in terms of regularization, DropBlock is used instead of traditional Dropout, and GIoU constraint is strengthened in the loss function to balance the detection box accuracy and feature space continuity.

[0169] Step S4: model detection;

[0170] After the CED-YOLO model is trained, the performance is verified based on an independent test set and real industrial scene data. In the test stage, the best.pt is used, and the model is deployed in an NVIDIA A6000 GPU hardware environment, with the average inference speed (FPS) and detection fitness (P, R, mAP@0.5, and mAP@0.5:0.95) as the core indicators. The test results show that the model for detecting micro-defects such as cracks and scratches reaches the highest fitness of 34.2% of the YOLO model, with a training time shortened by 33.6%. On this basis, the CED-YOLO model is further trained, and the highest fitness can reach 37.3% (Fitness), which is 3.1% higher than the baseline YOLO model. At this time, the training time is still faster than the original YOLO by more than 25%, and the single-frame inference time is stable within 15 ms, meeting the real-time detection requirements of the industry.

[0171] To verify the detection performance of the CED-YOLO method proposed in this embodiment on the industrial part dataset, this embodiment compares it with various existing YOLO improved models. Table 1 shows the performance comparison of each method on this part dataset.

[0172] Table 1. Method performance comparison table

[0173]

[0174] Among them, mAP50 represents the average precision when the IoU threshold is set to 50%; mAP50:95 represents the average of all mAP when the IoU threshold is from 50% to 95% (step 5%); Fitness represents fitness; Params (M) represents the model parameter amount; FLOPs (G) represents the model calculation amount.

[0175] From the above table, it can be seen that the CED-YOLO model is superior to all improved methods in various aspects on the industrial part dataset, proving that the CEMS module and DA module proposed in this embodiment can effectively improve the model's ability in all aspects of the industrial part defect detection task.

[0176] The other parts of this embodiment are the same as any one of Embodiments 1-2 described above, and thus will not be described again.

[0177] Embodiment 4

[0178] Based on any one of Embodiments 1-3 described above, this embodiment proposes a defect detection system based on an improved YOLOv5, which is used to execute the defect detection method based on the improved YOLOv5 described above; including a collection and preprocessing unit, a model optimization unit, a training unit, and a detection unit.

[0179] The acquisition pre-processing unit is configured to acquire production process image data, extract key frames, label defect regions, and construct a defect image dataset.

[0180] The model optimization unit is configured to improve a network framework based on a YOLOv5 model, obtain an improved CED-YOLO model, and input the defect image dataset into the CED-YOLO model to obtain an enhanced defect image dataset.

[0181] The improved network framework includes replacing a C3 module in a backbone network with a CEMS multi-scale feature extraction module and replacing a C3 module connected to a detection layer in a neck network with a DA spatial difference attention module.

[0182] The training unit is configured to train the CED-YOLO model based on the enhanced defect image dataset.

[0183] The detection unit is configured to input a to-be-detected image into the trained CED-YOLO model to obtain a defect detection result.

[0184] The embodiment also provides an electronic device including a memory and a processor, and the memory stores a computer program; when the computer program runs on the processor, the improved YOLOv5-based defect detection method is implemented.

[0185] The embodiment also provides a computer readable storage medium, and the computer readable storage medium stores computer instructions; when the computer instructions are executed on the electronic device, the improved YOLOv5-based defect detection method is implemented.

[0186] The other parts of the embodiment are the same as any one of the above-mentioned embodiments 1-3, and thus will not be described again.

[0187] The above is only a preferred embodiment of the present application, and does not limit the present application in any form. Any simple modification or equivalent change based on the technical essence of the present application to the above embodiment falls within the protection scope of the present application.

Claims

1. A defect detection method based on improved YOLOv5, characterized in that: The specific steps include: Step S1: Collect production process image data, extract key frames and mark defect areas, and build a defect image dataset; Step S2: Improve the network framework according to the YOLOv5 model to obtain an improved CED-YOLO model, and input the defect image dataset into the CED-YOLO model to obtain an enhanced defect image dataset; The improved network framework includes replacing the C3 module in the backbone network with a CEMS multi-scale feature extraction module, and replacing the C3 module connected to the detection layer in the neck network with a DA spatial differential attention module; Step S3: training the CED-YOLO model based on the enhanced defect image dataset; Step S4: Input the image to be detected into the trained CED-YOLO model to obtain the defect detection result; The step S2 specifically includes the following steps: Step S21: constructing a CEMS multi-scale feature extraction module based on channel-by-channel convolution and point-by-point convolution, replacing the C3 module in the backbone network with the CEMS multi-scale feature extraction module, and extracting and fusing multi-scale features based on the CEMS multi-scale feature extraction module; Step S22: Replace the C3 module connected to the detection layer in the neck network with the DA spatial differential attention module; The step S21 specifically includes the following steps: Step S211: obtaining an input feature map from the production process image dataset and segmenting it according to the channel dimension to obtain a first feature map and a second feature map; Step S212: Divide the second feature map into a first branch map and a second branch map, call the CBS module to process the first branch map, and obtain a feature map Y0; Step S213: calling the CBS module to process the second branch graph, and performing multidimensional convolution, integrating the extracted multidimensional features, and normalizing them to obtain a feature graph Y1; Step S214: The feature map Y0 and the feature map Y1 are spliced ​​in the channel dimension. A residual connection is used to sum the second feature map with the spliced ​​feature map, and then spliced ​​with the first feature map of the other branch in the channel dimension. Finally, the CBS module is used to perform channel integration on the spliced ​​feature map to obtain a multi-scale fused feature map. The step S22 specifically includes the following steps: Step S221: The multi-scale fusion feature map is processed by the CBS module, and the 1*1 point convolution is called to integrate the channels to compress the channel dimension again; Step S222: Calling 3*3 convolution to provide spatial information and generate foreground score and background score; Step S223: Calculate the difference between the foreground score and the background score, and expand the score difference between the target position and the background position; Step S224: Call the Sigmoid activation function to weight the obtained score difference and obtain the attention weight map; Step S225: According to the attention weight map, the attention weight is weighted to the multi-scale fusion feature map, the target position feature representation is enhanced, the background position feature representation is weakened, and a differential attention feature map is obtained.

2. A defect detection method based on improved YOLOv5 according to claim 1, characterized in that: The CBS module includes a 1*1 convolution module, a batch normalization module, and a SiLU activation function module connected in sequence.

3. The defect detection method based on improved YOLOv5 according to claim 1, characterized in that: A loss function is added to the DA module. The target position information of the current batch input is obtained according to the label information in the input data. The corresponding standard foreground and background score maps are generated, and the difference between them and the foreground and background scores generated in the DA module is calculated. The cross entropy loss is calculated on the difference to obtain the final loss value.

4. The defect detection method based on improved YOLOv5 according to claim 1, characterized in that: The step S3 specifically includes the following steps: Step S31: Divide the processed defect image dataset into a training set and a test set according to an 8:2 ratio; Step S32: Input the divided training set into CED-YOLO for training according to the set CED-YOLO training hyperparameters; Step S33: Input the test set into the trained CED-YOLO model to obtain the detection results.

5. A defect detection system based on improved YOLOv5, used to perform the defect detection method based on improved YOLOv5 according to claim 1; characterized in that, It includes acquisition pre-processing unit, model optimization unit, training unit and detection unit; The acquisition and preprocessing unit is used to acquire production process image data, extract key frames and mark defect areas, and construct a defect image data set; The model optimization unit is used to improve the network framework according to the YOLOv5 model to obtain an improved CED-YOLO model, and input the defect image dataset into the CED-YOLO model to obtain an enhanced defect image dataset; The improved network framework includes replacing the C3 module in the backbone network with a CEMS multi-scale feature extraction module, and replacing the C3 module connected to the detection layer in the neck network with a DA spatial differential attention module; The training unit is used to train the CED-YOLO model based on the enhanced defect image dataset; The detection unit is used to input the image to be detected into the trained CED-YOLO model to obtain a defect detection result; The model optimization unit is further used to construct a CEMS multi-scale feature extraction module based on channel-by-channel convolution and point-by-point convolution, replace the C3 module in the backbone network with the CEMS multi-scale feature extraction module, extract and fuse multi-scale features based on the CEMS multi-scale feature extraction module; replace the C3 module connected to the detection layer in the neck network with a DA spatial differential attention module; The model optimization unit is specifically used to obtain an input feature map from a production process image dataset, and segment it according to the channel dimension to obtain a first feature map and a second feature map; divide the second feature map into a first branch map and a second branch map, call the CBS module to process the first branch map, and obtain a feature map Y0; call the CBS module to process the second branch map, and perform multidimensional convolution, integrate the extracted multidimensional features, and normalize to obtain a feature map Y1; splice the feature map Y0 and the feature map Y1 in the channel dimension, use residual connection, sum the second feature map and the spliced ​​feature map, and then splice it with the first feature map of another branch in the channel dimension, and finally use the CBS module to perform channel integration on the spliced ​​feature map to obtain a multi-scale fusion feature map; call the CBS module to process the multi-scale fusion feature map, call 1*1 point convolution to integrate the channels, and compress the channel dimension again; Call 3*3 convolution to provide spatial information and generate foreground score and background score; Calculate the difference between the foreground score and the background score, and expand the score difference between the target position and the background position; The score difference obtained by weighting the Sigmoid activation function is used to obtain the attention weight map; according to the attention weight map, the attention weight is weighted to the multi-scale fusion feature map to enhance the target position feature representation and weaken the background position feature representation to obtain the differential attention feature map.

6. An electronic device, characterized in that: The invention comprises a memory and a processor; a computer program is stored on the memory; when the computer program is run on the processor, the defect detection method based on the improved YOLOv5 according to any one of claims 1 to 4 is implemented.

7. A computer-readable storage medium, characterized in that The computer-readable storage medium stores computer instructions; when the computer instructions are executed on the electronic device according to claim 6, the defect detection method based on the improved YOLOv5 according to any one of claims 1 to 4 is implemented.

Citation Information

Patent Citations

  • Steel surface defect detection system and method based on improved YOLOv5s

    CN118570138A

  • Light-weight industrial product surface defect detection method

    CN119313659A