Stamping part multi-class defect detection system and method based on fusion sample generation and feature contrast learning

By generating a rich set of defect samples and using feature comparison learning, combined with cross-layer attention reweighting and multi-scale feature fusion, the problem of sample scarcity in the detection of surface defects of stamped parts is solved, and efficient and stable multi-class defect detection is achieved.

CN122391070APending Publication Date: 2026-07-14SHANGHAI BAOSIGHT SOFTWARE CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
SHANGHAI BAOSIGHT SOFTWARE CO LTD
Filing Date
2026-03-11
Publication Date
2026-07-14

AI Technical Summary

Technical Problem

Existing surface defect detection technologies for stamped parts suffer from a high dependence on high-quality samples in their detection models. When samples are scarce, the model performance deteriorates, making it difficult to meet the requirements for efficient, stable, and accurate detection.

Method used

By generating a defect sample module, a rich and diverse defect sample set is constructed. Combined with a feature comparison learning mechanism, a cross-layer attention reweighting strategy and multi-scale feature fusion are adopted to achieve high-precision defect detection.

Benefits of technology

It significantly improves the model's generalization ability and detection accuracy under multi-category defect conditions, enhances the reliability and consistency of the detection system, and solves the problem of insufficient model convergence caused by sample scarcity.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122391070A_ABST
    Figure CN122391070A_ABST
Patent Text Reader

Abstract

The application provides a stamping part multi-class defect detection method and system fusing sample generation and feature contrast learning, comprising: generating a defect sample; training a defect detection model based on the defect sample; and detecting a target image based on the defect detection model. The defect sample generation comprises: generating a simulation abnormal mask based on a real defect sample, obtaining a normal sample image and an abnormal position mask, generating an embedding vector reflecting abnormal types and spatial distribution through embedding network coding, generating a defective image in a latent feature space, and obtaining a multi-state defective image through feature attention optimization and image enhancement. The application solves the technical problems that the existing stamping part surface defect detection has poor stability, low detection efficiency, and is difficult to adapt to large-scale online detection requirements under complex working conditions such as uneven illumination and metal reflection.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of surface defect detection technology, specifically to a multi-category defect detection system and method for stamped parts that integrates sample generation and feature comparison learning. Background Technology

[0002] In the automotive and home appliance manufacturing industries, stamped parts are key structural components, and their surface quality directly affects the assembly accuracy and appearance of the products. Due to the influence of factors such as raw material quality, die wear, lubrication conditions, and fluctuations in the stamping process, stamped parts are prone to various surface defects such as scratches, dents, cracks, and embossing during production. Therefore, how to achieve efficient, stable, and accurate surface defect detection has become an important research topic in industrial automated production.

[0003] Currently, traditional manual visual inspection methods are still widely used, but these methods suffer from low inspection efficiency, poor stability, and reliance on human experience, making it difficult to meet the quality and consistency requirements of large-scale, high-speed production lines. Therefore, intelligent inspection methods based on computer vision and deep learning are gradually emerging and have become a research hotspot.

[0004] Existing technical solutions, such as CN202411467285.X, present a method for detecting surface defects in stamped parts based on Markov random fields. This method proposes to fuse pixel-level Markov random fields (MRF) with a region merging algorithm to improve the accuracy of defect detection. This method improves detection accuracy in complex scenes by combining pixel information with edge region features. However, this approach is highly dependent on algorithm parameters, exhibits poor stability under uneven lighting or metal reflection conditions, and has high computational complexity, making it unsuitable for real-time online detection applications.

[0005] CN202110305370.6: A method for detecting surface defects in stamped parts based on SSD target detection and image enhancement attempts to improve the saliency of defects by enhancing image brightness and contrast, and then combining it with an SSD network to achieve automatic localization and classification. Although this method has good results under standard images, the image enhancement method is singular and lacks the ability to constrain abnormal structures, and the generated samples have limited effect on model training; at the same time, when defect samples are scarce or unevenly distributed, the detection accuracy of the model decreases significantly and the generalization ability is insufficient.

[0006] CN202411290456.6: A method for detecting defects in stamped parts based on a multi-angle high-speed visual inspection system. This method acquires workpiece images from different angles using a multi-camera array and integrates deep learning algorithms to achieve full-coverage inspection of the stamped part surface. While this approach has advantages in detection range and angle adaptability, the system hardware structure is complex, data annotation costs are high, and model training cycles are long. Furthermore, feature matching errors caused by multi-view fusion affect detection speed, making it difficult to balance real-time performance and accuracy.

[0007] CN202211507477.X: A method for identifying defects in stamped parts based on multi-model hierarchical discrimination. This method constructs a multi-model hierarchical structure and improves the identification rate of multiple types of defects through the linkage of initial inspection and re-inspection, thus enhancing the detection capability of certain complex defects. However, the model structure of this method is complex, consumes significant computational resources, and still suffers from insufficient feature discrimination capability for novel and minute defects. False positives and false negatives are relatively common, making it difficult to cope with complex and ever-changing industrial application scenarios.

