Shape defect sample generation method and system based on generative adversarial network and medium
Through the shape defect sample generation method based on generative adversarial networks, vector quantizers and discriminators are used to generate independent features, which solves the problem of difficulty in identifying unknown defect shapes in industrial products, achieving efficient data set expansion and model robustness improvement.
Patent Information
- Application Number
- CN202411171701.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-26
- Publication Date
- 2024-09-20
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Existing technology is difficult to effectively generate samples of unknown defect shapes in industrial products, making it difficult for detection models to identify new defects, and the cost of data set collection and model training is high.
A shape defect sample generation method based on a generative adversarial network is adopted to form a training data set by collecting original pictures and mask images, train a generative adversarial network model, and use vector quantizers and discriminators to generate independent features to achieve efficient generation of defect shapes.
It realizes the generation of defect samples of arbitrary shapes, expands the data set, improves the robustness of the detection model, reduces the insufficient number of data sets and training costs, and reduces economic losses.
Smart Images

Figure CN118675033A
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of computer vision, and specifically relates to a shape defect sample generation method, system and medium based on a generative adversarial network. Background Art
[0002] In today's era, science and technology are developing rapidly, especially in the manufacturing industry, where automated production technology for industrial products has become a trend. However, in this automated production environment, it is inevitable to produce defective products, such as bubbling, cracks, dust on metal O-rings, adhesion and corrosion on rubber products. These problems have attracted the attention of the manufacturing industry to defect detection, hoping to solve the problems. As a result, iconic efficient defect detection models such as faster-rcnn and yolo series were born.
[0003] However, the above-mentioned AI-based detection methods have relatively high requirements for data sets. Generally speaking, manufacturers need to collect hundreds of pictures of defective products in order to train a satisfactory detection model. However, collecting pictures of such defective products is time-consuming and labor-intensive, and the time cost is very high. In order to accelerate the acquisition of data sets, data enhancement methods are needed. At the same time, there is a more serious problem, that is, the occurrence of defects is very random. This is because of objective factors such as immature manufacturing processes and complex production environments, and the defect shapes on the produced products show great randomness. The detection model trained by the pictures collected at this stage may encounter new defect shapes that exceed the range of previously collected data in the future, resulting in the inability of the detection model to recognize this defect. Therefore, we need to prejudge the defect shapes that may appear and generate them using a generative network. This way of solving problems in advance helps to reduce economic losses, accelerate the collection of data sets, and avoid the time cost of retraining the detection model.
[0004] At present, a large number of generative models have emerged, but none of them are suitable for solving the above problems. For example, traditional generative adversarial models and diffusion models can only generate data for the distribution of training data sets, but cannot generate data outside the distribution. The iconic models are StyleGAN2, StyleGAN3, latent diffusion, etc. Conditional control generative adversarial models generally require detailed conditional information, and the generation effect depends entirely on the level of detail of the given conditions. However, detailed conditional information requires a lot of time to produce, such as MaskGAN. Vector quantization models cannot efficiently generate usable images. Often, only a few of 100 images are usable, and the randomness is too large, such as VQ-GAN. At the same time, if PS technology is used for defect deformation, it is not only time-consuming and labor-intensive, but also very costly and has a low cost-performance ratio. Summary of the invention
[0005] The purpose of the present invention is to provide a shape defect sample generation method, system and medium based on a generative adversarial network, aiming to solve the above-mentioned problems.
[0006] The present invention is mainly achieved through the following technical solutions: The shape defect sample generation method based on the generative adversarial network includes the following steps: Step S1: collect original images containing defects, and generate mask images of defects in the original images; form a training data set based on the original images and the mask images; Step S2: training a generative adversarial network model based on a training data set; Step S21: input the mask image into the encoder, extract the features of the mask image, reduce the spatial dimension of the mask image, and obtain a reduced-dimensional image feature tensor; Step S22: inputting the reduced-dimensional image feature tensor into a vector quantizer, discretizing the feature tensor, and reconstructing a new feature tensor, wherein each point in the new feature tensor is independent of each other; calculating the vector quantization loss between the feature tensor output by the encoder and the feature tensor reconstructed by the vector quantizer; Step S23: input the reconstructed new feature tensor into the decoder to restore it to a generated image that conforms to the original data distribution; calculate the reconstruction loss between the generated image and the original image; Step S24: a real image is obtained by concatenating the mask image and the original image, and a fake image is obtained by concatenating the mask image and the generated image, wherein the concatenation operation is to concatenate the color channels of the two images; the real image and the fake image are input into the discriminator to judge the authenticity and calculate the adversarial loss; Step S25: repeating steps S21 to S24, optimizing the total loss using an optimizer until the loss function converges, and obtaining a trained generative adversarial network model; the total loss is the sum of vector quantization loss, reconstruction loss, and adversarial loss; Step S3: Input mask images of different defect shapes into the trained generative adversarial network model to generate images with defects of corresponding shapes.
[0007] In order to better implement the present invention, further, step S22 includes the following steps: Step S221: Assume that the output feature tensor of the encoder is i , whose tensor size is ( B , Dim , h , w ); the vector quantizer is composed of a two-dimensional vector, denoted as c , whose dimensions are ( N , Dim ); in: B is the number of images for one training; Dim is the number of channels; N is the number of rows of the vector; h and w are the width and height of the feature tensor respectively; Step S222: Reshape the feature tensor i size, so that it becomes ( B * h * w , Dim ),at this time i is a two-dimensional vector. If each row is regarded as a point, there are B*w*h points, each of which consists of Dim values. Similarly, c have N points, each of which consists of Dim The value is composed of; Step S223: From i Select the first point in c Compare each point in the e 1. Repeat this operation, from c Obtained B * h * w point, denoted as { e 1, e 2, ..., e B*h*w}; Step S224: e 1, e 2, ..., e B*h*w Each point in} is composed of Dim values. e 1, e 2, ..., e B*h*w} is considered as a size of ( B * h * w , Dim ) and reshape it again to a tensor of size ( B , Dim , h , w ) e , and as the output of the vector quantizer; the tensor e is a tensor i The vector quantization result of eEach row in is independent of each other.
[0008] In order to better implement the present invention, further, in step S22, the vector quantization loss is: in: d H () is Hausdorff distance; sp [] is to terminate the gradient back propagation, α is a hyperparameter.
[0009] In order to better implement the present invention, further, in step S23, the reconstruction loss is: in: G To generate a module, m is the mask image, G ( m ) is used to generate the image. x is the corresponding mask image m The original picture.
[0010] In order to better implement the present invention, further, in step S24, the adversarial loss is: Where: D is the discriminator.
[0011] In order to better implement the present invention, further, in step S1, the background of the mask image is filled with black pixels, and the defective area is filled with other colors, and different defects are filled with different colors; the mask image and the original image have the same size.
[0012] The present invention is mainly achieved through the following technical solutions: The shape defect sample generation system based on generative adversarial network is based on the above-mentioned shape defect sample generation method based on generative adversarial network, and includes a data acquisition module, a training module and a generation module; the data acquisition module is used to collect original pictures containing defects, make mask pictures for the defects of the original pictures, and form a training data set based on the original pictures and the mask pictures; the training module is used to construct a generative adversarial network model, and train the generative adversarial network model with the training data set; the generation module is used to input mask pictures of different defect shapes into the trained generative adversarial network model to generate pictures with corresponding shape defects.
[0013] In order to better implement the present invention, further, in step S2, the generative adversarial network model includes a generation module and a discriminator, the generation module includes an encoder, a vector quantizer and a decoder, the encoder includes a front-end processing block and n down-sampling blocks arranged in sequence from front to back, the decoder includes n up-sampling blocks and a back-end processing block arranged in sequence from front to back, and the down-sampling blocks and the up-sampling blocks are jump-connected by corresponding cascade operations, so that the picture features output by the down-sampling blocks at the back end and the picture features output by the corresponding up-sampling blocks at the front end are cascaded as the input of the up-sampling blocks of the next layer; the down-sampling block at the back end is connected to the up-sampling block at the front end through a vector quantizer.
[0014] In order to better realize the present invention, further, the vector quantizer is composed of an embedding layer, which is composed of a two-dimensional vector, used for the output of the discrete encoder, and the result after discrete reconstruction is used as the input of the decoder; the front-end processing block includes a convolution layer, and the downsampling block includes a leakyrelu activation function, a convolution layer and a batch normalization layer arranged in sequence from front to back; the back-end processing block includes a deconvolution layer and a tanh activation function arranged in sequence from front to back, the upsampling block includes a relu activation function, a deconvolution layer and a batch normalization layer arranged in sequence from front to back, and the first three upsampling blocks also include a random drop layer arranged at the tail.
[0015] A computer-readable storage medium stores a computer program, which, when executed by a processor, implements the above-mentioned shape defect sample generation method based on a generative adversarial network.
[0016] The beneficial effects of the present invention are as follows: (1) The present invention solves the problems of difficulty in collecting defect samples and large randomness of defect shapes in industrial defect detection, and realizes the ability to generate defects of arbitrary shapes. The present invention can obtain a large amount of data that exceeds the distribution of the original data set, expand the richness of the data set, reduce the impact of large randomness of defect shapes, and alleviate the problem of insufficient data sets, thereby greatly improving the robustness of the detection model and having good practicality.
[0017] (2) In the present invention, firstly, the input of the model is a mask image, rather than a traditional real image, so that we can control the generation of the defect image by modifying the defect shape of the mask image. Secondly, a jump connection of cascade operation is used between the encoder and the decoder, so that the mapping between the mask image and the generated image is closer, which determines whether the defect corresponding to the mask shape can be correctly generated. Then, the vector quantizer can be used to discretize the image features, making the internal shape, color, structure and other features independent of each other, reducing their mutual interference during image generation.
[0018] (3) At the same time, the Hausdorff distance is used to measure the degree of discretization. On the one hand, it can accelerate the convergence speed of the model, and on the other hand, it can avoid the problem of model collapse caused by gradient vanishing and gradient exploding, thereby improving the quality of image generation. This measurement method is much stronger than KL divergence or mean square error. Finally, the input of our discriminator is also inconsistent with the traditional image input. The real image and the generated image must be cascaded with the mask image and treated as a whole before they can be input into the discriminator. The purpose is to further deepen the control ability of the mask image over the defect shape on the generated image.
[0019] (4) The vector quantizer is used to discretize the input image features into independent features by training a network composed of two-dimensional vectors, so that the shape features can be modified without affecting the generation of other features such as color and texture. In the vector quantizer, the present invention uses the Hausdorff distance to measure the distance between distributions, which greatly improves the problem that the traditional KL divergence will become 0 or ∞, and effectively improves the generation quality. The present invention can change the shape of product part defects and generate product images with the same structure and texture but different defect shapes. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] Figure 1 A schematic diagram of the overall structure of the generative adversarial network model of the present invention; Figure 2 A schematic diagram of the structure of the discriminator in the generative adversarial network model of the present invention; Figure 3 A schematic diagram of the structure of a generation module in a generative adversarial network model of the present invention; Figure 4 It is a schematic diagram of the structure of a vector quantizer; Figure 5 Schematic diagram of the original picture in the training data set; Figure 6 Schematic diagram of the corresponding original image and mask image; Figure 7 A schematic diagram of the working principle of the generative adversarial network model of the present invention; Figure 8 Generate images of different defect shapes output by the adversarial network model of the present invention; Fig. 9 The generated images output by Example 2 and other comparison models; Fig.10 This is the generated image output after removing different factors in the ablation experiment of Example 2. DETAILED DESCRIPTION
[0021] Embodiment 1: A shape defect sample generation system based on a generative adversarial network includes a data acquisition module, a training module and a generation module. The data acquisition module is used to collect pictures with defects and create mask images for the defects, and use the pictures with defects and the mask images as training data sets; the training module is used to train a generative adversarial network model using the training data sets; the generation module is used to use the mask images of any defect shape to input into the trained generative adversarial network model to obtain the required generated pictures.
[0022] Preferably, the training data set consists of an original image and a mask image corresponding to its defects. The size of the mask image is consistent with that of the original image, and its background is filled with black pixels. The defect area corresponding to the original image is filled with other colors, and different defects are filled with different colors, thereby forming a mask image corresponding to the original image.
[0023] like Figure 1 As shown, the network model includes a generation module and a discriminator. The generation module includes an encoder, a vector quantizer and a decoder arranged in sequence from front to back. The encoder and the decoder are connected by a jump connection of a cascade operation. The decoder outputs a generated picture and inputs it to the discriminator. The present invention inputs the mask image into the encoder to obtain the reduced-dimensional picture features, inputs it into the vector quantizer, obtains the feature tensor after discrete reconstruction, and then inputs it into the decoder to obtain the reconstructed generated picture that conforms to the distribution of the original picture. The generated picture and the original picture are input into the discriminator to judge the true or false. Specifically, the encoder is used to extract features and perform spatial dimensionality reduction on the input mask image; the vector quantizer is used to discretize the feature tensor extracted by the encoder so that the features therein are independent of each other; the decoder is used to restore the feature tensor after discrete reconstruction to a picture that conforms to the distribution of the original data. The discriminator is used to identify the true or false of the picture put into it, and give a score, so as to assist the generation module in updating the network parameters in a confrontational form.
[0024] Preferably, if Figure 3As shown, the encoder is composed of a front-end processing block and several down-sampling blocks; the decoder is composed of several up-sampling blocks and a back-end processing block; the up-sampling block and the down-sampling block are connected by a cascade operation for jump connection, which is used to cascade the picture features output by the down-sampling block and the picture features output by the corresponding up-sampling block as the input for the next up-sampling. The up-sampling block is related to the number of down-sampling blocks and the input picture size. The cascade operation is that if there are two tensors with a size of (1, 3, 512, 512), then after cascading these two tensors, they will become a tensor with a size of (1, 6, 512, 512). The vector quantizer is composed of an embedding layer, which is composed of a two-dimensional vector, used for the output of the discrete encoder, and the result after discrete reconstruction is used as the input of the decoder. The initial value of this vector is initialized by an even distribution.
[0025] Specifically, the front-end processing block is composed of only one convolution layer, and the back-end processing block includes a deconvolution layer and a tanh activation function arranged in sequence from front to back. The downsampling block includes a leakyrelu activation function, a convolution layer, and a batch normalization layer arranged in sequence from front to back. The upsampling layer includes a relu activation function, a deconvolution layer, and a batch normalization layer arranged in sequence from front to back. At the same time, the first three upsampling blocks are also added with a random drop layer, and the random drop layer is set at the tail of the upsampling block. The probability of dropping neurons is set to 0.5 to enhance the robustness of the model.
[0026] like Figure 4 As shown, the vector quantizer is composed of an embedding layer, which is composed of a two-dimensional vector with a size of (n, Dim), indicating that there are n rows of vectors, and the length of each row of vectors is Dim. The initial value of this two-dimensional vector is initialized by an even distribution between (-1 / 255, 1 / 255).
[0027] The working principle of the vector quantizer is: First, the input tensor is denoted as i, and its size is (B, Dim, h, w). It can be reshaped into (B*h*w, Dim), where B is the number of images trained at a time, Dim is the number of channels, and h and w are the width and height of the tensor. At this time, i is a two-dimensional vector. If each row is regarded as a point, it can be regarded as having B*w*h points, and each point is composed of Dim values. From the previous paragraph, we can see that the vector quantizer is also a two-dimensional vector, denoted as c, and its size is (n, Dim). Similarly, c can also be regarded as having n points, each point is composed of Dim values. Next, select the first point from i, compare it with each point in c, and get the point closest to it, denoted as e1, then select the second point in i and repeat the same operation to get e2, and so on. A total of B*h*w points can be obtained from c (the points obtained from c can be repeated), denoted as {e1, e2,..., e B*h*w}.
[0028] In summary, each point here is composed of Dim values, so this series of B*h*w points can be regarded as a vector of size (B*h*w, Dim), and reshaped again to become a tensor of size (B, Dim, h, w). We will record this tensor as e and use it as the output of the vector quantizer. Here, tensor e is the vector quantization result of tensor i, and each row in e is independent of each other. The output e of the vector quantizer is input into the decoder, and with the jump connection operation, the generated image corresponding to the mask map can be output.
[0029] like Figure 2 As shown, the input of the discriminator is different from the traditional adversarial generation model. The input of the discriminator of the present invention is: the whole after the cascade operation of the mask map and the original image is taken as the real image. The whole after the cascade operation of the mask map and the generated image is taken as the false image. In this way, the control of the mask map on the defect shape in the generated image can be strengthened. That is, the input received by the discriminator is the tensor after the cascade of the original image and the corresponding mask map, and the tensor after the cascade of the generated image and the corresponding mask map, which are input into the discriminator as the real image and the false image respectively. Preferably, the discriminator is composed of several convolution blocks, and the number thereof is related to the image size. The front convolution block includes a convolution layer, a batch normalization layer and a leakyrelu activation function arranged in sequence from front to back; the last convolution block is composed of only one convolution layer.
[0030] The loss of the generative adversarial network model includes the adversarial loss between the generator and the discriminator, the vector quantization loss in the vector quantizer, and the reconstruction loss between the generated image and the original image. The final loss formula of the entire model is: The Hausdorff distance is used to calculate the vector quantization loss between tensor e and tensor i. First, the Hausdorff distance calculation formula is as follows: in, d H ( X , Y ) indicates calculation X Each point x and Y The minimum distance between each point in maxd(x,Y) means to obtain d ( x , Y ) is calculated in the same way as the maximum value among a series of minimum values. maxd (y, X Finally, the maximum of the two values is obtained.
[0031] The formula for calculating the vector quantization loss using the Hausdorff distance used in the present invention is as follows: Among them, sp[] marks the termination of gradient back propagation, and α represents a hyperparameter, which is set to a smaller value here, generally 0.2. As can be seen from the above, here i represents the output of the encoder, and e represents the output of the vector quantizer. Therefore, terminating the gradient back propagation related to i in the first item of the loss means that in the back propagation, the weight parameters of the encoder will be frozen, and only the weight parameters of the vector quantizer will be optimized, so that the output result e of the vector quantizer will gradually approach the output result i of the encoder. Terminating the gradient back propagation related to e in the second item of the loss means that in the back propagation, only the weight parameters of the encoder will be updated, so that the output result i of the encoder will be close to the output result e of the vector quantizer. The purpose of this item is to slow down the optimization speed of the encoder and maintain the balance of confrontation.
[0032] The reconstruction loss calculation formula between the generated image and the original image is as follows: Among them, G represents the generation module, m represents the mask map, G(m) represents the generated image, and x represents the original image, where x and m are in correspondence.
[0033] The adversarial loss calculation formula is as follows: Where D represents the discriminator.
[0034] The present invention can make arbitrary shape changes to product defects, thereby generating a large number of defect samples with defect shapes that are not in the original data set, effectively reducing the labor cost of developing the detection model and the time cost of training the detection model. The generated images can be provided to enterprises as defective parts reserves, which also alleviates the problem of insufficient training data in the detection model to a certain extent and has good practicality.
[0035] Embodiment 2: A method for generating shape defect samples based on a generative adversarial network comprises the following steps: Step A1: Collect original images containing defects and annotate them with masks. The positions covered by the masks are the defect areas. The original images and mask images are used as the data sets required for training the generative adversarial network model.
[0036] Step A2: Input the mask image into the encoder to extract the features of the input image and reduce the spatial dimension of the image.
[0037] Step A3: Input the image feature tensor obtained in step A2 into the vector quantizer, discretize the feature tensor, and reconstruct a new feature tensor in which each point is independent of each other. The Hausdorff distance is used to calculate the vector quantization loss between the tensor output by the encoder and the tensor reconstructed by the vector quantizer.
[0038] Step A4: Input the feature tensor obtained in step A3 into the decoder to obtain the generated image. Calculate the reconstruction loss between the generated image and the original image.
[0039] Step A5: The generated image and the mask image are concatenated as a fake image, and the original image and the mask image are concatenated as a real image, and are input into the discriminator to judge the authenticity and calculate the adversarial loss.
[0040] Step A6: Repeat steps A2 to A5. Each cycle is counted as an iteration. Use the optimizer to optimize the total loss term until the loss function converges. Then, the trained generative adversarial network model can be obtained.
[0041] Step A7: Create mask images of different defect shapes and input them into the trained generative adversarial network model to generate images with defects of corresponding shapes.
[0042] The present invention selects metal circular plates as data sets for experiments to verify the ability of the generative adversarial network model. In this experiment, 132 pictures containing large oil stains were selected as the training data set, and 36 pictures containing cracks were selected as the test set. Figure 5 As shown, Figure 5 (a) is a picture of a metal circular plate with oil stain defects. Figure 5(b) is a picture of a metal circular plate with a crack defect.
[0043] Then, a mask image corresponding to the defects in the original image is created. Figure 6 As shown, Figure 6 (a) is the original image with defects. Figure 6 (b) corresponds to Figure 6 (a) The mask image of the defect in the original image. Here, the background area is covered with black, and the defect area is covered with white. If there are multiple defects, masks of multiple colors will be used to cover the defect area. The input of the present invention is different from the traditional adversarial generation model. The input is a mask image, not the original image with defects.
[0044] The mask image is input into the encoder to obtain the extracted feature tensor. At this time, this tensor is continuously entangled, denoted as i.
[0045] Input i into the vector quantizer, and the quantized tensor is recorded as e. At this time, e is discrete, and each point is independent, discretizing the continuous features in i. At this time, the improved Hausdorff distance is used to calculate the difference between e and i as the quantization loss. The formula is as follows: in, d H () is to calculate the Hausdorff distance between the two input tensors, sp [] marks the end of gradient back propagation, α Represents a hyperparameter, which is set to a smaller value, generally 0.2. The encoder is associated with i, and the vector quantizer is associated with e. Therefore, terminating the gradient back propagation related to i in the first term of the loss means that in the back propagation, the weight parameters of the encoder will be frozen, and only the weight parameters of the vector quantizer will be optimized, so that the output result e of the vector quantizer gradually approaches the output result i of the encoder.
[0046] For example, before the parameter update, i=[0.3, 0.7], e=[0, 1]. However, after the first parameter update, i is frozen and remains unchanged, but e will move a small step towards i, so e=[0.05, 0.95]. The purpose of this loss is to train the vector quantizer so that it can discretize i into a qualified tensor e with high quality. Similarly, the second term of the loss terminates the back propagation of the gradient related to e, which means that in the back propagation, only the weight parameters of the encoder are updated to make the encoder's output i close to the vector quantizer's output e, in order to slow down the encoder's optimization speed and maintain the balance of the confrontation. Because, if this term is not added, the encoder may be updated too quickly, causing the vector quantizer training to fail and fail to converge.
[0047] Input e into the decoder to get the generated image, and then calculate the reconstruction loss between the generated image and the original image. The calculation formula is as follows: in, G Represents the generated module, m represents the mask image, G ( m ) indicates generating a picture. x Represents the original picture, here x and m It is a corresponding relationship.
[0048] Finally, the original image, the generated image and the mask image are concatenated and input into the discriminator, and the adversarial loss calculation formula is as follows: Where D represents the discriminator.
[0049] The final loss formula of the entire model is: Repeat the above process and use the adam optimizer to optimize the loss term until the model converges to obtain a trained network model.
[0050] like Figure 7 and Figure 8 As shown in , inputting the mask images of defect shapes such as heart, triangle, "2" and "5" into the trained generative adversarial network model can obtain the corresponding generated images. Figure 8 As shown, Figure 8 Middle (a) - Figure 8 (c) shows the generated images of crack defects with different bending shapes. Figure 8 (d) is the generated image of the star-shaped defect. Figure 8 (e) in the middle is the generated image of the palm shape defect. Figure 8 (f) in the middle is a generated image of a bunny-shaped defect, which proves that our generative adversarial network model can generate generated images of defects of any specified shape.
[0051] like Fig. 9 As shown, Fig. 9 (a) is a generated image with crack shape defects output by this embodiment. Fig. 9 (b) is the generated image based on the VQ-GAN model. Fig. 9 (c) is the generated image based on the MaskGAN model. Fig. 9 (d) is the generated image based on the StyleGAN2 model. Fig. 9(e) is the generated image based on the StyleGAN3 model. Fig. 9 (f) in the middle is the generated image based on the latent diffsion model.
[0052] We used the same experimental conditions to conduct experiments on five iconic generative models, namely VQ-GAN, MaskGAN, StyleGAN2, StyleGAN3, and latentdiffsion, and obtained corresponding experimental results. It can be seen that the quality of the generated images output by the MaskGAN model and the VQ-GAN model is poor, and crack-type defects cannot be generated. The generated images output by the StyleGAN2 model and the StyleGAN3 model have a detailed background, but due to the small number of training data sets, the defect features cannot be learned, and thus no defects are generated. Finally, the generated images output by the latent diffsion model not only have a detailed background, but also have defects. However, the defects it generates are obviously large oil defects, not the crack defects we need. This is also because this model can only learn the shapes in the training data set, but cannot control the defect shapes. In comparison, the generative adversarial network model of the present invention can train a model with good results with only more than one hundred data, and its defect shape can also be controlled by a mask map.
[0053] In order to verify the effectiveness of the image generation of the present invention, as shown in Table 1, we generated 300 images of crack defects as training data sets, and used the original 36 crack images as test sets to train a Yolox model for verification. At the same time, we also used 5 models for comparison to generate 300 images each, and used the same experimental conditions to train a Yolox model for comparison. As can be seen from the data in Table 1, our generated images have the best effect and the trained detection model has the strongest ability.
[0054] like Fig.10 As shown, Fig.10 (a) is a generated image with crack shape defects output by this embodiment. Fig.10 (b) is a generated image with crack shape defects obtained by removing only the cascade operation in the discriminator in the network model. Fig.10 (c) is a generated image with crack-shaped defects obtained by removing only the discriminator in the network model. Fig.10 (d) is the generated image with crack shape defects obtained by removing only the Hausdorff distance in the network model. Fig.10 (e) is a generated image with crack-shaped defects obtained by removing only the vector quantizer in the network model. Fig.10(f) is a generated image with crack shape defects obtained by removing only the cascade operation of the generation module in the network model. The present invention conducted an ablation experiment and Fig.10 In (b), it can be seen that if the cascade operation in the discriminator is removed, although a little shadow of the defect is generated, it is not obvious enough. Obviously, the control ability of the mask image over defect generation has decreased. Fig.10 As can be seen in (c), when only the discriminator is removed, the generated image is very blurry and loses a lot of details. Fig.10 As can be seen in (d), for removing only the Hausdorff distance, the quality and integrity of the image are degraded. Fig.10 As can be seen in (e), when only the vector quantizer is removed, the generated image has completely lost its overall shape. This is the drawback of the features being entangled together. Therefore, a quantization method is needed to make the features of the image independent of each other and reduce interference. Fig.10 As can be seen in (f), when only the cascade operation in the generation module is removed, the model has completely lost control of the defect shape. Therefore, it is proved that each part of the generative adversarial network model is indispensable.
[0055] Table 1 The above description is only a preferred embodiment of the present invention and does not limit the present invention in any form. Any simple modification or equivalent change made to the above embodiment based on the technical essence of the present invention shall fall within the protection scope of the present invention.
Claims
1. A shape defect sample generation method based on a generative adversarial network, characterized in that: The following steps are involved: Step S1: collect original images containing defects, and generate mask images of defects in the original images; form a training data set based on the original images and the mask images; Step S2: training a generative adversarial network model based on a training data set; Step S21: input the mask image into the encoder, extract the features of the mask image, reduce the spatial dimension of the mask image, and obtain a reduced-dimensional image feature tensor; Step S22: inputting the reduced-dimensional image feature tensor into a vector quantizer, discretizing the feature tensor, and reconstructing a new feature tensor, wherein each point in the new feature tensor is independent of each other; calculating the vector quantization loss between the feature tensor output by the encoder and the feature tensor reconstructed by the vector quantizer; Step S23: input the reconstructed new feature tensor into the decoder to restore it to a generated image that conforms to the original data distribution; calculate the reconstruction loss between the generated image and the original image; Step S24: a real image is obtained by concatenating the mask image and the original image, and a fake image is obtained by concatenating the mask image and the generated image, wherein the concatenation operation is to concatenate the color channels of the two images; the real image and the fake image are input into the discriminator to judge the authenticity and calculate the adversarial loss; Step S25: repeating steps S21 to S24, optimizing the total loss using an optimizer until the loss function converges, and obtaining a trained generative adversarial network model; the total loss is the sum of vector quantization loss, reconstruction loss, and adversarial loss; Step S3: Input mask images of different defect shapes into the trained generative adversarial network model to generate images with defects of corresponding shapes.
2. The shape defect sample generation method based on generative adversarial network according to claim 1, characterized in that: The step S22 comprises the following steps: Step S221: Assume that the output feature tensor of the encoder is i , whose tensor size is ( B , Dim , h , w ); the vector quantizer is composed of a two-dimensional vector, denoted as c , whose dimensions are ( N , Dim ); in: B is the number of images for one training; Dim is the number of channels; N is the number of rows of the vector; h and w are the width and height of the feature tensor respectively; Step S222: Reshape the feature tensor i size, so that it becomes ( B * h * w , Dim ),at this time i is a two-dimensional vector. If each row is regarded as a point, there are B*w*h points, each of which consists of Dim values. Similarly, c have N points, each of which consists of Dim The value is composed of; Step S223: From i Select the first point in c Compare each point in the e 1. Repeat this operation, from c Obtained B * h * w point, denoted as { e 1, e 2, ..., e B*h*w }; Step S224: e 1, e 2, ..., e B*h*w Each point in} is composed of Dim values. e 1, e 2, ..., e B*h*w } is considered as a size of ( B * h * w , Dim ) and reshape it again to a tensor of size ( B , Dim , h , w ) e , and as the output of the vector quantizer; the tensor e is a tensor i The vector quantization result of e Each row in is independent of each other.
3. The shape defect sample generation method based on generative adversarial network according to claim 2, characterized in that: In step S22, the vector quantization loss is: in: d H () is Hausdorff distance; sp [] is to terminate the gradient back propagation, α is a hyperparameter.
4. The shape defect sample generation method based on generative adversarial network according to claim 1, characterized in that: In step S23, the reconstruction loss is: in: G To generate a module, m is the mask image, G ( m ) is used to generate the image. x is the corresponding mask image m The original picture.
5. The shape defect sample generation method based on generative adversarial network according to claim 4 is characterized in that: In step S24, the adversarial loss is: Where: D is the discriminator.
6. The shape defect sample generation method based on generative adversarial network according to claim 1, characterized in that: In step S1, the background of the mask image is filled with black pixels, and the defective area is filled with other colors, and different defects are filled with different colors; the mask image has the same size as the original image.
7. A shape defect sample generation system based on a generative adversarial network, based on the shape defect sample generation method based on a generative adversarial network according to any one of claims 1 to 6, characterized in that: It includes a data acquisition module, a training module and a generation module; the data acquisition module is used to collect original pictures containing defects, produce mask pictures for the defects in the original pictures, and form a training data set based on the original pictures and the mask pictures; the training module is used to construct a generative adversarial network model, and train the generative adversarial network model with the training data set; the generation module is used to input mask pictures of different defect shapes into the trained generative adversarial network model to generate pictures with defects of corresponding shapes.
8. The shape defect sample generation system based on generative adversarial network according to claim 7, characterized in that: In step S2, the generative adversarial network model includes a generation module and a discriminator, the generation module includes an encoder, a vector quantizer and a decoder, the encoder includes a front-end processing block and n down-sampling blocks arranged in sequence from front to back, the decoder includes n up-sampling blocks and a back-end processing block arranged in sequence from front to back, the down-sampling blocks and the up-sampling blocks are correspondingly jump-connected by cascading operations, so that the picture features output by the down-sampling blocks at the back end and the picture features output by the corresponding up-sampling blocks at the front end are cascaded as the input of the up-sampling blocks of the next layer; the down-sampling block at the rear end is connected to the up-sampling block at the front end through a vector quantizer.
9. The shape defect sample generation system based on generative adversarial network according to claim 8, characterized in that: The vector quantizer is composed of an embedding layer, which is composed of a two-dimensional vector and is used for the output of the discrete encoder, and the result after discrete reconstruction is used as the input of the decoder; the front-end processing block includes a convolution layer, and the downsampling block includes a leakyrelu activation function, a convolution layer and a batch normalization layer arranged in sequence from front to back; the back-end processing block includes a deconvolution layer and a tanh activation function arranged in sequence from front to back, the upsampling block includes a relu activation function, a deconvolution layer and a batch normalization layer arranged in sequence from front to back, and the first three upsampling blocks also include a random drop layer arranged at the tail.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the shape defect sample generation method based on a generative adversarial network described in any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Defect image generation method based on generative adversarial network
CN114022586A
Unsupervised defect detection method based on quantization auto-encoder
CN115375604A
Semantic-guided defect image generation method and device, equipment and storage medium
CN115409694A
Face image restoration method and system based on generation and identification balance confrontation
CN116468638A
Volume video fragment compression method based on deep learning
CN117119203A
Cited By
Transform-based oil and gas pipeline circumferential weld defect intelligent detection method
CN120451098A
Shaft neck defect detection method and device of steam turbine rotor and electronic equipment
CN120948613A
A method, apparatus, and electronic equipment for detecting journal defects in steam turbine rotors.
CN120948613B
