A method and apparatus for detecting surface anomalies in cables based on unsupervised algorithms.

By improving the ResNet50 network and using adaptive weighted convolution, combined with a domain knowledge learning module, the problems of low efficiency and insufficient robustness in cable inspection are solved, achieving efficient and accurate detection of cable surface anomalies.

CN121458639BActive Publication Date: 2026-07-31INSPUR QILU SOFTWARE IND
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
INSPUR QILU SOFTWARE IND
Filing Date
2025-10-27
Publication Date
2026-07-31

AI Technical Summary

Technical Problem

Existing cable inspection technologies suffer from low efficiency, high false negative rates, and strong subjectivity. Furthermore, unsupervised anomaly detection models are deficient in feature extraction and robustness, making it difficult to capture subtle defects on cable surfaces and adapt to the characteristics of the cable industry. They are also susceptible to noise.

Method used

An improved ResNet50 network is adopted, and Gaussian filtering, CLAHE algorithm and Otsu segmentation are combined for preprocessing. The model is pre-trained through a contrastive learning framework. Adaptive weighted convolution and domain knowledge learning modules are used to enhance feature extraction. A feature library is built and cosine similarity is calculated. The threshold is dynamically adjusted for anomaly segmentation.

Benefits of technology

It improves the efficiency and accuracy of cable inspection, enhances the ability to extract features of subtle defects in cables, improves the model's adaptability and noise robustness in the cable field, and reduces the false positive rate.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121458639B_ABST
    Figure CN121458639B_ABST
Patent Text Reader

Abstract

This invention relates to the field of computer vision and anomaly detection technology, specifically providing a method and apparatus for cable surface anomaly detection based on an unsupervised algorithm, comprising the following steps: S1, acquiring cable images, performing Gaussian filtering for noise reduction, CLAHE algorithm brightness normalization, Sobel edge extraction, and Otsu segmentation preprocessing; S2, improving ResNet50 and pre-training the model using a contrastive learning framework; S3, extracting defect-free cable features based on the pre-trained model, filtering for anomalies using patches, compressing the core set using a greedy algorithm, and constructing a feature library using FAISS; S4, preprocessing the image to be tested and extracting features, calculating the cosine similarity with the feature library to obtain patch-level anomaly scores; S5, dynamically adjusting the threshold based on the core set similarity, and combining dilatational erosion to achieve anomaly segmentation; S6, outputting the results according to the proportion of anomaly patches. Compared with existing technologies, this invention can improve the ability to identify subtle cable defects and enhance detection robustness, reduce annotation costs, and is suitable for industrial cable quality inspection.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer vision and anomaly detection technology, specifically providing a method and apparatus for detecting cable surface anomalies based on unsupervised algorithms. Background Technology

[0002] With the increasing level of industrial automation, cables, as core components for power transmission and signal connection, have an increasingly urgent need for quality inspection. Traditional cable inspection relies on manual visual inspection, which suffers from low efficiency, high missed detection rate, and strong subjectivity. Among existing automated inspection technologies, supervised learning methods require a large number of labeled samples, but cable defect samples are scarce and diverse, resulting in extremely high labeling costs. While unsupervised anomaly detection methods do not require defect samples, they still have the following shortcomings:

[0003] (1) Limited performance of backbone feature extraction network: Existing anomaly detection models based on general backbone networks (such as ResNet, ConvNext, etc.) have insufficient attention to local key features in their convolutional layers, making it difficult to capture feature information of subtle defects (such as scratches and bulges) on the cable surface, and their feature expression ability is weak.

[0004] (2) Lack of domain adaptability: The general unsupervised detection model does not incorporate knowledge of the cable domain (such as cable texture patterns and structural features), and has poor targeting for feature extraction of cable-specific anomalies (such as cable bulges, scratches, foreign objects, etc.).

[0005] (3) Insufficient robustness of the algorithm: When using a fixed threshold for anomaly detection, it is easily affected by image noise (such as light fluctuations, dirt on the cable surface, etc.), leading to false or missed anomalies and poor detection stability.

[0006] To address the aforementioned issues, there is an urgent need for an unsupervised anomaly detection technology that can enhance feature extraction capabilities, adapt to the cable industry, and exhibit strong noise resistance and robustness. Summary of the Invention

[0007] This invention addresses the shortcomings of the prior art by providing a highly practical method for detecting surface anomalies in cables based on an unsupervised algorithm.

[0008] A further technical objective of this invention is to provide a reasonably designed, safe, and applicable cable surface anomaly detection device based on an unsupervised algorithm.

[0009] The technical solution adopted by this invention to solve its technical problem is:

[0010] A method for detecting surface anomalies in cables based on an unsupervised algorithm, comprising the following steps:

[0011] S1. Acquire cable images, and preprocess them by Gaussian filtering for noise reduction, CLAHE algorithm brightness normalization, Sobel edge extraction and Otsu segmentation.

[0012] S2. Improve ResNet50 by using a contrastive learning framework for model pre-training;

[0013] S3. Based on the pre-trained model, extract the features of defect-free cables, filter them with abnormal patches, compress the greedy core set, and then use FAISS to build a feature library.

[0014] S4. Preprocess the image to be tested and extract features, calculate the cosine similarity with the feature library to obtain the patch-level anomaly score;

