Abnormal sample generation method
By generating abnormal samples in industrial quality inspection, the problem of difficulty in obtaining samples is solved. By randomly generating closed areas in normal samples and combining them with background images to generate simulated defect images, the model robustness and overfitting problems are solved. Improved the adaptability of the detection model.
Patent Information
- Application Number
- CN202411559633.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-04
- Publication Date
- 2024-12-06
- Estimated Expiration
- 2044-11-04
AI Technical Summary
In the field of industrial quality inspection, it is difficult to obtain sufficient abnormal samples, resulting in a lack of robustness in deep learning models when training, especially because the size and shape of abnormal samples are not fixed, making it difficult to simulate through normal samples.
By randomly generating smooth closed areas in normal samples and combining them with background images in a weighted fusion manner, a simulated defect image is generated for image enhancement, and a diverse anomaly samples are obtained.
It provides rapid generation of rich near-real anomalies samples, solves the model overfitting problem, improves the robustness of the detection model and adapts to light changes.
Smart Images

Figure CN119091255A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of industrial quality inspection, and in particular to a method for generating abnormal samples. Background Art
[0002] In the field of industrial quality inspection, models need to be trained to detect various abnormalities. If a deep learning model is used, sufficient samples are required for training. Normally, we can easily obtain normal samples, but we cannot obtain sufficient abnormal samples or all types of abnormalities. In order to improve the robustness of the algorithm, we urgently need to use normal samples to simulate abnormal samples. For example, among the surface defects of magnetic tiles, both pores and wear defects are darker than the surrounding area, but the size and shape are not fixed. For another example, in the quality inspection of cards, since the cards need to be coated, if the film is damaged, then under normal lighting, the damaged position will be brighter than the normal position. Similarly, the position, size and shape of the damage are not fixed.
[0003] Therefore, in the process of automated detection, a large number of abnormal samples need to be collected to train a robust model. Summary of the invention
[0004] In order to solve the technical problem of a large number of abnormal sample requirements, the present invention provides an abnormal sample generation method. The following technical solution is adopted: A method for generating an abnormal sample comprises the following steps: Step 1, generate a smooth closed area at a random position in the normal sample; Step 2, selecting the closed area in step 1 as the location where the defect occurs, and determining the type of the background image according to the type of the defect; Step 3, randomly select a position in the background image and cut out a closed area of the same shape; Step 4, pasting the extracted closed area to the normal sample in a weighted fusion manner to obtain a defect image; Step 5: Perform image enhancement on the defect image to obtain abnormal samples.
[0005] By adopting the above technical solution, a smooth closed area is first generated at a random position in the normal sample, and then a background image is randomly selected, and a position is randomly selected in the background image to cut out a closed area of the same shape, and then the cut-out area is pasted to the normal sample in a weighted fusion manner to obtain a defect image, and finally the defect image is enhanced to obtain an abnormal sample; Using the proposed solution, multiple abnormal samples can be quickly obtained based on normal samples, providing sufficient abnormal training samples for the detection model in the field of industrial quality inspection. The detection, classification or segmentation model can be trained based on the generated abnormal samples and normal samples, which can solve the problem of model overfitting caused by the lack of abnormal samples. Optionally, step 1 generates a smooth closed area by the following steps: Step 11, randomly select a point within the normal sample image range; Step 12, with the selected point as the center, generate the x-coordinates and y-coordinates of N points respectively, where the x-coordinates and y-coordinates obey Gaussian distribution, uniform distribution, Beta distribution, Poisson distribution or two-dimensional normal distribution, and obtain a set of N points; the distribution type of the x-coordinates and y-coordinates is not limited here.
[0006] Step 13, obtain the convex hull of the point set; Step 14, generating smooth curves based on the points of the convex hull, and these smooth curves form a closed area.
[0007] By adopting the above technical solution, a variety of closed areas can be obtained, the size and shape of the area are random, and the distribution type obeyed by the x-coordinate and y-coordinate can be set according to the actual cause of the defect, and the distribution parameters can be adjusted appropriately to simulate the generation process of the real defect as much as possible. Based on this, a variety of abnormal samples can be obtained.
[0008] Optionally, use the ConvexHull function from the scipy library to compute the convex hull.
[0009] Optionally, use the splprep function of the advanced scientific computing library scipy to perform a smooth curve fitting on the points on the convex hull.
[0010] Optionally, the type of the background image is determined according to the type of the defect, and the background image is a pure color image, a natural image, or is determined according to the actual defect type.
[0011] Optionally, enhancement methods include adjusting light intensity, saturation and hue, horizontal flipping and vertical flipping.
[0012] By adopting the above technical solutions and adjusting the light intensity, saturation and chroma, we can simulate samples obtained under different lighting conditions, enrich the lighting environment of the generated samples, and use the enhanced sample training to improve the model's robustness to lighting. Horizontal flipping and vertical flipping can increase the number of training samples on the one hand, and increase the model's robustness to horizontal flipping and vertical flipping on the other.
[0013] Optionally, in step 4, the weighted fusion weight coefficient k is a random number in the range of [0,1], and the defect image is calculated as follows: ; in is a normal sample image, is the background image, and k is the weighting coefficient.
[0014] By adopting the above technical solution, the weighted fusion coefficient of the normal sample image and the background image in the selected closed area can be flexibly adjusted, so that the generated defect image is more diversified and more in line with the actual defect generation process.
[0015] In summary, the present invention includes at least the following beneficial technical effects: The present invention can provide a method for generating abnormal samples, firstly generating a smooth closed area at a random position in a normal sample, then randomly selecting a background image, randomly selecting a position in the background image, cutting out a closed area of the same shape, then pasting the cut-out area to the normal sample in a weighted fusion manner to obtain a defect image, and finally performing image enhancement on the defect image to obtain an abnormal sample; the proposed method can quickly obtain a large number of rich and close-to-real abnormal samples based on normal samples, thereby solving the problem in the field of industrial quality inspection that a deep learning model cannot be trained due to a lack of abnormal samples or the trained model is overfitted. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 It is a flow chart of a method for generating abnormal samples of the present invention; Figure 2 This is a schematic diagram of the effect of a real abnormal sample in a specific embodiment of a method for generating an abnormal sample of the present invention, the left picture is a pore defect, and the right picture is a wear defect; Figure 3 This is a schematic diagram of the effect of using real normal samples to generate abnormal samples in a specific embodiment of an abnormal sample generation method of the present invention. The left picture is a real normal sample, and the three pictures on the right are generated abnormal samples. DETAILED DESCRIPTION
[0017] The present invention is further described in detail below in conjunction with the accompanying drawings. An embodiment of the present invention discloses a method for generating an abnormal sample.
[0018] Reference Figure 1-Figure 3 , a method for generating an abnormal sample, comprising the following steps: Step 1, generate a smooth closed area at a random position in the normal sample; Step 2, selecting the closed area in step 1 as the location where the defect occurs, and determining the type of the background image according to the type of the defect; Step 3, randomly select a position in the background image and cut out a closed area of the same shape; Step 4, pasting the extracted closed area to the normal sample in a weighted fusion manner to obtain a defect image; Step 5: Perform image enhancement on the defect image to obtain abnormal samples.
[0019] First, a smooth closed area is generated at a random position in the normal sample, then a background image is randomly selected, and a position is randomly selected in the background image to cut out a closed area of the same shape. Then, the cut-out area is pasted to the normal sample in a weighted fusion manner to obtain a defect image. Finally, the defect image is enhanced to obtain an abnormal sample. Using the proposed method, multiple abnormal samples can be quickly obtained based on normal samples, providing sufficient and diverse abnormal samples for the detection model in the field of industrial quality inspection. Training the detection, classification or segmentation model based on the generated abnormal samples and normal samples can solve the problem of model overfitting caused by the lack of abnormal samples. Step 1 Use the following steps to generate a smooth closed area: Step 11, randomly select a point within the normal sample image range; Step 12, with the selected point as the center, generate the x-coordinates and y-coordinates of N points respectively, where the x-coordinates and y-coordinates obey Gaussian distribution, uniform distribution, Beta distribution, Poisson distribution or two-dimensional normal distribution, and obtain a set of N points; the distribution type of the x-coordinates and y-coordinates is not limited here.
[0020] Step 13, obtain the convex hull of the point set; Step 14, generating smooth curves based on the points of the convex hull, and these smooth curves form a closed area.
[0021] A variety of closed areas can be obtained, and the size and shape of the area are random. The distribution type of the x-coordinate and y-coordinate can be set according to the actual cause of the defect, and the distribution parameters can be adjusted appropriately to simulate the generation process of the real defect as much as possible. Based on this, a variety of abnormal samples can be obtained.
[0022] The convex hull is calculated using the ConvexHull function of the scipy library.
[0023] Use the splprep function of the advanced scientific computing library scipy to perform smooth curve fitting on the points on the convex hull.
[0024] The type of the background image is determined according to the type of the defect. The background image is a pure color image, a natural image, or is determined according to the actual defect type.
[0025] Enhancement methods include adjusting light intensity, saturation and hue, horizontal flipping and vertical flipping.
[0026] Adjusting the light intensity, saturation, and chroma can simulate samples obtained under different lighting conditions, enrich the lighting environment of the generated samples, and use the enhanced sample training to improve the model's robustness to lighting. Horizontal flipping and vertical flipping can increase the number of training samples on the one hand, and increase the model's robustness to horizontal flipping and vertical flipping on the other.
[0027] In step 4, the weighted fusion coefficient k is a random number in the range of [0,1], and the defect image is calculated as follows: ; in is a normal sample image, is the background image, and k is the weighting coefficient.
[0028] The weighted fusion coefficients of the normal sample image and the background image in the selected closed area can be flexibly adjusted to make the generated defect image more diverse and more in line with the actual defect generation process.
[0029] The following is an implementation principle of an abnormal sample generation method based on an embodiment: The surface defects of the magnetic tile include pores and wear. Both defects are darker than the surrounding area. The real sample is as follows Figure 2 shown.
[0030] Since there are few real abnormal samples, it is difficult to obtain abundant abnormal samples to train a robust anomaly detection / segmentation model. To solve this problem, the following method can be used to generate abnormal samples.
[0031] Step 1: Get a normal sample and generate a smooth closed area at a random position of the normal sample as follows: Randomly select a point within the normal sample image range with coordinates x0, y0; With this point as the center, a point set of 80 random points is generated, where the x coordinates of these random points follow a Gaussian distribution with a mean of x0 and a variance of d1, and the y coordinates follow a Gaussian distribution with a mean of y0 and a variance of d2. By adjusting the values of d1 and d2, regions of different sizes and shapes can be obtained.
[0032] Get the convex hull of the point set. You can use the ConvexHull function of the advanced scientific computing library scipy to calculate the convex hull.
[0033] Based on the points on the convex hull, the spline interpolation algorithm is used to generate smooth curves, which form a closed area. The splprep function of the advanced scientific computing library scipy can be used to fit the points on the convex hull with smooth curves.
[0034] Step 2: Select the closed area in step 1 as the location where the defect occurs. Since the defect is darker than the surrounding area, the background image can be set to a pure black image. The background image here can also be a real defect image.
[0035] Step 3, randomly select a position in the background image and cut out a closed area of the same shape. Since the background image is a pure black image, only a pure black image of the same size as the normal sample is needed to cut out a closed area of the same shape at the same position.
[0036] Step 4: Paste the cut-out area into the normal image in a weighted fusion manner to obtain an abnormal sample. The weighting coefficient can be set to a random number between 0 and 1.
[0037] Steps 3 and 4 are equivalent to the following operations: The weighting coefficient k is a random number in the range [0,1].
[0038] The final defect image is calculated as: ; normal_img is the normal sample image, back_img is the background image, and a pure black image is used here.
[0039] Image enhancement is performed on abnormal samples. The enhancement methods include adjusting the light intensity, saturation and chroma, horizontal flipping and vertical flipping. Since the real sample is a grayscale image, only the light intensity needs to be adjusted, and the saturation and chroma do not need to be adjusted. Horizontal flipping and vertical flipping can be performed.
[0040] The abnormal samples generated according to the above steps are as follows Figure 3 shown.
[0041] The above are all preferred embodiments of the present invention, and are not intended to limit the protection scope of the present invention. Therefore, any equivalent changes made based on the structure, shape, and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for generating abnormal samples, characterized in that: The following steps are involved: Step 1, generate a smooth closed area at a random position in the normal sample; Step 2, selecting the closed area in step 1 as the location where the defect occurs, and determining the type of the background image according to the type of the defect; Step 3, randomly select a position in the background image and cut out a closed area of the same shape; Step 4, pasting the extracted closed area to the normal sample in a weighted fusion manner to obtain a defect image; Step 5: Perform image enhancement on the defect image to obtain abnormal samples.
2. The method for generating an abnormal sample according to claim 1, characterized in that: Step 1 Generate a smooth closed area using the following steps: Step 11, randomly select a point within the normal sample image range; Step 12, taking the selected point as the center, respectively generating x coordinates and y coordinates of N points, wherein the x coordinates and y coordinates obey Gaussian distribution, uniform distribution, Beta distribution, Poisson distribution or two-dimensional normal distribution, to obtain a set of N points; Step 13, obtain the convex hull of the point set; Step 14, generating smooth curves based on the points of the convex hull, and these smooth curves form a closed area.
3. The method for generating an abnormal sample according to claim 2, characterized in that: In step 13, the convex hull is calculated using the ConvexHull function of the advanced scientific computing library scipy.
4. The method for generating an abnormal sample according to claim 3, characterized in that: In step 14, the splprep function of the advanced scientific computing library scipy is used to perform smooth curve fitting on the points on the convex hull.
5. The method for generating an abnormal sample according to claim 1, characterized in that: In step 2, the type of the background image is determined according to the type of the defect. The background image is a pure color image, a natural image, or is determined according to the actual defect type.
6. The method for generating an abnormal sample according to claim 1, characterized in that: In step 5, the enhancement methods include adjusting light intensity, saturation and hue, horizontal flipping and vertical flipping.
7. The method for generating an abnormal sample according to claim 1, characterized in that: In step 4, the weighted fusion coefficient k is a random number in the range of [0,1], and the defect image is calculated as follows: ; in is a normal sample image, is the background image, and k is the weighting coefficient.
Citation Information
Patent Citations
Interactive data extension method based on Poisson image fusion and image stylization
CN111199531A
Defect sample generation method and device, electronic equipment and storage medium
CN111681162A
GAN neural network multi-distortion suppression model based on coordinate attention mechanism
CN116629337A
Defect zero sample generation method, device and equipment based on image generation mode
CN118247602A
Defect detection image enhancement method based on limited random sampling strategy
CN118333888A
Cited By
Defect contour generation method of machine vision deep learning surface defect sample, electronic equipment and storage medium
CN120047438A
Building curtain wall defect intelligent detection method and related equipment
CN121527614A
