# Glass Defect Detection with Improved Data Augmentation under Total Reflection Lighting
## 3 Principle and Improvement of Defect Detection Algorithm
### Increase Attention Mechanism  
The defects of mobile phone flat glass are micro-scale defects, which are difficult to detect. However, the detection performance of the YOLOv5 algorithm in terms of micro-defects is not satisfactory. Sometimes, there is a chance that it fails to detect defects or detect them accurately, resulting in low detection precision and a high missed detection rate, which falls short of conventional industrial needs. In order to boost its ability to detect micro-defects, the Convolutional Block Attention Module (CBAM) was added. Feature maps are generated when the input image is sliced by the Fouce structure, as shown in Figure 7. Then, the convolution operation is followed. At that time, CSP1-CBAM, the new structure, is constructed by embedding the CBAM in the backbone network CSP1 structure, which conducts a feature aggregation of the feature map. The CBAM, as a mixed-domain attention module, is composed of channel and spatial attention modules. Attention maps are generated by deriving the intermediate feature mapping of the two independent channel and spatial dimensions. An adaptive refinement of the features can build up the network's ability to express important features and suppress interference information, thereby improving detection precision. The CBAM hybrid attention module is shown in Figure 8. Figure 8b is the channel attention module, which generates a channel attention graph based on the relationship between features, focusing on \"what\" is meaningful on a given feature graph. Its specific implementation involves aggregating spatial information on the feature map using average pooling and global maximum pooling operations to produce feature descriptions in two different spatial dimensions. One is the global pooling feature and the other is the maximum pooling feature. These descriptions are then input into a multilayer perceptron (MLP) for learning. The outputs of the MLP are \"summed up\", and then processed through the mapping of the Sigmoid function of the loss function to generate two-dimensional channel attention maps. The formula is as follows:  
\[M_{c}(F)=\sigma(MLP(AvgPool(F))\,+\,MLP(MaxPool(F))) \tag{1}\]  
In this formula, \(\sigma\) represents the Sigmoid function, MLP represents the shared multi-layer perceptron, F represents the input feature, \(AvgPool\) represents the average pooling, and \(MaxPool\) represents the global maximum pooling. The channel attention weight (\(M_{c}\)  
Figure 7: Feature map. helps the model better select and utilize features, reduces redundant information, and enhances the model's interpretability and applicability. The spatial attention module is shown in Figure 8c; it generates a spatial attention map according to the spatial relationship between features. Focusing on \"where\" is the informative part, and it complements the channel attention module. Specifically, it carries out average pooling and global maximum pooling operations on the given feature vectors, and then splices the output results into a feature map, whose channel information will be aggregated. Next, by inputting the output results into a standard convolution layer for connection and convolution operations, a two-dimensional spatial attention map will be generated through the mapping processing of the loss function and Sigmoid function. The formula is as follows:  
\[M_{s}(F)=\sigma(f^{7\times 7}([AvgPool(F);\ MaxPool(F)])) \tag{2}\]  
In this formula, \(f^{7\times 7}\) represents the convolution operation, with the convolution kernel being \(7\times 7\), and \(F\) represents the input feature vector. The spatial attention weight \((M_{s})\) helps the model better capture local features and structural information, adapt to different scales of input data, reduce noise and interference, and enhance its interpretability. Figure 8: Attention module structure diagram. (**a**) CBAM; (**b**) CAM attention module; (**c**) SAM attention module. To verify the effect of adding the CBAM on the model's ability to detect small targets, a training and testing process is conducted.