Rectangular magnetic material defect cascade detection and sorting method based on symmetric structure feature enhancement and double-order Transform

By constructing a detection method based on symmetric structural feature enhancement and a two-order Transformer, and combining various preprocessing and network models, the accuracy and adaptability issues of edge defect detection in rectangular magnetic materials are solved, achieving efficient and accurate automated detection.

CN121459129APending Publication Date: 2026-02-03LIAONING UNIVERSITY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511567820.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-10-30
Publication Date
2026-02-03

AI Technical Summary

Technical Problem

Existing technologies for detecting edge defects in rectangular magnetic materials suffer from high subjectivity, low efficiency, limited adaptability to various scenarios, difficulty in effectively capturing global edge information, and lack of automatic and adaptive edge defect discrimination and spatial positioning capabilities for materials in changing process scenarios.

Method used

A detection method based on symmetric structure feature enhancement and two-order Transformer is adopted. It combines Canny edge extraction and correction, dynamic threshold binarization, symmetric defect feature enhancement, channel self-attention mechanism, Transformer encoding and interpretable region attention heatmap generator to construct a two-order Transformer network model, and achieves high-precision automatic detection through cascaded deployment.

Benefits of technology

It improves the accuracy and interpretability of edge defect detection in rectangular magnetic materials, reduces the probability of human error, adapts to complex industrial environments, and achieves rapid and efficient automated detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121459129A_ABST
    Figure CN121459129A_ABST
Patent Text Reader

Abstract

The invention discloses a rectangular magnetic material defect cascade detection and sorting method based on symmetric structure feature enhancement and double-order Transform, and belongs to the technical field of image recognition. The method comprises the following steps: 1, constructing a rectangular magnetic material edge defect image data set; 2, preprocessing the data by using a Canny edge extraction and correction module and a dynamic threshold binarization processing module; 3, constructing a double-order Transform self-attention network model suitable for automatic detection of edge defects of the rectangular magnetic material; 4, training a double-order Transform self-attention network model under different resolutions, and realizing the material edge defect detection capability under images with different resolutions; and 5, constructing a cascade detection system to realize automatic detection and sorting of edge defects of the rectangular magnetic material in an industrial production environment. According to the method, the detection time is shortened, the man-made subjectivity is reduced, and the sorting accuracy can be kept in the industrial production environment with the complex light source background.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application relates to a rectangular magnetic material defect cascade detection and sorting method based on symmetric structure feature enhancement and a double-stage Transformer. BACKGROUND

[0002] With the development of industrial manufacturing and material science, magnetic materials are increasingly used in new energy, automation, transportation and other fields. Due to production processes and environmental factors, rectangular magnetic materials are prone to produce holes, cracks, notches, edge wear and other various edge defects in the process of cutting, transportation and the like. These defects not only affect the performance and safety of use, but also affect the downstream system integration and quality traceability. Traditional edge defect detection methods mainly rely on manual inspection and conventional machine vision, which have the problems of strong subjectivity, low efficiency and limited adaptability to scenes.

[0003] In recent years, deep learning methods have made great breakthroughs in image detection. Convolutional neural networks (CNN) based on small convolution kernels perform well in feature extraction, but for rectangular magnetic materials with regular shapes and edge features, the edge defects usually present local and weak spatial signals, which limits the feature extraction to local receptive fields and makes it difficult to effectively capture global edge information. In addition, the positioning and differentiation of defect boundaries are insufficient, and are easily affected by external factors such as light, contrast and rotation. Transformer models have attracted attention due to their superior global modeling capabilities, but when directly applied to edge defect detection, they often face precision and robustness challenges due to sparse feature expression and inaccurate edge positioning.

[0004] In addition, existing methods pay insufficient attention to the edge feature extraction of input samples, and complex background and noise interference significantly affect the stability of detection. Moreover, there is a lack of effective modeling of material geometric symmetry, making it difficult to achieve automatic and adaptive edge defect discrimination and spatial positioning of materials in variable process scenarios. Therefore, an intelligent detection method that takes into account edge structure, global context and symmetric structure information is needed to improve the accuracy, explainability and real-world adaptability of defect discrimination. SUMMARY

[0005] In order to overcome the defects of the prior art in the automatic detection of the edge defects of the rectangular magnetic material, the application provides a rectangular magnetic material defect cascade detection and sorting method based on symmetric structure feature enhancement and double-stage Transformer.

