## 3 The Proposed Model
### Loss Function
\[L_{M}(k,v)=T_{K}\max\bigl{(}0,m^{+}-\|v_{k}\|\bigr{)}^{2}+\lambda(1-T_{K})\max \bigl{(}0,\|v_{k}\|-m^{-}\bigr{)}^{2} \tag{11}\]  
where \(v_{k}\) (see Equation (4)) is the output vector of the discriminator layer of the capsule network. \(k=0\) for real data, or \(k=1\) for generated false data. \(T_{K}=1\) if it is desired for the discriminator or generator to determine that this is true data at this point, or \(T_{K}=0\) if it is desired for the discriminator to determine that this is generated false data at this point. \(m^{+}\) and \(m^{-}\) are the baselines for determining whether the input image is true or false. If the mode of the vector is larger than \(m^{+}\), 0 is returned; if the mode of the vector is smaller than \(m^{+}\), the square of the difference between the two is returned; if the mode of the vector is smaller than \(m^{-}\), 0 is returned; if the mode of the vector is larger than \(m^{-}\), the square of the difference between the two is returned. In order to avoid the pattern collapse problem in the process of fighting against the loss function to reach Nash equilibrium, we add the perceptual loss function to enhance the feature and semantic information of the generated image during the generator training to make the generated image more realistic and clear. The formula is shown in Equation (12), where \(\phi\) is the pre-trained VGG19 feature extractor. \[L_{perceptual}(G_{P2N},G_{N2P},X,Y)=\|\phi(x)-\phi(G_{P2N}(G_{N2P}(x)))\|_{2}^ {2}+\|\phi(y)-\phi(G_{N2P}(G_{P2N}(y)))\|_{2}^{2} \tag{12}\]  
Identity loss and loop loss are used to ensure that the generated industrial defect images are consistent with the input industrial defect-free images in terms of content and structure, and the constraint generator generates industrial defect samples on the same background as the input defect-free samples, and the formulas are shown in Equations (13) and (14). \[L_{cycle}(G_{P2N},G_{N2P},X,Y)=E_{x\sim P_{data}(x)}\|G_{P2N}(G_{N2P}(x))-x\|_ {1}+E_{y\sim P_{data}(y)}\|G_{N2P}(G_{P2N}(y))-y\|_{1} \tag{13}\]  
\[L_{identity}(G_{P2N},G_{N2P},X,Y)=E_{x\sim P_{data}(x)}\|G_{N2P}(x)-x\|_{1}+E_{ y\sim P_{data}(y)}\|G_{P2N}(y)-y\|_{1} \tag{14}\]  
The training process of the PreCaCycleGAN model is shown in Algorithm 2. Unlike the standard CycleGAN model, in order to ensure the convergence and accuracy of the model, we get the optimal training steps through experiments. In steps 3 to 6, we update the optimized generator \(G_{P2N}\), discriminator \(D_{P2N}\), generator \(G_{N2P}\), discriminator \(D_{N2P}\), to obtain the final industrial defect sample generation model. ```

**Algorithm 1** PreCaCycleGAN Algorithm

## 4 Validation Experiments

To assess the effectiveness of PreCaCycleGAN-generated defect samples in enhancing the generalization performance of real industrial defect detection models, we employed the DAGM 2007 dataset as an experimental platform. DAGM 2007 [47] is a publicly accessible dataset of texture surface images with various types of defects, which simulates the real-world defect detection problem with high complexity and diversity. We compared the defect samples produced by PreCaCycleGAN with those produced by Tree-CycleGAN [42] and CycleGAN-TSS [43] and applied them to three state-of-the-art defect detection models that are widely adopted in practice, namely YOLOv5 [48], SSD [49], and Faster-RCNN [50]. We evaluated the impact of PreCaCycleGAN-generated defect samples on the generalization performance of defect detection models by measuring the mAP, false detection rate, and other metrics of the three generative models on different defect detection models and datasets. All experiments were conducted on a single NVIDIA GeForce GTX 3060 GPU. The training process lasted for 150 iterations with a batch size of 1. The learning rate was initially set to 0.0002 and was linearly decayed starting from the 100 th iteration. The hyperparameters in the loss function were empirically determined.