# Intelligent Detection of Underwater Defects in Concrete Dams Based on YOLOv8s-UEC
## 2 Methodology
### Adopted Loss Function  
The loss function of YOLOv8s consists of two parts: classification loss (\(Loss_{cla}\)) and regression loss (\(Loss_{bx}\)), which can be expressed as:  
\[Loss=Loss_{cla}+Loss_{bx} \tag{1}\]  
where the classification loss uses Binary Cross Entropy (BCE), the role of this loss is to determine whether the detection target is classified as such, and based on this output, network confidence results.  
The regression loss is caused by the difference in coordinates and dimensions between the predictor box and the object box, and the degree of regression can be measured by the ratio of the predictor box to the object box in regression-type tasks. Intersection over Union (IoU) is a way to describe the degree of overlap between the predictor box and the object box, which can be expressed as:  
\[\text{IOU}=\frac{|\text{A}\bigcap\text{B}|}{|\text{A}\bigcup\text{B}|} \tag{2}\]  
where A denotes the prediction box; B denotes the object box. IoU measures the degree of overlap of the two boxes by calculating the ratio of the intersection and concatenation of the areas of the two boxes A and B. Since this method cannot reflect the coordinate relationship between the two boxes, YOLOv8s adopts complete-IoU (CIoU) as the regression loss function. CIoU improves the regression accuracy by simultaneously considering three factors: overlap area, centroid distance, and aspect ratio of the box. The calculation of CIoU is shown in Figure 8, and it can be expressed as follows:  
\[\text{L}_{\text{CIoU}}=1-\text{IoU}+\frac{\rho^{2}\left(\text{b},\text{b}^{ \text{gt}}\right)}{c^{2}}+\alpha v \tag{3}\]  
\[c^{2}=(w^{c})^{2}+(h^{c})^{2} \tag{4}\]  
\[\alpha=\frac{v}{(1-\text{IoU})+v^{2}} \tag{5}\]  
\[v=\frac{4}{\pi^{2}}\bigg{(}\arctan\frac{w^{gt}}{h^{gt}}-\arctan\frac{w}{h} \bigg{)} \tag{6}\]  
Figure 8: Principle of loss function calculation.  
Since CIoU optimizes the relative aspect ratio of the box width and height, and based on the definition of parameter \(v\) in Equation (6), the gradient of \(v\) with respect to width \(w\) and height \(h\) has opposite signs. When the predicted box's width and height maintain a proportional relationship with the target box's dimensions, the penalty factor for optimizing the relative aspect ratio in CIoU becomes ineffective, significantly affecting the convergence speed. To address this problem, EIoU [51] proposes a loss function that directly penalizes the actual dimensions of the predicted box widths and heights, and the EIoU calculation is shown in Figure 8, which can be expressed as follows:  
\[\mathrm{L_{EIoU}}=1-\mathrm{IoU}+\frac{\rho^{2}\big{(}\mathrm{b},\mathrm{b} ^{\mathrm{gt}}\big{)}}{c^{2}}+\frac{\rho^{2}(\mathrm{w},\mathrm{w}^{\mathrm{gt }})}{(w^{\mathrm{e}})^{2}}+\frac{\rho^{2}\big{(}\mathrm{h},\mathrm{h}^{\mathrm{ gt}}\big{)}}{(h^{\mathrm{e}})^{2}} \tag{7}\]  
Introducing the Focal L1 loss to address the category imbalance between high- and low-quality samples in the regression task results in Focal-EIoU, denoted as:  
\[\mathrm{L_{Focal-EIoU}}=\mathrm{IoU}^{\gamma}\mathrm{L_{EIoU}} \tag{8}\]  
where \(\gamma\) is used to control the curve radian, after the original text and many tests to take 0.5.  
Since YOLOv8s uses Anchor-Free prediction, Distribution Focal Loss (DFL) is added to the regression loss to match this prediction method, and the final loss function of YOLOv8s can be expressed as the sum of the three losses:  
\[\mathrm{Loss}=\mathrm{Loss_{cla-BCE}}+\mathrm{Loss_{box-FocalEIoU}}+\mathrm{ Loss_{box-DFL}} \tag{9}\]