[0008] In summary, although existing surface defect detection technologies for stamped parts have made positive progress in terms of detection accuracy and algorithm structure, they still face the following core problems: the detection model is highly dependent on high-quality samples, and the scarcity of samples will limit the model's performance. Summary of the Invention

[0009] To address the shortcomings of existing technologies, the purpose of this invention is to provide a multi-category defect detection system and method for stamped parts that integrates sample generation and feature comparison learning.

[0010] According to one aspect of the present invention, a method for detecting multi-category defects in stamped parts by fusing sample generation and feature comparison learning includes: Step S1: Generate defect samples.

[0011] Step S2: Based on the defect samples, complete the training of the defect detection model.

[0012] Step S3: Detect the target image based on the defect detection model.

[0013] Preferably, step S1 includes: Sub-step S1.1: Generate a simulation anomaly mask based on real defect samples.

[0014] Sub-step S1.2: Obtain normal sample images and abnormal location masks.

[0015] Sub-step S1.3: Encode the features of normal sample images, simulated anomaly masks, and anomaly location masks through an embedding network to generate an embedding vector that reflects the anomaly type and spatial distribution.

[0016] Sub-step S1.4: Based on the embedding vector, iteratively generate sample images with defects in the latent feature space.

[0017] Sub-step S1.5: For the sample image, generate an optimized defect sample image by adjusting the attention weight distribution between cross-layer features.

[0018] Sub-step S1.6: For the optimized defect sample image, generate multiple change state images of the defect sample image through image enhancement.

[0019] Preferably, the image enhancement includes abnormal region blending, brightness perturbation, and rotational cropping.

[0020] Preferably, step S2 includes: Sub-step S2.1: Divide the defect sample image into multiple local regions, classify the local regions, and generate corresponding pseudo-labels for each local region based on the classification. The types of local regions include various defect types and normal types.

[0021] Sub-step S2.2: Extract multi-layer features for each local region.

[0022] Sub-step S2.3: Based on the multi-layer features of each local region, calculate the similarity of local regions of the same type and the feature similarity of local regions of different types, and generate a similarity matrix.

[0023] Sub-step S2.4: Generate feature representation weights based on the similarity matrix.

[0024] Sub-step S2.5: Calculate the feature distance between the local regions of each defect type and the local regions of the normal type.

[0025] Sub-step S2.6: Establish feature distance constraints between normal local regions and defective local regions by relaxing the contrastive loss function.

[0026] Sub-step S2.7: Based on pseudo-labels, feature representation weights, and feature distance constraints, complete the training of the defect detection model.

[0027] Preferably, step S3 includes: Sub-step S3.1: Obtain the target image.

[0028] Sub-step S3.2: Extract feature information from the target image.

[0029] Sub-step S3.3: Based on the defect detection model, analyze the feature information and output the defect information.

[0030] Preferably, sub-step S3.2 includes: Sub-step S3.2.1: Divide the target image into multiple sub-target regions.

[0031] Sub-step S3.2.2: Extract multi-layer features of all sub-target regions.

[0032] Preferably, sub-step S3.3 includes: Sub-step S3.3.1: Through a multi-layer feature adaptive aggregation mechanism, the spatial and semantic features of each sub-target region at different scales are mapped to a unified feature space and then weighted and fused to generate a feature tensor.

[0033] Sub-step S3.3.2: Based on the preset normal sample feature memory, calculate the feature distance between the features in the feature tensor and the features in the memory, generate anomaly scores, and generate anomaly distribution maps and mask information.

[0034] Sub-step S3.3.3: Based on the defect detection model, analyze the anomaly distribution map and mask information, detect each sub-target region, record the information of each sub-target region, and mark the areas where defects are suspected to exist.

[0035] Sub-step S3.3.4: Obtain feature information of the neighborhood of the suspected defect area, perform weighted processing on the neighborhood features, and combine the feature information of the neighborhood to confirm the suspected defect area.

[0036] Sub-step S3.3.5: Output defect location information, type information, and confidence level.

[0037] According to another aspect of the present invention, a multi-category defect detection system for stamped parts that integrates sample generation and feature comparison learning is characterized by comprising: Module M1: Generates defect samples.

[0038] Module M2: Based on defect samples, complete the training of the defect detection model.

[0039] Module M3: Based on the defect detection model, it detects the target image.

[0040] Preferably, the module M1 includes: Submodule M1.1: Generates a simulation anomaly mask based on real defect samples.

[0041] Submodule M1.2: Obtain normal sample images and abnormal location masks.