[0015] S5. Dynamically adjust the threshold based on the similarity of the core set, and combine it with dilatational erosion to achieve anomaly segmentation;

[0016] S6. Output the results based on the percentage of abnormal patches.

[0017] Furthermore, Gaussian filtering is used for image denoising;

[0018] The acquired RGB image is first converted to grayscale, and then Gaussian convolution is performed on the grayscale image.

[0019] The edge extraction:

[0020] For the grayscale image processed by CLAHE, the Sobel operator is used to calculate the gradient in the x and y directions to capture the direction and intensity of the edge. The gradient intensity of each pixel is judged along the gradient direction. If the gradient intensity of a pixel is not a local maximum in the gradient direction, the grayscale value is set to 0 to ensure that the edge width is 1 pixel.

[0021] The Otsu segment:

[0022] For the grayscale image after Canny edge detection, the Otsu algorithm is used to automatically determine the optimal segmentation threshold T_otsu by maximizing the inter-class variance between the foreground and background. Binarization is then used to automatically separate the cable region from the background, resulting in an image containing only the cable.

[0023] Furthermore, in step S2, ResNet50 is used as the backbone network, and the 3×3 convolution of ResNet50 is replaced with adaptive weight convolution to improve feature extraction capability; a domain knowledge learning module is added between layer1, layer2, layer3, and layer4 to enhance the model's adaptability to cable datasets.

[0024] The backbone model is trained using a contrastive learning framework with a dual-path architecture. The first path, the query encoder, uses an improved ResNet50 network, while the second path, the key encoder, has the same structure as the query encoder, and its parameters are updated via momentum.

[0025] For each input image, two different enhanced views are generated using an image enhancement strategy. Figure 1 View 2 is used for the query encoder and View 3 is used for the key encoder. They form positive sample pairs and negative sample pairs with other sample images in the same batch. After the image is processed by two-way encoding feature extraction, the feature vector is obtained and input into the InfoNCE loss function to calculate the loss.

[0026] The InfoNCE loss function is fine-tuned, and auxiliary constraints are added. The loss function formula is as follows:

[0027]

[0028] Where q is the feature vector extracted from the query, and k + , is the feature vector of the positive sample key. τ is the feature vector of the negative sample key, M is the number of negative samples, and τ is the temperature.

[0029] With the addition of a lightweight regularization term, the total loss function is:

[0030] Loss total =Loss infoNCE +λ1×|γ pos -1.0|+λ2×(max(0,0.6-φ scale_min )+max(0,φ scale_max -1.4))

[0031] λ1 is set to 0.01 to constrain the global scaling factor to be close to the initial value and avoid excessive deviation in the determination of positive weights. λ2 is set to 0.005 to ensure that the scaling range is stable between 0.6 and 1.4 and avoid excessive scaling of weights.

[0032] Furthermore, the adaptive weighted convolution includes:

[0033] (1) Initialize the convolutional kernel and learnable parameters;

[0034] Configure a fixed basic convolution kernel for a 3×3 convolution kernel C in For the number of input channels, C out To determine the number of output channels, the basic convolutional kernels are initialized using the Xavier method, which adapts the initial weight allocation to the cable image features.

[0035] Initialize positive weights to determine learnable parameters γpos With θ pos ,γ pos Initialize the global scaling factor to 1, θ pos Initialize the channel offset to 0;

[0036] Initialize the weight adjustment factor and learnable parameter φ prob φ scale_min_raw φ δ , where φ prob Used to control the probability of adjusting the positive mask, initialized to logit(0.3), and mapped to [0,1] after Sigmoid activation; φ scale_min_raw To minimize the scaling factor, it is initialized to ln(exp(0.8)-1), and φ is obtained through softplus. scale_min ;φ δ The scaling range difference parameter is initialized to ln(exp(0.4)-1), and after softplus, Δ is obtained. scale Finally, the maximum scaling φ is obtained. scale_max =φ scale_min +Δ scale ;

[0037] (2) Generate positive weight mask and adjustment factor;

[0038] Dynamically generate positive weight masks based on learnable parameters:

[0039]

[0040] Where σ is the Sigmoid function, γ pos W is the scaling factor. base For convolution kernel parameters, Indicates θ pos The dimension is expanded to W base The dimension adjusts the basic weights through learnable parameters, and then obtains a soft mask through Sigmoid activation. The closer the value is to 1, the more likely the corresponding weight is to be judged as a "positive weight" and participate in the adjustment.

[0041] (3) Positive weight adaptive adjustment;

[0042] W adj =W base

[0043] ×[1+(scale_factor-1)×prob_mask

[0044] ×positive_mask]

[0045] Among them W baseThe weights are the convolution kernel weights, and the scale_factor is the scaling factor, which is determined by the learnable parameter φ. scale_min With φ scale_max The sampling is determined, and the mask that needs to be adjusted in the positive weight coefficients of prob_mask is W. base A mask for determining whether a weight is positive among the weight coefficients;

[0046] γ pos θ pos φ prob φ scale_min_raw φ δ Both are learnable parameters, and W base We optimize together using backpropagation of the loss function.

[0047] Furthermore, the domain knowledge learning module includes:

[0048] (1) Assess the importance of features;

