# A Study on Tomato Disease and Pest Detection Method
## 4 Methods
### Bounded Box Regression Loss Function Improvement  
The loss function of YOLOv5 is composed of three sub-loss functions, namely the confidence loss function, the bounding box regression loss function, and the classification loss function, which are calculated as follows:  
\[\begin{split} L_{t5}=\sum_{i}^{N}(\lambda_{1}L_{cls}+\lambda_{2} L_{obj}+\lambda_{3}L_{box})\\ =\sum_{i}^{N}\left(\lambda_{1}\underset{j}{\sum}L_{cls_{j}}+ \lambda_{2}\underset{j}{\sum}L_{CloU_{j}}+\lambda_{3}\underset{j}{\sum}L_{ obj_{j}}\right)\end{split} \tag{1}\]  
Figure 7: CARAFE module structure.  
\(L_{box}\) is the bounding box regression loss, \(L_{obj}\) is the object confidence loss, \(L_{cls}\) is the object classification loss, \(\lambda_{1}\), \(\lambda_{2}\) and \(\lambda_{3}\) are the weights of the three losses, where the object classification loss and the object confidence loss are calculated using the Binary Cross Entropy (BCE) loss function, and the calculation formulas are as follows:  
\[L_{BCE}=-\frac{1}{n}{\sum\limits_{i}^{n}}[y_{i}\times\log\left(\sigma(x_{i}) \right)+\left(1-y_{i}\right)\times\log(1-\sigma(x_{i}))] \tag{2}\]  
\[\sigma(a)=\frac{1}{1+\exp(-a)} \tag{3}\]  
The bounding box regression loss in version 5.0 and later uses the CIoU loss as the bounding box regression loss function. The formula for calculating the CIoU loss is as follows:  
\[L_{CIoU}=1-IoU+\frac{\rho^{2}\left(b,b^{gt}\right)}{c^{2}}+\alpha v \tag{4}\]  
\[IoU=\frac{\left|b\cap b^{gt}\right|}{\left|b\cup b^{gt}\right|} \tag{5}\]  
\[v=\frac{4}{\pi^{2}}\left(arctan\frac{w^{gt}}{h^{gt}}-arctan\frac{w}{h} \right)^{2} \tag{6}\]  
\[\alpha=\frac{v}{\left(1-IoU\right)+v} \tag{7}\]  
Compared with the Generalized-IoU (GIoU) loss [35] used in previous versions of YOLOv5, the CIoU loss takes into account additional penalties for the distance between the centers of the predicted and ground truth boxes, the difference in their areas, and incomplete overlap between the two boxes. Specifically, the distance between the centers of the predicted and ground truth boxes and the difference in their areas are taken into account before the IoU is calculated. This ensures that for the same IoU, the predicted box with a smaller distance and a smaller area will score higher. The CIoU loss provides more significant penalties for incomplete overlap between the boxes, resulting in a more accurate regression of the predicted box to the shape and size of the ground truth box. However, when the aspect ratio of the predicted box and the ground truth box is linearly related, the aspect ratio penalty term becomes ineffective, which affects the regression of the predicted box. Therefore, in this paper, we propose to replace the CIoU loss with the Wise-IoU (WIoU) loss. The WIoU loss focuses more on the aspect ratio, position offset, and scale changes of the bounding box than the traditional IoU loss and other improved versions such as GIoU and CIoU. In addition, the WIoU loss has an important parameter, the outlier weight, which adjusts the degree of penalty for different IoU values in the bounding box matching process. Specifically, the larger the value of the outlier weight, the stronger the penalty for low IoU matching results and the weaker the penalty for high IoU matching results. This allows the bounding box regression to focus on the anchor boxes of normal quality, preventing low-quality examples from producing large harmful gradients and making the model more accurate in matching the shape and size of the ground truth box, thereby improving the accuracy of object detection.