Defect category determination method, system, electronic device, and medium

CN123016016APending Publication Date: 2026-10-30ZHUHAI BOJAY ELECTRONICS
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611086152.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-07-21
Publication Date
2026-10-30

AI Technical Summary

Technical Problem

若采用全局统一的固定阈值,阈值设定过严则容易漏检形态差异较大的真实同类缺陷,阈值设定过松则可能将形态差异较大的非同类缺陷误判为相似,检测准确性难以保证

Benefits of technology

[0005]本发明实施例至少具有如下有益效果:本发明通过构建“缺陷类别-特征子簇”双层聚类结构,首先在同一缺陷类别内部依据样本特征向量的距离分布进行聚类分簇,并为每个特征子簇自动生成专属的相似度阈值;对待检测图像,在目标缺陷类别范围内检索出候选相似样本后,根据各候选相似样本所属子簇的相似度阈值筛选有效样本,进而确定待检测图像的具体缺陷类别。本发明提供的方法通过自适应确定的相似度阈值解决了现有技术中因同类缺陷内部形态、深浅等差异导致特征分布离散时,固定阈值无法自适应不同子类判定边界的问题,能够在无需人工调参的情况下,显著提升缺陷类别判定的精准度与泛化能力。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123016016A_ABST
    Figure CN123016016A_ABST
Patent Text Reader

Abstract

The application discloses a kind of defect category determination method, system, electronic equipment and medium, the method specifically includes: the sample feature vector with same defect category label is classified into same sample set, respectively clustering is carried out to the sample feature vector in each sample set, obtain multiple feature subclusters;Similarity threshold is determined according to the distance distribution of sample feature vector in each feature subcluster;The feature vector to be detected of image to be detected is acquired and target defect category is determined;Search the preset number of sample feature vectors belonging to target defect category and the minimum distance of image to be detected feature vector, as candidate similar sample;Based on the sample feature vector in candidate similar sample and the distance of image to be detected feature vector is less than corresponding similarity threshold, determine the defect category to which image to be detected belongs.The method of the application solves the problem that fixed threshold value cannot adapt to different subclass determination boundary, significantly improves the accuracy of defect category determination.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer vision inspection, and in particular to a method, system, electronic device, and medium for determining defect categories. Background Technology

[0002] In the field of image classification, especially in industrial visual defect detection, accurate classification of defect images is crucial for defect tracing, process improvement, and automated quality inspection. Typically, the system compares the defect image to be detected with a labeled defect sample library, using distance metrics of feature vectors to determine the specific category (e.g., scratches, cracks, dirt), and even further, to identify the sub-type of that category (e.g., shallow or deep scratches). Currently, common defect classification methods rely on globally fixed thresholds or single-category fixed thresholds for similarity measurement. Globally fixed threshold methods use a single distance threshold as the similarity criterion for all types and forms of defects; when the distance between the feature vector of the defect to be detected and the feature vector of the sample is less than the threshold, they are considered similar. However, in actual industrial production, defects of the same type often exhibit significant differences in shape, size, and depth, resulting in highly discrete distributions of feature vectors in the feature space. For example, within the "scratch" category, the feature vectors of shallow scratches are relatively close together and concentrated, while the feature vectors of deep scratches have a wider distribution. If a globally uniform fixed threshold is used, setting the threshold too strictly may easily miss real defects of the same type with large morphological differences, while setting the threshold too loosely may misjudge dissimilar defects with large morphological differences as similar, making it difficult to guarantee detection accuracy. Summary of the Invention

[0003] This invention aims to at least solve one of the technical problems existing in the prior art. To this end, this invention proposes a defect category determination method, system, electronic device, and medium capable of achieving accurate defect category determination.

[0004] The defect category determination method according to a first aspect embodiment of the present invention includes: Obtain a defect sample library, wherein the defect sample library includes multiple sample feature vectors and a defect category label corresponding to each sample feature vector; Sample feature vectors with the same defect category label are grouped into the same sample set, and the sample feature vectors in each sample set are clustered to obtain multiple feature sub-clusters. Based on the distance distribution of the sample feature vectors within each feature sub-cluster, a similarity threshold corresponding to each feature sub-cluster is determined; Obtain the feature vector of the image to be detected and determine the category of the target defect; From the defect sample library, a preset number of sample feature vectors that belong to the target defect category and have the smallest distance to the feature vector to be detected are retrieved as candidate similar samples; Based on the sample feature vectors in the candidate similar samples whose distance from the feature vector to be detected is less than the corresponding similarity threshold, the defect category to which the image to be detected belongs is determined.

[0005] The embodiments of this invention have at least the following beneficial effects: This invention constructs a two-layer clustering structure of "defect category - feature sub-cluster." First, within the same defect category, clustering is performed based on the distance distribution of sample feature vectors, and a unique similarity threshold is automatically generated for each feature sub-cluster. For the image to be detected, after retrieving candidate similar samples within the target defect category, valid samples are selected based on the similarity threshold of the sub-cluster to which each candidate similar sample belongs, thereby determining the specific defect category of the image to be detected. The method provided by this invention solves the problem in existing technologies where a fixed threshold cannot adapt to the boundary of different sub-categories when feature distribution is discrete due to differences in internal shape, depth, etc., of defects within the same category. It can significantly improve the accuracy and generalization ability of defect category determination without manual parameter tuning.

[0006] According to some embodiments of the present invention, prior to the step of obtaining the defect sample library, the following steps are included: Obtain defect sample images and preprocess the defect sample images; Feature extraction is performed on defect sample images based on pre-trained convolutional neural networks to obtain the sample feature vector; The sample feature vectors are associated with the corresponding defect category labels to form the defect sample library.

