Spaces:
Sleeping
Sleeping
[email protected]
commited on
Commit
·
5ac8049
1
Parent(s):
7e98ad1
remove utils
Browse files- box-metrics.py +83 -2
box-metrics.py
CHANGED
@@ -3,7 +3,6 @@ import datasets
|
|
3 |
import numpy as np
|
4 |
from seametrics.payload import Payload
|
5 |
import torch
|
6 |
-
from utils import bbox_iou, bbox_bep
|
7 |
import datasets
|
8 |
|
9 |
_CITATION = """\
|
@@ -290,4 +289,86 @@ class box_metrics(evaluate.Metric):
|
|
290 |
return inter / ((a2 - a1).prod(2) + (b2 - b1).prod(2) - inter + eps)
|
291 |
|
292 |
|
293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import numpy as np
|
4 |
from seametrics.payload import Payload
|
5 |
import torch
|
|
|
6 |
import datasets
|
7 |
|
8 |
_CITATION = """\
|
|
|
289 |
return inter / ((a2 - a1).prod(2) + (b2 - b1).prod(2) - inter + eps)
|
290 |
|
291 |
|
292 |
+
def bbox_bep(box1, box2, xywh=False, eps=1e-7, bep1 = True):
|
293 |
+
"""
|
294 |
+
Calculates bottom edge proximity between two boxes
|
295 |
+
|
296 |
+
Input shapes are box1(1,4) to box2(n,4)
|
297 |
+
|
298 |
+
Implementation of bep2 from
|
299 |
+
Are object detection assessment criteria ready for maritime computer vision?
|
300 |
+
"""
|
301 |
+
|
302 |
+
# Get the coordinates of bounding boxes
|
303 |
+
if xywh: # transform from xywh to xyxy
|
304 |
+
(x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1)
|
305 |
+
w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
|
306 |
+
b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
|
307 |
+
b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
|
308 |
+
else: # x1, y1, x2, y2 = box1
|
309 |
+
b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
|
310 |
+
b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
|
311 |
+
w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
|
312 |
+
w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
|
313 |
+
|
314 |
+
# Bottom edge distance (absolute value)
|
315 |
+
# xb = torch.abs(b2_x2 - b1_x1)
|
316 |
+
xb = torch.min(b2_x2-b1_x1, b1_x2-b2_x1)
|
317 |
+
xa = w2 - xb
|
318 |
+
xc = w1 - xb
|
319 |
+
ybe = torch.abs(b2_y2 - b1_y2)
|
320 |
+
|
321 |
+
X2 = xb/(xb+xa)
|
322 |
+
Y2 = 1-ybe/h2
|
323 |
+
|
324 |
+
X1 = xb/(xb+xa+xc+eps)
|
325 |
+
Y1 = 1-ybe/(torch.max(h2,h1)+eps)
|
326 |
+
|
327 |
+
bep = X1*Y1 if bep1 else X2*Y2
|
328 |
+
|
329 |
+
return bep
|
330 |
+
|
331 |
+
def bbox_iou(box1, box2, xywh=False, GIoU=False, DIoU=False, CIoU=False, eps=1e-7):
|
332 |
+
"""
|
333 |
+
Calculates IoU, GIoU, DIoU, or CIoU between two boxes, supporting xywh/xyxy formats.
|
334 |
+
|
335 |
+
Input shapes are box1(1,4) to box2(n,4).
|
336 |
+
"""
|
337 |
+
|
338 |
+
# Get the coordinates of bounding boxes
|
339 |
+
if xywh: # transform from xywh to xyxy
|
340 |
+
(x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1)
|
341 |
+
w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
|
342 |
+
b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
|
343 |
+
b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
|
344 |
+
else: # x1, y1, x2, y2 = box1
|
345 |
+
b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1)
|
346 |
+
b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1)
|
347 |
+
w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps)
|
348 |
+
w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps)
|
349 |
+
|
350 |
+
# Intersection area
|
351 |
+
inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * (
|
352 |
+
b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1)
|
353 |
+
).clamp(0)
|
354 |
+
|
355 |
+
# Union Area
|
356 |
+
union = w1 * h1 + w2 * h2 - inter + eps
|
357 |
+
|
358 |
+
# IoU
|
359 |
+
iou = inter / union
|
360 |
+
if CIoU or DIoU or GIoU:
|
361 |
+
cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width
|
362 |
+
ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height
|
363 |
+
if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
|
364 |
+
c2 = cw**2 + ch**2 + eps # convex diagonal squared
|
365 |
+
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center dist ** 2
|
366 |
+
if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
|
367 |
+
v = (4 / math.pi**2) * (torch.atan(w2 / h2) - torch.atan(w1 / h1)).pow(2)
|
368 |
+
with torch.no_grad():
|
369 |
+
alpha = v / (v - iou + (1 + eps))
|
370 |
+
return iou - (rho2 / c2 + v * alpha) # CIoU
|
371 |
+
return iou - rho2 / c2 # DIoU
|
372 |
+
c_area = cw * ch + eps # convex area
|
373 |
+
return iou - (c_area - union) / c_area # GIoU https://arxiv.org/pdf/1902.09630.pdf
|
374 |
+
return iou # IoU
|