Abstract
Weed adaptability to environmental conditions poses a major challenge in agricultural production, often leading to yield reduction or even complete crop loss. Effective weed detection requires algorithms tailored to the unique visual and biological characteristics of each crop. However, despite the agricultural importance of garlic, no studies have developed optimized state-of-the-art deep learning models for weed identification in garlic fields. Addressing this gap, the present study focuses on optimizing two prominent object detection frameworks—YOLOv5 and YOLOv8—for accurate weed detection under real field conditions. A dataset of 600 RGB images containing garlic plants and five weed species was collected directly from the field. Different versions of YOLOv5 and YOLOv8 were optimized and evaluated based on mAP@0.5 and inference speed. Experimental results showed that YOLOv8n achieved superior performance, yielding an mAP@0.5 of 87.0% with an average inference time of 16 ms, compared to YOLOv5m with an mAP@0.5 of 85.0% and 50 ms. The findings highlight that YOLOv8n, with an input resolution of 320 × 320 pixels and optimized using the SGD optimizer, provides the best trade-off between detection accuracy and computational efficiency. These results demonstrate the potential of YOLOv8n for real-time weed detection and its application in precision agriculture machinery.
Similar content being viewed by others
Funding
This research did not receive any specific grant from funding agencies in the public, commercial, or not-for-profit sectors.
Author information
Authors and Affiliations
Corresponding author
Ethics declarations
Competing interests
The authors declare no competing interests.
Additional information
Publisher’s note
Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
Rights and permissions
Open Access This article is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License, which permits any non-commercial use, sharing, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons licence, and indicate if you modified the licensed material. You do not have permission under this licence to share adapted material derived from this article or parts of it. The images or other third party material in this article are included in the article’s Creative Commons licence, unless indicated otherwise in a credit line to the material. If material is not included in the article’s Creative Commons licence and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder. To view a copy of this licence, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
About this article
Cite this article
Shamohammadi, S., Bagherpour, H. & Bakhtiari, M.R. Architecture and hyperparameter optimization of YOLOv5 and YOLOv8 for weed detection in garlic fields. Sci Rep (2026). https://doi.org/10.1038/s41598-026-51662-0
Received:
Accepted:
Published:
DOI: https://doi.org/10.1038/s41598-026-51662-0


