An Enhanced YOLOv8n-Based Approach for Pig Behavior Recognition
Abstract
1. Introduction
2. Data Collection and Preprocessing
2.1. Data Collection
2.2. Dataset Construction and Analysis
- (1)
- Video-level Splitting Strategy: To evaluate the model’s genuine generalization performance on unseen data, the 20 source videos were pre-partitioned into training, validation, and testing sets according to a strict 8:1:1 ratio at the video level. This ensures that the test set contains entirely novel scenes and pig individuals never encountered during training.
- (2)
- Multi-frequency Frame Sampling: Given the high spatial-temporal correlation between consecutive frames, we adopted a class-specific temporal sampling strategy to balance the dataset: General Sampling: For common behaviors (Stand and Lie), a sparse sampling interval of 5 seconds was used to reduce redundancy; Targeted Sampling for Rare Behaviors: To mitigate the natural scarcity of aggressive interactions, segments containing fight and tail-bite were sampled at a higher frequency of 1.5 s. This targeted approach successfully increased the number of instances of rare behaviors, enabling the model to learn the fine-grained morphological features of mouth-to-tail and head-to-head contact. A total of 2500 high-quality images were finalized.
- (3)
- Training Set Augmentation: During each epoch, stochastic transformations, including horizontal flipping, rotation, brightness adjustment, and Mixup, were applied to the training images. This ensures that the model encounters a stochastic variety of samples in every iteration. A representative result of the augmentation process is presented in Figure 2.
- (4)
- Annotation and Quality Control: The images were manually annotated using LabelImg. To ensure scientific rigor and inter-observer reliability, a two-stage verification protocol was implemented. Three trained researchers performed the primary labeling based on strict morphological criteria. A cross-review was conducted where annotators swapped datasets to identify and correct discrepancies. The extracted images were manually annotated using the LabelImg tool, covering five distinct behaviors: stand, lie, eat, fight, and tail-bite. Table 1 provides an overview of the classification criteria for pig behaviors along with the associated dataset distribution.

