# NGIoU Loss: Generalized Intersection over Union Loss Based on a New Bounding Box Regression
## 3 NGIoU Loss  
Aiming at the aforementioned problem of GIoU Loss, this paper proposes a New Generalized Intersection over Union (NGIoU) Loss based on a new bounding box regression. Algorithm 2 demonstrates the novel loss function, as follows. ```
1:The predicted regression box \(bboxes\_p\) and the ground truth regression box \(bboxes\_g\). \(bboxes\_p\) and the ground truth regression box \(bboxes\_g\). \(bboxes\_g\). ```
1:The predicted regression box A and the ground-truth regression box B. 2:NGIoU Loss
3:Find the smallest closed convex shape C surrounding A and B. 4:If \(A\subseteq B\), add a new bounding box D that has the same center as B and is congruent with A, and find the smallest closed convex shape \(C^{\prime}{}_{1}\) surrounding A and D. The line between the upper left corner and the lower right corner of \(C^{\prime}{}_{1}\) is \(d^{\prime}{}_{1}\). 5:If \(B\subseteq A\), add a new bounding box D that has the same center as A and is congruent with B, and find the smallest closed convex shape \(C^{\prime}{}_{2}\) surrounding A and D. The line between the upper left corner and the lower right corner of C\({}^{\prime}{}_{2}\) is \(d^{\prime}{}_{2}\). 6:The line between the upper left corner and the lower right corner of A is \(d_{2}\). The line between the upper left corner and the lower right corner of B is \(d_{1}\). 7:\(IOU=\frac{A\cap B}{A\cup B},L_{IOU}=1-IOU\). 8:\(GIOU=IOU-\frac{C-A\cup B}{C},L_{GIOU}=1-GIOU\). 9:\(NGIOU= \begin{cases}IOU-\beta_{1}\frac{C^{\prime}{}_{1}-A\cup B}{C},A\subseteq B \\ IOU-\beta_{2}\frac{C^{\prime}{}_{2}-B\cup B}{C^{\prime}{}_{2}},B\subseteq A\\ IOU-\frac{C-A\cup B}{C},\text{else}\end{cases} \)
10:where, \(\beta_{1}\)=\(\frac{d^{\prime}{}_{1}}{d_{1}},\beta_{2}\)=\(\frac{d^{\prime}{}_{2}}{d_{2}}\). 11:\(L_{NGIoU}=1-NGIoU\). ```

**Algorithm 2** NGIoU bounding box loss function

DIoU Loss takes into account the distance between the center points of the regression box and the object, and uses the ratio of the Euclidean distance between the center points of the two boxes. The minimum length of the diagonal enclosing the two boxes is used to replace the penalty term of GIoU, which makes the regression faster, more effective and more stable. It is observed from Figure 1 that when two boxes overlap, by replacing B with \(A^{\prime}\), the diagonal length is changed from d to \(d^{\prime}\). The length becomes shorter, but the Euclidean distance between the center points of the boxes remains unchanged. That is to say, the denominator decreases and the numerator remains unchanged, which makes the whole penalty term larger, the regression unstable and it fluctuates greatly. Hence, inspired by the distance normalization method in DIoU Loss proposed by [PERSON] [17], we adjust the coefficient of the penalty term in the case of the overlapping. The definition of scale factor \(\beta\) is as follows:

\[\beta_{n} =\frac{\alpha_{n}}{\alpha^{\prime}{}_{n}}=\frac{d^{\prime}{}_{n}}{d _{n}},(n=1,2) \tag{1}\] \[\alpha_{n} =\frac{\rho(o,\sigma^{\prime})}{d_{n}},(n=1,2)\] (2) \[\alpha^{\prime}{}_{n} =\frac{\rho(o,\sigma^{\prime})}{d^{\prime}{}_{n}},(n=1,2) \tag{3}\]

Figure 1: NGIoU loss function. where \(o\) and \(o^{\prime}\) are the center points, \(\rho(. )\) is the Euclidean distance, \(d_{n}\) is the diagonal length of the smallest closed box covering the two boxes. Since the condition discussed here is overlapping, \(d_{1}\) is the diagonal length of B and \(d_{2}\) is the diagonal length of A. Similarly, \(d^{\prime}{}_{n}\) is obtained by adding the bounding box. The following figures show IoU Loss, GIoU Loss, and NGIoU Loss at different positions when the predicted regression box overlaps with the ground-truth regression box. As observed from Figure 2, when A is in the different positions within B, IoU Loss and GIoU Loss remain unchanged. It means that the moving gradient is unable to be obtained and the regression speed decreases. Nevertheless, NGIoU Loss decreases continuously as A approaches B until the center points of A and B overlap, as shown in Figure 2c.