[0007] According to some embodiments of the present invention, the step of determining the similarity threshold corresponding to each feature sub-cluster based on the distance distribution of the sample feature vectors within each feature sub-cluster includes: Calculate the Euclidean distance from the feature vector of each sample within the feature sub-cluster to the cluster center, and use the average value of the Euclidean distance as the average distance threshold; And / or, use the maximum value of the Euclidean distance as the maximum distance threshold.

[0008] According to some embodiments of the present invention, the step of retrieving a predetermined number of sample feature vectors belonging to the target defect category and having the smallest distance to the feature vector to be detected from the defect sample library as candidate similar samples includes: The number of candidate similar samples to be retrieved is determined, and the number of retrievals is the maximum value among the first number, the second number, and the third number; wherein, the first number is a preset lower limit for the number of retrievals, the second number is a preset minimum number of valid returned samples, and the third number is the product of a preset redundancy coefficient and the total number of sample feature vectors belonging to the target defect category in the defect sample library; Based on the number of searches, select the number of sample feature vectors that are closest to the feature vector to be detected from the sample feature vectors belonging to the target defect category, and use them as the candidate similar samples. According to some embodiments of the present invention, the step of retrieving a predetermined number of sample feature vectors belonging to the target defect category and having the smallest distance to the feature vector to be detected from the defect sample library as candidate similar samples further includes: The number of sample feature vectors among the candidate similar samples that satisfy the corresponding similarity threshold is obtained statistically. When the number of sample feature vectors is less than the preset minimum number of returned samples, the number of searches is increased, and the sample feature vectors belonging to the target defect category in the defect sample library are searched again to obtain supplementary candidate similar samples; The supplementary candidate similar samples are arranged in ascending order of their distance from the feature vector to be detected, and are added to the candidate similar samples in sequence until the number of the sample feature vectors in the candidate similar samples that meet the similarity threshold reaches the minimum number of returned samples.

[0009] According to some embodiments of the present invention, the step of determining the defect category of the image to be detected based on the sample feature vectors in the candidate similar samples whose distance to the feature vector to be detected is less than the corresponding similarity threshold further includes: A global default threshold is preset; If the similarity threshold cannot be determined based on the feature subcluster to which the candidate similar sample belongs, then the global default threshold shall be used as the similarity threshold corresponding to the candidate similar sample.

[0010] According to some embodiments of the present invention, when there are multiple target defect categories, the step of determining the defect category of the image to be detected based on the sample feature vectors in the candidate similar samples whose distance to the feature vector to be detected is less than the corresponding similarity threshold includes: The sample feature vectors whose distance from the feature vector to be detected is less than the corresponding similarity threshold are grouped according to their corresponding defect category labels; The average distance between the sample feature vector and the target feature vector in each group is calculated, and the target defect category corresponding to the group with the smallest average distance is determined as the defect category to which the image to be detected belongs.

[0011] A defect category determination system according to a second aspect of the present invention includes: The sample acquisition module is used to acquire a defect sample library, wherein the defect sample library includes multiple sample feature vectors and a defect category label corresponding to each sample feature vector; The clustering module is used to group sample feature vectors with the same defect category label into the same sample set, and to cluster the sample feature vectors in each sample set to obtain multiple feature sub-clusters. The threshold generation module is used to determine the similarity threshold corresponding to each feature sub-cluster based on the distance distribution of the sample feature vectors within each feature sub-cluster; The image acquisition module is used to acquire the feature vector of the image to be detected and determine the category of the target defect; The retrieval module is used to retrieve a preset number of sample feature vectors from the defect sample library that belong to the target defect category and have the smallest distance to the feature vector to be detected, as candidate similar samples; The category determination module is used to determine the defect category of the image to be detected based on the sample feature vectors in the candidate similar samples whose distance from the feature vector to be detected is less than the corresponding similarity threshold.

[0012] An electronic device according to a third aspect of the present invention includes a memory and a processor, the memory being used to store at least one program, and the processor being used to load the at least one program to execute the defect category determination method described in the above-described aspect embodiments.

[0013] According to a fourth aspect of the present invention, a computer-readable storage medium stores computer-executable instructions for causing a computer to perform the defect category determination method as described in the above-described aspects.

[0014] Additional aspects and advantages of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description

[0015] The above and / or additional aspects and advantages of the present invention will become apparent and readily understood from the description of the embodiments taken in conjunction with the following drawings, in which: Figure 1 This is a flowchart of the defect category determination method according to an embodiment of the present invention; Figure 2 This is an overall schematic diagram of the defect category determination method according to an embodiment of the present invention; Figure 3 This is a structural diagram of an electronic device provided in another embodiment of the present invention. Detailed Implementation

[0016] Embodiments of the present invention are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and should not be construed as limiting the present invention.

[0017] In the description of this invention, "several" means one or more, "multiple" means two or more, "greater than," "less than," "exceeding," etc. are understood to exclude the stated number, and "above," "below," "within," etc. are understood to include the stated number. If "first," "second," etc. are used in the description, they are only for the purpose of distinguishing technical features and should not be construed as indicating or implying relative importance or implicitly indicating the number of indicated technical features or the order of the indicated technical features.

[0018] In the field of industrial visual defect detection, accurate classification of defect images is a crucial step in defect tracing, process improvement, and automated quality inspection. Typically, the system needs to compare the defect image to be detected with a labeled defect sample library, determining the specific category (e.g., scratches, cracks, dirt) based on the distance metric of feature vectors, and even further confirming the specific morphological subclass (e.g., shallow or deep scratches). Currently, common defect classification methods rely on global fixed thresholds or single-category fixed thresholds for similarity measurement. Global fixed threshold methods use a single distance threshold as the similarity criterion for all types and forms of defects; when the distance between the feature vector of the defect to be detected and the feature vector of the sample is less than the threshold, they are considered similar. However, in actual industrial production, defects of the same type generally exhibit significant differences in shape, size, and depth, resulting in a highly discrete distribution of feature vectors in the feature space. For example, within the "scratch" category, the feature vectors of shallow scratches are relatively close to each other and concentrated, while the feature vectors of deep scratches have a wider distribution range. If a globally uniform fixed threshold is used, setting the threshold too strictly may easily miss real defects of the same type with large morphological differences, while setting the threshold too loosely may misjudge dissimilar defects with large morphological differences as similar, making it difficult to guarantee detection accuracy.

