# YOLOv5 DA: An Improved YOLOv5 Model for Posture Detection of Grouped Pigs
## 2 Materials and Methods
### YOLOv5 DA
The expression of the output feature map is shown in Equation (1), where \(p_{n}\) represents the offset of convolutional kernel elements relative to the center of the convolutional kernel, \(R\) indicates the set of all \(p_{n}\), \(p_{0}\) represents the coordinates of the convolutional kernel center relative to the upper left corner of the input feature map, \(\Delta p_{n}\) represents an offset of deformable convolution deformation, usually in decimal places. \(w(p_{n})\) presents a weight function, in which \(p_{n}\) is used as input to output the weight of the coordinate position \(p_{n}\), and the weight corresponding to each coordinate is known. \(p_{0}+p_{n}+\Delta p_{n}\) represents the original coordinate value plus the generated offset coordinate  
Figure 4: Schematic Diagram of Mosaic9 Data Augmentation: Adapted from Reference [22]. value, which results in a new coordinate value. \(x(p_{0}+p_{n}+\Delta p_{n})\) means to input the new coordinate value and output the pixel corresponding to its coordinates obtained by bilinear interpolation. The expression of \(x(p)\) is shown in Equation (2), where \(q\) represents the coordinates of all pixels in the image and \(x(q)\) stands for the input of a coordinate value and the output of its corresponding pixel value. The pixel values corresponding to each integer coordinate are known. \(G(q,p)\) represents the weight corresponding to coordinate \(q\). The expression of \(G(q,p)\) is shown in Equation (3), where \(q_{x}\) and \(p_{x}\) represent the abscissa of pixel point \(q\) and p in the image, respectively. \(q_{y}\) and \(p_{y}\) indicate the vertical coordinates of pixel point \(q\) and p in the image, respectively. \(g(q_{x},p_{x})\) represents the calculated weight. The expression of \(g(q_{x},p_{x})\) is shown in Equation (4), which means that when the difference between two coordinate values is between 0 and 1, the weight is 1 minus their difference, and if their difference is greater than 1, the weight is 0. \[y(p_{0})=\sum_{p_{n}\in R}w(p_{n})x(p_{0}+p_{n}+\Delta p_{n}) \tag{1}\]  
\[x(p)=\sum_{q}G(q,p)\cdot x(q)\ \ \ \ \ p=p_{0}+p_{n}+\Delta p_{n} \tag{2}\]  
\[G(q,p)=g(q_{x},p_{x})\cdot g\big{(}q_{y},p_{y}\big{)} \tag{3}\]  
\[g(a,b)=max(0,\ 1-|a-b|) \tag{4}\]  
Compared with traditional convolution, deformable convolution increases the learnable coordinate offsets, making the sampling position flexible, and thus the extracted features can better match the shape of the target object [38], which greatly improves the modeling ability of the model for irregular objects [39]. (3) Adaptive Spatial Feature Fusion  
Mainstream research usually uses a feature pyramid structure to utilize semantic information at high levels and spatial information at low levels. YOLOv5s adopts a feature pyramid structure attached to a bottom-up path aggregation network to learn richer semantic information. However, it fails to consider the varying significance of features across different resolutions during the process of multi-scale feature fusion. Direct addition of elements from adjacent feature layers or concatenating feature layers into the depth direction usually cannot achieve the best detection performance [40]. To address this issue, we introduced adaptive spatial feature fusion [24] in the Head. Figure 6 shows the diagram of this structure, which is mainly completed in two steps: size adjustment and adaptive fusion. Size adjustment is mainly achieved through three operations: up sampling, maximum pooling, and down sampling. For up sampling, the method first uses a \(1\times 1\) convolution to adjust the number of channels to the same size and then employs the nearest neighbor interpolation to enlarge the size. For \(1/2\) rate down sampling, it directly uses a \(3\times 3\) convolution with a step of 2 to complete. For \(1/4\) rate  
Figure 5: Schematic diagram of deformable convolution operation: Adapted from Reference [23]. down sampling, it first uses a maximum pooling with a step of 2 and then processes it using a 3 \(\times\) 3 convolution with a step of 2.