Small sample metal surface defect identification model based on metric element learning

By optimizing the multi-scale attention twin relation network and loss function, the problem of insufficient generalization ability of traditional deep learning in metal surface defect recognition is solved. It can effectively distinguish highly similar defects and quickly adapt to new defect types, thereby improving recognition accuracy and generalization ability.

CN121095191APending Publication Date: 2025-12-09CHINA UNIV OF PETROLEUM (EAST CHINA)
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511220737.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-08-29
Publication Date
2025-12-09

AI Technical Summary

Technical Problem

Traditional deep learning methods suffer from insufficient generalization ability in metal surface defect identification due to scarce samples, complex and variable features, and difficulty in adapting to dynamically added defect categories. Furthermore, they are unable to effectively distinguish highly similar defects.

Method used

A few-sample metal surface defect recognition model based on metric learning is adopted, which combines multi-scale feature enhancement and attention mechanism. Feature extraction and loss function optimization are performed through Multi-Scale Attention Siamese Relationship Network (MASRN) to improve the model's ability to distinguish highly similar defects.

Benefits of technology

It can quickly adapt to new defect types under small sample conditions, significantly improve the accuracy and generalization ability of metal surface defect identification, and effectively overcome the challenges of fine-grained classification.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121095191A_ABST
    Figure CN121095191A_ABST
Patent Text Reader

Abstract

The invention belongs to the technical field of metal surface defect identification, and particularly relates to a small sample metal surface defect identification model based on metric element learning. The name of the model is a multi-scale attention twinning relationship network; the multi-scale attention twinning relation network comprises a multi-scale attention feature extraction module and a loss function module, the multi-scale attention feature extraction module combines multi-scale feature fusion and a channel attention mechanism, and the multi-scale attention twinning relation network is obtained by introducing the multi-scale feature fusion. The accuracy of metal surface defect classification is improved by utilizing local information containing rich fine-grained feature information, and insufficient feature characterization caused by small samples is effectively relieved; a channel attention mechanism is introduced to serve as a compensation module to further optimize multi-scale fusion features, the feature representation capability of the model is enhanced, and the model can learn more useful information from important channels.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application belongs to the technical field of metal surface defect recognition, and particularly relates to a small sample metal surface defect recognition model based on metric meta learning. BACKGROUND

[0002] Metal surface defect recognition is crucial in industrial manufacturing, but due to the scarcity of defect samples and the complexity of defect features, traditional deep learning-based metal surface defect recognition methods are difficult to achieve satisfactory results. In addition, traditional deep learning methods often need to be retrained when facing new tasks, which makes them unsuitable for dynamic metal surface defect recognition scenarios. These problems highlight the application potential of small sample recognition methods based on meta learning in solving surface defect recognition problems. Meta learning aims to enable the model to learn effective generalization with only a small amount of labeled data. The metric-based meta learning method is an important branch of meta learning, and its core idea is to learn a metric to enable the model to achieve effective small sample learning classification on new tasks.

[0003] In metal surface defect classification, the morphology, size and texture of defects in the same category may show diversity. For example, even if it is the same type of scratch, its length, width and depth may be different, resulting in a big difference in appearance. At the same time, different categories of metal surface defects may look very similar. For example, a small scratch and a slight indentation may look difficult to distinguish. These characteristics make metal surface defect classification a highly fine-grained small sample classification problem. In this case, relying only on basic feature extraction may not be enough to capture sufficiently complex features and patterns, limiting the model's discrimination ability. SUMMARY

[0004] The application proposes a small sample metal surface defect recognition model based on metric meta learning, which solves the problem of insufficient generalization ability of traditional deep learning methods in metal surface defect recognition due to sample scarcity, complex and variable features, and difficulty in adapting to dynamically added defect categories. By introducing a meta learning framework, the model can quickly adapt to new defect types with a small amount of sample support, while combining multi-scale feature enhancement and attention mechanisms to significantly improve the discrimination ability of high similarity defects, effectively overcoming the fine-grained classification challenge in metal surface defect recognition.

[0005] The technical solution of the application is as follows: The small sample metal surface defect recognition model based on metric meta learning is named multi-scale attention twin relationship network, which includes a multi-scale attention feature extraction module and a loss function module. The multi-scale attention feature extraction module combines multi-scale feature fusion and channel attention mechanism. By introducing multi-scale feature fusion, the local information containing rich fine-grained feature information is used to improve the accuracy of metal surface defect classification and effectively alleviate the feature representation deficiency caused by small samples. By introducing the channel attention mechanism as a compensation module, the multi-scale fusion features are further optimized, the ability of model feature representation is enhanced, and the model can learn more useful information from important channels. The loss function module includes a mean square error similarity loss and a distance-based loss function. The small sample metal surface defect classification problem can be regarded as a regression problem. The goal of the model is to predict the similarity scores between samples through the similarity measurement module. The model parameters are updated using the mean square error similarity loss. The square of the difference between the model predicted value and the true value is used to measure the performance of the model. The distance-based loss function can directly act on the feature level, guiding the model to arrange the samples in a more structured way in the feature space. This structured feature representation helps the model better understand the relationship between samples in complex metal surface defect classification tasks and enhances the discriminative ability of the model.