3. Research Methodology
3.1. YOLO Object Detection Algorithm
3.2. Improved Method for Pig Behavior Recognition Based on YOLOv8n
3.2.1. SE Attention Mechanism
3.2.2. C3Ghost Convolutional Module
3.2.3. GIoU Loss Function
3.3. Improved YOLOv8n-Based Model for Pig Behavior Recognition
4. Experimental Results and Analysis
4.1. Experimental Environment
4.2. Evaluation Metrics
4.3. Experimental Results Analysis
4.3.1. Ablation Study Results and Analysis
4.3.2. Comparison of Experimental Results Across Different Models
4.3.3. Recognition Result Analysis
5. Conclusions
- (1)
- To address the challenges posed by limited computational resources in real-world farming applications and the need for real-time performance, the YOLOv8n model was enhanced by incorporating the GhostNet architecture and an SE attention mechanism module. Additionally, the bounding box loss function was modified from CIoU to GIoU, reducing the model’s computational complexity. As a result, the proposed YOLOv8n-based pig behavior recognition model exhibits a 42.93% reduction in parameters and a 38.27% decrease in floating-point operations. With an average precision of 96.8%, the model shows a 2.6 percentage point improvement over the original YOLOv8n, thereby achieving a balance between lightweight design and enhanced recognition accuracy.
- (2)
- The improved pig behavior recognition model outperforms advanced network architectures, including YOLOv9n, YOLOv10n, YOLO11n, and YOLOv12n, in terms of detection accuracy. Compared with widely used object detection models, this approach demonstrates robust performance in recognizing pig behaviors, even in challenging environments such as crowded pigpens and poor lighting. While achieving an inference speed of 126.26 FPS on an NVIDIA RTX 4060 GPU, the model’s small architectural footprint (only 1.7 MB) makes it an ideal candidate for real-time deployment on edge devices. Nevertheless, it should be noted that the performance metrics reported in this study are derived from single training runs. Due to computational resource constraints and our primary focus on architectural optimization for practical applications, we did not include variance or confidence intervals based on multiple random seeds. To ensure the reliability of the comparative results, a strictly identical experimental environment was maintained for all models, including hardware configurations, hyperparameter settings, and data partitioning. While this approach may involve minor stochastic variability, the consistent improvements observed across different behavior categories suggest that the proposed modifications are effective, and future research will incorporate multi-run statistical validation to further quantify the model’s stochastic stability.
- (3)
- The enhanced YOLOv8n model proposed in this study achieves significant improvements in detection accuracy and computational efficiency for pig behavior recognition. However, certain limitations persist. One challenge is the model’s difficulty distinguishing between stand and lie behaviors from certain angles, due to the similarity of their visual features. While the model performs well at detecting large-scale behaviors, it struggles to identify small-scale features. Additionally, the dataset used in this study is restricted to a single facility with pigs of similar body size. This narrow acquisition domain limits the model’s performance evaluation under domain shifts. While the model achieves robust in-domain performance, testing on unseen pens, under different lighting conditions, across various age groups, and on different farms is necessary to fully validate its generalizability across settings. Therefore, for broader practical applications, the model must be further adapted to cope with complex environmental changes.
- (4)
- Future research will focus on several key directions to further enhance the performance and applicability of the model. Firstly, multimodal data fusion will be explored to expand perception capabilities by incorporating additional sources, such as audio and temperature. Secondly, architectural innovations from the latest iterations, including the attention-centric C2PSA modules in YOLO12, the NMS-free training protocols featured in YOLO11, and the performance benchmarking framework introduced in YOLO26 [42], will be integrated to better resolve behavioral overlaps in high-density environments. Thirdly, the temporal dynamics of pig behaviors will be captured through sequence analysis, employing methods such as temporal convolutional networks or recurrent neural networks to distinguish transitional postures. Fourthly, efforts will continue to optimize the model’s weight for efficient deployment on resource-constrained edge devices. Moreover, the potential for extending this method to other livestock species will be investigated to advance intelligent agricultural technologies. Finally, practical considerations, including user feedback and real-time system integration, will be prioritized to enhance the system’s operational effectiveness.
Author Contributions
Funding
Data Availability Statement
Acknowledgments
Conflicts of Interest
References
- Hao, W.; Han, W.; Han, M.; Li, F. A novel improved yolov3-sc model for individual pig detection. Sensors 2022, 22, 8792. [Google Scholar] [CrossRef] [PubMed]
- Marcon, M.; Brossard, L.; Quiniou, N. Precision feeding based on individual daily body weight of group-housed pigs with an automatic feeder developed to allow for restricting feed allowance. Precis. Livest. Farming 2015, 15, e601. [Google Scholar]
- Tran, D.; Nguyen, T.N.; Khanh, P.C.P.; Tran, D. An iot-based design using accelerometers in animal behavior recognition systems. IEEE Sens. J. 2021, 22, 17515–17528. [Google Scholar] [CrossRef]
- Hou, S.; Wang, T.; Qiao, D.; Xu, D.J.; Wang, Y.; Feng, X.; Khan, W.A.; Ruan, J. Temporal-Spatial Fuzzy Deep Neural Network for the Grazing Behavior Recognition of Herded Sheep in Triaxial Accelerometer Cyber-Physical Systems. IEEE Trans. Fuzzy Syst. 2024, 33, 338–349. [Google Scholar] [CrossRef]
- Dai, X.; Wu, J.; Cheng, G.; Yang, L.; Wang, Y.; Li, Z.; Han, S. Applications and challenges of wearable devices in livestock and poultry health management. J. Nanjing Agric. Univ. 2025, 48, 766–780. [Google Scholar]
- Nasirahmadi, A.; Edwards, S.A.; Sturm, B. Implementation of machine vision for detecting behaviour of cattle and pigs. Livest. Sci. 2017, 202, 25–38. [Google Scholar] [CrossRef]
- Mellor, D.J. Updating animal welfare thinking: Moving beyond the “Five Freedoms” towards “a Life Worth Living”. Animals 2016, 6, 21. [Google Scholar] [CrossRef]
- Zhu, Z.; Wang, H.; Li, B.; Zhao, W.; Zhu, J.; Jia, N.; Zhao, Y. Research progress of deep learning in recognition of typical behaviors of livestock and poultry. J. Agric. Sci. Technol. 2024, 26, 110–124. [Google Scholar]
- Munsterhjelm, C.; Heinonen, M.; Valros, A. Effects of clinical lameness and tail biting lesions on voluntary feed intake in growing pigs. Livest. Sci. 2015, 181, 210–219. [Google Scholar] [CrossRef]
- Krsnik, B.; Yammine, R.; Pavičić, Ž.; Balenović, T.; Njari, B.; Vrbanac, I.; Valpotić, I. Experimental model of enterotoxigenic Escherichia coli infection in pigs: Potential for an early recognition of colibacillosis by monitoring of behavior. Comp. Immunol. Microbiol. Infect. Dis. 1999, 22, 261–273. [Google Scholar] [CrossRef]
- Rydhmer, L.; Zamaratskaia, G.; Andersson, H.K.; Algers, B.; Guillemet, R.; Lundström, K. Aggressive and sexual behaviour of growing and finishing pigs reared in groups, without castration. Acta Agric. Scand Sect. A 2006, 56, 109–119. [Google Scholar] [CrossRef]
- Kashiha, M.; Bahr, C.; Haredasht, S.A.; Ott, S.; Moons, C.P.; Niewold, T.A.; ödberg, F.O.; Berckmans, D. The automatic monitoring of pigs water use by cameras. Comput. Electron. Agric. 2013, 90, 164–169. [Google Scholar] [CrossRef]
- Yang, Q.; Xiao, D.; Lin, S. Feeding behavior recognition for group-housed pigs with the Faster R-CNN. Comput. Electron. Agric. 2018, 155, 453–460. [Google Scholar] [CrossRef]
- Nasirahmadi, A.; Sturm, B.; Olsson, A.; Jeppsson, K.; Müller, S.; Edwards, S.; Hensel, O. Automatic scoring of lateral and sternal lying posture in grouped pigs using image processing and Support Vector Machine. Comput. Electron. Agric. 2019, 156, 475–481. [Google Scholar] [CrossRef]
- Nasirahmadi, A.; Hensel, O.; Edwards, S.A.; Sturm, B. Automatic detection of mounting behaviours among pigs using image analysis. Comput. Electron. Agric. 2016, 124, 295–302. [Google Scholar] [CrossRef]
- Wang, R.; Gao, R.; Li, Q.; Zhao, C.; Ma, W.; Yu, L.; Ding, L. A lightweight cow mounting behavior recognition system based on improved YOLOv5s. Sci. Rep. 2023, 13, 17418. [Google Scholar] [CrossRef]
- Shang, C.; Wu, F.; Wang, M.; Gao, Q. Cattle behavior recognition based on feature fusion under a dual attention mechanism. J. Vis. Commun. Image Represent. 2022, 85, 103524. [Google Scholar] [CrossRef]
- Guo, J.; He, G.; Xu, L.; Liu, T.; Feng, D.; Liu, S. Behavior detection model of meat pigeons based on improved YOLOv4. Trans. Chin. Soc. Agric. Mach. 2023, 54, 347–355. [Google Scholar]
- Ge, S.; Ji, H.; Zhan, Y.; Li, X.; Zheng, W.; Wang, T. Lightweight pig posture recognition method based on improved YOLOv5s. J. China Agric. Univ. 2025, 30, 179–189. [Google Scholar]
- Guo, J.; Kong, Y.; Lin, L.; Xu, L.; Feng, D.; Cao, L.; Chen, J.; Ye, J.; Ye, S.; Yao, Z. Lightweight network based on Fourth order Runge-Kutta scheme and Hybrid Attention Module for pig face recognition. Comput. Electron. Agric. 2024, 223, 109099. [Google Scholar] [CrossRef]
- Guo, J.; Kong, Y.; Liu, S.; Liu, T.; Cao, L.; Liu, Y. Construction and application of a digital unmanned pig farming system. J. Huazhong Agric. Univ. 2024, 43, 288–296. [Google Scholar]
- Chen, H.; Yin, L.; Yang, M.; Zhang, S.; Lin, J. Multimodal pig behavior recognition based on vision and sensor fusion. Trans. Chin. Soc. Agric. Eng. 2025, 41, 194–203. [Google Scholar]
- Wang, C.; Yeh, I.; Mark Liao, H. Yolov9: Learning what you want to learn using programmable gradient information. In European Conference on Computer Visio; Springer: Berlin/Heidelberg, Germany, 2024; pp. 1–21. [Google Scholar]
- Wang, A.; Chen, H.; Liu, L.; Chen, K.; Lin, Z.; Han, J.; Ding, G. Yolov10: Real-time end-to-end object detection. Adv. Neural Inf. Process. Syst. 2024, 37, 107984–108011. [Google Scholar]
- Jocher, G.; Qiu, J. Ultralytics YOLO11. Available online: https://docs.ultralytics.com/zh/models/yolo11 (accessed on 14 May 2025).
- Tian, Y.; Ye, Q.; Doermann, D. Yolov12: Attention-centric real-time object detectors. arXiv 2025, arXiv:2502.12524. [Google Scholar]
- Reis, D.; Kupec, J.; Hong, J.; Daoudi, A. Real-time flying object detection with YOLOv8. arXiv 2023, arXiv:2305.09972. [Google Scholar] [CrossRef]
- Jocher, G.; Chaurasia, A.; Stoken, A.; Borovec, J.; Kwon, Y.; Michael, K.; Fang, J.; Wong, C.; Yifu, Z.; Montes, D. ultralytics/yolov5: V6. 2-yolov5 classification models, apple m1, reproducibility, clearml and deci. ai integrations. Zenodo 2022. [Google Scholar] [CrossRef]
- Wang, C.; Liao, H.; Wu, Y.; Chen, P.; Yeh, I. A new backbone that can enhance learning capability of CNN. 2020 IEEE. In CVF Conference on Computer Vision and Pattern Recognition Workshops (CVPRW); IEEE: Piscataway, NJ, USA, 2020; pp. 390–391. [Google Scholar]
- Song, G.; Liu, Y.; Wang, X. Revisiting the sibling head in object detector. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, Seattle, WA, USA, 13–19 June 2020; pp. 11563–11572. [Google Scholar]
- Zhang, S.; Chi, C.; Yao, Y.; Lei, Z.; Li, S.Z. Bridging the gap between anchor-based and anchor-free detection via adaptive training sample selection. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, Seattle, WA, USA, 13–19 June 2020; pp. 9759–9768. [Google Scholar]
- Li, X.; Wang, W.; Wu, L.; Chen, S.; Hu, X.; Li, J.; Tang, J.; Yang, J. Generalized focal loss: Learning qualified and distributed bounding boxes for dense object detection. Adv. Neural Inf. Process. Syst. 2020, 33, 21002–21012. [Google Scholar]
- Woo, S.; Park, J.; Lee, J.; Kweon, I.S. Cbam: Convolutional block attention module. In Proceedings of the European Conference on Computer Vision (ECCV), Munich, Germany, 8–14 September 2018; pp. 3–19. [Google Scholar]
- Yang, L.; Zhang, R.; Li, L.; Xie, X. Simam: A simple, parameter-free attention module for convolutional neural networks. In Proceedings of the International Conference on Machine Learning, Vienna, Austria, 18–24 July 2021; pp. 11863–11874. [Google Scholar]
- Cao, Y.; Xu, J.; Lin, S.; Wei, F.; Gcnet, H.H. Non-Local Networks Meet Squeeze-Excitation Networks and Beyond, 2019 IEEE. In Proceedings of the CVF International Conference on Computer Vision Workshop (ICCVW), Seoul, Republic of Korea, 27 October–2 November 2019; pp. 1971–1980. [Google Scholar]
- Li, Y.; Yao, T.; Pan, Y.; Mei, T. Contextual transformer networks for visual recognition. IEEE Trans. Pattern Anal. Mach. Intell. 2022, 45, 1489–1500. [Google Scholar] [CrossRef]
- Fang, P.; Hao, H.; Li, T.; Wang, H. Instance segmentation of broiler image based on attention mechanism and deformable convolution. Trans. Chin. Soc. Agric. Mach. 2021, 52, 257–265. [Google Scholar]
- Hu, J.; Shen, L.; Sun, G. Squeeze-and-excitation networks. In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, Seattle, WA, USA, 13–19 June 2018; pp. 7132–7141. [Google Scholar]
- Hou, M.; Hao, W.; Dong, Y.; Ji, Y. A detection method for the ridge beast based on improved YOLOv3 algorithm. Herit. Sci. 2023, 11, 167. [Google Scholar] [CrossRef]
- Mpofu, J.B.; Li, C.; Gao, X.; Su, X. Optimizing motion detection performance: Harnessing the power of squeeze and excitation modules. PLoS ONE 2024, 19, e308933. [Google Scholar] [CrossRef] [PubMed]
- Han, K.; Wang, Y.; Tian, Q.; Guo, J.; Xu, C.; Xu, C. GhostNet: More Features From Cheap Operations. In Proceedings of the 2020 IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), Seattle, WA, USA, 14–19 June 2020; pp. 1577–1586. [Google Scholar]
- Sapkota, R.; Cheppally, R.H.; Sharda, A.; Karkee, M. YOLO26: Key architectural enhancements and performance benchmarking for real-time object detection. arXiv 2025, arXiv:2509.25164. [Google Scholar] [CrossRef]










