A method for detecting surface defects in finished workpieces based on an improved YOLOv5
By improving the C3-CA+SE dual attention, BiFPN feature fusion, and adaptive loss function of the YOLOV5 model, the accuracy and cross-scale localization problems of the YOLOV5 model in small target detection are solved, achieving high-precision defect detection and cross-scene adaptation, and improving the intelligence and accuracy of industrial inspection.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- TAIYUAN UNIVERSITY OF SCIENCE AND TECHNOLOGY
- Filing Date
- 2026-01-26
- Publication Date
- 2026-06-02
AI Technical Summary
The existing YOLOv5 model suffers from several problems in small target detection scenarios, including loss of small target feature information, inability of the FPN feature fusion module to effectively distinguish features of different scales, and sensitivity of the loss function to small target bounding box offsets. These issues result in low detection accuracy and inaccurate cross-scale localization.
We construct a multi-dimensional improved YOLOv5 model that integrates the C3-CA+SE dual attention mechanism, BiFPN feature fusion structure, EIoU and NWD adaptive loss function, and ASFF-Detect detection head. We train and optimize the model using a multimodal defect dataset to improve the high accuracy and cross-scale consistency recognition of small target defects.
It achieves high-precision detection and stable positioning of defects in small targets, improves the model's ability to identify minor flaws in complex backgrounds, has good generalization ability across workpiece types, reduces the error rate, and assists in industrial process optimization.
Smart Images

