Intersection over Union (IoU) 01/16/2024 Intersection over Union (IoU) IoU loss \(IoU\) computes the ratio between the intersection and union of the predicted bounding box \(\text{pred}\) and the ground-truth bounding box \(\text{gt}\). \[ \text{IoU} = \frac{\text{Area of Intersection}}{\text{Area of Union}} = \,\, \frac{\text{gt} \cap \text{pred}}{\text{gt} \cup \text{pred}} \\ \] IoU is a good measure of how much the boxes intersect, but it does not describe the distance between the predicted and true bounding boxes when the intersection part is empty. In all empty intersection states, IoU losses are all 0 like the right below figure. IoU loss Distance-Intersection over Union (DIoU) DIoU loss addresses this issue by adding a distance term to the IoU loss. The loss is defined as: \[ \text{DIoU} = 1 - \text{IoU} + \frac{d^2(c_{\text{gt}}, \,\, c_{\text{pred}})}{diag^2} \] Where: \(c_{\text{gt}}\) and \(c_{\text{pred}}\) are the centroids of the ground truth and predicted bounding boxes. \(d(c_{\text{gt}}, \, c_{\text{pred}})\) is the Euclidean distance between the centroids of the boxes \(diag\) is the diagonal distance of the AABB that contains the two boxes. DIoU loss The term for square \(^2\) of the \(d\) and the \(diag\) is to emphasize the distance between centroids. The minimum DIoU loss value is 0. This is because, When \(\text{IoU} = 1\) (perfectly intersects), the term \(1 - \text{IoU} = 0\). When \(d(c_{\text{gt}}, \, c_{\text{pred}} ) = 0\), the distance term equal to \(0\). Thus, the overall DIoU loss would be \(0\). The term for \(\frac{d^2(c_{\text{gt}}, \,\, c_{\text{pred}})}{diag^2}\) is to normalize the distance to 0 ~ 1. Implementation