[0049] The features of the i-th layer of the model are:

[0050]

[0051] C i H represents the number of channels in the i-th layer. i W i Spatial dimension;

[0052] Flatten the features into Eliminate the interference of spatial structure on feature weights, and normalize the features using the following formula:

[0053]

[0054] in This represents the maximum value of the feature vector, used to avoid numerical overflow and ensure that the weights focus on the features most important to the domain. The final weights... Represents the relevance score of the feature domain corresponding to each element;

[0055] (2) Global feature information fusion;

[0056] Calculate the global average feature of the model at layer i:

[0057]

[0058] H i W i For spatial dimensions, F i For the features of the i-th layer, a learnable parameter θ is introduced to fuse global information with domain feature weights:

[0059]

[0060] Ensure that the weights include information about locally important features while also being guided by global domain characteristics, so as to avoid local noisy features being misjudged as domain-related.

[0061] Furthermore, in step S3, patch-level local features are extracted using the intermediate layers of the improved ResNet50 pre-trained model to enhance the feature response to subtle defects in the cable. First, the normal image size is adjusted to 224×224 pixels and normalized. Second, the normalized image is input into the backbone network, and multi-scale feature fusion is performed using the network's multi-layer features to enhance the model's ability to identify multi-scale defects. The feature vector size of layer 2 is F. layer2 ∈R 512×28×28 The feature vector size of layer 3 is F. layer3 ∈R 1024×14×14 The feature vector size of layer 4 is F. layer4 ∈R 2048×7×7 The feature vectors of layers 3 and 4 are upsampled to 28×28 and aligned with layer 2. Then, these three feature layers are stacked, and an adaptive pooling algorithm is used to project the features to 1024 dimensions, resulting in the feature vector F. patch ={f1,f2,…,f 784}, where f i ∈R 1024 ;

[0062] Each image generates multiple 1024-dimensional feature vectors. To improve the performance of the inference stage, the feature vectors need to be compressed.

[0063] First, perform anomaly patch vector filtering for F. patch For each patch feature, calculate the average cosine distance d between its k nearest neighbors in the feature space. avg Set an abnormal threshold in The average distance is the mean. Let d be the standard deviation. avg >d th The patch was identified as an abnormal patch and its feature vectors were removed, resulting in the processed vector set F. valid ;

[0064] Secondly, a greedy core set selection algorithm is used to select a small number of representative patch features from the filtered vector set, which greatly reduces the storage requirements while preserving the diversity of the feature space. The feature selection is performed using the following formula:

[0065]

[0066] Where d cos Let K be the cosine distance between vectors, and K be the size of the core set.

[0067] Randomly select one patch feature as the initial core set C0 = {c1}, and apply it to F. valid For each unselected feature f in the core set, calculate the minimum cosine distance with the current core set, and select the feature f with the furthest distance to add it to the core set C. t+1 Repeat this step until the core set size reaches K, resulting in the final core set C. K ;

[0068] Using the IndexFlatIP index type of the FAISS vector library, the 1024-dimensional feature vector of the core set C is L2 normalized and then added to the FAISS vector library.

[0069] Furthermore, in step S4, the image to be detected is first preprocessed by scaling its size to 224×224 and normalizing it. Then, the image is input into the ResNet50 model pre-trained in step S2 for feature extraction. The extracted intermediate layer features are then fused to obtain a feature vector set F with a shape of 784×1024. test Calculate F test Each vector in the core set C K The cosine similarity score obtained by performing nearest neighbor retrieval is the anomaly score.

[0070] For F test For each patch in the set, the maximum similarity and average similarity with the core set are calculated. The maximum similarity reflects the highest degree of matching between the patch and normal features; the closer the value is to 1, the more normal the patch is. The average similarity avoids misjudgments caused by mismatches of a single core feature. As a supplement to the maximum similarity, based on the logic that the lower the similarity, the higher the degree of abnormality, a patch-level abnormality score S is defined. test,i :

[0071] S test,i =1-α×Sim test,i,max -(1-α)×Sim test,i,avg

[0072] Where α is an adjustable parameter; the closer the value is to 1, the higher the degree of abnormality; the closer the value is to 0, the closer it is to normal. test,i,max Sim represents the maximum similarity. test,i,avg Indicates average similarity;

[0073] The feature map was restored to 224×224 using bilinear interpolation.

[0074] Furthermore, in step S5, anomaly segmentation is performed based on the anomaly score matrix in step S4, with those scores greater than a threshold being the abnormal part and those scores less than a threshold being the normal part.

[0075] First, we need to determine the threshold; the mean similarity within the core set is μ. core,sim The standard deviation is σ core,sim The lower limit of similarity between the core set and the normal sample patch (Sim) core,low for:

[0076] Sim core,low =μ core,sim -3σ core,sim

[0077] Upper limit S of patch-level anomaly score for normal samples core,high for:

[0078] S core,high =1-α×Sim core,low -(1-α)×(μ core,sim -2σ core,sim ), where α∈(0,1)

[0079] The threshold is dynamically adjusted according to the following formula:

[0080] T patch =S core,high +β×σ core,sim

