# An Improved Target Network Model for Rail Surface Defect Detection
## 2 Methodology
### The EloU Loss Function  
YOLOv7's loss function has three components: localization, object confidence, and classification loss. The choice of localization loss function is the complete intersection over union (CIOU).  
\[L_{CIOU}=1-\text{CIOU}=1-\text{IOU}+\frac{\rho^{2}(b,b^{\text{gt}})}{c^{2}}+\alpha \tag{5}\]  
\[v=\frac{4}{\pi^{2}}\bigg{(}\arctan\frac{w^{\text{gt}}}{h^{\text{gt}}}-\arctan \frac{w}{h}\bigg{)}^{2} \tag{6}\]  
\[\alpha=\frac{v}{(1-\text{IOU})+v} \tag{7}\]  
\[IOU=\frac{A\cap B}{A\cup B} \tag{8}\]  
where \(b\) and \(b^{\text{gt}}\) denote the centers of the predicted box and the ground truth box, respectively, \(\rho\) represents the Euclidean distance between the two centers of the sphere, \(c\) denotes the diagonal length of the smallest rectangle that encloses both the predicted box and the ground truth box, \(\alpha\) is the weight function, \(v\) is the factor to measure the aspect ratio, \(w\) and \(w^{\text{gt}}\) are the widths of the predicted box and the ground truth box, and \(h\) and \(h^{\text{gt}}\) are the heights of the predicted box and the ground truth box.  
However, CIOU only considers the relative proportion of width and height without taking into account the actual differences in width and height with their confidence levels. Therefore, when the width and height meet certain conditions, the penalty term of the loss function will fail, which is not conducive to model optimization. This paper introduces an effective IOU (EIOU) loss function to replace the CIOU. The target bounding box position and feature information are taken into account by the EIOU loss function. By separating the aspect ratio loss term from the CIOU and directly using the predicted height as a penalty  
Figure 4: The algorithm’s network structure.  
term, the process accelerates convergence and improves regression accuracy. The loss function consists of three parts: overlap area loss \(L_{IOU}\), center point distance loss \(L_{dis}\), and width-height loss \(L_{asp}\), following Equations (9) and (10).  
\[L_{EIOU}=L_{IOU}+L_{dis}+L_{asp}\] \[=1-IOU+\frac{\rho^{2}\left(b,b^{\text{gt}}\right)}{c^{2}}+\frac{ \rho^{2}\left(w,w^{\text{gt}}\right)}{c_{w}^{2}}+\frac{\rho^{2}\left(h,h^{ \text{gt}}\right)}{c_{h}^{2}} \tag{9}\]  
\[L_{\text{EIOU}}=L_{\text{IOU}}+L_{\text{dis}}+L_{\text{asp}}=1-\text{IOU}+ \frac{\rho^{2}\left(b,b^{\text{gt}}\right)}{c^{2}}+\frac{\rho^{2}\left(w,w^{ \text{gt}}\right)}{c_{w}^{2}}+\frac{\rho^{2}\left(h,h^{\text{gt}}\right)}{c_{ h}^{2}} \tag{10}\]  
where \(c_{w}\) and \(c_{h}\) are the widths and heights of the smallest rectangle, which contains both the prediction and the groundtruth boxes.  
The conventional IOU loss function can encounter issues when dealing with small objects because the IOU values for small objects are usually low. The EIOU loss function can better handle small objects by incorporating center point and aspect ratio information, improving small object detection performance. Additionally, the EIOU loss function is more sensitive to changes in the position and shape of the target box, making it adaptable to targets of different scales and shapes. This enhances the robustness of the model, enabling it to effectively detect targets in various scenarios.