# Multi-Scale Safety Helmet Detection Based on SAS-YOLOv3-Tiny
## 3 SAS-YOLOv3-Tiny Algorithm
### Improved Loss Function  
Recently, in terms of bounding box regression, _IOU_ loss optimizations have replaced previous regression loss optimizations (MSE loss, L1-Smooth loss, etc.). One of the most commonly used evaluation criteria for the performance of object detection algorithms is intersection over union (_IOU_), which is the ratio of the overlap area of the ground truth box and the prediction box to the total area of the two boxes, as shown in Formula (5). In Formula (5), \(A=(x,y,w,h)\) represents the prediction box and \(B=(x^{gt},y^{gt},w^{gt},h^{gt})\) represents the ground truth box.  
\[IOU=\frac{\left|A\cap B\right|}{\left|A\cup B\right|} \tag{5}\]  
Even though _IOU_ can reflect the detection effect of the prediction box and the ground truth box, it only works when the bounding boxes overlap and does not provide any adjustment gradient for the non-overlapped part. The concept of _IOU_ is based on the ratio, so it is insensitive to the object scale. In this paper, in Formula (6), the traditional regression loss MSE is replaced with CIoU [26], whose detection effect is more conducive to the actual scene. It inherits the advantages of the Generalized Intersection Over Union (GIoU) [27] and Distance-IoU(DIoU) [28], which not only considers the distance and overlap ratio but also considers the scale and the aspect ratio between the prediction box and the ground truth box so that it can carry out the bounding box regression better. The complete definition of CIoU loss function is shown in Formula (7). Therefore, the loss function of SAS-YOLOv3-tiny is shown in Formula (6), which is divided into three parts: \(loss_{CIoU}\) represents the regression loss, \(loss_{obj}\) represents the confidence loss and \(loss_{\text{class}}\) represents the category loss, they are shown as shown in Formulas (7)-(9).  
\[LOSS=loss_{CIoU}+loss_{obj}+loss_{class} \tag{6}\]  
\[loss_{CIoU}=1-CIoU\quad CIoU=IOU-\frac{\rho^{2}\left(b,b^{gt}\right)}{c^{2}}-\alpha\
u \tag{7}\]  
\[loss_{obj}=-\sum\limits_{i=0}^{K\times K}\sum\limits_{j=0}^{M}l_{ij}^{obj} \bigg{[}\hat{C}_{i}\log(C_{i})+(1-\hat{C}_{i})\log(1-C_{i})\bigg{]} \tag{8}\]  
\[loss_{class}=-\sum\limits_{i=0}^{K\times K}l_{ij}^{obj}\sum\limits_{c\in classes }\bigg{[}\hat{p}_{i}(c)\log(p_{i}(c))+(1-\hat{p}_{i}(c))\log(1-p_{i}(c))\bigg{]} \tag{9}\]  
In Formula (7), \(b\) and \(b^{gt}\) represent the center points of the prediction box and the ground truth box, respectively. Meanwhile, \(\rho^{2}\left(b,b^{gt}\right)\) represents the Euclidean distance between two center points, \(c\) represents the diagonal distance of the smallest closure region that can contain both the prediction box and the ground truth box, \(\alpha\) is the weight parameter and \(\
u\) is used to measure the similarity of aspect ratios. In Formulas (8) and (9), \(K\times K\) represents the size of the final feature map to be detected; \(I_{ij}^{obj}\) is used to determine whether the \(j\)-th prior box in the \(i\)-th grid is responsible for the object. If it is responsible for the object, it has a value of 1. Otherwise, its value is 0. The weight coefficients \(\lambda_{coord}\) and \(\lambda_{noobj}\) are set at 5 and 0.5, respectively, which are used to offset the imbalance between positive and negative samples.