# Research on an Insulator Defect Detection Method Based on Improved YOLOv5
## 3 Methodology  
### K-Means Algorithm for Re-Clustering Anchor Frames  
In general, in anchor-based target detection algorithms, most of the anchors are designed by hand. For example, in the classical algorithm models SSD and Faster-RCNN, nine anchors of different sizes and aspect ratios are designed, respectively. However, there is a disadvantage, in that the manually designed anchors are not guaranteed to be well-suited to different datasets. If the size of the anchor design and the size of the target in the dataset are relatively different, the detection effect of the model will be affected. For YOLOv2, [PERSON] suggests using k-means clustering instead of manual design, by clustering the bounding box of the training set and automatically generating a set of anchors that are more suitable for the dataset, which can make the network detect better. Since the default anchor box of the YOLOv5 algorithm saves the preset anchor box for the COCO dataset and insulator defect detection, the image size and detection target of the COCO dataset do not match with this dataset. We use the k-means clustering method to recalculate the anchor box that matches the labeled box of this dataset. The k-means clustering method is performed mainly by calculating the distance (similarity) between samples to cluster the closer samples into the same class (cluster) [26]. The k-means algorithm's primary process is as follows:  
1. Initialize K cluster centers (assume K = 2). 2. Randomly select k samples among all samples as the initial centers of clusters, as shown in Figure 2a, where the two black solid dots represent the two cluster centers randomly initialized. 3. Calculate the distance of each sample from the center of each cluster (Euclidean distance), and then divide the sample into the clusters closest to it. Different colors are used to distinguish different clusters, as in Figure 2b. 4. Update the cluster centers and calculate the mean value of all samples in each cluster as the new cluster centers. As shown in Figure 2c, the two blue solid points have moved to the centers of the corresponding clusters. 5. Repeat Step 3 to Step 4 until the cluster centers are not changing or the cluster centers are changing very little to satisfy the given termination condition. The final clustering results are shown in Figure 2d. Figure 2: K-means clustering. (**a**) Randomly initialize two cluster centers; (**b**) Calculate the Euclidean distance between the sample and the cluster center. (**c**) Update the cluster centers. (**d**) Final clustering result. For the k-means algorithm to re-cluster the anchor box, usually, the bounding box is represented by the top-left vertex and the bottom-right vertex, i.e., (x1, y1, x2, y2). When doing clustering on the box, we only need the width and height of the box as features and also need to perform normalization on the width and height of the box using the width and height of the image first, i.e., \(w=\frac{w_{box}}{w_{img}},h=\frac{h_{box}}{h_{img}}\); if we directly use the Euclidean distance as a metric in the standard k-means algorithm, then the large box clusters will generate more error than the small box clusters in the clustering results. Since we only care about the intersection over union (IOU) of the anchor and box and do not care about the box size, it is more appropriate to use the IOU as a metric, as shown in Figure 3.  
Suppose we have anchor \(=(w_{a},h_{b})\), \(box=(w_{b},h_{b})\), then we have (1):  
\[IOU(box,anchor)=\frac{intersection(box,anchor)}{union(box,anchor)- intersection(box,anchor)} \tag{1}\]  
We do not care about the box's position when calculating the IOU here. We assume all boxes' upper left vertices are at the origin. Obviously, the value of the IOU is between 0 and 1. If two boxes are more similar, their IOU values are more significant. The more similar the two boxes are, the closer they should be, so the final metric is shown in (2).