# An Improved YOLOv5s-Based Helmet Recognition Method for Electric Bikes
## 4 Materials and Methods
### Improved YOLOv5s Helmet Detection Algorithm
#### 4.2.3 Loss Function Improvement  
(1) NWD loss  
In this paper, a new loss function, called Normalized Gaussian Wasserstein Distance (NWD), is introduced into the target detection model based on YOLOv5 to measure the distance between the predicted frame and the ground live frame. The NWD loss uses a Gaussian distribution to calculate the distance between two probability distributions and normalizes them to ensure comparability. For two 2D Gaussian distributions  
Figure 5: CARAFE upsampling module. \(\mu_{1}=N(m_{1},\sum 1)\) and \(\mu_{2}=N(m_{2},\sum 2)\), the second-order Wasserstein distance between \(\mu_{1}\) and \(\mu_{2}\) is calculated as shown in Equation (1):  
\[{W_{2}}^{2}(\mu_{1},\,\mu_{2})=\left\|m_{1}-m_{2}\right\|_{2}^{2}+\left\|\Sigma_ {1}^{1/2}-\Sigma_{2}^{1/2}\right\|_{F}^{2} \tag{1}\]  
In the given equation,\(\left\|\cdot\right\|_{F}\) is the Frobenius norm, and \(m_{n}\) and \(\sum n\) represent the mean and covariance matrices of the \(n^{th}\) 2D Gaussian distribution, respectively. Furthermore, when considering Gaussian distributions \(N_{a}\) and \(N_{b}\) modeled by the bounding boxes \(A=(cx_{a},cy_{a},w_{a},h_{a})\) and \(B=(cx_{b},cy_{b},w_{b},h_{b})\), the equation can be simplified, as shown in Equation (2). \[{W_{2}}^{2}(N_{a},\,N_{b})=\left\|\left(\left[cx_{a},cy_{a},\frac{w_{a}}{2}, \frac{h_{a}}{2}\right]^{T},\left[cx_{b},cy_{b},\frac{w_{b}}{2},\frac{h_{b}}{2 }\right]^{T}\right)\right\|_{2}^{2} \tag{2}\]  
In the equation, \((cx_{a},cy_{a})\), \((cx_{b},cy_{b})\), \((w_{a},h_{a})\) and \((w_{b},h_{b})\) represent the center point coordinates, width, and height of bounding boxes A and B, respectively. \({W_{2}}^{2}(N_{a},N_{b})\) is a distance measure and cannot be used as a similarity measure. To solve this problem, the exponential form of the Wasserstein distance is normalized to derive a new metric called the Normalized Wasserstein Distance (NWD), as shown in Equation (3). \[NWD(N_{a},\,N_{b})\,=\,exp\left(-\frac{\sqrt{{W_{2}}^{2}(N_{a},\,N_{b})}}{C}\right) \tag{3}\]  
In Equation (3), \(C\) represents a constant closely related to the dataset, and is used to control the penalty when there is a poor overlap between the predicted and the actual bounding boxes. A higher value of \(C\) corresponds to a smaller penalty, while a lower value of \(C\) corresponds to a larger penalty. In this paper, the value of \(C\) is set to 3. Figures 6 and 7 show the comparison of IoU deviation curves [11] and NWD deviation curves in two different scenarios. The boxSize parameter represents the scale configuration of the bounding box. The horizontal axis represents the pixel deviation between the center points of the predicted box B and the actual box A. The vertical axis represents the corresponding IoU and NWD values for a given deviation. The curves of different colors represent the IoU Deviation or NWD Deviation curves for the respective scale settings. Since the position of the bounding box can only be changed discretely, the numerical deviation curves are presented as scatter plots. By examining the deviation curves, it is evident that NWD is more sensitive to changes in pixel deviation and better reflects the accuracy and stability of the target detection algorithm. Compared to the Intersection over Union (IoU), the Normalized Wasserstein Distance (NWD) has several obvious advantages:  
1. IoU only quantifies the overlap and union of two bounding boxes and does not provide additional detailed information. On the contrary, NWD converts bounding boxes into probability distributions using the Gaussian distribution, enabling the measurement of the distance between these distributions. Due to the continuity of the Gaussian distribution, NWD provides enhanced granularity in identifying similarity between bounding boxes. Therefore, NWD provides richer information and a more accurate assessment of similarity. 2. IoU requires the bounding boxes to have the same scale and orientation, and its accuracy has decreased.