## 3 Establishment of YOLOv5-Based Optimized Detection Model and Evaluation Metrics
### Efficient Multi-Scale Attention (EMA) Mechanism  
To address the challenges in weld images, such as the wide variation in defect sizes and the subtle nature of defect regions, EMA is introduced to dynamically assign weights to the critical areas in the feature maps, thereby enhancing detection accuracy. The overall structure of the EMA model is shown in Figure 4. By reshaping part of the channel dimensions into the batch and cross-dimensional interactions, it avoids channel dimensionality reduction, effectively retaining the information from each channel.  
Initially, the input feature \(X\in\mathbb{R}^{C\times H\times W}\) is divided into \(G\) sub-features along the channel dimension, where \(X=[X_{0},X_{i}\ldots,X_{G-1}]\), and \(X_{i}\in\mathbb{R}^{C//G\times H\times W}\). Here, \(H\) and \(W\) represent the height and width of the image, and \(C\) represents the number of channels. To maintain generality, \(G\) is chosen to be much smaller than \(C\), and the learned attention weights are used to strengthen the feature representation of the areas of interest in each sub-feature. Then, to enable the local receptive field of neurons to capture multi-scale spatial information, EMA uses three parallel branches to extract attention weights from the grouped feature maps. These branches include two parallel 1 \(\times\) 1 convolution branches and one 3 \(\times\) 3 convolution branch. The first two branches use adaptive 1D horizontal global pooling and 1D vertical global pooling to encode the channels. The encoded channel features are processed with 1 \(\times\) 1 convolutions to capture cross-channel interaction information, and the Sigmoid activation function is used for feature selection. The third 3 \(\times\) 3 convolutional branch skips normalization and average pooling to capture local cross-channel interaction features, thereby expanding the feature space. The cross-space learning part encodes global spatial information through 2D global average pooling, which essentially performs mean aggregation across spatial dimensions, as follows:  
Figure 3: Flowchart of the network structure for the optimized YOLOv5 model.  
\[Z_{c}=\frac{1}{H\times W}\sum_{j=1}^{H}\sum_{i=1}^{W}x_{c}(i,j) \tag{1}\]  
where \(Z_{c}\) represents the output features of the channel with index \(c\) after pooling; \(x_{c}\) (\(i,j\)) represents the input features of the channel with index \(c\) with a width of \(i\) and a height of \(j\).  
Additionally, a Softmax operation is applied to the pooled feature map to normalize the outputs, converting the response values for each class into a probability distribution. Cross-space learning extends the feature space through three branches to achieve aggregation of cross-space information, building short- and long-term dependencies of the information. To adapt to changes in the defect detection box scale, EMA is integrated into the first C3 module of the backbone network, forming the C3_EMA module. This effectively combines the advantages of both, enabling it to handle complex local features while also capturing and understanding the global contextual information. Therefore, this method can dynamically adjust feature attention across various scales and types of weld defects, enhancing multi-scale object detection capability, making it highly suitable for weld defect detection.