# Research on Tiny Target Detection Technology of Fabric Defects Based on Improved YOLO
## 3 Methods
### Optimized Loss Function  
The loss function used in the YOLOv4 network is the Complete Intersection over union (CIOU) loss function. On the basis of DIOU, CIOU considers the aspect ratio of the detection frame into the loss function, and increases the aspect ratio influence factor \(\alpha\
u\), the accuracy of the frame regression has been further improved.  
CIOU is represented by Equation (3).  
\[CIOU=IOU-\frac{\rho^{2}\Big{(}b_{pred},b_{gt}\Big{)}}{c^{2}}+\alpha\
u \tag{3}\]  
_IOU_ represents the intersection ratio between the ground truth box and the prediction box, \(\rho\) represents the Euclidean distance, \(b_{pred}\) and \(b_{gt}\) represent the center point of the ground truth box and the prediction box, \(c\) represents the diagonal distance of the smallest circumscribed rectangle, \(\
u\) is used to measure the consistency of the aspect ratio, and \(\alpha\) is used to balance the value of \(\
u\).  
Although CIOU takes into account the overlapping area, the center point distance and aspect ratio of the frame regression, the aspect ratio difference reflected by \(\
u\) in its formula is not the real difference between the width and height and its confidence.  
Therefore, sometimes it will hinder the model from effectively optimizing the similarity and slow down the convergence speed of the model, so this paper replaces the CIOU with the EIOU which is more sensitive to the aspect ratio of the detection frame and has a faster convergence speed.  
EIOU is represented by Equation (4), \(EIOU_{loss}\) is represented by Equation (5).  
\[EIOU=IOU+\frac{\rho^{2}(b,b^{gt})}{c^{2}}+\frac{\rho^{2}(w,w^{gt})}{C_{w}^{2} }+\frac{\rho^{2}(h,h^{gt})}{C_{h}^{2}} \tag{4}\]  
\[EIOU_{loss}=1-EIOU \tag{5}\]  
\(C_{w}\) represents the width of the minimum bounding box covering the predicted box and the ground truth box, \(C_{h}\) represents the height of the minimum bounding box covering the predicted box and the ground truth box, \(w\) represents the width of the predicted box, \(w^{gt}\) represents the width of the ground-truth box, \(h\) represents the height of the predicted box, and \(h^{gt}\) represents the height of the ground-truth box.  
When the value of EIOU is larger, it indicates that the overlap between the predicted box and the ground truth box is larger, the loss value is smaller, and the prediction result of the model is better.  
However, in the process of backpropagation, the momentum provided by \(EIOU_{loss}\) for network training is constant, and it can also be understood that the gradient of \(EIOU_{loss}\) is constant, which cannot give the network a larger momentum to speed up the training of the network when the predicted box and the ground truth box are too far apart. Therefore, this paper proposes a new loss function based on EIOU, namely \(CEIOU_{loss}\) (Curve \(EIOU_{loss}\)).  
\begin{table}
\begin{tabular}{c c} \hline \hline
**Feature Layer** & **Anchors** \\ \hline
104 \(\times\) 104 & (4,38) (5,22) (16,38) \\
26 \(\times\) 26 & (28,400) (72,119) (64,168) \\
13 \(\times\) 13 & (36,349) (122,30) (408,103) \\ \hline \hline \end{tabular}
\end{table}
Table 1: Anchors for each feature layer.  
\[CEIOU_{loss}\] is represented by Equation (6). \[CEIOU_{loss}=3\times\ln 3-3\times\ln(2+EIOU)\] (6) The comparison of \[CEIOU_{loss}\] and \[EIOU_{loss}\] is shown in Figure 6.  
It can be seen from the Figure 6 that \(EIOU_{loss}\) is a straight line, \(CEIOU_{loss}\) is a curve and both lines decrease with the increase in EIOU, but when the EIOU is small, that is, when the gap between the predicted box and the ground truth box is large, \(CEIOU_{loss}\) will provide a greater momentum to the training of the network, so that next time the network adjusts the prediction box more accurately, reducing the number of times that the predicted box and the ground truth box overlap, and Since \(EIOU_{loss}\) is a straight line, the momentum it provides is equal regardless of how the magnitude of EIOU changes.