[0042] Submodule M1.3: It uses an embedding network to encode features of normal sample images, simulated anomaly masks, and anomaly location masks to generate embedding vectors that reflect the anomaly type and spatial distribution.

[0043] Submodule M1.4: Based on the embedding vector, iteratively generates sample images with defects in the latent feature space.

[0044] Submodule M1.5: For sample images, it generates optimized defect sample images by adjusting the attention weight distribution between cross-layer features.

[0045] Submodule M1.6: For the optimized defect sample image, generate multiple change state images of the defect sample image through image enhancement.

[0046] Preferably, the image enhancement includes abnormal region blending, brightness perturbation, and rotational cropping.

[0047] Compared with the prior art, the present invention has the following beneficial effects: 1. This invention constructs a sample generation module, which simulates the structural morphology, abnormal texture and spatial distribution of defects in the latent feature space through spatial anomaly embedding and diffusion generation mechanisms. It can generate a sufficient number of defect samples with rich types and high consistency with real data, thereby significantly alleviating the phenomenon of insufficient model convergence caused by insufficient samples and improving the generalization ability of the model under multi-class defect conditions.

[0048] 2. This invention introduces a cross-layer attention reweighting strategy, which dynamically adjusts the attention distribution during the anomaly implantation process to ensure that the defect region is strictly controlled by the position and shape of the real anomaly mask, so that the anomaly information does not spread to the surrounding background, thereby avoiding artifact generation and background contamination problems, and improving the authenticity, structural stability and spatial controllability of the generated sample.

[0049] 3. This invention designs a multi-scale feature fusion module and a contrastive learning structure. By fusing shallow texture features, deep semantic features, and multi-scale features across scales, and combining regional feature similarity and feature distance constraints, the model can simultaneously focus on large-scale structural defects and fine-grained local anomalies, thereby significantly improving the accuracy of defect localization and category recognition.

[0050] 4. This invention centrally schedules and optimizes the entire process of sample generation, feature comparison, and model training through a main control and training management module. This achieves synergistic consistency between sample generation quality, feature learning objectives, and detection model training strategies, making the model training process more stable and efficient, and improving the reliability, consistency, and engineering deployability of the final detection system. Attached Figure Description

[0051] Other features, objects, and advantages of the present invention will become more apparent from the following detailed description of non-limiting embodiments with reference to the accompanying drawings: Figure 1 This is a schematic diagram of the method provided in Example 1.

[0052] Figure 2 The system flowchart provided for Embodiment 2.

[0053] Figure 3 The algorithm structure diagram provided in Example 2. Detailed Implementation

[0054] The present invention will now be described in detail with reference to specific embodiments. These embodiments will help those skilled in the art to further understand the present invention, but do not limit the invention in any way. It should be noted that those skilled in the art can make several changes and improvements without departing from the concept of the present invention. These all fall within the scope of protection of the present invention.

[0055] Example 1: This embodiment provides a method for detecting multi-category defects in stamped parts by integrating sample generation and feature contrast learning. This method achieves high-precision detection of multi-category defects in stamped parts by constructing large-scale simulated defect samples, introducing a feature contrast learning mechanism, and combining a context-aware detection strategy. The method first constructs a sufficiently diverse defect sample set through a multi-stage sample generation mechanism. Then, it trains the defect detection model based on pseudo-labels, feature similarity, and contrast loss constraints. Finally, in the detection stage, it achieves accurate defect localization and identification based on multi-scale feature aggregation, feature distance calculation, and context weighting. This method effectively alleviates the problem of insufficient real defect samples and strengthens the ability to distinguish between different defect types and normal areas at the feature level, thereby achieving stable and high-precision detection of defects in stamped parts.

[0056] Step S1: Generate defect samples.

[0057] Based on the above scheme, a large number of defective sample images are constructed by combining manual construction, simulation inference, and feature space generation. This allows the model to obtain rich defect representations during the training phase, thereby improving the model's generalization ability. This step uses real defect samples as a reference to generate defect patterns of different types, scales, and locations in the latent space, thus effectively expanding the amount of training data.

[0058] It is understandable that step S1 also includes the following sub-steps: S1.1: Generate a simulated anomaly mask based on real defect samples.

[0059] This mask is used to simulate the structure, shape, and distribution of real defects, serving as a base template for subsequent generation, making the simulated defects more closely resemble the characteristics of real defects.

[0060] Step S1.1 includes: S1.1.1: Obtain defect annotation information of at least one set of real defect samples, wherein the defect annotation information includes a defect region mask; S1.1.2: Model the mask of the defect region to obtain a mask generator; S1.1.3: The mask generator generates multiple simulated anomaly location masks, which are used to indicate the location, area range and / or shape distribution of the region where subsequent defects are generated; S1.2: Obtain normal sample images and abnormal location masks.

