A small sample fabric defect detection method based on category centroid calibration and dynamic discriminant constraint
By employing category centroid calibration and dynamic discrimination constraints, the problems of feature instability and fuzzy classification boundaries in fabric defect detection are solved, achieving high-precision and stable fabric defect detection, which is suitable for automated fabric quality inspection.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- ZHONGYUAN ENGINEERING COLLEGE
- Filing Date
- 2026-03-05
- Publication Date
- 2026-06-05
AI Technical Summary
Existing fabric defect detection technologies have limited accuracy in detecting minute and linear defects in complex backgrounds, and it is difficult to balance detection stability and efficiency. In particular, when the number of samples is limited and the class distribution is unbalanced, the feature representation is unstable and the classification and discrimination boundaries are blurred.
A small-sample fabric defect detection method based on category centroid calibration and dynamic discriminant constraints is adopted. The centroid feature calibration module CFCM guides the defect features to align with the global semantic centroid, and the dynamic discriminant constraint module DDCM is introduced into the classification branch of the detection head to actively widen the discrimination boundary and improve the stability of feature representation and classification accuracy.
It significantly improves the recall rate for detecting minor defects, maintains the stability and generalization ability of the model detection, and is suitable for automated fabric quality inspection.
Smart Images

Figure CN122156147A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical field of target detection, and in particular to a method for detecting defects in small sample fabrics. Background Technology
[0002] In the textile industry, fabric quality inspection is a crucial step in ensuring product consistency and production stability. During weaving and finishing processes, fabrics are prone to defects such as warp and weft misalignment and abnormal textures due to factors such as equipment fluctuations, process parameter deviations, or repetitive operational errors. These defects are typically characterized by high randomness, low frequency, and irregular shapes. Their internal features exhibit strong homogeneity and lack clear structural information, making it difficult to obtain defect sample images and often requiring extensive manual inspection and screening. Traditional manual visual inspection methods are not only labor-intensive and inefficient but also significantly affected by subjective factors, making them unsuitable for meeting the requirements of inspection efficiency and consistency under large-scale continuous production conditions. Therefore, utilizing machine vision technology to automate the detection of fabric defects has become an important development direction for quality control in the textile industry.
[0003] With the development of deep learning technology, object detection methods based on convolutional neural networks have been gradually applied to the field of fabric defect detection. Existing object detection methods mainly fall into two categories: single-stage detection methods and two-stage detection methods. Two-stage detection methods typically combine candidate region generation with object classification and location regression to complete the detection process, offering advantages such as high positioning accuracy and good detection stability. Single-stage detection methods, on the other hand, directly predict the object category and location through end-to-end regression, offering advantages such as fast detection speed and simple structure. However, in complex backgrounds and detailed object detection scenarios, single-stage detection methods still have shortcomings in object positioning accuracy and recall, while two-stage detection methods struggle to meet the actual needs of industrial production in terms of detection efficiency and real-time performance.
[0004] Unlike natural scene targets, fabric defects exhibit significant differences in appearance and spatial distribution. Fabric defects typically appear as small, elongated, or horizontally / vertically distributed linear structures. Their grayscale variations and texture features are highly similar to the background fabric, lacking clear and stable structural boundaries. Directly applying target detection networks designed for natural scenes to fabric defect detection tasks often fails to effectively distinguish defects from background areas, easily leading to missed detections and false detections. To address these issues, existing research has primarily focused on improving detection models at the network structure level, such as texture feature enhancement, multi-scale feature fusion, and attention mechanisms, thus improving fabric defect detection performance to some extent. However, these methods do not adequately consider the feature stability and class discrimination issues of fabric defects under conditions of limited sample size, weak feature representation, and subtle differences between categories. In actual detection processes, the number of defect samples is usually far less than the number of background samples, and different defect categories are prone to overlapping distributions in the feature space, leading to unstable feature representations and blurred classification boundaries, thus affecting detection accuracy and reliability. These problems are particularly pronounced in scenarios involving the detection of small and linear defects.
[0005] In summary, existing fabric defect detection technologies still have limited accuracy in detecting minute and linear defects under complex background conditions. The design of these methods fails to fully consider the morphological characteristics of fabric defects and the differences in backbone network feature extraction methods, easily leading to unstable feature representation and insufficient discriminative ability. Although existing object detection algorithms have made some progress in detection accuracy and efficiency, in application scenarios with limited fabric defect samples, uneven class distribution, and weak defect features, it remains difficult to simultaneously achieve target localization accuracy, detection recall, and model robustness. Therefore, designing a fabric defect detection method that can ensure detection accuracy while maintaining stability and efficiency, based on the existing object detection framework, remains a technical challenge to be solved in this field. Summary of the Invention
[0006] To address the technical challenges of unstable features in some defect samples and easy deviation of classification boundaries in fabric defect detection, this invention proposes a small-sample fabric defect detection method based on category centroid calibration and dynamic discrimination constraints. This invention introduces a category centroid calibration mechanism to guide the alignment of defect features of different morphologies towards a stable global semantic centroid, enhancing the stability of feature representation. Simultaneously, it introduces dynamic discrimination constraints to actively widen the discrimination boundary based on the semantic similarity between fabric image samples and category prototypes, effectively mitigating the classification performance degradation caused by interference from similar semantic categories under limited sample conditions. This invention significantly improves the detection performance of various fabric defects while effectively maintaining the stability of the model, achieving further improvement in overall detection performance in complex industrial scenarios.
[0007] To achieve the above objectives, the technical solution of the present invention is implemented as follows:
[0008] A method for detecting defects in small samples of fabrics based on category centroid calibration and dynamic discriminant constraints, comprising the following steps:
[0009] S1: Obtain the fabric defect image dataset, preprocess the fabric defect image dataset, and divide it into base class and new class datasets. Then, divide the base class and new class datasets into training set, validation set and test set, respectively.
[0010] S2: Construct a small-sample fabric defect detection network model; the fabric defect detection network model includes a backbone network, a region proposal network, and a detection head connected in sequence; a centroid feature calibration module (CFCM) is introduced between the backbone network and the region proposal network, which is used to process the input features based on the feature calibration method of the category centroid to guide the defect features of different shapes to align with the stable global semantic centroid; a dynamic discriminant constraint module (DDCM) is introduced into the classification branch of the detection head, which is used to actively widen the discrimination boundary according to the semantic similarity relationship between the fabric image sample and the category prototype;
[0011] S3: Train and validate the fabric defect detection network model using the training set and validation set to obtain the trained fabric defect detection network model;
[0012] S4: Use the trained fabric defect detection network model to detect fabric images in the test set to obtain fabric defect classification and localization images.
[0013] Furthermore, the base class and new class datasets are constructed according to the classification principle of the small sample object detection task. The base class defect samples are used for the basic detection model training in the first stage, while the new class defects provide only a small number of labeled samples for the model fine-tuning training in the second stage. For the base class training set and the new class training set, Mosaic data augmentation is used, and the data augmented images are labeled using the LabelImg annotation tool.
[0014] Furthermore, the backbone network adopts a ResNet-101 network structure, including an initial convolutional layer, a pooling layer, and four residual feature extraction stages performed sequentially. The four residual feature extraction stages include residual stage res1, residual stage res2, residual stage res3, and residual stage res4 performed sequentially. Residual stage res1 is used to extract low-level texture of the fabric image, residual stages res2 and res3 are used to extract mid-level texture features of the fabric image, and res4 outputs a high-level semantic feature map F.
[0015] Furthermore, in the centroid feature calibration module CFCM, the method for processing input features based on category centroids to guide the alignment of defect features of different forms towards a stable global semantic centroid is as follows: receiving the high-level semantic feature map F output by the backbone network, and unfolding the high-level semantic feature map F along the spatial dimension into a set of one-dimensional feature vectors. The system retrieves a pre-built set of global category centroids and calculates temporary category centroids by combining the cosine similarity between each feature vector and the global category centroid. It then corrects the deviation of the original feature vector at each spatial location by combining the spatial displacement of each feature vector with the category centroid and a nonlinear adjustment factor, thereby obtaining calibrated feature vectors. All calibrated feature vectors are then reconstructed into a two-dimensional calibrated feature map F*, which is output to the region proposal network.
[0016] Furthermore, temporary class centroids are calculated by retrieving a pre-constructed set of global class centroids and combining the cosine similarity between each feature vector and the global class centroids, including:
[0017] S221. Based on the candidate regions of the labeled samples, construct a global set of category centroids for each fabric defect category in the feature space. ,in, Indicates the first The semantic center of fabric-like defects in the feature space. Number of fabric defect categories;
[0018] S222, By measuring the feature vector Feature vector of each spatial location With the global category centroid The cosine similarity between features is used to assign features to the closest semantic partitions:
[0019] ;
[0020] in, Indicates the first Feature vector of spatial location This is the index of the semantic partition to which the vector belongs after calculation. It is an L2 norm;
[0021] S223. Aggregate features belonging to the same semantic partition to obtain the temporary category centroid of that partition:
[0022] ;
[0023] in, Indicates the total number of spatial locations. For indicator functions, This represents the temporary local centroid obtained from statistical analysis of the actual annotation information within the current training batch.
[0024] Furthermore, the original feature vector at each spatial location is biased by combining the spatial displacement of each feature vector with the class centroid with a nonlinear adjustment factor, including:
[0025] S241. Calculate the spatial displacement of the characteristic and the centroid of the corresponding region. :
[0026] ;
[0027] S242. The original features are superimposed and corrected using a nonlinear adjustment factor to obtain the calibrated feature vector. :
[0028] ;
[0029] in, It is a non-linear adjustment factor. Let L be the square of the L2 norm of the spatial displacement. This is a hyperparameter used to control the rate of exponential decay and adjust the strength of the offset correction.
[0030] Furthermore, the centroid feature calibration module (CFCM) also includes:
[0031] The global class centroid is iteratively updated based on the temporary class centroid using an exponential moving average method:
[0032] ;
[0033] in, This represents the momentum coefficient used to control the smoothness of the centroid update.
[0034] Furthermore, the detection head receives RoI features extracted by performing RoI Align operation on the calibration feature map F* based on candidate regions generated by the region proposal network as input to the regression branch and the classification branch. Fine-tuning is performed at the position in the regression branch, and a dynamic discriminant constraint module DDCM is introduced in the classification branch. Based on the semantic similarity relationship between the RoI features and the category prototype, a dynamic discriminant constraint term is introduced to actively widen the discrimination boundary and obtain the corrected prediction result.
[0035] Furthermore, based on the semantic similarity relationship between the RoI features and the category prototype, a dynamic discrimination constraint term is introduced to actively widen the discrimination boundary, obtaining the corrected prediction result, including:
[0036] Calculate the semantic similarity between RoI features and prototypes of each category:
[0037] ;
[0038] in, For the first The feature vector of the i-th RoI and the i-th Similarity of prototypes in each category Indicates the first Each RoI feature vector For the first Prototype representation of each category;
[0039] A set of candidate interference categories is selected by using a similarity threshold:
[0040] ;
[0041] in, For similarity threshold, The true class label of the sample Background category;
[0042] Candidate interference categories Sort by similarity in descending order, and select the top K as the core interference categories to accurately define the difficult-to-classify regions;
[0043] A dynamic constraint term is introduced to penalize the original predicted values corresponding to the core interference categories, while the original predicted values for non-core interference categories remain unchanged.
[0044] ;
[0045] in, For the original classification logits, This is the scaling factor. The revised prediction results This is for dynamically identifying constraint terms.
[0046] Furthermore, the training method for the small-sample fabric defect detection network model is as follows:
[0047] Phase 1: Input the base class defect samples from the training set into the untrained small sample fabric defect detection network model. Each training round completes the following: backbone network feature extraction, CFCM feature calibration, RPN candidate region generation, detection head, calculation of multi-task loss, and SGD optimizer update of all network parameters. The DDCM module is not introduced in the first phase. After each training round, the detection accuracy of the model is evaluated using the validation set, and the base class training model with the best performance on the validation set is saved.
[0048] The second stage: Using the model parameters obtained from the base class training as initial weights, the new class defect samples from the training set are input into the optimal base class training model. In each round of training, the following are completed: backbone network feature extraction, CFCM feature calibration, RPN candidate region generation, detection head, calculation of multi-task loss, and SGD optimizer to update all network parameters. In the second stage, the DDCM module is introduced. After each round of fine-tuning, the model's detection accuracy for the new class defects is evaluated using the validation set. Early stopping is used to prevent overfitting. The model with the best performance on the validation set is saved as the trained fabric defect detection network model.
[0049] The beneficial effects of this invention are as follows:
[0050] This invention presents a small-sample fabric defect detection method based on category centroid calibration and dynamic discriminant constraints. The category centroid calibration module (CFCM) enhances the representation stability of defect features under limited sample conditions, effectively reducing localization errors. Simultaneously, the dynamic discriminant constraint module (DDCM) actively suppresses interference from semantically similar categories during the classification stage, widening the discrimination boundary between different defect categories and solving the problem of false detection of easily confused targets in fabric defect detection. Experiments show that this method has a significant performance gain compared to baseline methods on standard datasets, not only greatly improving the recall rate of detecting minute defects but also maintaining detection stability while considering the model's generalization ability, providing a reliable technical means for automated fabric quality inspection. Attached Figure Description
[0051] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0052] Figure 1 This is a flowchart of the fabric defect detection method based on category centroid calibration and dynamic discrimination constraints of the present invention.
[0053] Figure 2 This is the defect detection network model of the present invention.
[0054] Figure 3 This is a design diagram of the centroid feature calibration module CFCM of the present invention.
[0055] Figure 4 This is the design diagram of the Dynamic Discriminant Constraint Module (DDCM) of the present invention.
[0056] Figure 5 The results are visualizations of the present invention. (a) is a visualization of the baseline detection results, and (b) is a visualization of the detection results of the present invention. Detailed Implementation
[0057] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0058] A small-sample fabric defect detection method based on category centroid calibration and dynamic discriminant constraints, such as Figure 1 As shown, the steps are as follows:
[0059] S1: Obtain the fabric defect image dataset, preprocess the fabric defect image dataset, and divide the preprocessed dataset into a training set, a validation set, and a test set.
[0060] In this embodiment, the fabric defect image dataset uses the TianChi dataset. The publicly available TianChi dataset contains nine defect types, each with different shapes and sizes: knots, three-fiber defects, coarse fibers, sparse or dense weaves, sizing spots, holes, coarse warp, calender marks, and fuzz. It contains a total of 6186 fabric defect images, each with a size of 640×640 pixels.
[0061] Furthermore, the fabric defect image dataset is preprocessed, including data cleaning and normalization.
[0062] Furthermore, the preprocessed dataset is constructed according to the classification principles of small-sample object detection tasks: the nine defect categories are divided into non-overlapping base classes and novel classes, with no overlap at the category level. Base class defect samples are used for the first stage of basic detection model training, while novel class defects provide only a small number of labeled samples for the second stage of model fine-tuning training. In this embodiment, three different category splitting schemes are used to allow defect categories to take turns as novel classes in training. Under each splitting scheme, base class defect samples are used to train the basic detection model; novel class defects provide only a small number of labeled samples for fine-tuning training, forming a small-sample fabric defect detection dataset based on N-way K-shot. Specific splitting scheme: Following common classification strategies for small-sample object detection, the nine fabric defect categories in the dataset are divided into base classes and novel classes. In each splitting scheme, three categories are selected as novel classes, and the remaining six categories are selected as base classes. Only K-shot labeled samples are retained for the novel classes to construct the small-sample training set. To reduce the randomness of category division, this paper designs three different new category division schemes, as follows: Group 1 (holes, pulp spots, fibrous grains), Group 2 (coarse diameter, sparse and dense, three-fiber), and Group 3 (knots, rolling marks, coarse fibers).
[0063] Furthermore, the base class is divided into training, validation, and test sets, and the new class is also divided into training, validation, and test sets.
[0064] Furthermore, for both the base class training set and the new class training set, Mosaic data augmentation was used, and the data augmented images were labeled using the LabelImg annotation tool.
[0065] S2: Construct a small-sample fabric defect detection network model, such as Figure 2 As shown, the fabric defect detection network model includes a backbone network, a region proposal network (RPN), and a detection head connected in sequence. A centroid feature calibration module (CFCM) is introduced between the backbone network and the region proposal network. Based on the feature calibration method of the category centroid, the input features are processed to guide the defect features of different shapes to align with the stable global semantic centroid. A dynamic discriminant constraint module (DDCM) is introduced in the classification branch of the detection head to actively widen the discrimination boundary according to the semantic similarity relationship between the fabric image sample and the category prototype.
[0066] In this embodiment, the backbone network adopts the ResNet-101 network structure. This network introduces a residual connection mechanism in the multi-layer convolutional structure, which enables features to be transferred across different depths, thereby effectively alleviating the gradient vanishing and feature degradation problems in the training process of deep networks.
[0067] Specifically, the backbone network includes an initial convolutional layer (Conv1), a pooling layer (Pool1), and four residual feature extraction stages connected in sequence. These four residual feature extraction stages are residual stages res1, res2, res3, and res4, performed sequentially. Residual stage res1 extracts low-level texture features from the fabric image; residual stages res2 and res3 extract mid-level texture features; and res4 outputs a high-level semantic feature map F. This high-level semantic feature map serves as the input feature for the subsequent class centroid calibration module (CFCM), representing the high-level semantic information of fabric defects and providing reliable high-level feature support for stable modeling of class semantic centers under small sample conditions based on the accumulation of residual features.
[0068] In this embodiment, the Centroid Feature Calibration (CFCM) module constrains and aligns sample features by maintaining a category-level semantic center during the base class training phase. Feature calibration primarily enhances the stability of fabric defect feature representations under limited sample conditions by guiding defect features towards a stable reference direction, thus mitigating the instability of feature representations when the sample size is small. It is particularly important to note that the design of the centroid feature calibration aims to gradually learn a stable global semantic centroid through cross-batch accumulation. The feature distribution provided by a single batch or single instance often exhibits significant randomness, easily leading to unstable feature representations and exacerbating the discreteness and drift problems of features in the semantic space. By introducing a global centroid, the centroid feature calibration module can uniformly model different samples at the feature level, improving the stability of feature representations under small sample conditions.
[0069] Specifically, the centroid feature calibration module (CFCM), such as Figure 3 As shown, by retrieving a pre-constructed set of global category centroids and combining the cosine similarity between each feature vector and the global category centroid, a temporary category centroid is calculated. The original feature vector at each spatial location is then corrected for deviation by combining the spatial displacement of each feature vector with the category centroid and a nonlinear adjustment factor. This calibrates the high-level semantic feature map of the backbone network, allowing defect features of the same category to cluster towards the global centroid, thereby generating a more aggregated and robust calibrated feature representation, providing high-quality feature input for the RPN.
[0070] Specifically, the processing procedure of the Centroid Feature Calibration (CFCM) module is as follows: Figure 3 As shown, it specifically includes:
[0071] S21. Receive the high-level semantic feature map F output by the backbone network, and expand the high-level semantic feature map F along the spatial dimension into a set of one-dimensional feature vectors. ,in The total number of spatial locations (as shown in the figure) ), For the height of the special map, The width of the feature map. The number of channels, i.e., the dimension of the feature vector at each spatial location. For the first Feature vectors of spatial locations.
[0072] S22. Based on the candidate regions of the labeled samples, construct a global category centroid set for each category of fabric defects. By calculating the cosine similarity between each feature vector in the one-dimensional feature vector set and the global category centroid, assign the features to the closest semantic partitions and aggregate the features in the same partitions to obtain temporary category centroids.
[0073] Specifically, firstly, based on the candidate regions of the labeled samples, a global set of category centroids for each fabric defect category in the feature space is constructed. ,in, Indicates the first The semantic center of fabric-like defects in the feature space. Number of fabric defect categories;
[0074] Furthermore, by measuring the feature vector Feature vector of each spatial location With the global category centroid The cosine similarity between features is used to assign features to the closest semantic partitions. The mapping process is calculated using the following formula: :
[0075] ;
[0076] in, Indicates the first Feature vector of spatial location This is the index of the semantic partition to which the vector belongs after calculation. It is an L2 norm;
[0077] Furthermore, features belonging to the same semantic partition are aggregated to obtain the temporary category centroid of that partition, calculated using the following formula:
[0078] ;
[0079] in, Indicates the total number of spatial locations. For indicator functions, This represents the temporary local centroid obtained from statistical analysis of the actual annotation information within the current training batch.
[0080] S23. To reduce the impact of sample size fluctuations on semantic modeling, we do not directly... Instead of using the temporary class centroid as the final centroid, the global class centroid is iteratively updated using an exponential moving average based on the temporary class centroid to suppress feature drift.
[0081] Specifically, using the temporary category centroid Iterative update of global category centroid To gradually absorb the statistical characteristics of the global samples and suppress feature drift, its update formula is:
[0082] ;
[0083] in, This represents the momentum coefficient used to control the smoothness of the centroid update. This represents the temporary local centroid obtained from the statistical data of the actual annotation information within the current training batch. This process ensures that the semantic center can gradually absorb the statistical characteristics of the global samples, avoiding feature drift caused by fluctuations in a single batch of samples.
[0084] S24. Calculate the spatial displacement of each feature vector relative to the temporary centroid of its corresponding partition, and then apply a nonlinear adjustment factor to the original feature vectors to obtain the calibrated feature vectors. This process enables features at different spatial locations to adaptively cluster towards their corresponding stable centers based on their semantic contribution, effectively reducing intra-class feature variance and suppressing background interference.
[0085] Specifically, first calculate the spatial displacement between the feature and the centroid of its respective region. :
[0086] ;
[0087] Subsequently, the original features are superimposed and corrected using a nonlinear adjustment factor to obtain the calibrated feature representation. This causes features within the same semantic region to cluster towards the centroid of the corresponding category. The calculation formula is:
[0088] ;
[0089] in, It is a non-linear adjustment factor. Let L be the square of the L2 norm of the spatial displacement. This is a hyperparameter used to control the rate of exponential decay and adjust the strength of the offset correction.
[0090] The Centroid Feature Calibration (CFCM) module constrains and aligns sample features during the base class training phase by maintaining category-level semantic centers. Feature calibration primarily enhances the stability of fabric defect feature representations under constrained sample conditions by guiding defect features towards a stable reference direction, thus mitigating the instability of feature representations when the sample size is small.
[0091] S25. Reconstruct all calibrated feature vectors into a two-dimensional calibration feature map F*, and output it to the region proposal network.
[0092] In this embodiment of the application, the Region Proposal Network (RPN) is based on a two-dimensional calibrated feature map F*. It presets anchor boxes of different scales and proportions at each pixel position, calculates the objectivity score and position offset of each anchor box through two convolutional layers, and finally filters and generates a set of fabric defect candidate regions with high-quality semantic response through non-maximum suppression (NMS).
[0093] In this embodiment, the detection head receives RoI features extracted from candidate regions generated by the Region Proposal Network and the RoI Align operation performed on the calibration feature map F* during the new class fine-tuning stage. These features are used as inputs for the regression and classification branches. Fine-tuning is performed at the position of the regression branch. A Dynamic Discriminant Constraint Module (DDCM) is introduced into the classification branch. This module is specifically used in the classification score (logits) generation stage to strengthen the discrimination boundary between semantically similar categories by introducing dynamic discriminant constraints into the classification logit score level, effectively alleviating the classification bias problem under the condition of scarce samples.
[0094] Specifically, firstly, based on the candidate regions generated by RPN, the RoI Align operation is performed on the two-dimensional calibration feature map F* to extract the features of each candidate region, and after pooling, the RoI feature vector set is obtained;
[0095] Furthermore, the RoI feature vector set is input into the regression branch of the detection head (composed of two fully connected layers). The position offset correction value of each candidate region is output through regression calculation. The coordinates of the candidate regions generated by RPN are refined to obtain the accurate position coordinates of the defects.
[0096] Furthermore, a dynamic discriminant constraint module (DDCM) is introduced into the classification branch, and dynamic constraint terms are introduced in the classification logits generation stage to correct the prediction scores of easily confused categories.
[0097] In this embodiment, the Dynamic Discriminant Constraint Module (DDCM) filters candidate interference categories based on the semantic similarity between RoI features and prototypes of each category, and sorts the elements in the set in descending order of similarity, selecting the top K categories with the highest similarity as core interference objects to accurately define the classification difficulties.
[0098] Specifically, such as Figure 4 As shown, the processing procedure of the Dynamic Discriminant Constraint Module (DDCM) is as follows:
[0099] First, calculate the semantic similarity between RoI features and each category prototype: First, construct a set of category prototypes, and then for each calibrated RoI feature... Calculate its relationship with the prototypes of each category. Cosine similarity matrix between :
[0100] ;
[0101] in, For the first The feature vector of the i-th RoI and the i-th Similarity of prototypes in each category Indicates the first [unit] after pooling Each RoI feature vector For the first Prototype representation of each category.
[0102] Furthermore, this example does not impose constraints on all outliers simultaneously, but only focuses on categories with potential interference, using a similarity threshold. Filter out the candidate interference category set :
[0103] ;
[0104] in, For similarity threshold, The true class label of the sample Background category.
[0105] Furthermore, in the field of fabric defect detection, since confusion can easily arise between different categories, this embodiment further categorizes candidate interference categories. Sort by similarity in descending order, and select the top K as the core interference categories to accurately define the difficult-to-classify regions;
[0106] Furthermore, at the classification logits level, a dynamic constraint term is introduced to penalize the original predicted values corresponding to the core interference categories, while the original predicted values for non-core interference categories remain unchanged.
[0107] ;
[0108] in, For the original classification logits, This is the scaling factor. The revised prediction results A dynamic discriminative constraint term, namely adaptive interval injection, is used to widen the discriminative boundary between RoI features and easily confused semantic categories. Finally, the modified logits are used for cross-entropy loss-supervised training to improve detection stability. This invention introduces dynamic semantic discriminative constraints at the classification logits level to actively widen the discriminative boundary. This approach enhances the penalty effect of semantically similar dissimilar categories in the cross-entropy loss, widening the discriminative boundary between RoI features and easily confused categories. Under the condition of high prototype reliability, this mechanism further improves the model's discriminative reliability for minor fabric defects and easily confused targets.
[0109] S3: Use the training set and validation set to train and validate the fabric defect detection network model to obtain the trained fabric defect detection network model.
[0110] In this embodiment, all experiments were conducted on a server running the Ubuntu operating system. The hardware configuration included two NVIDIA RTX 4090 GPUs, using the PyTorch framework. The training process consisted of two phases: base class training and new class fine-tuning. The base class training phase iterated 15,000 times with an initial learning rate of 0.02; the new class fine-tuning phase iterated 1,000-2,000 times based on the number of shots, adjusting the learning rate to 0.01. The optimizer used momentum-driven SGD with a momentum factor of 0.9 and a weight decay of 0.0001. The loss function consisted of the region proposal network loss, the detection head's classification loss, and the regression loss, with the classification branch trained under supervised conditions using modified logits.
[0111] like Figure 2 As shown, the training process is as follows:
[0112] Phase 1, Base Class Training:
[0113] Input the base class defect samples from the training set into the untrained small sample fabric defect detection network model;
[0114] Each training round completes the following steps: backbone network feature extraction → CFCM feature calibration → RPN candidate region generation → detection head (without using DDCM module) → calculation of multi-task loss → SGD optimizer updates all network parameters.
[0115] The multi-task loss includes classification and regression loss in the RPN stage, and classification and bounding box regression loss in the detection head stage. The CFCM module performs class centroid alignment and distribution stabilization constraints on the base class features in this stage to construct a base class feature space with a clear structure and stable discrimination. The DDCM module is not introduced in this stage to avoid introducing additional discrimination perturbations during the base class's full learning stage, thereby ensuring the full convergence and stability of the base class semantic representation.
[0116] After each round of training, the detection accuracy of the model is evaluated using the validation set, and the base class training model with the best performance on the validation set is saved.
[0117] The model has stable feature representation and candidate region generation capabilities, providing good initialization parameters for adapting to small sample sizes of new classes.
[0118] The second stage is new class training. In this stage, the model parameters obtained from the base class training are used as initial weights, and rapid adaptive learning is achieved using a small number of new class samples.
[0119] Input the new class of defect samples from the training set into the optimal base class to train the model;
[0120] The training iterations are performed 1000 to 2000 times based on the value of K (1000 iterations when K=1 / 2, and 2000 iterations when K=3 / 5 / 10). Each training round completes the following: backbone network feature extraction → CFCM feature calibration → RPN candidate region generation → detection head (including DDCM module) → calculation of multi-task loss → SGD optimizer updates all network parameters.
[0121] Among them, the CFCM module continues to play the role of class centroid alignment in the new class stage, guiding the features of new class samples to gather in a stable semantic direction; at the same time, the DDCM module is introduced to apply dynamic discrimination constraints in the classification stage, thereby alleviating the discrimination confusion problem between the new class and semantically similar base classes by adjusting the decision boundaries between different classes.
[0122] After each round of fine-tuning, the model's detection accuracy for new types of defects is evaluated using a validation set, and an early stopping method is used to prevent overfitting.
[0123] Save the model with the best performance on the validation set as the trained fabric defect detection network model.
[0124] The final model maintains a relatively stable performance in detecting base class defects while effectively identifying new types of defects, thus completing the task of detecting defects in small sample fabrics.
[0125] S4: Use the trained fabric defect detection network model to detect fabric images in the test set to obtain fabric defect classification and localization images.
[0126] Simulation verification:
[0127] The effectiveness of the algorithm improvement proposed in this invention was verified through ablation experiments, and the experimental results are shown in Table 1. As can be seen from Table 1, the detection performance was improved to varying degrees after gradually introducing different functional modules based on the baseline model (DeFRCN: Decoupled Faster R-CNN). First, after introducing the feature calibration module based on class centroid calibration (CFCM), the model achieved a stable improvement in overall detection accuracy, especially in the detection performance of new defect classes, indicating that this module can effectively alleviate the problem of unstable feature distribution under small sample conditions. Furthermore, by integrating the dynamic discriminant constraint module (DDCM), the model showed a more significant improvement under higher shot settings, indicating that the proposed dynamic discriminant constraint can effectively optimize the discriminant boundary between classes and reduce the confusion between new defect classes and semantically similar base classes. The comprehensive experimental results show that the CFCM and DDCM modules have good complementarity in small sample fabric defect detection tasks and can synergistically improve the overall detection performance of the model, thus verifying the effectiveness and rationality of the improved scheme of this invention.
[0128] Table 1 Ablation experimental results based on the TianChi dataset
[0129]
[0130] In addition, to compare the performance of the proposed model, comparative experiments with other models were included. The experimental results are shown in Table 2. It can be seen that compared with other YOLO models, the defect detection network model proposed in this invention has higher detection accuracy.
[0131] Table 2 Comparative experimental data based on the TianChi dataset
[0132]
[0133] In addition, a visualization experiment was conducted, and the visualization results are as follows: Figure 5 As shown, the detection results demonstrate that the small-sample fabric defect detection method based on category centroid calibration and dynamic discrimination constraints is very effective for small targets and can detect some targets with a very small area ratio relative to the background.
[0134] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A method for detecting small-sample fabric defects based on category centroid calibration and dynamic discriminant constraints, characterized in that, The steps are as follows: S1: Obtain the fabric defect image dataset, preprocess the fabric defect image dataset, and divide it into base class and new class datasets. Then, divide the base class and new class datasets into training set, validation set and test set, respectively. S2: Construct a small-sample fabric defect detection network model; the fabric defect detection network model includes a backbone network, a region proposal network, and a detection head connected in sequence; a centroid feature calibration module (CFCM) is introduced between the backbone network and the region proposal network, which is used to process the input features based on the feature calibration method of the category centroid to guide the defect features of different shapes to align with the stable global semantic centroid; a dynamic discriminant constraint module (DDCM) is introduced into the classification branch of the detection head, which is used to actively widen the discrimination boundary according to the semantic similarity relationship between the fabric image sample and the category prototype; S3: Train and validate the fabric defect detection network model using the training set and validation set to obtain the trained fabric defect detection network model; S4: Use the trained fabric defect detection network model to detect fabric images in the test set to obtain fabric defect classification and localization images.
2. The method for detecting small-sample fabric defects based on category centroid calibration and dynamic discriminant constraints according to claim 1, characterized in that, The base class and new class datasets are constructed according to the classification principle of the small sample object detection task. The base class defect samples are used for the first stage of basic detection model training, while the new class defects provide only a small number of labeled samples for the second stage of model fine-tuning training. Mosaic data augmentation is used for both the base class training set and the new class training set, and the data augmented images are labeled using the LabelImg annotation tool.
3. The method for detecting small-sample fabric defects based on category centroid calibration and dynamic discriminant constraints according to claim 2, characterized in that, The backbone network adopts the ResNet-101 network structure, which includes an initial convolutional layer, a pooling layer, and four residual feature extraction stages performed sequentially. The four residual feature extraction stages include residual stage res1, residual stage res2, residual stage res3, and residual stage res4 performed sequentially. Residual stage res1 is used to extract low-level texture of the fabric image, residual stages res2 and res3 are used to extract mid-level texture features of the fabric image, and res4 outputs a high-level semantic feature map F.
4. The method for detecting small-sample fabric defects based on category centroid calibration and dynamic discriminant constraints according to claim 3, characterized in that, In the Centroid Feature Calibration Module (CFCM), the method for processing input features based on category centroids to guide the alignment of defect features of different forms towards a stable global semantic centroid is as follows: The high-level semantic feature map F output by the backbone network is received, and the high-level semantic feature map F is unfolded along the spatial dimension into a set of one-dimensional feature vectors. The system retrieves a pre-built set of global category centroids and calculates temporary category centroids by combining the cosine similarity between each feature vector and the global category centroid. It then corrects the deviation of the original feature vector at each spatial location by combining the spatial displacement of each feature vector with the category centroid and a nonlinear adjustment factor, thereby obtaining calibrated feature vectors. All calibrated feature vectors are then reconstructed into a two-dimensional calibrated feature map F*, which is output to the region proposal network.
5. The method for detecting small-sample fabric defects based on category centroid calibration and dynamic discriminant constraints according to claim 4, characterized in that, Temporary class centroids are calculated by retrieving a pre-constructed set of global class centroids and combining the cosine similarity between each feature vector and the global class centroids, including: S221. Based on the candidate regions of the labeled samples, construct a global set of category centroids for each fabric defect category in the feature space. ,in, Indicates the first The semantic center of fabric-like defects in the feature space. Number of fabric defect categories; S222, By measuring the feature vector Feature vector of each spatial location With the global category centroid The cosine similarity between features is used to assign features to the closest semantic partitions: ; in, Indicates the first Feature vector of spatial location This is the index of the semantic partition to which the vector belongs after calculation. It is an L2 norm; S223. Aggregate features belonging to the same semantic partition to obtain the temporary category centroid of that partition: ; in, Indicates the total number of spatial locations. For indicator functions, This represents the temporary local centroid obtained from statistical analysis of the actual annotation information within the current training batch.
6. The method for detecting small-sample fabric defects based on category centroid calibration and dynamic discriminant constraints according to claim 5, characterized in that, The original feature vector at each spatial location is biased by combining the spatial displacement of each feature vector with the class centroid with a nonlinear adjustment factor, including: S241. Calculate the spatial displacement of the characteristic and the centroid of the corresponding region. : ; S242. The original features are superimposed and corrected using a nonlinear adjustment factor to obtain the calibrated feature vector. : ; in, It is a non-linear adjustment factor. Let L be the square of the L2 norm of the spatial displacement. This is a hyperparameter used to control the rate of exponential decay and adjust the strength of the offset correction.
7. The method for detecting small-sample fabric defects based on category centroid calibration and dynamic discriminant constraints according to claim 6, characterized in that, The Centroid Feature Calibration Module (CFCM) also includes: The global class centroid is iteratively updated based on the temporary class centroid using an exponential moving average method: ; in, This represents the momentum coefficient used to control the smoothness of the centroid update.
8. The method for detecting small-sample fabric defects based on category centroid calibration and dynamic discriminant constraints according to any one of claims 1-7, characterized in that, The detection head receives RoI features extracted by performing RoI Align operation on the calibration feature map F* based on candidate regions generated by the region proposal network as input to the regression branch and the classification branch. Fine-tuning is performed at the position in the regression branch, and a dynamic discriminant constraint module DDCM is introduced in the classification branch. Based on the semantic similarity relationship between the RoI features and the category prototype, a dynamic discriminant constraint term is introduced to actively widen the discrimination boundary and obtain the corrected prediction result.
9. The method for detecting small-sample fabric defects based on category centroid calibration and dynamic discriminant constraints according to claim 8, characterized in that, Based on the semantic similarity relationship between the RoI features and the category prototype, a dynamic discrimination constraint term is introduced to actively widen the discrimination boundary, obtaining the corrected prediction result, including: Calculate the semantic similarity between RoI features and prototypes of each category: ; in, For the first The feature vector of the i-th RoI and the i-th Similarity of prototypes in each category Indicates the first RoI feature vectors, For the first Prototype representation of each category; A set of candidate interference categories is selected by using a similarity threshold: ; in, For similarity threshold, The true class label of the sample Background category; Candidate interference categories Sort by similarity in descending order, and select the top K as the core interference categories to accurately define the difficult-to-classify regions; A dynamic constraint term is introduced to penalize the original predicted values corresponding to the core interference categories, while the original predicted values for non-core interference categories remain unchanged. ; in, For the original classification logits, This is the scaling factor. The revised prediction results This is for dynamically identifying constraint terms.
10. The method for detecting small-sample fabric defects based on category centroid calibration and dynamic discriminant constraints according to claim 9, characterized in that, The training method for the small-sample fabric defect detection network model is as follows: Phase 1: Input the base class defect samples from the training set into the untrained small sample fabric defect detection network model. Each training round completes the following: backbone network feature extraction, CFCM feature calibration, RPN candidate region generation, detection head, calculation of multi-task loss, and SGD optimizer update of all network parameters. The DDCM module is not introduced in the first phase. After each training round, the detection accuracy of the model is evaluated using the validation set, and the base class training model with the best performance on the validation set is saved. The second stage: Using the model parameters obtained from the base class training as initial weights, the new class defect samples from the training set are input into the optimal base class training model. In each round of training, the following are completed: backbone network feature extraction, CFCM feature calibration, RPN candidate region generation, detection head, calculation of multi-task loss, and SGD optimizer to update all network parameters. In the second stage, the DDCM module is introduced. After each round of fine-tuning, the model's detection accuracy for the new class defects is evaluated using the validation set. Early stopping is used to prevent overfitting. The model with the best performance on the validation set is saved as the trained fabric defect detection network model.