# CPAM: Cross Patch Attention Module for Complex Texture Tile Block Defect Detection
## 3 The Proposed Method  
In this paper, a new attention module-CPAM is proposed, which can be easily plugged into the model. The structure of CPAM is shown in Figure 1, and it is mainly divided into a pooling part, a convolution part, and a weighting part. First, the input feature mapping \(F\) is globally pooled to obtain the compressed feature mapping \(f\in R^{H\times W\times 1}\), which can be expressed as  
\[f_{(i,j)}=\text{Pool}\Big{(}X_{(i,j)}\Big{)} \tag{1}\]  
\(f\) represents the whole feature map of the input is compressed into a one-dimensional feature map, \(X\) is the tensor of the input feature map, and the height and width of the feature mapping are represented by H and W, respectively. Then, the feature mapping \(f\) is pooled into patches, and each weight in the pooled feature mapping \(f^{\prime}\in R^{P\times P\times 1}\) represents the pooled patch, which can be expressed as  
\[f^{\prime}_{(i,j)}=\text{ Pool }_{patch}\Big{(}f_{(p\times i,p\times j)} \Big{)} \tag{2}\]\(p\) represents the open square of the patch value, that is \(p^{2}=\text{ patch}\). The information in the feature mapping F has different spatial location information, and the arrangement of spatial location information affects the patch correlation. Therefore, three sets of strategies with no identical directionality are set up in this study, as shown in Figure 2. In this study, considering two spatial orientations simultaneously can increase stability, and its discussed specifically in the experimental section. In the experiment (c) strategy is the best, so (c) strategy is chosen, \(f^{\prime}\) transposed to obtain \(f^{\prime\prime}\in R^{P\times P\times 1}\), which can be expressed as  
\[f^{\prime\prime}_{(i,j)}=\text{Transpose}\left(f^{\prime}_{(i,j)}\right) \tag{3}\]  
This gives the arrangement of the feature mappings \(f^{\prime}\) and \(f^{\prime\prime}\) in the X and Y directions, by joining the feature mappings \(f^{\prime}\) and \(f^{\prime\prime}\) into a one-dimensional vector, respectively, which are \(f_{1}\in R^{P^{2}\times 1}\) and \(f_{2}\in R^{P^{2}\times 1}\), then it is easy to cancel the two vectors to obtain \(f_{\text{concat}}\in R^{P^{2}\times 2}\). \[f_{\text{conv}}\ =\text{Conv}(f_{\text{concat}}\ ) \tag{4}\]  
The obtained \(f_{\text{conv}}\) represents the feature mapping that distinguishes the importance of different patches. After completing the convolution part, \(f_{\text{conv}}\) needs to be weighted into the input feature map, so \(f_{\text{conv}}\) needs to be converted into a weightable size. In the weighting part, \(f_{\text{conv}}\) is separated into two-dimensional feature mapping \(f_{split}\in R^{P\times P\times 1}\), which is converted into \(f_{\text{pool}}\in R^{H\times W\times 1}\) by the pooling operation, and then \(g\in R^{H\times W\times 1}\) will be obtained by the sigmoid function, and the weighting part can be expressed as  
\[g=\sigma(\text{Pool}(\text{Split}(f_{\text{conv}}\ ))) \tag{5}\]  
In the equation, \(\sigma\) is the sigmoid function. Finally, the weighted feature mapping \(F_{CPAM}\) can be obtained, and the output is expressed as  
\[y_{(i,j)}=x_{(i,j)}\otimes g_{(i,j)} \tag{6}\]  
Figure 1: The structure of CPAM. Figure 2: The connection strategies with different directionality. Functionally, the most important function of CPAM is to coalesce feature information as a combination of multiple patches, which can obtain weights that contain a large amount of information. Then, CPAM employs two different spatial orientation alignment methods to construct stable one-dimensional information, using one-dimensional convolution to quickly help the model distinguish the importance of different patches. Structurally, CPAM is very simple, divided into a pooling part, convolution part and weighting part, and there is only one convolutional layer in the whole structure, so the operation is efficient.