Abstract:Pig counting is a critical task in large-scale breeding and intelligent management, and the manual counting method is time-consuming and labor-intensive. Since fattening pigs are more active and like to congregate, there are many high-density areas in the image, which causes problems such as adhesion and occlusion between pigs, making pig counting difficult. Based on the SOLO v2 instance segmentation algorithm, a high-density group pig counting model in natural breeding scenarios was proposed, which incorporated multi-scale feature pyramids and deformable convolutions networks version 2. Further, by optimizing the model structure, the consumption and occupation of computing resources were reduced. The pig count dataset published by iFLYTEK was divided into two parts: pig segmentation dataset and pig count test set. The pig segmentation dataset was used to train the segmentation model to achieve herd segmentation and pig counts, and the inventory accuracy was tested in the pig inventory test set. The experimental results showed that the high-density pig counting model proposed had a segmentation accuracy of 96.7% and a model weight size of 256 MB, which was 1/3 less than that before the improvement. Each improved method proposed improved the model's segmentation accuracy and achieved highaccuracy segmentation of individual pigs in the case of occlusion, adhesion, and overlap. In the 500 image pig counting test set, the number of images when the model counted pigs with an error of 0 was 207, which was 26% more than that before the improvement. The number of images when the error of the model counting pigs was less than two pigs, accounted for 97.2% of the total number of test images. The number of images with a counting error of more than three pigs, accounted for only 1% of the total number of images. Finally, for the pig herd counting method based on YOLO v5, the model had a better segmentation effect and counting accuracy, proving the methods effectiveness for counting pigs in groups. Therefore, the model presented not only achieved accurate counting of high-density pig herds, but it also significantly reduced the model's reliance on computing equipment by optimizing the model structure, which made it suitable for online counting of pig herds on real farms.