# MAS-YOLO: A Lightweight Detection Algorithm for PCB Defect Detection Based on Improved YOLOv12
## 2 Methodology
### Improved YOLOv12 Algorithm
#### 2.2.1 Median-Enhanced Channel and Spatial Attention Block (MECS)  
In PCB defect detection tasks, the effective extraction of feature maps is crucial. Defects on PCBs typically manifest as small, localized features and are often affected by complex background interference. The Median-enhanced Channel and Spatial Attention Block module incorporates a median enhancement operation to fuse channel and spatial attention  
Figure 1: Overall network structure diagram of MAS-YOLO.  
mechanisms. It is designed to significantly improve the model's feature extraction capability for PCB defects, particularly enhancing robustness in detecting small targets under complex background conditions. The structure of the MECS module is illustrated in Figure 2.  
Firstly, the input feature map undergoes three different types of pooling operations. The aim of these pooling operations is to capture the global information of the feature map at different levels and to enhance noise suppression by incorporating the specially introduced median pooling. Since PCB defects are typically small and difficult to extract directly from the overall background, the use of median pooling helps to suppress noise in complex backgrounds while preserving key information. The expressions for these pooling results are as follows:  
\[P_{avg}\ =\ AvgPool(X), \tag{1}\]  
\[P_{max}\ =\ MaxPool(X), \tag{2}\]  
\[P_{med}\ =\ MedianPool(X). \tag{3}\]  
Next, the pooled feature maps are further processed by a Multi-Layer Perceptron (MLP). To enhance the efficiency and effectiveness of feature learning, the MLP first applies a 3 \(\times\) 3 convolutional layer for dimensionality reduction, followed by a 1 \(\times\) 1 convolutional layer to restore the channel dimension. This design not only reduces redundant information but also improves the discriminative capability of the features. Finally, the outputs of the MLP are aggregated and passed through a Sigmoid activation function to generate the channel attention weights:  
\[A_{channel}\ =\sigma(MLP(P_{avg}\ )+MLP(P_{max}\ )+MLP(P_{med}\ )). \tag{4}\]  
where the Sigmoid activation function maps the output values to the range of [0, 1], thereby generating the channel attention map. This attention map is then multiplied element-wise with the original input feature map to perform weighted processing. This operation enhances critical information while suppressing irrelevant background noise. The formulation is expressed as follows:  
\[X_{weighted}\ =\ X\odot A_{channel}. \tag{5}\]  
Figure 2: Structure of Median-enhanced Channel and Spatial Attention Block.  
In PCB defect detection, this process enhances the model's sensitivity to defect features, particularly in identifying subtle circuit anomalies. By emphasizing important channels and suppressing irrelevant background noise from circuit traces, it improves detection precision. The spatial attention mechanism further strengthens the spatial feature extraction capability through multi-scale convolution operations. Given that PCB defects may appear at various locations and exhibit significant variations in scale, the use of multi-scale convolutions facilitates the capture of defect information across different resolutions. In this component, the input feature map is first processed by a 7 \(\times\) 7 depthwise convolutional layer to extract fundamental spatial features, followed by several depthwise convolutional layers with varying kernel sizes to obtain more refined spatial information. After fusing these multi-scale features, a spatial attention map is generated, whose expression is given as follows:  
\[X_{final}\ =X_{weighted}\odot A_{space}. \tag{6}\]  
The spatial attention map is multiplied element-wise with the weighted feature map to highlight important defect regions, further enhancing the model's adaptability to complex backgrounds and multi-scale defects.