Metal part surface defect detection algorithm based on mold forming
By using technologies such as CNN feature extractor and dual attention encoder in the detection of surface defects of metal parts, the problems of small object detection and data imbalance are solved, and high-precision and fast defect detection are achieved, suitable for multiple object detections. field.
Patent Information
- Application Number
- CN202410777025.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-17
- Publication Date
- 2024-11-01
AI Technical Summary
The existing metal parts defect detection algorithms have problems such as low recognition accuracy and high computing resource requirements in the problems of small object detection and data imbalance, especially in the field of metal parts defect detection.
Using a CNN-based feature extractor, combined with a dual attention encoder and decoder, segmented prediction head and object detector, the deep separation convolution, multi-scale feature extraction, self-attention mechanism and fine-grained feature enhancement strategy are constructed. Efficient metal parts surface defect detection algorithm to improve the recognition ability and calculation speed of small targets.
It realizes high-precision and fast surface defect detection of metal parts, can accurately identify small target defects, and significantly improve the calculation speed while ensuring identification accuracy. It is suitable for a variety of target detection fields.
Smart Images

Figure CN118887157A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the field of artificial intelligence automated detection technology, specifically a metal parts surface defect detection algorithm based on mold forming. Background Art
[0002] Metal parts defect detection is an important quality control task in the manufacturing industry. In the process of industrial mass production, defects are inevitable on the surface of metal parts. If the defects are not detected and handled in time, the functionality of the metal parts will be seriously affected. These defects may reduce the strength, durability and reliability of metal parts, and even cause product failure or accidents. Therefore, defect detection is crucial to ensure the quality and safety of metal products. However, traditional metal parts defect detection is completed by manual visual inspection or measurement using simple tools. It has low production efficiency and is prone to problems such as unreliable human subjectivity. With the rapid development and continuous progress of computers and artificial intelligence, some detection methods based on deep learning have been applied in the manufacturing field.
[0003] Deep learning originated from the study of artificial neural networks. In recent years, it has made breakthrough progress in the field of computer vision, which has greatly promoted the development of target detection tasks. Neural networks have powerful feature extraction and representation learning capabilities, which can achieve efficient and accurate detection. Common target detection algorithms are now divided into single-stage and two-stage. Single-stage target algorithms, such as the YOLO series, can detect targets in real time by dividing the image into grid cells and predicting the category and bounding box of the target in each cell. The two-stage target detection algorithm generates candidate target regions based on region extraction, then performs feature extraction and classification of convolutional neural networks on each candidate region, and finally uses a regressor to accurately locate the bounding box of the target. In addition to the target detection model, image segmentation technology can label images at the pixel level and assign each pixel in the image to the corresponding category. This provides more detailed and accurate object boundary and region information, thereby better meeting the needs of fine-grained understanding and analysis of image content. Therefore, some segmentation models are also applied to defect detection of metal parts.
[0004] However, there are still some key issues that have not been resolved in the current metal parts defect detection.
[0005] First, the problem of small target detection. For small-sized targets, it is often difficult to accurately identify and locate them because they occupy less information in the image. In particular, defects on metal parts often belong to small target detection, so the algorithm suitable for metal part defect detection must solve problems such as target feature loss and background interference.
[0006] Second, the problem of data imbalance. When identifying targets, the data distribution between positive and negative samples is usually unbalanced. In an image, the target pixels account for a small proportion of the total pixels, that is, the number of negative samples is much larger than the number of positive samples. Unbalanced data distribution may have a negative impact on the learning and performance of the model. Among the above methods, there are improvements to these problems, such as using deep convolutional neural networks to enhance feature propagation and information flow. Or introduce feature pyramids so that the model can automatically identify targets of different sizes. In order to better focus on global relevance, some methods add transformer attention mechanisms, learn the spatial position and channel relevance of targets, and strengthen contextual information transmission and feature fusion. However, these methods are still not very accurate in detecting and positioning small or dense targets. Especially in the professional field of metal defect detection, there is a lack of a large amount of corresponding data, and the model cannot fully learn the information of related categories, resulting in low recognition accuracy. Finally, when processing high-resolution images, the above improvements will increase computing and memory requirements, and high-quality prediction results cannot be guaranteed in resource-constrained devices or scenarios. Summary of the invention
[0007] In view of the above technical problems, the present invention provides a high-precision, fast-operating algorithm for detecting surface defects of metal parts based on mold forming.
[0008] A metal part surface defect detection algorithm based on die forming, which is implemented by a detection system, which consists of a feature extractor, a foreground selector, a dual attention encoder and decoder, a segmentation prediction head and an object detector;
[0009] The feature extractor is a CNN-based feature extraction framework that uses deep separable convolution to reduce the number of parameters and computation while expanding the receptive field, thereby increasing the model's multi-scale perception of defect features. Therefore, it can maintain good performance while reducing model size and computational complexity;
[0010] The foreground selector is a top-down scoring modulation based on multi-scale features, which is used to determine whether the features output by the previous feature extractor belong to the foreground. This method improves the sensitivity of detection and prevents defect features from being missed.
[0011] The dual-attention encoder and decoder use a self-attention mechanism, focusing on the semantic relevance in the spatial dimension and channel dimension to improve the representation ability of features. At the same time, combined with the foreground selector, a fine-grained feature enhancement strategy is adopted during encoding to enhance the calculation of fine-grained features and improve the recognition ability of small target defect problems.
[0012] The segmentation prediction head is a feature-based classifier used to determine whether each pixel in the image is a defect feature. If so, it further determines which defect category it belongs to, which is used to improve the accuracy of defect detection. The target detector performs target box regression calculation and target category prediction on the obtained features. In particular, with the assistance of the semantic information of the segmentation head, it can pay more attention to and locate small target defect pixels, and improve the recognition accuracy of defect categories.
[0014] The steps of the metal parts surface defect detection algorithm based on mold forming are as follows:
[0015] Step 1: Process the metal part image through the feature extractor to obtain the feature map f of the input image l (i,j) ;
[0016] Read the metal parts image dataset X = {x1, x2, ..., x i ,...x n} in image x i When the image size is uniformly set to 1024*1024, a convolution layer with a convolution kernel of 1*1 and a global maximum pooling layer are used; the preprocessed image is input into the network module of the backbone network ResNet101-DCN for feature extraction to obtain the feature map of each image; in this process, the convolution layer and the pooling layer work together to enable the network to extract the key features of the image at multiple scales, thereby providing an accurate basis for subsequent analysis and processing. This design method makes full use of the advantages of deep learning technology in image processing, making the recognition and analysis of metal parts images more accurate and efficient.
[0017] Step 2: The foreground selector performs foreground discrimination on the extracted features
[0018] Step 2.1: Map the feature layer position (i, j) of each scale of the feature map output in step 1 to (x, y) in the original image, and then calculate the scale of the target predicted by the feature map based on whether the chessboard distance between the mapped position and the center coordinate of the defect label is within the scale of the target predicted by the feature map. Within the range of , the corresponding feature map is labeled, and the label is 1 within the prediction range, otherwise it is 0. In particular, this method is different from the traditional label assignment in that the predicted defect scale allows the overlap of two feature scales (i.e. ), this method helps to find multiple overlapping defect problems. Step 2.2: For the feature map output in step 1, use high-level semantic features to modulate the low-level feature prediction results. This can avoid the loss of defect feature information and provide useful information even when there is a lack of training data, making the prediction results more adaptive. First, use a multi-layer perceptron (MLP) to predict the deep feature map f lThe category semantic score matrix S in l , the category semantic score matrix is a two-dimensional matrix whose number of rows and columns corresponds to the size of the feature map, and each element represents the score of the defect category of the pixel at the corresponding position. This matrix is then used with the low-level feature map f l-1 After multiplication, it is combined with the original feature map f l-1 The new low-level feature map is obtained by adding them together. This helps the model better understand the defect targets in the image and provide more accurate feature representation. Finally, the new feature map after addition is used as the input of the multi-layer perceptron to predict the category semantic score matrix S on the low-level feature map. l-1 The formula is as follows:
[0019] S l-1 =MLP(f l-1 (1+UP(a l *S l ));
[0020] Step 3: Encode the feature map processed by step 2. This module mainly consists of multi-head attention, normalization, residual connection and forward feedback network.
[0021] Step 3.1: Fine-grained feature enhancement strategy, which improves the recognition ability of small target defect problems by highlighting the subtle structure and texture information in the image and reducing the influence of irrelevant or interfering features. The foreground score S calculated by the multi-layer perceptron in step 2 is used j and category score C j Calculate the fine-grained features that the attention module needs to focus on in this layer of features. The calculation formula is:
[0022] P j =S j ×C j ;
[0023] Step 3.2: Compress each feature map into tokens. In order to preserve the order information in the sequence, add the input token and the position code as the input of the multi-head attention. Self-attention includes normalization of the input sequence, residual connection and layer normalization. The sum of the fine-grained features and position codes calculated in step 3.1 is used as the Q and K values of the multi-head attention, and the feature prediction map processed by the multi-layer perceptron in step 2.2 is used as the V of the multi-head attention. Through multiple self-attention heads, the model can more accurately distinguish the semantic features of different defect categories and better learn the dependencies between different positions. Finally, the output of the self-attention is nonlinearly activated, which includes two fully connected layers and an activation function to independently map and transform the features of each position. Repeat the process of 3.2 6 times, and the output of the last layer of encoder is used as the final output. Through repeated encoding operations, the model can better establish a multi-level representation of the input sequence. This method can help the model understand more distant semantic relationships even if the defect data samples are unevenly distributed, thereby improving the performance and generalization ability of the model.
[0024] Step 3.3: Use the encoder output of step 3.1 as the input of the decoder. Unlike the traditional transformer decoder, the encoder can process all encoder outputs in parallel instead of one by one. First, add the input embedding to the position encoding. Then let the added input vector perform attention calculation with itself. Then, the output of the self-attention layer is residually connected to the input embedding vector to avoid the loss of defect features. Then perform layer normalization to stabilize model training.
[0025] In the feedforward network, the feature vector at each position is processed by two linear transformations and an activation function (ReLU is used here). Finally, the output of the feedforward neural network layer is residually connected to the output of the self-attention layer, and then layer normalization is performed. Repeat step 3.3 for 6 times as the output of the decoder. Through repeated decoder layer operations, the dependencies of various features in the input sequence can be better captured, thereby improving the accuracy of defect category prediction in the generated sequence.
[0026] Step 4: Training of segmentation head and object identifier. The segmentation head and object identifier receive the output of the decoder at the same time.
[0027] Step 4.1: The segmentation head contains five convolutional layers for feature extraction and spatial information integration. After that, the activation function (ReLU) is used to increase the nonlinearity of the feature space. Finally, there are two branch paths. One output is restored to the same size as the input image through an upsampling layer (using bilinear interpolation). The other path is through a convolutional layer for classification prediction. The number of its output channels is equal to the number of target categories, and the softmax function is used to generate the probability distribution of each pixel position belonging to each category.
[0028] Step 4.2: Receive the output of the decoder in step 4.1 and step 3, and first perform global average pooling to compress the spatial dimension of each channel of the output sequence into a single value to extract the global feature representation. The extracted global features are then input into the fully connected layer to extract the correlation and high-level semantic information between the features, and convert the global feature vector into a representation suitable for defect classification and defect location regression. The feature data then needs to pass through two fully connected layers, and continue to pass through the first fully connected layer (classification layer). The softmax function is also used to classify the target defect category, and the output feature belongs to the probability of each defect problem. After the last fully connected layer (regression layer), it is used to predict the bounding box (position) of the defect target. It will output information such as the location coordinates, width, and height of the target.
[0029] The beneficial effects of the present invention are as follows: the target detection method not only performs well in small target defect detection, but can also be applied to other target detection fields, such as autonomous driving, video surveillance, medical imaging, etc. Compared with traditional target detection methods, the present method has higher accuracy and faster calculation speed, and can better meet the needs of practical applications.
[0030] The method of the present application adopts a variety of advanced technologies, including dual attention encoder and decoder, feature extractor and foreground selector. Among them, the dual attention encoder and decoder are the core parts of the present method, which can effectively encode and decode the image defect features and significantly improve the calculation speed while ensuring the recognition accuracy. The feature extractor can extract high-quality features in the image at multiple scales, which is an important basis for subsequent defect detection. The foreground selector further eliminates the influence of background features and other interferences, allowing the model to effectively focus on the defect target, thereby improving the accuracy and efficiency of target detection.
[0031] This target detection method also has the following advantages:
[0032] 1. High accuracy: The use of dual attention encoder and decoder makes the model highly sensitive to defect features, and even small target defects can be easily identified, thereby improving the accuracy of defect detection.
[0033] 2. Fast calculation speed: By integrating multiple technologies, feature processing can be performed in parallel, thereby significantly improving the calculation speed while ensuring recognition accuracy.
[0034] 3. Applicable to a variety of fields: Not only suitable for small target defect detection, but also can be applied to other target detection fields, such as autonomous driving, video surveillance, medical imaging, etc.
[0035] This target detection method has broad application prospects in practical applications and can provide efficient and accurate target detection services for various fields. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] Figure 1 It is the overall flow chart of the present invention;
[0037] Figure 2 Identify module structure diagrams for invention prospects;
[0038] Figure 3 This is a structural diagram of the general module for inventing encoding and decoding. DETAILED DESCRIPTION
[0039] Example 1
[0040] The following will combine the accompanying drawings and preferred implementation examples to explain the specific implementation, structure, features and efficacy of a metal part surface defect detection algorithm based on die forming proposed by the present invention in detail and with specific steps as follows:
[0041] Step 1: Get the feature map of the input image
[0042] Read the metal parts image dataset X = {x1, x2, ..., x i ,...x n} in image x i When the image size is uniformly reset to 1024*1024, a convolution layer with a convolution kernel of 1*1 and a global maximum pooling layer are used. Then the preprocessed image is input into the network module with the backbone network of ResNet101-DCN for feature extraction, and finally a feature map with 2048 channels is obtained for each image.
[0043] Step 2: Perform foreground identification. See the schematic diagram for details. Figure 2
[0044] Use the feature map output from step 1 as input and input it into a multi-layer perceptron to calculate the category score C f , and take the maximum output category score as the final category score S c We use high-level semantic information to guide the prediction of low-level features. So the final output of low-level semantics is S l-1 :
[0045] S l-1 =MLP(f l-1 (1+UP(a l *S l ))
[0046] Step 3: Select the target token and perform encoding and decoding. First, calculate the token that can be used for enhanced calculation based on the MLP output, that is, fine-grained enhanced calculation. The calculation formula is:
[0047] S p =S c ·S l
[0048] Then according to the calculated S p value, select several relatively large S p The feature maps corresponding to the values and their positions are used as the pre-information for subsequent fine-grained enhancement. The loss function of the foreground discrimination module uses FocalLoss, which is calculated as follows:
[0049] L f =-a f (1-P f ) r log(P f )
[0050] P f Represents the foreground probability calculated by the multilayer perceptron in this step. a f and r are hyperparameters, and their empirical values are generally 0.25 and 2 respectively.
[0051] Then the encoding operation is performed. The feature map of step 1 is accepted and compressed into a sequence signal (Token), and the position code is added as the input of the encoder. p The target sequence and positional information are obtained by using the value and position information. The sum of the target token and the positional code is used as the Q and K values in the multi-head attention code, and the individual feature token is used as the V value. Then Q, K, and V are used for attention calculation, and the fully connected layers and activation functions are interspersed to perform nonlinear processing on the obtained attention features. The above encoding process is repeated 6 times, and the self-attention calculation of the last layer uses the output as the total output of the encoder. Finally, decoding is performed. The improved decoder can process all the outputs of the encoder in parallel compared to the traditional transformer decoder. Similar to the encoder calculation, the input embedding is first added to the positional code. Then the added input vector is used to perform attention calculation with itself. After that, the output of the self-attention layer is residually connected to the input embedding vector, followed by layer normalization and nonlinear activation. The above operation is also repeated 6 times, and the output of the last decoding is used as the output of the entire decoder.
[0052] Step 4 trains the segmentation head and object detector. The segmentation head contains five convolutional layers and an activation function (ReLU). Finally, the feature map is restored to the same size as the input image through an upsampling layer (using bilinear interpolation). At the same time, the features that have passed the activation function will pass through a convolutional layer and a softmax function to output the predicted label for each pixel. We use Dice loss in the segmentation head training, and the specific calculation formula is:
[0053]
[0054] |X| and |Y| represent the true mask label and predicted value of the segmentation. In the above formula, |X∩Y| represents the intersection of the true value and the predicted value, and |X| and |Y| represent the number of elements.
[0055] The object detector consists of a global average pooling layer and three fully connected layers. The global average pooling layer is used to compress the spatial dimension of each channel of the feature map into a single value to extract a global feature representation. The first fully connected layer extracts the correlation and high-level semantic information between features, and converts the global feature vector into a representation suitable for object classification and position regression. Then the feature data needs to pass through two fully connected layers, and continue to pass through the first fully connected layer (classification layer). The softmax function is also used to classify the target category and output the probability that the feature belongs to each category. After the last fully connected layer (regression layer), it is used to predict the bounding box (position) of the target. It can output information such as the location coordinates, width, and height of the target. We use the Hungarian matching algorithm to filter the data whose predicted category is closest to the true value of the predicted box. The loss function is as follows:
[0056]
[0057] The above shows and describes the basic principles and main features of the present invention and the advantages of the present invention. The various components mentioned in the present invention are common technologies in the existing field. Those skilled in the art should understand that the present invention is not limited to the above embodiments. The above embodiments and descriptions are only to illustrate the principles of the present invention. Without departing from the spirit and scope of the present invention, the present invention may have various changes and improvements, which fall within the scope of the present invention to be protected. The scope of protection claimed by the present invention is defined by the attached claims and their equivalents.
Claims
1. A metal part surface defect detection algorithm based on die forming, which is implemented by a detection system, the detection system consists of a feature extractor, a foreground selector, a dual attention encoder and decoder, a segmentation prediction head and an object detector; The detection algorithm is as follows: Step 1: Process the metal part image through the feature extractor to obtain the feature map f of the input image l (i,j) ; Step 2: The foreground selector performs foreground discrimination on the extracted features to improve the accuracy of subsequent defect target recognition; Step 3: Encode the feature map processed in step 2. This module mainly consists of multi-head attention, normalization, residual link and forward feedback network; Step 4: Training of the segmentation head and the target identifier. The segmentation head and the target identifier receive the output of the decoder at the same time.
2. A metal part surface defect detection algorithm based on die forming according to claim 1, characterized in that The specific method of step 1 is: Read the metal part image dataset X = {x1, x2, ..., x i ,…x n } in image x i When the image size is uniformly set to 1024*1024, a convolution layer with a convolution kernel of 1*1 and a global maximum pooling layer are used. The preprocessed image is input into the backbone network ResNet101-DCN for feature extraction to obtain the feature map of each image.
3. A metal part surface defect detection algorithm based on mold forming according to claim 1, characterized in that The specific method of step 2 is: Map the feature layer position (i, j) of each scale in the feature map output in step 1 to the original image (x, y), and then calculate the distance between the mapped position and the center coordinate of the defect label based on whether it is at the scale of the target predicted by the feature map. Within the range of , label the corresponding feature map, the label is 1 within the prediction range, otherwise it is 0; For the feature map output in step 1, high-level semantic features are used to modulate the low-level feature prediction results to avoid the loss of defect feature information, which can make the prediction results more comprehensive. First, a multi-layer perceptron (MLP) is used to predict the deep feature map f l The category semantic score matrix S in l , which contains the confidence that each pixel belongs to a certain defect category or is defect-free. This matrix is then used with the low-level feature map f l-1 After multiplication, it is combined with the original feature map f l-1 Add together to get a new low-level feature map; Finally, the added new feature map is used as the input of the multi-layer perceptron to predict the category semantic score matrix S on the low-level feature map l-1 , the formula is as follows: S l-1 =MLP(f l-1 (1+UP(a l *S l ))。 4. A metal part surface defect detection algorithm based on die forming according to claim 3, characterized in that The specific method of step 3 is: Step 3.1: Fine-grained feature enhancement strategy can avoid the problem of small target defect target loss while providing a more explanatory expression of defect feature recognition and optimizing the recognition decision of the model. The specific implementation is to use the foreground score S calculated by the multi-layer perceptron in step 2 j and category score C j Calculate the fine-grained features in this layer of features that the attention module needs to focus on; The calculation formula is: P j =S j ×C j ; Step 3.2: Compress each feature map into tokens. In order to preserve the order information in the sequence, add the input token and the position code as the input of the multi-head attention. The self-attention module includes the steps of normalizing the input sequence, residual connection and layer normalization. In step 3.1, the sum of the calculated fine-grained features and position encoding is used as the Q and K values of the multi-head attention. In step 2, the feature prediction map processed by the multi-layer perceptron is used as the V of the multi-head attention. Through multiple self-attention heads, the model can more accurately distinguish the semantic features of different defect categories and better learn the dependencies between different positions. Finally, the output of the self-attention is nonlinearly activated. This process includes two fully connected layers and an activation function to independently map and transform the features of each position. Repeat the process in 3.2 6 times, and the output of the last layer of encoder is the final output. Step 3.3: Use the encoder output in step 3.2 as the input of the decoder. Unlike the traditional transformer decoder, our encoder can process all encoder outputs in parallel instead of one by one. First, add the input embedding to the positional encoding, then let the added input vector perform attention calculation with itself, and then perform residual connection between the output of the self-attention layer and the input embedding vector to avoid the loss of defect information. Then perform layer normalization to stabilize model training. In the feedforward network, the feature vector at each position is processed by two linear transformations and an activation function (ReLU is used here); finally, the output of the feedforward neural network layer is residually connected with the output of the self-attention layer, and then the layer is normalized, and step 3.3 is repeated 6 times as the output of the decoder. At this time, the output information mainly includes the possible defect categories of the metal parts in the image and the corresponding probability distribution.
5. The metal part surface defect detection algorithm based on mold forming according to claim 1 is characterized in that The specific method of step 4 is: Step 4.1: The segmentation head contains five convolutional layers for feature extraction and spatial information integration. After that, the activation function (ReLU) is used to increase the nonlinearity of the feature space. Finally, there are two branch paths. One output is restored to the same size as the input image through an upsampling layer (using bilinear interpolation), and the other path is through a convolutional layer for classification prediction. The number of output channels is equal to the number of target categories. The softmax function is used to generate the probability distribution of each pixel position belonging to each category. Step 4.2: Receive the output of the decoder in step 4.1 and step 3, first perform global average pooling to compress the spatial dimension of each channel of the feature map into a single value to extract the global feature representation, then input the extracted global features into the fully connected layer to extract the correlation and high-level semantic information between the features, and convert the global feature vector into a representation suitable for target classification and position regression. Then the feature data needs to pass through two fully connected layers, and continue to pass through the first fully connected layer (classification layer). The softmax function is also used to classify the target category, and the output feature belongs to each category. The probability; after the last fully connected layer (regression layer), it is used to predict the bounding box (position) of the target, which can output the target's position coordinates, width, height and other information.
6. A metal part surface defect detection algorithm based on die forming according to claim 1, characterized in that The feature extractor is a feature extraction framework based on CNN, which uses deep separable convolution to reduce the number of parameters and the amount of calculation, thereby reducing the model size and calculation complexity while being able to extract high-quality features; The foreground selector is a top-down scoring modulation based on multi-scale features, which is used to determine whether the features output by the previous feature extractor belong to the foreground, so as to avoid the problem of missed defect detection; The dual-attention encoder and decoder use a self-attention mechanism, focusing on the semantic relevance in the spatial dimension and channel dimension to improve the representation ability of defect features. At the same time, combined with the foreground selector, a fine-grained feature enhancement strategy is adopted during encoding to enhance the calculation of fine-grained features and improve the recognition ability of small target defect problems.
Citation Information
Cited By
Wood surface defect detection method, system, medium and equipment
CN119205758A
