## 3 Improvement of Defect Detection Algorithm
### Loss Function Based on NWD  
Due to the limited area occupied by the small target in the polarization degree image, even a slight offset of one pixel in the bounding box prediction can lead to significant errors  
Figure 5: COT self-attention mechanism. Figure 6: Improved SPPCSPC module. in the prediction process. However, the YOLOv7 network lacks a bounding box regression function specifically designed for small targets, resulting in low regression accuracy when dealing with them. Therefore, we need to choose a loss function that is more tailored to small targets. Based on this, the regression loss of normalized Wasserstein distance (NWD) is chosen as the target loss function in YOLOv7. The traditional loss function, which is based on the intersection over union (IoU) measure, has limited sensitivity to small targets due to the discreteness of boundary frame position changes. This sensitivity can lead to small position deviations, resulting in cases where the IoU between the true frame and any anchor falls below the minimum positive threshold. Consequently, a detection network trained on small targets lacks sufficient monitoring information, significantly reducing detection performance. On the other hand, NWD calculates the similarity between the small target prediction box and the ground truth box by normalizing the corresponding two-dimensional Gaussian distributions. This approach smooths out position deviations and can measure the similarity between boundary boxes that do not overlap or contain each other [24]. Furthermore, NWD is not sensitive to objects of different scales, which can improve the detection accuracy of the network model for small target objects. In the bounding box, the target pixel and the background pixel are concentrated in the center and border of the bounding box respectively [25]. Therefore, modeling the bounding box as a two-dimensional (2D) Gaussian distribution, where the center pixel has the highest weight and the importance of pixels gradually decreases from the center towards the boundary, can better describe the weights of different pixels in the bounding box. Specifically, for bounding boxes \(R=(c_{x},c_{y},w,h)\), where \(\left(c_{x},c_{y}\right)\), \(w\) and \(h\) represent center coordinates, width and height, respectively. The equation of its intrinsic ellipse can be expressed as  
\[\frac{(x-\mu_{x})^{2}}{\sigma_{x}^{2}}+\frac{(y-\mu_{y})^{2}}{\sigma_{y}^{2}}=1 \tag{5}\]  
where \(\left(\mu_{x},\mu_{y}\right)\) are the central coordinates of the ellipse and \(\sigma_{x}\) and \(\sigma_{y}\) are the lengths of the semi-axes along the x and y axes. Therefore, \(\mu_{x}=c_{x}\), \(\mu_{y}=c_{y}\), \(\sigma_{x}=w/2\), \(\sigma_{y}=h/2\). The probability density function of a 2D Gaussian distribution is given by the following equation:  
\[f(x|\mu,\Sigma)=\frac{exp(-\frac{1}{2}(x-\mu)^{T}\Sigma^{-1}(x-\mu))}{2\pi| \Sigma|^{\frac{1}{2}}} \tag{6}\]  
The similarity between boundary boxes A and B can be converted to the distribution distance between two Gaussian distributions, which is calculated using the Wasserstein distance from optimal transport theory. For two two-dimensional Gaussian distributions \(\mu_{1}=N(m_{1},\Sigma_{1})\) and \(\mu_{2}=N(m_{2},\Sigma_{2})\), the second-order Wasserstein distance between \(\mu_{1}\) and \(\mu_{2}\) is defined as [26]:  
\[W_{2}^{2}(\mu_{1},\mu_{2})=\|m_{1}-m_{2}\|_{2}^{2}+Tr(\Sigma_{1}+\Sigma_{2}-2 (\Sigma_{2}^{1/2}\Sigma_{1}\Sigma_{2}^{1/2})^{1/2}) \tag{7}\]  
which can be simplified as:  
\[W_{2}^{2}(\mu_{1},\mu_{2})=\|m_{1}-m_{2}\|_{2}^{2}+\|\However, \(W_{2}^{2}(N_{a},N_{b})\) is a distance measure and cannot be used directly as a similarity measure (i.e., a value between 0 and 1 as an IoU). Therefore, we use its exponential form to normalize and obtain a new metric called normalized Wasserstein distance (NWD):  
\[NWD(N_{a},N_{b})=\exp\left(-\frac{\sqrt{W_{2}^{2}(N_{a},N_{b})}}{C}\right) \tag{10}\]  
where C is a constant that is closely related to the data set.