[0081] Where β∈(0,1) is used to control the effect of variance on the threshold, for S test,i >T patch The pixel value of the abnormal patch block is set to 255, while the pixel value of other areas is set to 0, thereby generating an abnormal segmentation map. The segmentation map is then subjected to dilation and erosion to eliminate isolated points and avoid misjudgment.

[0082] Furthermore, in step S6, based on the anomaly segmentation map obtained in step S5, if the proportion of the number of anomaly patches exceeds a certain number, the image is recorded as an anomaly image, and the anomaly segmentation map is output for locating the anomaly, thereby completing the anomaly detection.

[0083] A cable surface anomaly detection device based on an unsupervised algorithm, characterized in that it comprises: at least one memory and at least one processor;

[0084] The at least one memory is used to store a machine-readable program;

[0085] The at least one processor is configured to call the machine-readable program to execute a cable surface anomaly detection method based on an unsupervised algorithm.

[0086] Compared with existing technologies, the cable surface anomaly detection method and apparatus based on unsupervised algorithms of the present invention have the following outstanding advantages:

[0087] This invention solves the problems of low efficiency, high false negative rate, and strong subjectivity in manual inspection, thereby improving the efficiency and accuracy of cable inspection. By setting different weight parameters for different convolution kernels, the invention achieves adaptability to changes in input features by controlling the activation intensity of positive weights without introducing complex calculations, while avoiding the disruption of feature suppression logic caused by negative weight perturbations, thus enhancing the ability to extract key defect features.

[0088] This addresses the challenge of traditional methods failing to capture key features of a target domain due to significant differences in feature distribution across various domains. By guiding the model to selectively extract key domain-specific features, the model's feature extraction capability in specific domains is improved. Attached Figure Description

[0089] 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 some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0090] Figure 1 This is a flowchart illustrating a cable surface anomaly detection method based on an unsupervised algorithm. Detailed Implementation

[0091] To enable those skilled in the art to better understand the present invention, the present invention will be further described in detail below with reference to specific embodiments. Obviously, the described embodiments are merely 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.

[0092] The following is a preferred embodiment:

[0093] like Figure 1 As shown in this embodiment, a cable surface anomaly detection method based on an unsupervised algorithm has the following steps:

[0094] S1. Acquire cable images, and preprocess them by Gaussian filtering for noise reduction, CLAHE algorithm brightness normalization, Sobel edge extraction and Otsu segmentation.

[0095] To ensure high-quality acquisition and feature preservation of cable images, an "industrial-grade hardware configuration + phased adaptive preprocessing" solution is adopted, with the specific steps as follows:

[0096] An industrial camera combined with a custom optical system is used to achieve full surface coverage of the cable, avoiding feature loss due to shooting angle and lighting. The industrial camera is a high-definition 2-megapixel camera operating at 50fps, outputting an image resolution of 1920×1080. The lens is a 16mm fixed-focus industrial lens with a depth of field of 20-40cm, suitable for large-diameter cables. A custom ring light source with a 45-degree angle is used to enhance shadow contrast in defective areas.

[0097] (1) Use Gaussian filtering for image denoising;

[0098] To address high-frequency noise in industrial environments, Gaussian filtering is employed for adaptive denoising, balancing noise suppression with defect detail preservation. Specifically, the acquired RGB images are first converted to grayscale, and then Gaussian convolution is performed on the grayscale images.

[0099] (2) Use the CLAHE algorithm for brightness normalization;

[0100] To address the issue of uneven lighting caused by reflections on cable surfaces, the CLAHE algorithm is used for brightness equalization, preventing localized areas of the image from being too bright or too dark.

[0101] (3) Edge extraction;

[0102] For the grayscale image processed by CLAHE, the Sobel operator is used to calculate the gradients in the x-direction (cable axis) and y-direction (cable radial direction) to capture the direction and intensity of the edges. The gradient intensity of each pixel is judged along the gradient direction. If the gradient intensity of a pixel is not a local maximum in its gradient direction, its grayscale value is set to 0 (edge ​​thinning) to ensure that the edge width is 1 pixel.

[0103] (4) Image segmentation;

[0104] For the grayscale image after Canny edge detection, the Otsu algorithm is used to automatically determine the optimal segmentation threshold T_otsu by maximizing the inter-class variance between the foreground (cable) and the background. Binarization is then used to automatically separate the cable region from the background, resulting in an image containing only the cable.

[0105] S2. Improve ResNet50 by using a contrastive learning framework for model pre-training;

[0106] Using ResNet50 as the backbone network, the 3×3 convolutions of ResNet50 are replaced with adaptive weighted convolutions to improve feature extraction capabilities; a domain knowledge learning module is added between layer1, layer2, layer3, and layer4 to enhance the model's adaptability to cable datasets.

[0107] The backbone model is trained using a contrastive learning framework with a dual-path architecture. The first path, the query encoder, uses the improved ResNet50 network described above, with an input image size of 224×224. The second path, the key encoder, has the same structure as the query encoder, and its parameters are updated via momentum with a momentum coefficient of 0.996.

[0108] For each input image, two different enhanced views are generated using an image enhancement strategy. Figure 1 View 1 is used for the query encoder, and View 2 is used for the key encoder, forming positive sample pairs and negative sample pairs with other sample images in the same batch. After feature extraction through two-way encoding, the image yields a 2048-dimensional feature vector, which is then input into the InfoNCE loss function to calculate the loss.