[0061] Normal images provide background texture and material information, while anomalous location masks are used to determine the spatial location of defect implantation or simulation.

[0062] Step S1.2 includes: S1.2.1: Obtain at least one normal sample image from the normal sample library; S1.2.2: Select an anomaly location mask that matches the normal sample image from the simulation anomaly location mask generated in step S1.1; S1.2.3: Form a data pair to be generated, wherein the data pair to be generated includes at least a normal sample image and its corresponding abnormal location mask.

[0063] S1.3: By embedding a network, feature encoding is performed on normal sample images, simulated anomaly masks, and anomaly location masks to generate embedding vectors that reflect the anomaly type and spatial distribution.

[0064] Step S1.3 includes: S1.3.1: Input normal sample images into the embedded network to extract background and texture features of normal images; S1.3.2: Input the abnormal location mask into the embedded network or spatial coding branch to extract spatial features that characterize the abnormal location, shape and spatial distribution; S1.3.3: The type features used to characterize the defect type are fused with the spatial features to obtain an embedding vector; The embedding vector is used to characterize the defect type and spatial distribution, serving as conditional information for defect sample generation.

[0065] Embedded networks map input combinations to a latent feature space, allowing the type, morphology, and spatial distribution of defects to be expressed in a vector manner.

[0066] S1.4: Based on the embedding vector, iteratively generate sample images with defects in the latent feature space.

[0067] By utilizing latent space generative frameworks (such as diffusion models and generative adversarial networks), embedded vectors can be restored to visible images, making the generated defective samples more natural in terms of texture, color, and spatial structure.

[0068] Step S1.4 includes: S1.4.1: Map the normal sample image to the latent feature space to obtain the latent feature representation; S1.4.2: Using the embedding vector obtained in step S1.3 as a condition, iteratively update the latent feature representation to generate a latent feature representation with defects; S1.4.3: During the iteration process, the potential features are constrained according to the abnormal location mask, so that defect features are generated within the area covered by the abnormal location mask, while the area outside the area covered by the abnormal location mask remains consistent with the normal sample image. S1.4.4: Transform the generated latent feature representation into the image space to obtain the initial defect sample image.

[0069] S1.5: For the sample image, an optimized defect sample image is generated by adjusting the attention weight distribution between cross-layer features.

[0070] By strengthening the representation of defective regions through a cross-layer feature fusion mechanism, defects become more prominent and easier to identify.

[0071] Step S1.5 includes: S1.5.1: Obtain the initial defect sample image and the normal sample image; S1.5.2: Generate a weight map based on the degree of difference between the initial defect sample image and the normal sample image within the masked area of ​​the abnormal location; S1.5.3: The weight map is used to adjust the attention weight distribution among features of different layers in the generative network, so that regions with smaller differences receive higher attention weights; S1.5.4: Based on the adjusted attention weights, continue iterative generation or perform one or more refinement generation to obtain the optimized defect sample image; The attention weight adjustment allows the generated defect texture to more fully cover the abnormal location mask indication area and improves the consistency between the defect boundary and the abnormal location mask.

[0072] S1.6: Perform image enhancement processing on the optimized defect sample image to generate multiple images of changing states; the image enhancement processing includes one or more of geometric enhancement and imaging enhancement, geometric enhancement includes one or more of rotation, flipping, scaling, translation, and affine transformation, and imaging enhancement includes one or more of brightness adjustment, contrast adjustment, noise perturbation, blurring, and compression perturbation.

[0073] Image enhancement gives defect samples rich variations, including positional changes, brightness variations, and structural perturbations, thereby improving the robustness of the model.

[0074] Based on the above scheme, the comprehensive application of latent space generation, attention redistribution and enhancement processing makes the generated defective samples more diverse and realistic, thereby significantly enhancing the effectiveness of training data.

[0075] Step S2: Based on the defect samples, complete the training of the defect detection model.

[0076] Based on the above scheme, the model learns the distribution differences between each defect category and the normal area in the feature space from a large number of defect samples. It strengthens the inter-class differentiation ability and weakens the intra-class differences through feature comparison constraints, thereby forming a detection model that can stably identify stamping defects.

[0077] It is understandable that step S2 also includes the following sub-steps: S2.1: Divide the defect sample image into multiple local regions, classify the local regions, and generate corresponding pseudo-labels for each local region based on the classification. The types of local regions include various defect types and normal types.

[0078] Step S2.1 includes: S2.1.1: Divide each defect sample image into multiple local regions, wherein the local region is one of the following: image block divided by a fixed grid, image block cropped by a sliding window, or local region corresponding to the feature map position; S2.1.2: Determine the type of each local region, wherein the type includes at least: various defect types and normal types; S2.1.3: Generate pseudo-labels for each local region based on its type; wherein, the pseudo-labels include local region category pseudo-labels and / or local region similarity pseudo-labels.