[0019] Please refer to Figures 1-3 , Figure 1This is a flowchart of a defect category determination method according to an embodiment of the present invention. This embodiment discloses a defect category determination method, which includes, but is not limited to, the following steps: Step S100: Obtain a defect sample library, wherein the defect sample library includes multiple sample feature vectors and a defect category label corresponding to each sample feature vector; It should be noted that the defect sample database serves as the data foundation for subsequent clustering, similarity threshold generation, and similarity retrieval. It pre-maps various defect sample images from a high-dimensional pixel space to a semantically discriminative feature space using a pre-trained deep feature extraction network, and persistently stores them as feature vectors. Simultaneously, each feature vector is bound to a corresponding defect category label, enabling the database to support rapid filtering by category and vector similarity calculation. Specifically, the original defect sample database stores multiple defect sample images, each with a defect category label recording the type of defect, including but not limited to scratches, cracks, dirt, dents, and burrs. For the same defect type, it can be further subdivided into multiple subcategories based on visual attributes such as shape, size, and depth. For example, scratches can be subdivided into shallow scratches, deep scratches, and wide scratches; cracks can be subdivided into surface micro-cracks and through-cracks; and dirt can be subdivided into point dirt and sheet dirt. These images are processed into high-dimensional sample feature vectors by the pre-trained deep feature extraction network. It is understandable that the defect category label corresponding to the sample feature vector is the defect category label corresponding to the defect sample image.

[0020] In some specific embodiments of the present invention, the deep feature extraction network can be a model such as ResNet-50, MobileNetV3-Small or Vision Transformer (ViT) pre-trained on an industrial defect dataset, as long as it can achieve feature extraction of defect sample images, the present invention does not limit it.

[0021] Step S200: Group the sample feature vectors with the same defect category label into the same sample set, and cluster the sample feature vectors in each sample set to obtain multiple feature sub-clusters; It should be noted that in step S200, the sample feature vectors are first classified into major categories based on the defect category label to ensure that the samples in each feature sub-cluster obtained by clustering belong to the same defect category. Then, unsupervised clustering is performed within each sample set based on the distance between feature vectors (such as Euclidean distance), so that the discrete samples of the same type in each defect category are divided into multiple sub-clusters with similar features, making the feature distribution within each sub-cluster relatively concentrated.

[0022] In some specific embodiments of the present invention, firstly, for the feature library... All samples, according to their category labels By grouping, we obtain several sample sets: ·····①; In formula ①, This represents the set of all samples under category c, where samples within the same set have the same semantic category (e.g., all are "scratches"). Then, for each sample set... Internally, clustering is performed, grouping samples with similar feature distributions and morphologies into the same sub-cluster. The clustering process iteratively partitions based on the Euclidean distance between feature vectors, for each set of feature vectors in category c. The clustering process can be represented as: ·····②; In formula ②, This represents the k-th sub-cluster under category c. The number of clusters for category c (can be adaptively determined based on the sample size or preset, such as "deep scratches" or "shallow scratches"). Samples within each sub-cluster are close to each other in the feature space, indicating that they have high consistency in visual attributes such as shape, size, and depth.

[0023] In some specific embodiments of the present invention, the clustering algorithm can be K-means clustering, density-based DBSCAN clustering, hierarchical clustering, or clustering based on Gaussian mixture models, and the specific choice can be made according to the characteristics and quantity of the sample distribution. For example, when the sample size is large and the distribution is relatively concentrated, K-means, which has high computational efficiency, can be selected; when there are outliers and noise needs to be automatically identified, DBSCAN can be selected; and for scenarios where the number of clusters needs to be predetermined, hierarchical clustering can be used.

[0024] Step S300: Determine the similarity threshold corresponding to each feature sub-cluster based on the distance distribution of the sample feature vectors within each feature sub-cluster; It should be noted that the similarity threshold in step S300 refers to a distance boundary value generated based on the actual distribution statistical characteristics of samples within a feature subcluster, used to determine whether two feature vectors belong to the same defect subclass. This threshold is not manually set or globally uniform, but rather a statistical feature quantity dynamically determined from the actual distance distribution of the sample feature vectors within the subcluster. For a feature subcluster, firstly, the mean vector of all sample feature vectors within the cluster is calculated as the cluster center; then, the Euclidean distance from each sample feature vector within the feature subcluster to the cluster center is calculated, denoted as a set of distance values. These distance values ​​characterize the distribution of samples within the subcluster around the cluster center in the feature space, and the similarity threshold is the representative statistic extracted from this set of distance values. For example, taking the arithmetic mean of the Euclidean distance values ​​as the similarity threshold for the sub-cluster represents the typical deviation of samples within the sub-cluster from its cluster center. This is suitable for sub-clusters with low morphological variability and concentrated feature distribution (such as the fine, shallow scratch sub-cluster). In subsequent judgment, if the Euclidean distance between the detected feature vector and the feature vector of a candidate similar sample is less than this typical deviation, it can be quantitatively determined that the two are sufficiently close in the feature space and are considered to belong to the same defect subclass. Alternatively, taking the maximum value of the Euclidean distance values ​​as the similarity threshold defines the farthest boundary of samples within the sub-cluster from the cluster center. This is suitable for defect sub-clusters with gradual morphological changes and a wide distribution range (such as the deep scratch sub-cluster). When the Euclidean distance between the detected feature vector and the candidate similar sample does not exceed this boundary, the detected defect can be considered to fall within the feature distribution range of the sub-cluster.