[0109] To improve the discriminative power and training stability of cable features, the InfoNCE loss function was fine-tuned and auxiliary constraints were added. The loss function formula is as follows:

[0110]

[0111] Where q is the feature vector extracted from the query, and k + , is the feature vector of the positive sample key. Let M be the feature vector of the negative sample key, M be the number of negative samples, and τ be the temperature. To enhance feature discrimination, the initial temperature value is set to 0.1, and then adjusted to 0.12 after 100 training rounds.

[0112] To avoid feature logic corruption caused by excessively large updates to learnable parameters, a lightweight regularization term is added. Therefore, the total loss function is:

[0113] Loss total =Loss infoNCE +λ1×|γ pos -1.0|+λ2

[0114] ×(max(0,0.6-φ scale_min )+max(0,φ scale_max -1.4))

[0115] λ1 is set to 0.01 to constrain the global scaling factor to be close to the initial value and avoid excessive deviation in the determination of positive weights. λ2 is set to 0.005 to ensure that the scaling range is stable between 0.6 and 1.4 and avoid excessive scaling of weights.

[0116] S3. Based on the pre-trained model, extract the features of defect-free cables, filter them with abnormal patches, compress the greedy core set, and then use FAISS to build a feature library.

[0117] Patch-level local features are extracted using intermediate layers of an improved ResNet50 pre-trained model to enhance the feature response to subtle defects in cables. First, the normal image size is adjusted to 224×224 pixels and normalized. Then, the normalized image is input into the backbone network, and multi-layer features (e.g., layer 2, layer 3, layer 4) are used for multi-scale feature fusion to enhance the model's ability to identify multi-scale defects. The feature vector size of layer 2 is F. layer2 ∈R 512 ×28×28 The feature vector size of layer 3 is F. layer3 ∈R 1024×14×14 The feature vector size of layer 4 is F. layer4 ∈R 2048 ×7×7 The feature vectors of layers 3 and 4 are upsampled to 28×28 and aligned with layer 2. Then, these three feature layers are stacked, and an adaptive pooling algorithm is used to project the features to 1024 dimensions, resulting in the feature vector F. patch ={f1,f2,…,f 784}, where f i ∈R 1024 .

[0118] After feature extraction in the previous step, each image generates 784 1024-dimensional feature vectors. If the dataset contains 500 normal images, approximately 390,000 feature vectors will be generated. To improve the performance of the inference stage, these 390,000 feature vectors need to be compressed.

[0119] First, perform anomaly patch vector filtering on F. patch For each patch feature, calculate the average cosine distance d between its k nearest neighbors (k=20) in the feature space. avg Set an abnormal threshold in The average distance is the mean. Let d be the standard deviation. avg >d th The patch was identified as an abnormal patch and its feature vector was removed, resulting in the processed vector set F. valid .

[0120] Secondly, a greedy core set selection algorithm is used to select a small number of representative patch features from the filtered vector set, significantly reducing storage requirements while preserving the diversity of the feature space. Feature selection is performed using the following formula:

[0121]

[0122] Where d cosLet K be the cosine distance between vectors, and K be the size of the core set.

[0123] Randomly select one patch feature as the initial core set C0 = {c1}, and apply it to F. valid For each unselected feature f in the core set, calculate its minimum cosine distance to the current core set, and select the feature f with the furthest distance to add it to the core set C. t+1 Repeat this step until the core set size reaches K, resulting in the final core set C. K .

[0124] Using the IndexFlatIP index type of the FAISS vector library, the 1024-dimensional feature vector of the core set C is L2 normalized and then added to the FAISS vector library.

[0125] S4. Preprocess the image to be tested and extract features, calculate the cosine similarity with the feature library to obtain the patch-level anomaly score;

[0126] First, the image to be detected is preprocessed by scaling it to 224×224 and normalizing it. Then, the image is input into the ResNet50 model pre-trained in step S2 for feature extraction. After feature fusion of the extracted intermediate layer features, a feature vector set F with a shape of 784×1024 is obtained. test Calculate F test Each vector in the core set C K Perform nearest neighbor retrieval with the number of neighbors set to 5, and the resulting cosine similarity score is the anomaly score.

[0127] For F test For each patch in the core set, its maximum similarity and average similarity are calculated. Maximum similarity reflects the highest degree of matching between the patch and normal features; the closer the value is to 1, the more likely it is to be a normal patch. Average similarity serves as a supplement to maximum similarity, avoiding misjudgments caused by mismatches of a single core feature. Based on the logic that lower similarity indicates a higher degree of anomaly, a patch-level anomaly score S is defined. test,i :

[0128] S test,i =1-α×Sim test,i,max -(1-α)×Sim test,i,avg

[0129] Here, α is an adjustable parameter. The closer the value is to 1, the higher the degree of abnormality; the closer the value is to 0, the closer it is to normal.

[0130] Since the feature map corresponding to the patch-level anomaly score is 28×28, bilinear interpolation is used to restore the feature map to 224×224.

[0131] S5. Dynamically adjust the threshold based on the similarity of the core set, and combine it with dilatational erosion to achieve anomaly segmentation;

