JieLi 李杰 commited on
Commit
afa5cfb
·
unverified ·
1 Parent(s): 95c7bc2

Reduce G/D/CIoU logic operations (#6074)

Browse files

Consider that the default value is CIOU,adjust the order of judgment could reduce the number of judgments.
And “elif CIoU:” didn't need 'if'.

Co-authored-by: 李杰 <[email protected]>

Files changed (1) hide show
  1. utils/metrics.py +4 -4
utils/metrics.py CHANGED
@@ -222,20 +222,20 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=
222
  union = w1 * h1 + w2 * h2 - inter + eps
223
 
224
  iou = inter / union
225
- if GIoU or DIoU or CIoU:
226
  cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
227
  ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
228
  if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
229
  c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
230
  rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
231
  (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
232
- if DIoU:
233
- return iou - rho2 / c2 # DIoU
234
- elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
235
  v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
236
  with torch.no_grad():
237
  alpha = v / (v - iou + (1 + eps))
238
  return iou - (rho2 / c2 + v * alpha) # CIoU
 
 
239
  else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
240
  c_area = cw * ch + eps # convex area
241
  return iou - (c_area - union) / c_area # GIoU
 
222
  union = w1 * h1 + w2 * h2 - inter + eps
223
 
224
  iou = inter / union
225
+ if CIoU or DIoU or GIoU:
226
  cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
227
  ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
228
  if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
229
  c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
230
  rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
231
  (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
232
+ if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
 
 
233
  v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
234
  with torch.no_grad():
235
  alpha = v / (v - iou + (1 + eps))
236
  return iou - (rho2 / c2 + v * alpha) # CIoU
237
+ else:
238
+ return iou - rho2 / c2 # DIoU
239
  else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
240
  c_area = cw * ch + eps # convex area
241
  return iou - (c_area - union) / c_area # GIoU