[0025] It is understood that the above embodiments use Euclidean distance as a distance metric for illustration. In other embodiments, other distance metrics such as cosine distance and Manhattan distance can also be used to statistically analyze the distance distribution within the feature sub-clusters and generate corresponding similarity thresholds. The principle is the same as the above process, and all should be covered within the protection scope of this invention.

[0026] Step S400: Obtain the feature vector of the image to be detected and determine the category of the target defect; It should be noted that the image to be detected undergoes the same preprocessing steps as when the sample library was constructed, including defect region cropping, size normalization, and pixel normalization, to obtain standardized input. Subsequently, this standardized input is fed into the same pre-trained feature extraction network as in step S100 for forward propagation, extracting the feature vector to be detected with the same dimension as the sample feature vector. Because the same preprocessing specifications and feature extraction network are used, the feature vector to be detected and the sample feature vectors in the library reside in the same high-dimensional feature space, and their Euclidean distance can be directly used for similarity measurement. The method for determining the target defect category can be flexibly selected according to the actual application: for example, the upstream quality inspection system can automatically issue target category labels based on the product model and inspection station, or operators can manually specify one or more defect categories to be compared through an interactive interface; in scenarios involving multi-category classification, multiple target defect categories can be set, such as simultaneously including scratches, cracks, and dirt, so that subsequent steps can comprehensively determine the optimal classification.

[0027] Step S500: From the defect sample library, retrieve a preset number of sample feature vectors that belong to the target defect category and have the smallest distance to the feature vector to be detected, as candidate similar samples; It should be noted that step S500 first uses the target defect category label as a filtering condition to select all sample feature vectors belonging to the target category in the defect sample library, forming a retrieval subset; then, it calculates the Euclidean distance between the feature vector to be detected and each sample feature vector in the subset, sorts the distance values ​​in ascending order, and selects the K samples with the smallest distance as candidate similar samples. Here, K is a positive integer, representing the number of candidates returned in the initial retrieval.

[0028] In some specific embodiments of the present invention, in one embodiment, a fixed number of searches is used, for example, K is preset to 20. This method is simple to implement and is suitable for production lines with relatively balanced sample sizes for each category.

[0029] Step S600: Based on the sample feature vectors in the candidate similar samples whose distance from the feature vector to be detected is less than the corresponding similarity threshold, determine the defect category to which the image to be detected belongs.

[0030] It should be noted that, since each candidate similar sample has been assigned a feature sub-cluster identifier during clustering in step S200, step S600 first queries the corresponding similarity threshold from the "category-feature sub-cluster-similarity threshold" mapping table constructed in step S300 based on the defect category label and sub-cluster identifier corresponding to the candidate similar sample; then, it calculates the Euclidean distance between the feature vector to be detected and the feature vector of the candidate similar sample, and compares the distance with the queried similarity threshold; if the distance is less than the similarity threshold corresponding to the feature sub-cluster, the candidate similar sample is determined to be a valid similar sample and included in the final decision set; otherwise, it is determined to be an invalid match and excluded, so as to determine the defect category to which the image to be detected belongs.

[0031] In some specific embodiments of the present invention, when there is only one target defect category, the number of valid samples that meet the similarity threshold condition among all candidate similar samples is counted. If the number of valid samples exceeds a preset threshold (e.g., greater than or equal to 3), the image to be detected is determined to belong to the target defect category, and the valid samples are arranged from closest to furthest from the feature vector to be detected. The corresponding original defect image and metadata are taken as the basis for judgment and output. This embodiment is applicable to the fixed-point re-inspection stage of the production line where the defect type has been clearly identified, and can quickly confirm the defect attribution.

[0032] It should be noted that steps S100 to S600 are based on grouping by defect category labels and then performing sub-cluster clustering. The original fixed threshold is replaced by a similarity threshold adaptively generated by the feature distance distribution within each sub-cluster. This allows for fine-grained screening and category determination based on the specific threshold of the sub-cluster to which the candidate similar samples belong during the similarity determination stage. This overall solution effectively overcomes the feature discretization problem caused by differences in shape, depth, etc., of similar defects, and significantly improves the accuracy of defect category determination and production line versatility without relying on manual parameter tuning.

[0033] Reference Figure 2 In addition, prior to step S100 in the above-described embodiments, the following steps are included, but are not limited to: Step S101: Obtain defect sample images and preprocess the defect sample images; Step S102: Extract features from defect sample images based on pre-trained convolutional neural networks to obtain sample feature vectors; Step S103: Associate the sample feature vector with the corresponding defect category label to form a defect sample library.

