Intersection over Union

2023. 4. 21. 05:59ML

IoU

IoU is Intersection over Union. Let's we have two overlappig boxes.

$IoU = \frac{Intersection}{Union} = \frac{Intersection}{Area1 + Area2 - Intersction}$.

When two bounding boxes perfectly match, IoU = 1. When two boxes are far away, IoU=0.

IoU of two boxes is $\frac{0.25}{1.75}$. If IoU is 1, two boxes is same. So IoU means how similar the two boxes are.

However IoU has a plateau making it infeasible to optimize in the case of nonoverlapping bounding boxes.

GIOU

GIoU is generalized IoU. Let's add one minimum box contain two boxes.

$IoU = IoU - \frac{AreaC - Union}{AreaC}$.

When two bounding boxes perfectly match, GIoU = 1. When two boxes are far away, GIoU=0.

GIoU of two boxes is $\frac{0.25}{1.75} - \frac{2.25 - 1.75}{2.25} = -\frac{5}{63}$.

However GIoU suffer from the problems of slow convergence and inaccurate regression.

 

DIoU

DIoU is Distance-IoU. Let's add two diagonal lines of Intersection box and Cbox.

$DIoU = IoU - \frac{p^{2}}{c^{2}}$.

c is euclidean distance minmax points of Cbox, p is euclidean distance of center points of two boxes.

When two bounding boxes perfectly match, DIoU = 1. When two boxes are far away, DIoU=0.

DIoU of two boxes is $\frac{0.25}{1.75} - \frac{0.5}{4.5} = \frac{2}{63}$

And we can add aspect ratio to DIoU.

 

CIoU

CIoU is Complete IoU. It based on DIoU.

$CIoU = IoU - (\frac{p^{2}}{c^{2}} + \alpha v)$.

$v=\frac{4}{\pi}(\arctan\frac{w_{1}}{h_{1}} - \arctan\frac{w_{2}}{h_{2}})^{2}$.

$\alpha = \frac{v}{(1 - IoU) + v}$.

When two bounding boxes perfectly match, CIoU = 1. When two boxes are far away, CIoU=0.

CIoU of two boxes is $\frac{0.25}{1.75} - (\frac{0.5}{4.5} + \alpha v)$.

$v = \frac{4}{\pi}(\arctan\frac{1}{1} - \arctan\frac{1}{1})^{2} = 0, \alpha = 0$

So CIoU of boxes is $\frac{0.25}{1.75} - (\frac{0.5}{4.5} + 0) = \frac{2}{63}$

 

Code

def bbox_iou(bbox1, bbox2, xywh=True, iou_type='iou', eps=EPS):
    if xywh:
        area1 = tf.reduce_prod(bbox1[..., 2:4], -1)
        area2 = tf.reduce_prod(bbox2[..., 2:4], -1)
        bbox1 = tf.concat([bbox1[..., :2] - bbox1[..., 2:] * 0.5, bbox1[..., :2] + bbox1[..., 2:] * 0.5], -1)
        bbox2 = tf.concat([bbox2[..., :2] - bbox2[..., 2:] * 0.5, bbox2[..., :2] + bbox2[..., 2:] * 0.5], -1)
    else:
        area1 = tf.reduce_prod(bbox1[..., 2:4] - bbox1[..., :2], -1)
        area2 = tf.reduce_prod(bbox2[..., 2:4] - bbox2[..., :2], -1)
 
    Left_Top = tf.maximum(bbox1[..., :2], bbox2[..., :2])
    Right_Bottom = tf.minimum(bbox1[..., 2:], bbox2[..., 2:])

    inter_section = tf.maximum(Right_Bottom - Left_Top, 0.0)
    inter_area = tf.reduce_prod(inter_section, -1)
    union_area = tf.maximum(area1 + area2 - inter_area, eps)

    iou = inter_area / union_area

    if iou_type in ['giou', 'diou', 'ciou']:
        c_Left_Top = tf.minimum(bbox1[..., :2], bbox2[..., :2])
        c_Right_Bottom = tf.maximum(bbox1[..., 2:], bbox2[..., 2:])
        if iou_type == 'giou':
            c_area = tf.maximum(tf.reduce_prod(c_Right_Bottom - c_Left_Top, -1), eps)
            giou = iou - (c_area - union_area)/c_area
            return giou

    elif iou_type in ['diou', 'ciou']:
        center_xy1 = (bbox1[..., :2] + bbox1[..., 2:]) * 0.5
        center_xy2 = (bbox2[..., :2] + bbox2[..., 2:]) * 0.5
        p_square = tf.reduce_sum(tf.square(center_xy1 - center_xy2), -1)
        c_square = tf.maximum(tf.reduce_sum(tf.square(c_Right_Bottom - c_Left_Top), -1), eps)
        if iou_type == 'diou':
            diou = iou - p_square/c_square
            return diou
 
        w1 = bbox1[..., 2] - bbox1[..., 0]
        h1 = tf.maximum(bbox1[..., 3] - bbox1[..., 1], eps)
        w2 = bbox2[..., 2] - bbox2[..., 0]
        h2 = tf.maximum(bbox2[..., 3] - bbox2[..., 1], eps)

        v = 4/tf.square(np.pi) * tf.square(tf.math.atan(w1/h1) - tf.math.atan(w2/h2))
        alpha = v/tf.maximum((1 - iou + v), eps)
        ciou = iou - p_square/c_square - alpha*v
        return ciou
    return iou

Reference

GIoU: https://arxiv.org/pdf/1902.09630.pdf

DIoU, CIoU: https://arxiv.org/pdf/1911.08287.pdf

'ML' 카테고리의 다른 글

Global average Pooling  (0) 2022.10.24