[0006] Optionally, the multi-scale feature fusion includes parallel multi-scale feature fusion and cascaded multi-scale feature fusion. The parallel multi-scale feature fusion extracts features of different scales through multiple branches or multiple convolution blocks constructed, and fuses them at a certain stage through addition, splicing or other operations to obtain more rich information. The cascaded multi-scale feature fusion cascades features of different scales layer by layer to form a multi-layer feature pyramid to capture more complex semantic information.

[0007] Optionally, the cascaded multi-scale feature fusion uses ResNet-12 as the backbone network and connects four convolution blocks. After each convolution block, a set of double-channel, half-resolution feature maps is generated.

[0008] Through the above technical solutions, a hierarchical multi-scale feature pyramid is constructed to systematically capture rich features from fine-grained local details to high-level semantic information. The model can simultaneously perceive the local fine texture of the metal surface defect (such as short scratches, point pits) and the global morphological structure (such as long cracks, large area patches), providing a sufficient and diverse feature basis for subsequent multi-scale feature fusion and attention optimization.

[0009] Optionally, the cascaded multi-scale feature fusion uses c to represent the number of channels, h to represent the height, and w to represent the width. The size of the feature map output by the first convolution block is , and the number of channels remains unchanged. The size of the feature map output by the second convolution block is , the number of channels doubles, and the height and width are halved; the size of the output feature map of the third convolutional block is , which further increases the number of channels and again reduces the height and width to 1 / 4 of the original size, to extract higher-level features; at the fourth convolutional block, the size of the output feature map is , the number of channels doubles again, and the height and width are each reduced to 1 / 8 of the original size, which represents the deepest feature extraction and contains high-level abstract representations of the input data; then, reverse fusion is performed, starting from the fourth convolutional block, the small-size feature map will be upsampled twice, connected with the feature map of the third convolutional block, to form high-resolution features; then, it is upsampled again twice, connected with the feature map of the second convolutional block, to form higher-resolution features; then, it is upsampled again twice, connected with the feature map of the first convolutional block, to form a cascaded multi-scale fusion feature for subsequent tasks.

[0010] Through the above technical solution, a deep fusion multi-scale feature pyramid is constructed to achieve comprehensive and multi-granularity representation of metal surface defects from microscopic details to macro semantic, and ultimately improve the classification accuracy in small sample scenarios. Through the top-down forward propagation (convolutional block), different scale features (from details to abstraction) are gradually extracted and condensed; then, through the bottom-up reverse fusion (upsampling and connection), high-level semantic information and low-level precise positioning information are deeply fused, effectively solving the recognition challenges brought by the diversity of metal defect morphology and size and the high similarity between different defects. The final cascaded multi-scale fusion feature , contains deep class semantic information and shallow fine-grained texture details, providing an extremely information-rich feature representation for subsequent relationship calculation, fundamentally alleviating the problem of insufficient feature representation under small sample conditions.

[0011] Optionally, in the channel attention mechanism, the multi-scale fusion feature is subjected to average pooling and maximum pooling operations to obtain two spatial context descriptors: and ; then, and are input into a shared multi-layer perceptron network, and then the two features output by the multi-layer perceptron network are added and passed through a sigmoid activation function to obtain a channel attention map Finally, the channel attention map is used to weight the multi-scale fusion feature to obtain a new, attention-adjusted weighted multi-scale fusion feature .

[0012] By the technical solution, the core purpose of the operation is to adaptively calibrate and strengthen the channel dimension response of the multi-scale fusion feature , so that the model can automatically focus on the channel features most critical to the current metal surface defect identification task, thereby significantly improving the discriminability of the features. By simultaneously performing average pooling and maximum pooling on each channel of the feature map, global background information and significant local features can be captured respectively, thereby more comprehensively representing the spatial context information. The shared MLP and Sigmoid function finally generate a weight vector (channel attention map ) for accurately quantifying the importance of each feature channel. The multi-scale features after fusion are "reprocessed" to suppress the responses of channels irrelevant to the current defect category, redundant or noisy, while amplifying the signals of key channels containing discriminative details such as specific textures, edges, and patches. This is crucial for distinguishing different categories of defects that are visually highly similar (e.g., scratches vs. pits).

[0013] Optionally, the loss function module includes a similarity loss based on mean square error and a distance-based loss function; the similarity loss based on mean square error is used to optimize the output of the similarity measurement module, so that the similarity score of the positive sample pair is close to 1, while the similarity score of the negative sample pair is close to 0, improving the accuracy of the model when judging the similarity of the sample pair; the distance-based loss function is used to optimize the feature space, so that the model can learn to distinguish the feature representation of different categories, improve the accuracy of the model similarity measurement, and enhance the generalization ability of the model to new samples.

