# Insulators and Defect Detection Based on the Improved Focal Loss Function
## 2 Materials and Methods  
### Introduction to Algorithm and Network Structure  
The YOLOv5 [23] model was presented in 2020 and is at a relatively high level of speed and accuracy of detection. The model can choose different network depths and feature diagram widths according to the requirement of accuracy and speed. The official code of YOLOv5 provides five versions: YOLOv5n, YOLOv5s, YOLOv5m, YOLOv5l, and YOLOv5x. In this study, due to the small number of insulator datasets and the high requirement for recognition speed, YOLOv5s is selected as the basic training model in this paper. Figure 1 shows the specific structure of the YOLOv5 network model.  
The YOLOv5 network is mainly divided into four parts, namely input, backbone network, neck network, and output. At first, the adaptive anchor frame is calculated by K-means clustering to improve the accuracy of locating small target defect of broken insulator. Backbone is mainly used to extract image features, mainly using Focus and Cross Stage Partial (CSP) Network structures compared to previous generations. The Focus structure slices through the incoming images, reducing the computation. The CSPstructure [24] is used to accelerate reasoning and speed up training while ensuring accuracy. FPN [25] and PANet [26] multi-scale feature fusion are used in the Feature Fusion Module to output detection results at different feature levels, not only improve the detection rate and the localization and recognition accuracy of each scale target, but also combine CSP structure fusion features to optimize feature fusion speed. Finally, the target box is regressed by CIOU loss function [27], and the binary cross-entropy loss function is used for classification and confidence regression. The detection output section eliminates redundant target boxes by non-maximum suppression (NMS) [28], predicts image features, generates boundary boxes, and predicts categories.  
The loss function of YOLOV5 consists of three parts: classified loss, rectangular loss, and confidence loss, which are weighted together to form the ultimate loss function. As shown in Equation (1).  
\[Loss=a\cdot loss_{obj}+b\cdot loss_{rect}+c\cdot loss_{clc} \tag{1}\]  
Figure 1: YOLOv5 network model structure diagram.  
Among them \(a,b,c\) is the weight of each of the three loss functions, usually, confidence loss gets the maximum weight, rectangular loss and classified loss are the next most weight.  
The classification loss and confidence loss functions used by YOLOv5 are two-class cross-entropy loss, and the formula is shown in Equation (2). Where \(N\) is the total number of categories, \(y_{i}\) is the true value of the current category and \(p(y_{i})\) is the predicted probability of the current category.  
Classified loss function is mainly used to predict the category of each lattice in three classification boxes. The confidence loss function is mainly used to predict the confidence of each prediction box, that is, the reliability of the prediction box. The greater the confidence value, the more reliable the prediction box is, that is, the closer to the true minimum box of the target.  
\[Loss_{clc,obj}=-\frac{1}{N}\sum_{i=1}^{N}y_{i}\cdot\log(p(y_{i}))+(1-y_{i}) \cdot\log(1-p(y_{i})) \tag{2}\]  
Binary cross-entropy is used to judge the difference between the predicted result of a classification model and the true value. If the predicted value \(p(y_{i})\) is closer to 1, then the value of the loss function should be closer to 0, that is, the smaller the difference between the predicted result and the true value, the smaller the value of the loss function. Conversely, if the predicted \(p(y_{i})\) is closer to 0 at this point, that is, the greater the difference between the predicted result and the true value, the greater the value of the loss function.