| Behavior Category | Behavior Image | Label | No. of Instances | Judgment Criteria for Annotation |
|---|---|---|---|---|
| stand | ![]() | stand | 6340 | All four hooves are in contact with the ground; the spine is horizontal or slightly arched. |
| lie | ![]() | lie | 7850 | The limbs are tucked or extended; a large area of the torso is touching the floor. |
| eat | ![]() | eat | 5380 | The snout is inside or significantly overlapping the feeding trough area. |
| fight | ![]() | fight | 2520 | Head-to-head or head-to-body contact; involves pushing or biting postures between individuals. |
| tail-bite | ![]() | tail-bite | 2250 | The attacker’s snout is in direct contact with or extremely close to the tail region of another pig. |
| AcMix | CBAM | ECA | Triplet | SE | mAP |
|---|---|---|---|---|---|
| √ | 95.9 | ||||
| √ | 95.7 | ||||
| √ | 95.9 | ||||
| √ | 96.0 | ||||
| √ | 96.8 |
| Model ID | GIoU | SE | C3Ghost | Parameters | FLOPs | Precision (%) | Recall (%) | mAP (%) |
|---|---|---|---|---|---|---|---|---|
| 1 | — | — | — | 3,006,233 | 8.1 × 109 | 93.4 | 92.1 | 94.2 |
| 2 | √ | — | — | 3,006,233 | 8.1 × 109 | 93.8 | 93.4 | 94.8 |
| 3 | — | √ | — | 3,016,985 | 8.1 × 109 | 94.2 | 94.0 | 95.1 |
| 4 | — | — | √ | 1,714,661 | 5.0 × 109 | 93.2 | 92.8 | 94.0 |
| 5 | √ | √ | — | 3,016,985 | 8.1 × 109 | 94.8 | 94.5 | 95.6 |
| 6 | √ | — | √ | 1,714,661 | 5.0 × 109 | 94.0 | 94.2 | 95.2 |
| 7 | — | √ | √ | 1,715,173 | 5.0 × 109 | 95.1 | 95.4 | 96.0 |
| 8 | √ | √ | √ | 1,715,173 | 5.0 × 109 | 96.4 | 96.1 | 96.8 |
| Model ID | mAP (%) | Parameters | FLOPs | |
|---|---|---|---|---|
| YOLOv5n | 93.7 | 70.92 | 7.00 × 106 | 15.8 × 109 |
| YOLOv8n | 94.2 | 117.90 | 3.00 × 106 | 8.10 × 109 |
| YOLOv9n | 95.8 | 32.15 | 2.62 × 106 | 10.7 × 109 |
| YOLOv10n | 95.2 | 86.21 | 2.70 × 106 | 8.20 × 109 |
| YOLO11n | 95.5 | 84.03 | 2.60 × 106 | 6.30 × 109 |
| YOLOv12n | 96.1 | 120.53 | 2.51 × 106 | 5.80 × 109 |
| Improved YOLOv8n | 96.8 | 126.20 | 1.70 × 106 | 5.00 × 109 |
| Model ID | Stand | Lie | Eat | Fight | Tail-Bite |
|---|---|---|---|---|---|
| YOLOv5n | 88.5 | 95.1 | 91.2 | 89.4 | 90.5 |
| YOLOv8n | 89.2 | 96.4. | 93.5 | 90.8 | 92.1 |
| YOLOv9n | 90.5 | 96.8 | 94.4 | 91.5 | 91.8 |
| YOLOv10n | 87.8 | 96.5 | 93.2 | 90.0 | 91.4 |
| YOLO11n | 90.1 | 97.0 | 92.1 | 91.2 | 90.8 |
| YOLOv12n | 91.5 | 97.2 | 94.0 | 92.5 | 94.2 |
| Improved YOLOv8n | 91.0 | 98.5 | 95.2 | 92.5 | 96.8 |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2026 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license.
Share and Cite
Guo, J.; Xu, Y.; Lin, L.; Zhang, B.; Zhou, P.; Luo, S.; Zhuo, Y.; Ji, J.; Luo, Z.; Cheng, G. An Enhanced YOLOv8n-Based Approach for Pig Behavior Recognition. Computers 2026, 15, 230. https://doi.org/10.3390/computers15040230
Guo J, Xu Y, Lin L, Zhang B, Zhou P, Luo S, Zhuo Y, Ji J, Luo Z, Cheng G. An Enhanced YOLOv8n-Based Approach for Pig Behavior Recognition. Computers. 2026; 15(4):230. https://doi.org/10.3390/computers15040230
Chicago/Turabian StyleGuo, Jianjun, Yudian Xu, Lijun Lin, Beibei Zhang, Piao Zhou, Shangwen Luo, Yuhan Zhuo, Jingyu Ji, Zhijie Luo, and Guangming Cheng. 2026. "An Enhanced YOLOv8n-Based Approach for Pig Behavior Recognition" Computers 15, no. 4: 230. https://doi.org/10.3390/computers15040230
APA StyleGuo, J., Xu, Y., Lin, L., Zhang, B., Zhou, P., Luo, S., Zhuo, Y., Ji, J., Luo, Z., & Cheng, G. (2026). An Enhanced YOLOv8n-Based Approach for Pig Behavior Recognition. Computers, 15(4), 230. https://doi.org/10.3390/computers15040230