[0079] Pseudo-labels are used to address the problems of difficult and costly manual annotation of defective regions, enabling the model to obtain semantic information about the region through self-supervision.

[0080] S2.2: Extract multi-layer features for each local region.

[0081] Step S2.2 includes: S2.2.1: Input the defect sample image into the feature extraction network to obtain feature maps of at least two layers; S2.2.2: Align feature maps from different layers to the same resolution and then stitch or aggregate them; S2.2.3: Perform a local aggregation operation on the aligned multi-layer feature map for each local region to obtain the multi-layer feature vector of the local region; wherein the local aggregation operation includes adaptive average pooling, max pooling, or a combination of both.

[0082] By jointly extracting features from deep, medium, and shallow layers, the feature representation combines local texture with global semantics.

[0083] S2.3: Based on the multi-layer features of each local region, calculate the similarity of local regions of the same type and the feature similarity of local regions of different types, and generate a similarity matrix.

[0084] Step S2.3 includes: S2.3.1: Within a training batch, obtain feature vectors for multiple local regions; S2.3.2: Calculate the similarity between each pair of the feature vectors to obtain the similarity of local regions of the same type and the similarity of local regions of different types; S2.3.3: Arrange the similarities according to the local region index to generate a similarity matrix.

[0085] By comparing the feature representations across multiple regions, a foundation matrix is ​​formed for subsequent contrastive learning.

[0086] S2.4: Generate feature representation weights based on the similarity matrix.

[0087] Step S2.4 includes: S2.4.1: Normalize the similarity matrix to obtain the weight matrix; S2.4.2: The weight matrix is ​​used to indicate the strength of the "attraction / repulsion" relationship between local regions, wherein the higher the similarity of local regions of the same type, the greater the weight, and the lower the weight of local regions of different types.

[0088] The weights reflect the relative contributions of different regions to the feature distribution, making the model pay more attention to defective regions with significant differences.

[0089] S2.5: Calculate the characteristic distance between the local regions of each defect type and the local regions of the normal type.

[0090] Step S2.5 includes: Step S2.5.1: Summarize the feature vector sets of local regions for normal types and the feature vector sets of local regions for each defect type respectively; Step S2.5.2: Calculate the characteristic distance between each defect type and the normal type, wherein the characteristic distance includes one or more of Euclidean distance, cosine distance or Mahalanobis distance; Step S2.5.3: Output the distance measurement results of each defect type relative to the normal type.

[0091] Feature distance characterizes the degree of separation between different categories in the feature space and is a key input for contrastive learning.

[0092] S2.6: Establish feature distance constraints between normal local regions and defective local regions by relaxing the contrastive loss function.

[0093] Step S2.6 includes: S2.6.1: Apply a "narrowing" constraint to local regions of the same type to reduce their feature distance; S2.6.2: Apply a "pull-away" constraint to heterogeneous local regions, making their feature distance greater than a preset interval threshold; S2.6.3: Introduce the aforementioned feature representation weights into the loss function so that the contribution of different local regions to the total loss corresponds to their respective weights; S2.6.4: Through the relaxed contrast loss function, the local region features of the normal type are clustered, the local region features of different defect types are separated from each other, and a separable distance is formed with the normal type.

[0094] This loss function encourages "similar samples to be closer together and different samples to be farther apart", thereby improving the reliability of classification and detection.

[0095] S2.7: Based on pseudo-labels, feature representation weights, and feature distance constraints, complete the training of the defect detection model.

[0096] Step S2.7 includes: S2.7.1: Use the pseudo-label of the local region as the supervision signal, the feature representation weight obtained from similarity as the loss weighting signal, and the relaxed contrast loss as the feature constraint signal; S2.7.2: Jointly optimize the parameters of the defect detection model to enable the model to obtain feature representation capabilities for defect detection; S2.7.3: After training is completed, a defect detection model and a normal sample feature memory or memory feature set for subsequent detection are obtained (which can be established simultaneously during the training phase or after training).

[0097] By comprehensively constraining the model, it can achieve stable discrimination ability.

[0098] Based on the above scheme, by combining pseudo-labels, similarity matrices and contrast loss, a strong ability to distinguish multiple types of defects is achieved through training, so that the defect detection model still has high recognition performance in complex scenarios.

[0099] Step S3: Detect the target image based on the defect detection model.

[0100] Based on the above scheme, the accurate identification and localization of defective regions can be achieved by extracting features from the target image, projecting features, measuring feature distances, and performing context-weighted analysis.

[0101] Understandably, step S3 also includes: S3.1: Acquire the target image.

[0102] S3.2: Extract feature information from the target image.

[0103] Understandably, step S3.2 also includes: S3.2.1: Divide the target image into multiple sub-target regions.

