# YOLOv5 DA: An Improved YOLOv5 Model for Posture Detection of Grouped Pigs
## 2 Materials and Methods
### YOLOv5 DA  
When pigs are housed in groups, it is common for live pigs to gather, shelter, and adhere, which often causes missed or false detections in the object detection model. This issue arises from the scarcity of small targets within the dataset, coupled with the YOLOv5 model's constrained ability to extract features from irregularly shaped objects. To further reduce the detection errors and missing rate of the object detection model, we made three improvements to YOLOv5s to obtain the YOLOv5 DA model. Firstly, in data preprocessing, the Mosaic9 data augmentation method was used to increase the number of small targets in the dataset; secondly, deformable convolution was introduced into the feature extraction network (Backbone) for flexible sampling positions; finally, adaptive spatial feature fusion was introduced in the Head to adaptively learn the fusion weights of different scale features. In the following sections, we will provide detailed explanations of these three optimizations. The final model structure diagram is shown in Figure 3.  
Figure 3: YOLOv5 DA model structure diagram. Group-housed pigs often gather and block each other, which will generate more small targets in the image [36], which may largely reduce the detection accuracy of object detection. To address this issue, we introduced the Mosaic9 data augmentation method in data preprocessing [22]. Figure 4 shows the specific implementation process. Firstly, this method randomly selects nine images from the training images of a batch size, and then each image is randomly cropped and zoomed. Finally, the nine cropped and zoomed images are reassembled into a new image, which is repeated multiple times to form multiple enhanced images for model training. The utilization of Mosaic9 data augmentation in the training set can augment the number of small targets in the dataset and enhance the background information of the samples [37]. Thereby it boosted the model's generalization and detection capabilities for small targets, ultimately improving the object detection model's performance. (2) Deformable convolution  
Under group housing conditions, pigs tend to gather and block, which inevitably leads to irregular shapes of individuals. Traditional convolution always uses fixed-size convolution kernels for sampling at fixed positions in the feature map, which greatly limits the modeling's ability to handle irregular objects. To address this issue, we introduced a more flexible deformable convolution (DC) [23] into the feature extraction network (Backbone). Figure 5 shows the specific implementation process of deformable convolution. Firstly, based on the input image, traditional convolutional kernels are used to extract the feature maps. Secondly, the acquired feature maps are fed into the model as input, and an additional convolutional layer is incorporated to generate the deformation offset required for deformable convolution. Among them, the offset layer is 2N, where 2 indicates the 2D offsets X and Y, respectively, and N is the number of channels. Then, the offset of the obtained deformable convolution deformation is added to the feature map extracted in the first step to obtain the offset position. Next, the bilinear interpolation algorithm is used to obtain the pixels at each position after the offset to obtain the output feature map. Finally, the newly obtained output feature map is used as input data to the next layer for subsequent feature extraction. During training, the convolutional kernels used to generate feature maps extracted in the first step and offsets extracted in the second step are synchronously learned. Learning of the offset is achieved through backpropagation using bilinear interpolation algorithms.