[0034] It should be noted that before converting the defect sample image into a sample feature vector, a standardization process for the defect sample image is included, such as size normalization, channel conversion, and pixel normalization, to eliminate interference from imaging illumination and size differences. Specifically, this includes: cropping the defect region from the original defect sample image to remove irrelevant background interference, allowing the subsequent feature extraction model to focus on the defect itself; the input is a single small image from the dataset, which, through defect region localization and cropping, obtains a sub-image containing only the defect target, providing standardized input for subsequent feature extraction; and original images covering various industrial product appearance defects are collected, including but not limited to common defect types such as scratches, cracks, dirt, dents, and burrs. The input is a single small image from the dataset, let the original image be I(x,y), where... W represents the width of the image, and H represents the height of the image. Since the defect region in the original image typically occupies only a small portion of the entire image, the background region introduces a large amount of noise information unrelated to the defect, interfering with the attention of the feature extraction network. Therefore, this invention first locates the defect region in the input image, determining the bounding box position of the defect in the image through object detection or thresholding methods: ······③; In formula ③, The coordinates of the top left corner of the defect area are: The coordinates are the bottom right corner. Based on this bounding box, the defect sub-image is cropped from the original image: ······④; In formula ④, ,in, , This represents the width of the cropped sub-image. The height of the cropped sub-image. The cropped image retains only the defect and its immediate edge regions, significantly reducing background interference. Finally, the cropped image data is converted into the tensor format specified by the model to obtain the standard input. This provides a standardized and focused data foundation for subsequent feature extraction. After preprocessing, the defect sample images have been cropped to standard input. This tensor is then input into a pre-trained deep feature extraction network. The forward propagation process of feature extraction can be represented as: ·····⑤; In formula ⑤, Let i be the standard input for the i-th defect sample image. These are the learnable parameters of the network (which remain fixed during the inference phase). To output the feature vector, This represents the dimension of the feature vector. After extraction, the feature vector is bound and encapsulated with the defect category label and index identifier. A complete record for each sample constitutes a triple: ······⑥; In formula ⑥, Label the defect category of the sample (such as "scratches", "cracks", "dirt" etc.). For a pre-defined set of categories; This serves as a unique index identifier for the sample within the database, used to trace the original image and metadata. All sample records are organized into an indexable, structured feature library, denoted as [reference needed]. , where N is the total number of defect images in the sample database. This defect sample database supports category-label-based filtering queries and vector similarity retrieval, providing a data foundation for subsequent efficient targeted retrieval.

[0035] Reference Figure 2 Furthermore, step S300 in the above-described embodiments also includes, but is not limited to, the following steps: Step S310: Calculate the Euclidean distance from the feature vector of each sample in the feature sub-cluster to the cluster center, and use the average value of the Euclidean distance as the average distance threshold. Step S320, and / or, using the maximum value of the Euclidean distance as the maximum distance threshold.

[0036] It should be noted that after clustering in step S300, this embodiment of the invention statistically analyzes the feature distance distribution within each sub-cluster to generate a specific similarity threshold. For each sub-cluster... First, calculate the Euclidean distance from each sample within the cluster to the cluster center: ······⑦; In formula ⑦, Let be the cluster center vector of the corresponding sub-cluster. This represents the Euclidean distance. A smaller Euclidean distance indicates greater similarity between the two feature vectors; a larger distance indicates greater dissimilarity. Based on the intra-cluster distance set, two types of thresholds are calculated separately: ······⑧; ······⑨; In equations ⑧ and ⑨, The average distance threshold within a cluster represents the "typical" distribution range of samples in that sub-cluster, and is suitable for scenarios where the defect morphology distribution is relatively uniform. The maximum distance threshold within a cluster represents the "boundary" distribution range of samples in that sub-cluster, suitable for scenarios where there are some outliers in the defect morphology but still within an acceptable range. Users can choose to use the average threshold and / or the maximum threshold according to their actual business needs. Furthermore, after obtaining the two types of similarity thresholds, this embodiment of the invention establishes a "category—sub-cluster—similarity threshold" mapping table: ······⑩.

[0037] Reference Figure 2 Furthermore, step S500 in the above-described embodiments also includes, but is not limited to, the following steps: Step S510: Determine the number of candidate similar samples to be retrieved. The number of retrievals is the maximum value among the first number, the second number, and the third number. The first number is a preset lower limit for the number of retrievals, the second number is a preset minimum number of valid returned samples, and the third number is the product of the preset redundancy coefficient and the total number of sample feature vectors belonging to the target defect category in the defect sample library. Step S520: Based on the number of searches, select the number of sample feature vectors that are closest to the feature vector to be detected from the sample feature vectors belonging to the target defect category, and use them as candidate similar samples. It should be noted that steps S510 to S520 are further refinements of the adaptive determination of the number of candidate similar samples retrieved in step S500 and the retrieval execution process. In traditional fixed retrieval number schemes, if the retrieval number is set too small, samples that are actually very similar to the feature vector to be detected may be missed, resulting in insufficient information for subsequent judgment; if the retrieval number is set too large, a large number of irrelevant samples will be introduced, increasing the computational cost of subsequent judgment steps. To solve this problem, this step adopts a dynamic retrieval number determination strategy that integrates multiple factors.

[0038] Specifically, let the set of target categories to be compared in this detection task be . It may contain a single category (single-category determination scenario) or multiple categories (multi-category attribution determination scenario). For each target category First, from the feature library The sample subset of a category is filtered out using a category filter. This filtering operation quickly locates results using category tags, avoiding blind searches across the entire database and significantly reducing computational load. Then, the initial search count is calculated. The number of searches needs to consider two factors: firstly, it must be sufficient to cover the multi-sample distribution of the category; secondly, the search time must be controlled. This invention uses an adaptive calculation formula: ······ ; Mode middle, Let c be the total number of samples in category c. The preset redundancy coefficient (within the range of [0.1, 0.5]) is used to reserve redundancy space based on the theoretical maximum number of samples; The minimum number of valid return samples required by the business can be configured according to the actual quality inspection standards; This is the absolute lower bound for the number of searches, ensuring that even categories with smaller sample sizes can obtain sufficient search results. The maximum value among the three is taken as the initial number of searches.

[0039] In subset Perform vector similarity retrieval in the middle, using the feature vector to be detected. Given a query vector, return the vector with the closest feature distance. One sample: ······ ; Mode In the process, each returned candidate similar sample j contains three core pieces of information: index ID (used to trace the original image and metadata), similarity distance, and so on. and the category label of the sample. Sub-cluster number The above information is used for subsequent matching with similarity thresholds, and the search results serve as the original input for subsequent threshold filtering and judgment.

