## 3 Improvement of Defect Detection Algorithm
### COT Self Attention Mechanism  
In polarization-degree images, extracting discriminative features proves challenging due to the small size of objects and the limited visualization information. In the YOLOv7 network, as convolutional layers deepen, the high contrast of small and medium targets in polarization degree images against the background tends to be diminished, further obscuring the boundary between the target and the background, thereby compromising the model's recognition accuracy and detection output. Hence, it is imperative to maximize the information dimension and depth accessible to the defect small targets. Typically, in acquired digital images, an inherent coexistence relationship exists between \"target and scene\" and \"target and target\" and leveraging this coexistence can significantly enhance the algorithm's detection performance for small defective targets [21]. From a research perspective [22], appropriately modeling contextual information can boost target detection performance, particularly when the network's depth increases, as it can more effectively demonstrate the enhancing effect of the attention mechanism. Grounded in this understanding, this paper incorporates the COT self-attention mechanism into YOLOv7 [23].  
The COT structure is depicted in Figure 5. In contrast to a traditional self-attention mechanism, where all pairs of query key relationships are learned in isolation without considering the rich context between them, the COT structure overcomes this limitation. It efficiently leverages the contextual information between input keys to inform the learning of a dynamic attention matrix, thereby enhancing the visual representation with information beneficial to small target detection and yielding improved model performance.  
In COT, set the input feature map \(X\in\mathbb{R}^{H\times W\times C}\). The key, query and value are defined as \(K=X\), \(Q=X\) and \(V=XW_{p}\). First use the \(k\times k\) group convolution spatially for all adjacent keys within the \(k\times k\) grid to represent each key in context. The learned context key \(K^{1}\in\mathbb{R}^{H\times W\times C}\) naturally reflects the static context information between local adjacent keys and \(K^{1}\) is represented as the static context of input \(X\). The context key \(K^{1}\) and the query \(Q\) are cascaded and the attention matrix [23] is implemented  
Figure 4: YOLOv7 structural block diagram.  
by two successive \(1\times 1\) convolution (\(W_{\theta}\) with ReLU activation function and \(W_{\delta}\) without activation function).  
\[A=\left[K^{1},Q\right]W_{\theta}W_{\delta} \tag{3}\]  
For each header, the local attention matrix at each spatial location of \(A\) is learned based on query features and key features of the context, rather than isolated query keyword pairs. This approach enhances self-attention learning with the additional guidance of the mined static context \(K^{1}\). Next, the focus feature graph \(K^{2}\)[23] is calculated by aggregating all values \(V\) based on the context's attention matrix \(A\),  
\[K^{2}=V\otimes A \tag{4}\]  
Given the concern that feature graph \(K^{2}\) captures dynamic feature interactions between inputs, \(K^{2}\) is named the dynamic context of the inputs. The final output feature graph \(Y\) is the fusion of static \(K^{1}\) and dynamic context \(K^{2}\).  
COT can simultaneously capture the above two spatial context relationships in the input key, increase the effective information in the feature map and improve the effective extraction of small scratch features on the surface of the photovoltaic module. Therefore, in this paper, a COT self-attention mechanism is added to SPPCSPC module, which can improve accuracy and does not produce a large amount of calculation. The improved SPPCSPC module is shown in Figure 6.