[0014] Optionally, in the similarity loss based on mean square error, the performance of the model is measured by calculating the square of the difference between the predicted value and the true value of the model, and the similarity score output by the similarity measurement module is: ; wherein, Sim denotes the similarity measurement module; WMSF denotes the weighted multi-scale fusion feature obtained by the multi-scale attention feature extraction module; The similarity loss based on mean square error can be defined as: ; wherein, Sim(i) denotes the similarity score of the i-th sample pair given by the similarity measurement module; is the true sample pair label; is the total number of sample pairs.

[0015] ​By the technical solution, a direct, stable and interpretable optimization target is provided for the model, and the similarity scores between the output samples are forced to be consistent with the true labels, thereby providing a reliable probability basis for small sample metal defect classification. In a small sample learning scenario, data scarcity makes the learning process extremely unstable. The MSE loss provides a clear and unambiguous gradient direction for model parameter update by calculating the simple squared difference between the predicted value and the true value, effectively guiding the model to quickly converge to the correct mapping relationship.

[0016] Optionally, in the distance-based loss function, a contrastive loss based on Euclidean distance is used, which can be defined as: ; wherein, is the label of the sample pair, and is 1 if they belong to the same class, otherwise is 0; is the feature representation of the support set and the query set; represents and the Euclidean distance between them; is a distance boundary value; Based on the above two loss functions, the total loss is defined as: ; wherein, r is a parameter for balancing the weights of the two losses.

[0017] By the technical solution, explicit constraints are made at the feature space level, a highly discriminative metric space is shaped through the mechanism of "pulling the same class closer and pushing the different class farther", and is optimized in cooperation with the similarity loss, thereby comprehensively improving the feature discriminability and generalization ability of the model in complex small sample defect recognition tasks.

[0018] A method for training a small sample metal surface defect recognition model based on metric meta-learning, characterized in that the trained model is the multi-scale attention twin relationship network in claim 1, and the training method is One-shot, and the specific training steps are as follows: S1, each category in the support set contains only one labeled sample, in each iteration process, the sample in the support set and the sample in the query set are respectively input into two sub-networks of the multi-scale attention twin relationship network sharing weights, to obtain weighted multi-scale fusion features of the support set and the query set ; S2, and are connected in the depth dimension to obtain the connection features .

[0019] Optionally, the multi-scale attention twin network can be trained using K-shot, and the specific training steps are as follows: D1, Support Set Each class contains K labeled samples, and unlike one-shot learning, in each iteration, it supports focusing the K samples of each class. and samples in the query set They were fed into the subnetworks of the multi-scale attention twin network. middle; D2. For the K sample features of each class in the support set. Summarize the results and calculate their average characteristics. ; D3, will support the average features of each category in the set. Features of the query image Connecting them together yields connection characteristics. ; D4. Similar to One-shot learning, the multi-scale attention twin network feeds the connection features into the similarity measurement module to convert the features of the support set and query set into similarity scores. The higher the score, the greater the probability that the two images belong to the same category.

[0020] After using the above technical solution, the beneficial effects of the present invention are: This invention pioneers an efficient and robust meta-learning method based on metric learning to address the highly fine-grained problem of classifying small-sample metal surface defects. It fully leverages the advantages of Siamese networks and relational networks in small-sample classification and enriches the model's feature representation through multi-scale feature fusion. Simultaneously, an attention mechanism is used as a compensation module to ensure the model focuses on key features.

[0021] This invention combines the advantages of Siamese networks in feature extraction with the ability of relational networks in handling class relationships. Addressing the challenge of classifying metal surface defects with small sample sizes, this invention introduces multi-scale feature fusion into the multi-scale attention Siamese network. This leverages local information containing rich, fine-grained features to improve the accuracy of metal surface defect classification and effectively mitigate the insufficient feature representation caused by small sample sizes. Furthermore, this invention introduces channel attention as a compensation module to further optimize the multi-scale fused features, enhancing the model's feature representation capabilities and enabling it to learn more useful information from important channels. Additionally, this invention proposes a loss function module to improve the stability of the training process and the model's learning ability. Attached Figure Description

[0022] In order to make the technical solutions of the embodiments of the present application or the prior art clearer, the accompanying drawings needed in the embodiments or prior art description will be briefly introduced. Obviously, the accompanying drawings in the following description are only some embodiments of the present application, and other accompanying drawings can be obtained by those of ordinary skill in the art without any creative effort based on these accompanying drawings.

[0023] Fig. 1 is a structural schematic diagram of a multi-scale attention twin relationship network in the embodiment; Fig. 2 is a structural schematic diagram of multi-scale feature fusion in the embodiment; Fig. 3 is a structural schematic diagram of a channel attention mechanism in the embodiment. DETAILED DESCRIPTION