[0040] Reference Figure 2 Furthermore, step S500 in the above-described embodiments also includes, but is not limited to, the following steps: Step S530: Count the number of sample feature vectors that meet the corresponding similarity threshold among the candidate similar samples; Step S540: When the number of sample feature vectors is less than the preset minimum number of returned samples, increase the number of searches and re-search the sample feature vectors belonging to the target defect category in the defect sample library to obtain supplementary candidate similar samples. Step S550: The supplementary candidate similar samples are arranged in ascending order of distance from the feature vector to be detected, and are added to the candidate similar samples in sequence until the number of sample feature vectors that meet the similarity threshold in the candidate similar samples reaches the minimum number of returned samples.

[0041] It should be noted that in actual industrial applications, due to factors such as the limited total number of samples in the target defect category, the rarity of the defect features to be inspected, or the strict similarity threshold settings for certain subclusters, the number of sample feature vectors that meet the corresponding similarity threshold conditions among the candidate similar samples returned in the initial search may not reach the preset minimum number of returned samples. If the category determination stage is directly entered at this time, the confidence of the determination result will decrease due to insufficient effective information. Therefore, this step ensures that the final number of effective samples available for determination is not lower than the lower limit of business requirements by automatically expanding the search scope and selectively supplementing samples. Specifically, when the number of effective samples after similarity threshold screening is insufficient to meet the minimum business requirements, the search scope is automatically expanded for a second search, and high-quality samples are supplemented first based on similarity distance to ensure the stability and usability of the output results. First, the number of all effective samples that pass the threshold determination under the current target category is counted: ······ ; The number of valid samples is compared with the preset minimum number of returned samples. Compare. If If so, the current candidate set is used directly as the final result, without any additional operations. If this fails, the fallback retry mechanism will be triggered. The system will automatically expand the search scope and calculate the new number of searches: ······ ; Mode middle, This is a preset expansion factor used to provide ample supplementary space when the original search base is insufficient; This sets an absolute upper limit on the number of searches to avoid impacting system performance due to excessively large single search volumes. The system is based on... To increase the number of searches, a vector similarity search is performed again to obtain a new candidate set. The determination method of step S600 is repeated on the new candidate set to obtain the validity label of all samples. Then, the samples are ranked according to similarity distance. Sort the results in ascending order. The result construction strategy is as follows: prioritize selecting all... Valid samples; if the number of valid samples still does not reach the target Then, from the samples that did not pass the threshold judgment, they are added sequentially from the closest to the furthest until the minimum number requirement is reached, thus obtaining the final valid result set. In some embodiments, deduplication based on index ID is performed throughout the result construction process to ensure that the same defective sample does not appear repeatedly in the final result set.

[0042] Reference Figure 2Furthermore, step S600 in the above-described embodiments also includes, but is not limited to, the following steps: Step S610: Preset a global default threshold; Step S620: For any candidate similar sample, if the corresponding similarity threshold cannot be determined based on its feature subcluster, then the global default threshold is used as the similarity threshold corresponding to the candidate similar sample.

[0043] It should be noted that steps S610 to S620 provide a fallback judgment mechanism for abnormal operating conditions. In the normal process, step S600 determines the similarity based on the feature subcluster to which the candidate similar sample belongs, by querying the corresponding similarity threshold from the "Category-Feature Subcluster-Similarity Threshold" mapping table. However, in actual industrial deployments, some candidate similar samples may not be assigned to valid feature subclusters (e.g., during clustering, the sample is marked as noise due to feature anomalies, and its subcluster is identified as null or invalid), or the defect category to which the sample belongs may not have been clustered as a whole (e.g., after a new defect category is added to the database, clustering updates have not been triggered, resulting in no corresponding record in the mapping table). In such cases, the system cannot obtain the subcluster similarity threshold corresponding to the candidate similar sample, resulting in a missing judgment condition. To solve the above problem, this step presets a global default threshold as a fallback judgment standard. This global default threshold can be pre-determined based on the feature distribution statistics of the entire defect sample database. For example, the distance distribution from all sample feature vectors to the global center of their respective categories can be calculated, and its quantiles or empirical values ​​can be taken. In actual judgment, when a candidate similar sample cannot be matched with a valid sub-cluster similarity threshold, the system automatically downgrades to use the global default threshold instead, and continues to perform distance comparison and validity judgment. This ensures that the system can still run normally and output category judgment results under extreme conditions such as incomplete clustering or abnormal data, thereby enhancing the robustness and engineering practicality of the method.

[0044] In some embodiments of the present invention, the situations where a backup plan is actually needed in engineering projects mainly include: Rule 1 (invalid sub-cluster fallback): If the sample's sub-cluster number If the value is empty, it means that the sample was not assigned to any valid sub-cluster during the clustering process (possibly due to abnormal sample quality or lack of coverage by the clustering algorithm). In this case, the similarity threshold of the sub-cluster cannot be obtained, and the system automatically downgrades to using the global default threshold.

[0045] Rule 2 (Category without clustering data as a fallback): If the sample belongs to a category Not in the threshold mapping table If a record exists, it indicates that the category has not yet undergone clustering or that clustering data is missing (e.g., for newly added defect categories), and the global default threshold is used in a downgraded manner.

[0046] Reference Figure 2 Furthermore, when there are multiple target defect categories, step S600 in the above-described embodiment also includes, but is not limited to, the following steps: Step S630: Group the sample feature vectors whose distance from the feature vector to be detected is less than the corresponding similarity threshold according to their corresponding defect category labels; Step S640: Calculate the average distance between the sample feature vector and the feature vector to be detected in each group, and determine the target defect category corresponding to the group with the smallest average distance as the defect category to which the image to be detected belongs.

