Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

Linear sum assignment (#99)

Rory Clear92ec61072a82
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..f9c34de
--- /dev/null
+++ b/test.py
@@ -0,0 +1,32 @@
+import numpy as np
+from scipy.optimize import linear_sum_assignment as scipy_linear_sum_assignment
+from yolox.tracker.matching import linear_sum_assignment2
+import time
+
+def test_linear_sum_assigment():
+ for i in range(1,300):
+ input = np.random.rand(i,i)
+ a, b = linear_sum_assignment2(input)
+ c, d = scipy_linear_sum_assignment(input)
+ np.testing.assert_allclose(a,c)
+ np.testing.assert_allclose(b,d)
+
+def test_linear_sum_assigment_speed():
+ total_scipy = 0
+ total = 0
+ for i in range(1,300):
+ input = np.random.rand(i,i)
+ st = time.perf_counter()
+ linear_sum_assignment2(input)
+ t = time.perf_counter() - st
+ total += t
+ st = time.perf_counter()
+ scipy_linear_sum_assignment(input)
+ t_scipy = time.perf_counter() - st
+ total_scipy += t_scipy
+ print(t / t_scipy, i)
+ #np.testing.assert_(t < t_scipy*50, f"test_linear_sum_assigment_speed slow from {i}")
+ print("time vs scipy =", f"{(total / total_scipy) * 100:.1f}%")
+
+test_linear_sum_assigment()
+test_linear_sum_assigment_speed()
\ No newline at end of file
diff --git a/yolox/tracker/matching.py b/yolox/tracker/matching.py
index d2a3fd9..8b02531 100644
--- a/yolox/tracker/matching.py
+++ b/yolox/tracker/matching.py
@@ -35,6 +35,92 @@ def lapjv(cost: np.ndarray, cost_limit=np.inf):
y[c] = r
return x, y
+# todo try # https://github.com/ShawxingKwok/Kwok-algorithm/blob/main/Python.py
+def linear_sum_assignment2(cost): # 50x slower vs scipy
+ cost = np.asarray(cost, float)
+ nr, nc = cost.shape
+ u = np.zeros(nr)
+ v = np.zeros(nc)
+ shortest = np.full(nc, np.inf)
+ path = np.full(nc, -1, int)
+ col4row = np.full(nr, -1, int)
+ row4col = np.full(nc, -1, int)
+
+ for curRow in range(nr):
+ minVal = 0.0
+ remaining = np.arange(nc - 1, -1, -1)
+ num_remaining = nc
+
+ SR = np.zeros(nr, bool)
+ SC = np.zeros(nc, bool)
+ shortest[:] = np.inf
+
+ sink = -1
+ localRow = curRow
+
+ while sink < 0:
+ SR[localRow] = True
+ jlist = remaining[:num_remaining]
+ rcost = minVal + cost[localRow, jlist] - u[localRow] - v[jlist]
+ mask_better = rcost < shortest[jlist]
+ if mask_better.any():
+ idxs = jlist[mask_better]
+ shortest[idxs] = rcost[mask_better]
+ path[idxs] = localRow
+ best_idx = -1
+ lowest = np.inf
+ for idx_in_remaining, j in enumerate(jlist):
+ val = shortest[j]
+ if val < lowest or (val == lowest and row4col[j] == -1):
+ lowest = val
+ best_idx = idx_in_remaining
+ if lowest == np.inf:
+ return np.arange(nr), -np.ones(nr, int)
+
+ minVal = lowest
+ j = jlist[best_idx]
+
+ SC[j] = True
+
+ if row4col[j] == -1:
+ sink = j
+ else:
+ localRow = row4col[j]
+
+ remaining[best_idx], remaining[num_remaining - 1] = (
+ remaining[num_remaining - 1],
+ remaining[best_idx],
+ )
+ num_remaining -= 1
+
+ u[curRow] += minVal
+ SR_indices = np.nonzero(SR)[0]
+ for i in SR_indices:
+ if i == curRow:
+ continue
+ assigned_col = col4row[i]
+ if assigned_col >= 0:
+ u[i] += minVal - shortest[assigned_col]
+ else:
+ u[i] += minVal
+ SC_indices = np.nonzero(SC)[0]
+ for jidx in SC_indices:
+ v[jidx] -= minVal - shortest[jidx]
+
+ j = sink
+ while True:
+ i = path[j]
+ row4col[j] = i
+ prev = col4row[i]
+ col4row[i] = j
+ j = prev
+ if i == curRow:
+ break
+
+ a = np.arange(nr, dtype=int)
+ b = col4row.copy()
+ return a, b
+
def ious(atlbrs, btlbrs):
"""
Compute cost based on IoU