[0104] S3.2.2: Extract multi-layer features of all sub-target regions.

[0105] S3.3: Based on the defect detection model, analyze the feature information and output the defect information.

[0106] It is understandable that step S3.3 also includes the following sub-steps: S3.3.1: Through a multi-layer feature adaptive aggregation mechanism, spatial and semantic features of different scales of each sub-target region are mapped to a unified feature space and weighted fusion is performed to generate a feature tensor.

[0107] Multi-scale aggregation solves the problem of different defect sizes, enabling all features to be expressed in a unified space.

[0108] S3.3.2: Based on the preset normal sample feature memory, calculate the feature distance between the features in the feature tensor and the features in the memory, generate anomaly scores, and generate anomaly distribution maps and mask information.

[0109] Step S3.3.2 includes: S3.3.2.1: A normal sample feature memory bank is pre-established, the memory bank containing representative feature vectors extracted and filtered from normal samples; S3.3.2.2: For each sub-target region feature in the feature tensor, calculate the distance between it and at least one nearest neighbor feature in the memory to obtain the anomaly score of the sub-target region; S3.3.2.3: Fill the image space with the anomaly scores of each sub-target region to generate an anomaly distribution map, and generate mask information according to the preset threshold.

[0110] The larger the feature distance, the more it deviates from the normal pattern, and it is judged as highly suspicious.

[0111] S3.3.3: Based on the defect detection model, analyze the anomaly distribution map and mask information, detect each sub-target area, record the information of each sub-target area, and mark the areas where defects are suspected to exist.

[0112] S3.3.4: Obtain neighborhood feature information of the suspected defect area, perform weighted processing on the neighborhood features, and combine the neighborhood feature information to confirm the suspected defect area.

[0113] Context-aware mechanisms leverage neighborhood structures to enhance defect detection and reduce false detections.

[0114] S3.3.5: Output defect location information, type information, and confidence level.

[0115] Based on the above scheme, the combined application of multi-layer feature aggregation, feature distance measurement and context confirmation mechanism makes the final defect identification result more accurate and reliable.

[0116] Example 2: A method for multi-class defect detection in stamped parts that integrates sample generation and feature comparison learning includes: M1: Sample generation module.

[0117] The sample generation module addresses the shortage of rare defect samples on the surface of stamped parts by generating diverse and reliable defect samples, providing sufficient training data to support the detection model. This module includes the following sub-modules: M1.1: Spatial Anomaly Embedding Submodule (MAE). This submodule receives normal sample images and anomaly location masks, inputs both into the embedding network for feature encoding, and generates embedding vectors reflecting the anomaly type and spatial distribution. Its output serves as the input condition for the diffusion generation submodule, guiding the generation of subsequent anomaly samples.

[0118] M1.2: Diffusion Generation Submodule (LDM). This submodule is based on the latent variable diffusion modeling method, iteratively generating sample images with defects in the latent feature space. By introducing spatial anomaly embedding results and anomaly masks as control conditions, the generated anomalies are ensured to appear only within specified regions, thereby guaranteeing the spatial controllability and structural consistency of the defects. The generated images are then passed to the adaptive attention reweighting submodule for feature optimization.

[0119] M1.3: Adaptive Attention Reweighting Submodule (AAR). This submodule adaptively corrects the correlation between cross-layer features during the diffusion generation process. By adjusting the croMM-attention weight distribution, it strengthens the feature response of defective regions and suppresses background information interference, thereby improving the realism of defective samples. The corrected feature map is output to the anomaly mask generation and data augmentation submodule.

[0120] M1.4: Anomaly Mask Generation and Data Augmentation Submodule. When the number of real defect masks is limited, this submodule utilizes textual inverMion technology to generate simulated anomaly masks, expanding data diversity. Augmented datasets are generated through operations such as anomaly region blending, brightness perturbation, and rotation cropping, further increasing the number of generated samples and coverage. The final output sample data and mask information are then passed to the feature contrast learning module.

[0121] M2: Feature extraction and contrastive learning module.

[0122] This module extracts multi-level representation features from the samples output by the sample generation module and constructs a comparative learning mechanism between defective and normal samples to enhance the model's ability to discriminate subtle structural differences. This module includes the following sub-modules: M2.1: Patch Segmentation and Feature Extraction Submodule (ReConPatch). This submodule segments the input image into multiple local regions (patches) and uses a pre-trained convolutional neural network to extract multi-layer features for each patch. The output feature set provides the foundation for subsequent feature comparison learning.

[0123] M2.2: Feature Contrast and Pseudo-Label Generation Submodule. This submodule calculates the similarity between similar and dissimilar patch features, and establishes feature distance constraints between normal and abnormal samples by introducing a relaxed contrastive loss function. Simultaneously, it uses a similarity matrix to construct automatic pseudo-labels, assisting the network in completing differentiated feature learning in unsupervised environments. The output is the feature representation weights and a set of pseudo-labels.