[0132] Based on the abnormal score matrix in step S4, abnormal segments are divided, with those exceeding the threshold being the abnormal part and those below the threshold being the normal part.

[0133] First, we need to determine the threshold; the mean similarity within the core set is μ. core,sim The standard deviation is σ core,sim The lower limit of similarity between the core set and the normal sample patch is:

[0134] Sim core,low =μ core,sim -3σ core,sim

[0135] The upper limit of patch-level anomaly scores for normal samples is:

[0136] S core,high =1-α×Sim core,low -(1-α)×(μ core,sim -2σ core,sim ), where α∈(0,1)

[0137] The threshold is dynamically adjusted according to the following formula:

[0138] T patch =S core,high +β×σ core,sim

[0139] Where β∈(0,1), for S test,i >T patch The pixel value of the abnormal patch block is set to 255, while the pixel value of other areas is set to 0, thereby generating an abnormal segmentation map. The segmentation map is then subjected to dilation and erosion to eliminate isolated points and avoid misjudgment.

[0140] S6. Output the results based on the percentage of abnormal patches;

[0141] Based on the anomaly segmentation map obtained in step S5, if the number of anomaly patches accounts for more than 5%, the image is recorded as an anomaly image, and the anomaly segmentation map is output to locate the anomaly, thereby completing the anomaly detection.

[0142] The adaptive weighted convolution includes:

[0143] By setting different weight parameters for different convolutional kernels, the ability to adapt to changes in input features is achieved by controlling the activation intensity of positive weights without introducing complex calculations. At the same time, the feature suppression logic is not disrupted by negative weight perturbations, thus enhancing the ability to extract key defect features. The specific structure and implementation steps are as follows:

[0144] (1) Initialize the convolutional kernel and learnable parameters;