[0024] The technical solutions in the embodiments of the present application will be described clearly and completely in combination with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, not all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without any creative effort fall within the scope of protection of the present application.

[0025] The embodiments of the present application disclose a small sample metal surface defect recognition model based on metric meta-learning. EMBODIMENT

[0026] According to Figs. 1 to 3 As shown in the figure, the small sample metal surface defect recognition model based on metric meta-learning includes the following contents.

[0027] I. Multi-scale attention twin relationship network 1. Overall framework The goal of meta-learning is not to learn the knowledge of a specific task, but to master the strategy of how to effectively learn a new task. This means that through meta-learning, the model can learn the skill of recognizing, adapting to, and performing new tasks without the need for a large amount of data or time for training from scratch. For this goal, the present application first proposes a Siamese Relation Network (SRN), which mainly includes two components: a Feature Extraction Module (FEM) and a Similarity Measurement Module (SMM). The Feature Extraction Module uses two sub-networks with shared weights to extract features from each image in the support set and the query set, respectively. Like most few-shot learning models, the Feature Extraction Module adopts an architecture setting containing four convolutional blocks to extract features, each of which contains a convolutional layer, a ReLU activation function layer, a batch normalization layer, and a max-pooling layer. After processing through the four convolutional blocks, the model then outputs the feature representation of the image through two fully connected layers. After feature extraction, the Siamese Relation Network sends the extracted features to the Similarity Measurement Module to calculate the similarity scores of the query set images with each class of the support set. The Similarity Measurement Module is composed of several convolutional blocks and fully connected layers. Each convolutional block contains a convolutional layer, a ReLU activation function layer, a batch normalization layer, and a max-pooling layer. For the fully connected layers, except for the output layer using a Sigmoid activation function to output similarity scores between 0 and 1, the remaining layers all use a ReLU activation function. In few-shot learning classification, the Relation Network uses a similarity loss based on Mean Squared Error (MSE) to train the model to regress the similarity scores to the true values of the sample pairs' labels. Then, during the training process, the Siamese Relation Network follows the same loss function setting to update the model parameters.

[0028] Subsequently, for the characteristics of small sample metal surface defect classification, the present application adopts further strategies based on the Siamese Relation Network and proposes a Multi-Scale Attention Siamese Relation Network (MASRN), the overall framework of which is shown in Fig. 1 Firstly, the Feature Extraction Module of the Multi-Scale Attention Siamese Relation Network combines multi-scale feature fusion and channel attention mechanism, which is referred to as the Multi-Scale Attention Feature Extraction Module (MAFEM) by the present application. Secondly, to further improve the performance of the model, the Multi-Scale Attention Siamese Relation Network adopts a similarity loss based on Mean Squared Error in addition to a distance-based loss function for auxiliary optimization to update the model parameters.

[0029] 2、One-shot and K-shot training methods In One-shot learning, each class in the task support set contains only one labeled sample. The first step is that in each iteration process, the sample in the support set and the sample The two subnetworks that are respectively input into the multi-scale attention Siamese relation network share weights In the process, multi-scale fusion features weighted by the support set and query set are obtained. and The second step, and They are connected along the depth dimension to obtain connection features. .

[0030] In K-shot learning, sets are supported. Each category contains K labeled samples, meaning the model has the opportunity to learn the characteristics of each category from richer sources of information. As a first step, unlike one-shot learning, in each iteration, the support focuses on K samples from each category. and samples in the query set They were fed into the subnetworks of the multi-scale attention twin network. In the second step, to better capture and represent the general features of the categories, this invention supports the K sample features of each category in the support set. Summarize the results and calculate their average characteristics. This approach helps improve the model's performance and robustness in complex environments. The third step involves averaging the features of each class in the support set. Features of the query image Connecting them together yields connection characteristics. Next, in the fourth step, similar to One-shot learning, the multi-scale attention twin network feeds the connection features into the similarity measurement module to convert the features of the support set and query set into similarity scores. The higher the score, the greater the likelihood that the two images belong to the same category.

[0031] One-shot and K-shot are two training methods under different data support conditions. Their common goal is to train the model into a "metric learning model" that can quickly identify metal surface defects and has strong generalization ability.

[0032] One-shot refers to correctly classifying a new defect sample when there is only one labeled sample for each defect category. It simulates the extreme data scarcity of industrial scenarios to the greatest extent possible, forcing the model to learn to extract the most essential and discriminative features from a single sample. The training effect is that when the model encounters a completely new defect category with only one example, it can accurately determine whether other unknown images on the production line belong to that category based on this single supporting sample. The model achieves the ability to "learn by analogy".

