# MAS-YOLO: A Lightweight Detection Algorithm for PCB Defect Detection Based on Improved YOLOv12
## 2 Methodology
### Improved YOLOv12 Algorithm  
Based on YOLOv12, this paper proposes an improved algorithm for PCB defect detection--MAS-YOLO. The model is optimized in three main aspects to enhance both detection accuracy and robustness for PCB defects. The overall framework of the improved YOLOv12 algorithm is shown in Figure 1.  
First, a Median-enhanced Channel and Spatial Attention Block is integrated into the Backbone. Unlike conventional attention mechanisms that may struggle with noise suppression in complex industrial settings, the MECS module innovatively combines median enhancement with depthwise convolution. This unique fusion not only broadens the receptive field but also selectively amplifies defect-specific features while robustly mitigating noise interference, a critical advancement over prior arts where noise can significantly degrade detection accuracy. Second, the Adaptive Hierarchical Feature Integration Network is introduced in the Neck. While multi-scale feature fusion is a common strategy, AHFIN distinguishes itself through its adaptive Deformable Convolution and gradient compression. Unlike static fusion methods, AHFIN dynamically recalibrates feature importance across scales, effectively capturing the hierarchical nature of PCB defects. This dynamic adjustment is particularly advantageous in PCB inspection, where defects of varying morphologies necessitate a flexible feature integration approach. Third, the original YOLOv12 loss function (CIoU) is substituted with the Slide Alignment Loss. Traditional loss functions often fail to adequately address the geometric complexities of PCB defects, such as irregular shapes and inconsistent orientations. SAL introduces a novel adaptive weighting scheme that holistically considers center deviation, size ratio, and angle differences. Thiscomprehensive approach not only refines bounding box regression but also inherently compensates for sample imbalance, a limitation that has persisted in previous frameworks.  
When contrasted with similar works like SSHP-YOLO [24], YOLO-MBBi [25], and YOLO-DHGC [30], MAS-YOLO's originality lies in its cohesive and synergistic architecture. While these prior models incorporate elements such as multi-scale fusion and adaptive losses, MAS-YOLO uniquely integrates median-enhanced attention, adaptive hierarchical fusion, and geometry-aware loss optimization within a unified framework. This integration is not merely incremental but represents a substantial advancement in addressing the specific challenges of PCB defect detection. For instance, SSHP-YOLO focuses on enhancing Backbone features through multi-branch blocks but lacks the sophisticated attention and loss mechanisms present in MAS-YOLO. YOLO-MBBi, while effective in high-precision detection, does not achieve the same level of computational efficiency and adaptability across diverse defect morphologies as MAS-YOLO. Similarly, YOLO-DHGC employs dense connections for feature enhancement but does not incorporate the geometric and noise-aware refinements that MAS-YOLO introduces.  
Through these improvements, the MAS-YOLO model demonstrates enhanced detection accuracy and real-time performance in PCB defect detection tasks, thereby meeting the dual requirements of detection speed and accuracy in industrial applications. The following sections provide a detailed explanation of the improved modules and loss function.