[0124] M2.3: Anomaly Memory Construction and Anomaly Score Calculation Submodule. This submodule constructs an anomaly memory from the set of normal patch features, which is used to compare new sample features during the inference phase. By calculating the minimum distance between the input features and the memory features, the anomaly score for each image region is obtained, thereby generating an anomaly distribution map and mask information. The output information is passed to the subsequent detection module for accurate identification.

[0125] M3: Multi-scale feature fusion module.

[0126] This module fuses shallow and deep feature information to improve the model's robustness and discriminative power against multi-scale defects. Through a multi-layer feature adaptive aggregation mechanism, spatial and semantic features at different scales are mapped to a unified feature space and then weighted and fused. The fused feature tensor is then passed to the defect detection and classification module for defect category identification and localization.

[0127] M4: Defect detection and classification module.

[0128] This module generates defect identification and classification output for the samples. Internally, it includes an adaptive anchor box generation unit, a context-aware feature enhancement unit, and a multi-branch detection head. Its main workflow is as follows: M4.1: Using the fused feature tensor as input, the anchor box generation unit predicts the location regions where defects may exist.

[0129] M4.2: Introduces a context-aware mechanism to perform feature weighting on the target region and enhance the neighborhood information of the defect region; M4.3: The multi-branch detection head structure outputs the type label, confidence level, and location bounding box for each type of defect, enabling the localization and identification of multiple types of defects.

[0130] The test results are ultimately sent to the main control module for aggregation and performance evaluation.

[0131] M5: Main control and training management module.

[0132] This module is the core control unit of the system, used for overall management, parameter scheduling, and model optimization of the entire process. Its main functions include: (1) Responsible for coordinating the data flow between the sample generation, feature comparison learning and detection modules; (2) Monitor the network training status and adjust the strategy accordingly; (3) Record the output data and model performance indicators at each stage to achieve training closed-loop optimization.

[0133] The output optimized model parameters and test result reports are used for defect detection of stamped parts and product quality tracking.

[0134] M6: Overall system operation process.

[0135] The system operates according to the following process: ① The sample generation module generates multi-category rare defect samples based on real samples; ② The feature extraction and contrastive learning module completes feature parsing and feature difference modeling; ③ The multi-scale feature fusion module integrates multi-layer semantic information to enhance detection robustness; ④ The defect detection and classification module identifies surface defects of various categories and outputs the detection results; ⑤ The main control module summarizes the results and performs training optimization to achieve adaptive updates of the system.

[0136] Through the above-described structured design, this invention achieves an organic integration of rare defect sample generation and feature comparison learning for stamped parts, solving the problems of scarce multi-category defect samples and insufficient detection accuracy in actual production. This system boasts advantages such as efficient model training, strong feature discrimination capability, and high detection accuracy, demonstrating promising prospects for industrial applications.

[0137] As shown in Table 1 below, this table illustrates the impact of different methods on the three defect test indicators of the local stamping parts dataset. A total of 217 test data sheets of local stamping parts were collected, including three defects: necking, indentation, and cracking, with an average improvement of 8.9%.

[0138] The overall detection process is highly automated, with data utilization and training convergence speed improved simultaneously, while significantly reducing the actual workload of manual collection and labeling of rare defects.

[0139] Extensive practical testing has verified that the method of this invention exhibits excellent adaptability and robustness in defect detection of stamped parts of various types, sizes, and working conditions, and can stably output high-quality detection results.

[0140] Table 1 This invention also provides a multi-category defect detection system for stamped parts that integrates sample generation and feature comparison learning. The multi-category defect detection system for stamped parts that integrates sample generation and feature comparison learning can be implemented by executing the process steps of the multi-category defect detection method for stamped parts that integrates sample generation and feature comparison learning. That is, those skilled in the art can understand the multi-category defect detection method for stamped parts that integrates sample generation and feature comparison learning as a preferred embodiment of the multi-category defect detection system for stamped parts that integrates sample generation and feature comparison learning.

[0141] Those skilled in the art will understand that, besides implementing the system and its various devices, modules, and units provided by this invention in the form of purely computer-readable program code, the same functions can be achieved entirely through logical programming of the method steps, making the system and its various devices, modules, and units of this invention function in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers, and embedded microcontrollers. Therefore, the system and its various devices, modules, and units provided by this invention can be considered as a hardware component, and the devices, modules, and units included therein for implementing various functions can also be considered as structures within the hardware component; alternatively, the devices, modules, and units for implementing various functions can be considered as both software modules implementing the method and structures within the hardware component.

