How can i match bounding box coordinates for grount-truth and predicted part?

Question:

I worked on a model. and I have 100 bounding boxes for gt and predicted parts. I would like to calculate iou score for each. But I cannot do this directly, because of the orders. Order of coordinates of bounding boxes are different. For example the first bounding box for gt is overlap with 43th bounding box of predicted. I tried to find the matched orders and calculate the iou for each with the following code:

for i in range (0,100):
    for j in range(0,100):
        
        pxmin,pymin,pxmax,pymax=pred['boxes'][i]
        gtxmin,gtymin,gtxmax,gtymax=gt[j]
       

        ixmin=max(pxmin,gtxmin)
        ixmax=min(pxmax,gtxmax)
        iymin=max(pymin,gtymin)
        iymax=min(pymax,gtymax)
    
        parea=(pxmax-pxmin)*(pymax-pymin)
        gtarea=(gtxmax-gtxmin)*(gtymax-gtymin)
        iarea=(ixmax-ixmin)*(iymax-iymin)
        IOU=iarea/(parea+gtarea-iarea)
        
        if 1>IOU>0.7:
            print(i,j,np.array(IOU))

but this time, it for each orders match with different orders more than once even they are not overlaped. for instance:

0 43 0.9042958
0 44 0.9875165
0 58 0.9222096
1 16 0.7543962
1 41 0.9221437
2 26 0.90981704
....

I cannot take the highest iou score, because some of them are not overlaped with each other even they have the highest iou score.How can I solve this issue?

Asked By: dilara

||

Answers:

The problem is that your snippet assigns a positive value to iarea if both (ixmax-ixmin) and (iymax-iymin) are negative (there is no intersection in that case) resulting in positive IOU.

A quick fix would be this:

for i in range (0,100):
    for j in range(0,100):
        
        pxmin,pymin,pxmax,pymax=pred['boxes'][i]
        gtxmin,gtymin,gtxmax,gtymax=gt[j]
       

        ixmin=max(pxmin,gtxmin)
        ixmax=min(pxmax,gtxmax)
        iymin=max(pymin,gtymin)
        iymax=min(pymax,gtymax)
    
        parea=(pxmax-pxmin)*(pymax-pymin)
        gtarea=(gtxmax-gtxmin)*(gtymax-gtymin)
        if (ixmax - ixmin) < 0 and (iymax - iymin) < 0:
            iarea = 0
        else:
            iarea = (ixmax - ixmin) * (iymax - iymin)
        IOU=iarea/(parea+gtarea-iarea)
        
        if 1>IOU>0.7:
            print(i,j,np.array(IOU))

Please note that this can be done much more efficiently using numpy (I can provide an example if you will).

Answered By: Michal Racko
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.