Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 直接提供bbox_iou函数,以取消Cython依赖 #1040

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ venv.bak/
# mypy
.mypy_cache/
.vscode
.idea
.tensorboard
exp/coco*
*.pth
Expand All @@ -114,5 +115,5 @@ data/
tmp/
exp/json
tmp_*/
example/res/
examples/res/
data/
39 changes: 35 additions & 4 deletions detector/tracker/tracker/matching.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
import cv2
import numpy as np
import torch
import math

import scipy
from scipy.spatial.distance import cdist
from scipy.optimize import linear_sum_assignment

from cython_bbox import bbox_overlaps as bbox_ious
from tracker.utils import kalman_filter
import time
from trackers.utils import kalman_filter

def bbox_iou(box1, box2, eps=1e-7): # CIoU
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
box2 = box2.T

# Get the coordinates of bounding boxes
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]

# Intersection area
inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
(torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)

# Union Area
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
union = w1 * h1 + w2 * h2 - inter + eps

iou = inter / union
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height

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 distance squared

v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
with torch.no_grad():
alpha = v / (v - iou + (1 + eps))
return iou - (rho2 / c2 + v * alpha) # CIoU

def merge_matches(m1, m2, shape):
O,P,Q = shape
Expand Down Expand Up @@ -65,7 +96,7 @@ def ious(atlbrs, btlbrs):
if ious.size == 0:
return ious

ious = bbox_ious(
ious = bbox_iou(
np.ascontiguousarray(atlbrs, dtype=np.float),
np.ascontiguousarray(btlbrs, dtype=np.float)
)
Expand Down
4 changes: 0 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,3 @@ def is_installed(package_name):
print("\nInstall third-party pycocotools for Windows...")
cmd = 'python -m pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI'
os.system(cmd)
if not is_installed('cython_bbox'):
print("\nInstall `cython_bbox`...")
cmd = 'python -m pip install git+https://github.com/yanfengliu/cython_bbox.git'
os.system(cmd)
37 changes: 34 additions & 3 deletions trackers/tracking/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,40 @@
from scipy.spatial.distance import cdist
from scipy.optimize import linear_sum_assignment

from cython_bbox import bbox_overlaps as bbox_ious
from trackers.utils import kalman_filter
import time
import math
import torch

def bbox_iou(box1, box2, eps=1e-7): # CIoU
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
box2 = box2.T

# Get the coordinates of bounding boxes
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]

# Intersection area
inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
(torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)

# Union Area
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
union = w1 * h1 + w2 * h2 - inter + eps

iou = inter / union
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height

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 distance squared

v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
with torch.no_grad():
alpha = v / (v - iou + (1 + eps))
return iou - (rho2 / c2 + v * alpha) # CIoU


def merge_matches(m1, m2, shape):
O,P,Q = shape
Expand Down Expand Up @@ -65,7 +96,7 @@ def ious(atlbrs, btlbrs):
if ious.size == 0:
return ious

ious = bbox_ious(
ious = bbox_iou(
np.ascontiguousarray(atlbrs, dtype=np.float),
np.ascontiguousarray(btlbrs, dtype=np.float)
)
Expand Down