Concept/ML&DL

IoU(Intersection over Union)

Naranjito 2023. 6. 14. 14:01
  • IoU

Intersection over Union, is used to evaluate the performance of object detection by comparing the ground truth bounding box to the preddicted bounding box.

 

  • Ground Truth bounding box

Hand labeled, the actual images themselves. 

 

  • Computing Intersection over Union

It can therefore be determined via :

- The numerator : we compute the area of overlap between the predicted bounding box and the ground-truth bounding box.

- The denominator : is the area of union, or more simply, the area encompassed by both the predicted bounding box and the ground-truth bounding box.

 

For example :

The predicted bounding boxes that heavily overlap with the ground-truth bounding boxes have higher scores than those with less overlap. This makes Intersection over Union an excellent metric for evaluating custom object detectors.

 

  • calculate IoU

 

 

X = ( A 1 , B 1 , C 1 , D 1 )

 

Y = ( A 2 , B 2 , C 2 , D 2 )
A inter and B inter stand for the bounding box's top-left corner coordinates, while C inter and D inter stand for the bounding box's bottom-right corner coordinates.

 

A i n t e r = m a x ( A 1 , A 2 )

 

B i n t e r = m a x ( B 1 , B 2 )

 

C i n t e r = m i n ( C 1 , C 2 )

 

D i n t e r = m i n ( D 1 , D 2 )

 

There is one condition that needs to be met: 

 

( C i n t e r < A i n t e r )   o r   ( D i n t e r < B i n t e r )

 

When the boxes don’t intersect, IoU cannot be calculated.


The overlap and the union parts of boxes X and Y are calculated. Equation (10) help us calculate the intersection points using the overlap boundary points, whereas equations (11) and (12) are used for calculating n(X) and n(Y) of the set X and Y.

Equations 13, on the other hand, is the union formula which is the sum of individual n(X) and n(Y) minus the intersection of both. Using Eq. (12) and Eq. (10), we can calculate Eq. (14).

 

10 .   | X   Y |   =   ( C i n t e r   A i n t e r )   x   ( D i n t e r   B i n t e r )

 

11 .   | X | =   ( C 1 - A 1 ) ×   ( D 1 -   B 1 )

 

12 .   | Y | =   ( C 2 - A 2 ) ×   ( D 2 -   B 2 )

 

13 .   | X | | Y | = | X | + | Y | - X   Y  

 

14 .   I n t e r s e c t i o n   o v e r   U n i o n   ( I o U ) = TP TP + FN + FP

Let's say that the ground truth bounding box for a picture of a dog is [A1=50, B1=100, C1=200, D1=300]

and the predicted bounding box is [A2=80, B2=120, C2=220, D2=310].

X =   [ 50 , 100 , 200 , 300 ]   Y = [ 80 , 120 , 220 , 310 ]

 

The visual representation of the box is shown below:


The calculation is as follows:

 

| X Y |   =   ( 200 - 80 )   *   ( 300 - 120 )   =   120   *   180 =   21 , 600

 

| X | = ( 200 - 50 )   *   ( 300 - 100 )   =   150   *   200   =   30 , 000

 

| Y | = ( 220 - 80 )   *   ( 310 - 120 )   =   140   *   190   =   26 , 600

 

| X | | Y | = 30 , 000   +   26 , 600     21 , 600   =   35 , 000

To calculate the IoU score, we can now enter the following values into the IoU formula:

 

I o U =   21 , 600 35 , 000 =   0 . 62
def calculate_iou(gt_bbox, pre_bbox):
    """
    args
    gt_bbox[array]:1*4 single gt bbox
    pre_bbox[array]:1*4 single pre bbox
    
    return
    iou[float]:iou between 2bboxes
    """
    
    xmin=np.max([gt_bbox[0],pre_bbox[0]])
    ymin=np.max([gt_bbox[1],pre_bbox[1]])
    xmax=np.min([gt_bbox[2],pre_bbox[2]])
    ymax=np.min([gt_bbox[3],pre_bbox[3]])
    
    intersection = max(0, xmax - xmin) * max(0, ymax - ymin)
    gt_area=(gt_bbox[2]-gt_bbox[0])*(gt_bbox[3]-gt_bbox[1])
    pred_area=(pre_bbox[2]-pre_bbox[0])*(pre_bbox[3]-pre_bbox[1])
    union=gt_area+pred_area-intersection
    
    return intersection/union, [xmin, ymin, xmax, ymax]

 

http://ronny.rest/tutorials/module/localization_001/iou/

https://www.v7labs.com/blog/intersection-over-union-guide