Figure CN122134624A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of industrial defect detection technology, and in particular to a method for detecting surface defects in finished workpieces based on an improved YOLOV5. Background Technology
[0002] In the appearance quality inspection of industrial products, surface defect identification technology based on target detection has become a key means, especially in the inspection of small-sized workpieces such as aluminum, steel and electronic components. The accurate identification of small target defects directly affects the product quality control and defect tracing effect. In recent years, deep learning-driven single-stage detection algorithms have been widely used due to their high inference efficiency and simple structure. The YOLOv5 model has become one of the mainstream algorithms in the field of industrial defect detection due to its high degree of engineering and convenient deployment.
[0003] However, existing YOLOv5 models still have many limitations in small object detection scenarios. The C3 module of its backbone network is prone to losing small object feature information during downsampling, affecting the extraction of contours and textures. The FPN feature fusion module fails to effectively distinguish the importance of features at different scales, which can easily cause shallow small object features to be masked by deep semantic features. The loss function, such as CIoU, is extremely sensitive to the offset of small object bounding boxes, which leads to gradient weakening or disappearance during training, making it difficult to optimize the localization effect of small objects. In addition, its detection head regresses feature maps at different scales independently without establishing consistency between scales, which can easily lead to inaccurate cross-scale localization. Summary of the Invention
[0004] This invention provides a method for detecting surface defects in finished workpieces based on an improved YOLOv5. It constructs a multi-dimensional improved YOLOv5 model that integrates a C3-CA+SE dual attention mechanism, a BiFPN feature fusion structure, an EIoU and NWD adaptive loss function, and an ASFF-Detect detection head. By training and optimizing with a multimodal defect dataset, it achieves high-precision, cross-scale consistent identification of small target defects, improves the model's detection accuracy, real-time performance, and cross-scene adaptability, and meets the actual needs of industrial sites for intelligent and precise defect detection.
[0005] A method for detecting surface defects in finished workpieces based on an improved YOLOv5 includes the following steps: S1. Acquire images of surface defects on the workpiece, preprocess the images of surface defects on the workpiece, perform data augmentation operations, and construct a defect dataset. S2, Construct a YOLOv5 model with multi-dimensional improvements; S3, Train the multi-dimensional improved YOLOV5 model using the multimodal defect dataset; S4. The trained multi-dimensional improved YOLOV5 model is used to detect defects in workpiece images. The defect type, location coordinates and confidence score are output. The accuracy, recall and mAP@0.5 indexes are collected to evaluate the detection performance of the multi-dimensional improved YOLOV5 model. S5 visualizes the detection results, outputting defect location annotation maps, statistical charts of detection accuracy for each category, and suggestions for optimizing industrial processes.
[0006] Optionally, the preprocessing and data augmentation operations in S1 include: Perform adaptive histogram equalization and denoising operations on the image; Data augmentation is performed through random flipping, rotation, and brightness adjustment; images are divided into training and validation sets according to a preset ratio and then labeled.
[0007] Optionally, the multi-dimensional improved YOLOv5 model in S2 includes: S21, replace the C3 module in the 8th layer of the YOLOV5 backbone network with the C3-CA module, and embed the SE attention mechanism to construct a C3-CA+SE dual attention structure to enhance the capture of small target detail features; S22, replace the first two FPN structures of the YOLOV5 head network with BiFPN modules, and improve the integration efficiency of shallow small target features through adaptive weighted fusion; S23 adopts an adaptive fusion loss function of EIoU and NWD, and dynamically allocates the weights of the two losses according to the proportion of small targets in the current training batch, so as to solve the problem of small target loss being masked by medium and large targets and gradient vanishing. S24, replace the native detection head with the ASFF-Detect detection head to optimize cross-scale target localization consistency.
[0008] Optionally, the weight coefficients of NWD loss and EIoU loss in the adaptive fusion loss function of EIoU and NWD are dynamically adjusted based on the proportion of small targets in the current batch, as follows: ; ; ; ; in, The proportion of smaller goals within the overall goal. For the number of feature layers, For the first The number of targets on the layer feature map This is an indicator function that returns 1 when the condition is true. For the target area, These are the weighting coefficients for NWD loss. Based on NWD weights, For the maximum NWD weight, These are the weighting coefficients for the EIOU loss. For the final bounding box loss, , These are the normalized loss and EIoU loss, respectively, based on Wasserstein distance.
[0009] Optionally, the ASFF-Detect detection head in S24 achieves dynamic fusion of multi-scale feature maps through a unified channel mapping, spatial alignment and target scale perception fusion weight mechanism, and inputs the fusion results into the classification branch and regression branch respectively to complete target detection.
[0010] Optionally, the multi-dimensional improved YOLOV5 model is trained for 200 rounds, and the validation set mAP@0.5 is used as the evaluation metric. Training is terminated early when the validation set mAP@0.5 shows no improvement for 10 consecutive rounds.
[0011] Optionally, the precision is expressed as: ; The recall rate is expressed as: ; The mAP@0.5 metric is expressed as follows: ; in, The number of samples correctly classified as positive. The number of samples that were misclassified as positive. The number of samples that were misclassified as negative. The total number of categories, Number of tests This represents the average precision for each category.
[0012] The beneficial effects of this invention are: This invention constructs a multi-dimensional improved YOLOv5 model, introduces a C3-CA+SE dual attention structure in the backbone network, integrates a BiFPN feature fusion module in the head network, combines an adaptive weighted loss function of EIoU and NWD with an ASFF-Detect head, and achieves high-precision detection and stable localization of defects in small targets, significantly improving the model's ability to identify minute flaws in complex backgrounds.
[0013] This invention constructs a multimodal defect dataset and introduces a dynamic training optimization mechanism, enabling the model to have good generalization ability across workpiece types. It exhibits strong robustness and low error rate in scenarios such as aluminum, steel, and electronic components, reducing the generalization error by 40%. In addition, through the visualization output and statistical analysis of defect type identification results, it can assist in generating targeted industrial process optimization suggestions, effectively reducing the workload of manual inspection and the risk of missed inspections, and improving the overall practicality and reliability of the system. Attached Figure Description
[0014] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only for this invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0015] Figure 1 This is a schematic diagram of the detection method flow according to an embodiment of the present invention. Detailed Implementation
[0016] The present invention will now be described in detail with reference to the accompanying drawings and specific embodiments. Those skilled in the art may employ other alternative methods to implement some well-known technologies; moreover, the accompanying drawings are only for more specific description of the embodiments and are not intended to specifically limit the present invention.
[0017] like Figure 1 As shown, a method for detecting surface defects in finished workpieces based on an improved YOLOv5 includes the following steps: S1 collects and organizes relevant data sources, including workpiece surface defect images and historical fault records. These data will be integrated into a comprehensive defect detection dataset to support subsequent analysis and modeling.
[0018] The specific processes of data preprocessing and augmentation include: Image preprocessing: Adaptive histogram equalization and Gaussian denoising are performed sequentially on the defective image to improve image contrast and signal-to-noise ratio; Data augmentation: Increase sample diversity and avoid model overfitting by performing operations such as random flipping (horizontal flip probability 0.5), ±15° rotation (bilinear interpolation), and ±20% brightness adjustment; Dataset construction: The dataset was divided into a training set (2175 images) and a validation set (544 images) in an 8:2 ratio. The annotation format adopted the YOLO standard format, and the bounding box annotation error was ≤1 pixel.
[0019] S2 uses a multi-dimensional improved YOLOv5 model as the detection core. The specific improvements to the core modules are as follows: Backbone network improvement: The C3 module in layer 8 of the YOLOV5 backbone network is replaced with the C3-CA module, and the SE attention mechanism is embedded to construct a "C3-CA+SE" dual attention structure to enhance the capture of small target detail features; Head network improvement: The first two FPN structures of the YOLOV5 head network are replaced with BiFPN modules, and the integration efficiency of shallow small target features is improved through adaptive weighted fusion. Loss function improvement: Adaptive fusion loss function of EIoU and NWD is adopted, and the weights of the two losses are dynamically allocated according to the proportion of small targets in the current training batch to solve the problems of small target loss being masked by medium and large targets and gradient vanishing. Detection head improvement: The native detection head has been replaced with the ASFF-Detect detection head to optimize cross-scale target localization consistency.
[0020] “C3-CA+SE” dual attention structure: The C3-CA module retains the original C3 dual-branch structure (the main branch contains multiple Bottleneck layers, and the auxiliary branch is a 1×1 convolution), and embeds a coordinate attention layer (CoordAtt) at the end of the main branch. By decomposing the attention weights in the horizontal and vertical coordinate directions, it enhances the model's robustness to changes in the position of small targets. The input and output dimensions and interface of this module are consistent with the standard C3 module, and it can be directly replaced without additional parameter adjustments. SE attention mechanism: After being connected to the C3-CA module, it adaptively adjusts the channel feature weights through a "compression-excitation" process to enhance the response of key features of small targets and suppress irrelevant background features; BiFPN Feature Fusion Module: Only the first two FPN structures in the YOLOv5 head network are replaced, while the last two FPN structures are retained for feature fusion of medium and large targets. Learnable adaptive weight parameters are introduced to dynamically allocate fusion weights for features of different scales through training. A fast normalized weighted fusion method is adopted to reduce feature redundancy. The activation function is placed before the feature fusion operation, supporting dynamic multi-branch structures and strengthening the transmission and preservation of shallow small target features.
[0021] EIoU and NWD adaptive fusion loss function: The loss function is composed of a weighted average of the NWD loss (adapting to small targets) and the EIoU loss (adapting to medium and large targets). The weights are dynamically allocated based on the proportion of small targets in the current training batch, as shown in the following formula: ; ; ; ; in, The proportion of smaller goals within the overall goal. For the number of feature layers, For the first The number of targets on the layer feature map This is an indicator function that returns 1 when the condition is true. For the target area, These are the weighting coefficients for NWD loss. Based on NWD weights, For the maximum NWD weight, These are the weighting coefficients for the EIOU loss. For the final bounding box loss, , These are the normalized loss and EIoU loss, respectively, based on Wasserstein distance.
[0022] ASFF-Detect head: First, the 8×, 16×, and 32× downsampled feature maps output by the FPN are uniformly mapped to a 256-dimensional feature channel using 1×1 convolution. Then, bilinear interpolation is used to upsample the 16× and 32× feature maps to an 8× spatial resolution, eliminating feature incompatibility issues caused by scale differences. Finally, the fusion weights are dynamically allocated based on the target scale confidence of local regions in the feature maps—the 8× feature map is given higher weight to regions dominated by small targets, and the 32× feature map is given higher weight to regions dominated by large targets. The fused features are input into the classification branch (outputting defect category confidence) and the regression branch (outputting bounding box coordinates) to complete the prediction.
[0023] S3 trains an improved YOLOv5 model based on a prepared multimodal defect dataset and optimizes model parameters to enhance cross-scenario robustness.
[0024] Training parameter configuration: adopt cosine annealing learning rate scheduling strategy (T_max=200); iterate 200 times, and enable early stopping strategy (if there is no improvement in mAP@0.5 on the validation set for 10 consecutive times, training will be terminated).
[0025] S4 uses a trained, improved YOLOV5 model to detect surface defects in finished workpieces. The model inferences and outputs the defect type, location coordinates, and confidence level, filtering out low-confidence detection results.
[0026] The model's detection performance was evaluated using precision, recall, and mAP@0.5 metrics, and the specific calculation method is as follows: ; ; ; in, The number of samples correctly classified as positive. The number of samples that were misclassified as positive. The number of samples that were misclassified as negative. The total number of categories, Number of tests This represents the average precision for each category.
[0027] S5 provides detailed visualizations of detected defects, detection accuracy statistics, and potential optimization suggestions, specifically including: Defect location and annotation diagram: Mark the defect boundary box, defect type and confidence level on the original workpiece image; Detection accuracy statistics charts: bar charts comparing accuracy and recall for each type of defect, and an overall mAP@0.5 trend chart; Optimization Recommendations: Based on the test results, we propose industrial process optimization directions for high-frequency defect types (such as adjusting processing temperature, optimizing raw material purity, etc.), providing valuable insights for system maintenance and optimization.
[0028] This invention encompasses any substitutions, modifications, equivalent methods, and solutions made within the spirit and scope of this invention. To provide the public with a thorough understanding of this invention, specific details are described in detail in the following preferred embodiments; however, those skilled in the art will fully understand the invention even without these details. Furthermore, to avoid unnecessary misunderstanding of the essence of this invention, well-known methods, processes, procedures, components, and circuits are not described in detail.
[0029] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A method for detecting surface defects in finished workpieces based on an improved YOLOv5, characterized in that, Includes the following steps: S1. Acquire images of surface defects on the workpiece, preprocess the images of surface defects on the workpiece, perform data augmentation operations, and construct a defect dataset. S2, Construct a YOLOv5 model with multi-dimensional improvements; S3, Train the multi-dimensional improved YOLOV5 model using the multimodal defect dataset; S4. The trained multi-dimensional improved YOLOV5 model is used to detect defects in workpiece images. The defect type, location coordinates and confidence score are output. The accuracy, recall and mAP@0.5 indexes are collected to evaluate the detection performance of the multi-dimensional improved YOLOV5 model. S5 visualizes the detection results, outputting defect location annotation maps, statistical charts of detection accuracy for each category, and suggestions for optimizing industrial processes.
2. The method for detecting surface defects in finished workpieces based on an improved YOLOv5 according to claim 1, characterized in that, The preprocessing and data augmentation operations in S1 include: Perform adaptive histogram equalization and denoising operations on the image; Data augmentation is performed through random flipping, rotation, and brightness adjustment; images are divided into training and validation sets according to a preset ratio and then labeled.
3. The method for detecting surface defects in finished workpieces based on an improved YOLOv5 according to claim 2, characterized in that, The multi-dimensional improved YOLOv5 model in S2 includes: S21, replace the C3 module in the 8th layer of the YOLOV5 backbone network with the C3-CA module, and embed the SE attention mechanism to construct a C3-CA+SE dual attention structure to enhance the capture of small target detail features; S22, replace the first two FPN structures of the YOLOV5 head network with BiFPN modules, and improve the integration efficiency of shallow small target features through adaptive weighted fusion; S23 adopts an adaptive fusion loss function of EIoU and NWD, and dynamically allocates the weights of the two losses according to the proportion of small targets in the current training batch, so as to solve the problem of small target loss being masked by medium and large targets and gradient vanishing. S24, replace the native detection head with the ASFF-Detect detection head to optimize cross-scale target localization consistency.
4. The method for detecting surface defects in finished workpieces based on an improved YOLOv5 according to claim 3, characterized in that, The weight coefficients of NWD loss and EIoU loss in the adaptive fusion loss function of EIoU and NWD are dynamically adjusted based on the proportion of small targets in the current batch, as follows: ; ; ; ; in, The proportion of smaller goals within the overall goal. For the number of feature layers, For the first The number of targets on the layer feature map This is an indicator function that returns 1 when the condition is true. For the target area, These are the weighting coefficients for NWD loss. Based on NWD weights, For the maximum NWD weight, These are the weighting coefficients for the EIOU loss. For the final bounding box loss, , These are the normalized loss and EIoU loss, respectively, based on Wasserstein distance.
5. The method for detecting surface defects in finished workpieces based on an improved YOLOv5 according to claim 4, characterized in that, The ASFF-Detect head in S24 achieves dynamic fusion of multi-scale feature maps through a unified channel mapping, spatial alignment and target scale perception fusion weight mechanism, and inputs the fusion results into the classification branch and regression branch respectively to complete target detection.
6. The method for detecting surface defects in finished workpieces based on an improved YOLOv5 according to claim 5, characterized in that, The multi-dimensional improved YOLOv5 model is trained for 200 epochs, and the validation set mAP@0.5 is used as the evaluation metric. Training is terminated early when the validation set mAP@0.5 shows no improvement for 10 consecutive epochs.
7. The method for detecting surface defects in finished workpieces based on an improved YOLOv5 according to claim 6, characterized in that, The precision is expressed as: ; The recall rate is expressed as: ; The mAP@0.5 metric is expressed as follows: ; in, The number of samples correctly classified as positive. The number of samples that were misclassified as positive. The number of samples that were misclassified as negative. The total number of categories, Number of tests This represents the average precision for each category.