# A Lightweight Forest Test Image Recognition Model Based on Improved YOLOv8
## 3 Methods
### Improved Loss Function  
In the task of detecting small objects in forestry pest images, where the proportion of small objects is relatively high, the rational design of loss functions can significantly enhance the detection performance of the model. The loss function in YOLOv8 consists of multiple components, including the classification loss (VFL Loss) and the regression loss in the form of CIOU Loss + DFL. The formula for the VFL Loss function is given by Equation (3) [30]. \[\mathrm{VFL}(\mathrm{p},\mathrm{q})=\left\{ \begin{array}{ll}-\mathrm{q}( \mathrm{q}\mathrm{log}(\mathrm{p})+(1-\mathrm{q})\log(1-\mathrm{p}))&\mathrm{q} >0\\ -\mathrm{\alpha}\mathrm{p}^{\gamma}\log(1-\mathrm{p})&\mathrm{q}=0\end{array} \right. \tag{3}\]  
In the aforementioned formula, \"q\" represents the Intersection over Union (IoU) between the bounding box (predicted box) and the ground truth box. IoU is calculated by dividing the intersection of the predicted box and the ground truth box by the union of the two boxes. The variable \"p\" represents the score or probability. If the two boxes intersect (q > 0), it is considered a positive sample, and if there is no intersection, then q is set to 0, indicating a negative sample. Figure 5: CBAM (Convolutional Block Attention Model). The definition of CIoU, as given in Equation (4), incorporates an additional penalty term on top of DIoU. \[\mathcal{L}_{\text{CIoU}}=1-\text{IoU}+\frac{\rho^{2}\left(\text{b},\text{b}^{ \text{gt}}\right)}{\text{c}^{2}}+\alpha\upsilon \tag{4}\]  
\[\alpha=\frac{\upsilon}{\left(1-\text{IoU}\right)+\upsilon} \tag{5}\]  
\[\upsilon=\frac{4}{\pi^{2}}\left(\arctan\frac{\text{w}^{\text{gt}}}{\text{h}^{ \text{gt}}}-\arctan\frac{\text{w}}{\text{h}}\right)^{2} \tag{6}\]  
Among these, \(\alpha\) serves as a weight function, defined in Equation (5). \(\upsilon\) gauges the similarity in the aspect ratio between the predicted box and the ground truth, as outlined in Equation (6). The terms \(\text{w}^{\text{gt}}/\text{h}^{\text{gt}}\) and \(\text{w}/\text{h}\) denote the aspect ratios of the ground truth box and the predicted box. b and \(\text{b}^{\text{gt}}\) represent the center points of the predicted box and the ground truth box, respectively. \(\rho\) denotes the Euclidean distance between the two rectangular boxes, and c signifies the diagonal distance of the closed region between these two rectangular boxes. DFL loss (distribution focal loss) is a loss function designed to address the issue of class imbalance. Similar to focal loss, DFL incorporates information about class distribution, providing improved handling of imbalanced class scenarios. The formula for DFL is expressed by Equation (7). \[\text{DFL}(\text{S}_{i},\text{S}_{i+1})=-((\text{y}_{i+1}-\text{y})\log(\text {S}_{i})+(\text{y}-\text{y}_{i})\log(\text{S}_{i+1})) \tag{7}\]  
However, CIoU has its drawbacks. Firstly, the computation of CIoU involves the calculation of inverse trigonometric functions, which increases the computational cost of the model, particularly in large-scale object detection tasks. Secondly, CIoU does not account for the balance of hard and easy samples. Thirdly, CIoU considers aspect ratio as a penalty term. When the aspect ratio of the actual box and the predicted box is the same, but the values of width and height are different, the penalty term fails to reflect the true difference between the two bounding boxes. Therefore, this paper introduces Wise-IoU (WIoU). In terms of computational speed, the additional computation cost of WIoU mainly lies in the calculation of the focusing coefficient and the mean statistics of IoU loss. Under the same experimental conditions, WIoU has a faster speed compared to CIoU because it does not involve aspect ratio calculations, with WIoU's computation time being 87.2% of CIoU. In terms of performance improvement, WIoU considers not only the area, centroid distance, and overlap area, but also introduces a dynamic non-monotonic focusing mechanism.