[0033] K-shot refers to the case where there are K (K > 1) labeled samples for each defect category, and more robust and accurate classification of new defect samples is completed. A little more sample information is used to correct the bias or noise that may exist in a single sample, and the general feature representation of the category is learned. By calculating the average features of the K samples, the model can filter out accidental details (such as abnormal lighting, slight position shift) in a single sample, thereby obtaining a more stable and representative "prototype" feature of the category. The effect after training: when the model encounters a completely new defect category and has K examples, it can integrate the information of the K samples to form a more reliable "category prototype". With this prototype as the standard, the confidence and accuracy of the classification result of the query image will usually be significantly higher than One-shot. The model realizes the ability of "summarizing and then judging".

[0034] 3. Multi-scale attention feature extraction module The multi-scale attention feature extraction module (MAFEM) combines multi-scale feature fusion (MSFF) and channel attention (CA) mechanisms.

[0035] (1) Multi-scale feature fusion Multi-scale feature fusion (MSFF) is an important technique in computer vision for extracting and combining information at different scales. It can help the model better understand and process local details and global information in images. The structure of multi-scale feature fusion basically includes two types: parallel multi-scale feature fusion (PMSFF) and cascaded multi-scale feature fusion (CMSFF). These two multi-scale fusion methods are shown in Fig. 2 Parallel multi-scale feature fusion can extract features at different scales through multiple branches or multiple convolution blocks constructed, and then fuse them through addition, splicing, etc. at a certain stage to obtain more rich information. In contrast, cascaded multi-scale feature fusion can cascade features at different scales layer by layer to form a multi-level feature pyramid to capture more complex semantic information.

[0036] The present application chooses to use cascaded multi-scale feature fusion. Specifically, using ResNet-12 as the backbone network, four convolution blocks are connected, and after each convolution block, a set of double-channel, half-resolution feature maps are generated. If the present application uses c to represent the number of channels, h to represent the height, and w to represent the width, then the size of the feature map output by the first convolution block is , the number of channels remains unchanged; the size of the feature map output by the second convolution block is , the number of channels is doubled, and the height and width are halved; the size of the feature map output by the third convolution block is , and the height and width are reduced to 1 / 4 of the original size again, extracting higher-level features; at the fourth convolutional block, the output feature map has a size of , the number of channels is doubled again, and the height and width are reduced to 1 / 8 of the original size, representing the deepest feature extraction, containing high-level abstract representations of the input data. Then, reverse fusion is performed. From the fourth convolutional block, the small-size feature map will be upsampled twice, connected with the feature map of the third convolutional block to form a high-resolution feature; then it is upsampled twice again, connected with the feature map of the second convolutional block to form a higher-resolution feature; and then it is upsampled twice again, connected with the feature map of the first convolutional block to form a cascaded multi-scale fusion feature for subsequent tasks. The cascaded multi-scale feature fusion can make the model better understand the semantic information at different scales, thereby improving the performance of the model on new tasks.

[0037] (2) Channel attention mechanism Each channel in the multi-scale fusion feature can be regarded as a feature detector, which captures some abstract features in the input data, such as edges, textures, or higher-level semantic information. However, each channel has different importance. In order to make the model learn more useful information from the important channels of the multi-scale fusion feature , the present application uses channel attention as a compensation module after the multi-scale feature fusion module, which enhances the performance of the network by learning the importance of each channel in the multi-scale fusion feature . The channel attention mechanism is shown in Fig. 3 . First, the present application performs average pooling and maximum pooling operations on the multi-scale fusion feature to obtain two spatial context descriptors: and . This operation compresses and aggregates the spatial information within each channel to obtain a summary or description of the channel, which helps to reduce the number of parameters, reduce the computational complexity, and improve the global perception ability of the model for each channel. Then, and are input into a shared multi-layer perceptron (MLP) network. Then, the two features output by the multi-layer perceptron network are added and passed through a sigmoid activation function to obtain a channel attention map , The weight coefficients in the channel attention map represent the importance of each channel in the task. Finally, the channel attention map is used to weight the multi-scale fusion feature to obtain a new, attention-adjusted weighted multi-scale fusion feature This process can make the model focus on the important channels in the task and improve the overall performance of the model.

[0038] 4、Loss function module The present application considers using a similarity loss based on mean square error (MSE) to update the model parameters in the twin relationship network. The similarity loss based on mean square error is used to optimize the output of the similarity measurement module, so that the similarity score of the positive sample pair is close to 1, and the similarity score of the negative sample pair is close to 0, thereby improving the accuracy of the model when judging the similarity of the sample pair. Inspired by metric learning, the present application constructs a distance-based loss function based on the similarity loss based on mean square error for auxiliary optimization. The distance-based loss function is used to optimize the feature space, so that the model can learn to distinguish the feature representation of different categories. This not only further improves the accuracy of the model's similarity measurement, but also enhances the model's generalization ability to new samples. Before introducing the two loss functions, the present application first constructs a one-hot vector to represent the sample pair label, if the sample pair is a positive sample pair, then , if the sample pair is negative, then .