[0142] Specific embodiments of the present invention have been described above. It should be understood that the present invention is not limited to the specific embodiments described above, and those skilled in the art can make various changes or modifications within the scope of the claims, which do not affect the essence of the present invention. Unless otherwise specified, the embodiments and features described in this application can be arbitrarily combined with each other.

Claims

1. A method for detecting multi-category defects in stamped parts by integrating sample generation and feature comparison learning, characterized in that, include: Step S1: Generate defect samples; Step S2: Based on the defect samples, complete the training of the defect detection model; Step S3: Detect the target image based on the defect detection model.

2. The method according to claim 1, characterized in that, Step S1 includes: Sub-step S1.1: Generate a simulation anomaly mask based on real defect samples; Sub-step S1.2: Obtain normal sample images and abnormal location masks; Sub-step S1.3: Encode the features of normal sample images, simulated anomaly masks, and anomaly location masks through an embedding network to generate an embedding vector that reflects the anomaly type and spatial distribution; Sub-step S1.4: Based on the embedding vector, iteratively generate sample images with defects in the latent feature space; Sub-step S1.5: For the sample image, generate an optimized defect sample image by adjusting the attention weight distribution between cross-layer features; Sub-step S1.6: For the optimized defect sample image, generate multiple change state images of the defect sample image through image enhancement.

3. The method according to claim 2, characterized in that, The image enhancement includes abnormal region blending, brightness perturbation, and rotational cropping.

4. The method according to claim 1, characterized in that, Step S2 includes: Sub-step S2.1: Divide the defect sample image into multiple local regions, classify the local regions, and generate corresponding pseudo-labels for each local region based on the classification. The types of local regions include various defect types and normal types. Sub-step S2.2: Extract multi-layer features for each local region; Sub-step S2.3: Based on the multi-layer features of each local region, calculate the similarity of local regions of the same type and the feature similarity of local regions of different types, and generate a similarity matrix; Sub-step S2.4: Generate feature representation weights based on the similarity matrix; Sub-step S2.5: Calculate the feature distance between the local regions of each defect type and the local regions of the normal type; Sub-step S2.6: Establish feature distance constraints between normal local regions and defective local regions by relaxing the contrastive loss function; Sub-step S2.7: Based on pseudo-labels, feature representation weights, and feature distance constraints, complete the training of the defect detection model.

5. The method according to claim 1, characterized in that, Step S3 includes: Sub-step S3.1: Acquire the target image; Sub-step S3.2: Extract feature information from the target image; Sub-step S3.3: Based on the defect detection model, analyze the feature information and output the defect information.

6. The method according to claim 5, characterized in that, Sub-step S3.2 includes: Sub-step S3.2.1: Divide the target image into multiple sub-target regions; Sub-step S3.2.2: Extract multi-layer features of all sub-target regions.

7. The method according to claim 1, characterized in that, The sub-step S3.3 includes: Sub-step S3.3.1: Through a multi-layer feature adaptive aggregation mechanism, the spatial and semantic features of each sub-target region at different scales are mapped to a unified feature space, and weighted fusion is performed to generate a feature tensor; Sub-step S3.3.2: Based on the preset normal sample feature memory, calculate the feature distance between the features in the feature tensor and the features in the memory, generate anomaly scores, and generate anomaly distribution maps and mask information. Sub-step S3.3.3: Based on the defect detection model, analyze the anomaly distribution map and mask information, detect each sub-target region, record the information of each sub-target region, and mark the areas where defects are suspected to exist; Sub-step S3.3.4: Obtain feature information of the neighborhood of the suspected defect area, perform weighted processing on the neighborhood features, and combine the feature information of the neighborhood to confirm the suspected defect area. Sub-step S3.3.5: Output defect location information, type information, and confidence level.

8. A multi-category defect detection system for stamped parts that integrates sample generation and feature comparison learning, characterized in that, include: Module M1: Generates defect samples; Module M2: Based on defect samples, completes the training of the defect detection model; Module M3: Based on the defect detection model, it detects the target image.

9. The method according to claim 8, characterized in that, The module M1 includes: Submodule M1.1: Generates a simulation anomaly mask based on real defect samples; Submodule M1.2: Obtain normal sample images and masks of abnormal locations; Submodule M1.3: It uses an embedding network to encode features of normal sample images, simulated anomaly masks, and anomaly location masks to generate embedding vectors that reflect the anomaly type and spatial distribution. Submodule M1.4: Based on the embedding vector, iteratively generates sample images with defects in the latent feature space; Submodule M1.5: For sample images, it generates optimized defect sample images by adjusting the attention weight distribution between cross-layer features; Submodule M1.6: For the optimized defect sample image, generate multiple change state images of the defect sample image through image enhancement.

10. The method according to claim 9, characterized in that, The image enhancement includes abnormal region blending, brightness perturbation, and rotational cropping.