# A Strawberry Ripeness Detection Method Based on Improved YOLOv8
## 2 Materials and Methods
### Network Model Architecture for Strauberry Ripeness Detection
#### 2.3.3 Wise-IoU Loss Function  
Traditional loss functions typically consider only the IoU (Intersection over Union) between the predicted and ground-truth bounding boxes while neglecting classification-related information. As a result, detection accuracy can be further improved by designing more comprehensive loss functions. YOLOv8 adopts DFLoss (distribution focal loss) and CIoU (Complete IoU) Loss to compute the bounding box regression loss. However, CIoU employs a monotonic focusing mechanism and does not account for the balance between hard and easy samples. Consequently, when the training set contains a significant number of low-quality or noisy samples, the overall detection performance may degrade. The formulation of CIoU is provided in the following equation [28]:  
\[L_{CLoU}=1-IoU+\frac{\rho^{2}(b^{A},b^{B})}{c^{2}}+\alpha\upsilon \tag{7}\]  
where \(\rho^{2}(b^{A},b^{B})\) is the Euclidean distance between the centers of the predicted box \(b^{A}\) and ground-truth box \(b^{B}\), \(c\) is the diagonal length of their minimum enclosing box, \(\upsilon\) measures aspect ratio consistency, and \(\alpha\) is a positive trade-off parameter.  
This study introduces the Wise-IoU loss function, which incorporates a dynamic non-monotonic focusing mechanism to better balance sample contributions during training. Instead of relying solely on geometric factors such as distance and aspect ratio, Wise-IoU employs an outlier-aware evaluation strategy to assess the quality of predicted boxes. This approach prevents the model from being overly penalized by poorly aligned predictions, thereby enhancing the robustness of the loss function. The mathematical formulation of Wise-IoU is presented in Equations (8)-(10) [29,30].  
\[L_{WIoU}=rR_{WIoU}L_{IoU},r=\frac{\beta}{\delta\alpha^{\beta-\alpha}} \tag{8}\]  
\[\beta=\frac{IoU_{*}^{L}}{\overline{L_{IoU}}}\in[0,+\infty) \tag{9}\]  
\[R_{WIoU}=exp(\frac{(x-x_{gt})^{2}+(y-y_{gt})^{2}}{(w_{2}^{2}+h_{2}^{*})^{*}}) \tag{10}\]  
Here, \(r\) and \(\delta\) are positive hyperparameters controlling the base focusing strength and its scale, while Equation (9) compares the current IoU loss to a reference so that harder samples (\(\beta>1\)) receive more focus and easier ones (\(\beta<1\)) are down-weighted. The exponential term \(R_{WIoU}\) (Equation (10)) further boosts well-aligned predictions by measuring the squared center-point offset normalized by the enclosing box dimensions (\(c_{w}\), \(c_{h}\)).  
The formulas represent the IoU-based loss calculation. When the overlap between the anchor box and the predicted box is high, the loss function reduces the penalty term for high-quality anchor boxes and increases the emphasis on the distance between their center points. The second formula defines the penalty term specific to Wise-IoU, which enhances the loss contribution of anchor boxes with ordinary quality. In the equations, the superscript * indicates exclusion from backpropagation, effectively preventing the model from generating non-convergent gradients. The normalization factor represents an incremental sliding average value. The outlier degree, also defined in the formulas, quantifies the deviation of a sample. A smaller value indicates a high-quality anchor box and is therefore assigned a smaller gradient gain. Conversely, predicted boxes with larger outlier degrees are also assigned smaller gradient gains, which suppresses the influence of noisy or low-quality samples. This mechanism allows the loss function to concentrate more effectively on anchor boxes of ordinary quality, thereby improving the overall robustness and detection performance of the network.