[0006] The technical scheme adopted by the application is as follows: a rectangular magnetic material defect cascade detection and sorting method based on symmetric structure feature enhancement and double-stage Transformer, which comprises the following steps: S1. Collect diversified rectangular magnetic material image samples, and label whether there are edge defects in each picture to construct a data set for edge defect detection; The collected images cover multiple rectangular magnetic material images under different angles and different brightness conditions, the diversified rectangular magnetic material images are labeled with image sample labels, the reliability of the labeled labels is checked manually, and the images are divided into a training set, a verification set and a test set.

[0007] S21. The Canny edge extraction and correction module is used to extract the material body edge in the image, fit the edge into a quadrilateral, and obtain the mapping rule of distortion correction; S211. The Canny edge algorithm is used to extract the significant edge to generate a binary edge image, and the detected multiple contour points are combined; S212. The edge main body is screened out, the minimum circumscribed quadrilateral of the edge main body is calculated as the inclined frame of the material body boundary, the four vertices of the quadrilateral are obtained, and the distances from the four vertices to the upper right corner of the image are calculated, the nearest point is selected as the upper left corner point of the quadrilateral, the other three vertices of the upper right, the lower right and the lower left are determined in the clockwise direction, and the vertex coordinates of the target rectangle are determined according to the three points of the upper left corner, wherein the target rectangle should be located in the center of the image, and the length and width are parallel to the image edge; S213. The perspective transformation matrix is calculated according to the sorted source quadrilateral vertices and the target rectangle vertices. The matrix establishes the spatial mapping relationship between the distorted image and the corrected image.

[0008] S22. The dynamic threshold binarization module is used to dynamically binarize the image according to the statistical parameters of the magnetic rectangular material image, and highlight the edge main body of the magnetic material; The corrected material image is converted into a gray image, and all non-white pixel regions are detected. Based on the non-white pixel mean, combined with the contrast and brightness distribution, an adaptive factor is constructed, and the best binary threshold is dynamically calculated. The region with a pixel value not higher than the threshold is defined as a black region, and the rest is white, so that the edge region and the material main body region are separated.

[0009] S23. The symmetry feature enhancement module is used to extract multiple symmetry features from the corrected rectangular magnetic material binary image and fuse them into multi-dimensional features.

[0010] For the corrected rectangular magnetic material binary image, the horizontal and vertical center lines of the image are selected as the spatial symmetry reference of the material, and divided into four quadrants. Then, mirror transformation is performed on each quadrant, and the operation sequence of the quadrants is as follows: keeping the original image, flipping left and right, flipping up and down, and flipping in both directions. Based on the corresponding relationship between the mirror quadrants, the average difference graph and the second statistical quantity difference graph between the quadrants are calculated. Finally, the above quadrant feature graphs and the edge defect average difference graph and the second statistical quantity difference graph are stacked into a six-channel feature graph.

[0011] S2. The data is preprocessed using the Canny edge extraction and correction module and the dynamic threshold binary processing module to construct the multi-dimensional features of the rectangular magnetic material; The image preprocessing and multi-dimensional feature construction include a Canny edge extraction and correction module, a dynamic threshold binary module, and a symmetry feature enhancement module; S3. A two-stage Transformer self-attention network model suitable for automatic detection of edge defects of rectangular magnetic materials is constructed; The two-stage Transformer self-attention network model suitable for automatic detection of edge defects of rectangular magnetic materials includes a channel attention module, a Patch embedding module, a Transformer encoder, and a detector; S31. The channel attention module is used to calculate the correlation between the channels of each pixel of the multi-dimensional feature, and assign channel weights to the multi-dimensional feature, so that the model focuses on the defect feature information; For the input five-channel stacked image features, first, extract all channel pixel values at each spatial location. After reorganizing the five-channel pixel representation at this location into a channel vector, generate query Query, key Key, and value Value matrices through linear embedding in the channel dimension. Then, calculate the product of Query and Key at each pixel spatial point and normalize it through scaling to obtain the relative difference importance distribution between channels. The normalized attention weights between channels are obtained through the Softmax function. Finally, the attention weights are used to weight and fuse the Value features to obtain the channel attention feature map.

[0012] S32. The patch embedding module is used to convert the input multi-channel feature map into an input format available to the Transformer encoder; First, the upper layer feature map is divided into square patches of the same size. After each patch is converted into a feature block by convolution, it is flattened into a one-bit sequence, and position embedding and spatial information are added. All sequences are spliced to form an input format available to the Transformer encoder.

