## 3 The Proposed Model
### Loss Function  
The CycleGAN model generation involves two types of loss functions: the adversarial loss function and the reconstruction loss function. The adversarial loss function aims to minimize the discrepancy between the data distributions of the generated image and the target domain, thus producing more realistic images. The reconstruction loss function ensures that the mapping relation between the source and target domains is consistent and aligned. To enhance the quality of the industrial defect images, we incorporate the perceptual loss function and the capsule loss function based on the U-Net structure and the capsule network structure. We also add the identity mapping loss to better capture the industrial defect features. The overall loss function is given by Equation (8). \[ \begin{array}{ll}L_{PreCaCycleGAN}=&L_{GAN}(G_{P2N},D_{P2N},X,Y)+L_{GAN}(G_{ N2P},D_{N2P},Y,X)\\ &+\lambda_{1}L_{cycle}(G_{P2N},G_{N2P},X,Y)+\lambda_{2}L_{identity}(G_{P2N},G_{ N2P},X,Y)\\ &+\lambda_{3}L_{perceptual}(G_{P2N},G_{N2P},X,Y)\end{array}  \tag{8}\]  
Standard generative adversarial networks adopt a binary, zero-sum game, which poses a very large very small game problem [38]. In this game, the generator and the discriminator compete to reach the final Nash equilibrium, as shown in Equation (9). The CycleGAN model employs the Sigmoid cross-entropy loss function as the adversarial loss function, which is suitable for logical classification problems. However, this loss function can cause gradient vanishing problems in the training of generative adversarial networks, affecting model convergence and optimization. To address this issue, we use the least squares method as the adversarial loss function and combine it with the edge loss of the capsule network. This improves the stability and convergence of the training and ensures the authenticity and diversity of the generated samples. The model training loop consists of defective industrial samples and non-defective industrial samples that are mutually generated with the same two parts of the loss function. As an example, we introduce the following formulas to generate defective samples (y) from non-defective samples (x) by using the generator \(G_{P2N}\) and the discriminator \(D_{P2N}\), and we show the adversarial loss function in Equation (10). \[G*,D*=arg\min_{G}\max_{D}\mathrm{L}(G_{P2N},D_{P2N},G_{N2P},D_{N2P},X,Y) \tag{9}\]  
\[ \begin{array}{ll}L_{GAN}(G_{P2N},D_{P2N-1},D_{P2N-2},X,Y)\\ &=E_{X\sim P_{data}(x)}\big{[}(D_{P2N-1}(G(x))-1)^{2}\big{]}\\ &+E_{y\sim P_{data}(y)}\big{[}(D_{P2N-1}(y)-1)^{2}\big{]}+E_{x\sim P_{data}(x) }\big{[}(D_{P2N-1}(G(x)))^{2}\big{]}\\ &+\lambda E_{y\sim P_{data}(y)}[-L_{M}D_{P2N-2}(y),T=1]\\ &+\lambda E_{x\sim P_{data}(x)}[-(D_{P2N-2}(G(x))),T=0]\end{array}  \tag{10}\]where \(D_{P2N-1}\) is the discriminator branch that uses the PatchGAN structure and \(D_{P2N-2}\) is the discriminator branch that uses the capsule network. The first part on the right-hand side of the equal sign is the loss function corresponding to the generator, and its optimization objective is to make the value \(D_{P2N-1}(G(x))\) of the discriminator that discriminates the generated image approach 1. The second and third parts are the loss functions corresponding to the discriminator, and their optimization objective is to make the value of the discriminator that discriminates the real image \(D_{P2N-1}(G(x))\) approach 1, and the value of the discriminator that discriminates the generated image \(D_{P2N-1}(G(x))\) approach 0. The fourth and fifth parts are the edge loss functions, and \(\lambda\) is a hyperparameter that indicates the relative importance of the edge loss in the improved adversarial loss. The capsule discriminator only needs to determine whether the input image is a real image or a generated fake image, which is defined in Equation (11).