## 3 Proposed Methods
### Defect Detection and Localization  
So long as the proposed model has been trained well, it can be used to determine whether a new image sample is defective or not and, if so, where the defect is located. It should be noted that every CNN other than the discriminator \(D\) is utilized in the testing phase, as shown in Figure 2b. Given a \(M\times N\) pixel-sized texture image, \(r\times c\) pixel-sized image patches are extracted in sequence where the size is similar to that in the training phase. The well-trained model can then reconstruct each image patch, and all of them are then rearranged to a full-sized reconstructed image. Next, a pixel-level comparison is conducted upon the input and reconstructed images, which can be expressed as:  
\[I_{residual}=abs\big{(}I_{org}-I_{rec}\big{)} \tag{5}\]  
Figure 3: Gaussian pyramid to extract fixed-size image patches from different scale level. where \(I_{residual}\) denotes the result of the pixel-level difference which is termed residual image and \(abs(\cdot)\) represents the absolute value operation. Besides the reconstruction use case, the proposed model is also employed to implement LDA. Figure 4b depicts the final detection result which is directly derived from the residual image; it contains some sparkles. In fact, such inferences often appear in the borders of texture patterns, and noise points can also be misjudged as defects. Generally, the gray values of these areas are higher than those of the true defect regions. As a result, a relatively precise localization of defect can be realized only by a pixel-level difference. Figure 4c displays the detection result with local difference analysis (LDA), where previous sparkles are eliminated. The LDA module therefore plays an important role in the localization of defects. In the testing phase, two feature vectors are obtained, which are powerful feature representations with respect to the input and reconstructed image patches. The reconstructed version of a defective image patch shows lower sensitivity in abnormal regions, so there are apparent differences between their corresponding features. For a defect-free image patch, the opposite is true. A similarity analysis can thus be carried out between the two feature vectors, through which the probability of defect occurrence can be acquired. The above description can be formulated as follows:  
\[I^{r,c}_{mask}=\left\{ \begin{array}{cc}1,&if\ ||f^{r,c}_{i}-f^{r,c}_{o} ||_{2}>\varepsilon\\ &0,&otherwise\end{array} \right. \tag{6}\]  
where \(r=(1,2,\ \ldots,M/m)\), and \(c=(1,2,\ \ldots,N/n)\). \((M,N)\) and \((m,n)\) denote the size of the whole image and the image patch, respectively. The top-right corner \((r,c)\) represents the appointed image patch in the mask image \(I_{mask}\). \(f^{r,c}_{i}\) denotes the feature vector for the original image \(I_{org}\), as does \(f^{r,c}_{o}\) for the reconstructed image \(I_{rec}\). \(\varepsilon\) is a threshold to control the sensitivity of being evaluated as a defective region. All mask patches \(I^{r,c}_{mask}\) are then rearranged to form a full-sized mask image \(I_{mask}\). The first row in Figure 5 displays four defective images with different texture background; the second demonstrates four corresponding mask images through which regional-level rough defect localization can be accomplished. Figure 4: The comparison between methods with and without local difference analysis (LDA) module. (**a**) Original image; (**b**) The final detection result without LDA; (**c**) The final detection result with LDA. Together with the residual image and mask image, a fusion strategy is put into effect to synthesize them and obtain the final detection result. The process can be formulated as:  
\[I_{result}=\left\{ \begin{array}{c}1,\;if\;I_{residual}\odot I_{mask}>\mu+ \gamma\cdot\sigma\\ 0,otherwise\end{array} \right. \tag{7}\]  
where \(\odot\) denotes the element-wise multiplication operation (Hadamard product) and \(\mu\) and \(\sigma\) represent the mean and standard deviation of the residual image \(I_{residual}\) respectively.