Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 27a0ef1a45 |
+66
-78
@@ -111,59 +111,60 @@ if HAS_NUMBA:
|
|||||||
return acc
|
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_greedy(
|
def _jit_score_bitmap_rescored_strided(
|
||||||
spread: np.ndarray,
|
spread: np.ndarray,
|
||||||
dx: np.ndarray, dy: np.ndarray, bins: np.ndarray,
|
dx: np.ndarray, dy: np.ndarray, bins: np.ndarray,
|
||||||
bit_active: np.uint8,
|
bit_active: np.uint8,
|
||||||
min_score: nb.float32,
|
bg: np.ndarray,
|
||||||
greediness: nb.float32,
|
stride: nb.int32,
|
||||||
) -> np.ndarray:
|
) -> np.ndarray:
|
||||||
"""Score bitmap con early-exit greedy (no rescore background).
|
"""Variante con sub-sampling: valuta solo pixel su griglia stride×stride.
|
||||||
|
Score restituito ha stessa shape (H, W); celle non valutate = 0.
|
||||||
|
|
||||||
Per ogni pixel iteriamo le N feature; abortiamo non appena diventa
|
4× speed-up con stride=2 (NMS recupera precisione in full-res).
|
||||||
impossibile raggiungere `min_required` count anche aggiungendo
|
Numba prange richiede step costante: itero su indici griglia e
|
||||||
tutte le feature rimanenti. min_required = greediness * min_score * N.
|
moltiplico per stride dentro il body.
|
||||||
|
|
||||||
greediness=0 → nessun early-exit (equivalente a kernel base).
|
|
||||||
greediness=1 → exit non appena hits + remaining < min_score * N.
|
|
||||||
Tipico: 0.7-0.9 → 2-4x speed-up senza perdere match.
|
|
||||||
"""
|
"""
|
||||||
H, W = spread.shape
|
H, W = spread.shape
|
||||||
N = dx.shape[0]
|
N = dx.shape[0]
|
||||||
acc = np.zeros((H, W), dtype=np.float32)
|
acc = np.zeros((H, W), dtype=np.float32)
|
||||||
if N == 0:
|
ny = (H + stride - 1) // stride
|
||||||
return acc
|
nx = (W + stride - 1) // stride
|
||||||
min_req = greediness * min_score * N
|
for yi in nb.prange(ny):
|
||||||
inv_N = nb.float32(1.0 / N)
|
y = yi * stride
|
||||||
for y in nb.prange(H):
|
for i in range(N):
|
||||||
for x in range(W):
|
b = bins[i]
|
||||||
hits = 0
|
mask = np.uint8(1) << b
|
||||||
for i in range(N):
|
if (bit_active & mask) == 0:
|
||||||
b = bins[i]
|
continue
|
||||||
mask = np.uint8(1) << b
|
ddy = dy[i]
|
||||||
if (bit_active & mask) == 0:
|
yy = y + ddy
|
||||||
# Nessun chance per questa feature
|
if yy < 0 or yy >= H:
|
||||||
if hits + (N - i - 1) < min_req:
|
continue
|
||||||
break
|
ddx = dx[i]
|
||||||
continue
|
x_lo = 0 if ddx >= 0 else -ddx
|
||||||
ddy = dy[i]
|
x_hi = W if ddx <= 0 else W - ddx
|
||||||
yy = y + ddy
|
rem = x_lo % stride
|
||||||
if yy < 0 or yy >= H:
|
if rem != 0:
|
||||||
if hits + (N - i - 1) < min_req:
|
x_lo += stride - rem
|
||||||
break
|
x = x_lo
|
||||||
continue
|
while x < x_hi:
|
||||||
ddx = dx[i]
|
if spread[yy, x + ddx] & mask:
|
||||||
xx = x + ddx
|
acc[y, x] += 1.0
|
||||||
if xx < 0 or xx >= W:
|
x += stride
|
||||||
if hits + (N - i - 1) < min_req:
|
if N > 0:
|
||||||
break
|
inv = 1.0 / N
|
||||||
continue
|
for yi in nb.prange(ny):
|
||||||
if spread[yy, xx] & mask:
|
y = yi * stride
|
||||||
hits += 1
|
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:
|
else:
|
||||||
if hits + (N - i - 1) < min_req:
|
acc[y, x] = 0.0
|
||||||
break
|
|
||||||
acc[y, x] = nb.float32(hits) * inv_N
|
|
||||||
return acc
|
return acc
|
||||||
|
|
||||||
@nb.njit(cache=True, parallel=True, fastmath=True, boundscheck=False)
|
@nb.njit(cache=True, parallel=True, fastmath=True, boundscheck=False)
|
||||||
@@ -241,9 +242,8 @@ 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_greedy(
|
_jit_score_bitmap_rescored_strided(
|
||||||
spread, dx, dy, b, np.uint8(0xFF),
|
spread, dx, dy, b, np.uint8(0xFF), bg, np.int32(2),
|
||||||
np.float32(0.5), np.float32(0.8),
|
|
||||||
)
|
)
|
||||||
_jit_popcount_density(spread)
|
_jit_popcount_density(spread)
|
||||||
|
|
||||||
@@ -258,7 +258,7 @@ 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_greedy(spread, dx, dy, bins, bit_active, min_score, greediness):
|
def _jit_score_bitmap_rescored_strided(spread, dx, dy, bins, bit_active, bg, stride):
|
||||||
raise RuntimeError("numba non disponibile")
|
raise RuntimeError("numba non disponibile")
|
||||||
|
|
||||||
def _jit_popcount_density(spread):
|
def _jit_popcount_density(spread):
|
||||||
@@ -291,46 +291,34 @@ 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,
|
bit_active: int, bg: np.ndarray, stride: int = 1,
|
||||||
) -> 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(
|
||||||
np.ascontiguousarray(spread, dtype=np.uint8),
|
spread_c, dx_c, dy_c, bins_c, np.uint8(bit_active), bg_c,
|
||||||
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
|
# Fallback: chiamate separate (stride ignorato in fallback)
|
||||||
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)
|
||||||
|
|
||||||
|
|
||||||
def score_bitmap_greedy(
|
|
||||||
spread: np.ndarray, dx: np.ndarray, dy: np.ndarray, bins: np.ndarray,
|
|
||||||
bit_active: int, min_score: float, greediness: float,
|
|
||||||
) -> np.ndarray:
|
|
||||||
"""Score bitmap con early-exit greedy. Per coarse-pass aggressivo.
|
|
||||||
|
|
||||||
Non applica rescore background: usare quando la scena ha basso clutter
|
|
||||||
o quando si vuole mass-prune varianti via top-level rapidamente.
|
|
||||||
"""
|
|
||||||
if HAS_NUMBA and len(dx) > 0:
|
|
||||||
return _jit_score_bitmap_greedy(
|
|
||||||
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.float32(min_score), np.float32(greediness),
|
|
||||||
)
|
|
||||||
# Fallback: kernel base senza early-exit
|
|
||||||
return score_bitmap(spread, dx, dy, bins, bit_active)
|
|
||||||
|
|
||||||
|
|
||||||
def popcount_density(spread: np.ndarray) -> np.ndarray:
|
def popcount_density(spread: np.ndarray) -> np.ndarray:
|
||||||
if HAS_NUMBA:
|
if HAS_NUMBA:
|
||||||
return _jit_popcount_density(np.ascontiguousarray(spread, dtype=np.uint8))
|
return _jit_popcount_density(np.ascontiguousarray(spread, dtype=np.uint8))
|
||||||
|
|||||||
+7
-15
@@ -40,7 +40,6 @@ from pm2d._jit_kernels import (
|
|||||||
score_by_shift as _jit_score_by_shift,
|
score_by_shift as _jit_score_by_shift,
|
||||||
score_bitmap as _jit_score_bitmap,
|
score_bitmap as _jit_score_bitmap,
|
||||||
score_bitmap_rescored as _jit_score_bitmap_rescored,
|
score_bitmap_rescored as _jit_score_bitmap_rescored,
|
||||||
score_bitmap_greedy as _jit_score_bitmap_greedy,
|
|
||||||
popcount_density as _jit_popcount,
|
popcount_density as _jit_popcount,
|
||||||
HAS_NUMBA,
|
HAS_NUMBA,
|
||||||
)
|
)
|
||||||
@@ -574,8 +573,8 @@ 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,
|
||||||
greediness: float = 0.0,
|
|
||||||
) -> 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:
|
||||||
@@ -648,23 +647,16 @@ class LineShapeMatcher:
|
|||||||
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) - solo coarse.
|
||||||
# greediness > 0: usa kernel greedy con early-exit (no rescore bg)
|
# coarse_stride > 1: valuta solo 1 pixel ogni stride, ~stride² speed-up.
|
||||||
# per il pruning. ~2-4x speed-up sul top con greediness=0.8.
|
cs = max(1, int(coarse_stride))
|
||||||
use_greedy_top = greediness > 0.0
|
|
||||||
|
|
||||||
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)]
|
||||||
if use_greedy_top:
|
score = _jit_score_bitmap_rescored(
|
||||||
score = _jit_score_bitmap_greedy(
|
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,
|
||||||
top_thresh, greediness,
|
)
|
||||||
)
|
|
||||||
else:
|
|
||||||
score = _jit_score_bitmap_rescored(
|
|
||||||
spread_top, lvl.dx, lvl.dy, lvl.bin, bit_active_top,
|
|
||||||
bg_cache_top[var.scale],
|
|
||||||
)
|
|
||||||
return vi, float(score.max()) if score.size else -1.0
|
return vi, float(score.max()) if score.size else -1.0
|
||||||
|
|
||||||
kept_coarse: list[tuple[int, float]] = []
|
kept_coarse: list[tuple[int, float]] = []
|
||||||
|
|||||||
Reference in New Issue
Block a user