# Traffic Sign Detection Based on the Improved YOLOv5
## 2 Improved YOLOv5 Algorithm
### Loss Function Optimization  
The network's capacity for prediction will be impacted by the loss function selected. The purpose of the loss function is to calculate the deviation between the predicted value and the real value, calculate the gradient through the loss function, and update the weight in reverse, so a good loss function can greatly optimize the performance of the network. Traditional loss functions depend on the aggregation of the bounding box regression metrics, for instance, the distance, overlapping area, and aspect ratio between the predicted bounding box and the ground truth bounding box. However, most of the currently put-forward and employed methods do not take into consideration the direction matching between the ground truth bounding box and the predicted bounding box. This deficiency gives rise to slow convergence. IoU loss-regresses the four bounding coordinate points of the bounding box as a whole, which is the ratio of the intersection and union between the predicted bounding box and the ground truth bounding box generated by the training process of the network using the merged set [20]. The IoU principle formula and IoU-loss formula are shown in Equations (5) and (6). \[\text{IoU}=\frac{|M\cap N|}{|M\cup N|} \tag{5}\]  
\[\text{IoU-loss}=1-\text{IOU}=1-\frac{|M\cap N|}{|M\cup N|} \tag{6}\]  
where \(M\) is the area of the predicted bounding box and \(N\) is the area of the ground truth bounding box. In this case, when the IoU-loss value is smaller, it means that the intersection area between two boxes is larger. In contrast, the degree of overlap is low. However, in an actual situation, we may encounter a state in which the predicted bounding box and the true bounding box do not overlap. When the value of IoU is 0 and the value of IoU-loss is 1, the loss function loses its derivative property. The distance between two boxes cannot be calculated, so it cannot continue to learn. Although the size of IoU is the same and can be calculated, the position of the prediction frame is different, which affects the recognition accuracy, and the IoU-loss cannot function properly, as shown in Figure 4 [21]. Figure 4: IoU-loss failure scenario. The relevant loss functions, such as GIoU, DIoU, and CIoU, do not take into consideration the direction mismatch between two boxes, which leads to slow and inefficient convergence of the model. Compared with DIoU-loss, CIoU-loss takes into consideration the aspect ratio in the regression elements but still does not take into consideration the vector angle between two expectations. Therefore, in this article, we choose the SIoU [22] loss function, which takes into consideration the angle of the vector between the two expected regressions and defines a new penalty indicator to improve the speed of training and the accuracy of inference. By introducing the direction into the loss function, the SIoU loss function converges at a faster rate in the training phase with better inference performance compared to existing methods such as CIoU. The proposed improved method improves the model detection accuracy and converges at a faster rate. The SIoU loss function consists of four cost functions: IoU cost, distance cost, angle cost and shape cost. The formula is shown in Formula (7). \[\text{SIoU-loss}=1-\text{IoU}+\frac{\Delta+\Omega}{2} \tag{7}\]  
where IoU is the IoU cost, \(\Delta\) is the distance loss, and \(\Omega\) is the shape cost. The formula for the distance cost is shown in (8). \[\Delta=\sum_{t=x,y}\left(1-e^{-\gamma\rho_{t}}\right) \tag{8}\]  
The parameter definition for distance cost is shown in Formulas (9)-(11). \[\rho_{x}=\frac{b_{x_{y}}^{gt}-b_{cx_{y}}}{C_{w}} \tag{9}\]  
\[\rho_{y}=\frac{b_{xy}^{gt}-b_{cy}}{C_{h}} \tag{10}\]  
\[\gamma=2-\Lambda \tag{11}\]  
where \(C_{w}\) and \(C_{h}\) are the width and height of the smallest rectangle bounded by two boxes, \(\gamma\) is the distance value, and \(\Lambda\) is the angle cost.