Abstract:
Timely and accurate detection and counting of dense sheep are often required for health management, feed allocation, disease prevention, and production planning in livestock farming. However, conventional manual counting cannot fully meet the requirements of large-scale farming in recent years. Particularly, sheep can be densely distributed, with severe target occlusion and highly similar appearances, leading to low efficiency and accuracy. In this study, a lightweight model, DMR-YOLO (Dynamic Multi-scale Residual YOLO), was proposed for the detection and counting of dense sheep. YOLOv8n was also integrated with knowledge distillation. Non-contact accurate counting was performed on large-scale populations of closely packed sheep. A Dynamic Multi-Scale Residual (DMR) attention was designed to extract individual features from dense sheep flocks. A multi-scale parallel structure was employed to reduce the issues from fixed single-scale convolution kernels, static weight allocation, and unidirectional gradient propagation in conventional channel attention mechanisms, such as Efficient Channel Attention (ECA). Specifically, three convolutional kernel branches were introduced with the sizes 1×3, 1×5, and 1×7 to capture short-, medium- and long-range channel dependencies, respectively. Three adaptive dynamic weights were generated to match the three convolution operations after global average pooling. A fully connected layer consisted of Linear and Softmax functions. The outputs of the multi-scale convolution group were then weighted and fused, followed by Sigmoid reconstruction. Furthermore, a residual connection was introduced at the output. The original input features were combined with the attention-enhanced features using an adaptive weighting mechanism. The training stability was improved to mitigate gradient vanishing in deep networks. Knowledge distillation was employed to maintain high detection accuracy for the lightweight and easily deployable model. The teacher network was taken as the pre-trained YOLOv8m with high detection accuracy but a large parameter size. The DMR-YOLO served as the student network. A temperature coefficient was introduced to soften the output of the softmax function during distillation. The student network was implemented to learn the dark knowledge from the teacher network. The optimal parameters of distillation were determined as
T = 7 and
α = 0.6. The soft loss was calculated using Kullback-Leibler divergence, while the hard loss was calculated using the cross-entropy loss function. The final distillation loss function combined the two losses with the coefficient α. All experiments were conducted on a self-built tan sheep dataset from a breeding farm in Ningxia, China. The dataset contained 1,765 annotated images with a resolution of
1920 ×
1080 pixels. Six typical scenarios included daytime, nighttime, large targets (bounding box area > 0.5% of image area), small targets (bounding box area < 0.2% of image area), and indoor and outdoor environments. The dataset was divided into training, validation, and test sets at a ratio of 8:1:1. Experimental results demonstrated that after knowledge distillation, DMR-YOLO achieved a mAP@0.5 of 98.10% and a mAP@0.5-0.95 of 92.10%, a precision of 95.80% and a recall of 95.10%, with only 3.04 million parameters and 8.26 GFLOPs. Compared with the baseline YOLOv8n model, precision, recall, mAP@0.5, and mAP@0.5-0.95 increased by 1.70, 1.80, 0.20, and 5.60 percentage points, respectively. Compared with the teacher model YOLOv8m, the computational cost and parameters were reduced by 89.6% and 88.2%, respectively, while other performance metrics nearly reached the teacher model's level. Furthermore, ablation experiments confirmed that better performance was achieved with the DMR module and knowledge distillation, with superior accuracy of 85.2% and 87.9% and lower computational cost, compared with YOLOv11m and YOLOv12m. The excellent counting accuracy was also obtained under six typical scenarios, with an average counting error of 1.96 sheep, an average counting error rate of 3.82%, and an average counting accuracy of 96.31%, indicating strong generalization in complex real-world farm settings. This finding can provide a theoretical basis and technical support for real-time accurate detection and deployment of large-scale dense sheep flocks.