# MAS-YOLO: A Lightweight Detection Algorithm for PCB Defect Detection Based on Improved YOLOv12
## 2 Methodology
### Improved YOLOv12 Algorithm
#### 2.2.3 Slide Alignment Loss (SAL)  
In PCB defect detection, targets are typically small in size and exhibit irregular shapes. The traditional CIoU loss function shows limitations in addressing issues such as mismatched aspect ratios and angle deviations of bounding boxes. To overcome these challenges, this paper proposes Slide Alignment Loss, a loss function that not only considers center deviation, size ratio, and angle differences but also incorporates an adaptive weighting mechanism for sample difficulty, thereby enabling more precise bounding box regression. The SAL function is composed of multiple parts. First, the center distance loss is used to measure the normalized error between the predicted box center and the true box center, with the formula given by the following:  
\[\mathcal{L}_{\text{center}}=\frac{1}{C}\Big{\|}C_{\text{pred}}-C_{\text{true }}\Big{\|}. \tag{9}\]  
where \(C_{pred}\) and \(C_{true}\) represent the predicted and actual box centers, respectively, and \(C\) is used for normalization to ensure that errors are comparable across different sizes. Secondly, the size ratio loss is used to compute the relative differences in width and height between the predicted bounding box and the ground truth. By evaluating the ratio of dimensions between the predicted and ground truth boxes, the SAL function can better adjust the shape of the target bounding box to more closely match the ground truth. To address the discrepancy in rotation angles, SAL introduces an angular deviation loss. This loss is calculated by measuring the cosine similarity between the predicted and true bounding boxes to quantify the inconsistency in their angles:  
\[\mathcal{L}_{\text{angle}}=1-\cos(\theta_{\text{pred}}-\theta_{\text{true}}). \tag{10}\]  
where \(\theta_{\text{pred}}\) and \(\theta_{\text{true}}\) represent the rotation angles of the predicted and true bounding boxes, respectively. The rotation angle \(\theta\) is defined as the angle between the bounding box's orientation and a reference axis, typically ranging from \(-\pi/2\) to \(\pi/2\). For detected samples, \(\theta_{\text{pred}}\) is obtained directly from the model's output, while \(\theta_{\text{true}}\) is derived from the ground truth annotation. The cosine similarity is computed using the dot product of the two angle vectors divided by the product of their magnitudes. This loss is particularly effective for handling irregular shapes on PCBs, effectively addressing the alignment issue of rotation angles. Moreover, the angular deviation loss innovatively incorporates a dynamic angle adjustment mechanism. This mechanism adaptively adjusts the weight of the angle loss based on the difference between \(\theta_{\text{pred}}\) and \(\theta_{\text{true}}\). When the angle difference is large, the weight is increased to prioritize aligning the rotation angles. Conversely, when the angle difference is small, the weight is reduced to focus more on other aspects of the bounding box regression. This dynamic adjustment enhances the model's ability to converge more effectively on accurate angle predictions. The gradient of the angular deviation loss with respect to the model's predictions is computed as follows: Let \(\theta_{\text{pred}}\) be the predicted angle and \(\theta_{\text{true}}\) be the true angle. The gradient \(\frac{\partial\mathcal{L}_{\text{angle}}}{\partial\theta_{\text{pred}}}\) is given by the following:  
\[\frac{\partial\mathcal{L}_{\text{angle}}}{\partial\theta_{\text{pred}}}=\sin( \theta_{\text{pred}}-\theta_{\text{true}}). \tag{11}\]  
This gradient is then backpropagated through the network to update the model's parameters, allowing it to learn the correct alignment of bounding box angles. The dynamic angle adjustment mechanism ensures that the model not only learns the correct angles but also does so efficiently, leading to improved performance in detecting defects with varying orientations.