[0013] S33. The Transformer encoder is used to calculate the global correlation between patches and generate a deep attention feature map; For the input full sequence, first, the correlation weight between all patches is calculated by the multi-head self-attention mechanism to realize the global interaction and fusion of information. Then, the interactive features are nonlinearly transformed and mapped by the feedforward neural network module to obtain a patch feature sequence that integrates global spatial context information, thereby generating an attention feature map representing the global correlation between patches.

[0014] S34. The attention heat map generator is used to show the global attention focus area and highlight the material defect position; The patch features output by the Transformer are mean-pooled in the embedding space dimension to obtain an attention score matrix for each patch. Then, the matrix is interpolated to the shape of the input image by upsampling, thereby realizing the saliency visualization of the defect area in the spatial position and highlighting the rectangular magnetic material edge defects.

[0015] S35. The detector is used to determine whether the rectangular magnetic material in the input image has defects according to the deep attention feature.

[0016] The detector uses a multi-layer perception (MLP) to nonlinearly transform the class features and obtain prediction scores for the two classes of edge defects. After Softmax normalization, the probability distribution of whether there is a defect is output, and the final defect discrimination result is given according to the maximum probability.

[0017] S4. Train the two-stage Transformer self-attention network model at different resolutions and optimize the loss function; S41. Set the input image size and patch size of the constructed two-stage Transformer self-attention network model to high and low resolution versions, respectively; S42. Input the multi-dimensional features obtained after preprocessing and symmetric feature enhancement and their corresponding defect labels into the two resolution versions of the two-stage Transformer self-attention network model described in S41, respectively, and complete forward propagation to obtain the defect classification probability output by the model. S43. Calculate the loss function between the two output results of the two models and the real defect class label respectively, and use the cross-entropy loss function to measure the classification accuracy; S44. Based on the loss function result obtained in S43, the parameters of each layer of the network are continuously optimized using the back propagation algorithm, and the two stages of S42 and S43 are continuously iterated until the model accuracy and the significant area positioning result on the validation set reach the preset convergence condition, thereby obtaining the best double-stage Transformer network model weight.

[0018] S5. Cascade detection is realized by using the double-stage Transformer self-attention network model at different resolutions.

[0019] S51. The two double-stage Transformer self-attention network models at high and low resolutions obtained by training are encapsulated and deployed into two rectangular magnetic material defect detectors respectively; S52. The two rectangular magnetic material defect detectors in S51 are installed in the industrial production line in order from low to high resolution to form a cascade detection system; S53. The rectangular magnetic material is sequentially passed through the cascade detection system, first through the low-resolution detector for rapid screening of unqualified materials, and then through the high-resolution detector for accurate detection, and the edge defect sorting of the rectangular magnetic material in the industrial environment is realized by cooperating with the sorting control instruction.

[0020] The beneficial effects of the present application are that the present application proposes a rectangular magnetic material edge defect detection method based on the Transformer algorithm, by innovatively constructing a double-stage Transformer network model, the fusion of Canny edge extraction and correction, dynamic threshold binarization, symmetric defect feature enhancement, channel self-attention mechanism, Transformer coding and interpretable region attention heat map generator, the recognition accuracy of the model is improved, the detection of the edge defects of the rectangular magnetic material is realized, and a cascade detection system is constructed by using the cascade deployment method to further guarantee the accuracy of the defect detection. By using the above method, it can be compatible with the production environment of complex industrial conditions with variable brightness background, effectively reduce the probability of human error detection, greatly reduce the detection time of single piece material, and ensure the end-to-end automatic detection ability, which has important significance for industrial material quality inspection, intelligent manufacturing and other fields. BRIEF DESCRIPTION OF DRAWINGS

[0021] Figure 1 The method flowchart of the present application.

[0022] Figure 2 The rectangular magnetic material (defect) example in the present application is shown in the figure. Figure 3 A dynamic threshold binarization result graph of the rectangular magnetic material (defect) example graph in the present application example; Figure 4 A pixel error graph of the rectangular magnetic material (defect) example graph in the present application example; Figure 5 A MSE score feature graph of the rectangular magnetic material (defect) example graph in the present application example; Figure 6 A double-stage Transformer network model structure graph in the present application example; Figure 7 A double-resolution cascade detector detection and sorting flowchart in the present application example. DETAILED DESCRIPTION

[0023] The above content of the present application will be further described in the form of examples, but this should not be understood as limiting the scope of the above subject matter of the present application to the following examples. Any form of variation and / or change made on the basis of the above content of the present application will fall within the scope of protection of the present application.

