Skip to content

Commit

Permalink
Apply SIoU on Bounding Box IoU metrics as default
Browse files Browse the repository at this point in the history
- method metrics/bbox_iou
- applying it as default metric
  • Loading branch information
rodsei committed Apr 30, 2023
1 parent 8211a03 commit 1097214
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion utils/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __call__(self, p, targets): # predictions, targets
pxy = pxy.sigmoid() * 2 - 0.5
pwh = (pwh.sigmoid() * 2) ** 2 * anchors[i]
pbox = torch.cat((pxy, pwh), 1) # predicted box
iou = bbox_iou(pbox, tbox[i], CIoU=True).squeeze() # iou(prediction, target)
iou = bbox_iou(pbox, tbox[i], SIoU=True).squeeze() # iou(prediction, target)
lbox += (1.0 - iou).mean() # iou loss

# Objectness
Expand Down
23 changes: 21 additions & 2 deletions utils/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def print(self):
print(' '.join(map(str, self.matrix[i])))


def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7):
def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, SIoU=False, eps=1e-7):
# Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)

# Get the coordinates of bounding boxes
Expand All @@ -243,9 +243,28 @@ def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7

# IoU
iou = inter / union
if CIoU or DIoU or GIoU:
if CIoU or DIoU or GIoU or SIoU:
cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width
ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height
if SIoU: # SIoU Loss https://arxiv.org/pdf/2205.12740.pdf
s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5
s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5
sigma = torch.pow(s_cw ** 2 + s_ch ** 2, 0.5) + eps
sin_alpha_1 = torch.abs(s_cw) / sigma
sin_alpha_2 = torch.abs(s_ch) / sigma
threshold = pow(2, 0.5) / 2
sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1)
# angle_cost = 1 - 2 * torch.pow( torch.sin(torch.arcsin(sin_alpha) - np.pi/4), 2)
# angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - np.pi / 2)
angle_cost = torch.sin(torch.arcsin(sin_alpha) * 2)
rho_x = (s_cw / (cw + eps)) ** 2
rho_y = (s_ch / (ch + eps)) ** 2
gamma = angle_cost - 2
distance_cost = 2 - torch.exp(gamma * rho_x) - torch.exp(gamma * rho_y)
omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2)
omiga_h = torch.abs(h1 - h2) / torch.max(h1, h2)
shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4)
return iou - 0.5 * (distance_cost + shape_cost)
if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center dist ** 2
Expand Down
2 changes: 1 addition & 1 deletion utils/segment/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __call__(self, preds, targets, masks): # predictions, targets, model
pxy = pxy.sigmoid() * 2 - 0.5
pwh = (pwh.sigmoid() * 2) ** 2 * anchors[i]
pbox = torch.cat((pxy, pwh), 1) # predicted box
iou = bbox_iou(pbox, tbox[i], CIoU=True).squeeze() # iou(prediction, target)
iou = bbox_iou(pbox, tbox[i], SIoU=True).squeeze() # iou(prediction, target)
lbox += (1.0 - iou).mean() # iou loss

# Objectness
Expand Down

0 comments on commit 1097214

Please sign in to comment.