# Surface Defect Detection of Preform Based on Improved YOLOv5
## 3 YOLOv5 Network and Improvement
### Improved Methods
#### 3.2.1 Coordinate Attention Module  
Attention mechanisms are more effective for general classification tasks and are widely used in various neural networks. Currently, channel attention mechanisms such as the SE module and hybrid attention mechanisms such as the CBAM module have been applied [38; 39]. However, SE only encodes information between channels, ignoring equally important spatial relationships. The CBAM module obtains location information by reducing the channel dimension of the input tensor, and then uses convolution to calculate spatial information. However, it can only extract the rejection relationship of the feature map and cannot extract the long-distance dependency relationship.  
The attention module can convert any intermediate feature tensor \(X=[x_{1},x_{2},x_{\mathrm{c}}]\in R^{\mathrm{C}\times H\times W}\) is used as the input, and outputs a transformation tensor \(Y=[y_{1},y_{2},y_{\mathrm{c}}]\) with the same size as \(X\). C is the number of channels, and H and W are the height and width of the input image, respectively. The CA module structure is shown in Figure 4. The calculation process includes two parts: coordinate information embedding and coordinate attention generation [40].  
In order to solve the disadvantage of encoding global pooling as spatial information for each channel in other attention modules, the CA module uses coordinate information embedding to convert global pooling into a pair of one-dimensional feature codes. Specifically, for the input \(X\), we use two spatial range pool cores \((\mathrm{H},1)\) or \((1,\,\mathrm{W})\) to encode each channel along the horizontal and vertical coordinate, respectively. Therefore, the output of  
Figure 4: CA module structure.  
the \(c\)-th channel at height \(h\) and the output of the \(c\)-th channel at width \(w\) can be determined using the following expression:  
\[z_{c}^{h}(h)=\frac{1}{W}\sum_{0\leq i<W}x_{c}(h,i) \tag{1}\]  
\[z_{c}^{w}(w)=\frac{1}{H}\sum_{0\leq j<H}x_{c}(j,w) \tag{2}\]  
By computing the two formulas, the process of aggregating characteristics takes place across two spatial directions. The result is a set of bi-directional sensing feature maps capable of capturing long-distance relationships along a single spatial direction, while maintaining precise location information along the other. This approach enables the network to more accurately locate objects of interest.  
The second part is coordinate attention generation. First, connect the formulas together, and then send them to the \(1\times 1\) convolution function \(F_{1}\), resulting in the following formula:  
\[f=\delta\Big{(}F_{1}\Big{(}\Big{[}z^{h},z^{w}\Big{]}\Big{)}\Big{)} \tag{3}\]  
where \(\delta\) is a nonlinear activation function, \(f\in R^{C/r\times(H+W)}\) is an intermediate feature map encoding spatial information in the horizontal and vertical directions, and \(r\) is the ratio of down sampling. Then, we split \(f\) into two independent tensors along the spatial dimension, \(f^{\ h}\in R^{C/r\times H}\) and \(f^{\ w}\in R^{C/r\times W}\). The other two \(1\times 1\) convolutional transformation functions \(F_{h}\) and \(F_{w}\) are used to transform \(f^{\ h}\) and \(f^{\ w}\) into tensors with the same channel number for input X, respectively:  
\[\begin{split}& g^{h}=\sigma\Big{(}F_{h}\Big{(}f^{\ h}\Big{)} \Big{)}\\ & g^{w}=\sigma(F_{w}(f^{\ w}))\end{split} \tag{4}\]  
where \(\sigma\) is sigmoid function. The outputs \(g^{h}\) and \(g^{w}\) are expanded and used as attention weights, respectively. Finally, the output of the CA module can be written as:  
\[y_{c}(i,j)=x_{c}(i,j)\times g_{c}^{h}(i)\times g_{c}^{w}(j) \tag{5}\]