Abstract:
Timely and accurate knowledge of the number of sheep is of great significance for health management, feed allocation, disease prevention, and production planning in sheep farming. Under large-scale farming conditions, sheep are densely distributed with severe target occlusion and highly similar appearances, leading to low efficiency and accuracy in traditional manual counting methods. To address these challenges, this study proposed a lightweight dense sheep detection and counting model, DMR-YOLO (Dynamic Multi-scale Residual YOLO), which integrated YOLOv8n with knowledge distillation, aiming to achieve non-contact accurate counting of large-scale dense sheep populations. To enhance the model's ability to extract individual features in dense sheep flocks, a Dynamic Multi-scale Residual attention module was designed. Unlike traditional channel attention mechanisms such as ECA that suffered from fixed single-scale convolution kernels, static weight allocation, and unidirectional gradient propagation, the proposed DMR module employed a multi-scale parallel structure. Specifically, it introduced three convolutional kernel branches of sizes 1×3, 1×5, and 1×7 to capture short-range, medium-range, and long-range channel dependencies, respectively. After global average pooling, a fully connected layer consisting of Linear and Softmax operations generated three adaptive dynamic weights to match the three convolution operations. 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 module's output, which combined the original input features with the attention-enhanced features through an adaptive weighting mechanism. This design effectively mitigated the gradient vanishing problem in deep networks and improved training stability. To maintain high detection accuracy while keeping the model lightweight and easily deployable, a knowledge distillation strategy was employed. The pre-trained YOLOv8m, which had high detection accuracy but a large parameter size, was used as the teacher network, while the proposed DMR-YOLO served as the student network. The distillation process introduced a temperature coefficient T to soften the output of the Softmax function, enabling the student network to learn the dark knowledge from the teacher network. Through parameter optimization, the optimal distillation parameters 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 these two losses with the coefficient α. All experiments were conducted on a self-built Tan sheep dataset collected from a breeding farm in Ningxia, China. The dataset contained 1,765 annotated images with a resolution of 1920 ×
1080 pixels, covering six typical scenarios: daytime, nighttime, large targets (bounding box area > 0.5% of image area), small targets (bounding box area < 0.2% of image area), 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. Ablation experiments further confirmed the effectiveness of the DMR module and the knowledge distillation strategy. Moreover, compared with YOLOv11m and YOLOv12m, DMR-YOLO achieved superior detection accuracy with approximately 85.2% fewer parameters and 87.9% lower computational cost. The model demonstrates excellent counting accuracy across six typical environmental conditions, 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%, showing strong generalization in complex real-world farm settings. This study provides a theoretical basis and technical support for real-time accurate detection and deployment of large-scale dense sheep flocks.