Spaces:
Runtime error
Runtime error
Khunndev Sisyphus commited on
Commit ·
1f3b2fa
1
Parent(s): 4608266
Make server centroid dedup O(n) and tune dedup thresholds.
Browse filesUltraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
- postprocess.py +32 -25
postprocess.py
CHANGED
|
@@ -24,9 +24,9 @@ IOU_THRESHOLD = 0.45
|
|
| 24 |
IOU_THRESHOLD_DENSE = 0.52
|
| 25 |
IOU_RECALL_RAW_COUNT = 1500
|
| 26 |
MAX_DET = 6000
|
| 27 |
-
CENTROID_MERGE_NN_FACTOR = 0.
|
| 28 |
-
IOS_NMS_THRESHOLD = 0.
|
| 29 |
-
END_ASPECT_RATIO_MAX =
|
| 30 |
|
| 31 |
|
| 32 |
def size_diversity_ratio(boxes: list) -> float:
|
|
@@ -144,13 +144,19 @@ def merge_by_median_nn(
|
|
| 144 |
) -> tuple[list, list, dict]:
|
| 145 |
"""Centroid dedup using a nearest-neighbour median threshold.
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
"""
|
| 151 |
import numpy as np
|
| 152 |
|
| 153 |
-
|
|
|
|
| 154 |
return boxes, scores, {"merged": 0, "median_nn": None}
|
| 155 |
|
| 156 |
boxes_arr = np.asarray(boxes, dtype=np.float32)
|
|
@@ -158,31 +164,32 @@ def merge_by_median_nn(
|
|
| 158 |
cy = (boxes_arr[:, 1] + boxes_arr[:, 3]) / 2
|
| 159 |
centroids = np.stack([cx, cy], axis=1)
|
| 160 |
|
| 161 |
-
#
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
| 166 |
median_nn = float(np.median(nn))
|
| 167 |
merge_threshold = CENTROID_MERGE_NN_FACTOR * median_nn
|
| 168 |
thresh2 = merge_threshold * merge_threshold
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
| 172 |
kept_idx: list[int] = []
|
| 173 |
-
kept_centroids: list = []
|
| 174 |
for i in order:
|
| 175 |
ci = centroids[i]
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
if not duplicate:
|
| 184 |
-
kept_idx.append(i)
|
| 185 |
-
kept_centroids.append(ci)
|
| 186 |
|
| 187 |
kept_boxes = [boxes[i] for i in kept_idx]
|
| 188 |
kept_scores = [scores[i] for i in kept_idx]
|
|
|
|
| 24 |
IOU_THRESHOLD_DENSE = 0.52
|
| 25 |
IOU_RECALL_RAW_COUNT = 1500
|
| 26 |
MAX_DET = 6000
|
| 27 |
+
CENTROID_MERGE_NN_FACTOR = 0.4
|
| 28 |
+
IOS_NMS_THRESHOLD = 0.7
|
| 29 |
+
END_ASPECT_RATIO_MAX = 3.5
|
| 30 |
|
| 31 |
|
| 32 |
def size_diversity_ratio(boxes: list) -> float:
|
|
|
|
| 144 |
) -> tuple[list, list, dict]:
|
| 145 |
"""Centroid dedup using a nearest-neighbour median threshold.
|
| 146 |
|
| 147 |
+
Boxes whose centroid is within ``CENTROID_MERGE_NN_FACTOR * median_nn`` of an
|
| 148 |
+
already kept (higher-score) box are treated as duplicates and dropped.
|
| 149 |
+
|
| 150 |
+
Performance: the median NN distance is estimated from an evenly-spaced sample
|
| 151 |
+
of centroids (O(K*n)) instead of a full O(n^2) pairwise matrix, and the greedy
|
| 152 |
+
dedup checks each candidate against all kept-so-far in a single vectorised op
|
| 153 |
+
(preallocated buffer, no per-step copy). On dense bundles (2000+ pipes) the
|
| 154 |
+
old pairwise matrix + Python loop took seconds; this keeps it in tens of ms.
|
| 155 |
"""
|
| 156 |
import numpy as np
|
| 157 |
|
| 158 |
+
n = len(boxes)
|
| 159 |
+
if n < 3:
|
| 160 |
return boxes, scores, {"merged": 0, "median_nn": None}
|
| 161 |
|
| 162 |
boxes_arr = np.asarray(boxes, dtype=np.float32)
|
|
|
|
| 164 |
cy = (boxes_arr[:, 1] + boxes_arr[:, 3]) / 2
|
| 165 |
centroids = np.stack([cx, cy], axis=1)
|
| 166 |
|
| 167 |
+
# Median NN distance from an evenly-spaced sample -> O(K*n), no n^2 matrix.
|
| 168 |
+
sample_idx = np.unique(np.linspace(0, n - 1, min(256, n)).astype(np.int32))
|
| 169 |
+
k = len(sample_idx)
|
| 170 |
+
diff = centroids[sample_idx][:, None, :] - centroids[None, :, :] # (k,n,2)
|
| 171 |
+
dist2 = np.sum(diff * diff, axis=2) # (k,n)
|
| 172 |
+
dist2[np.arange(k), sample_idx] = np.inf # exclude self
|
| 173 |
+
nn = np.sqrt(dist2.min(axis=1))
|
| 174 |
median_nn = float(np.median(nn))
|
| 175 |
merge_threshold = CENTROID_MERGE_NN_FACTOR * median_nn
|
| 176 |
thresh2 = merge_threshold * merge_threshold
|
| 177 |
|
| 178 |
+
# Greedy dedup in score order; each candidate checks all kept so far in ONE
|
| 179 |
+
# vectorised op (preallocated buffer, no per-step copy).
|
| 180 |
+
order = np.argsort(scores)[::-1]
|
| 181 |
+
kept_buf = np.empty((n, 2), dtype=np.float32)
|
| 182 |
+
kept_count = 0
|
| 183 |
kept_idx: list[int] = []
|
|
|
|
| 184 |
for i in order:
|
| 185 |
ci = centroids[i]
|
| 186 |
+
if kept_count:
|
| 187 |
+
d = kept_buf[:kept_count] - ci
|
| 188 |
+
if (d[:, 0] * d[:, 0] + d[:, 1] * d[:, 1] < thresh2).any():
|
| 189 |
+
continue
|
| 190 |
+
kept_buf[kept_count] = ci
|
| 191 |
+
kept_count += 1
|
| 192 |
+
kept_idx.append(int(i))
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
kept_boxes = [boxes[i] for i in kept_idx]
|
| 195 |
kept_scores = [scores[i] for i in kept_idx]
|