Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6db2086ead |
+9
-82
@@ -110,63 +110,6 @@ if HAS_NUMBA:
|
|||||||
acc[y, x] *= inv
|
acc[y, x] *= inv
|
||||||
return acc
|
return acc
|
||||||
|
|
||||||
@nb.njit(cache=True, parallel=True, fastmath=True, boundscheck=False)
|
|
||||||
def _jit_score_bitmap_rescored_strided(
|
|
||||||
spread: np.ndarray,
|
|
||||||
dx: np.ndarray, dy: np.ndarray, bins: np.ndarray,
|
|
||||||
bit_active: np.uint8,
|
|
||||||
bg: np.ndarray,
|
|
||||||
stride: nb.int32,
|
|
||||||
) -> np.ndarray:
|
|
||||||
"""Variante con sub-sampling: valuta solo pixel su griglia stride×stride.
|
|
||||||
Score restituito ha stessa shape (H, W); celle non valutate = 0.
|
|
||||||
|
|
||||||
4× speed-up con stride=2 (NMS recupera precisione in full-res).
|
|
||||||
Numba prange richiede step costante: itero su indici griglia e
|
|
||||||
moltiplico per stride dentro il body.
|
|
||||||
"""
|
|
||||||
H, W = spread.shape
|
|
||||||
N = dx.shape[0]
|
|
||||||
acc = np.zeros((H, W), dtype=np.float32)
|
|
||||||
ny = (H + stride - 1) // stride
|
|
||||||
nx = (W + stride - 1) // stride
|
|
||||||
for yi in nb.prange(ny):
|
|
||||||
y = yi * stride
|
|
||||||
for i in range(N):
|
|
||||||
b = bins[i]
|
|
||||||
mask = np.uint8(1) << b
|
|
||||||
if (bit_active & mask) == 0:
|
|
||||||
continue
|
|
||||||
ddy = dy[i]
|
|
||||||
yy = y + ddy
|
|
||||||
if yy < 0 or yy >= H:
|
|
||||||
continue
|
|
||||||
ddx = dx[i]
|
|
||||||
x_lo = 0 if ddx >= 0 else -ddx
|
|
||||||
x_hi = W if ddx <= 0 else W - ddx
|
|
||||||
rem = x_lo % stride
|
|
||||||
if rem != 0:
|
|
||||||
x_lo += stride - rem
|
|
||||||
x = x_lo
|
|
||||||
while x < x_hi:
|
|
||||||
if spread[yy, x + ddx] & mask:
|
|
||||||
acc[y, x] += 1.0
|
|
||||||
x += stride
|
|
||||||
if N > 0:
|
|
||||||
inv = 1.0 / N
|
|
||||||
for yi in nb.prange(ny):
|
|
||||||
y = yi * stride
|
|
||||||
for xi in range(nx):
|
|
||||||
x = xi * stride
|
|
||||||
v = acc[y, x] * inv
|
|
||||||
bgv = bg[y, x]
|
|
||||||
if bgv < 1.0:
|
|
||||||
r = (v - bgv) / (1.0 - bgv + 1e-6)
|
|
||||||
acc[y, x] = r if r > 0.0 else 0.0
|
|
||||||
else:
|
|
||||||
acc[y, x] = 0.0
|
|
||||||
return acc
|
|
||||||
|
|
||||||
@nb.njit(cache=True, parallel=True, fastmath=True, boundscheck=False)
|
@nb.njit(cache=True, parallel=True, fastmath=True, boundscheck=False)
|
||||||
def _jit_score_bitmap_rescored(
|
def _jit_score_bitmap_rescored(
|
||||||
spread: np.ndarray, # uint8 (H, W)
|
spread: np.ndarray, # uint8 (H, W)
|
||||||
@@ -242,9 +185,6 @@ if HAS_NUMBA:
|
|||||||
_jit_score_bitmap(spread, dx, dy, b, np.uint8(0xFF))
|
_jit_score_bitmap(spread, dx, dy, b, np.uint8(0xFF))
|
||||||
bg = np.zeros((32, 32), dtype=np.float32)
|
bg = np.zeros((32, 32), dtype=np.float32)
|
||||||
_jit_score_bitmap_rescored(spread, dx, dy, b, np.uint8(0xFF), bg)
|
_jit_score_bitmap_rescored(spread, dx, dy, b, np.uint8(0xFF), bg)
|
||||||
_jit_score_bitmap_rescored_strided(
|
|
||||||
spread, dx, dy, b, np.uint8(0xFF), bg, np.int32(2),
|
|
||||||
)
|
|
||||||
_jit_popcount_density(spread)
|
_jit_popcount_density(spread)
|
||||||
|
|
||||||
else: # pragma: no cover
|
else: # pragma: no cover
|
||||||
@@ -258,9 +198,6 @@ else: # pragma: no cover
|
|||||||
def _jit_score_bitmap_rescored(spread, dx, dy, bins, bit_active, bg):
|
def _jit_score_bitmap_rescored(spread, dx, dy, bins, bit_active, bg):
|
||||||
raise RuntimeError("numba non disponibile")
|
raise RuntimeError("numba non disponibile")
|
||||||
|
|
||||||
def _jit_score_bitmap_rescored_strided(spread, dx, dy, bins, bit_active, bg, stride):
|
|
||||||
raise RuntimeError("numba non disponibile")
|
|
||||||
|
|
||||||
def _jit_popcount_density(spread):
|
def _jit_popcount_density(spread):
|
||||||
raise RuntimeError("numba non disponibile")
|
raise RuntimeError("numba non disponibile")
|
||||||
|
|
||||||
@@ -291,29 +228,19 @@ def score_bitmap(
|
|||||||
|
|
||||||
def score_bitmap_rescored(
|
def score_bitmap_rescored(
|
||||||
spread: np.ndarray, dx: np.ndarray, dy: np.ndarray, bins: np.ndarray,
|
spread: np.ndarray, dx: np.ndarray, dy: np.ndarray, bins: np.ndarray,
|
||||||
bit_active: int, bg: np.ndarray, stride: int = 1,
|
bit_active: int, bg: np.ndarray,
|
||||||
) -> np.ndarray:
|
) -> np.ndarray:
|
||||||
"""Score bitmap + rescore fusi in un solo pass (JIT).
|
"""Score bitmap + rescore fusi in un solo pass (JIT)."""
|
||||||
|
|
||||||
stride > 1: valuta solo pixel su griglia stride×stride. Le celle non
|
|
||||||
valutate restano 0 nello score map. Pensato per coarse-pass al top
|
|
||||||
della piramide; il refinement full-res poi recupera precisione.
|
|
||||||
"""
|
|
||||||
if HAS_NUMBA and len(dx) > 0:
|
if HAS_NUMBA and len(dx) > 0:
|
||||||
spread_c = np.ascontiguousarray(spread, dtype=np.uint8)
|
|
||||||
dx_c = np.ascontiguousarray(dx, dtype=np.int32)
|
|
||||||
dy_c = np.ascontiguousarray(dy, dtype=np.int32)
|
|
||||||
bins_c = np.ascontiguousarray(bins, dtype=np.int8)
|
|
||||||
bg_c = np.ascontiguousarray(bg, dtype=np.float32)
|
|
||||||
if stride > 1:
|
|
||||||
return _jit_score_bitmap_rescored_strided(
|
|
||||||
spread_c, dx_c, dy_c, bins_c, np.uint8(bit_active), bg_c,
|
|
||||||
np.int32(stride),
|
|
||||||
)
|
|
||||||
return _jit_score_bitmap_rescored(
|
return _jit_score_bitmap_rescored(
|
||||||
spread_c, dx_c, dy_c, bins_c, np.uint8(bit_active), bg_c,
|
np.ascontiguousarray(spread, dtype=np.uint8),
|
||||||
|
np.ascontiguousarray(dx, dtype=np.int32),
|
||||||
|
np.ascontiguousarray(dy, dtype=np.int32),
|
||||||
|
np.ascontiguousarray(bins, dtype=np.int8),
|
||||||
|
np.uint8(bit_active),
|
||||||
|
np.ascontiguousarray(bg, dtype=np.float32),
|
||||||
)
|
)
|
||||||
# Fallback: chiamate separate (stride ignorato in fallback)
|
# Fallback: chiamate separate
|
||||||
score = score_bitmap(spread, dx, dy, bins, bit_active)
|
score = score_bitmap(spread, dx, dy, bins, bit_active)
|
||||||
out = (score - bg) / (1.0 - bg + 1e-6)
|
out = (score - bg) / (1.0 - bg + 1e-6)
|
||||||
return np.maximum(0.0, out).astype(np.float32)
|
return np.maximum(0.0, out).astype(np.float32)
|
||||||
|
|||||||
+61
-8
@@ -573,8 +573,9 @@ class LineShapeMatcher:
|
|||||||
verify_ncc: bool = True,
|
verify_ncc: bool = True,
|
||||||
verify_threshold: float = 0.4,
|
verify_threshold: float = 0.4,
|
||||||
coarse_angle_factor: int = 2,
|
coarse_angle_factor: int = 2,
|
||||||
coarse_stride: int = 1,
|
|
||||||
scale_penalty: float = 0.0,
|
scale_penalty: float = 0.0,
|
||||||
|
pyramid_propagate: bool = True,
|
||||||
|
propagate_topk: int = 8,
|
||||||
) -> list[Match]:
|
) -> list[Match]:
|
||||||
"""
|
"""
|
||||||
scale_penalty: se > 0, riduce lo score per match a scala diversa da 1.0:
|
scale_penalty: se > 0, riduce lo score per match a scala diversa da 1.0:
|
||||||
@@ -646,18 +647,36 @@ class LineShapeMatcher:
|
|||||||
end = min(n, i + half + 1)
|
end = min(n, i + half + 1)
|
||||||
neighbor_map[vi_c] = vi_sorted[start:end]
|
neighbor_map[vi_c] = vi_sorted[start:end]
|
||||||
|
|
||||||
# Pruning varianti via top-level (parallelizzato) - solo coarse.
|
# Pruning varianti via top-level (parallelizzato).
|
||||||
# coarse_stride > 1: valuta solo 1 pixel ogni stride, ~stride² speed-up.
|
# Quando pyramid_propagate=True ritorna anche le top-K posizioni
|
||||||
cs = max(1, int(coarse_stride))
|
# del picco (in coord top-level) per restringere la fase full-res
|
||||||
|
# a piccoli crop attorno ai candidati (vs intera scena).
|
||||||
|
peaks_by_vi: dict[int, list[tuple[int, int, float]]] = {}
|
||||||
|
|
||||||
def _top_score(vi: int) -> tuple[int, float]:
|
def _top_score(vi: int) -> tuple[int, float]:
|
||||||
var = self.variants[vi]
|
var = self.variants[vi]
|
||||||
lvl = var.levels[min(top, len(var.levels) - 1)]
|
lvl = var.levels[min(top, len(var.levels) - 1)]
|
||||||
score = _jit_score_bitmap_rescored(
|
score = _jit_score_bitmap_rescored(
|
||||||
spread_top, lvl.dx, lvl.dy, lvl.bin, bit_active_top,
|
spread_top, lvl.dx, lvl.dy, lvl.bin, bit_active_top,
|
||||||
bg_cache_top[var.scale], stride=cs,
|
bg_cache_top[var.scale],
|
||||||
)
|
)
|
||||||
return vi, float(score.max()) if score.size else -1.0
|
if score.size == 0:
|
||||||
|
return vi, -1.0
|
||||||
|
best = float(score.max())
|
||||||
|
if pyramid_propagate and best > 0:
|
||||||
|
# Top-K posizioni > top_thresh (max propagate_topk)
|
||||||
|
flat = score.ravel()
|
||||||
|
k = min(propagate_topk, flat.size)
|
||||||
|
idx = np.argpartition(-flat, k - 1)[:k]
|
||||||
|
peaks: list[tuple[int, int, float]] = []
|
||||||
|
for i in idx:
|
||||||
|
s = float(flat[i])
|
||||||
|
if s < top_thresh * 0.7:
|
||||||
|
continue
|
||||||
|
yt, xt = int(i // score.shape[1]), int(i % score.shape[1])
|
||||||
|
peaks.append((xt, yt, s))
|
||||||
|
peaks_by_vi[vi] = peaks
|
||||||
|
return vi, best
|
||||||
|
|
||||||
kept_coarse: list[tuple[int, float]] = []
|
kept_coarse: list[tuple[int, float]] = []
|
||||||
all_top_scores: list[tuple[int, float]] = []
|
all_top_scores: list[tuple[int, float]] = []
|
||||||
@@ -717,14 +736,48 @@ class LineShapeMatcher:
|
|||||||
for sc in unique_scales:
|
for sc in unique_scales:
|
||||||
bg_cache_full[sc] = _bg_for_scale(density_full, sc, 1)
|
bg_cache_full[sc] = _bg_for_scale(density_full, sc, 1)
|
||||||
|
|
||||||
|
# Margine in full-res attorno ad ogni peak top: copre incertezza
|
||||||
|
# downsampling (sf_top px) + spread_radius + slack per NMS.
|
||||||
|
propagate_margin = sf_top + self.spread_radius + max(8, nms_radius // 2)
|
||||||
|
H_full, W_full = spread0.shape
|
||||||
|
|
||||||
def _full_score(vi: int) -> tuple[int, np.ndarray]:
|
def _full_score(vi: int) -> tuple[int, np.ndarray]:
|
||||||
var = self.variants[vi]
|
var = self.variants[vi]
|
||||||
lvl0 = var.levels[0]
|
lvl0 = var.levels[0]
|
||||||
score = _jit_score_bitmap_rescored(
|
if not pyramid_propagate or vi not in peaks_by_vi or not peaks_by_vi[vi]:
|
||||||
|
# Path legacy: scansiona intera scena
|
||||||
|
return vi, _jit_score_bitmap_rescored(
|
||||||
spread0, lvl0.dx, lvl0.dy, lvl0.bin, bit_active_full,
|
spread0, lvl0.dx, lvl0.dy, lvl0.bin, bit_active_full,
|
||||||
bg_cache_full[var.scale],
|
bg_cache_full[var.scale],
|
||||||
)
|
)
|
||||||
return vi, score
|
# Path piramide propagata: valuta solo crop locali attorno
|
||||||
|
# alle posizioni dei picchi top-level (riproiettati a full-res).
|
||||||
|
score_full = np.zeros((H_full, W_full), dtype=np.float32)
|
||||||
|
mark = np.zeros((H_full, W_full), dtype=bool)
|
||||||
|
bg = bg_cache_full[var.scale]
|
||||||
|
for xt, yt, _s in peaks_by_vi[vi]:
|
||||||
|
cx0 = xt * sf_top
|
||||||
|
cy0 = yt * sf_top
|
||||||
|
x_lo = max(0, cx0 - propagate_margin)
|
||||||
|
x_hi = min(W_full, cx0 + propagate_margin + 1)
|
||||||
|
y_lo = max(0, cy0 - propagate_margin)
|
||||||
|
y_hi = min(H_full, cy0 + propagate_margin + 1)
|
||||||
|
if x_hi <= x_lo or y_hi <= y_lo:
|
||||||
|
continue
|
||||||
|
if mark[y_lo:y_hi, x_lo:x_hi].all():
|
||||||
|
continue
|
||||||
|
# Crop spread + bg, valuta kernel sul crop
|
||||||
|
spread_crop = np.ascontiguousarray(spread0[y_lo:y_hi, x_lo:x_hi])
|
||||||
|
bg_crop = np.ascontiguousarray(bg[y_lo:y_hi, x_lo:x_hi])
|
||||||
|
score_crop = _jit_score_bitmap_rescored(
|
||||||
|
spread_crop, lvl0.dx, lvl0.dy, lvl0.bin,
|
||||||
|
bit_active_full, bg_crop,
|
||||||
|
)
|
||||||
|
score_full[y_lo:y_hi, x_lo:x_hi] = np.maximum(
|
||||||
|
score_full[y_lo:y_hi, x_lo:x_hi], score_crop,
|
||||||
|
)
|
||||||
|
mark[y_lo:y_hi, x_lo:x_hi] = True
|
||||||
|
return vi, score_full
|
||||||
|
|
||||||
candidates_per_var: list[tuple[int, np.ndarray]] = []
|
candidates_per_var: list[tuple[int, np.ndarray]] = []
|
||||||
raw: list[tuple[float, int, int, int]] = []
|
raw: list[tuple[float, int, int, int]] = []
|
||||||
|
|||||||
Reference in New Issue
Block a user