## 3 The Proposed Model
### Discriminator Structure  
We proposed a discriminator D with a PatchGAN and a capsule network with two branches [22] to optimize the discriminative output, as illustrated in Figure 3. The input image undergoes three feature extraction layers and then bifurcates into two branches for the output. The first branch employs the original PatchGAN discriminator structure to assess the local authenticity of the image. The second branch utilizes the capsule network to achieve sample discrimination and evaluate the global consistency of the image. To better preserve the spatial information of industrial defect samples, we employed vector encoding of the primary capsule layer and the digit capsule layer to represent the probability of feature existence and spatial information in the capsule network. This enhances the realism and diversity of the generated defect samples, as well as their interpretability and controllability. We also employed a dynamic routing mechanism between two consecutive capsule layers to iteratively learn and predict the features of the lower layer and achieve an adaptive feature combination. The dynamic routing relationship between capsule \(i\) in layer \(l\) and capsule \(j\) in layer (\(l+1\)) is depicted in Equation (1). \[\hat{u}_{j|i}=W_{ij}\cdot u_{i} \tag{1}\]  
where the output of capsule \(i\) is \(u_{i}\), the weight matrix between capsule \(i\) and capsule \(j\) is \(W_{ij}\), and the prediction vector from capsule \(i\) to capsule \(j\) is \(\hat{u}_{j|i}\). The discriminator only needs to output two types of data, true or false, and \(j\) takes the value {0,1}. \(i\) is then determined by the total number of master capsules and takes the value {1 \(\leq i\leq 4096\) \(i\)\(\in\)N}. After the prediction vectors are input to the dynamic routing incentive mechanism, for each prediction vector, a routing weight needs to be defined, which is the log prior  
Figure 3: Schematic of PreCaCycleGAN discriminator structure. Figure 2: Schematic diagram of PreCaCycleGAN generator structure. probability between capsule \(i\) and capsule \(j\). The coupling coefficient is obtained using softmax, as shown in Equation (2). \[c_{ij}=\frac{exp\big{(}b_{ij}\big{)}}{\Sigma_{k}exp\big{(}b_{ij}\big{)}} \tag{2}\]  
where \(c_{ij}\) is the coupling coefficient of the prediction vector and \(b_{ij}\) is the routing weight. Since there is no initial routing preference, the initial routing weight of each capsule is the same, and the sum of the coupling coefficients of all prediction vectors is 1, so the initial value of \(b_{ij}\) is set to 0. So the output sum \(s_{j}\) of capsule \(j\) in the \((l+1)\)th layer is shown in Equation (3). \[s_{j}=\Sigma_{i}c_{ij}\cdot\hat{u}_{j|i} \tag{3}\]  
where \(s_{j}\) is the output vector of layer \(j\), representing the sum probability of all weighted prediction vectors in this layer, and it is necessary to use the squash function for \(s_{j}\) to ensure that the probability takes values between \([0,1]\), as shown in Equation (4). \[v_{j}=\frac{\|s_{j}\|^{2}}{1+\|s_{j}\|^{2}}\cdot\frac{s_{j}}{\|s_{j}\|} \tag{4}\]  
where, \(v_{j}\) is the predicted probability output of layer \(j\) after compression, and in the calculation, in order to prevent the denominator from being 0, the denominator is preprocessed by adding \(\varepsilon\), where \(\varepsilon\) is taken as \(10^{-8}\), as shown in Equation (5). \[\|s\|\approx\sqrt{\sum_{i}s_{j}^{2}+\varepsilon} \tag{5}\]  
The routing iteration protocol is represented by the dot product of the output vector and the prediction vector, and the larger the dot product represents the smaller the pinch angle, which proves that the consistency of the output vector and the prediction vector is better, and the protocol \(a_{ij}\) is shown in Equation (6). \[a_{ij}=v_{j}\cdot\hat{u}_{j|i} \tag{6}\]  
The dynamic routing incentive mechanism is a cyclic structure, so the routing weights \(b_{ij}\) need to be updated before the next cycle, and the formula is shown in Equation (7).