[0039] (1) Similarity loss based on mean square error The small sample metal surface defect classification problem in the present application can be regarded as a regression problem, because the goal of the model is to predict the similarity score between samples through the similarity measurement module, rather than predicting discrete positive and negative sample pair labels. The similarity loss based on mean square error is often used in regression problems, which measures the performance of the model by calculating the square of the difference between the model's predicted value and the true value. Specifically, the similarity score output by the similarity measurement module is: ; In the formula, denotes the similarity measurement module; denotes the weighted multi-scale fusion feature obtained by the multi-scale attention feature extraction module. Then, the similarity loss based on MSE can be defined as: ; In the formula, is the similarity score given by SMM for the sample pair; is the true sample pair label; is the total number of sample pairs.

[0040] (2) Distance-based loss function In the small sample learning scenario, the information of each sample is extremely valuable. Distance-based loss functions can directly act on the feature level, guiding the model to arrange the samples in a more structured way in the feature space. This structured feature representation helps the model better understand the relationship between samples in the complex metal surface defect classification task and enhances the discriminative ability of the model. Therefore, the invention considers using a contrastive loss based on Euclidean distance, which can be defined as: ; In the formula, is the label of the sample pair, which is 1 if they belong to the same class, otherwise 0; is the feature representation of the support set and the query set; represents the Euclidean distance between and ; is a distance boundary value.

[0041] Based on the above two loss functions, the total loss is defined as: ; In the formula, r is a parameter that balances the weights of the two losses.

[0042] II. Experiment and analysis 1. Experimental details The invention carried out a series of comparative experiments on the NEU-CLS dataset to demonstrate the effectiveness of the proposed method, and carried out a series of ablation experiments to study the influence of various factors on the performance of the model. To evaluate the performance of the model on unseen new tasks, the invention adopted two experimental strategies as shown in Table 1.

[0043] Table 1 Experimental strategy

[0044] Both experimental strategies select 3 classes as the meta-training set, each containing 100 images, and the remaining 3 classes as the meta-test set, each containing 300 images. During testing, the model only learns to identify new defect classes based on 1 or 5 samples per class. It should be noted that in the experimental result table of the invention, the classification accuracy is the average accuracy of the two experimental strategies.

[0045] 2. Experimental results The performance of the proposed method is evaluated and compared with existing mainstream methods. The proposed method adopts two different experimental strategies for 3-way 1-shot (single sample three categories) and 3-way 5-shot (five sample three categories) classification tasks, and the average accuracy of the two experimental strategies is regarded as the final evaluation result to ensure the reliability of the test results. In order to ensure the consistency of the experiment, all methods are carried out under the same experimental setting. As shown in Tables 2 and 3, compared with other mainstream methods, the multi-scale attention twin relationship network achieves the best performance in 3-way 1-shot and 3-way 5-shot tasks, reaching 93.62% and 96.58% respectively. Specifically, the average precision of the multi-scale attention twin relationship network is improved by 0.34% and 1.28% compared with the most advanced FSDR method in 3-way 1-shot and 3-way 5-shot tasks respectively. In addition, the time efficiency is also evaluated, which is represented by the inference time of the model in the test stage. The experimental results show that the multi-scale attention twin relationship network can meet the requirements of real-time diagnosis of surface defects. Compared with other methods, the multi-scale attention twin relationship network trains a more robust metal surface defect classification model based on metric meta-learning, which enriches the feature representation of the model and makes the model focus on the key information that is beneficial to defect classification. In addition, the loss function calculation method designed in the present application allows the model to find an optimal balance between the accuracy of similarity measurement and the structured representation of feature space, which further improves the performance of the model.

[0046] Table 2 Classification results of multi-scale attention twin relationship network and mainstream methods in 3-way 1-shot task

[0047] Table 3 Classification results of multi-scale attention twin relationship network and mainstream methods in 3-way 5-shot task

[0048] 3、Other experimental results (1) Influence of hyperparameter value r on model performance The present application conducts a series of experiments on the NEU-CLS dataset to study the influence of the hyperparameter value r on the performance of the multi-scale attention twin relationship network. Specifically, the hyperparameter value r is set to 0.1, 0.5, 1. The experimental results are shown in Table 4, and the recognition accuracy of the model increases first and then decreases with the increase of the weight value. This phenomenon shows that although the use of distance-based loss for auxiliary optimization can improve the performance of the model, the weight cannot be too heavy.

[0049] Table 4 Influence of hyperparameter value r on performance of multi-scale attention twin relational network

