glenn-jocher commited on
Commit
5a7d79f
·
unverified ·
1 Parent(s): 82ed33a

Update general.py bbox_iou() with eps improvements (#736)

Browse files
Files changed (1) hide show
  1. utils/general.py +20 -21
utils/general.py CHANGED
@@ -339,41 +339,37 @@ def compute_ap(recall, precision):
339
  return ap
340
 
341
 
342
- def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-12):
343
  # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
344
  box2 = box2.T
345
 
346
  # Get the coordinates of bounding boxes
347
  if x1y1x2y2: # x1, y1, x2, y2 = box1
348
- b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2] + eps, box1[3] + eps
349
- b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2] + eps, box2[3] + eps
350
  else: # transform from xywh to xyxy
351
- b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2 + eps
352
- b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2 + eps
353
- b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2 + eps
354
- b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2 + eps
355
 
356
  # Intersection area
357
  inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
358
  (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
359
 
360
  # Union Area
361
- w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1
362
- w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1
363
- union = w1 * h1 + w2 * h2 - inter
364
 
365
- iou = inter / union # iou
366
  if GIoU or DIoU or CIoU:
367
  cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
368
  ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
369
- if GIoU: # Generalized IoU https://arxiv.org/pdf/1902.09630.pdf
370
- c_area = cw * ch # convex area
371
- return iou - (c_area - union) / c_area # GIoU
372
- if DIoU or CIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
373
- # convex diagonal squared
374
- c2 = cw ** 2 + ch ** 2
375
- # centerpoint distance squared
376
- rho2 = ((b2_x1 + b2_x2) - (b1_x1 + b1_x2)) ** 2 / 4 + ((b2_y1 + b2_y2) - (b1_y1 + b1_y2)) ** 2 / 4
377
  if DIoU:
378
  return iou - rho2 / c2 # DIoU
379
  elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
@@ -381,8 +377,11 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=
381
  with torch.no_grad():
382
  alpha = v / ((1 + eps) - iou + v)
383
  return iou - (rho2 / c2 + v * alpha) # CIoU
384
-
385
- return iou
 
 
 
386
 
387
 
388
  def box_iou(box1, box2):
 
339
  return ap
340
 
341
 
342
+ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-9):
343
  # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
344
  box2 = box2.T
345
 
346
  # Get the coordinates of bounding boxes
347
  if x1y1x2y2: # x1, y1, x2, y2 = box1
348
+ b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
349
+ b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
350
  else: # transform from xywh to xyxy
351
+ b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
352
+ b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
353
+ b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
354
+ b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2
355
 
356
  # Intersection area
357
  inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
358
  (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
359
 
360
  # Union Area
361
+ w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
362
+ w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
363
+ union = w1 * h1 + w2 * h2 - inter + eps
364
 
365
+ iou = inter / union
366
  if GIoU or DIoU or CIoU:
367
  cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
368
  ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
369
+ if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
370
+ c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
371
+ rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
372
+ (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
 
 
 
 
373
  if DIoU:
374
  return iou - rho2 / c2 # DIoU
375
  elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
 
377
  with torch.no_grad():
378
  alpha = v / ((1 + eps) - iou + v)
379
  return iou - (rho2 / c2 + v * alpha) # CIoU
380
+ else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
381
+ c_area = cw * ch + eps # convex area
382
+ return iou - (c_area - union) / c_area # GIoU
383
+ else:
384
+ return iou # IoU
385
 
386
 
387
  def box_iou(box1, box2):