A Wood Surface Defect Detection Method Based on Multi-View Encoding and Feature Memory
By employing multi-view encoding and feature memory methods, the problems of data dependence and complex texture backgrounds in wood surface defect detection are solved, achieving efficient and stable defect detection and localization, adapting to diverse defect types, and reducing data preparation costs.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- QILU UNIVERSITY OF TECHNOLOGY (SHANDONG ACADEMY OF SCIENCES)
- Filing Date
- 2026-02-11
- Publication Date
- 2026-04-21
AI Technical Summary
Existing methods for detecting defects on wood surfaces rely on a large amount of labeled data, which is costly and difficult to adapt to diverse and unknown defect types. Traditional methods are not robust enough in complex texture backgrounds and are difficult to achieve stable and reliable detection.
A method based on multi-view encoding and feature memory is adopted. By constructing a contrastive multi-view encoder and an anomaly detection framework, a normal feature memory is built using a lightweight chromaticity energy gating module and a dual-branch multi-level feature fusion module. Combined with a cluster-guided greedy core set sampling algorithm, the detection and localization of defects on the wood surface are realized.
It reduces data preparation costs, adapts to complex textures and low-contrast defects, improves detection stability and positioning accuracy, accommodates diverse defect types, and outputs pixel-level anomaly heatmaps for easy quality inspection recording.
Smart Images

