# Study on Lightweight Model of Maize Seeding Object Detection Based on YOLOv7
## 2 Materials and Methods
### Lw-Yolov7
#### 2.3.3 Improvements to the Loss Function  
The accuracy and convergence speed of object detection model depends on the loss function largely. The loss function of YOLOv7 contains three parts: coordinate loss, confidence loss, and classification loss. YOLOv7 adopts the calculation method of CIoU loss in the coordinate loss, and its expression is shown in Equation (3). \[L_{CIoU}=1-IoU+\frac{\rho^{2}(b,bs^{t})}{c^{2}}+av \tag{3}\]  
In Equation (3), \(\rho^{2}(b,bs^{t})\) is the Euclidean distance between the center points of the predicted box and ground truth. \(c\) represents the diagonal distance of the minimum circumscribed matrix. IoU is defined as the ratio of the intersection area between the predicted box and the ground truth to the union area of the two bounding boxes. \(a\) is a weight parameter, and v indicates the similarity between length and width. Equation (4) and Equation (5) display the expressions of \(a\) and v, respectively. \[a=\frac{v}{(1-IoU)+v} \tag{4}\]  
\[v=\frac{4}{\pi}(\arctan\frac{w^{gt}}{h^{gt}}-\arctan\frac{w}{h})^{2} \tag{5}\]  
In Equation (5), wgt and hgt represent the width and height of the ground truth, and \(w\) and \(h\) represent the width and height of the predicted box. CIoU is a newer evaluation metric that improves upon the IoU and its variants. The CIoU takes into account not only the extent of overlap between the predicted box and ground truth but also their distance apart and aspect ratio differences. However, it does not take into account the direction between ground truth and predicted box results in slow model convergence, low efficiency, low performance, etc. Therefore, we introduce the SIoU loss function [43] to optimize the coordinate loss. Compared to CIoU, the advantages of SIoU include robustness to partial occlusion, better handling of objects with different scales, and faster convergence speed during the training process. The calculation method of the SIoU loss function mainly includes four parts: angle cost, distance cost, shape cost, and IoU cost, and its expression, shown in Equation (6) is as follows:  
\[L_{SIoU}=1-IoU+\frac{\Delta+\Omega}{2} \tag{6}\]  
In Equation (6), \(\Delta\) is the distance cost, and \(\Omega\) is the shape cost. Since the angle cost is added to the SIoU function, it will reduce the probability of a penalty term equal to 0. This not only accelerates the convergence of the loss function, but it also reduces the prediction errors. The parameters and their relationships for the SIoU loss function are shown in Figure 9.  
B represents the predicted box and B\({}_{\rm gt}\) represents the ground truth. C\({}_{\rm x}\) is the width of the minimum bounding matrix. C\({}_{\rm y}\) is the height of the minimum bounding matrix. C\({}_{\rm w}\) and C\({}_{\rm h}\) are the width difference and height difference between the center point of the predicted box and the ground truth, respectively. \(\sigma\) is the shortest linear distance between the predicted box and the ground truth. Sin \(\alpha\) is the ratio of C\({}_{\rm h}\) to \(\sigma\), and sin \(\beta\) is the ratio of C\({}_{\rm w}\) to \(\sigma\). If \(\alpha\) is equal to 0 or \(\pi/2\), the angle cost (\(\Lambda\)) will be 0. If \(\alpha\) is less than \(\pi/4\), we prioritize the minimization of \(\alpha\). Otherwise, we prioritize the minimization of \(\beta\). The expression for the angle cost is given by Equation (7):  
\[\Lambda=1-2\sin^{2}(\arcsin(x)-\frac{\pi}{4}) \tag{7}\]  
in Equation (7),  
\[x=\frac{C_{h}}{\sigma}=\sin(\alpha) \tag{8}\]  
\[\sigma=\sqrt{\left(b_{c_{x}}^{gt}-b_{c_{x}}\right)^{2}+\left(b_{c_{y}}^{gt}-b_ {c_{y}}\right)^{2}} \tag{9}\]  
\[C_{h}=\max(b_{c_{y}}^{gt},b_{c_{y}})-\min(b_{c_{y}}^{gt},b_{c_{y}}) \tag{10}\]  
The distance cost in the SloU loss function is determined by the distance between the center points of the ground truth and the predicted box, which is further affected by the angle cost.