[0050] (2) Influence of different feature extraction modules on model performance A series of experiments were conducted to explore the influence of different feature extraction modules on the performance of the multi-scale attention twin relational network, and the experimental results are shown in Table 5. From the experimental results, it can be seen that the cascaded multi-scale feature fusion module and the channel attention mechanism module have a positive effect on the improvement of model performance. Specifically, the cascaded multi-scale feature fusion module improves the recognition accuracy of the model by 1.15% and 0.87% on the 3-way 1-shot and 3-way 5-shot tasks. In addition, on the basis of the cascaded multi-scale feature fusion module, the CA module further enhances the performance of the model, increasing by 0.52% and 0.69% on the 3-way 1-shot and 3-way 5-shot tasks, respectively. This step-by-step analysis strategy effectively reveals the incremental value of each module in performance improvement, highlighting the synergistic effect of integrating these modules to optimize the overall performance of the multi-scale attention twin relational network.

[0051] Table 5 Influence of different feature extraction modules on performance of multi-scale attention twin relational network

[0052] (3) Ablation experiment The influence of multi-scale attention feature extraction modules and new loss function calculation methods on the classification performance of the multi-scale attention twin relational network is shown in Table 6. Compared with the twin relational network, the multi-scale attention twin relational network has significantly improved classification accuracy on the 3-way 1-shot and 3-way 5-shot classification tasks. When the multi-scale attention feature extraction module is removed, the recognition accuracy of the two tasks decreases by 1.67% and 1.56%, respectively, indicating the effectiveness of the multi-scale attention feature extraction module in extracting key multi-scale fusion features. When the new loss function calculation method is removed, the recognition accuracy of the two tasks decreases by 0.44% and 0.26%, respectively, indicating the effectiveness of the new loss function calculation method in improving the accuracy of similarity measurement and optimizing feature representation. In addition, by comparing the experimental results of the two modules, it can be found that compared with the new loss function calculation method, the multi-scale attention feature extraction module contributes more to the performance improvement of the multi-scale attention twin relational network.

[0053] Table 6 Ablation experiment on NEU-CLS dataset

[0054] The present application focuses on solving the problem of highly fine-grained small sample metal surface defect classification based on metric meta-learning. First, by fusing the advantages of twin networks in feature extraction and the ability of relationship networks in similarity measurement, the present application proposes a twin relationship network. Then, aiming at the characteristics of small sample metal surface defect classification, further strategies are taken on the basis of the twin relationship network, and a multi-scale attention twin relationship network is proposed. The network effectively integrates the high-level features and low-level features of the defect samples through cascading multi-scale feature fusion to enrich the feature representation of the model. Then, by introducing the channel attention mechanism, the model can focus on important information to improve the information processing process. In addition, an optimized loss function calculation method is introduced, which can improve the similarity measurement accuracy of the model while helping the model to learn more robust feature representation, thereby further improving the performance of the model. Finally, the performance of the proposed method is evaluated on the metal surface defect dataset, and the influence of multiple factors on the performance of the model is studied in depth. The experimental results prove the superiority of the multi-scale attention twin relationship network in small sample metal surface defect recognition.

[0055] In general, the technical solutions of the present application not only significantly improve the ability of the model to capture and analyze defect features, but also improve the accuracy of metal surface defect classification, and provide new theoretical and practical guidance for small sample learning.

[0056] The above is only a preferred embodiment of the present application, and is not intended to limit the present application. Any modification, equivalent replacement, improvement, etc. within the technical solutions of the present application shall be included in the protection scope of the present application.

Claims

1. A few-sample metal surface defect recognition model based on metric learning, characterized in that, The model is named Multi-Scale Attention Siamese Network, which includes a multi-scale attention feature extraction module and a loss function module. The multi-scale attention feature extraction module combines multi-scale feature fusion and channel attention mechanisms. By introducing multi-scale feature fusion, it utilizes local information containing rich fine-grained feature information to improve the accuracy of metal surface defect classification and effectively alleviate the insufficient feature representation caused by small samples. By introducing a channel attention mechanism as a compensation module, the multi-scale fusion features are further optimized, the model's feature representation ability is enhanced, and the model can learn more useful information from important channels. The loss function module includes a mean squared error similarity loss and a distance-based loss function. The mean squared error similarity loss is used to update the model parameters, and the square of the difference between the model's predicted value and the true value is calculated to measure the model's performance. The distance-based loss function directly acts on the feature level, guiding the model to arrange samples in the feature space in a more structured way. This structured feature representation helps the model better understand the relationship between samples in complex metal surface defect classification tasks, thereby enhancing the model's discrimination ability.

2. The few-sample metal surface defect recognition model based on metric learning according to claim 1, characterized in that, The multi-scale feature fusion includes parallel multi-scale feature fusion and cascaded multi-scale feature fusion. Parallel multi-scale feature fusion extracts features at different scales by constructing multiple branches or multiple convolutional blocks, and fuses them at a certain stage through operations such as addition and concatenation to obtain richer information. Cascaded multi-scale feature fusion cascades features at different scales layer by layer to form a multi-level feature pyramid in order to capture more complex semantic information.

3. The few-sample metal surface defect recognition model based on metric learning according to claim 2, characterized in that, The cascaded multi-scale feature fusion uses ResNet-12 as the backbone network, which connects four convolutional blocks. Each convolutional block generates a set of double-channel, half-resolution feature maps.

