Skip to content

Commit

Permalink
Merge pull request #1750 from mikel-brostrom/fix-giou
Browse files Browse the repository at this point in the history
use union area instead of intersection area in giou
  • Loading branch information
mikel-brostrom authored Nov 22, 2024
2 parents e576cdb + b4aea27 commit 3d6ed29
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions boxmot/utils/iou.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ def hmiou_batch(bboxes1, bboxes2):
@staticmethod
def giou_batch(bboxes1, bboxes2) -> np.ndarray:
"""
:param bbox_p: predict of bbox(N,4)(x1,y1,x2,y2)
:param bbox_g: groundtruth of bbox(N,4)(x1,y1,x2,y2)
:param bboxes1: predict of bbox(N,4)(x1,y1,x2,y2)
:param bboxes2: groundtruth of bbox(N,4)(x1,y1,x2,y2)
:return:
"""
# for details should go to https://arxiv.org/pdf/1902.09630.pdf
# ensure predict's bbox form
# Ensure predict's bbox form
bboxes2 = np.expand_dims(bboxes2, 0)
bboxes1 = np.expand_dims(bboxes1, 1)

Expand All @@ -104,12 +103,16 @@ def giou_batch(bboxes1, bboxes2) -> np.ndarray:
yy2 = np.minimum(bboxes1[..., 3], bboxes2[..., 3])
w = np.maximum(0.0, xx2 - xx1)
h = np.maximum(0.0, yy2 - yy1)
wh = w * h
iou = wh / (
(bboxes1[..., 2] - bboxes1[..., 0]) * (bboxes1[..., 3] - bboxes1[..., 1]) +
(bboxes2[..., 2] - bboxes2[..., 0]) * (bboxes2[..., 3] - bboxes2[..., 1]) -
wh
)
wh = w * h # Intersection area

# Compute areas of individual boxes
area1 = (bboxes1[..., 2] - bboxes1[..., 0]) * (bboxes1[..., 3] - bboxes1[..., 1])
area2 = (bboxes2[..., 2] - bboxes2[..., 0]) * (bboxes2[..., 3] - bboxes2[..., 1])

# Union area
union_area = area1 + area2 - wh

iou = wh / union_area

xxc1 = np.minimum(bboxes1[..., 0], bboxes2[..., 0])
yyc1 = np.minimum(bboxes1[..., 1], bboxes2[..., 1])
Expand All @@ -118,9 +121,11 @@ def giou_batch(bboxes1, bboxes2) -> np.ndarray:
wc = xxc2 - xxc1
hc = yyc2 - yyc1
assert (wc > 0).all() and (hc > 0).all()
area_enclose = wc * hc
giou = iou - (area_enclose - wh) / area_enclose
giou = (giou + 1.0) / 2.0 # resize from (-1,1) to (0,1)
area_enclose = wc * hc # Area of the smallest enclosing box

# Corrected GIoU computation
giou = iou - (area_enclose - union_area) / area_enclose
giou = (giou + 1.0) / 2.0 # Resize from (-1,1) to (0,1)
return giou


Expand Down

0 comments on commit 3d6ed29

Please sign in to comment.