glenn-jocher commited on
Commit
0414637
·
unverified ·
1 Parent(s): a84cd02

Update metrics.py with IoU protected divides (#8550)

Browse files
Files changed (1) hide show
  1. utils/metrics.py +5 -5
utils/metrics.py CHANGED
@@ -259,7 +259,7 @@ def box_area(box):
259
  return (box[2] - box[0]) * (box[3] - box[1])
260
 
261
 
262
- def box_iou(box1, box2):
263
  # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
264
  """
265
  Return intersection-over-union (Jaccard index) of boxes.
@@ -277,10 +277,10 @@ def box_iou(box1, box2):
277
  inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp(0).prod(2)
278
 
279
  # IoU = inter / (area1 + area2 - inter)
280
- return inter / (box_area(box1.T)[:, None] + box_area(box2.T) - inter)
281
 
282
 
283
- def bbox_ioa(box1, box2, eps=1E-7):
284
  """ Returns the intersection over box2 area given box1, box2. Boxes are x1y1x2y2
285
  box1: np.array of shape(4)
286
  box2: np.array of shape(nx4)
@@ -302,12 +302,12 @@ def bbox_ioa(box1, box2, eps=1E-7):
302
  return inter_area / box2_area
303
 
304
 
305
- def wh_iou(wh1, wh2):
306
  # Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2
307
  wh1 = wh1[:, None] # [N,1,2]
308
  wh2 = wh2[None] # [1,M,2]
309
  inter = torch.min(wh1, wh2).prod(2) # [N,M]
310
- return inter / (wh1.prod(2) + wh2.prod(2) - inter) # iou = inter / (area1 + area2 - inter)
311
 
312
 
313
  # Plots ----------------------------------------------------------------------------------------------------------------
 
259
  return (box[2] - box[0]) * (box[3] - box[1])
260
 
261
 
262
+ def box_iou(box1, box2, eps=1e-7):
263
  # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
264
  """
265
  Return intersection-over-union (Jaccard index) of boxes.
 
277
  inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp(0).prod(2)
278
 
279
  # IoU = inter / (area1 + area2 - inter)
280
+ return inter / (box_area(box1.T)[:, None] + box_area(box2.T) - inter + eps)
281
 
282
 
283
+ def bbox_ioa(box1, box2, eps=1e-7):
284
  """ Returns the intersection over box2 area given box1, box2. Boxes are x1y1x2y2
285
  box1: np.array of shape(4)
286
  box2: np.array of shape(nx4)
 
302
  return inter_area / box2_area
303
 
304
 
305
+ def wh_iou(wh1, wh2, eps=1e-7):
306
  # Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2
307
  wh1 = wh1[:, None] # [N,1,2]
308
  wh2 = wh2[None] # [1,M,2]
309
  inter = torch.min(wh1, wh2).prod(2) # [N,M]
310
+ return inter / (wh1.prod(2) + wh2.prod(2) - inter + eps) # iou = inter / (area1 + area2 - inter)
311
 
312
 
313
  # Plots ----------------------------------------------------------------------------------------------------------------