[0047] It should be noted that in actual industrial quality inspection scenarios, the defect type of the image to be inspected may not be uniquely determined before entering the judgment stage. For example, the upstream defect detection module may output multiple suspected categories, or the operator may not be able to distinguish whether the defect is a "scratch" or a "crack" by visual inspection alone. Therefore, the operator may assign multiple suspected categories to the image to be inspected, and when processed by the method of this embodiment, the multiple suspected categories become multiple target defect categories. To address this issue, steps S630 to S640 provide a statistical judgment method based on the average distance of effective samples. Since the feature vector to be inspected has been compared with each candidate similar sample in the previous step, and effective samples that meet the sub-cluster similarity threshold condition have been selected, these effective samples constitute the "acceptable sample set" of each category corresponding to the defect to be inspected in the feature space. Step S630 groups these effective samples according to the defect category label, so that the samples in each group belong to the same semantic defect category. Step S640 calculates the arithmetic mean of the Euclidean distance between each group of effective samples and the feature vector to be inspected to obtain the average distance of each category. The average distance comprehensively reflects the overall proximity of the detected feature vector to the valid samples of each category in the feature space. The smaller the average distance, the more densely the detected feature vector is located within the feature distribution region of that category of samples, thus increasing the confidence level of that category as the category to which the detected image belongs. Therefore, selecting the target defect category with the smallest average distance as the final determination result can effectively reduce the discrimination bias caused by individual noisy samples and improve the accuracy and robustness of multi-category determination.

[0048] Specifically, for each target category The effective result set obtained based on the above-mentioned embodiments Calculate the average similarity distance of all valid samples in this class: ······ ; Mode middle, For this category, For the first The Euclidean distance of each sample. The smaller the average distance, the higher the similarity between the defect to be detected and the overall category. The rule for determining the category to which a defect belongs is: ······ ; The system selects the category with the smallest average similarity distance as the final category to which the defect to be inspected is assigned. The system can output the determination result and arrange the similar images of the assigned category first, while the similar images of the remaining categories are arranged in order of increasing average distance.

[0049] According to an embodiment of a second aspect of the present invention, a defect category determination system is provided, the system specifically comprising: The sample acquisition module is used to acquire a defect sample library, which includes multiple sample feature vectors and a defect category label corresponding to each sample feature vector. The clustering module is used to group the feature vectors of samples with the same defect category label into the same sample set, and to cluster the feature vectors of samples in each sample set to obtain multiple feature sub-clusters; The threshold generation module is used to determine the similarity threshold corresponding to each feature sub-cluster based on the distance distribution of the sample feature vectors within each feature sub-cluster. The image acquisition module is used to acquire the feature vector of the image to be detected and determine the category of the target defect; The retrieval module is used to retrieve a preset number of sample feature vectors from the defect sample library that belong to the target defect category and have the smallest distance to the feature vector to be detected, as candidate similar samples; The category determination module is used to determine the defect category of the image to be detected based on the sample feature vectors in the candidate similar samples whose distance from the feature vector to be detected is less than the corresponding similarity threshold.

[0050] It should be noted that the defect category determination system provided in this embodiment of the invention implements a clustering-based similarity threshold defect category determination function. After acquiring a feature vector library containing category labels, the system performs internal clustering of similar features and automatically generates a specific similarity threshold based on the distance distribution within the sub-clusters, thereby establishing a correlation between the determination criteria and the actual distribution of samples. After extracting features from the image to be detected and initially screening candidate similar samples within the target category range, the system performs validity screening based on the threshold of the sub-cluster to which each candidate similar sample belongs, and finally determines the defect attribution based on the valid samples. This scheme enables the similarity determination threshold to adaptively adjust with the feature distribution of defect samples, thereby solving the problem of inaccurate determination of fixed thresholds due to morphological differences in similar defects.

[0051] like Figure 3 As shown, Figure 3This is a structural diagram of an electronic device provided in one embodiment of the present invention. The present invention also provides an electronic device, comprising: The processor 801 can be implemented using a general-purpose central processing unit (CPU), microprocessor, application specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of this application. The memory 802 can be implemented as a read-only memory (ROM), static storage device, dynamic storage device, or random access memory (RAM). The memory 802 can store the operating system and other applications. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 802 and is called and executed by the processor 801 using the defect category determination method of the embodiments of this application. The 803 input / output interface is used to implement information input and output. The communication interface 804 is used to enable communication and interaction between this device and other devices. Communication can be achieved through wired means (such as USB, network cable, etc.) or wireless means (such as mobile network, WIFI, Bluetooth, etc.). Bus 805 transmits information between various components of the device (e.g., processor 801, memory 802, input / output interface 803, and communication interface 804); The processor 801, memory 802, input / output interface 803, and communication interface 804 are connected to each other within the device via bus 805.

[0052] It should be noted that the electronic device that performs the defect category determination method in the embodiments of the present invention, as well as any other electronic device that can be used to perform the method, are not specifically limited in the present invention.

[0053] Memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory may optionally include memory remotely located relative to the processor, and these remote memories can be connected to the processor via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof. The device embodiments described above are merely illustrative, and the units described as separate components may or may not be physically separate, and may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.

[0054] It will be understood by those skilled in the art that all or some of the steps and systems in the methods disclosed above can be implemented as software, firmware, hardware, and suitable combinations thereof. Some or all of the physical components can be implemented as software executed by a processor, such as a central processing unit, digital signal processor, or microprocessor, or as hardware, or as an integrated circuit, such as an application-specific integrated circuit. Such software can be distributed on a computer-readable medium, which can include computer storage media (or non-transitory media) and communication media (or transient media). As is known to those skilled in the art, the term computer storage media includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storing information (such as computer-readable instructions, data structures, program modules, or other data). Computer storage media includes, but is not limited to, RAM, ROM, EEPROM, flash memory or other memory technologies, CD-ROM, digital versatile disc (DVD) or other optical disc storage, magnetic cartridges, magnetic tape, disk storage or other magnetic storage devices, or any other medium that can be used to store desired information and is accessible to a computer. Furthermore, as is known to those skilled in the art, communication media typically include computer-readable instructions, data structures, program modules, or other data in modulated data signals such as carrier waves or other transmission mechanisms, and may include any information delivery medium.

