# Fabric Defect Detection Method Using SA-Pix2 pix Network and Transfer Learning
## 2 Materials and Methods
### The SA-Pix2 pix Model
#### 2.1.2 Loss Function  
In addressing the intricacies of image reconstruction, this section adopts a composite approach involving multiple loss functions to train the model effectively. Initially, the traditional pixel-by-pixel loss, known as the averaged absolute value error (L1 loss function) [43], is employed in this study. This loss function serves to constrain the generated images by the generator, ensuring minimal deviation from true values. Compared to the mean squared loss function (L2 loss function), the L1 loss function offers advantages in terms of rapid convergence and stable gradients, rendering it the preferred choice as the pixel-by-pixel loss function for the network.  
Simultaneously, to empower the generator to process images with a higher level of detail and to ensure proficient reconstruction of defective regions within fabric images, the structural loss function (SSIM loss function) is introduced. Recognizing that L1 loss fails to encapsulate regional structural characteristics in images, especially in scenarios involving irregular textures, the SSIM loss function emerges as a critical addition. Leveraging luminance, contrast, and structure (structural) elements, the SSIM loss function operates in tandem with the L1 loss function to optimize the network model. It specifically accentuates image details more effectively than the L1 loss alone, as shown in Equation (1):  
\[G^{*}=\underset{G}{\operatorname{argminmax}}L_{CGAN}(D,G)+\lambda(L1+L^{*}{}_{ SSIM}) \tag{1}\]  
In the equation, \(\lambda\) represents a weight parameter regulating the balance between the generator's Conditional Generative Adversarial Network (_CGAN_) loss function and the _L1_ and _SSIM_ loss functions. \(L_{CGAN}\) (\(D\), \(G\)) denotes the _CGAN_ target loss [44], as expressed in Equation (2):  
\[\underset{G}{\operatorname{argminmax}}L_{CGAN}(D,G)=E_{x\sim p_{data}(x)}[ \log D(x|y)]+E_{z\sim p_{data}(z)}[\log(1-D(G(z|y)))] \tag{2}\]  
In the equation, \(x\) represents the label, which corresponds to normal sample images, \(y\) stands for the additional input information (in this context, the input condition is defect images), and \(z\) denotes the defect images. \(D\) represents the discriminator, while \(G\) represents the generator. \(p_{data}(x)\) signifies the dataset of normal samples, and \(p_{data}(z)\) represents the dataset of defect samples.  
Typically, the _SSIM_ loss function [45] is formulated as follows:  
\[L_{SSIM}(x,y)=l(x,y)^{\alpha}\cdot c(x,y)^{\beta}\cdot s(x,y)^{\gamma} \tag{3}\]  
In the equation, \(x\) and \(y\) represent normal samples and images generated by the generator, respectively. \(\alpha\), \(\beta\), and \(\gamma\) are constants. Here, \(l(x,y)\) denotes the comparison of the brightness of the two images, \(c(x,y)\) signifies the contrast comparison between the two images, and \(s(x,y)\) represents the structural comparison of the two images.  
The reconstruction process yields images with a subtle difference in brightness and contrast compared to the originals, leading to a diminished value of the Structural Similarity Index (_SSIM_) loss function. This diminutive value, as encountered in Equation (1), curtails the efficacy of the SSIM loss function and renders it susceptible to noise. To address this limitation, an enhancement is introduced to the SSIM loss function, as delineated in Equation (4). This modification serves to amplify the weight of the SSIM loss function within Equation (1), thereby augmenting the sensitivity of the Conditional Generative Adversarial Network (CGAN) to image disparities. The refined SSIM loss function, as per Equation (4), is instrumental in elevating the network's discernment of subtle variations, promoting improved performance in capturing nuanced image differences during the reconstruction process.  
\[L^{*}{}_{SSIM}=1-\min(l(x,y),c(x,y),s(x,y)) \tag{4}\]