# Automated Surface Defect Inspection Based on Autoencoders and Fully Convolutional Neural Networks
## 2 The Proposed Algorithm
### Procedure for Defect Detection  
The proposed algorithm is a template-based algorithm for defect detection. Figure 2 shows the block diagram of the proposed algorithm, which contains two neural networks: an AE and an FCN. Given a test image \(X\), the AE, denoted by \(F\), reproduces the test image \(X\). That is,  
\[Y=F(X), \tag{4}\]  
where \(Y\) is the image reproduced by the AE. In the proposed system, the AE is expected to remove the defects of the input test image \(X\). Therefore, defects may not be reproduced by the AE when image \(X\) is defective. We view the image \(Y\) reproduced by the AE as the template for the image \(X\).  
By comparing the test target \(X\) with its template \(Y\) reproduced by the AE, it is then possible to identify defect regions. To carry out the comparison, the input image \(X\) is first separated into a set of non-overlapping blocks with equal size. Let \(x\) be a block of \(X\). To determine whether there exists defects in \(x\), we compute L2 distance between \(x\) and its counterpart \(y\) in the template \(Y\). As shown in Figure 3, the blocks \(x\) and \(y\) have the same size. The location of \(x\) in \(X\) is also the same as that of \(y\) in \(Y\). A defect is detected when the L2 distance, denoted by \(D(x,y)\), is larger than a pre-specified threshold \(T\).  
One issue in this approach is that the AE may not have the same capacity for the reconstruction of different blocks in \(X\). This is because local features for an image may vary. It is more difficult to reconstruct areas containing complex patterns. As a result, for a block \(x\) in the areas with large variations, the discrepancy between \(x\) and its counterpart \(y\) would be high even if the input image \(X\) is not defective. In these cases, it may be necessary to adopt a higher threshold value \(T\) for determining a defective block. A single threshold \(T\) for defect detection may not be appropriate for all the blocks from an input image.  
Figure 2: The overview of the proposed quality inspection of surface algorithm.  
In this study, the FCN is adopted to solve the issue stated above. It is used to segment the template \(Y\) into \(N\) regions \(Y(1),...,Y(N)\), where \(N\) is the number of coherent regions in \(Y\). That is,  
\[\{Y(1),...,Y(N)\}=G(Y), \tag{5}\]  
where \(G\) denotes the FCN operations. Each region \(Y(i)\) produced by the FCN is a set of pixels sharing common features such as colors or textures. Each \(Y(i)\) can be associated with a threshold \(T_{i}\), \(i=1,...,N\). For a block \(x\), when its counterpart \(y\) belongs to \(Y(i)\), we then adopt the threshold \(T_{i}\) for the defect detection. That is, we first define the sets  
\[X(i)=\{x:y\in Y(i)\},i=1,...,N. \tag{6}\]  
In this case, the block \(x\in X(i)\) is said to be defective when \(D(x,y)>T_{i}\). In this way, different threshold values can be selected for defect detection in accordance with the local features for different regions. The summary of the proposed algorithm is also provided in Algorithm 1, where the set of defective blocks, denoted by \(\mathcal{C}\), contains the final results of the proposed algorithm. Based on the final \(\mathcal{C}\), the locations of defective blocks can be easily identified. The defect attributes such as their patterns and areas can then be effectively visualized and measured.