# Yolo Based Defects Detection Algorithm for EL in PV Modules with Focal and Efficient IoU Loss
## 2 Materials and Methods
### Focal-EloU Loss  
The EloU loss function is designed to enhance the precision and effectiveness of bounding box regression in object detection tasks. This loss function optimizes target detection by focusing on three key aspects as follows:  
1. Center Point Distance Loss: This component aims to minimize the distance between the center points of the predicted bounding box and the ground truth bounding box. By doing so, it improves the accuracy of localizing the object within the bounding box;
2. Consideration of Size Differences: EloU penalizes discrepancies in width and height between the predicted bounding box and the ground truth bounding box. This penalty mechanism ensures that the predicted bounding box closely aligns with the shape and size of the actual object, thereby refining the detection accuracy;
3. Incorporation of Minimum Enclosing Box Size: By integrating the size of the minimum enclosing box that encompasses both the predicted and ground truth bounding boxes into the loss function, EloU becomes more attuned to the object's size and position. This inclusion enhances the sensitivity of the loss function to variations in object size and spatial orientation, contributing to more precise object localization. The EloU loss function is expressed as shown in Equation (8). \[ \begin{split} Loss_{EloU}=L_{IoU}+L_{dis}+L_{asp}\\ =1-IoU+\frac{\rho^{2}\left(b,b^{st}\right)}{c^{2}}+\frac{\rho^{2} \left(y,b^{st}\right)}{c^{2}_{w}}+\frac{\rho^{2}\left(b,b^{st}\right)}{c^{2}_ {h}}\end{split}  \tag{8}\]  
where \(b,w,\ h\) represent the center coordinates, width, and height of the predicted box while \(b^{st},\ w^{st},\ h^{st}\) correspond to the center coordinates, width, and height of the annotated box. \(\rho\left(b,b^{st}\right)\) quantifies the Euclidean spatial distance between the centers of the two boxes, and \(c,\ c_{w},\ c_{h}\) represent the diagonal length, width, and height of the minimum enclosing box for the two boxes, respectively. To address the issue of class imbalance in classification tasks, Focal Loss has been proposed as a specially designed loss function that effectively tackles this challenge. The Focal Loss function is expressed in Equation (9). \[Loss_{Focal}=-\alpha_{t}(1-p_{t})^{\gamma}\text{log}(p_{t}) \tag{9}\]  
where \(\alpha_{t}\) is used to adjust the ratio of positive and negative samples, \(\gamma\) is a parameter controlling the model's focus on easy and hard-to-classify samples, and \(p_{t}\) is the class probability. In the training phase of neural networks, a large number of predicted bounding boxes have low IoU ratios with the ground truth boxes, indicating poor sample quality. Learning from these low-quality samples can result in significant loss oscillation, negatively affecting the model training process. To address the issue of class imbalance in regression tasks and enhance the contribution of high-quality samples during neural network learning,an improved version of Focal Loss called Focal-L1 Loss is proposed [26]. The gradient of Focal-L1 Loss is expressed as in Equation (10). \[g(x)=\left\{ \begin{matrix}-x\alpha\text{ln}(\beta x),0<x\leq 1,\frac{1}{ \varepsilon}\leq\beta\leq 1\\ -\alpha\text{ln}(\beta),x>1,\frac{1}{\varepsilon}\leq\beta\leq 1\end{matrix} \right. \tag{10}\]  
where \(\alpha\) is used to constrain the gradient intensity, and \(\beta\) is used to control the curvature of the curve, ensuring that the gradient intensity falls within the range of (0,1). By taking the derivative of the gradient function, we obtain the loss function expression of Focal-L1 Loss, which can be denoted as in Equation (11). \[L_{f}(x)=\left\{ \begin{matrix}-\frac{ax^{2}(2\text{ln}(\beta x)-1)}{4},0<x\leq 1 ;\frac{1}{\varepsilon}\leq\beta\leq 1\\ -\alpha\text{ln}(\beta)x+C,x>1;\frac{1}{\varepsilon}\leq\beta\leq 1\end{matrix} \right. \tag{11}\]  
where \(C\) is a constant to satisfy \(C=\frac{2\alpha\text{ln}\beta+\alpha}{4}\) in order to maintain the continuity of the loss function.