# Insulators and Defect Detection Based on the Improved Focal Loss Function
## 2 Materials and Methods
### Dynamic Weight Calculation  
In the Focal loss function, since there is no standard value protocol for the weight factors \(\alpha\) and \(\gamma\), the values need to be selected according to the sample distribution of the dataset. The empirical values of 0.25 for \(\alpha\) and 2 for \(\gamma\) were commonly used in the results presented in the literature [15]. However, empirical methods do not necessarily apply to all datasets and need to be dynamically adjusted according to the dataset itself and the variation in the distribution of positive and negative samples and difficult samples during training. By adjusting the alpha and gamma values, we can obtain a better detection network, but the disadvantage is that the alpha and gamma values require us to continuously  
Figure 2: Focal loss decay curve. (**a**) Authentic labels \(y=0\). (**b**) Authentic labels \(y=1\). adjust the training process, which inevitably interrupts the network training process, thus prolonging the network training time. Therefore, in the paper, the two hyperparameters \(\alpha\) and \(\gamma\) are designed as parameters that can be dynamically adjusted during the network training process. \(\alpha\) is defined as Equation (4). \[\alpha=(\sum_{i=1}^{3}\frac{P_{i}}{P_{i}+N_{i}})/3 \tag{4}\]  
where \(P_{i}\) is the number of positive samples in each feature map. \(N_{i}\) is the number of negative samples in each feature map. At the output end of the YOLOv5 network, three feature diagrams of different sizes are generated after each iteration and then matched with a predesigned nine prior bounding box. The feature map is the feature of different scales extracted from the image after the image input to the network is convolutional. These three feature maps of different scales are used to predict the target in the image and generate the corresponding information of the target frame. Every three prior bounding boxes are matched with one feature map, and when the aspect ratio of the prior bounding box to the real box was lower than the threshold it was a positive sample and higher than the threshold it was a negative sample. Therefore, the number of positive and negative samples on three different feature diagrams is not the same. By calculating the number of positive and negative samples on each feature map, the proportion of positive samples in the overall sample can be calculated. In the algorithm, the ratio of positive samples to population samples on each feature map is first calculated. Second, the scales obtained on the three feature maps are summed. Finally, the result of the addition is averaged as the value of \(\alpha\). The ratio of positive to negative samples varies according to the characteristic diagrams generated during each iteration, and \(\alpha\) varies according to the ratio of positive to negative samples. Positive and negative samples can be given different weights by changing \(\alpha\). Positive samples are given a larger weight, and negative samples are given a smaller weight. Additionally, \(\gamma\) is defined as Equation (5). \[\gamma=e^{\left|true-pred\right|} \tag{5}\]  
where \(true\) is the true value of each predicted object, 0 or 1. \(pred\) is the prediction confidence. \(\gamma\) is mainly used for the adjustment of difficult and easy samples. The division of difficult and easy samples mainly depends on the difference between the predicted value and the actual value. In addition, the difference between the predicted value and the true value is between 0 and 1. Therefore, the design of \(\gamma\) in this paper is obtained by amplifying the difference between the predicted value and the actual value by the formula of the exponent \(e\). Through the exponential formula of \(e\), the value range of \(\gamma\) can be limited between 1 and 2.7. Compared with the fixed 2.0 value, it has a more flexible value space. During each iteration, the network makes probabilistic predictions for each prediction box, calculating \(\gamma\) by the difference between each prediction and the true value.