[0055] The embodiments of the present invention have been described in detail above with reference to the accompanying drawings. However, the present invention is not limited to the above embodiments. Within the scope of knowledge possessed by those skilled in the art, various changes can be made without departing from the spirit of the present invention.

Claims

1. A method for determining defect categories, characterized in that, include: Obtain a defect sample library, wherein the defect sample library includes multiple sample feature vectors and a defect category label corresponding to each sample feature vector; Sample feature vectors with the same defect category label are grouped into the same sample set, and the sample feature vectors in each sample set are clustered to obtain multiple feature sub-clusters. Based on the distance distribution of the sample feature vectors within each feature sub-cluster, a similarity threshold corresponding to each feature sub-cluster is determined; Obtain the feature vector of the image to be detected and determine the category of the target defect; From the defect sample library, a preset number of sample feature vectors that belong to the target defect category and have the smallest distance to the feature vector to be detected are retrieved as candidate similar samples; Based on the sample feature vectors in the candidate similar samples whose distance from the feature vector to be detected is less than the corresponding similarity threshold, the defect category to which the image to be detected belongs is determined.

2. The defect category determination method according to claim 1, characterized in that, Prior to the step of obtaining the defect sample library, the following steps are included: Obtain defect sample images and preprocess the defect sample images; Feature extraction is performed on defect sample images based on pre-trained convolutional neural networks to obtain the sample feature vector; The sample feature vectors are associated with the corresponding defect category labels to form the defect sample library.

3. The defect category determination method according to claim 1 or 2, characterized in that, The step of determining the similarity threshold corresponding to each feature sub-cluster based on the distance distribution of the sample feature vectors within each feature sub-cluster includes: Calculate the Euclidean distance from the feature vector of each sample within the feature sub-cluster to the cluster center, and use the average value of the Euclidean distance as the average distance threshold; And / or, use the maximum value of the Euclidean distance as the maximum distance threshold.

4. The defect category determination method according to claim 1 or 2, characterized in that, The step of retrieving a predetermined number of sample feature vectors from the defect sample library that belong to the target defect category and have the smallest distance to the feature vector to be detected, as candidate similar samples, includes: The number of candidate similar samples to be retrieved is determined, and the number of retrievals is the maximum value among the first number, the second number, and the third number; wherein, the first number is a preset lower limit for the number of retrievals, the second number is a preset minimum number of valid returned samples, and the third number is the product of a preset redundancy coefficient and the total number of sample feature vectors belonging to the target defect category in the defect sample library; Based on the number of searches, select the number of sample feature vectors that are closest to the feature vector to be detected from the sample feature vectors belonging to the target defect category, and use them as the candidate similar samples.

5. The defect category determination method according to claim 4, characterized in that, The step of retrieving a predetermined number of sample feature vectors from the defect sample library that belong to the target defect category and have the smallest distance to the feature vector to be detected, as candidate similar samples, further includes: The number of sample feature vectors among the candidate similar samples that satisfy the corresponding similarity threshold is obtained statistically. When the number of sample feature vectors is less than the preset minimum number of returned samples, the number of searches is increased, and the sample feature vectors belonging to the target defect category in the defect sample library are searched again to obtain supplementary candidate similar samples; The supplementary candidate similar samples are arranged in ascending order of their distance from the feature vector to be detected, and are added to the candidate similar samples in sequence until the number of the sample feature vectors in the candidate similar samples that meet the similarity threshold reaches the minimum number of returned samples.

6. The defect category determination method according to claim 1 or 2, characterized in that, The step of determining the defect category of the image to be detected based on the sample feature vectors in the candidate similar samples whose distance to the feature vector to be detected is less than the corresponding similarity threshold further includes: A global default threshold is preset; If the similarity threshold cannot be determined based on the feature subcluster to which the candidate similar sample belongs, then the global default threshold shall be used as the similarity threshold corresponding to the candidate similar sample.

7. The defect category determination method according to claim 1 or 2, characterized in that, When there are multiple target defect categories, the step of determining the defect category of the image to be detected based on the sample feature vectors in the candidate similar samples whose distance to the feature vector to be detected is less than the corresponding similarity threshold includes: The sample feature vectors whose distance from the feature vector to be detected is less than the corresponding similarity threshold are grouped according to their corresponding defect category labels; The average distance between the sample feature vector and the target feature vector in each group is calculated, and the target defect category corresponding to the group with the smallest average distance is determined as the defect category to which the image to be detected belongs.

8. A defect category determination system, characterized in that, include: The sample acquisition module is used to acquire a defect sample library, wherein the defect sample library includes multiple sample feature vectors and a defect category label corresponding to each sample feature vector; The clustering module is used to group sample feature vectors with the same defect category label into the same sample set, and to cluster the sample feature vectors in each sample set to obtain multiple feature sub-clusters. The threshold generation module is used to determine the similarity threshold corresponding to each feature sub-cluster based on the distance distribution of the sample feature vectors within each feature sub-cluster; The image acquisition module is used to acquire the feature vector of the image to be detected and determine the category of the target defect; The retrieval module is used to retrieve a preset number of sample feature vectors from the defect sample library that belong to the target defect category and have the smallest distance to the feature vector to be detected, as candidate similar samples; The category determination module is used to determine the defect category of the image to be detected based on the sample feature vectors in the candidate similar samples whose distance from the feature vector to be detected is less than the corresponding similarity threshold.

9. An electronic device, characterized in that, It includes a memory and a processor, the memory being used to store at least one program, and the processor being used to load the at least one program to execute the defect category determination method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions for causing a computer to perform the defect category determination method as described in any one of claims 1 to 7.