[0024] Example 1

[0025] A rectangular magnetic material defect cascade detection and sorting method based on symmetric structure feature enhancement and double-stage Transformer, characterized in that it specifically comprises the following steps: S01. Constructing a rectangular magnetic material edge defect image dataset; First, a high-definition camera is used to shoot multiple rectangular magnetic material images, including oblique shooting (±20°), low illumination (50-200 Lux), high reflectivity (mirror reflectivity >40%), etc. Each image size is 1700 900, and each type of defect sample is not less than 500. For each picture, whether it exists edge defect and defect category is labeled according to manual inspection, and each image and corresponding defect label is stored in JSON form to form a dataset for edge defect detection. Finally, the above dataset is divided into training set, validation set and test set in the ratio of 7:2:1.

[0026] S02. Preprocess the data using Canny edge extraction and correction module and dynamic threshold binarization processing module, and further enhance the symmetric feature to construct the multi-dimensional feature of the rectangular magnetic material: The data preprocessing and feature enhancement obtaining multi-dimensional feature process comprises Canny edge extraction and correction, dynamic threshold binarization and symmetric feature enhancement; the Canny edge extraction and correction module is used for extracting the material main body edge in the image, fitting the edge into a quadrilateral, and obtaining the mapping rule of distortion correction; the channel attention module is used for comparing the pixel differences between channels by using the multi-channel feature map extracted by the Canny edge enhancement module; the dynamic threshold binarization module is used for dynamically binarizing the image according to the statistical parameters of the magnetic rectangular material image, highlighting the edge main body of the magnetic material; and the symmetric feature enhancement module is used for extracting various symmetric features from the corrected rectangular magnetic material binary image and fusing them into multi-dimensional features.

[0027] The specific method is: S21. uniformly process the format and picture size of the rectangular magnetic material edge defect image data set in step S01; S22. input the image in S21 into the Canny edge extraction and correction module, extract and screen the material main body edge, calculate the minimum area circumscribed quadrilateral as the fitting outer frame, and take the vertices of the fitting outer frame to calculate the spatial mapping matrix thereof with the target rectangle for image correction.

[0028] Canny edge extraction and screening: convert the input image into a gray image , and use a Gaussian filter to suppress noise, and the specific calculation formula is:

[0029] wherein is a two-dimensional Gaussian kernel.

[0030] Finally, edge detection is performed by using a Canny operator to obtain an edge set, and the specific calculation formula is:

[0031] In order to remove false edges, connected domain analysis and area threshold filtering are adopted, and the edge set E of the largest connected region is reserved as the material main body edge.

[0032] Fitting outer frame calculation: the edge set in the edge set is fitted with the minimum circumscribed quadrilateral, and the quadrilateral vertices are calculated. The specific optimization target is:

[0033] wherein

[0034] Spatial mapping matrix calculation: The vertices of the minimum area quadrangle obtained by fitting The first three points are selected in clockwise order, the width and height of the ideal rectangle are calculated W, H , and the coordinates of the four vertices of the ideal rectangle at the center of the image are calculated. The spatial mapping matrix is calculated by perspective transformation H , and the specific calculation formula is as follows: The perspective transformation satisfies: ,

[0035] Two constraint equations are obtained by unfolding

[0036] The linear equations are obtained:

[0037] The four sets of vertices are brought in, and eight equations are obtained to form a matrix form:

[0038] Among them:

[0039] Finally, the solution is:

[0040] The complete spatial mapping matrix is obtained:

[0041] S23. The image in S21 is input into the dynamic threshold binarization module, the threshold is calculated according to the image parameters, and the material edge is obtained by binarizing the image.

[0042] Dynamic threshold calculation: First, the material image is converted into a gray scale image , the pixel set with all gray scale values less than 255 (white) in the gray scale image is extracted , and the gray scale mean value of the non-white pixel set is calculated , and the specific calculation formula is as follows:

[0043] The threshold is calculated in combination with and the standard deviation of gray scale distribution, the maximum and minimum of gray scale value, contrast and other parameters, and the specific calculation formula is as follows:

[0044] Among them is the non-white pixel factor, and the standard deviation of image gray scale , the maximum and minimum , contrast , the correlation formula is as follows:

