Next Article in Journal
System-Level Integration and Evaluation of an APS-SoC-Based Electrical Resistance Tomography Measurement System
Previous Article in Journal
High-Speed, UV-NIR Dual-Band Photodetection via a 2H-MoSe2/Si/1T-WS2 Bipolar Heterojunction
Previous Article in Special Issue
Energy-Efficient and Economy-Sustainable Technology for Online Seed Viability Detection Using Hyper Spectrum
 
 
Font Type:
Arial Georgia Verdana
Font Size:
Aa Aa Aa
Line Spacing:
Column Width:
Background:
Article

TMAF-YOLO: A Lightweight Model for In Situ Detection of Tomato Maturity and Defective Fruits in Greenhouses

College of Engineering, China Agricultural University, Beijing 100083, China
*
Author to whom correspondence should be addressed.
Sensors 2026, 26(15), 4950; https://doi.org/10.3390/s26154950
Submission received: 6 July 2026 / Revised: 26 July 2026 / Accepted: 31 July 2026 / Published: 5 August 2026

Abstract

Accurate tomato maturity detection is essential for harvesting decisions, quality grading, and postharvest handling in greenhouse production. However, leaf occlusion, fruit overlap, complex backgrounds, illumination variation, and subtle color differences between adjacent maturity stages limit real-time detection performance. To improve accuracy and deployment efficiency, this study proposes TMAF-YOLO, a lightweight detection model based on YOLOv8n. In this model, LGhostConv replaces selected convolutional structures to reduce redundant computation. A tomato maturity-aware aggregation fusion module, termed TMAF, is introduced to enhance color, texture, and local structural feature representation in the detection branches. MA-CB Focal Loss is used to improve learning under class imbalance and hard-sample conditions. Experimental results showed that TMAF-YOLO achieved Precision, Recall, mAP50, and mAP50-95 values of 0.889, 0.873, 0.954, and 0.776, respectively. The model contained 2.647 M parameters and required 7.3 GFLOPs, with an inference speed of 209.030 FPS. Compared with YOLOv8n and heavier detection models, TMAF-YOLO achieved higher detection accuracy with fewer parameters and lower computational cost. It also outperformed YOLOv11n and YOLOv12n in detection accuracy while maintaining real-time inference performance. These results indicate that the proposed model is suitable for real-time greenhouse tomato maturity detection and can support automated harvesting and grading.

1. Introduction

Tomato is an important vegetable crop in protected agriculture. Its maturity directly affects harvesting time, transport management, postharvest ripening, quality grading, and commercial value. In greenhouse environments, tomato fruits are often affected by leaf occlusion, fruit overlap, uneven illumination, and scale variation. Manual maturity assessment is inefficient and easily influenced by subjective experience. Therefore, a rapid, stable, and non-contact detection method is needed to support automated harvesting, grading management, and robotic visual perception in protected agriculture [1,2,3].
Among deep learning detection methods, region-based convolutional neural network (R-CNN) models and their variants were among the earlier methods applied to fruit and vegetable detection and counting tasks. An improved Faster R-CNN model, MatDet, was proposed for multi-object tomato maturity detection in complex scenarios [1]. The model used a 50-layer residual network (ResNet-50), region-of-interest alignment (RoIAlign), and a path aggregation network (PANet) to improve detection under occlusion, overlap, and illumination variation, achieving a mean average precision (mAP) of 96.14%. Faster R-CNN has also been used for tomato-related detection tasks. Highly occluded immature tomatoes on plants were detected using Faster R-CNN, with an average precision of 87.83% [4]. Greenhouse tomato flowers were detected and counted using Faster R-CNN, achieving an average precision of 96.02% [5]. These methods have strong localization ability and can provide stable target region information. However, their model structures are relatively complex, and their inference speed and lightweight deployment ability remain limited.
With the development of Transformer architectures, end-to-end detectors such as Detection Transformer (DETR), DETR with Improved deNoising anchOr boxes (DINO), and Real-Time Detection Transformer (RT-DETR) have been introduced into object detection tasks. DETR formulates object detection as a set prediction problem [6]. DINO improves the convergence and detection performance of DETR-like models through denoising training and query optimization [7]. RT-DETR further improves the Transformer detection structure for real-time detection requirements [8]. In fruit and vegetable maturity recognition, PDSI-RTDETR was proposed for tomato maturity detection. Compared with the original RT-DETR, its mAP50 and mAP50-95 increased by 3.9 and 4.1 percentage points, respectively [9]. An improved RT-DETR was also applied to fruit ripeness detection, indicating the potential of Transformer detectors in global feature modeling [10]. However, these models usually have higher training costs, larger parameter sizes, and greater hardware requirements.
In contrast, You Only Look Once (YOLO) series models provide a better balance between detection accuracy and inference speed. They have become common methods for fruit and vegetable detection and maturity recognition. A lightweight improved YOLOv5 model was proposed for real-time tomato detection and mobile deployment [2]. Compared with the original YOLOv5s, the number of parameters and floating-point operations (FLOPs) were reduced by 78% and 84.15%, respectively, while maintaining high detection accuracy. RGB-D information was combined with YOLOv5 to detect and count greenhouse tomato clusters [3]. MTD-YOLO was proposed for multitask detection of cherry tomato fruits, clusters, and maturity [11]. The model achieved an overall detection score of 86.6% and an average inference time of 4.9 ms. An improved YOLOX model was proposed for tomato ripeness and stem recognition, achieving an mAP of 92.17% and improving recognition under sample imbalance and scale variation [12]. YOLOv8 was combined with a lightweight Swin Transformer for strawberry ripeness detection [13]. Compared with YOLOv5s, CenterNet, and the Single Shot MultiBox Detector (SSD), the mAP50 increased by 1.6%, 33.5%, and 3.4%, respectively. These results indicate that improved YOLO models are suitable for fine-grained fruit maturity recognition. In addition, recent YOLO-based studies on tomato detection and ripening recognition have improved detection performance using attention mechanisms, lightweight structures, multiscale feature fusion, and postprocessing optimization [14,15,16,17].
Existing studies have verified the effectiveness of deep learning methods in fruit and vegetable detection and maturity recognition. However, further improvement is still needed for in situ greenhouse tomato detection. First, the color transition between semi-mature and mature tomatoes is continuous, making fine-grained classification difficult. Second, leaf occlusion, fruit overlap, and illumination variation weaken maturity-related feature representation. Third, defective samples are relatively limited, and conventional classification losses may underemphasize minority and hard samples. Previous studies have shown that Focal Loss and Class-Balanced Loss can adjust the loss weights of hard and imbalanced samples, thereby improving the learning ability for long-tailed classes and difficult samples [18,19]. To address these issues, this study proposes TMAF-YOLO based on YOLOv8n.
The main contributions of this study are as follows.
(1)
A lightweight tomato maturity detection model was constructed for complex greenhouse scenarios. Inspired by the low-cost feature generation strategy in GhostNet, an LGhostConv module was designed and embedded in key positions of the Backbone and Neck [20]. This design reduces the number of parameters and computational cost while maintaining detection performance.
(2)
A tomato maturity-aware aggregation fusion module, termed TMAF, was proposed. This module was designed to address continuous color transitions between adjacent maturity stages, weak local texture differences, and insufficient feature representation caused by occlusion and overlap. By integrating local texture enhancement and channel-spatial recalibration, TMAF strengthens maturity-related feature representation.
(3)
MA-CB Focal Loss was developed to improve learning under class imbalance and hard-sample conditions. By combining class-balanced weights with the Focal Loss modulation term, the loss enhances learning for minority and difficult samples, including defective fruits and adjacent maturity stages.