[0145] Configure a fixed basic convolution kernel for a 3×3 convolution kernel (C in For the number of input channels, C out (For the number of output channels), the basic convolution kernels are initialized using the Xavier method to ensure that the initial weight allocation is adapted to the cable image features.

[0146] Initialize positive weights to determine learnable parameters γ pos With θ pos ,γ pos Initialize the global scaling factor to 1, θ pos Initialize the channel offset to 0.

[0147] Initialize the weight adjustment factor and learnable parameter φ prob φ scale_min_raw φ δ , where φ prob Used to control the probability of adjusting the positive mask, initialized to logit(0.3), and mapped to [0,1] after Sigmoid activation; φ scale_min_raw To minimize the scaling factor, it is initialized to ln(exp(0.8)-1), and φ is obtained through softplus. scale_min ;φ δ The scaling range difference parameter is initialized to ln(exp(0.4)-1), and after softplus, Δ is obtained. scale Finally, the maximum scaling φ is obtained. scale_max =φ scale_min +Δ scale .

[0148] (2) Generate positive weight mask and adjustment factor;

[0149] Dynamically generate positive weight masks based on learnable parameters:

[0150]

[0151] Where σ is the Sigmoid function, γ pos W is the scaling factor. base For convolution kernel parameters, Indicates θ pos The dimension is expanded to W base Dimension. The base weights are adjusted using learnable parameters, and then activated by Sigmoid to obtain a soft mask. The closer the value is to 1, the more likely the corresponding weight is to be judged as a "positive weight" and participate in the adjustment.

[0152] Based on the learnable parameter φ probThe probability prob = σ(φ) is obtained through the Sigmoid function. prob Then, a mask is generated by sampling from the Bernoulli distribution based on the probability prob.

[0153] (3) Positive weight adaptive adjustment;

[0154] W adj =W base ×[1+(scale_factor-1)×prob_mask×positive_mask]

[0155] Among them W base The weights are the convolution kernel weights, and the scale_factor is the scaling factor, which is determined by the learnable parameter φ. scale_min With φ scale_max The sampling is determined, and the mask that needs to be adjusted in the positive weight coefficients of prob_mask is W. base This is a mask used to determine which weights are positive among the weight coefficients.

[0156] γ pos θ pos φ prob φ scale_min_raw φ δ Both are learnable parameters, and W base We optimize together using backpropagation of the loss function.

[0157] The domain knowledge learning module aims to address the problem of significant differences in feature distribution across different domains, making it difficult for traditional methods to capture key features of the target domain. By guiding the model to selectively extract key domain features from existing features, the module improves the model's feature extraction capabilities in specific domains. The specific structure and implementation steps are as follows:

[0158] (1) Assess the importance of features:

[0159] The features of the i-th layer of the model are:

[0160]

[0161] C i H represents the number of channels in the i-th layer. i W i For spatial dimensions.

[0162] Flatten the features into Eliminate the interference of spatial structure on feature weights, and normalize the features using the following formula:

[0163]

[0164] in This represents the maximum value of the feature vector, used to avoid numerical overflow and ensure that the weights focus on the features most important to the domain. The final weights... This represents the relevance score of the feature domain corresponding to each element.

[0165] (2) Global feature information fusion;

[0166] Calculate the global average feature of the model at layer i:

[0167]

[0168] H i W i For spatial dimensions, F i For the i-th layer features of the model, a learnable parameter θ is introduced to fuse global information with domain feature weights;

[0169]

[0170] This step ensures that the weights include information about locally important features while also being guided by global domain characteristics, thus preventing local noisy features from being misjudged as domain-related.

[0171] Based on the above method, a cable surface anomaly detection device based on an unsupervised algorithm in this embodiment is characterized by comprising: at least one memory and at least one processor;

[0172] The at least one memory is used to store a machine-readable program;

[0173] The at least one processor is configured to call the machine-readable program to execute a cable surface anomaly detection method based on an unsupervised algorithm.

[0174] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.

Claims

1. A cable surface anomaly detection method based on an unsupervised algorithm, characterized by, It has the following steps: S1. Acquire cable images, and perform Gaussian filtering for noise reduction, CLAHE algorithm brightness normalization, Sobel edge extraction, and Otsu segmentation preprocessing; S2. Improve ResNet50 by using a contrastive learning framework for model pre-training; Using ResNet50 as the backbone network, the 3×3 convolutions of ResNet50 are replaced with adaptive weighted convolutions to improve feature extraction capabilities; a domain knowledge learning module is added between layer 1, layer 2, layer 3, and layer 4 to enhance the model's adaptability to cable datasets. The adaptive weighted convolution includes: (1) Initialize the convolution kernel and learnable parameters; Configuring a fixed base convolution kernel for a 3×3 convolution kernel , is the input channel number, is the output channel number, and the base convolution kernel adopts an Xavier initialization method to adapt the initialized weight distribution to the cable image features; Initialize positive weights to determine learnable parameters and , Initialize the global scaling factor to 1. Initialize the channel offset to 0; Initialize the weight adjustment factor learnable parameters , , ,in Used to control the probability of adjusting the positive mask, initialized to logit(0.3), and mapped to [0,1] after Sigmoid activation; To minimize the scaling factor, it is initialized to ln(exp(0.8)-1), and obtained using softplus. ; The scaling range difference parameter is initialized to ln(exp(0.4)-1), and obtained after softplus. Ultimately, the maximum scaling is obtained. ; (2) Generate positive weight mask and adjustment factor; Dynamically generate positive weight masks based on learnable parameters: ; in, For the Sigmoid function, This is the global scaling factor. For convolution kernel parameters, Indicates will Dimensions expanded to The dimension adjusts the basic weights through learnable parameters, and then obtains a soft mask through Sigmoid activation. The closer the value is to 1, the more likely the corresponding weight is to be judged as a "positive weight" and participate in the adjustment. (3) Positive weights adaptively adjusted; ; in For convolution kernel weights, For adaptive hyperparameters, based on learnable parameters and Sampling determination, The mask that needs to be adjusted in the positive weighting coefficients. for A mask for determining positive weights among the weight coefficients; , , , , Both are learnable parameters, and Optimize together through backpropagation of the loss function; S3. Based on the pre-trained model, extract the features of defect-free cables, filter them with abnormal patches, compress the greedy core set, and then use FAISS to build a feature library. S4. Preprocess the image to be tested and extract features, calculate the cosine similarity with the feature library to obtain the patch-level anomaly score; S5. Dynamically adjust the threshold based on the similarity of the core set, and combine it with dilatational erosion to achieve anomaly segmentation; S6. Output the results based on the percentage of abnormal patches.

2. The cable surface anomaly detection method based on an unsupervised algorithm according to claim 1, characterized in that, Gaussian filtering is used for image denoising; The acquired RGB image is first converted to grayscale, and then Gaussian convolution is performed on the grayscale image. The edge extraction: For the grayscale image after CLAHE processing, the Sobel operator is used to calculate the gradient in the x and y directions to capture the direction and intensity of the edge. The gradient intensity of each pixel is judged along the gradient direction. If the gradient intensity of a pixel is not a local maximum in the gradient direction, the grayscale value is set to 0 to ensure that the edge width is 1 pixel. The Otsu segmentation: For the grayscale image after Canny edge detection, the Otsu algorithm is used to automatically determine the optimal segmentation threshold T_otsu by maximizing the inter-class variance between the foreground and background. Binarization is then used to automatically separate the cable region from the background, resulting in an image containing only the cable.

3. The cable surface anomaly detection method based on an unsupervised algorithm according to claim 2, characterized in that, In step S2, the backbone model is trained using a contrastive learning framework. The framework adopts a dual-path architecture. The first path query encoder uses an improved ResNet50 network, and the second path key encoder has the same structure as the query encoder. The parameters are updated through momentum. For each input image, two different augmented views are generated through an image augmentation strategy. View 1 is used for the query encoder and View 2 is used for the key encoder, forming a positive sample pair and a negative sample pair with other sample images in the same batch. The image is processed by two-way encoding feature extraction to obtain a feature vector, which is then input into the InfoNCE loss function to calculate the loss. The InfoNCE loss function was fine-tuned, and auxiliary constraints were added. The loss function formula is as follows: ; Where q is the feature vector extracted by the query. For positive sample key feature vectors, Let M be the feature vector of the negative sample key, and M be the number of negative samples. For temperature; With the addition of a lightweight regularization term, the total loss function is: ; Setting it to 0.01 constrains the global scaling factor to be close to its initial value, preventing excessive deviation in the determination of positive weights. Set it to 0.005 to ensure the scaling range remains stable between 0.6 and 1.4, avoiding excessive weight scaling.

4. The cable surface anomaly detection method based on an unsupervised algorithm according to claim 3, characterized in that, The domain knowledge learning module includes: (1) Assess the importance of features; The features of the i-th layer of the model are: ; Let i be the number of channels in the i-th layer. Spatial dimension; Flatten the features into To eliminate the interference of spatial structure on feature weights, the features are normalized using the following formula: ; in , representing the maximum value of the feature vector, is used to avoid numerical overflow and ensure that the weights focus on the features most important to the domain. The final weights Represents the relevance score of the feature domain corresponding to each element; (2) Global feature information fusion; Calculate the global average feature of the model at layer i: ; For spatial dimensions, For the features of the i-th layer, learnable parameters are introduced. This involves fusing global information with domain feature weights. ; Ensure that the weights include information about locally important features while also being guided by global domain characteristics, so as to avoid local noisy features being misjudged as domain-related.

5. The cable surface anomaly detection method based on an unsupervised algorithm according to claim 4, characterized in that, In step S3, patch-level local features are extracted using the intermediate layers of the improved ResNet50 pre-trained model to enhance the feature response of minor defects in the cable. First, the normal image size is adjusted to 224×224 pixels and normalized. Then, the normalized image is input into the backbone network, and multi-scale feature fusion is performed using the network's multi-layer features to enhance the model's ability to identify multi-scale defects. The feature vector size of layer 2 is The feature vector size of layer 3 is The feature vector size of layer 4 is The feature vectors of layers 3 and 4 are upsampled to 28×28 and aligned with layer 2. Then, these three feature layers are stacked, and an adaptive pooling algorithm is used to project the features to 1024 dimensions to obtain the feature vector. ,in ; Each image generates multiple 1024-dimensional feature vectors. To improve the performance of the inference stage, the feature vectors need to be compressed. First, perform exception patch vector filtering. For each patch feature, calculate the average cosine distance of its k nearest neighbors in the feature space. Set an abnormal threshold ,in The average distance is the mean. Let be the standard deviation, if The patch was identified as an abnormal patch and its feature vectors were removed, resulting in the processed vector set. ; Secondly, a greedy core set selection algorithm is used to select a small number of representative patch features from the filtered vector set, which greatly reduces storage requirements while preserving the diversity of the feature space. The feature selection is performed using the following formula: ; in Let K be the cosine distance between vectors, and K be the size of the core set. Randomly select one patch feature as the initial core set. ,right For each unselected feature f in the core set, calculate the minimum cosine distance with the current core set, and select the feature f with the furthest distance to add it to the core set. Repeat this step until the core set size reaches K, thus obtaining the final core set. ; Using the IndexFlatIP index type of the FAISS vector library, the 1024-dimensional feature vector of the core set C is L2 normalized and then added to the FAISS vector library.

6. The cable surface anomaly detection method based on an unsupervised algorithm according to claim 5, characterized in that, In step S4, the image to be detected is first preprocessed by scaling it to 224×224 and normalizing it. Then, the image is input into the ResNet50 model pre-trained in step S2 for feature extraction. The extracted intermediate layer features are then fused to obtain a feature vector set with a shape of 784×1024. ,calculate Each vector in the core set The cosine similarity score obtained by performing nearest neighbor retrieval is the anomaly score. for For each patch in the set, the maximum similarity and average similarity with the core set are calculated. The maximum similarity reflects the highest degree of matching between the patch and normal features; the closer the value is to 1, the more normal the patch is. The average similarity avoids misjudgments caused by mismatches of a single core feature. As a supplement to the maximum similarity, and based on the logic that the lower the similarity, the higher the degree of abnormality, a patch-level abnormality score is defined. : ; in This is an adjustable parameter; the closer the value is to 1, the higher the degree of abnormality; the closer the value is to 0, the closer it is to normal. Indicates the maximum similarity. Indicates average similarity; The feature map was restored to 224×224 using bilinear interpolation.

7. The cable surface anomaly detection method based on an unsupervised algorithm according to claim 6, characterized in that, In step S5, anomaly segmentation is performed based on the anomaly score matrix in step S4, with those scores greater than a threshold being the abnormal part and those scores less than a threshold being the normal part. First, we need to determine the threshold; the mean similarity within the core set is [value missing]. The standard deviation is The lower limit of similarity between the core set and the normal sample patch is: ; The upper limit of patch-level anomaly scores for normal samples is: ; in The threshold is dynamically adjusted according to the following formula: ; in Used to control the effect of variance on the threshold, for The pixel value of the abnormal patch block is set to 255, while the pixel value of other areas is set to 0, thereby generating an abnormal segmentation map. The segmentation map is then subjected to dilation and erosion to eliminate isolated points and avoid misjudgment.

8. The cable surface anomaly detection method based on an unsupervised algorithm according to claim 7, characterized in that, In step S6, based on the anomaly segmentation map obtained in step S5, if the proportion of the number of anomaly patches exceeds a certain number, the image is recorded as an anomaly image, and the anomaly segmentation map is output for anomaly localization, thereby completing anomaly detection.

9. A cable surface anomaly detection device based on an unsupervised algorithm, characterized in that, include: At least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is configured to invoke the machine-readable program to perform the method according to any one of claims 1 to 8.