[0045] wherein are empirical factors, the value is( .

[0046] Image binarization: Using the calculated dynamic threshold, the pixel value of the binarized image is calculated, and the specific calculation formula is as follows:

[0047] wherein is the binarized image calculated by the binarization formula.

[0048] S24. Use the spatial mapping matrix calculated in S22 H The binarized image calculated in S23 is corrected to the ideal rectangular material edge, and the specific calculation formula is as follows:

[0049] All pixels of the original material image are mapped to the corrected rectangular coordinates H , to obtain the standard rectangular material image, wherein the image blank generated due to rectangular correction is filled with white.

[0050] S25. Input the ideal rectangular edge image calculated in S24 to the symmetric feature enhancement module to extract two symmetric features and synthesize learnable multi-dimensional features.

[0051] Symmetric feature enhancement: First, the binarized image is divided into four quadrants along the horizontal and vertical midlines (from left to right, from top to bottom, respectively. Q1, Q2, Q3, Q4), then Q2, Q3, Q4 are respectively horizontally mirrored, vertically mirrored, and double-axis mirrored, and the four quadrant data are stacked into a four-channel feature tensor.

[0052] Calculate the pixel average error between the four channels for each pixel to form a pixel error map , the specific calculation formula is:

[0053] Then divide the four-channel feature tensor into small patches with a size of (50, 50), and calculate the pixel mean square error (MSE) between the four channels for each patch. The specific calculation formula is:

[0054] ​wherein, N is the number of pixels in the patch; c is the channel number in the patch; is the four-channel mean variance, and its calculation formula is:

[0055] Secondly, the obtained MSE score feature map is obtained by bilinear interpolation upsampling operation to obtain a channel difference feature map consistent with the size of the input image The specific calculation steps are: Let the pixel point The corresponding position on the patch-level MSE score feature map is: ,

[0056] Then:

[0057] wherein, , , , , , ; The interpolation parameter is set to ; Finally, the six feature maps are stacked into a six-channel feature map.

[0058] S03. Construct a two-stage Transformer network model suitable for automatic detection of edge defects of rectangular magnetic materials: The two-stage Transformer network model includes a channel attention module, a patch embedding module, a Transformer encoder, an attention heat map generator, and a detector; the channel attention module is used to calculate the inter-channel correlation of each pixel of the multi-dimensional feature, and to assign channel weights to the multi-dimensional feature, so that the model focuses on the defect feature information; the patch embedding module is used to convert the input multi-channel feature map into an input format available for the Transformer encoder; the Transformer encoder is used to calculate the global correlation between patches and generate a deep attention feature map; the attention heat map generator is used to display the global attention focus area and highlight the material defect position; the detector is used to determine whether there is a defect in the rectangular magnetic material in the input image according to the deep attention feature.

[0059] The specific method is: S31. Input the multi-dimensional feature channel into the channel attention module, which is calculated by using the multi-head attention mechanism to calculate the inter-channel weight of the same pixel. The specific steps are as follows: The feature vector corresponding to each pixel of the input feature map is extracted, the feature vector is mapped into query, key and value vectors, and the correlation score between each pixel channel is calculated. The specific calculation formula is as follows:

[0060] Then, the attention output is connected with the input in residual connection, and is fused into a channel feature map.

[0061] In addition, the channel feature map is input into the detector 1 to determine whether there is a defect. The specific calculation formula is as follows:

[0062] Where y [0,1] represents the probability of whether a defect exists, is a learnable parameter.

[0063] S32. Input the channel feature map obtained in S31 into the Patch embedding module. The feature map is divided and encoded by patch. The specific steps are as follows: First, the feature map output by S31 is converted into two resolution scales of (224, 224) and (640, 640). Then, the patch is divided in the spatial dimension according to the fixed size of 16x16. Each patch is taken as an independent input unit, and a position encoding is introduced for each patch vector to preserve spatial information. The patch feature is expanded to , wherein is the feature vector of the patch, is the position encoding of the patch.

[0064] S33. Input the Patch embedding feature obtained in S32 into the Transformer encoder to calculate the global correlation between patches to generate a deep attention feature map. The specific steps are as follows: First, the multi-head attention is calculated. For each patch vector , it is mapped into query , key and value vectors. The specific calculation process is as follows:

[0065] wherein, is a learnable parameter, d is the input dimension, is the single-head attention dimension.

[0066] The self-attention output of each head is calculated:

[0067] The multi-head attention input is spliced and the final patch attention feature is obtained through linear mapping:

[0068] wherein is a linear mapping matrix, h is the number of heads.

[0069] The patch attention feature is then input into the feedforward network and residual connection is made, and the specific calculation process is: First, the multi-head attention output is mapped through two layers of feedforward network (FFN) for nonlinear mapping:

[0070] wherein, is a weight matrix, is a bias term.

[0071] The deep attention feature map output by the overall Transformer encoder is:

[0072] wherein, LayerNorm is a normalization operation, and the specific calculation is:

[0073] S34. The deep attention feature map obtained in S33 is input into the attention heat map generator to generate a defect visualization heat map, and the specific calculation process is: First, the patch attention weight mapping is calculated, and the attention weight of each patch in the deep feature map is calculated , and the specific calculation formula is:

[0074] Then, the spatial heat map is reconstructed, and the patch attention weight mapping is mapped back to the original feature map space. The size is adjusted to the original image size through bilinear interpolation, and the specific calculation formula is:

[0075] wherein is bilinear interpolation, is a spatial coordinate, for final attention heat map.

[0076] S35. The depth attention feature generated in S34 is input into the detector to determine the rectangular magnetic material defects, and the specific calculation process is as follows: First, the depth attention feature map is globally averaged pooled, and the specific calculation formula is as follows:

[0077] Then, the is input into the detector 2 to determine the defects:

[0078] where y [0, 1] represents the probability of whether the defect exists, is a learnable parameter.

[0079] S04. In the two-stage Transformer network model established in S04, the data in the rectangular magnetic material edge defect detection data set collected in S01 is trained, and the loss function is optimized to realize the automatic detection and classification ability of the material image edge defects; S41. The detection result obtained in step S03 is used to calculate the detector loss function Loss, and the specific calculation formula is as follows:

[0080] where y is the true label, is the probability of the model predicting a positive example.

[0081] The total loss function in the training process is:

[0082] where is the loss function of different detectors; is the total loss function for training; is the loss function weight, and the value is 0.3, 0.7.

[0083] S42. The cross-entropy loss value obtained in S41 is combined with the Adam optimizer and the direction propagation automatic differentiation mechanism to optimize the whole network parameters. The training hyperparameters are set as follows: the batch size is 64, the training epoch is 100, and the initial learning rate (lr) is 10 -3 ​The learnable parameters of all Transformer encoding modules, the weights and biases of the multi-head self-attention mechanism, and all the weights and biases of the MLP classification head are iteratively optimized through the back propagation algorithm and the Adam optimizer. In addition, to improve the stability and generalization ability of the model, cosine annealing learning rate decay and weight decay strategies are applied during training to ensure efficient and stable convergence of the model. The specific decay formula is:

[0084] wherein is the minimum learning rate, set to 10 -5 ; is the current training round; is the maximum training round; is the initial learning rate; W is the set of learnable weights; is the set of weights after weight decay; is the weight decay coefficient, set to 10 -5 .

[0085] S43. Verify and optimize the model using the rectangular magnetic material edge defect image validation set obtained in step S01 to obtain the optimal configuration parameters and weights of the two-stage Transformer network model.

[0086] S44. Use the rectangular magnetic material edge defect image test set obtained in step S01 to perform the final index evaluation of the model.

[0087] S45. Based on the validation set optimization results of S43 and the test set evaluation results of S44, save the optimal weights of the two two-stage Transformer self-attention network models (excluding detector 1) for low resolution and high resolution for detector deployment.

[0088] S05. Construct a cascade detection system to realize automatic detection of rectangular magnetic material edge defects in conjunction with sorting control instructions. The specific steps are as follows: S51. Encapsulate the pre-processing tool and the high-resolution and low-resolution two two-stage Transformer self-attention network models obtained through training to form two rectangular magnetic material defect detectors; S52. Deploy the two detectors in the order from low resolution to high resolution to the industrial production line to construct a cascade detection system. The low-resolution detector is used for rapid preliminary inspection, and the high-resolution detector is used for accurate detection. S53. Continuous inference is performed on the material images collected in real time by the production line, a low-resolution detector quickly filters most unqualified materials, and a high-resolution detector accurately identifies potential defects in qualified materials, forming an automated and industrial rectangular magnetic material edge defect detection and sorting system, and achieving dual optimization of detection efficiency and detection accuracy.

[0089] The above embodiments are only the preferred embodiments of the present application, but the implementation of the present application is not limited in any implementation aspect, and any simple modification, equivalent replacement and improvement made by those skilled in the art without departing from the essence and principles of the present application are included in the protection scope of the present application.

Claims

1. A method for cascaded defect detection and sorting of rectangular magnetic materials based on symmetric structural feature enhancement and a two-order Transformer, characterized in that, The steps are as follows: S1. Collect diverse rectangular magnetic material image samples and label each image to identify edge defects, thus constructing a dataset for edge defect detection; S2. Data preprocessing was performed using the Canny edge extraction and correction module and the dynamic threshold binarization module to construct multidimensional features of rectangular magnetic materials; S3. Construct a two-order Transformer self-attention network model suitable for automatic detection of edge defects in rectangular magnetic materials; S4. Train a two-stage Transformer self-attention network model at different resolutions and optimize the loss function; S41. Set the input image size and patch size of the constructed two-stage Transformer self-attention network model to two versions, high resolution and low resolution, respectively; S42. Input the multidimensional features obtained after preprocessing and symmetric feature enhancement and their corresponding defect labels into the two resolution versions of the two-order Transformer self-attention network model described in S41, and complete the forward propagation respectively to obtain the defect classification probability output by the model. S43. Calculate the loss function between the two outputs of the two models and the true defect category label respectively, and use the cross-entropy loss function to measure the classification accuracy; S44. Based on the loss function results obtained in S43, the parameters of each layer of the network are continuously optimized using the backpropagation algorithm. The two stages of S42 and S43 are iterated until the model accuracy and salient region localization results on the validation set reach the preset convergence conditions, thereby obtaining the optimal weights of the two-order Transformer network model. S5. Implement cascaded detection using a two-stage Transformer self-attention network model at different resolutions.

2. The method for cascaded detection and sorting of defects in rectangular magnetic materials based on symmetric structural feature enhancement and a two-order Transformer, as described in claim 1, is characterized in that... In S1, the acquired images include multiple rectangular magnetic material images under different angles and brightness conditions. The acquired diverse rectangular magnetic material images are labeled with image sample labels, the reliability of the labels is manually checked, and the images are divided into training set, validation set, and test set.

3. The method for cascaded detection and sorting of defects in rectangular magnetic materials based on symmetric structural feature enhancement and a two-order Transformer, as described in claim 1, is characterized in that... The specific method in S2 is as follows: Image preprocessing and multidimensional feature construction includes: Canny edge extraction and correction module, dynamic threshold binarization module and symmetry feature enhancement module; The S21.Canny edge extraction and correction module is used to extract the edges of the main material in the image, fit the edges into quadrilaterals, and obtain the mapping rules for distortion correction. S211. Use the Canny edge algorithm to extract significant edges and generate a binary edge image, and then merge the detected contour points. S212. Filter out the edge subject, calculate the minimum circumscribed quadrilateral as the inclined border of the material subject boundary, obtain the four vertices of the quadrilateral, and calculate the distance from each of the four vertices to the upper right corner of the image. Select the nearest point as the upper left corner of the quadrilateral, and sort along the clockwise direction to determine the upper right, lower right and lower left vertices. At the same time, determine the vertex coordinates of the target rectangle based on the three upper left points. The target rectangle should be located in the center of the image and its length and width are parallel to the edge of the image. S213. Calculate the perspective transformation matrix based on the sorted vertices of the source quadrilateral and the target rectangle. This matrix establishes the spatial mapping relationship between the distorted image and the corrected image. S22. The dynamic threshold binarization module is used to dynamically binarize the image based on the statistical parameters of the magnetic rectangular material image, highlighting the main body of the magnetic material edge; The corrected material image is converted into a grayscale image, and all non-white pixel regions are detected. Based on the mean value of non-white pixels, combined with contrast and brightness distribution, an adaptive factor is constructed, and the optimal binarization threshold is dynamically calculated. Regions with pixel values ​​not higher than the threshold are defined as black regions, and the rest are white, thus separating the edge region from the main material region. S23. The Symmetry Feature Enhancement Module is used to extract various symmetry features from the corrected binary image of the rectangular magnetic material and fuse them into multidimensional features; For the corrected binary image of the rectangular magnetic material, the horizontal and vertical central axes of the image are selected as the spatial symmetry reference of the material and divided into four quadrants. Then, a mirror transformation is performed on each quadrant. The operation in quadrant order is to retain the original image, flip horizontally, flip vertically, and flip bidirectionally. Based on the correspondence between the mirrored quadrants, the average difference map and the quadratic statistical difference map between the quadrants are calculated. Finally, the feature maps of each quadrant and the average difference map and the quadratic statistical difference map of the edge defects are stacked into a six-channel feature map.

4. The method for cascaded detection and sorting of defects in rectangular magnetic materials based on symmetric structural feature enhancement and a two-order Transformer, as described in claim 1, is characterized in that... In S3, the two-order Transformer self-attention network model suitable for automatic detection of edge defects in rectangular magnetic materials includes: a channel attention module, a patch embedding module, a Transformer encoder, and a detector; S31. The channel attention module is used to calculate the inter-channel correlation of each pixel of the multidimensional feature and assign channel weights to the multidimensional feature, so that the model focuses on the defect feature information. The S32.Patch embedding module is used to convert the input multichannel feature map into an input format usable by the Transformer encoder; S33. The Transformer encoder is used to calculate the global correlation between patches and generate a deep attention feature map; S34. The attention heatmap generator is used to display the global attention focus area, highlighting the location of material defects; S35. The detector is used to determine whether there are defects in the rectangular magnetic material in the input image based on the depth attention feature.

5. The method for cascaded detection and sorting of defects in rectangular magnetic materials based on symmetric structural feature enhancement and a two-order Transformer, as described in claim 4, is characterized in that... The specific method of S31 is as follows: For the input five-channel stacked image features, firstly, the pixel values ​​of all channels are extracted at each spatial location. After reorganizing the five-channel pixel representation at that location into a channel vector, the query, key, and value matrices are generated respectively through linear embedding along the channel dimension. Then, the product of query and key between channels at each pixel spatial point is calculated and scaled and normalized to obtain the relative difference importance distribution between channels. The normalized attention weights between channels are obtained through the Softmax function. Finally, the attention weights are used to perform weighted fusion of the value features to obtain the channel attention feature map.

6. The method for cascaded detection and sorting of defects in rectangular magnetic materials based on symmetric structural feature enhancement and a two-order Transformer, as described in claim 4, is characterized in that... The specific method of S32 is as follows: First, the upper-layer feature map is divided into square patches of the same size. Each patch is converted into a feature block through convolution, then flattened into a one-bit sequence, and position embedding and spatial information are added. All sequences are then concatenated to form an input format usable by the Transformer encoder.

7. The method for cascaded detection and sorting of defects in rectangular magnetic materials based on symmetric structural feature enhancement and a two-order Transformer, as described in claim 4, is characterized in that... The specific method of S33 is as follows: For the entire input sequence, the correlation weights between all patches are first calculated through a multi-head self-attention mechanism to achieve global interaction and fusion of information. Then, the feedforward neural network module performs nonlinear transformation and mapping on the interactive features to obtain a patch feature sequence that integrates global spatial context information, thereby generating an attention feature map that represents the global correlation between patches.

8. The method for cascaded detection and sorting of defects in rectangular magnetic materials based on symmetric structural feature enhancement and a two-order Transformer, as described in claim 4, is characterized in that... The specific method of S34 is as follows: The patch features output by the Transformer are averaged and pooled in the embedding space dimension to obtain the attention score matrix of each patch. Then, the matrix is ​​upsampled to match the shape of the input image, thereby visualizing the spatial saliency of the defect region and highlighting the edge defects of rectangular magnetic materials.

9. The method for cascaded detection and sorting of defects in rectangular magnetic materials based on symmetric structural feature enhancement and a two-order Transformer, as described in claim 4, is characterized in that... The specific method of S35 is as follows: The detector uses a multilayer perceptron (MLP) to perform a nonlinear transformation on the class features to obtain prediction scores for whether the edge is a defect or not. After Softmax normalization, it outputs the probability distribution of whether it is a defect or not, and gives the final defect discrimination result based on the maximum probability.

10. The method for cascaded detection and sorting of defects in rectangular magnetic materials based on symmetric structural feature enhancement and a two-order Transformer, as described in claim 1, is characterized in that... The specific steps in S5 are as follows: S51. Encapsulate the two bi-stage Transformer self-attention network models obtained from training at high and low resolutions, and deploy them into two rectangular magnetic material defect detectors respectively; S52. Install the two rectangular magnetic material defect detectors from S51 into the industrial production line in order from low to high resolution to form a cascaded detection system; S53. Rectangular magnetic materials are sequentially passed through a cascaded detection system. First, a low-resolution detector is used for rapid screening of materials that fail the initial inspection. Then, a high-resolution detector is used for precise detection. Combined with sorting control instructions, edge defect sorting of rectangular magnetic materials in an industrial environment is achieved.