# YOLOv7-3D: A Monocular 3D Traffic Object Detection Method from a Roadside Perspective
## 3 Proposed Method
### Loss Function  
In this work, the model does not directly predict the seven degrees of freedom of the object \((x,y,z,l,w,h,\theta)\) but predicts nine degrees of freedom \((x_{c},y_{c},\varphi_{z},\sigma_{z},l,w,h,\sin(\theta),\cos(\theta))\). Here, \((x_{c},y_{c})\) represent the projected 2D coordinates of the center point of the 3D bounding box on the image. \((\varphi_{z},\sigma_{z})\) denote the depth compensation value and the logarithmic variance of the depth value. The introduction of \(\sigma_{z}\) aims to quantify the uncertainty of the predictions by considering the variance. \((l,w,h)\) represent the length, width, and height of the 3D bounding box. \((\sin(\theta),\cos(\theta))\) are used to compute the heading angle. _Classification loss function_: The paper introduces Focal Loss to address the issue of class imbalance in the Rope3D dataset. Focal Loss reduces the weight assigned to easily classified samples and increases the weight assigned to hard samples in order to focus more on challenging samples. This helps improve the performance of the model in scenarios with class imbalance:  
\[L_{cls}=-\alpha_{t}(1-P_{t})^{\gamma}log(P_{t}) \tag{6}\]  
where \(P_{t}\) is the predicted probability by the model, indicating the probability of the sample belonging to the target class. \(\alpha_{t}\) is the balance factor used to adjust the weights of positive and negative samples. In general, it is defined as the ratio of the number of positive samples to the total number of samples. \(\gamma\) is the focusing parameter used to adjust the weight difference between easily classified samples and hard samples. A larger value of \(\gamma\) will make the model pay more attention to hard samples. _2D box regression loss function_: For the prediction of the 2D bounding box, this paper uses the CIoU (Complete Intersection over Union) loss. \[L_{IOU}=1-IOU+\frac{\rho^{2}(b,b^{*})}{c^{2}}+\alpha v \tag{7}\]  
where IOU represents the traditional Intersection over Union, \(\rho^{2}(b,b^{*})\) denotes the Euclidean distance between the center points of the predicted box and the ground truth box. \(\alpha\) is a balance factor used to adjust the weights of the position and shape losses, and \(v\) represents the position and shape loss. The specific formula is as follows:  
\[\alpha=\frac{v}{1-IOU+v} \tag{8}\] \[v=\frac{4}{\pi^{2}}\bigg{(}\arctan\frac{w^{*}}{h^{*}}-\arctan \frac{w}{h}\bigg{)}^{2}\]  
_Projection center point loss function_: For the projection of the 3D object center point onto the 2D image, this paper uses L1 loss for direct regression. Unlike previous methods, this paper modifies the prediction of the center point to be an offset relative to the center point of the 2D bounding box rather than an offset relative to the entire image. Here, \(P\) represents the ground truth projected point coordinates, and \(P^{*}\) represents the predicted projected point coordinates. \[L_{center}=||P-P^{*}||_{1} \tag{9}\]_Dimension-aware loss function_: For the prediction of the 3D bounding box dimensions, this paper employs a dimension-aware L1 loss function that is suitable for size estimation. The loss function is defined as follows:  
\[L_{dim}=\parallel\frac{\left(\mathbf{s}-\mathbf{s}^{*}\right)}{\mathbf{s}} \parallel_{1} \tag{10}\]  
Here, \(\parallel\cdot\parallel_{1}\) denotes the L1 norm. Previous algorithms used a baseline L1 loss, i.e., \(L^{\prime}_{dim}=\parallel\mathbf{s}-\mathbf{s}^{*}\parallel_{1}\). It is worth noting that, compared to the baseline L1 loss \(L^{\prime}_{dim}\), the dimension-aware loss used in this paper incorporates dynamic compensation generation. By re-computing \(L^{\prime}_{dim}\), compensation weights \(w_{dim}=L^{\prime}_{dim}/L_{dim}\) are generated to make the average value of the final loss function \(L_{dim}\) equal to the standard loss. In this way, the proposed loss can be seen as a redistribution of the standard L1 loss.