A small sample fabric defect detection method based on feature uncertainty coding and feature storage
By employing feature uncertainty encoding and feature storage methods, the problems of misjudgment of background texture and instability of new feature classes in small sample fabric defect detection are solved, improving detection accuracy and robustness, and making it suitable for defect detection against complex fabric backgrounds.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- ZHONGYUAN ENGINEERING COLLEGE
- Filing Date
- 2026-04-21
- Publication Date
- 2026-06-05
AI Technical Summary
Existing methods for detecting defects in small-sample fabrics are prone to misclassifying background textures as defects when the fabric background texture is complex and the defect morphology is diverse. This results in high noise in the candidate boxes, unstable distribution of new defect features across iterations, and insufficient detection accuracy and robustness.
We employ a feature uncertainty coding and feature storage approach. By introducing a feature uncertainty coding module into the Region Proposal Network (ROI), we can explicitly characterize the noise and variance of defect features and suppress spurious responses. We also construct a feature storage module in the ROI head network to achieve cross-iteration feature caching and momentum coding, thereby stabilizing the distribution of new class features.
It improves the quality of candidate region generation and feature discrimination ability, significantly enhances the accuracy and robustness of fabric defect detection, and adapts to high-quality detection in small sample scenarios.
Smart Images

Figure CN122156186A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical field of target detection and industrial defect detection, and in particular to a method for detecting defects in small sample fabrics. Background Technology
[0002] Fabric defect detection is a core aspect of textile quality control, directly impacting product quality and market competitiveness. Currently, it is mainly divided into two categories: manual inspection and automated inspection. Manual inspection relies on the visual judgment of inspectors, resulting in low efficiency, long cycles, and susceptibility to subjective experience and fatigue. This leads to inconsistent results and high rates of missed and false detections, making it difficult to meet the high-efficiency inspection needs of modern textile production lines. Automated inspection methods based on computer vision and deep learning, on the other hand, can achieve rapid and objective defect identification and have become the mainstream development direction for fabric inspection. As an important branch of automated detection, small-sample object detection technology has the core advantage of reducing dependence on massive amounts of labeled data. It can complete object detection with only a small number of new class labeled samples, perfectly meeting the real pain points of scarce defect samples (long-tail distribution) and extremely high labeling costs in textile scenarios. Currently, two mainstream research paradigms have formed in this field: one is the transfer learning-based method (such as TFA and DeFRCN), which uses base class data for pre-training and then fine-tunes the detection head to adapt to new classes. It has a simple structure, stable training, and is easy to implement in engineering. The other is the meta-learning-based method (such as Meta R-CNN), which uses base class tasks to allow the model to learn a general "learning strategy" to adapt to new classes. However, the training process is complex and computationally expensive. It is also susceptible to feature drift in complex fabric texture backgrounds, making actual deployment difficult.
[0003] Although various small-sample object detection methods perform well on general datasets such as COCO and PASCAL VOC, there are fundamental differences between fabric defect detection scenarios and general object detection scenarios. Fabrics themselves have complex textures (such as warp and weft interlacing textures), and defects (such as holes, stains, uneven yarn, skipped yarns, missing warp, etc.) are generally characterized by diverse shapes, low contrast, small size, and easy confusion with background textures. This leads to significant bottlenecks when general methods are directly transferred: First, in the candidate region generation stage, the model cannot effectively separate complex fabric textures from weak defect features, easily misjudging background textures as defects and generating a large number of high-noise candidate boxes. Moreover, existing methods mostly use deterministic feature representations, lacking explicit characterization of the uncertainty of defect features, further aggravating interference. Second, in the feature learning stage, new defect samples are scarce and intra-class differences are significant (such as the size and position of similar stains are variable). The RoI feature distribution fluctuates drastically across iterations. Existing methods mostly rely on limited sample updates in a single mini-batch, lacking cross-iteration feature reuse and stabilization mechanisms, making it difficult to form a compact and separable new class feature distribution, ultimately resulting in insufficient detection accuracy and robustness. To alleviate the above problems, the academic community has carried out targeted explorations: ERNet proposed by Li et al. enhances the fine-grained defect feature representation through multi-view attention mechanism and data augmentation; FS-SSDD proposed by Liu et al. integrates CNN and Transformer architecture and introduces adaptive contrastive proposal encoding loss to reduce noise interference; some methods improve the robustness of new defect features through vector quantization feature aggregation.
[0004] Overall, existing methods for detecting fabric defects in small samples still have significant shortcomings: most rely on deterministic feature representation, which makes it difficult to characterize the noise and distribution instability of fabric defects; the collaborative optimization of candidate region quality control and feature distribution stabilization is insufficient, and it cannot effectively solve the core pain points of amplifying model bias in high-noise candidate boxes and unstable feature learning for new types of defects. There is still considerable room for improvement in detection accuracy, robustness and generalization ability in textile industrial scenarios. Summary of the Invention
[0005] To address the challenges of detecting fabric defects in small-sample scenarios, where complex background textures, diverse defect morphologies (such as holes, stains, and uneven yarn), and low contrast are prevalent, existing methods often misclassify background textures as defects, leading to high candidate box noise and unstable cross-iteration distribution of new defect features. This results in weak detection accuracy, robustness, and generalization ability. This invention proposes a small-sample fabric defect detection method based on feature uncertainty encoding and feature storage. By introducing a feature uncertainty encoding module into the Region Proposal Network (ROI), the noise and variance of defect features are explicitly characterized, suppressing spurious responses to improve candidate box quality. Furthermore, a feature storage module is constructed in the ROI head network. Through cross-iteration feature caching, momentum encoding, and supervised contrastive learning, the distribution of new feature classes is stabilized, ultimately achieving high-precision and robust fabric defect detection in small-sample scenarios.
[0006] To achieve the above objectives, the technical solution of the present invention is implemented as follows:
[0007] A method for detecting small-sample fabric defects based on feature uncertainty encoding and feature storage, comprising the following steps:
[0008] S1: Obtain the dataset, preprocess the dataset, and divide the preprocessed dataset into base class training set, new class training set, validation set and test set;
[0009] S2: Construct a small sample target detection network model; the small sample target detection network model includes a feature extraction network, a region proposal network, an ROI head network, and a detection head connected in sequence; the feature extraction network includes a backbone network and a feature enhancement module in sequence; the region proposal network includes a feature uncertainty encoding module; the ROI head network includes a feature storage module;
[0010] S3: Use the base class training set, the new class training set, and the validation set to train and validate the small sample target detection network model to obtain the trained detection model;
[0011] S4: Use the trained detection model to detect images in the test set and obtain target localization and classification results.
[0012] Furthermore, when constructing new training sets, a K-shot few-shot learning setting is adopted;
[0013] The validation set and test set contain both base class targets and new class targets;
[0014] It also includes data augmentation on the base class training set and the new class training set. Data augmentation includes: random horizontal flipping, random vertical flipping, random cropping, color dithering, and affine transformation of bounding box coordinates to maintain label consistency.
[0015] Furthermore, the backbone network adopts the ResNet-101 backbone network to extract multi-scale deep features of fabric images in the dataset;
[0016] The feature enhancement module employs the FPN module, which is used to enhance the multi-scale deep features extracted by the ResNet-101 backbone network to obtain a multi-scale feature pyramid.
[0017] Furthermore, the region proposal network sequentially includes a feature uncertainty coding module and an RPN Head;
[0018] The feature uncertainty coding module is used to perform uncertainty coding on each scale feature in the multi-scale feature pyramid to obtain uncertainty features. The uncertainty coding models each scale feature in the multi-scale feature pyramid as a probability distribution and obtains uncertainty features through reparameterized sampling and residual fusion.
[0019] The RPN Head takes the uncertainty features as input and obtains candidate regions through shared feature extraction and RPN dual-branch prediction.
[0020] Furthermore, the ROI head network sequentially includes: an ROI Pooling layer, an online encoder, a momentum encoder, and a feature storage module;
[0021] The ROI Pooling layer extracts candidate region features from different scales of the feature pyramid based on the size of the candidate region area, and uses the ROIAlign method to map the candidate region features into a fixed-size ROI feature map. ;
[0022] The online encoder uses ROI feature maps As input, dynamic feature representations are obtained through encoding operations. The parameters of the online encoder are updated through gradient backpropagation;
[0023] The momentum encoder uses ROI feature maps As input, stable feature representations are obtained through encoding operations. The parameters of the momentum encoder are updated via an exponential moving average.
[0024] The feature storage module is used to store stable feature representations of the momentum encoder output during each forward propagation. Perform quality screening and storage;
[0025] The detection head uses a cosine similarity classifier when calculating the classification score.
[0026] Furthermore, in the feature uncertainty encoding module, each scale feature in the multi-scale feature pyramid is modeled as a probability distribution, and uncertainty features are obtained through reparameterized sampling and residual fusion, including:
[0027] St1, for each scale feature in the multi-scale feature pyramid The mean and log-variance of the distribution are predicted by two parallel lightweight encoders, respectively.
[0028] Step 2: Based on the preset truncation interval, truncate the logarithmic variance and obtain the truncated logarithmic variance. ;
[0029] St3, Introducing an adjustable scaling factor Calculate the standard deviation: ;
[0030] St4. Sample noise from a standard normal distribution and generate probabilistic features through reparameterized sampling: , For noise sampled from a standard normal distribution, This indicates element-wise multiplication;
[0031] St5, combine the sampled probability feature z with each scale feature Residual fusion is performed according to preset weights to obtain features after feature uncertainty encoding: , , To preset weights, and .
[0032] Furthermore, the online encoder and the momentum encoder employ the same encoding operation: encoding the ROI feature map... Deep feature extraction and adaptive average pooling are performed sequentially on the three bottleneck residual blocks. Operations, fully connected layers, L2 normalization; each bottleneck residual block includes a parallel backbone path and a skip connection branch. The backbone path includes, in sequence: a 1×1 dimensionality reduction convolutional layer, a 3×3 spatial feature extraction convolutional layer, and a 1×1 dimensionality increase convolutional layer; the skip connection branch includes a 1×1 projection convolutional layer.
[0033] The momentum encoder parameters are updated using an exponential moving average as follows:
[0034]
[0035] Where m is the momentum coefficient. These are the parameters for the online encoder.
[0036] Furthermore, the stability characteristics of the momentum encoder output are represented. Quality screening and storage include:
[0037] Quality screening: Representation of stable characteristics of momentum encoder output Perform quality screening: retain only foreground features that simultaneously satisfy the following conditions: classification confidence level greater than a preset confidence threshold and intersection-union ratio (IU) between candidate boxes and ground truth boxes greater than a preset IU threshold;
[0038] Storage: Maintain a queue storage structure Q with a fixed capacity of K. Store the filtered stable feature representations into the queue storage structure Q. To address the scarcity of new class samples in small sample detection scenarios, the capacity K is divided into a base class storage area and a new class storage area. The capacity of the new class storage area is greater than that of the base class storage area, and a first-in-first-out cyclic covering mechanism is adopted.
[0039] Furthermore, the training process includes:
[0040] Base class pre-training stage: Using the base class training set as input, after forward propagation through the feature extraction network, region proposal network, ROI head network and detection head, the loss function of the base class pre-training stage is calculated; only the online encoder parameters are updated through backpropagation, and the momentum encoder parameters are frozen; the momentum encoder parameters are updated through exponential moving average.
[0041] The small sample fine-tuning stage: K training samples are selected from the base class training set and the new class training set to form the training set for the small sample fine-tuning stage. The parameters of the feature extraction network are fixed. After forward propagation through the feature extraction network, region proposal network, ROI head network and detection head, the loss function of the small sample fine-tuning stage is calculated. Only the parameters of the online encoder are updated through backpropagation, and the momentum encoder parameters are frozen. The momentum encoder parameters are updated through exponential moving average.
[0042] The loss function in the base class pre-training stage includes weighted cross-entropy classification loss, Smooth L1 regression loss, and KL divergence regularization loss of the feature uncertainty coding module; the cross-entropy classification loss includes the foreground-background classification loss of the RPN and the multi-class classification loss of the detection head; the Smooth L1 regression loss includes the bounding box regression loss of the RPN and the bounding box regression loss of the detection head.
[0043] The loss function in the small sample fine-tuning stage is based on the loss function in the base class pre-training stage, with the addition of supervised contrast loss.
[0044] Furthermore, the KL divergence regularization loss of the feature uncertainty encoding module is:
[0045]
[0046] Where H, W, and C are the height, width, and number of channels of the feature map, respectively. , These are the feature positions in channel c. The distribution mean and standard deviation;
[0047] The supervised comparison loss is:
[0048]
[0049] in, The set of positive samples includes other samples of the same type as sample i in the same batch, and stable features generated by the momentum encoder for sample i. ; The negative sample set includes samples in the current batch that are dissimilar to sample i, and historical features in the queue storage structure that are dissimilar to sample i. , , Samples , , The normalization characteristics, For temperature coefficient, This represents the inner product, used to calculate the cosine similarity between two normalized features.
[0050] The beneficial effects of this invention are as follows:
[0051] Compared with existing methods for detecting industrial defects in small samples, this invention improves detection performance simultaneously in both candidate region generation and feature discrimination learning stages through the synergistic optimization of feature uncertainty encoding and feature storage. Firstly, in the candidate region generation stage, a feature uncertainty encoding mechanism is introduced to model the original deterministic features as a probability distribution. Through reparameterized sampling and KL divergence regularization constraints, feature noise is explicitly modeled and suppressed, effectively reducing the interference of complex background textures and weak-contrast defects on candidate region generation, lowering the probability of false responses, and improving the accuracy and stability of candidate box localization, thereby providing higher-quality region feature input for subsequent detection heads. Secondly, in the feature learning stage, by constructing a partitioned feature repository and combining a momentum encoder with a supervised contrastive learning strategy, cross-batch feature reuse and hard-negative sample mining are achieved. This alleviates the problems of easy drift in the distribution of new class features and non-compactness within classes under small sample conditions, promoting greater aggregation of similar defect features in the embedding space and greater separability of dissimilar features, thus significantly enhancing the model's discrimination and generalization capabilities for new class targets. Attached Figure Description
[0052] 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.
[0053] Figure 1 This is a flowchart of the small sample fabric defect detection method based on feature uncertainty encoding and feature storage according to the present invention.
[0054] Figure 2 This is the overall framework of the small-sample fabric defect detection method based on feature uncertainty encoding and feature storage of the present invention.
[0055] Figure 3 This paper compares traditional feature representation methods with the method proposed in this invention.
[0056] Figure 4 This is the feature storage and momentum feature learning module of the present invention.
[0057] Figure 5 A visualization comparing the feature activations of the Baseline model and the proposed method. Detailed Implementation
[0058] 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.
[0059] A few-sample target detection method based on feature uncertainty encoding and feature storage, such as Figure 1 As shown, the steps are as follows:
[0060] S1: Obtain the dataset, preprocess the dataset, and divide the preprocessed dataset into base class training set, new class training set, validation set, and test set.
[0061] Dataset Acquisition: This invention uses a self-built CID (Cotton Inspection Dataset) cotton defect detection dataset as the primary experimental dataset. The CID dataset is a refined dataset specifically designed for textile inspection, containing various common defect types encountered in cotton fabric production. It features dense target distribution and class imbalance. The dataset contains 2890 cotton fabric images, each ranging in size from 1200×1050 pixels to 1700×1490 pixels, with an average of 1515×1328 pixels. It contains 79285 bounding boxes, averaging 27.43 bounding boxes per image. The dataset includes 10 cotton defect categories, numbered from class_0 to class_9. Class_7 is the primary defect type, accounting for 70.6%, while the remaining 9 categories are secondary defect types.
[0062] The dataset was preprocessed as follows: First, the original cotton fabric images were grayscale normalized to the [0,1] range to eliminate differences caused by different acquisition devices and lighting conditions. Then, the defect areas in the dataset were labeled with bounding boxes using a labeling tool. The labeling format adopted the PASCAL VOC standard (including category labels and bounding box coordinate information) to ensure the accuracy and consistency of the labeling.
[0063] Base class and new class partitioning: Under the small sample setting, the preprocessed CID dataset is divided into base class and new class: Six defect classes (class_0, class_3, class_4, class_5, class_7, class_8) are selected as base classes, containing a total of 74,385 bounding boxes; the remaining four defect classes (class_1, class_2, class_6 and class_9) are selected as new classes.
[0064] Dataset partitioning: The dataset is divided into training, validation, and test sets. The training set includes a base class training set and a new class training set. The base class training set contains 2022 images for base class pre-training; the new class training set uses a K-shot setting (K=5,10,30), that is, K labeled images are randomly selected for each new class defect, with only a small number of labeled images to construct a small-sample training scenario, containing 20-120 images for small-sample fine-tuning; the validation set contains 433 images (containing both base class and new class samples); the test set contains 435 images (a balanced distribution of base class and new class samples), used to comprehensively evaluate the model's detection performance and generalization ability.
[0065] Training set data augmentation: To enhance the robustness of the model, data augmentation was performed on the training set images, including: random horizontal flipping (probability 0.5), random vertical flipping (probability 0.5), random cropping (cropping ratio 0.8-1.0), and color dithering (brightness, contrast, and saturation adjustment range ±10%). Simultaneously, corresponding affine transformations were performed on the bounding box coordinates to maintain label consistency. The final training, validation, and test sets consisted of 2022+ (4×K) images, 433 images, and 435 images, respectively.
[0066] S2: Construct a few-shot target detection network model; the few-shot target detection network model includes a feature extraction network, a region proposal network, a ROI head network, and a detection head connected in sequence; the feature extraction network includes a backbone network and a feature enhancement module; the region proposal network includes a feature uncertainty encoding module; the ROI head network includes a feature storage module. Figure 2 As shown, the few-shot object detection network model constructed in this invention is based on the Faster R-CNN architecture.
[0067] S21. Feature extraction network: The backbone network adopts the ResNet-101 backbone network; the feature enhancement module adopts the FPN module.
[0068] Specifically, the ResNet-101 backbone network is used to extract multi-scale deep features from fabric images, taking into account both detailed features of defects (such as small skipped stitches) and global semantic features (such as large-area stains), providing multi-scale feature input for subsequent candidate region generation. The ResNet-101 backbone network includes sequentially connected convolutional layers, batch normalization layers, ReLU activation layers, max pooling layers, and multiple sets of residual blocks.
[0069] In this embodiment, the convolutional layer has a kernel size of 7×7 and a stride of 2; the max pooling layer has a kernel size of 3×3 and a stride of 2; the residual block consists of four groups of residual blocks, each corresponding to a stage, and the four groups of residual blocks contain 3, 4, 23, and 3 residual units respectively. The gradient vanishing problem in deep networks is solved through shortcut connections of identity mapping. The ResNet-101 backbone network outputs four feature maps at four different resolutions (each resolution corresponds to a scale) in four different stages: res2 (resolution is 1 / 4 of the input image, number of channels 256), res3 (resolution is 1 / 8 of the input image, number of channels 512), res4 (resolution is 1 / 16 of the input image, number of channels 1024), and res5 (resolution is 1 / 32 of the input image, number of channels 2048).
[0070] Specifically, the FPN module is used to perform top-down path enhancement and lateral connections on the multi-scale feature maps output by ResNet-101, generating a multi-scale feature pyramid:
[0071] The feature map res5 is obtained by reducing the number of channels to 256 through 1×1 convolution. ;
[0072] The feature map p5 is upsampled by 2x and then element-wise added to the feature map res4 (which has been aligned with the 1×1 convolution channel). Finally, it is smoothed using a 3×3 convolution to obtain the final feature map. ;
[0073] The feature map p4 is upsampled by 2x and then added element-wise to the feature map res3 (which has been aligned with the 1×1 convolution channel). Finally, it is smoothed using a 3×3 convolution to obtain the final feature map. ;
[0074] The feature map p3 is upsampled by 2x and then element-wise added to the feature map res2 (which has been aligned with the 1×1 convolution channel). Finally, it is smoothed using a 3×3 convolution to obtain the final feature map. ;
[0075] Max pooling of p5 yields (The resolution is 1 / 64 of the input image).
[0076] Ultimately, a multi-scale feature pyramid containing rich semantic information is formed. This top-down path enhancement allows low-level feature maps to fuse with high-level semantic information, while lateral connections preserve the spatial details of low-level features, making it particularly suitable for detecting fabric defects at different scales.
[0077] S22. Region Proposal Network (RPN): Used for uncertainty encoding and noise suppression of multi-scale feature pyramids, generating high-quality fabric defect candidate regions and removing spurious response candidate boxes caused by background texture, thereby achieving feature enhancement. The Region Proposal Network processes the feature maps at each scale output by the FPN module and includes two core components: a feature uncertainty encoding module and the RPN Head.
[0078] Feature uncertainty coding module: such as Figure 3As shown, a comparison is given between traditional feature representation methods and the method proposed in this invention. Addressing the issues of complex background textures and susceptibility of defect features to noise interference in fabric defect detection, this invention introduces a feature uncertainty encoding module in the RPN (Random Feature Network) to convert deterministic feature representations into probability distribution representations. By explicitly modeling feature uncertainty, background spurious responses are suppressed. The feature uncertainty encoding module performs uncertainty encoding on each scale feature in the multi-scale feature pyramid to obtain uncertain features. This uncertainty encoding models each scale feature in the multi-scale feature pyramid as a probability distribution, and uncertain features are obtained through reparameterized sampling and residual fusion.
[0079] Specifically, such as Figure 2 As shown, the processing steps in the feature uncertainty coding module include:
[0080] First, mean / log-variance encoding: for the feature map of each scale output by the FPN module. Where H, W, and C are the height, width, and number of channels of the feature map, respectively, the mean and log-variance of the decibel prediction distribution are obtained through two parallel lightweight encoders:
[0081] ;
[0082] ;
[0083] Among them, lightweight encoders and lightweight encoder Both are implemented using two 1×1 convolution layers, with the first convolution layer... The input features are mapped from C channels to C channels, with ReLU activation introducing non-linearity, followed by a second convolutional layer. The input is then mapped back to C channels to match the output channel count. A 1×1 convolution is used instead of a larger kernel because it does not change the spatial dimension of the feature map (H×W remains unchanged), ensuring spatial alignment of the features before and after encoding, which facilitates subsequent residual connections. The mean μ represents the deterministic center of the features, and the log-variance logvar represents the degree of uncertainty of the features.
[0084] Furthermore, logvariance interval truncation: To prevent the encoder from predicting the variance as infinitesimal (leading to the distribution degenerating into a deterministic point estimate) or infinite (leading to training instability), logvariance is truncated.
[0085] ;
[0086] in, This is a function that cuts off the interval.
[0087] Furthermore, standard deviation calculation: An adjustable scaling factor α (set to 0.3 in this embodiment) is introduced to calculate the standard deviation:
[0088] ;
[0089] Furthermore, reparameterized sampling generates probabilistic features: noise is sampled from the standard normal distribution, and probabilistic features are generated through reparameterized sampling.
[0090] ;
[0091] in, For noise sampled from a standard normal distribution, This indicates element-wise multiplication. This sampling method allows the gradient to propagate back through μ and σ, ensuring that the encoder parameters are learnable.
[0092] Furthermore, residual fusion: To balance the exploratory nature of features (randomness introduced by sampling) and stability (determinism of the original features), the sampled probabilistic features z and the original features F are residually fused with a weight of 0.3:0.7 to obtain features after feature uncertainty encoding. In the inference stage, the residual fusion of the mean μ and the original features is directly used as deterministic prediction, thereby realizing explicit modeling and constraint of feature uncertainty in small sample scenarios.
[0093] .
[0094] KL divergence regularization constraint: To prevent the encoded distribution from deviating too much, KL divergence is introduced as a regularization term in the loss function during subsequent training. KL divergence measures the learned distribution. With the prior standard normal distribution The difference between them, for a Gaussian distribution, the KL divergence has a closed-form solution:
[0095] ;
[0096] in, This represents the index of the c-th channel of the feature map.
[0097] In the actual loss function, for all feature locations and channels Sum the KL divergences and take the average:
[0098] ;
[0099] in, , These are the feature positions in channel c. The distribution mean and standard deviation.
[0100] RPN Head: The RPN Head is used to generate and filter high-quality candidate regions. The RPN Head takes the aforementioned uncertainty features as input and obtains candidate regions through shared feature extraction and RPN two-branch prediction.
[0101] Specifically, the RPN Head processing procedure includes:
[0102] First, shared feature extraction: Input a 3×3 convolutional layer to extract shared features In this embodiment, the 3×3 convolutional layer has 512 channels, a stride of 1, and a padding of 1.
[0103] Furthermore, dual-branch prediction: will share features Input two parallel The convolutional branch performs binary classification of the foreground (defects) and background, as well as bounding box position offset prediction.
[0104] The classification branch of the RPN Head: outputs 2A channels (A=3 is the number of anchor boxes), predicting the foreground / background probability of each anchor box;
[0105] The regression branch of the RPN Head outputs 4A channels and predicts the positional offset of each anchor box relative to the ground truth box. .
[0106] Furthermore, multi-scale anchor box generation: in the feature pyramid Different anchor frames with different reference dimensions and aspect ratios are set for different levels. The baseline size is adaptively adjusted according to the feature level: Layer usage Pixel anchor frame Layer usage , Layer usage , Layer usage , Layer usage This ensures that defects of different scales, from small to large, can be captured.
[0107] RPN Loss: RPN training uses binary classification cross-entropy loss. and Smooth L1 regression loss :
[0108] ;
[0109] in, Let be the foreground probability of the i-th anchor box. For real labels (1 for foreground, 0 for background). This is the predicted bounding box offset. This is the actual offset. The number of anchor boxes sampled for classification training (i.e., mini-batch size). This represents the total number of activated anchor box locations on the feature map. This is the balance factor (set to 1.0).
[0110] In the subsequent training process, after non-maximum suppression (NMS, IoU threshold set to 0.7), the top-2000 candidate regions are retained during the training phase and the top-1000 candidate regions are retained during the inference phase, and then output to the ROI head network.
[0111] S23, ROI Head Network: Used to align and encode the features of candidate regions for fabric defects. A dual-stream encoder enables dynamic learning and stable reference of features, a feature storage module facilitates cross-iteration feature reuse, and finally, supervised contrastive loss is designed to enhance the discriminative power of defect features, addressing the problem of unstable feature distribution under small sample sizes. Figure 2 As shown, the ROI head network includes an ROI Pooling layer, an inline encoder, a momentum encoder, and a feature storage module.
[0112] ROI Pooling Layer: This layer uses the Region of Interest Align (ROIAlign) method to align the candidate regions output by the RPN, obtaining aligned features. Based on the area of the candidate regions, it adaptively selects different levels of the feature pyramid for feature extraction: specifically, area... Candidate boxes from Layer extraction, Candidate boxes from Layer extraction, Candidate boxes from Layer extraction, Candidate boxes from Layer extraction, area The maximum scale candidate box from Layer extraction. ROIAlign uses bilinear interpolation to map the features of candidate regions at arbitrary scales to a fixed-size layer. ROI feature map As an alignment feature, it avoids the quantization error of traditional ROIPooling and retains sub-pixel level spatial accuracy.
[0113] Online encoder: Used to encode the alignment features output by the ROIAlign layer online, obtaining a dynamic feature representation. Specifically, the output of ROIAlign... Feature map First, the deep feature extraction module is used, which consists of three cascaded bottleneck residual blocks. The specific structure is as follows: The first bottleneck residual block is used for channel dimensionality upscaling. The main path includes: a 1×1 dimensionality reduction convolutional layer (mapping 256 dimensions to 512 dimensions), a 3×3 spatial feature extraction convolutional layer, and a 1×1 dimensionality upscaling convolutional layer (mapping 512 dimensions to 2048 dimensions). Its skip connection branch includes a 1×1 projective convolutional layer to upscale the 256 channels of the input feature to 2048 channels, which is then element-wise added to the output of the main path. The second and third bottleneck residual blocks have the same structure. Their main paths each include: a 1×1 dimensionality reduction convolutional layer (reducing 2048 dimensions to 512 dimensions), a 3×3 spatial feature extraction convolutional layer, and a 1×1 dimensionality upscaling convolutional layer (restoring 512 dimensions to 2048 dimensions). Their skip connection branches use identity mapping, directly adding the 2048-dimensional input feature to the output of the main path element-wise. Each of the above convolutional layers is followed by a batch normalization layer and a ReLU activation function. After this deep feature extraction module, the output is... The high-dimensional feature map is then compressed into an adaptive average pooling layer. The global feature vector is obtained; the pooled multidimensional global feature vector is flattened into a one-dimensional vector through the Flatten operation; finally, it is reduced to 1024 dimensions through a fully connected layer (FC) and then subjected to L2 normalization to obtain a high-dimensional dynamic feature representation. .
[0114] ;
[0115] in, For adaptive average pooling operation, It is a fully connected layer.
[0116] Momentum encoder: With the same structure as the online encoder, it is used to encode the aligned features output by the ROIPooling layer online to obtain stable feature representations. The forward propagation of the momentum encoder does not participate in gradient backpropagation, but is only used to generate stable feature representations as positive samples for contrastive learning.
[0117] Specifically, the processing procedure of the momentum encoder is as follows:
[0118] ;
[0119] Momentum encoders have the same structure as in-circuit encoders, but their parameter update methods differ. The parameters of a momentum encoder... Updated using the Exponential Moving Average (EMA):
[0120] ;
[0121] Where m is the momentum coefficient (set to 0.999 in this embodiment). These are the parameters for the online encoder. A momentum encoder with slow updates provides more consistent feature representations, mitigating the drastic fluctuations in feature distribution in small-sample scenarios.
[0122] Feature storage module: Used to persistently cache high-quality foreground features from historical batches to enable feature reuse across iterations and to mine rich hard negative samples for subsequent contrastive learning, such as... Figure 4 As shown.
[0123] Specifically, the feature storage module maintains a queue storage structure Q with a fixed capacity of K. In each forward propagation, the stable feature representations output by the momentum encoder are first subjected to quality screening: based on the results of the classification branch of the detector head, only high-quality foreground features that simultaneously satisfy the following conditions: classification confidence greater than a preset confidence threshold (e.g., 0.5) and the intersection-union ratio (IU) of the candidate box and the ground truth box greater than a preset IU threshold (e.g., 0.4).
[0124] The selected stable features will be stored in a queue storage structure Q. To address the extreme scarcity of novel class samples in small-sample detection scenarios, queue Q employs a base class and novel class partitioning strategy: the total capacity K is divided into a base class storage area (capacity K = K / K). ) and new type of storage area (capacity is In this embodiment, the capacity of the new class storage area is set to be greater than the capacity of the base class storage area, specifically divided into: and This provides greater storage space for cross-batch feature reuse of new classes. Both storage areas independently employ a first-in, first-out (FIFO) cyclic overwrite mechanism, meaning that when a new feature is enqueued and the queue is full, the oldest historical feature is automatically overwritten, thus ensuring the timeliness of feature distribution in the storage queue.
[0125] Supervised contrastive loss: The goal of contrastive learning is to narrow the feature distance between similar samples and widen the feature distance between dissimilar samples. For the i-th foreground sample in the current batch, its set of positive samples... This includes: other samples of the same type as sample i in the same batch; and stable features generated by the momentum encoder for sample i. (As its own positive sample pairs). Negative sample set This includes: samples that are different from sample i in the current batch; and historical features that are different from sample i in the feature storage queue Q (as hard negative samples).
[0126] The supervised contrastive loss function is defined as:
[0127] ;
[0128] in, This is the set of foreground sample indices for the current batch. For sample i, the normalized features are... The temperature coefficient is set to 0.7 in this embodiment, and · represents the inner product (i.e., cosine similarity, since the features have been normalized). Temperature coefficient Controlling the smoothness of the distribution: The smaller the value, the steeper the distribution, and the higher the model's requirements for distinguishing hard-to-bear samples; The larger the value, the smoother the distribution and the more stable the learning process. This loss function optimizes multiple positive sample pairs simultaneously using the log-sum-exp form. Compared with traditional triplet loss or contrastive loss, it can make fuller use of sample information within the batch and queue, enhancing the inter-class discriminativeness and intra-class compactness of features.
[0129] S24. Inspection head: Used to complete the final classification and location of fabric defects.
[0130] The detection head includes classification and regression branches, each represented by dynamic features. For input, the specific processing procedure is as follows:
[0131] Classification branches of detection heads:
[0132] Classification score calculation: A cosine similarity classifier is used to calculate dynamic feature representation. The normalized inner product of the class weights with the prototype weight vectors of each class is used as the classification logits. Let the class weight matrix be... (N is the number of classes, including base classes and new classes), the weight matrix is obtained by performing L2 normalization row by row. The classification score is calculated as follows:
[0133] ;
[0134] in, A learnable temperature scalar (initialized to 20.0) is used to adjust the scale of cosine similarity. This is in contrast to traditional linear classifiers. The cosine similarity classifier eliminates the magnitude difference between features and weights through normalization, so that the classification decision depends only on directional similarity, making it more suitable for class imbalance problems in small sample scenarios.
[0135] Classification Output: The logits (with dimension N, where N is the number of classes) of the cosine similarity classifier are normalized using Softmax to obtain the confidence probability distribution for each class:
[0136] ;
[0137] Regression branch of the detection head:
[0138] Offset prediction: A class-independent strategy is adopted, using a fully connected layer (output dimension 4) to predict the positional offset of the candidate region relative to the ground truth bounding box. ,in For center point offset, This is the logarithmic value for scaling the width and height.
[0139] Regression Output: The offset of the bounding box regression head Convert to absolute coordinates:
[0140] ;
[0141] ;
[0142] ;
[0143] ;
[0144] in, The center coordinates and width and height of the candidate region.
[0145] During the inference phase, candidate boxes with low confidence are first filtered out by a confidence threshold (set to 0.05). Then, a class-specific nonmaximum suppression algorithm (NMS, with an IoU threshold set to 0.5) is applied to remove overlapping detection boxes. Finally, the top 100 detection results with the highest confidence in each image are retained as the model output.
[0146] S3: Use the base class training set, the new class training set, and the validation set to train and validate the small sample target detection network model to obtain the trained detection model.
[0147] Model training is divided into two stages: the base class pre-training stage and the few-sample fine-tuning stage.
[0148] Base class pre-training phase:
[0149] The complete network is trained on the base class training set to learn a general feature representation of fabric images. The training configuration is as follows:
[0150] Optimizer: SGD (Stochastic Gradient Descent), initial learning rate 0.02, momentum 0.9, weight decay 0.0001;
[0151] Learning rate scheduling: The Warm-up policy linearly increases to 0.02 in the first 500 iterations, and then decays to 0.002 and 0.0002 in the 60,000th and 80,000th iterations, respectively.
[0152] Batch size: 16 (8 images per GPU, trained in parallel using 2 NVIDIA RTX 4090 GPUs).
[0153] Total number of iterations: 10,000;
[0154] The overall loss function is a weighted sum of classification loss, regression loss, and KL divergence loss (contrastive loss is not used during pre-training because only base class data is available):
[0155] ;
[0156] in:
[0157] Cross-entropy classification loss, including foreground-background classification loss of RPN and multi-class classification loss of detector head;
[0158] Smooth L1 regression loss, including the bounding box regression loss of the RPN and the bounding box regression loss of the detection head;
[0159] : KL divergence regularization loss of the feature uncertainty encoding module.
[0160] The loss weighting coefficient is set as follows: =1.0, =0.0001 (KL loss has a smaller weight, avoiding over-constraining of the feature distribution).
[0161] Small sample fine-tuning stage:
[0162] Fine-tuning is performed on the new class training set (4 classes × K images, K=5, 10, 30) and a subset of the base class training set (K images randomly selected from each class, maintaining a balance between base and new class samples). The parameters of the feature extraction network (ResNet-101 and FPN) are fixed, and only the parameters of the RPN, ROI head network, detection head, feature uncertainty encoding module, and feature storage module are updated. The fine-tuning configuration is as follows:
[0163] Optimizer: SGD, initial learning rate 0.001 (1 / 20 of the pre-trained rate), momentum 0.9, weight decay 0.0001;
[0164] Learning rate scheduling: Decays to 0.0001 and 0.00001 in the 1000th and 2500th iterations, respectively;
[0165] Batch size: 16 (8 images per GPU, using 2 GPUs);
[0166] Total number of iterations: 4000 (the training cycle is shorter in small sample scenarios);
[0167] Input image size: same as in the pre-training phase;
[0168] The overall loss function includes a supervised contrastive loss:
[0169] ;
[0170] The loss weighting coefficient is set as follows: , , The contrast loss weights are adaptively adjusted based on the number of shots: 0.3 when K=5 (to avoid overfitting due to very few samples) and 0.7 when K=30 (to enhance contrast learning due to a large number of samples).
[0171] In each iteration:
[0172] Forward propagation: Input batch images, and extract candidate region features through feature extraction network, RPN, and ROIAlign;
[0173] Feature encoding: The inline encoder and momentum encoder encode features respectively. and ;
[0174] Feature storage: Filter high-quality foreground features (confidence ≥ 0.5 and IoU ≥ 0.4) and store them in the repository queue;
[0175] Loss calculation: Calculate classification loss, regression loss, contrastive loss (using historical features in the queue as negative samples), and KL divergence loss;
[0176] Backpropagation: Update only online encoder parameters Freeze the momentum encoder; Momentum update: Update the momentum encoder parameters via EMA;
[0177] The weights of the model that performs best on the validation set are saved as the final detection model. All experiments were conducted on a GPU consisting of 2×NVIDIA RTX 4090 (24GB VRAM), CUDA version 11.7, PyTorch version 2.0.1, and Detectron2 framework version 0.6.
[0178] S4: Use the trained detection model to detect images in the test set and obtain target localization and classification results.
[0179] During the inference phase, the input test set images are processed through a feature extraction network, RPN, ROIAlign, and the forward propagation of the detection head. The output includes the bounding boxes, class labels, and confidence scores for each detected defect in the image. The detection results are saved in PASCAL VOC format, including bounding box coordinates. Category ID and confidence score.
[0180] (1) Ablation test
[0181] The ablation experiment used the following combination of methods for comparative verification:
[0182] Baseline: Standard Faster R-CNN framework (ResNet-101+FPN+ETF classifier), using only classification loss and regression loss.
[0183] Baseline+FU: Adds a feature uncertainty (FU) coding module to the baseline.
[0184] Baseline+FR: Add a feature storage module (FR) to the Baseline.
[0185] Baseline+FU+FR: The method of this invention simultaneously applies an uncertainty coding module and a feature storage module.
[0186] Ablation experiments showed that, under small sample settings of 5-shot, 10-shot, and 30-shot, the proposed method (Baseline+FU+FR) achieved nAP50 accuracy of 15.922%, 21.877%, and 34.991%, respectively, representing improvements of approximately 4.7, 5.4, and 7.0 percentage points compared to the baseline model; and mAP50 accuracy of 24.960%, 36.807%, and 48.011%, respectively, representing improvements of approximately 4.8, 5.8, and 5.7 percentage points compared to the baseline model. These results demonstrate that the proposed method can stably improve the detection accuracy of novel target types even under very limited sample conditions, effectively identify industrial defect targets of different scales and morphologies, and maintain high detection reliability even in scenarios with complex backgrounds and weak target contrast. Furthermore, this invention does not rely on large-scale manually labeled data. Model training and deployment can be completed with only 5 to 30 labeled instances per class, significantly reducing the data collection and labeling costs for industrial defect detection, improving the system's adaptability and practical value in real industrial production line environments, and providing reliable technical support for the automation and intelligence of industrial quality inspection.
[0187] Table 1 Ablation Experiment Results
[0188]
[0189] (2) Visualization of results analysis
[0190] like Figure 5 The image shows the feature activation visualization results of the method of the present invention on the CID test set. The visualization results show that:
[0191] (a) True bounding boxes in the original image: Showing the locations of defects marked in the cotton fabric image. There are multiple unevenly distributed defect targets in the image, with small defects having low contrast with the fabric background texture.
[0192] (b) EngineCAM activation distribution of the baseline model on samples: The activation heatmap of the baseline model exhibits a dispersed, low-contrast characteristic, with insufficient concentration of activation regions. In complex fabric texture backgrounds, the model generates a large number of spurious activations (i.e., false responses to the background texture), resulting in a lack of discriminative activity in the activation distribution. This indicates that the standard detector has limited ability to capture features of the target region when processing very few samples.
[0193] (c) Activation Distribution After Fusing FU and FR Modules: The method of this invention significantly improves activation concentration. The Uncertainty Encoding (FU) module, through random sampling and residual fusion in the feature space, enables the model to better explore and learn the feature representations of rare defects, reducing false responses to background textures. The Feature Storage (FR) module further enhances the concentration of the activation heatmap by improving the discriminative power of new class features. The fused activation distribution shows highly concentrated heatmap regions (red / yellow), with significantly improved overlap with the actual defect locations, and a significantly improved signal-to-noise ratio of the activation signal, verifying the superiority of the method of this invention in accurately locating subtle defects and effectively suppressing background spurious responses.
[0194] Comparative analysis shows that the combination of Feature Uncertainty Encoding (FU) and Feature Storage (FR) has a significant effect on enhancing feature discriminativeness and robustness, especially under conditions with very few samples and complex backgrounds. It can effectively improve the model's ability to focus on target features and ensure the accuracy and reliability of defect detection.
[0195] Summarize:
[0196] This invention addresses the practical technical challenges of detecting fabric defects in small sample sizes in the textile industry. Focusing on the core issues of high candidate box noise in complex texture backgrounds, unstable feature learning for new defect types, and insufficient detection accuracy and generalization ability, this invention proposes a small sample target detection method that integrates feature uncertainty encoding and feature storage on the Faster R-CNN architecture. This method achieves high-precision and robust detection of fabric defects in small sample scenarios, effectively solving the problem of poor transferability and adaptability of general small sample detection methods in textile industry scenarios.
[0197] The core technological improvements of this invention are reflected in two key stages of the detection process: In the candidate region generation stage, a feature uncertainty coding module is constructed in the region proposal network to model deterministic features as Gaussian probability distributions. Reparameterized sampling is used to achieve probabilistic representation of features, and KL divergence regularization constraints are used to avoid distribution degradation. At the same time, the sampled features are fused with the residuals of the original features, balancing feature exploration and stability, effectively reducing the pseudo-response interference of complex fabric textures, and improving the localization accuracy and quality of candidate boxes. In the feature learning stage, a feature repository with base class-new class partitioning is designed in the ROI head network. A dual-stream feature extraction architecture is constructed in combination with the momentum encoder. Stable features of the momentum encoder are used as positive samples, and historical outlier features in the repository are used as hard negative samples. Supervised contrastive loss is used to achieve cross-batch feature reuse and hard example mining, which solves the problem of easy drift and non-compact distribution of new class defect features under small sample conditions, and significantly enhances the inter-class discriminativeness and intra-class aggregation of features.
[0198] To adapt to industrial inspection scenarios, this invention constructs a dedicated cotton defect detection dataset CID, which characterizes the characteristics of dense target distribution and class imbalance in industrial scenarios. It also designs a phased training strategy of base class pre-training + small sample fine-tuning, fixing the backbone network parameters and updating only the detection-related modules. At the same time, it proposes a weighted joint optimization scheme of classification loss, regression loss, supervised comparison loss and KL divergence loss. The loss weights can be adaptively adjusted according to the number of new class samples (5-shot / 10-shot / 30-shot), which takes into account both the stability of model training and small sample adaptability.
[0199] Experimental results demonstrate that the proposed method significantly outperforms the baseline model on the CID dataset. With 5-shot, 10-shot, and 30-shot small sample configurations, the mAP50 index is improved by 4.776, 5.765, and 5.675 percentage points, respectively, compared to the standard Faster R-CNN, while the nAP50 index is improved by 4.719, 5.390, and 7.037 percentage points, respectively. Feature activation visualization results also prove that the proposed method effectively focuses on defect target features, significantly reduces spurious activations from background textures, and exhibits stronger anti-interference capabilities in detecting low-contrast, small-sized defects. Ablation experiments further verify the synergistic optimization effect of the feature uncertainty encoding module and the feature storage module; the fusion of the two modules has a significant positive effect on improving detection performance.
[0200] The technical solution of this invention combines theoretical innovation with engineering practicality. Its innovation lies in the synergy between explicit modeling of feature uncertainty and efficient reuse of cross-iteration features, breaking through the limitations of traditional small-sample detection methods that rely on deterministic feature representation and only utilize batch-based sample updates. At the same time, this invention does not rely on massive amounts of labeled data. Model training and deployment can be completed with only 5 to 30 labeled instances for each new type of defect, which greatly reduces the labeling cost and data collection difficulty of defect detection in the textile industry.
[0201] In practical applications, this invention can be directly adapted to the automated inspection needs of textile production lines, effectively improving the detection accuracy and generalization ability of fabric defects in small sample scenarios. It provides a new implementation idea and technical solution for small sample target detection technology in the field of industrial defect detection, and has broad application prospects in textile quality control, industrial automated inspection and other fields. At the same time, its design idea of feature uncertainty coding and partitioned feature storage can also be transferred to other industrial defect detection scenarios, and has good technical promotion value.
[0202] 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 feature uncertainty encoding and feature storage, characterized in that, The steps are as follows: S1: Obtain the dataset, preprocess the dataset, and divide the preprocessed dataset into base class training set, new class training set, validation set and test set; S2: Construct a small sample target detection network model; the small sample target detection network model includes a feature extraction network, a region proposal network, an ROI head network, and a detection head connected in sequence; the feature extraction network includes a backbone network and a feature enhancement module in sequence; the region proposal network includes a feature uncertainty encoding module; the ROI head network includes a feature storage module; S3: Use the base class training set, the new class training set, and the validation set to train and validate the small sample target detection network model to obtain the trained detection model; S4: Use the trained detection model to detect images in the test set and obtain target localization and classification results.
2. The method for detecting small-sample fabric defects based on feature uncertainty encoding and feature storage according to claim 1, characterized in that, When constructing a new class training set, a K-shot few-shot learning setting is used; The validation set and test set contain both base class targets and new class targets; It also includes data augmentation on the base class training set and the new class training set. Data augmentation includes: random horizontal flipping, random vertical flipping, random cropping, color dithering, and affine transformation of bounding box coordinates to maintain label consistency.
3. The method for detecting small-sample fabric defects based on feature uncertainty encoding and feature storage according to claim 1, characterized in that, The backbone network adopts the ResNet-101 backbone network to extract multi-scale deep features of fabric images in the dataset; The feature enhancement module employs the FPN module, which is used to enhance the multi-scale deep features extracted by the ResNet-101 backbone network to obtain a multi-scale feature pyramid.
4. The method for detecting small-sample fabric defects based on feature uncertainty encoding and feature storage according to claim 3, characterized in that, The region proposal network comprises, in sequence, a feature uncertainty coding module and an RPN Head; The feature uncertainty coding module is used to perform uncertainty coding on each scale feature in the multi-scale feature pyramid to obtain uncertainty features. The uncertainty coding models each scale feature in the multi-scale feature pyramid as a probability distribution and obtains uncertainty features through reparameterized sampling and residual fusion. The RPN Head takes the uncertainty features as input and obtains candidate regions through shared feature extraction and RPN dual-branch prediction.
5. The method for detecting small-sample fabric defects based on feature uncertainty encoding and feature storage according to claim 4, characterized in that, The ROI head network includes, in sequence: an ROI Pooling layer, an online encoder, a momentum encoder, and a feature storage module; The ROI Pooling layer extracts candidate region features from different scales of the feature pyramid based on the size of the candidate region area, and uses the ROIAlign method to map the candidate region features into a fixed-size ROI feature map. ; The online encoder uses ROI feature maps As input, dynamic feature representations are obtained through encoding operations. The parameters of the online encoder are updated through gradient backpropagation; The momentum encoder uses ROI feature maps As input, stable feature representations are obtained through encoding operations. The parameters of the momentum encoder are updated via an exponential moving average. The feature storage module is used to store stable feature representations of the momentum encoder output during each forward propagation. Perform quality screening and storage; The detection head uses a cosine similarity classifier when calculating the classification score.
6. The method for detecting small-sample fabric defects based on feature uncertainty encoding and feature storage according to claim 4 or 5, characterized in that, In the feature uncertainty encoding module, each scale feature in the multi-scale feature pyramid is modeled as a probability distribution. Uncertainty features are obtained through reparameterized sampling and residual fusion, including: St1, for each scale feature in the multi-scale feature pyramid The mean and log-variance of the distribution are predicted by two parallel lightweight encoders, respectively. Step 2: Based on the preset truncation interval, truncate the logarithmic variance and obtain the truncated logarithmic variance. ; St3, Introducing an adjustable scaling factor Calculate the standard deviation: ; St4. Sample noise from a standard normal distribution and generate probabilistic features through reparameterized sampling: , For noise sampled from a standard normal distribution, This indicates element-wise multiplication; St5, combine the sampled probability feature z with each scale feature Residual fusion is performed according to preset weights to obtain features after feature uncertainty encoding: , , To preset weights, and .
7. The method for detecting small-sample fabric defects based on feature uncertainty encoding and feature storage according to claim 5, characterized in that, The online encoder and the momentum encoder employ the same encoding operation: encoding the ROI feature map. Deep feature extraction and adaptive average pooling are performed sequentially on the three bottleneck residual blocks. Operations, fully connected layers, L2 normalization; Each bottleneck residual block includes a parallel backbone path and a skip connection branch. The backbone path consists of a 1×1 dimensionality reduction convolutional layer, a 3×3 spatial feature extraction convolutional layer, and a 1×1 dimensionality increase convolutional layer. The skip connection branch contains a 1×1 projection convolutional layer. The momentum encoder parameters are updated using an exponential moving average as follows: Where m is the momentum coefficient. These are the parameters for the online encoder.
8. The method for detecting small-sample fabric defects based on feature uncertainty encoding and feature storage according to claim 5 or 7, characterized in that, Stability characteristics of the momentum encoder output Quality screening and storage include: Quality screening: Representation of stable characteristics of momentum encoder output Perform quality screening: retain only foreground features that simultaneously satisfy the following conditions: classification confidence level greater than a preset confidence threshold and intersection-union ratio (IU) between candidate boxes and ground truth boxes greater than a preset IU threshold; Storage: Maintain a queue storage structure Q with a fixed capacity of K. Store the filtered stable feature representations into the queue storage structure Q. To address the scarcity of new class samples in small sample detection scenarios, the capacity K is divided into a base class storage area and a new class storage area. The capacity of the new class storage area is greater than that of the base class storage area, and a first-in-first-out cyclic covering mechanism is adopted.
9. The method for detecting small-sample fabric defects based on feature uncertainty encoding and feature storage according to claims 1-5 or 7, characterized in that, The training process includes: Base class pre-training stage: Using the base class training set as input, after forward propagation through the feature extraction network, region proposal network, ROI head network and detection head, the loss function of the base class pre-training stage is calculated; only the online encoder parameters are updated through backpropagation, and the momentum encoder parameters are frozen; the momentum encoder parameters are updated through exponential moving average. The small sample fine-tuning stage: K training samples are selected from the base class training set and the new class training set to form the training set for the small sample fine-tuning stage. The parameters of the feature extraction network are fixed. After forward propagation through the feature extraction network, region proposal network, ROI head network and detection head, the loss function of the small sample fine-tuning stage is calculated. Only the parameters of the online encoder are updated through backpropagation, and the momentum encoder parameters are frozen. The momentum encoder parameters are updated through exponential moving average. The loss function in the base class pre-training stage includes weighted cross-entropy classification loss, Smooth L1 regression loss, and KL divergence regularization loss of the feature uncertainty coding module; the cross-entropy classification loss includes the foreground-background classification loss of the RPN and the multi-class classification loss of the detection head; the Smooth L1 regression loss includes the bounding box regression loss of the RPN and the bounding box regression loss of the detection head. The loss function in the small sample fine-tuning stage is based on the loss function in the base class pre-training stage, with the addition of supervised contrast loss.
10. The method for detecting small-sample fabric defects based on feature uncertainty encoding and feature storage according to claim 9, characterized in that, The KL divergence regularization loss of the feature uncertainty encoding module is: Where H, W, and C are the height, width, and number of channels of the feature map, respectively. , These are the feature positions in channel c. The distribution mean and standard deviation; The supervised comparison loss is: in, The set of positive samples includes other samples of the same type as sample i in the same batch, and stable features generated by the momentum encoder for sample i. ; The negative sample set includes samples in the current batch that are dissimilar to sample i, and historical features in the queue storage structure that are dissimilar to sample i. , , Samples , , The normalization characteristics, For temperature coefficient, This represents the inner product, used to calculate the cosine similarity between two normalized features.