## 3 Materials and Methods
### Improved YOLOv8 Model
#### 3.4.3 Loss Function  
In tomato ripeness detection tasks, accurate localization and classification are key to model performance, especially when dealing with tomato images with different ripeness levels. To improve the performance of the model in such tasks, we have improved the loss function of the original YOLOv8 model. The CIOU loss function originally used in the model, as a commonly used target image regression loss, is able to take into account geometric relationships between target images, including factors such as position, size, and relative angle, during the training process. However, the performance of CIOU is not always ideal when dealing with targets with highly imbalanced category distributions and hard-to-detect targets, especially in cases where objects have large scale differences or are occluded. To address this problem, we introduce the Focal-EIOU Loss, which combines the idea of Focal Loss and the optimization of EIOU and is able to further improve the accuracy of small and hard-to-detect objects. The Focal-EIOU Loss firstly mitigates the effect of the background region by adding an adjustment factor to reduce the dominance of the easy-to-detect objects on the loss, which allows the model to pay more attention to the harder-to-detect and smaller tomato targets. At the same time, the EIOU loss incorporates more geometric information into the calculation process, not only considering the degree of overlap between the target frames but also enhancing the shape and orientation of the target frames and other factors, which makes the regression accuracy more refined, especially when dealing with tomatoes with different morphologies, and can effectively improve the fit of the frames. With this improvement, Focal-EIOU is able to guide the model to focus more on the actual location and shape of the tomatoes during the optimization process, rather than being distracted by a large number of background and easily identifiable regions. In object detection, the target in the image is predicted by the regression of the bounding box. A common metric for assessing the degree of overlap between the predicted bounding box and the true bounding box is the Intersection over Union (IOU). It measures the degree of overlap by calculating the ratio of the area of the intersection region of the predicted and real boxes to the area of their concatenated region. It can be calculated by Formula (1), where \(A,B\subseteq S\in R^{n}\) represent any two boxes. \[IOU=\left|A\cap B\right|/\left|A\cap B\right| \tag{1}\]  
The Focal-EIOU loss function consists of two parts: one is Focal loss, and the other is EIOU loss. The formula for Focal loss is  
\[FL(p_{t})=-\alpha_{t}(1-p_{t})^{\gamma}\log(p_{t}) \tag{2}\]  
where \(p_{t}\) is the model's predicted probability for the target category. The \(\gamma\) is an adjustment factor that is mainly used to adjust the weights between easy and hard-to- categorize samples. It can usually be adjusted by observing the performance of the model during training. Typically, the value of \(\gamma\) is set between [0,5]. \(\alpha_{t}\) is the weight of the category, usually used to adjust for category imbalances. The EIOU loss function consists of three parts. They are IOU loss(\(L_{IOU}\)), distance loss(\(L_{dis}\)), and aspect loss(\(L_{asp}\)). The EIOU loss function is defined as Formula (3):  
\[\begin{split}& L_{EIOU}=L_{IOU}+L_{dis}+L_{asp}\\ &=1-IOU+\frac{\rho^{2}\left(b,b^{st}\right)}{\left(w^{c}\right)^{ 2}+\left(h^{c}\right)^{2}}+\frac{\rho^{2}(w,w^{st})}{\left(w^{c}\right)^{2}}+ \frac{\rho^{2}(h,h^{st})  
the width and height of the smallest box covering the anchor box and the real box. The Focal-EIOU loss function is expressed as Equation (4):  
\[L_{Focal^{\prime}EIOU}=\alpha_{t}(1-IOU)^{\gamma}L_{EIOU} \tag{4}\]  
where IOU is Formula (1). The \(\gamma\) is an adjustment factor that is mainly used to adjust the weights between easy and hard-to-categorize samples. It can usually be adjusted by observing the performance of the model during training.