3.2. Detection Results
To verify the performance of the proposed PPR-YOLO model in sow and piglet target detection, as well as piglet posture recognition tasks, we first conducted model validation on the overall images in the test set. In a total of 246 images, there were 2369 piglet targets and 246 sow targets. The results of sow and piglet posture recognition are shown in
Table 2.
To determine the specific
threshold setting in this study, a step size of 0.1 was used on the dataset to track changes in the proportions of piglets’ resting and suckling behaviors under different
thresholds, as illustrated in
Figure 9. The blue curve represents the recall rate of piglet suckling behavior above the
threshold, while the orange curve indicates the recall rate of piglet resting postures below the
threshold. A larger
threshold causes missed detections of suckling postures, whereas a smaller one leads to resting postures being misclassified as suckling postures. Therefore, to balance both, this paper sets
to 0.3, which excludes 96.2% of piglet suckling behaviors while retaining 91.7% of resting postures.
By setting the
threshold to 0.3, the piglet targets detected by the PPR-YOLO model were filtered, resulting in a total of 1659 piglet targets. The specific posture recognition results are presented in
Table 3. Compared to the overall piglet posture recognition results shown in
Table 2, the posture recognition performance for piglets outside the sow area did not exhibit significant changes. This is because piglets within the sow area are predominantly in a ventral lying and nursing posture, whereas piglets outside the sow area are more likely to be occluded by barriers and other factors. Consequently, in
Table 3, the recognition precision (P) for piglets outside the sow area decreased by 1.9% compared to the overall piglet posture recognition precision in
Table 2. Conversely, due to the tendency of piglets within the sow area to occlude each other during nursing behaviors, excluding the detection of piglets within the sow area in
Table 3 resulted in an increase in the recognition recall rate (R) by 3.3% compared to the overall piglet posture recognition recall rate in
Table 2.
To further evaluate the performance of the PPR-YOLO model,
Figure 10 presents the curves of precision, recall, and F1 scores as functions of the confidence threshold, as well as the precision–recall (P-R) curve. The results show that precision gradually increases with the confidence threshold, reaching 100% at a confidence value of 0.995 (
Figure 10a). Meanwhile, recall exhibits a downward trend; once the piglet posture confidence rises to 0.94, recall drops to 0 (
Figure 10b). The F1 score peaks at a confidence value of 0.412, indicating the optimal balance between precision and recall (
Figure 10c). The P-R curve further confirms the model’s overall performance under different confidence thresholds. At an IoU of 0.5, the average accuracy for all piglet posture categories is 88.2% (
Figure 10d). These findings demonstrate that PPR-YOLO maintains high detection accuracy and recall capability across various threshold settings.
Figure 11 illustrates the detection results of sow targets and piglet postures. Specifically,
Figure 11a,b show the detection results of overall and outside sow area piglet postures when the sow is not in the lactation state.
Figure 11c and 11d depict the detection results during the sow’s lactation state, where overall and filtered outside sow area piglet postures are shown, respectively. When the sow is not in the lactation state, piglets generally move outside the sow’s bounding box, as indicated by the red rectangular markers in
Figure 11b. However, due to piglets being at the edge of the enclosure and occluding each other, the model sometimes misclassifies ventral lying piglets as other postures. During the sow’s lactation state, as shown in
Figure 11c, piglets exhibit severe stacking and suckling behaviors, leading to occlusion and potential missed detections. In contrast,
Figure 11d demonstrates that piglets outside the sow area were successfully detected without misses.
Figure 10 presents the confusion matrices for the detection results of sow targets and piglet postures in the test set. Specifically,
Figure 12a,b correspond to the overall and outside sow area image detection results, respectively. The confusion matrices (a) and (b) do not show significant differences. From confusion matrix (a), it can be observed that the body size difference between sows and piglets is substantial, resulting in no recognition confusion. In terms of specific piglet posture recognition, certain postures are prone to be confused with adjacent postures. For example, piglets in a lateral lying posture are more likely to be recognized as ventral lying postures, while other postures (sitting, kneeling, and standing) require transitioning through ventral lying postures. Therefore, the feature differences are considerable, and the probability of confusion is low.
3.3. Ablation Experiments
To evaluate the impact of image cropping of the sow’s farrowing bed area during the dataset creation process on the detection results of the PPR-YOLO algorithm, we compared the training performance of the sow target and piglet posture detection models using both the original uncropped data and the cropped dataset. The training and testing sets were divided identically, and the specific test results are presented in
Table 4 below. After cropping, the images input into the model have a smaller image compression ratio. Consequently, compared to the uncropped data, the test recall rate (R),
[email protected], and
[email protected]:0.95 increased by 2.2%, 1.8%, and 2.3%, respectively. However, the increase in recall introduced more challenging samples, resulting in a slight decrease in precision by 1.3%. Overall, the cropped data effectively improved the model’s performance.
The proposed PPR-YOLO model is based on YOLOv11s. By replacing standard convolutions with the Conv2 module and substituting the original BottleNeck structure with the inverted bottleneck convolution module IBCNeck, the model achieves the sow target and piglet posture detection tasks described in this study. To assess the impact of different improvement strategies on the original model, we conducted ablation experiments using the control variable method. Training and testing were performed on the same dataset, and the specific results are shown in
Table 5 below.
From
Table 5, it can be observed that the improved Conv2 module enhances the original model’s
[email protected] and
[email protected]:0.95 metrics by 1.2% and 1.6%, respectively, without increasing the computational load during inference. This is because although the Conv2 module employs a multi-branch structure in the training phase to enhance feature extraction, it merges the multiple branches into a single branch at inference time via structural re-parameterization, thereby maintaining computational efficiency and memory usage. Consequently, introducing the Conv2 module does not increase time or memory complexity during inference.
Using only the Conv2 module yields results comparable to using only the IBCNeck module. However, the IBCNeck module introduces an increase in the number of parameters and floating-point operations, with a 14.9% and 34.7% increase compared to the original YOLOv11s model, respectively. Nevertheless, the PPR-YOLO model, which combines both Conv2 and IBCNeck, improves
[email protected] and
[email protected]:0.95 by 1.4% and 2.3% over YOLOv11s in sow and piglet posture detection tasks while retaining efficient inference through the Conv2 module’s structural re-parameterization. These results show that PPR-YOLO achieves noticeable performance gains without significantly increasing inference complexity, making it well-suited for commercial farm scenarios that demand high real-time performance and accuracy.
In this study, a class-agnostic NMS algorithm was employed during the model’s post-processing stage to address the uncertainty that a single piglet target might be assigned multiple posture classes. To validate the impact of this post-processing strategy on the PPR-YOLO model, a performance evaluation was conducted on the same test set, and the specific results are presented in
Table 6. It can be observed that applying a single-class NMS strategy sacrifices some piglet detection performance in the test set (with a 0.7% decrease in recall), yet it significantly increases the detection precision (P) for piglet postures by 1.4%.
To enhance the interpretability of the improved PPR-YOLO model, Eigen-CAM [
25] was utilized to visualize the features of the last convolutional layer before the small target detection head in the detection model. As shown in
Figure 13,
Figure 13a is the original input image,
Figure 13b shows the detection results of the proposed algorithm, and
Figure 13c–f display the heatmaps of regions of interest for features under different improvements. The improvements in different modules (
Figure 13d,e) show certain attention enhancements compared to the baseline YOLOv11s model (
Figure 13f). However, when using only the IBCNeck module, some irrelevant regions are also focused on. The visualization results of the proposed algorithm (
Figure 13c) compared to the original YOLOv11s model (
Figure 13f) demonstrate that the improved model pays more attention to the piglet regions, suppresses interference from irrelevant areas, and focuses more on the detailed regions of individual piglets, facilitating the differentiation of different piglet postures.
3.4. Comparison of Different Models
To further evaluate the performance of the proposed PPR-YOLO model in sow target and piglet posture detection tasks, mainstream single-stage object detection algorithms were compared, including RetinaNet, SSD, YOLOv3, YOLOv5s, and YOLOv8s [
26,
27,
28]. These models were trained and tested on the same sow target and piglet posture detection dataset, and the specific results are presented in
Table 7 below. The results indicate that the proposed PPR-YOLO model outperforms current mainstream algorithms across multiple performance metrics evaluated on the test set. The model achieved a detection precision of 87.7% and a recall rate of 88.2% and demonstrated the best performance among all compared algorithms, fully validating its accuracy in the piglet posture detection task.
Figure 14 presents a comprehensive performance comparison of the different detection algorithms. In the figure, models that were positioned closer to the outer edge exhibit better performance. As seen in
Table 7, the proposed PPR-YOLO model achieves
[email protected] and
[email protected]:0.95 scores of 92.0% and 74.8%, respectively, significantly outperforming other models, especially in the high IoU threshold metric
[email protected]:0.95. This indicates its superior detection capability in complex scenarios. The model has a parameter count of 10.8M, significantly lower than YOLOv3’s 61.5M, and is comparable to YOLOv8s’s 11.1M, highlighting its lightweight design, which reduces storage and computational resource requirements. In terms of computational complexity, although the PPR-YOLO model has 28.7 GFLOPs, which is slightly higher than YOLOv5s’s 17.5 GFLOPs, it achieves over 5% higher detection accuracy and nearly a 3% improvement in recall rate compared to YOLOv5s. This demonstrates a balanced advantage between performance and efficiency.
3.5. Analysis of Piglet Resting Postures
The proposed piglet posture detection algorithm, PPR-YOLO, was applied to continuous video detection. Six pens were selected, each housing between 6 and 11 piglets. Videos captured between 12:30 p.m. and 1:30 p.m. were selected for piglet posture detection, performing detection every 10 s. The distribution of piglet postures across different pens is shown in
Figure 15, ordered from the smallest to the largest number of piglets. It was observed that in Pen C2, a higher proportion of piglets were located within the sow’s area, and the proportion of comfortable lateral lying postures was the lowest, indicating that piglets in this pen might have been experiencing hunger. In Pen C4, the highest proportion of lateral lying resting postures was observed, and the proportion of piglets within the sow’s area was only slightly lower than in Pen C1, suggesting that piglets in Pen C4 were in a more comfortable state. In Pen C1, the combined proportion of lateral and ventral lying resting postures was the highest, but the proportion of ventral lying was excessively high, indicating that piglets in this pen might have been experiencing stress or discomfort.
The posture distribution of piglets in Pens C2 and C4 over time is illustrated in
Figure 16. In the figure, the horizontal axis represents the video frame index (every 10 s for a total of 1 h), while the vertical axis shows the distribution of piglet postures detected by the PPR-YOLO model at each moment in time. From bottom to top, the stacked legend denotes piglets in lateral lying, ventral lying, other postures, and piglets within the sow’s area, respectively. Due to factors such as pen layout and occlusion between piglets, the number of detected piglets fluctuated. In Pen C2, which housed seven piglets, two instances of sow lactation were observed in
Figure 16a. During these times, 100% of piglets were within the sow’s area. The original video shows that during the second lactation, the sow refused to nurse, causing piglets to increase their movement within the pen (indicated by the green area), which also explains the reduced proportion of comfortable lateral lying postures. Over the entire hour, the percentages of piglets in lateral lying, ventral lying, other postures, and within the sow’s area were 10.54%, 12.23%, 4.08%, and 73.15%, respectively. In Pen C4, which housed nine piglets, two instances of sow lactation were also observed, but the piglets quickly transitioned to lateral lying postures with minimal transitional ventral lying postures, as shown in
Figure 16b. This indicates that piglets in Pen C4 received sufficient milk intake and were in a satisfied state. Overall, the percentages of piglets in lateral lying, ventral lying, other postures, and within the sow’s area were 49.69%, 14.06%, 2.39%, and 33.87%, respectively.