Figure CN121708008B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the technical field of image data processing, and more specifically, relates to a method for detecting surface defects in wood based on multi-view coding and feature memory. Background Technology
[0002] In daily production and life, wood is a very important environmentally friendly material, widely used in furniture manufacturing, construction and decoration, and packaging and transportation. In the panel manufacturing process, wood veneers are typically glued and pressed to form plywood, which is then further processed into various wood products. Veneers are prone to various surface defects during tree growth, rotary cutting, and subsequent processing, such as growth defects like dead knots, dry scars, and bark, as well as processing defects like notches, cracks, and burrs. These defects are widespread and complex in form, weakening the mechanical properties and structural stability of plywood to varying degrees, thus affecting the safety and economic value of the products.
[0003] To ensure the quality of plywood and its products, surface defect detection of wood veneers is typically required. Based on the detection results, the veneers are then graded and sorted to meet the quality requirements of different application scenarios. Traditional manual inspection methods and existing physical inspection methods struggle to achieve stable and reliable results while maintaining safety and efficiency. To improve inspection accuracy and automation, computer vision-based defect detection methods are gradually developing. While schemes based on manual features and traditional machine learning classifiers can achieve certain results in some scenarios, their feature design relies on experience, their robustness and generalization ability to complex textures are limited, and they struggle to adapt stably to significant variations in defect morphology.
[0004] In recent years, supervised detection methods based on deep learning have become the mainstream technology for wood defect detection. These mainly include two-stage target detection algorithms (such as Faster R-CNN and Cascade R-CNN) and single-stage target detection algorithms (such as the YOLO series and RetinaNet). For example, Chinese patent document CN117876663A discloses a wood defect detection method and system based on deep neural networks. This method includes the following steps: acquiring a wood defect dataset and dividing it into a training set, a validation set, and a test set; constructing an initial defect detection model based on the YOLOv8 model; training the initial defect detection model using the training set to obtain the final defect detection model; inputting the validation set into the final defect detection model and outputting the detection results. This detection method can improve the detection accuracy of small target defect samples in wood.
[0005] However, compared to traditional methods, the aforementioned supervised methods achieve significant improvements in detection accuracy and localization capability: two-stage methods typically possess high detection accuracy and strong localization capability, making them suitable for high-standard quality inspection and small defect identification scenarios; single-stage methods offer fast inference speed and flexible deployment, making them more likely to meet the real-time inspection needs of production lines. However, supervised inspection methods generally rely on large amounts of high-quality labeled data, resulting in high data collection and labeling costs, and subjective differences in labeling standards may affect model performance. Furthermore, when testing for defect types with few or no samples in the training set, the model is prone to missed or false detections, making it difficult to adapt to the diverse open-set detection requirements of wood defect types. Summary of the Invention
[0006] The present invention aims to overcome at least one of the defects of the prior art and provide a wood surface defect detection method based on multi-view coding and feature memory to solve the problems of diverse wood surface defect types, difficulty in covering abnormal samples, and high annotation costs.
[0007] The detailed technical solution of this invention is as follows:
[0008] A method for detecting surface defects in wood based on multi-view encoding and feature memory, the method comprising:
[0009] S1. Obtain the original wood dataset and preprocess the original wood images to convert them into Lab images;
[0010] S2. Construct a wood surface defect detection model and process the Lab image to obtain a core set of normal features, including:
[0011] A contrastive multi-view encoder was constructed, and positive and negative samples were constructed using the preprocessed Lab images to pre-train the encoder.
[0012] An anomaly detection framework is constructed, in which a pre-trained and frozen contrastive multi-view encoder is embedded as a feature encoding module for feature extraction. Based on the extracted features, a lightweight chroma energy gating module and a dual-branch multi-level feature fusion module are introduced to adapt and fuse features, resulting in a local feature set of normal samples and forming a normal feature memory. Subsequently, a cluster-guided greedy core set subset sampling algorithm is used to select the core set of normal features.
[0013] S3. After the anomaly detection framework is constructed and the feature core set is obtained, the wood surface image to be detected is input into the wood surface defect detection model to extract local features, and the nearest neighbor distance between each local feature and the features in the normal feature core set is calculated. Based on the nearest neighbor distance, an anomaly heatmap of the image to be detected is generated to achieve defect detection and pixel-level localization.
[0014] According to a preferred embodiment of the present invention, in step S1, the preprocessing of the original wood image specifically includes:
[0015] First, read the original wood image and its corresponding annotation information, and then perform sliding window cropping in the unmarked continuous normal area to obtain normal local image samples;
[0016] Then, the normal local images obtained by cropping are further filtered: the images are converted to the Lab color space, the L channel is taken as the structural information representation, and a discrete cosine transform frequency domain analysis is performed on the local window to obtain a comprehensive score; then, the quantile threshold of the comprehensive score of all samples is calculated and filtered accordingly, and the samples that pass the threshold are retained to construct the training set.
[0017] According to a preferred embodiment of the present invention, in step S2, the setting of positive and negative samples consists of two parts: the first part is global multi-view contrast learning, in which the L channel and ab channel of the Lab image are regarded as two views, the L view and ab view of the same image are positive samples, and the L view and ab view of different images are negative samples; the second part is local multi-view contrast learning, in which the local features of the L view are set as anchor points, the local features of the ab view from the same image and the same spatial location are taken as positive samples, and the local features of the ab view from different images or the same image but different spatial locations are taken as negative samples.
[0018] According to a preferred embodiment of the present invention, in step S2, constructing a contrastive multi-view encoder and pre-training the encoder using positive and negative samples constructed from the pre-processed Lab images specifically includes:
[0019] The contrastive multi-view encoder uses a dual-branch structure, with each branch having a parameter-independent half-channel width ResNet50 network. One branch processes the L-view, and the other branch processes the ab-view.
[0020] First, global multi-view comparison learning is performed: after the two branches extract features from the input Lab image, global average pooling is applied and the global feature vector is obtained through projection head mapping. and ;
[0021] Then, local multi-view contrastive learning is performed: after the conv3 output feature maps of the two branches, a dense projection head is introduced, treating each spatial location of the feature map of this layer as a local feature embedding, and projecting the channel dimension to 128 dimensions through a 1×1 convolution. Subsequently, batch normalization and activation functions are applied to normalize the vector at each location, thereby obtaining the local feature vector. and ;
[0022] Subsequently, the contrastive loss is calculated at both the global and local feature levels. The two contrastive losses are then summed using a weighted method to obtain the total loss. By continuously optimizing the total loss, the pre-trained encoder is obtained.
[0023] According to a preferred embodiment of the present invention, the contrast loss is specifically as follows:
[0024] When calculating the contrast loss between global feature vectors, a similarity function is defined as follows:
[0025]
[0026] in , , and Representing L-view respectively and ab view After two encoders and The extracted global feature representation, Represents two representation vectors and A similarity scoring function is used to measure the similarity between two representation vectors. and The degree of similarity between them; It is a hyperparameter that controls the similarity scale;
[0027] set up Next, an approximation is made using a binary classification discriminant form of noise contrast estimation to calculate the contrast loss between positive and negative samples in global contrastive learning:
[0028] First, we introduce a global normalization constant. and and will Divide by the corresponding or The obtained scale-calibrated matching score, the global normalization constant and The calculation method is as follows:
[0029]
[0030]
[0031] in It is the size of the training set. This is the size of the current batch. It is the number of negative samples. Indicates the number within the current batch The similarity score is calculated between the L-view features as anchor points and the ab-view features. This indicates that the L-view features are traversed within the current batch as anchor points, and then compared with... The similarity scores of the ab view features of each negative sample and one corresponding positive sample are calculated and then summed; Indicates the number within the current batch The similarity score is calculated between the features of the ab view and the features of the L view, using these features as anchor points. This means that within the current batch, the ab view features are traversed as anchor points, and the similarity scores are calculated and summed with the L view features of K negative samples and 1 corresponding positive sample.
[0032] When anchoring L-view features and calculating the contrast loss between them and ab feature samples, the loss is defined as follows: for:
[0033]
[0034] in When representing the anchored L-view feature, the first L-view features Compared to the positive sample ab view features in the contrast learning memory bank Similarity score between them This represents the approximate matching probability between positive samples after processing with a global normalization constant, using L-view features as anchor points. Indicates the first L-view features Compare the features of negative samples (ab-views) randomly sampled from the learning memory bank. Similarity score between them This represents the approximate matching probability between negative samples obtained after processing with a global normalization constant when using L-view features as anchor points. For batch size, The number of negative samples. Represents the probability distribution of noise. A reference intensity for noise;
[0035] When anchoring ab view features and calculating the contrast loss between them and L feature samples, define for:
[0036]
[0037] in When representing anchored ab view features, the first AB view features Compared to the positive sample L-view features in the contrast learning memory bank Similarity score between them This represents the approximate matching probability between positive samples after processing with a global normalization constant, using ab view features as anchor points. Indicates the first AB view features Compare the features of the negative L-view sample randomly sampled from the learning memory bank. Similarity score between them This represents the approximate matching probability between negative samples obtained after processing with a global normalization constant when using ab view features as anchor points.
[0038] When calculating the contrast loss between local feature vectors, a similarity scoring function is also set. As shown below:
[0039]
[0040] in and This represents the local features of two views obtained from the feature maps of the intermediate layers of the network. It is a hyperparameter that controls the similarity scale;
[0041] Loss function for local feature contrast learning The formula is:
[0042]
[0043] in This is a flattened representation of the local feature indices in the feature map. Indicates the first One sampled Index of local features of location, This represents the local features of the L-view, which serves as the anchor point. As a positive sample, and These are local features in the ab view of the same image and at the same spatial location. This represents a negative sample randomly sampled from all spatial locations of the ab feature map in the current batch. The number of negative samples. Represents the probability distribution of noise. Indicates batch size, and Indicates the spatial size of the feature map. The number of anchor points randomly sampled;
[0044] Finally, the total loss function is defined as:
[0045]
[0046] in, and These are the weight parameters.
[0047] According to a preferred embodiment of the present invention, in step S2, a lightweight chromaticity energy gating module and a dual-branch multi-level feature fusion module are introduced to perform feature adaptation and fusion, thereby obtaining a local feature set of normal samples and constructing a normal feature memory library. Specifically, this means:
[0048] A lightweight gating module based on chromaticity energy, comprising encoders and layer ab branch feature map It generates a weight map using its own chromaticity energy. The adjusted feature map is obtained by element-wise weighting of the feature maps of branches a and b. ,in, The calculation method is as follows:
[0049]
[0050] in This indicates element-wise multiplication; Indicates the weighted adjustment The layer feature map is used as the feature map for the ab branch in subsequent fusion and compression operations.
[0051] encoder and After the layer obtains the weighted feature map, in The layer obtains the corresponding feature map and the weighted feature map Next, the module proceeds to the dual-branch multi-level feature fusion module. First, within the same network layer, the two features are concatenated along the channel dimension to form a complete Lab fusion feature map. First, put the two layers Scale to the same space size, then apply to different levels. Cross-layer fusion and compression are performed. Specifically, Layer 2 is used as the target grid size, and the local feature grid of Layer 1 is aligned to the reference layer grid through bilinear interpolation. Then, the features of the two layers are spliced across the layers, and the spliced features are compressed to 1024 dimensions through adaptive pooling mapping to obtain the local feature set of normal samples. This local feature set is then used to construct the normal feature memory.
[0052] According to a preferred embodiment of the present invention, in step S2, a normal feature core set is selected by combining a cluster-guided greedy core set subset sampling algorithm, as follows:
[0053] The set of local features extracted and fused from all normal samples during the training phase of anomaly detection is as follows:
[0054]
[0055] in, For the number of local features, Indicates the first Local feature vectors, Indicates the feature dimension;
[0056] First, the original features Perform a linear projection to obtain Mapping dimension D to a smaller dimension d than the current dimension forms the projection space. and in the projection space Perform KMeans clustering on the above, with the objective function being to minimize the within-cluster squared error:
[0057]
[0058] in These are the cluster numbers obtained from clustering. Indicates the first The center of each cluster, It is the number of clusters obtained from clustering. It is the dimensionality reduction after projection. Local feature vectors, It is the first The cluster number to which each sample belongs. Then it is the first The center of the cluster to which each sample belongs;
[0059] Based on the results of feature clustering in the projection space, the sample indices are grouped according to their corresponding cluster numbers, denoted as the first... The set of sample indices corresponding to each cluster is:
[0060]
[0061] Let the total number of feature vectors in the core set obtained by the final subsampling be Then, the number of subsamples is allocated to each cluster according to the cluster size. And constrain each non-empty cluster to sample at least one vector:
[0062]
[0063] in This represents the cluster number traversal variable in the summation, used to calculate the number of samples across all clusters in the denominator. Accumulate;
[0064] After determining the clustering and quotas, a greedy approximation strategy is used to solve for the sub-core set. The details are as follows:
[0065] With the first Taking a cluster as an example, this embodiment first starts with the complete set of the projection space. Random selection The set of anchor points consists of several starting points:
[0066]
[0067] in Represents the first in the projection space Each low-dimensional feature is represented, and the initial distance from each point within the cluster to the set of anchor points is defined. Its average distance to the set of anchor points:
[0068]
[0069] in, Indicates the first Unselected clusters Vector distance The initial distance to the selected point;
[0070] Next, select suitable vectors from the unselected vectors and add them to the selected set. , No. The wheel in the The selected set within each cluster is and use Representing vectors The nearest distance to the currently selected set, then the first... The rule for selecting the vector in the round is:
[0071]
[0072] Next, use the newly selected vector to update the nearest distance of the remaining vectors within the cluster:
[0073]
[0074] In cluster Repeat the above steps until all selections are complete. 1 point, to obtain cluster Subcore set in projection space ;
[0075] Based on sub-core set The set of numbers The final cluster core set is obtained as follows:
[0076]
[0077] Finally, the core sets of each cluster are... By merging, we can obtain the total core set of normal features. .
[0078] According to a preferred embodiment of the present invention, the specific steps of S3 are as follows:
[0079] First, the feature vectors in the core set of normal features are constructed into a data structure that supports fast nearest neighbor retrieval;
[0080] During detection, local features are extracted from the input image and the same stitching and fusion processing as in the training phase is performed. Then, the nearest neighbor distance between each local feature and the feature vector in the core set is calculated, and the mean of its k nearest neighbor distances is used as the anomaly score of that local feature.
[0081] The anomaly scores of all local features are rearranged according to their spatial position in the feature map to obtain the corresponding local anomaly score grid.
[0082] The local anomaly fractional grid is upsampled to the original image resolution using bilinear interpolation, and then further smoothed using Gaussian smoothing to obtain a pixel-level anomaly heatmap of the image.
[0083] After obtaining the anomaly heatmap, on the one hand, the AUROC index can be calculated by combining the ground truth mask data to quantitatively evaluate the detection effect; on the other hand, based on the anomaly heatmap, the defect region contour can be extracted through thresholding and connected component analysis to achieve pixel-level localization and visual annotation of the defect region.
[0084] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0085] (1) This invention provides open set defect detection based on feature memory, which is naturally adapted to unknown defects. By constructing a local feature core set of normal samples, and using the nearest neighbor distance between the local feature to be tested and the core set as the basis for anomalies during the inference stage, the detection of defect types not seen during the training stage is realized, which meets the needs of open set scenarios with diverse and complex wood defects.
[0086] (2) This invention uses normal samples as the main learning object, eliminating the need to collect a large number of samples for each type of defect over a long period of time and perform detailed annotation. It can complete model deployment and iteration faster when the texture changes or the defect distribution changes in a new batch, reducing the threshold for the implementation of the quality inspection system, reducing data preparation costs, and adapting to the rapid online and model change of the production line.
[0087] (3) This invention improves positioning stability for complex textures and low-contrast defects. The natural texture of wood surface is strong and has many interferences, and defects such as burrs have low contrast and unclear boundaries. Traditional methods easily mistake texture for defects or cause contour drift. This invention makes the defect heat map more focused and the contour more closely fit by a more suitable characterization and positioning process for wood texture differences.
[0088] (4) The present invention uses lightweight chromaticity gating to suppress false detections due to chromaticity drift, making it more robust to production line illumination fluctuations and white balance drift. To address the feature instability problem caused by illumination changes and white balance drift, a weighted gating based on chromaticity energy is introduced to suppress unstable responses during feature utilization and localization, making the system more stable and reliable in continuous operation scenarios.
[0089] (5) The present invention can output pixel-level abnormal heat maps and defect contour superposition results, which can be used for subsequent quantitative evaluation, facilitate quick verification by on-site personnel, and can be linked with sorting equipment to form traceable quality inspection records. Attached Figure Description
[0090] Figure 1 This is a flowchart of the wood surface defect detection method based on multi-view encoding and feature memory as described in this invention.
[0091] Figure 2 This is a structural diagram of the wood surface defect detection model described in this embodiment of the invention.
[0092] Figure 3 This is a schematic diagram of the original wood image collected in an embodiment of the present invention.
[0093] Figure 4 This is a schematic diagram of a partial image obtained by cropping in an embodiment of the present invention.
[0094] Figure 5 This is a schematic diagram of the defect image and the corresponding truth mask in an embodiment of the present invention.
[0095] Figure 6 This is the training framework for the comparative multi-view encoder in this embodiment of the invention.
[0096] Figure 7 This is a schematic diagram of positive and negative samples for local feature comparison learning in an embodiment of the present invention.
[0097] Figure 8 This is a schematic diagram of the anomaly detection framework described in an embodiment of the present invention.
[0098] Figure 9 This is a comparison diagram between the contour marking results and the ground truth mask in an embodiment of the present invention.
[0099] Figure 10 This is a comparison diagram of the defect marker outline and the coarse marker data outline in an embodiment of the present invention.
[0100] Figure 11 This is a comparison chart of the number of defects detected in this embodiment of the invention with the number of defects detected in the coarse data. Detailed Implementation
[0101] The present disclosure will be further described below with reference to the accompanying drawings and embodiments.
[0102] It should be noted that the following detailed descriptions are exemplary and intended to provide further illustration of this disclosure. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this disclosure pertains.
[0103] Compared to supervised detection methods based on deep learning, anomaly detection-based techniques typically learn the normal appearance distribution using only normal samples during the training phase. In the inference phase, they determine defects by measuring the deviation of test samples from the normal distribution. This allows for defect detection and localization even when anomaly samples are scarce or difficult to cover, and also possesses a certain ability to detect unknown defects. Therefore, anomaly detection is more suitable for industrial scenarios where wood defects are diverse, defect samples are scarce, and their distribution changes frequently.
[0104] Therefore, to address the shortcomings of existing technologies, this invention proposes an innovative defect detection scheme that combines a self-supervised learning-based contrastive multi-view encoding technique with a feature memory-based anomaly detection framework. This invention constructs a feature encoder that considers both global and local representation capabilities through multi-view contrastive learning, and embeds this encoder into an anomaly detection framework built on PatchCore. During the inference phase, anomaly scores are obtained through feature distance measurement, enabling defect detection and localization. To further improve the matching and effectiveness of the encoder features and the feature memory construction process, this invention also designs a dual-branch multi-level feature fusion module, a lightweight chroma energy gating module, and a cluster-guided greedy core set subset sampling module to adapt and filter features, constructing a more representative feature core set.
[0105] The model of this invention obtains discriminative feature representations by comparing multi-view encoders and constructs a normal feature core set within an anomaly detection framework based on PatchCore. Then, during the inference phase, it obtains anomaly scores based on feature distance to achieve defect detection and localization. The anomaly detection framework used in this invention belongs to the feature embedding-based anomaly detection method. This type of method uses only normal samples during the training phase to learn the representation and distribution of normal appearance from the feature space level. During the inference phase, it extracts global or local features of the image to be inspected and obtains anomaly scores by measuring the degree of deviation between these features and the normal feature distribution, thereby achieving anomaly detection. Furthermore, when using local features for representation, a local anomaly score map corresponding to the feature space location can be obtained and upsampled back to the original image resolution to achieve pixel-level localization and segmentation of the abnormal region. Feature embedding-based anomaly detection methods can be constructed in various ways, such as teacher-student structures, single-class classification methods, and feature memory methods, among which PatchCore is a representative framework of feature memory methods.
[0106] The following further describes the wood surface defect detection method based on multi-view coding and feature memory bank of the present invention in conjunction with specific embodiments.
[0107] Example 1
[0108] Refer Figure 1 , this embodiment provides a wood surface defect detection method based on multi-view coding and feature memory bank, and the method includes:
[0109] S1. Obtain the original wood data set, preprocess the original wood images therein, and convert the original wood images into Lab images;
[0110] The data used in this embodiment comes from a self-built wood data set. All wood images are collected by a 25 million pixel industrial camera. During the collection process, an LED light source is used to supplement the light on the wood surface to ensure imaging consistency. The resolution of a single collected image is about 7000×3600 pixels, and more than 3000 original images are collected in total. Example images are as Figure 3 shown;
[0111] To achieve the detection and pixel-level positioning of wood surface defects, especially generate abnormal heat maps and mark defect areas for defects with low contrast (such as burrs), rather than just determining whether there are abnormalities in the image. For this reason, in the training stage of this embodiment, the normal local areas in the original wood images are used as the main training sample source. Therefore, the annotation information needs to be read one by one from the collected original wood images, avoiding the defect areas, and cropping local normal images from the unmarked normal continuous areas for the model to learn the normal appearance distribution to construct pure and representative training data. The annotation information refers to the information corresponding to the original wood images one by one, which is used to indicate the approximate position and category of the defects;
[0112] The specific cropping process is as follows: Read the original image and its corresponding annotation information, and perform sliding window cropping in the unmarked continuous normal area; discard it when the pixel size of the croppable area is less than 224×224, and when the croppable area is greater than or equal to 224×224, crop it with a 224×224 pixel window and a 200 pixel step length to obtain as many normal local image samples as possible. In addition to normal samples, this embodiment can also crop defect local images according to the defect positions and sizes in the annotation information and store them separately according to defect categories for subsequent testing and comparative evaluation. Example of the cropped local images is as Figure 4 shown.
[0113] Because some original wood images have white backgrounds around the wood edges, black borders are easily formed after imaging. These black borders may be mistakenly cropped into normal samples, affecting the encoder training quality. Therefore, this embodiment further filters the normal local images obtained from cropping: the images are converted to the Lab color space, the L (luminance) channel is used as the structural information representation, and Discrete Cosine Transform (DCT) frequency domain analysis is performed on the local window to comprehensively evaluate the energy intensity of mid-to-high frequency components such as texture and edges in the image, as well as their coverage in the image, to obtain a comprehensive score; then, quantile thresholds are calculated for all sample scores and filtered accordingly, retaining samples that pass the threshold for training set construction. Through the above filtering, samples containing black borders can be effectively removed, and samples with weak texture information and low learnability can be filtered out, thereby constructing a more stable normal training set. After cropping and filtering, this embodiment obtains approximately 500,000 normal local images of 224×224 pixels from approximately 3,000 original images as the training set.
[0114] S2, construct as follows Figure 2 The wood surface defect detection model shown processes Lab images to obtain a normal feature core set, including:
[0115] A contrastive multi-view encoder was constructed, and positive and negative samples were constructed using the preprocessed Lab images to pre-train the encoder.
[0116] An anomaly detection framework is constructed, in which a pre-trained and frozen contrastive multi-view encoder is embedded as a feature encoding module for feature extraction. Based on the extracted features, a lightweight chroma energy gating module and a dual-branch multi-level feature fusion module are introduced to adapt and fuse features, resulting in a local feature set of normal samples and forming a normal feature memory. Subsequently, a cluster-guided greedy core set subset sampling algorithm is used to select the core set of normal features.
[0117] In this embodiment, to obtain high-quality representations of wood surface features, a ResNet50 variant network is used as the feature extraction backbone. Self-supervised pre-training is performed in the Lab color space, combining global multi-view contrastive learning and intermediate layer local feature contrastive learning. After pre-training, the feature extractor parameters are frozen and embedded into the anomaly detection framework as a feature encoding module. During the feature extraction stage, local features from the L-view and ab-view are extracted from the intermediate layer of the encoder. These features are then adapted and fused using a dual-branch multi-level feature fusion module and a lightweight chromaticity energy gating module. Subsequently, a memory bank is constructed for the local features of normal samples, and a cluster-guided greedy core set subset sampling method is used to select representative features from the memory bank to form the core set.
[0118] Specifically, the first step is to build a comparison multi-view encoder:
[0119] This embodiment uses a dual-branch structure, with each branch having a parameter-independent half-channel width ResNet50 network for multi-view contrast learning in the Lab color space: one branch processes the L view, and the other branch processes the ab views. After extracting features from the input Lab image on each branch, these features are global average pooled and mapped to a 128-dimensional global feature vector through a projector, denoted as follows: and .
[0120] To obtain local features from the intermediate layer, this embodiment introduces a dense projection head after the conv3 output feature maps of the two-way network. Each spatial location of the feature map in this layer is treated as a local feature embedding, and the channel dimension is projected to 128 dimensions using a 1×1 convolution. This is followed by batch normalization and activation functions, and the vector at each location is normalized to obtain a 128-dimensional local feature vector. When an image is input to the encoder, in addition to obtaining the global representation... and In addition, there are local feature sets from the intermediate layers. and .
[0121] Because this embodiment adds intermediate layer local feature comparison learning on top of global feature comparison learning, the setting of positive and negative samples has two parts: The first part is basic global multi-view comparison learning, which treats the L channel and ab channel of the Lab image as two views. The L view and ab view of the same image are positive samples, and the L view and ab view of different images are negative samples. The second part is the newly added local multi-view comparison learning, which is performed on the local feature vectors extracted from the intermediate layer of the network. The local features of the L view are set as anchor points. Local features of the ab view from the same image and the same spatial location are taken as positive samples, and local features of the ab view from different images or the same image but different spatial locations are taken as negative samples. The positive and negative samples for local feature comparison learning are set as follows: Figure 7 As shown.
[0122] Furthermore, this embodiment calculates the contrastive loss at both the global and local feature levels, and then obtains the total loss by weighted summation of the two contrastive losses. By continuously optimizing the total loss, the encoder learns both global semantic information and local texture structure simultaneously, thereby improving the quality of wood surface representation. The training framework diagram of the contrastive learning encoder is shown below. Figure 6 As shown.
[0123] The contrastive loss function is as follows:
[0124] When calculating the contrast loss between global feature vectors, a function for evaluating similarity needs to be defined first, as shown below:
[0125]
[0126] in , , and Representing L-view respectively and ab view After two encoders and The extracted global feature representation, Represents two representation vectors and A similarity scoring function is used to measure the similarity between two representation vectors. and The degree of similarity between them; It is a hyperparameter that controls the similarity scale. The smaller the value, the more sensitive it is to similarity, thereby controlling the sensitivity of similarity.
[0127] set up Next, the contrastive loss between positive and negative samples in global contrastive learning needs to be calculated. However, when the training set is large, directly calculating the contrastive loss in the full softmax form is computationally expensive. Therefore, this embodiment uses a binary discriminant form of Noise Contrast Estimation (NCE) as an approximation. This is a method that approximates the full softmax distribution while reducing the computational burden. In the global contrastive learning of this embodiment, in order to correlate the model output with the noise distribution probability in the NCE binary discriminant form... Perform comparisons on the same scale, Scale calibration is performed by introducing a global normalization constant. and and will Divide by the corresponding or The obtained scale-calibrated matching score is used for subsequent discriminative loss calculation. Global normalization constant. and The calculation method is as follows:
[0128]
[0129]
[0130] in It is the size of the training set. This is the size of the current batch. It is the number of negative samples. Indicates the number within the current batch The similarity score is calculated between the L-view features as anchor points and the ab-view features. This indicates that the L-view features are traversed within the current batch as anchor points, and then compared with... The similarity scores of the ab view features of each negative sample and one corresponding positive sample are calculated and then summed; Indicates the number within the current batch The similarity score is calculated between the features of the ab view and the features of the L view, using these features as anchor points. This means that within the current batch, the features of the ab view are traversed as anchor points, and the similarity scores are calculated and summed with the L view features of K negative samples and 1 corresponding positive sample. In other words, within a batch, the similarity score between each feature and its positive and negative samples is calculated, the sum is then averaged, and finally multiplied by... This is used to approximate the sum of similarity scores for all samples in the training set. and The estimate is obtained from a batch of sampled candidates in the early stage of training and is used as a constant approximation for subsequent scale calibration.
[0131] This embodiment will... The output exponential similarity score divided by the normalization constant and The approximate matching probability is obtained and used to calculate the NCE discriminative loss. When anchoring the L-view feature and calculating the contrastive loss between it and the ab feature samples, the loss is defined. for:
[0132]
[0133] in When representing the anchored L-view feature, the first L-view features Compared to the positive sample ab view features in the contrast learning memory bank Similarity score between them This represents the approximate matching probability between positive samples after processing with a global normalization constant, using L-view features as anchor points. Indicates the first L-view features Compare the features of negative samples (ab-views) randomly sampled from the learning memory bank. Similarity score between them This represents the approximate matching probability between negative samples obtained after processing with a global normalization constant when using L-view features as anchor points. For batch size, The number of negative samples. Represents the probability distribution of noise. The first term represents the baseline intensity of the noise. The second term represents the discriminant for positive samples, which encourages the model to increase the matching score of positive samples during training. The third term represents the discriminant for negative samples, which encourages the model to decrease the matching score of negative samples, thereby bringing the representation of positive samples closer to the model and pushing the representation of negative samples further away.
[0134] Similarly, when anchoring ab view features and calculating the contrast loss between them and L feature samples, the following definition is used: for:
[0135]
[0136] in When representing anchored ab view features, the first AB view features Compared to the positive sample L-view features in the contrast learning memory bank Similarity score between them This represents the approximate matching probability between positive samples after processing with a global normalization constant, using ab view features as anchor points. Indicates the first AB view features Compare the features of the negative L-view sample randomly sampled from the learning memory bank. Similarity score between them This represents the approximate matching probability between negative samples obtained after processing with a global normalization constant when using ab view features as anchor points.
[0137] In addition to the global contrastive loss, this embodiment also performs local feature contrastive learning in the intermediate layers of the network, which also requires a similarity scoring function. This embodiment uses the same formula as before. As shown below.
[0138]
[0139] in and This represents the local features of two views obtained from the feature maps of the intermediate layers of the network. It is also a hyperparameter that controls the similarity scale, used to control the sensitivity to similarity. (Using...) The loss function for local feature comparison learning is calculated by setting similarity scores for local features. The formula is:
[0140]
[0141] in This is a flattened representation of the local feature indices in the feature map. Indicates the first One sampled Index of local features of location, This represents the local features of the L-view, which serves as the anchor point. As a positive sample, and These are local features in the ab view of the same image and at the same spatial location. This represents a negative sample randomly sampled from all spatial locations of the ab feature map in the current batch. The number of negative samples. Represents the probability distribution of noise. Indicates batch size, and Indicates the spatial size of the feature map. This represents the number of anchor points randomly sampled.
[0142] Finally, the total loss function is defined as:
[0143]
[0144] in and The weights are used to balance the impact of different loss terms on the training process, thereby controlling the learning intensity of the model. By continuously optimizing the total loss, the model's ability to represent both the global and local aspects of wood images is trained. When the total contrast loss no longer decreases, the model training is complete, and the parameters are frozen to prepare for embedding an anomaly detection framework.
[0145] However, a contrastive multi-view encoder alone is insufficient for wood surface defect detection; an anomaly detection framework is also needed to achieve defect detection and localization. This embodiment references the PatchCore framework to build an anomaly detection workflow for wood surface defect detection, embedding a pre-trained contrastive multi-view encoder with frozen parameters as a feature extractor, thereby achieving pixel-level detection and localization of wood surface defects. Since the contrastive multi-view encoder has a dual-branch structure, to make its output features more suitable for subsequent memory construction and distance measurement, this embodiment introduces a lightweight chromaticity energy gating module and a dual-branch multi-level feature fusion module in the feature extraction stage to obtain more stable and representative feature representations. The anomaly detection framework is as follows: Figure 8 As shown.
[0146] In production line scenarios, changes in lighting and camera white balance drift can cause unstable responses in some normal areas due to variations in the ab branch features. Directly using the ab features from the multi-view encoder to build the memory library in PatchCore can easily introduce false detections in low-contrast defect scenarios such as burrs, thus affecting the accuracy of defect location and contour positioning. Given the weak chromaticity difference between burr defects and the surrounding normal areas, the signal-to-noise ratio of the ab branch may decrease under chromaticity perturbation, leading to abnormally high responses in normal areas. Therefore, this embodiment proposes a lightweight gating module based on chromaticity energy, which separately... and layer ab branch feature map It generates a weight map using its own chromaticity energy. The adjusted feature map is obtained by element-wise weighting of the feature maps of branches a and b. ,in, ;
[0147] ab branch feature maps of encoder Layer 2 For example, a weight map is generated using its own chromaticity energy. The adjusted feature map is obtained by element-wise weighting of the features of branches a and b. In regions with weak chromaticity information, the weights of the ab features are reduced, while their weights are maintained in regions with significant chromaticity, thereby mitigating false detections caused by ab noise drift. The calculation method is as follows:
[0148]
[0149] in This indicates element-wise multiplication, which multiplies the original feature map and the weight map element-wise. It does not require additional convolutional layers and large-scale parameters, resulting in lower computational overhead. This represents the feature map of Layer 2 after weight adjustment. This feature map will be used as the feature map of the ab branch for subsequent fusion and compression operations.
[0150] Furthermore, to form a compact and representative local representation before entering the anomaly detection framework, this embodiment proposes a dual-branch multi-level feature fusion module. This module fuses and compresses the local features of the two branches before the features are input into the anomaly detection framework to build the memory, forming a more compact and information-rich joint local representation. This reduces the storage and retrieval overhead of the memory while maintaining expressive power. To balance semantic and textural information, this embodiment extracts local features from the output feature maps of the conv2 and conv3 stages of the two-branch network for memory construction. .
[0151] Specifically, suppose the input image, after passing through the two branches of the comparison multi-view encoder, in The layer obtains the corresponding feature map and the weighted feature map First, within the same network layer, the two are concatenated along the channel dimension to form a complete Lab fusion feature map. This allows for the complementary fusion of luminance structure and chrominance information while maintaining spatial alignment, providing a unified feature space for subsequent distance measurements in the memory database. The next step involves analyzing different levels of... For cross-layer fusion and compression, feature fusion requires concatenating the features from two layers along the channel dimension. Therefore, the two layers must first be concatenated. Scale to the same spatial size. To avoid a surge in the number of local features during alignment, this embodiment uses Layer 2 as the target mesh size and aligns the local feature mesh of Layer 1 to the reference layer mesh using bilinear interpolation. Then, the features of the two layers are spliced across layers, and the spliced features are compressed to 1024 dimensions using adaptive pooling mapping. In this way, while controlling the feature size, low-dimensional local features containing two view information, L and ab, and possessing both spatial details and semantic information are obtained.
[0152] After the aforementioned feature gating and fusion processing, a set of local features of normal samples can be obtained, forming a normal feature memory. However, directly using the entire memory for nearest neighbor distance retrieval would incur significant storage and computational overhead. Therefore, this embodiment proposes a cluster-guided greedy core set subset sampling algorithm, selecting a small number of representative features from the entire normal feature memory to form a core set, thereby reducing time and space costs. The algorithm first performs coarse-grained clustering of all local features to divide them into several clusters. Then, within each cluster, a greedy approximation strategy is used to solve for the sub-core set. Finally, the sub-core sets of each cluster are merged to obtain the total core set. This method ensures coverage of different normal patterns through clustering and guarantees representativeness through the core set strategy. Simultaneously, due to the limited number of samples within each cluster, the complexity of solving the core set within each cluster is significantly reduced, and the computational overhead of solving the core set is effectively reduced under the divide-and-conquer approach.
[0153] Specifically, let the set of local features of all normal samples extracted and fused during the training phase of anomaly detection be:
[0154]
[0155] in, For the number of local features, Indicates the first Local feature vectors, feature dimension =1024. To reduce the computational cost of clustering, this embodiment first processes the original features... Perform a linear projection to obtain , from dimension Map to smaller In this embodiment, the projection space is defined as a mapping from 1024 dimensions to 128 dimensions. and in the projection space Perform KMeans clustering on the above, with the objective function being to minimize the within-cluster squared error:
[0156]
[0157] in These are the cluster numbers obtained from clustering. Indicates the first The center of each cluster, It is the number of clusters obtained from clustering. It is the dimensionality reduction after projection. Local feature vectors, It is the first The cluster number to which each sample belongs. Then it is the first The center of the cluster to which each sample belongs. By minimizing the sum of squared distances from each sample to the cluster center, the clusters are made more compact and the similarity between samples in the same cluster is improved.
[0158] Based on the results of feature clustering in the projection space, the sample indices are grouped according to their corresponding cluster numbers, denoted as the first... The set of sample indices corresponding to each cluster is:
[0159]
[0160] Let the total number of feature vectors in the core set obtained by the final subsampling be . Then, the number of subsamples is allocated to each cluster according to the cluster size. And constrain each non-empty cluster to sample at least one vector:
[0161]
[0162] in This represents the cluster number traversal variable in the summation, used to calculate the number of samples across all clusters in the denominator. Accumulate;
[0163] This design ensures that large clusters obtain more representative vectors, while small clusters are not completely ignored, thus improving coverage of normal distributions across multiple patterns. After determining the clustering and quotas, a representative sub-core set is calculated within each cluster. Solve The common expression for this is minimizing the coverage radius, which means aiming to select representative points that can cover the feature space as much as possible, minimizing the distance from any point to the nearest representative point. Within each cluster, a separate sub-core set needs to be calculated. Directly solving the above problem of minimizing the coverage radius is usually costly. Therefore, the embodiment adopts a greedy approximation strategy, as follows:
[0164] With the first Taking a cluster as an example, this embodiment first starts with the complete set of the projection space. Random selection The set of anchor points consists of several starting points:
[0165]
[0166] in Represents the first in the projection space Each low-dimensional feature is represented, and the initial distance from each point within the cluster to the set of anchor points is defined. Its average distance to the set of anchor points:
[0167]
[0168] in, Indicates the first Unselected clusters Vector distance The initial distance to the selected points is used as the basis for determining the next step. Then, suitable vectors are continuously selected from the unselected vectors and added to the selected set. Assume the first The wheel in the The selected set within each cluster is and use Representing vectors The nearest distance to the currently selected set, then the first... The rule for selecting the vector in the round is:
[0169]
[0170] That is, select the vector that is furthest from the current set. Join The coverage of the core set is expanded, and then the nearest distance of the remaining vectors within the cluster is updated using the newly selected vector:
[0171]
[0172] In cluster Repeat the above steps until all selections are complete. 1 point, to obtain cluster Subcore set in projection space It is important to note that the point selection strategy within a cluster is based on the projected space. The selection was completed in the middle, but the selected objects essentially correspond to the set of numbers of the original samples. ,in Therefore, the final cluster core set is defined as follows:
[0173]
[0174] In other words, this embodiment solves for the core set of low-dimensional features in the low-dimensional space of the projection. Obtain the index of the selected vector, then return to the original space to index the selected local feature vector based on the index, and add it to the core set. middle, This is the core set required in this embodiment. Then, the core sets of each cluster... By merging, we can obtain the total core set of normal features. .
[0175] After obtaining the core set, during the testing phase, by calculating the nearest neighbor distance between the local features of the image under test and the feature vectors in the core set, it is possible to infer whether there are anomalies and the approximate location and outline of the anomalies.
[0176] S3. After the anomaly detection framework is constructed and the feature core set is obtained, the wood surface image to be detected is input into the wood surface defect detection model to extract local features, and the nearest neighbor distance between each local feature and the features in the normal feature core set is calculated. Based on the nearest neighbor distance, an anomaly heatmap of the image to be detected is generated to achieve defect detection and pixel-level localization, as follows:
[0177] First, the feature vectors in the normal feature core set are constructed into a data structure that supports fast nearest neighbor retrieval, enabling efficient calculation of distances between features during the inference phase. During detection, local features are extracted from the input image and subjected to the same stitching and fusion processing as during training. Then, the nearest neighbor distance between each local feature and the feature vectors in the core set is calculated, and the mean of its k nearest neighbor distances is used as the anomaly score for that local feature. This anomaly score reflects the degree of deviation of the local representation from the normal distribution; a higher score indicates a more likely anomaly in the local region. The anomaly scores of all local features are rearranged according to their spatial position in the feature map to obtain the corresponding local anomaly score grid. For image-level discrimination, this embodiment aggregates the local anomaly scores of a single image and takes the maximum value as the image-level anomaly score for that image. To obtain a pixel-level anomaly heatmap, this embodiment performs bilinear interpolation upsampling to the original image resolution on the local anomaly score grid and further performs Gaussian smoothing to obtain the anomaly heatmap of the image.
[0178] After obtaining the anomaly heatmap, on the one hand, it can be combined with the ground truth mask data to calculate indicators such as AUROC to quantitatively evaluate the detection effect; on the other hand, it can be based on the anomaly heatmap to achieve pixel-level localization and visual annotation of the defect area.
[0179] Specifically, a threshold t is set, and the abnormal heatmap is binarized: pixels with an abnormal score greater than t are identified as abnormal pixels, otherwise they are identified as normal pixels, thus obtaining a binary mask M. Then, connected component analysis is performed on mask M, aggregating abnormal pixels into several independent abnormal regions based on spatial connectivity. Excessively small noisy connected regions can be filtered out based on an area threshold. The contour boundaries of the retained regions are extracted to obtain their location information. Finally, the contours of the abnormal regions are superimposed onto the original image to achieve intuitive marking and localization of defective regions. The comparison between the contour marking results and the ground truth mask is shown in the figure. Figure 9 As shown.
[0180] The truth mask is created by selecting representative defect images from the test data, where defective areas are marked with 1 and normal areas with 0. A total of 10 types of defects are selected, such as... Figure 3 The defects shown include shallow burrs, coarse burrs, deep burrs, bark, dead knots, dry scars, rotten core, gaps, cracks, and bark. Twelve representative images are selected for each defect type, and a pixel-level ground truth mask is created for each image. An example of a defect image and its corresponding ground truth mask is shown below. Figure 5 As shown.
[0181] When presenting visualized inspection results, traditional target detection methods typically locate defects using bounding boxes. While this method can detect defects, the marked area is often significantly larger than the actual defect area, making it difficult to depict the fine boundary shape of the defect. When it is necessary to statistically analyze defect percentage, defect area, or perform refined sorting, estimating using the bounding box area introduces a non-negligible systematic bias. In contrast, outputting pixel-level anomaly heatmaps during the inference stage and extracting defect region contours through thresholding and connected component analysis enables pixel-level localization and visual labeling of defects. Figure 10 As shown, for irregularly shaped defects such as cracks and gaps, the target detection bounding box often covers a large area of normal regions. However, the contour boundary output by this embodiment can better match the actual shape of the defect, providing a more accurate spatial basis for subsequent defect area statistics and defect proportion assessment.
[0182] Furthermore, supervised learning-based target detection algorithms typically rely on the coverage of defect types and appearances in the training data. When faced with defect morphologies that are few in the training set or not included, they are prone to missed detections or false positives. For example, the labeled data used in the data preprocessing stage of this embodiment is generated by the target detection algorithm, and incomplete labeling or missed detections are common during labeling. In contrast, this embodiment uses an anomaly detection framework for defect detection. During the training stage, it only uses normal samples to learn the normal appearance distribution, and during the inference stage, it provides hints for potential defect areas based on anomaly responses. Therefore, when this embodiment is applied to other samples, it can generate higher anomaly responses in areas not marked by coarse labeling and form new defect candidate areas, thereby improving the defect detection rate and supplementing the discovery of some missed defects. For example... Figure 11 In the four sampled samples, the coarse label only detected 42 defects, while the method described in this embodiment detected 89 defects. Among them, 39 newly detected defect candidate areas were confirmed by manual review to be real defects. Therefore, the effective detection number is increased by about 92.9% compared with the coarse label.
[0183] This invention innovatively integrates multi-view encoding technology with the PatchCore anomaly detection framework and incorporates multiple feature processing modules for organic combination. This enables stable detection and localization of wood surface defects without relying on large amounts of labeled data, fundamentally avoiding dependence on labeled data. Furthermore, it has been specifically improved for wood data, exhibiting significantly superior performance in detecting low-contrast defects compared to existing anomaly detection models. This provides the wood processing industry with a low-cost and highly accurate defect detection solution.
[0184] Obviously, the above embodiments of the present invention are merely examples for clearly illustrating the technical solutions of the present invention, and are not intended to limit the specific implementation of the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the claims of the present invention should be included within the protection scope of the claims of the present invention.
Claims
1. A method for detecting surface defects in wood based on multi-view encoding and feature memory, characterized in that, The method includes: S1. Obtain the original wood dataset and preprocess the original wood images to convert them into Lab images; S2. Construct a wood surface defect detection model and process the Lab image to obtain a core set of normal features, including: A contrastive multi-view encoder was constructed, and positive and negative samples were constructed using the preprocessed Lab images to pre-train the encoder. The contrastive multi-view encoder uses a dual-branch structure, with each branch having a ResNet50 network with a parameter-independent half-channel width. One branch processes the L view, and the other branch processes the ab view. An anomaly detection framework is constructed, in which a pre-trained and frozen contrastive multi-view encoder is embedded as a feature encoding module for feature extraction. Based on the extracted features, a lightweight chroma energy gating module and a dual-branch multi-level feature fusion module are introduced to adapt and fuse features, resulting in a local feature set of normal samples and forming a normal feature memory. Subsequently, a cluster-guided greedy core set subset sampling algorithm is used to select the core set of normal features. In S2, a lightweight chromaticity energy gating module and a dual-branch multi-level feature fusion module are introduced to perform feature adaptation and fusion, obtaining a local feature set of normal samples and forming a normal feature memory library. Specifically, this means: A lightweight gating module based on chromaticity energy, comprising encoders and layer ab branch feature map It generates a weight map using its own chromaticity energy. The adjusted feature map is obtained by element-wise weighting of the feature maps of branches a and b. ,in, The calculation method is as follows: in This indicates element-wise multiplication; Indicates the weighted adjustment The layer feature map is used as the feature map for the ab branch in subsequent fusion and compression operations. encoder and After the layer obtains the weighted feature map, in The layer obtains the corresponding feature map and the weighted feature map Next, the module proceeds to the dual-branch multi-level feature fusion module. First, within the same network layer, the two features are concatenated along the channel dimension to form a complete Lab fusion feature map. First, put the two layers Scale to the same space size, then apply to different levels. Cross-layer fusion and compression are performed. Specifically, Layer 2 is used as the target grid size, and the local feature grid of Layer 1 is aligned to the reference layer grid through bilinear interpolation. Then, the features of the two layers are spliced across the layers, and the spliced features are compressed to 1024 dimensions through adaptive pooling mapping to obtain the local feature set of normal samples. This local feature set is then used to construct a normal feature memory. S3. After the anomaly detection framework is constructed and the feature core set is obtained, the wood surface image to be detected is input into the wood surface defect detection model to extract local features, and the nearest neighbor distance between each local feature and the features in the normal feature core set is calculated. Based on the nearest neighbor distance, an anomaly heatmap of the image to be detected is generated to achieve defect detection and pixel-level localization.
2. The method for detecting wood surface defects based on multi-view encoding and feature memory according to claim 1, characterized in that, In step S1, the original wood image is preprocessed, specifically including: First, read the original wood image and its corresponding annotation information, and then perform sliding window cropping in the unmarked continuous normal area to obtain normal local image samples; Then, the normal local images obtained by cropping are further filtered: the images are converted to the Lab color space, the L channel is taken as the structural information representation, and a discrete cosine transform frequency domain analysis is performed on the local window to obtain a comprehensive score; then, the quantile threshold of the comprehensive score of all samples is calculated and filtered accordingly, and the samples that pass the threshold are retained to construct the training set.
3. The method for detecting wood surface defects based on multi-view encoding and feature memory according to claim 1, characterized in that, In S2, the setting of positive and negative samples has two parts: the first part is global multi-view contrast learning, which regards the L channel and ab channel of the Lab image as two views, the L view and ab view of the same image as positive samples, and the L view and ab view of different images as negative samples; the second part is local multi-view contrast learning, which sets the local features of the L view as anchor points, takes the local features of the ab view from the same image and the same spatial location as positive samples, and takes the local features of the ab view from different images or the same image but different spatial locations as negative samples.
4. The method for detecting wood surface defects based on multi-view encoding and feature memory according to claim 3, characterized in that, In step S2, the construction of a contrastive multi-view encoder and the pre-training of the encoder using positive and negative samples constructed from the preprocessed Lab images specifically include: First, global multi-view comparison learning is performed: after the two branches extract features from the input Lab image, global average pooling is applied and the global feature vector is obtained through projection head mapping. and ; Then, local multi-view contrastive learning is performed: after the conv3 output feature maps of the two branches, a dense projection head is introduced, treating each spatial location of the feature map of this layer as a local feature embedding, and projecting the channel dimension to 128 dimensions through a 1×1 convolution. Subsequently, batch normalization and activation functions are applied to normalize the vector at each location, thereby obtaining the local feature vector. and ; Subsequently, the contrastive loss is calculated at both the global and local feature levels. The two contrastive losses are then summed using a weighted method to obtain the total loss. By continuously optimizing the total loss, the pre-trained encoder is obtained.
5. The method for detecting wood surface defects based on multi-view encoding and feature memory according to claim 4, characterized in that, The specific comparison loss is as follows: When calculating the contrast loss between global feature vectors, a similarity function is defined as follows: in , , and Representing L-view respectively and ab view After two encoders and The extracted global feature representation, Represents two representation vectors and A similarity scoring function is used to measure the similarity between two representation vectors. and The degree of similarity between them; It is a hyperparameter that controls the similarity scale; set up Next, an approximation is made using a binary classification discriminant form of noise contrast estimation to calculate the contrast loss between positive and negative samples in global contrastive learning: First, we introduce a global normalization constant. and and will Divide by the corresponding or The obtained scale-calibrated matching score, the global normalization constant and The calculation method is as follows: in It is the size of the training set. This is the size of the current batch. It is the number of negative samples. Indicates the number within the current batch The similarity score is calculated between the L-view features as anchor points and the ab-view features. This indicates that the L-view features are traversed within the current batch as anchor points, and then compared with... The similarity scores of the ab view features of each negative sample and one corresponding positive sample are calculated and then summed; Indicates the number within the current batch The similarity score is calculated between the features of the ab view and the features of the L view, using these features as anchor points. This means that within the current batch, the ab view features are traversed as anchor points, and the similarity scores are calculated and summed with the L view features of K negative samples and 1 corresponding positive sample. When anchoring L-view features and calculating the contrast loss between them and ab feature samples, the loss is defined as follows: for: in When representing the anchored L-view feature, the first L-view features Compared to the positive sample ab view features in the contrast learning memory bank Similarity score between them This represents the approximate matching probability between positive samples after processing with a global normalization constant, using L-view features as anchor points. Indicates the first L-view features Compare the features of negative samples (ab-views) randomly sampled from the learning memory bank. Similarity score between them This represents the approximate matching probability between negative samples obtained after processing with a global normalization constant when using L-view features as anchor points. For batch size, The number of negative samples. Represents the probability distribution of noise. A reference intensity for noise; When anchoring ab view features and calculating the contrast loss between them and L feature samples, define for: in When representing anchored ab view features, the first AB view features Compared to the positive sample L-view features in the contrast learning memory bank Similarity score between them This represents the approximate matching probability between positive samples after processing with a global normalization constant, using ab view features as anchor points. Indicates the first AB view features Compare the features of the negative L-view sample randomly sampled from the learning memory bank. Similarity score between them This represents the approximate matching probability between negative samples obtained after processing with a global normalization constant when using ab view features as anchor points. When calculating the contrast loss between local feature vectors, a similarity scoring function is also set. As shown below: in and This represents the local features of two views obtained from the feature maps of the intermediate layers of the network. It is a hyperparameter that controls the similarity scale; Loss function for local feature contrast learning The formula is: in This is a flattened representation of the local feature indices in the feature map. Indicates the first One sampled Index of local features of location, This represents the local features of the L-view, which serves as the anchor point. As a positive sample, and These are local features in the ab view of the same image and at the same spatial location. This represents a negative sample randomly sampled from all spatial locations of the ab feature map in the current batch. The number of negative samples. Represents the probability distribution of noise. Indicates batch size, and Indicates the spatial size of the feature map. The number of anchor points randomly sampled; Finally, the total loss function is defined as: in, and These are the weight parameters.
6. The method for detecting wood surface defects based on multi-view encoding and feature memory according to claim 1, characterized in that, In step S2, a cluster-guided greedy core set subset sampling algorithm is used to select a normal feature core set, as follows: The set of local features extracted and fused from all normal samples during the training phase of anomaly detection is as follows: in, For the number of local features, Indicates the first Local feature vectors, Indicates the feature dimension; First, the original features Perform a linear projection to obtain Mapping dimension D to a smaller dimension d than the current dimension forms the projection space. and in the projection space Perform KMeans clustering on the above, with the objective function being to minimize the within-cluster squared error: in These are the cluster numbers obtained from clustering. Indicates the first The center of each cluster, It is the number of clusters obtained from clustering. It is the dimensionality reduction after projection. Local feature vectors, It is the first The cluster number to which each sample belongs. Then it is the first The center of the cluster to which each sample belongs; Based on the results of feature clustering in the projection space, the sample indices are grouped according to their corresponding cluster numbers, denoted as the first... The set of sample indices corresponding to each cluster is: Let the total number of feature vectors in the core set obtained by the final subsampling be Then, the number of subsamples is allocated to each cluster according to the cluster size. And constrain each non-empty cluster to sample at least one vector: in This represents the cluster number traversal variable in the summation, used to calculate the number of samples across all clusters in the denominator. Accumulate; After determining the clustering and quotas, a greedy approximation strategy is used to solve for the sub-core set. The details are as follows: For the Each cluster, first from the complete set of the projection space. Random selection The set of anchor points consists of several starting points: in Represents the first in the projection space Each low-dimensional feature is represented, and the initial distance from each point within the cluster to the set of anchor points is defined. Its average distance to the set of anchor points: in, Indicates the first Unselected clusters Vector distance The initial distance to the selected point; Next, select suitable vectors from the unselected vectors and add them to the selected set. , No. The wheel in the The selected set within each cluster is and use Representing vectors The nearest distance to the currently selected set, then the first... The rule for selecting the vector in the round is: Next, use the newly selected vector to update the nearest distance of the remaining vectors within the cluster: In cluster Repeat the above steps until all selections are complete. 1 point, to obtain cluster Subcore set in projection space ; Based on sub-core set The set of numbers The final cluster core set is obtained as follows: Finally, the core sets of each cluster are... By merging, we can obtain the total core set of normal features. .
7. The method for detecting wood surface defects based on multi-view encoding and feature memory according to claim 6, characterized in that, The specific steps of S3 are as follows: First, the feature vectors in the core set of normal features are constructed into a data structure that supports fast nearest neighbor retrieval; During detection, local features are extracted from the input image and the same stitching and fusion processing as in the training phase is performed. Then, the nearest neighbor distance between each local feature and the feature vector in the core set is calculated, and the mean of its k nearest neighbor distances is used as the anomaly score of that local feature. The anomaly scores of all local features are rearranged according to their spatial position in the feature map to obtain the corresponding local anomaly score grid. The local anomaly fractional grid is upsampled to the original image resolution using bilinear interpolation, and then further smoothed using Gaussian smoothing to obtain a pixel-level anomaly heatmap of the image. After obtaining the anomaly heatmap, on the one hand, the AUROC index is calculated by combining the ground truth mask data to quantitatively evaluate the detection effect; on the other hand, based on the anomaly heatmap, the defect region contour is extracted through thresholding and connected component analysis to achieve pixel-level localization and visual annotation of the defect region.
Citation Information
Patent Citations
Wood defect detection method and system based on deep neural network
CN117876663A
Wood surface defect detection method and device based on multi-view anomaly detection
CN119991677A
Carbon fiber prepreg appearance defect detection method and system based on deep learning
CN121504862A