4. The few-sample metal surface defect recognition model based on metric learning according to claim 3, characterized in that, The cascaded multi-scale feature fusion uses c to represent the number of channels, h to represent the height, and w to represent the width. The size of the feature map output by the first convolutional block is [size missing]. The number of channels remains unchanged; the size of the output feature map of the second convolutional block is... The number of channels is doubled, while the height and width are halved; the size of the feature map output by the third convolutional block is... It further increased the number of channels and reduced the height and width again to 1 / 4 of the original size, thereby extracting higher-level features; by the fourth convolutional block, the size of the output feature map was... The number of channels is doubled again, and the height and width are each reduced to 1 / 8 of the original size. This represents the deepest feature extraction, which contains a high-level abstract representation of the input data. Then, reverse fusion is performed. Starting from the fourth convolutional block, the small feature map is upsampled twice and connected with the feature map of the third convolutional block to form a high-resolution feature. Then, it is upsampled twice again and connected with the feature map of the second convolutional block to form an even higher-resolution feature. Then, upsampling is performed twice more, and the feature map is concatenated with the feature map of the first convolutional block to form a cascaded multi-scale fused feature. For use in subsequent tasks.

5. The few-sample metal surface defect recognition model based on metric learning according to claim 1, characterized in that, In the aforementioned channel attention mechanism, multi-scale fusion features are... Performing average pooling and max pooling operations yields two spatial context descriptors: and After that, and Each feature is input into a shared multilayer perceptron network. Then, the two features output by the multilayer perceptron network are summed and passed through a sigmoid activation function to obtain the channel attention map. Finally, the channel attention map Used for multi-scale fusion features Weighting is performed to obtain a new, attention-adjusted weighted multiscale fusion feature. .

6. The few-sample metal surface defect recognition model based on metric learning according to claim 1, characterized in that, The loss function module includes a similarity loss based on mean squared error and a distance-based loss function. The mean squared error similarity loss is used to optimize the output of the similarity measurement module, making the similarity score of positive sample pairs close to 1 and the similarity score of negative sample pairs close to 0, thereby improving the accuracy of the model in judging the similarity of sample pairs. The distance-based loss function is used to optimize the feature space, enabling the model to learn feature representations that distinguish different categories, improving the accuracy of the model's similarity measurement, and enhancing the model's generalization ability to new samples.

7. The few-sample metal surface defect recognition model based on metric learning according to claim 6, characterized in that, In the similarity loss based on mean squared error, the model's performance is measured by calculating the square of the difference between the model's predicted value and the true value. The similarity score output by the similarity measurement module is: ; in, This indicates the similarity measurement module; This represents the weighted multi-scale fusion feature obtained by the multi-scale attention feature extraction module; Similarity loss based on mean squared error can be defined as: ; in, The similarity measurement module provides the first... Similarity score between samples; These are real sample pairs labeled; It represents the total number of sample pairs.

8. The few-sample metal surface defect recognition model based on metric learning according to claim 6, characterized in that, The distance-based loss function uses a contrastive loss based on Euclidean distance, which can be defined as: ; in, These are the labels of the sample pairs; a value of 1 indicates that the samples belong to the same category, while a value of 0 indicates that they belong to the same category. It is a feature representation of support sets and query sets; express and The Euclidean distance between them; It is a distance from the boundary value; Based on the two loss functions mentioned above, the overall loss is defined as: ; Where r is a parameter that balances the two loss weights.

9. A method for training a few-sample metal surface defect recognition model based on metric learning, characterized in that, The model being trained is named the Multi-Scale Attention Siamese Network as described in claim 1, and the training method is One-shot. The specific training steps are as follows: S1, Support Set Each category contains only one labeled sample, and in each iteration, the number of samples in the support set is... and samples in the query set The two subnetworks that are respectively input into the multi-scale attention Siamese relation network share weights In the process, multi-scale fusion features weighted by the support set and query set are obtained. and ; S2, and They are connected along the depth dimension to obtain connection features. .

10. The method for training a few-sample metal surface defect recognition model based on metric learning according to claim 9, characterized in that, The multi-scale attention twin network is trained using K-shot, and the specific training steps are as follows: D1, Support Set Each class in the dataset contains K labeled samples. Unlike one-shot learning, in each iteration, it supports focusing the K samples of each class. and samples in the query set Subnetworks of the multi-scale attention twin network were fed into the subnetworks. middle; D2. For the K sample features of each class in the support set. Summarize the results and calculate their average characteristics. ; D3, will support the average features of each category in the set. Features of the query image Connecting them together yields connection characteristics. ; D4. Similar to One-shot learning, the multi-scale attention twin network feeds the connection features into the similarity measurement module to convert the features of the support set and query set into similarity scores. The higher the score, the greater the probability that the two images belong to the same category.