# YOLOv7-3D: A Monocular 3D Traffic Object Detection Method from a Roadside Perspective
## 3 Proposed Method
### Loss Function
_Viewpoint loss function_: Instead of directly predicting the object's yaw angle (the global orientation angle of the object in the camera coordinate system, ranging from \(-\pi\) to \(\pi\)), this paper chooses to regress the observation angle \(\theta\) for each object. The observation angle \(\theta\) represents the angle between the object's direction and the camera axis, with the camera origin as the center and the line connecting the camera origin to the object's center as the radius, after rotating the object around the camera's \(y\)-axis to align with the camera's \(z\)-axis. The transformation relationship between these two angles is shown in Figure 5. In addition, each is encoded as a vector. Using the target position, the yaw angle can be obtained: Furthermore, each \(\theta\) is encoded as a vector \(\left[\sin(\theta),\cos(\theta)\right]^{T}\). By using \(\theta\) and the object's position, the yaw angle \(ry\) can be obtained:  
\[ry+\frac{\pi}{2}-\theta =\alpha+\frac{\pi}{2} \tag{11}\] \[\alpha =ry-\theta\]  
The final viewpoint loss function computes the loss using SmoothL1 Loss on the normalized \(\left[\sin(\theta),\cos(\theta)\right]^{T}\). _Depth loss function_: In 3D object detection, depth estimation is a task with uncertainty since the depth of an object in real-world scenarios can be influenced by various factors such as viewpoint, occlusion, and image noise. This paper utilizes the Laplacian Aleatoric Uncertainty Loss to predict the depth of 3D objects, quantifying the uncertainty of the predictions by introducing a variance term. This loss reduces the weight of depth  
Figure 5: The diagram illustrating the viewing angle. predictions with higher variance (i.e., greater uncertainty), thereby increasing the accuracy and reliability of depth predictions with lower uncertainty. \[L_{IOU}=1-IOU+\frac{\rho^{2}(b,b^{*})}{c^{2}}+\alpha v \tag{7}\]0  
Here, \(\mathbb{P}\) represents the set of real 2D bounding box centers, \((\varphi_{z},\sigma_{z})\) denotes the depth offset and logarithmic variance, \(z\) is the true depth value of the object, and \(z^{*}\) is the predicted depth value of the object. The predicted depth value \(z^{*}\) is obtained by reversing the sigmoid transformation of the model's predicted depth offset \(\varphi_{z}\) back to the original linear range:  
\[L_{IOU}=1-IOU+\frac{\rho^{2}(b,b^{*})}{c^{2}}+\alpha v \tag{7}\]1  
By combining the projected points of the center on the feature map \((x_{c},y_{c})\), the inverse matrix of the camera intrinsic parameters \(K^{-1}\in\mathbb{R}^{3\times 3}\), the 3D center position of the object in meters can be calculated as:  
\[\left[ \begin{array}{c}x\\ y\\ z\end{array} \right]_{3D}=K^{-1}_{3\times 3}\left[ \begin{array}{c}zx_{c}\\ zy_{c}\\ z\end{array} \right]_{2D} \tag{14}\]  
Once the seven degrees of freedom for the 3D bounding box are obtained, the coordinates of the eight corners of the 3D box on the 2D image can be calculated as:  
\[B=R_{\theta}\left[ \begin{array}{c}\pm h/2\\ \pm w/2\\ \pm l/2\end{array} \right]+\left[ \begin{array}{c}x\\ y\\ z\end{array} \right] \tag{15}\]  
Here, \(R_{\theta}\) is the rotation matrix for the yaw angle, \([h,w,l]^{T}\) represents the object's 3D dimensions, and \([x,y,z]^{T}\) represents the object's center coordinates.The overall loss is the sum of all the individual loss terms, with each loss term having a balancing weight parameter.