2. Materials and Methods

2.1. Overall Method Design

This study focuses on in situ tomato maturity detection before greenhouse harvesting. An overall application workflow was constructed, including greenhouse image acquisition, fruit detection and category recognition, harvesting decision-making, and postharvest grading management. As shown in Figure 1, tomato images were first acquired under natural growth conditions in the greenhouse. Then, the developed detection model was used to identify fruit locations, maturity stages, and defective fruits. Finally, the detection results were used to support harvesting time determination, defective fruit screening, and postharvest grading and quality management. It should be noted that this study mainly focuses on greenhouse tomato maturity and defective fruit detection. The harvesting, grading, and management processes in the figure are used to illustrate the potential application scenarios of the proposed model.

2.2. Dataset Construction and Annotation

The self-collected images used in this study were acquired in a tomato greenhouse at the Tianjin Academy of Agricultural Sciences, China, from 10 to 17 January 2026. Image acquisition was conducted between 9:00 a.m. and 12:00 p.m. using a HUAWEI Mate 60 smartphone (Huawei Device Co., Ltd., Dongguan, China) in automatic shooting mode. Exposure, focus, ISO sensitivity, and shutter speed were adjusted automatically by the built-in camera system. The shooting distance ranged from approximately 0.5 to 2.0 m, and the image resolution was 1280 × 760 pixels. Images were captured under naturally varying greenhouse illumination; illuminance was not measured instrumentally.
To represent practical greenhouse conditions, images were acquired from different viewing angles and distances. The collected scenes included fruits at different maturity stages and scales, together with leaf and branch occlusion, fruit overlap, dense fruit distribution, small targets, complex backgrounds, and local image blur. Representative examples of self-collected images are shown in Figure 2.
A total of 596 valid images were collected from the greenhouse. Because defective fruits were relatively scarce, 33 additional defective-fruit images were selected from the Tomato Fruit Diseases Dataset available on Kaggle (https://www.kaggle.com/datasets/profnourasemary/tomato-dataset, accessed on 1 July 2026). The additional images mainly consisted of tomato fruits exhibiting visible disease symptoms, together with a smaller number showing physical damage, decay, cracking, or other abnormal surface characteristics. The selected public images were annotated according to the same criteria as the self-collected images.
The 596 self-collected images were divided at the original-image level into training, validation, and test subsets containing 470, 63, and 63 images, respectively. The 33 public images were added exclusively to the training set to increase the number and diversity of defective-fruit samples. Consequently, the final training, validation, and test sets contained 503, 63, and 63 images, respectively. The validation and test sets therefore consisted entirely of self-collected greenhouse images.
The annotated tomato instances were divided into four categories: immature, semi-mature, mature, and defective. The first three categories represent successive normal maturity stages based primarily on fruit color and visible appearance. The defective category includes fruits with visible disease spots, physical damage, decay, cracking, or other abnormal surface characteristics and was treated as an independent appearance category rather than as a normal maturity stage.
All tomato instances were manually annotated with bounding boxes using LabelMe v6.0.0. The original annotations were stored in JSON format and subsequently converted into the format required by the YOLO detection framework. Partially occluded or locally blurred fruits were retained when their categories and principal visible boundaries could be reliably determined. Instances were excluded only when severe occlusion, insufficient visible information, or ambiguous appearance prevented consistent annotation. All annotations were reviewed according to uniform class definitions to ensure labeling consistency.
The final dataset contained 629 images and 2505 annotated tomato instances, including 1170 immature, 788 semi-mature, 304 mature, and 243 defective instances. Data augmentation was performed after dataset partitioning and was applied only to the training set. The validation and test sets were not augmented. The training set was used for model parameter optimization, the validation set for training monitoring and model selection, and the test set exclusively for final performance evaluation. The image and instance distributions before data augmentation are summarized in Table 1.

2.3. Construction of the TMAF-YOLO Model

2.3.1. TMAF-YOLO: An Improved YOLOv8n Model

The proposed TMAF-YOLO uses YOLOv8n as the baseline model. It is improved from three aspects: lightweight feature extraction, maturity-related feature fusion, and classification loss optimization. The overall architecture is shown in Figure 3. After the original image is input into the network, multiscale features are first extracted by the Backbone, in which selected convolutional structures are replaced by LGhostConv. Then, features at different scales are fused in the Neck, and TMAF modules are added to the P3/8 and P4/16 branches. Finally, the model outputs prediction results through three detection heads at P3/8, P4/16, and P5/32 scales. During training, the model is optimized using a joint loss function composed of WIoU, DFL, and MA-CB Focal Loss. The specific designs of LGhostConv, TMAF, and MA-CB Focal Loss are introduced in the following sections.

2.3.2. Lightweight Feature Extraction Module: LGhostConv

To reduce redundant computation during feature extraction and multiscale feature fusion, a lightweight Ghost convolution module, termed LGhostConv, was introduced into selected downsampling layers of the backbone and neck. Similar to GhostConv, LGhostConv generates output features through primary and complementary feature branches. However, it replaces the dense spatial convolution in the primary branch with a depthwise-separable operation and employs directional depthwise convolutions in the complementary branch.
Given an input feature map X with C i n channels, LGhostConv first applies a k × k depthwise convolution with stride s for spatial feature extraction and downsampling, followed by a 1 × 1 pointwise convolution for channel interaction. This process generates approximately half of the required output channels as the primary features. The remaining channels are generated by dividing the primary features into two groups and applying 1 × 3 and 3 × 1 depthwise convolutions to capture horizontal and vertical information, respectively. The directional features are then fused with the primary features through a parameter-free residual connection and concatenated to form the final output.
Ignoring bias terms, batch-normalization parameters, and activation operations, the number of parameters in LGhostConv is expressed as follows:
P L G h o s t C o n v = k 2 C i n + C i n m + 3 n
where m = ⌈ C o u t /2⌉ and n = C o u t m denote the numbers of primary and complementary feature channels, respectively. In comparison, a standard k × k convolution requires k 2 C i n C o u t parameters. Therefore, LGhostConv reduces model complexity by replacing dense spatial channel interactions with depthwise spatial filtering, pointwise channel projection, and low-cost directional feature generation. Its effects on detection accuracy, model size, computational complexity, and inference speed are evaluated in Section 3.3.

2.3.3. Tomato Maturity-Aware Aggregation Fusion Module: TMAF

To strengthen the representation of maturity- and defect-related cues in the fused neck features, a tomato maturity-aware aggregation fusion module, termed TMAF, was designed. TMAF is embedded in the P3 and P4 feature fusion paths of the YOLOv8n neck, where relatively high-resolution features are retained for fine-grained maturity and defect detection. The original P5 path is preserved to limit the additional computational cost. As illustrated in Figure 4, TMAF comprises a lightweight texture aggregation branch and a TMCAttention submodule.
Let the input feature be X , with a size of X R B × C × H × W , where B , C , H , and W denote the batch size, number of channels, feature map height, and feature map width, respectively. The input feature first passes through a 1 × 1 convolution for channel mapping. It is then split along the channel dimension into a retained branch and a texture branch:
X 1 , X 2 = S p l i t ( C o n v 1 × 1 ( X ) )
where X 1 is the retained branch, which maintains the direct transmission of input information. X 2 is the texture branch, which passes through three LTMBlocks in sequence to obtain texture-enhanced features at different levels, denoted as T 1 , T 2 , and T 3 . Each LTMBlock consists of a 1 × 1 convolution, a 3 × 3 depthwise convolution, a 5 × 5 depthwise convolution, and a 1 × 1 convolution. The depthwise convolutions are used to supplement local texture and edge details. The 1 × 1 convolutions are used for channel adjustment and feature fusion.
Then, the retained branch X 1 , and the texture branch outputs T 1 , T 2 , and T 3 are concatenated along the channel dimension. A 1 × 1 convolution is then used to complete feature fusion and obtain the fused feature F :
= C o n v 1 × 1 ( C o n c a t ( X 1 , T 1 , T 2 , T 3 ) )
To further adjust maturity-related feature responses, TMCAttention is applied after feature fusion. This structure contains three branches: channel attention, spatial attention, and residual connection. Channel attention is used to adjust the importance of different feature channels. It helps the model focus on color, texture, and defect responses related to maturity discrimination. Spatial attention is used to highlight fruit region responses and reduce interference from irrelevant background features. The outputs of the two attention branches are denoted as F c and F s , respectively.
Finally, the outputs of the channel attention branch and the spatial attention branch are fused and added to the residual branch. The output feature of the TMAF module is obtained as follows:
Y = F + C o n v 1 × 1 ( F c + F s )
where Y denotes the output feature of the TMAF module. Through this design, TMAF introduces local texture enhancement, multibranch feature aggregation, and channel-spatial attention recalibration while preserving the original fused features. This improves the feature representation ability of the P3 and P4 branches for key categories, including semi-mature, mature, and defective fruits.

2.3.4. Loss Function Optimization: MA-CB Focal Loss

To alleviate class imbalance among tomato categories and reduce the dominance of easily classified samples, the original classification loss of YOLOv8n was replaced with maturity-aware class-balanced focal loss, termed MA-CB Focal Loss. This modification was applied only to the classification branch, whereas WIoU and DFL were retained for bounding-box regression. MA-CB Focal Loss combines effective-number-based class weighting with focal modulation to adjust the contributions of different categories and prediction elements during training.
Let n c denote the number of annotated instances belonging to class c in the training set. The class-balanced weight is defined as follows:
α c = 1 β 1 β n c
where β ∈ [0, 1) controls the rate at which the effective number of samples increases with class frequency. A value closer to 1 increases the sensitivity of the class-balanced weights to differences in class frequency. Preliminary experiments were conducted using β ∈ {0.99, 0.999, 0.9999}. Among these settings, β = 0.999 achieved the best overall detection performance and was therefore fixed throughout training.
The values of n c were calculated exclusively from the annotated instances in the training set before data augmentation; validation and test annotations were not used to construct the class weights.
For prediction element i and class c , let z i c and y i c denote the predicted logit and target score, respectively, and let p i c = σ( z i c ). The probability assigned to the target state is calculated as p t , i c = y i c c p i c + (1 − y i c )(1 − p i c ).
The MA-CB Focal Loss is then expressed as follows:
L M A - C B = 1 S i = 1 N c = 1 C α c ( 1 p t , i c ) γ l B C E ( z i c , y i c )
where N and C denote the number of prediction elements and tomato categories, respectively, and S is the sum of the target scores used for loss normalization. The term l B C E ( z i c , y i c ) denotes the binary cross-entropy loss computed directly from the prediction logit z i c and the target score y i c . The focusing parameter γ controls the attenuation of easily classified elements. As γ increases, elements with high p t , i c contribute less to the classification loss, thereby increasing the relative contribution of difficult elements. Preliminary experiments were conducted using γ∈{1.0, 1.5, 2.0}. Among these settings, γ = 1.5 achieved the best overall detection performance and was therefore fixed throughout training.
The overall loss function of TMAF-YOLO is defined as
L t o t a l = λ b o x L W I o U + λ c l s L M A - C B + λ d f l L D F L
where L W I o U , L M A - C B and L D F L denote the WIoU-based bounding-box regression loss, the modified classification loss, and the distribution focal loss, respectively. The coefficients λ b o x , λ c l s , and λ d f l are their corresponding loss gains.

2.4. Experimental Settings and Evaluation Metrics

2.4.1. Hardware and Software Environment

All experiments in this study were conducted under the same hardware and software environment. The experimental platform used the Windows 11 operating system. The processor was an Intel Core i9-14900HX (Intel Corporation, Santa Clara, CA, USA), and the graphics card was an NVIDIA GeForce RTX 4060 Laptop GPU (NVIDIA Corporation, Santa Clara, CA, USA) with approximately 8 GB of memory. Model training, validation, and inference testing were all performed on this GPU to ensure comparability of experimental results among different models.
The software environment was built with Python 3.10.20. The deep learning framework used PyTorch 2.10.0 and TorchVision 0.25.0, and GPU acceleration was based on CUDA 12.8. The training, validation, and inference of YOLO series models were mainly implemented using the Ultralytics 8.4.42 framework. Image processing and numerical computation were performed using OpenCV 4.13.0, NumPy 2.2.6, and Scikit-learn 1.7.2.

2.4.2. Training Hyperparameters

During model training, YOLOv8n was used as the baseline model. YOLOv8n, improved module combination models, and the final TMAF-YOLO model were compared under the same training conditions. All models used the same dataset division. The training, validation, and test sets were divided at a ratio of 8:1:1. The input image size was uniformly set to 640 × 640 to meet the requirements of batch training for YOLO models.
The number of training epochs was set to 150, and the batch size was set to 8. AdamW was used as the optimizer, with an initial learning rate of 0.01 and a weight decay coefficient of 0.0005. To improve training stability and support further convergence in the later training stage, a staged learning rate and weight decay adjustment strategy was used. The learning rate was reduced to 0.5 times its previous value every 30 epochs. The weight decay was reduced to 0.5 times its previous value every 50 epochs.
For the YOLO series models and the proposed improved models, the same training parameters were used. DETR, DINO, and R-CNN series models were initialized with public pretrained weights. For the R-CNN series models, a small number of shallow backbone parameters were frozen. All models were trained on the same dataset and hardware environment. Their final performance was evaluated on the independent test set.

2.4.3. Evaluation Metrics

The following metrics were used to evaluate network performance. The evaluation covered both detection accuracy and model efficiency. For detection accuracy, Precision and Recall were used to measure the ability to suppress false detections and control missed detections, respectively. mAP50 represents the mean average precision of all classes at an IoU threshold of 0.5. It was used to evaluate target recognition ability under common detection conditions. mAP50-95 represents the average detection precision when the IoU threshold ranges from 0.5 to 0.95 with a step size of 0.05. It reflects localization accuracy and detection stability under stricter conditions.
For model efficiency, Param represents the number of model parameters and is used to measure model size and storage cost. FLOPs represent the number of floating-point operations and reflect computational complexity. FPS represents the number of image frames processed by the model per second. Infer time/ms represents the average inference time for a single image. FPS and Infer time/ms were jointly used to evaluate inference efficiency and provide a reference for real-time tomato maturity detection in greenhouse environments.

3. Results and Discussion

3.1. Training Process and Final Detection Result Analysis

3.1.1. Training Loss Curve Analysis

To observe the training convergence of different models, the training loss curves were plotted, as shown in Figure 5. As the number of training epochs increased, the training losses of all models showed an overall decreasing trend. The curves gradually became stable in the later training stage. This indicates that all models could complete effective training on the dataset used in this study. It should be noted that different detection models have different network structures and loss function components. Therefore, the numerical scales of their training losses are not completely consistent. The curves are mainly used to analyze convergence trends and training stability, rather than to directly judge model performance.

3.1.2. Validation mAP50 Curve Analysis

To further analyze the performance changes in the models on the validation set, the validation mAP50 curves were plotted, as shown in Figure 6. The mAP50 values of all models increased rapidly in the early training stage and then gradually entered a stable stage. This indicates that the models gradually learned tomato localization and maturity classification features. In the later training stage, the curves remained generally stable, and no obvious performance degradation was observed. These results indicate that the training process in this study had good stability.

3.1.3. Precision–Recall Curve Analysis

To analyze the relationship between precision and recall for different categories in the final model, the PR curve of TMAF-YOLO was plotted. The precision–recall curve is shown in Figure 7. The curves of the four categories were mainly located in the upper region of the PR space. This distribution shows that the model maintained good precision under high recall conditions. The AP values of immature, semi-mature, mature, and defective were 0.948, 0.958, 0.972, and 0.940, respectively. The overall mAP50 was 0.954, which corresponds to the mAP50 value reported in the evaluation results.
Among the four categories, mature had the highest AP value. This may be because mature tomatoes have a more distinct color and appearance features. The AP value of defective was slightly lower, possibly because of its smaller sample size and larger appearance variation. Overall, the PR curve results were consistent with the previous quantitative evaluation results. This further confirms that TMAF-YOLO achieved good detection performance across different categories.

3.1.4. Confusion Matrix Analysis

To further observe misclassification among different categories, the normalized confusion matrices of YOLOv8n and TMAF-YOLO were compared, as shown in Table 2. For YOLOv8n, the correct recognition ratios for immature, semi-mature, mature, and defective were 0.87, 0.86, 0.79, and 0.89, respectively. For TMAF-YOLO, these values increased to 0.91, 0.91, 0.86, and 0.90, respectively.
The diagonal values of TMAF-YOLO improved for all four categories. The improvements for mature and semi-mature were more evident, indicating that the improved model enhanced the discrimination ability for adjacent maturity stages. The off-diagonal values show that a small amount of confusion still existed between semi-mature and mature. This is mainly related to the continuous tomato ripening process and unclear color and texture boundaries. The defective category also had a small number of misclassifications, which may be associated with large appearance variations and limited sample size.
Figure 7. Precision–recall curve of TMAF-YOLO.
Figure 7. Precision–recall curve of TMAF-YOLO.
Sensors 26 04950 g007

3.2. Model Performance and Visualization Analysis

To evaluate the effectiveness of the proposed TMAF-YOLO model, it was compared with mainstream object detection methods. Under the same dataset division and experimental environment, YOLOv8n, YOLOv11n, YOLOv12n, DETR, DINO, Faster R-CNN, Grid R-CNN, Libra R-CNN, Cascade R-CNN, and Dynamic R-CNN were selected for comparison. These models cover one-stage detectors, end-to-end detectors, and two-stage detectors. This selection enables a broad comparison of different detection paradigms for greenhouse tomato maturity detection. In this study, the best model weights during training were selected using the validation set. The final detection performance of each model was then evaluated on the independent test set. The comparison results are shown in Table 3.
Table 3 shows that TMAF-YOLO achieved strong overall performance. Compared with the baseline YOLOv8n, the Precision of TMAF-YOLO increased from 0.858 to 0.889. Recall increased from 0.836 to 0.873, mAP50 increased from 0.916 to 0.954, and mAP50-95 increased from 0.744 to 0.776. These improvements demonstrate the effectiveness of the proposed modifications for tomato maturity detection. Meanwhile, TMAF-YOLO had 2.647 M parameters and 7.3 GFLOPs. Its average inference time was 4.784 ms, and its FPS reached 209.030. Thus, the model improved detection accuracy while maintaining a lightweight structure and real-time inference performance.
Although YOLOv11n and YOLOv12n are more recent detectors, they did not consistently outperform YOLOv8n on the present dataset. YOLOv11n achieved slightly higher Recall and mAP50-95 than YOLOv8n but lower Precision and mAP50, whereas YOLOv12n showed lower overall detection accuracy under the current settings. This result does not indicate that the newer architectures are intrinsically inferior. Their advantages on general-purpose benchmarks may not be fully realized on this relatively small and task-specific dataset, which contains substantial scale variation, fruit occlusion and overlap, local blur, and subtle visual differences among the four tomato categories. In contrast, TMAF-YOLO was specifically designed to strengthen high-resolution color, texture, and local structural features related to tomato maturity and defects. In addition, the same training configuration was used for all YOLO models to ensure a controlled comparison, although this configuration may not be optimal for each architecture.
DETR and DINO have strong global modeling capabilities, but their parameter counts reached 41.556 M and 47.393 M, respectively, and their average inference times were 41.777 ms and 33.152 ms, respectively. Among the two-stage detectors, Dynamic R-CNN and Grid R-CNN achieved Recall values of 0.885 and 0.873, respectively, indicating good target recall ability. However, the higher model complexity and inference latency of these models make them less favorable for lightweight real-time greenhouse deployment.
To provide a qualitative comparison, representative test images were selected for visualization analysis, as shown in Figure 8 and Figure 9. Figure 8 includes four cases: leaf occlusion, fruit overlap, small targets, and defective fruits. The compared one-stage and end-to-end detectors identified most visible fruits, but missed detections, incomplete localization, or category confusion still occurred in some occluded, overlapping, and small-target regions. In comparison, TMAF-YOLO showed more complete target coverage and more stable category recognition in the selected examples. As shown in Figure 9, the two-stage detectors also localized most visible targets, although their predictions were still affected by occlusion, overlap, complex backgrounds, and small targets. Together with Table 3, these results indicate that TMAF-YOLO achieved a favorable balance between detection performance and inference efficiency.
To further examine the spatial responses of the YOLO-based models, heatmap visualization was performed, as shown in Figure 10. High-response regions indicate image areas receiving greater attention during detection. The compared models generally responded to tomato regions, although some activation remained distributed over leaves, stems, fruit edges, or background areas. TMAF-YOLO showed relatively concentrated responses on visible fruit regions in the leaf-occlusion, fruit-overlap, and defective-fruit examples, while the response in the small-target scene remained relatively broad because of the limited target scale and complex background. Overall, these results suggest that the proposed feature-fusion structure helps suppress irrelevant background responses and strengthen fruit-related feature representation.

3.3. Ablation Study

To verify the effectiveness of each improved module, ablation experiments were conducted using YOLOv8n as the baseline model. The experiments were performed under the same dataset, training strategy, and experimental environment. The experimental settings included the original YOLOv8n, YOLOv8n + LGhostConv, YOLOv8n + TMAF, YOLOv8n + LGhostConv + TMAF, and the final TMAF-YOLO. The evaluation results on the test set are shown in Table 4.
As shown in Table 4, after introducing LGhostConv, the number of parameters decreased from 3.012 M to 2.729 M. FLOPs decreased from 8.2 G to 7.7 G, and FPS increased to 226.283. These results indicate that LGhostConv can reduce redundant computation and improve inference speed. After introducing TMAF alone, mAP50 increased from 0.916 to 0.941, and mAP50-95 increased from 0.744 to 0.762. This indicates that maturity-related feature representation was enhanced.
After adding MA-CB Focal Loss, the final TMAF-YOLO achieved Precision, Recall, mAP50, and mAP50-95 values of 0.889, 0.873, 0.954, and 0.776, respectively. Since this loss only acts on the classification branch during training, the number of parameters and computational cost remained the same as those of YOLOv8n + LGhostConv + TMAF. Compared with the original YOLOv8n, the inference time per image decreased from 6.493 to 4.784 ms, corresponding to a 26.3% reduction in visual detection latency. This reduction can shorten the waiting time for fruit recognition in an automated harvesting workflow, although it does not represent an equivalent reduction in the complete robotic harvesting cycle.
To further examine the effects of the proposed modules, detection-result and heatmap visualizations were performed, as shown in Figure 11 and Figure 12. As shown in Figure 11, YOLOv8n still exhibited missed detections or unstable category predictions in some occluded, overlapping, and small-target regions. LGhostConv largely maintained the baseline detection performance. After TMAF was introduced, target coverage became more complete in some challenging regions. The final TMAF-YOLO generally produced more complete and stable detection results in the selected examples, particularly for small targets.
Figure 8. Detection results of one-stage and end-to-end detectors. Green, orange, red, and magenta bounding boxes represent the immature, semi-mature, mature, and defective categories, respectively.
Figure 8. Detection results of one-stage and end-to-end detectors. Green, orange, red, and magenta bounding boxes represent the immature, semi-mature, mature, and defective categories, respectively.
Sensors 26 04950 g008
Figure 9. Detection results of two-stage detectors. Green, orange, red, and magenta bounding boxes represent the immature, semi-mature, mature, and defective categories, respectively.
Figure 9. Detection results of two-stage detectors. Green, orange, red, and magenta bounding boxes represent the immature, semi-mature, mature, and defective categories, respectively.
Sensors 26 04950 g009
Figure 10. Heatmap visualization of YOLO-series models.
Figure 10. Heatmap visualization of YOLO-series models.
Sensors 26 04950 g010
Figure 11. Detection results of ablation models. Green, orange, red, and magenta bounding boxes represent the immature, semi-mature, mature, and defective categories, respectively.
Figure 11. Detection results of ablation models. Green, orange, red, and magenta bounding boxes represent the immature, semi-mature, mature, and defective categories, respectively.
Sensors 26 04950 g011
Figure 12. Heatmap visualization of ablation models.
Figure 12. Heatmap visualization of ablation models.
Sensors 26 04950 g012
As shown in Figure 12, the responses of YOLOv8n were partly distributed over leaves, branches, or background regions in some scenes, whereas the models containing TMAF showed more concentrated responses on visible fruit areas. Together with the quantitative results in Table 4, these visualizations suggest that LGhostConv mainly contributes to complexity reduction and TMAF strengthens fruit-related feature representation, while the additional gain from MA-CB Focal Loss is reflected primarily in the quantitative results.

3.4. Discussion

3.4.1. Methodological Significance and Practical Implications

The experimental results demonstrate that TMAF-YOLO achieves a favorable trade-off between detection accuracy and inference efficiency for greenhouse tomato maturity detection. The methodological contribution lies in the task-oriented integration of lightweight feature extraction, maturity-aware feature fusion, and classification loss optimization to jointly address fine-grained maturity discrimination and resource-efficient deployment. LGhostConv is introduced at selected downsampling positions to reduce redundant computation and model complexity while preserving useful feature information.
In addition, the TMAF module is placed before the detection heads to strengthen the representation of maturity-related features. Through local texture aggregation and channel-spatial recalibration, the model can better capture tomato color transitions, local surface textures, and occlusion boundaries. This is beneficial for distinguishing adjacent maturity stages, especially semi-mature and mature tomatoes with similar visual appearances. MA-CB Focal Loss further adjusts the contribution of different categories and hard samples during training. It reduces the dominance of easily classified samples and increases the learning attention given to confused maturity stages and the defective class.
Based on the harvesting rule defined in Section 2.1, the missed-detection rate for mature tomatoes, defined as the proportion of true mature fruits predicted as background, decreased from 5% to 2%. The potential mistaken-harvest rate for immature tomatoes, defined as the proportion of true immature fruits predicted as semi-mature or mature, decreased from 6% to 2%. Together with the 26.3% reduction in per-image inference time, these results indicate potential benefits in reducing missed detections of mature fruits, premature harvesting of immature fruits, and visual-processing delay.

3.4.2. Limitations and Potential Improvements

This study still has several limitations. First, the dataset was mainly collected from greenhouse environments. Although it included common challenging conditions, such as leaf occlusion, fruit overlap, complex backgrounds, and different maturity stages, the data scale and scene diversity were still limited. Under sharp illumination changes, severe occlusion, or very small target conditions, missed detections and unstable classification may still occur. In future work, images from different greenhouses, shooting distances, illumination conditions, cultivars, and growth periods should be further collected to improve model generalization.
Second, tomato maturity changes continuously during fruit development. The visual boundary between semi-mature and mature tomatoes is not always clear. When only single RGB images are used, the model may be affected by illumination variation, fruit surface reflection, and partial occlusion. Future studies could introduce color space analysis, multiscale texture features, or time-series image information to better describe continuous maturity changes.
Finally, the proposed method is based on single-view RGB images, which cannot directly provide fruit depth, spatial position, or occlusion hierarchy. This limits the model when fruits are highly overlapped or severely covered by leaves. Multiview images, depth cameras, or lightweight three-dimensional perception methods could be considered in future work to improve spatial understanding in complex greenhouse scenes. However, no complete robotic harvesting experiment was conducted. The reported 26.3% reduction in per-image inference time therefore applies only to visual detection and should not be interpreted as an equivalent reduction in total harvesting time. Future work should evaluate harvesting time per fruit, harvesting success rate, and operational stability in real greenhouse robotic trials.

4. Conclusions

This study developed TMAF-YOLO, a lightweight tomato maturity detection model based on YOLOv8n. LGhostConv was introduced to reduce parameters and computational cost, while the TMAF module was designed to enhance maturity-related feature representation before the detection heads. In addition, MA-CB Focal Loss was used to improve model learning under class imbalance and hard-sample conditions.
The experimental results showed that TMAF-YOLO achieved mAP50 and mAP50-95 values of 0.954 and 0.776, respectively, while maintaining a lightweight structure and real-time inference capability. The ablation experiments further confirmed the contribution of LGhostConv, TMAF, and MA-CB Focal Loss. Compared with YOLOv8n, the visual detection latency was reduced by 26.3%. The confusion-matrix-based analysis further indicated lower missed-detection and potential mistaken-harvest rates for mature and immature tomatoes, respectively. These results demonstrate the potential of TMAF-YOLO to support efficient tomato maturity recognition and harvesting decision-making in greenhouse environments.
Future work will expand the dataset and evaluate embedded deployment and robotic harvesting performance under more diverse greenhouse conditions.

Author Contributions

C.H.: Conceptualization, Investigation, Methodology, Visualization, Writing—original draft. L.H.: Conceptualization, Investigation, Visualization, Writing—original draft. W.H.: Visualization and Writing—original draft. X.Z.: Conceptualization, Writing—original draft, Funding acquisition, Supervision. All authors have read and agreed to the published version of the manuscript.

Funding

This research was funded by the National Natural Science Foundation of China (grant number: 32071917).

Institutional Review Board Statement

Not applicable.

Informed Consent Statement

Not applicable.

Data Availability Statement

Data will be made available on request.

Conflicts of Interest

The authors declare no conflicts of interest.

References

  1. Wang, Z.; Ling, Y.; Wang, X.; Meng, D.; Nie, L.; An, G.; Wang, X. An improved Faster R-CNN model for multi-object tomato maturity detection in complex scenarios. Ecol. Inform. 2022, 72, 101886. [Google Scholar] [CrossRef]
  2. Zeng, T.; Li, S.; Song, Q.; Zhong, F.; Wei, X. Lightweight tomato real-time detection method based on improved YOLO and mobile deployment. Comput. Electron. Agric. 2023, 205, 107625. [Google Scholar] [CrossRef]
  3. Rong, J.; Zhou, H.; Zhang, F.; Yuan, T.; Wang, P. Tomato cluster detection and counting using improved YOLOv5 based on RGB-D fusion. Comput. Electron. Agric. 2023, 207, 107741. [Google Scholar] [CrossRef]
  4. Mu, Y.; Chen, T.-S.; Ninomiya, S.; Guo, W. Intact detection of highly occluded immature tomatoes on plants using deep learning techniques. Sensors 2020, 20, 2984. [Google Scholar] [CrossRef] [PubMed]
  5. Rahim, U.F.; Mineno, H. Tomato flower detection and counting in greenhouses using faster region-based convolutional neural network. J. Image Graph. 2020, 8, 107–113. [Google Scholar] [CrossRef]
  6. Carion, N.; Massa, F.; Synnaeve, G.; Usunier, N.; Kirillov, A.; Zagoruyko, S. End-to-end object detection with transformers. In Computer Vision—ECCV 2020; Vedaldi, A., Bischof, H., Brox, T., Frahm, J.-M., Eds.; Springer International Publishing: Cham, Switzerland, 2020; pp. 213–229. [Google Scholar] [CrossRef]
  7. Zhang, H.; Li, F.; Liu, S.; Zhang, L.; Su, H.; Zhu, J.; Ni, L.M.; Shum, H.-Y. DINO: DETR with improved denoising anchor boxes for end-to-end object detection. In Proceedings of the International Conference on Learning Representations, Kigali, Rwanda, 1–5 May 2023. [Google Scholar]
  8. Zhao, Y.; Lv, W.; Xu, S.; Wei, J.; Wang, G.; Dang, Q.; Liu, Y.; Chen, J. DETRs beat YOLOs on real-time object detection. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition; IEEE: Piscataway, NJ, USA, 2024; pp. 16965–16974. [Google Scholar] [CrossRef]
  9. Wang, S.; Jiang, H.; Yang, J.; Ma, X.; Chen, J.; Li, Z.; Tang, X. Lightweight tomato ripeness detection algorithm based on the improved RT-DETR. Front. Plant Sci. 2024, 15, 1415297. [Google Scholar] [CrossRef] [PubMed]
  10. Wu, M.; Qiu, Y.; Wang, W.; Su, X.; Cao, Y.; Bai, Y. Improved RT-DETR and its application to fruit ripeness detection. Front. Plant Sci. 2025, 16, 1423682. [Google Scholar] [CrossRef] [PubMed]
  11. Chen, W.; Liu, M.; Zhao, C.; Li, X.; Wang, Y. MTD-YOLO: Multi-task deep convolutional neural network for cherry tomato fruit bunch maturity detection. Comput. Electron. Agric. 2024, 216, 108533. [Google Scholar] [CrossRef]
  12. Li, Y.; Li, J.; Luo, L.; Wang, L.; Zhi, Q. Tomato ripeness and stem recognition based on improved YOLOX. Sci. Rep. 2025, 15, 1924. [Google Scholar] [CrossRef] [PubMed]
  13. Yang, S.; Wang, W.; Gao, S.; Deng, Z. Strawberry ripeness detection based on YOLOv8 algorithm fused with LW-Swin Transformer. Comput. Electron. Agric. 2023, 215, 108360. [Google Scholar] [CrossRef]
  14. Appe, S.N.; Arulselvi, G.; Balaji, G.N. CAM-YOLO: Tomato detection and classification based on improved YOLOv5 using combining attention mechanism. PeerJ Comput. Sci. 2023, 9, e1463. [Google Scholar] [CrossRef]
  15. Sun, H.; Zheng, Q.; Yao, W.; Wang, J.; Liu, C.; Yu, H.; Chen, C. An improved YOLOv8 model for detecting four stages of tomato ripening and its application deployment in a greenhouse environment. Agriculture 2025, 15, 936. [Google Scholar] [CrossRef]
  16. Dong, Y.; Qiao, J.; Liu, N.; He, Y.; Li, S.; Hu, X.; Yu, C.; Zhang, C. GPC-YOLO: An improved lightweight YOLOv8n network for the detection of tomato maturity in unstructured natural environments. Sensors 2025, 25, 1502. [Google Scholar] [CrossRef] [PubMed]
  17. Touko Mbouembe, P.L.; Liu, G.; Park, S.; Kim, J.H. Accurate and fast detection of tomatoes based on improved YOLOv5s in natural environments. Front. Plant Sci. 2024, 14, 1292766. [Google Scholar] [CrossRef] [PubMed]
  18. Lin, T.-Y.; Goyal, P.; Girshick, R.; He, K.; Dollár, P. Focal loss for dense object detection. IEEE Trans. Pattern Anal. Mach. Intell. 2020, 42, 318–327. [Google Scholar] [CrossRef] [PubMed]
  19. Cui, Y.; Jia, M.; Lin, T.-Y.; Song, Y.; Belongie, S. Class-balanced loss based on effective number of samples. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition; IEEE: Piscataway, NJ, USA, 2019; pp. 9268–9277. [Google Scholar] [CrossRef]
  20. Han, K.; Wang, Y.; Tian, Q.; Guo, J.; Xu, C.; Xu, C. GhostNet: More features from cheap operations. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition; IEEE: Piscataway, NJ, USA, 2020; pp. 1580–1589. [Google Scholar] [CrossRef]
Figure 1. Overall workflow of greenhouse tomato maturity detection.
Figure 1. Overall workflow of greenhouse tomato maturity detection.
Sensors 26 04950 g001
Figure 2. Representative tomato images acquired under typical greenhouse conditions: (a) mature fruits; (b) adjacent fruits at different maturity stages; (c) fruit overlap and foliage interference; (d) leaf and branch occlusion; (e) small targets in a relatively long-range view; and (f) local image blur.
Figure 2. Representative tomato images acquired under typical greenhouse conditions: (a) mature fruits; (b) adjacent fruits at different maturity stages; (c) fruit overlap and foliage interference; (d) leaf and branch occlusion; (e) small targets in a relatively long-range view; and (f) local image blur.
Sensors 26 04950 g002
Figure 3. Overall architecture of TMAF-YOLO.
Figure 3. Overall architecture of TMAF-YOLO.
Sensors 26 04950 g003
Figure 4. Structure of the TMAF module.
Figure 4. Structure of the TMAF module.
Sensors 26 04950 g004
Figure 5. Training loss curves of different detection models. (a) One-stage and end-to-end detectors; (b) two-stage detectors.
Figure 5. Training loss curves of different detection models. (a) One-stage and end-to-end detectors; (b) two-stage detectors.
Sensors 26 04950 g005
Figure 6. Validation mAP50 curves of different detection models. (a) One-stage and end-to-end detectors; (b) two-stage detectors.
Figure 6. Validation mAP50 curves of different detection models. (a) One-stage and end-to-end detectors; (b) two-stage detectors.
Sensors 26 04950 g006
Table 1. Distribution of images and annotated tomato instances before data augmentation.
Table 1. Distribution of images and annotated tomato instances before data augmentation.
Dataset SubsetImagesImmatureSemi-MatureMatureDefectiveTotal Instances
training5039166282432061993
validation63136832919267
test63118773218245
total62911707883042432505
Table 2. Normalized confusion matrices of YOLOv8n and TMAF-YOLO.
Table 2. Normalized confusion matrices of YOLOv8n and TMAF-YOLO.
Predicted/TrueImmatureSemi-MatureMatureDefectiveBackground
(a) YOLOv8nimmature0.870.030.000.060.43
semi-mature0.060.860.140.000.23
mature0.000.050.790.020.12
defective0.000.010.020.890.20
background0.070.060.050.030.00
(b) TMAF-YOLOimmature0.910.030.000.030.47
semi-mature0.020.910.100.000.20
mature0.000.040.860.040.08
defective0.000.000.020.900.25
background0.070.020.020.030.00
Table 3. Performance comparison of different detection models.
Table 3. Performance comparison of different detection models.
ModelPrecisionRecallmAP50mAP50-95Params (M)FLOPs (G)FPSInference Time (ms)
YOLOv8n0.8580.8360.9160.7443.0128.2154.0126.493
YOLOv11n0.8180.8480.9090.7502.5916.5199.4815.013
YOLOv12n0.8380.8240.8970.7282.5696.5140.7637.107
DETR0.7870.8750.8720.60741.55677.123.93741.777
DINO0.8590.8340.8990.70447.393218.630.16433.152
Faster R-CNN0.8460.8710.8820.66141.36461.724.69640.493
Grid R-CNN0.8590.8730.8700.67364.700154.019.38851.577
Libra R-CNN0.8670.8280.8720.65441.27062.424.96040.065
Cascade R-CNN0.8560.8610.8850.69369.16189.518.87552.980
Dynamic R-CNN0.8220.8850.8620.64141.36461.724.53940.751
TMAF-YOLO0.8890.8730.9540.7762.6477.3209.0304.784
Table 4. Ablation results of different improved modules.
Table 4. Ablation results of different improved modules.
ModelPrecisionRecallmAP50mAP50-95Params (M)FLOPs (G)FPSInference Time (ms)
YOLOv8n0.8580.8360.9160.7443.0128.2154.0126.493
YOLOv8n + LGhostConv0.8680.8200.9200.7492.7297.7226.2834.419
YOLOv8n + TMAF0.8750.8550.9410.7622.9307.8194.1755.150
YOLOv8n + LGhostConv + TMAF0.8800.8610.9490.7682.6477.3207.3404.823
TMAF-YOLO0.8890.8730.9540.7762.6477.3209.0304.784
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.

Share and Cite

MDPI and ACS Style

Huang, C.; He, L.; Huang, W.; Zhang, X. TMAF-YOLO: A Lightweight Model for In Situ Detection of Tomato Maturity and Defective Fruits in Greenhouses. Sensors 2026, 26, 4950. https://doi.org/10.3390/s26154950

AMA Style

Huang C, He L, Huang W, Zhang X. TMAF-YOLO: A Lightweight Model for In Situ Detection of Tomato Maturity and Defective Fruits in Greenhouses. Sensors. 2026; 26(15):4950. https://doi.org/10.3390/s26154950

Chicago/Turabian Style

Huang, Chenxiao, Linran He, Wentao Huang, and Xiaoshuan Zhang. 2026. "TMAF-YOLO: A Lightweight Model for In Situ Detection of Tomato Maturity and Defective Fruits in Greenhouses" Sensors 26, no. 15: 4950. https://doi.org/10.3390/s26154950

APA Style

Huang, C., He, L., Huang, W., & Zhang, X. (2026). TMAF-YOLO: A Lightweight Model for In Situ Detection of Tomato Maturity and Defective Fruits in Greenhouses. Sensors, 26(15), 4950. https://doi.org/10.3390/s26154950

Note that from the first issue of 2016, this journal uses article numbers instead of page numbers. See further details here.

Article Metrics

Back to TopTop