Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4419c237b2 |
@@ -110,6 +110,62 @@ 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_greedy(
|
||||||
|
spread: np.ndarray,
|
||||||
|
dx: np.ndarray, dy: np.ndarray, bins: np.ndarray,
|
||||||
|
bit_active: np.uint8,
|
||||||
|
min_score: nb.float32,
|
||||||
|
greediness: nb.float32,
|
||||||
|
) -> np.ndarray:
|
||||||
|
"""Score bitmap con early-exit greedy (no rescore background).
|
||||||
|
|
||||||
|
Per ogni pixel iteriamo le N feature; abortiamo non appena diventa
|
||||||
|
impossibile raggiungere `min_required` count anche aggiungendo
|
||||||
|
tutte le feature rimanenti. min_required = greediness * min_score * N.
|
||||||
|
|
||||||
|
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
|
||||||
|
N = dx.shape[0]
|
||||||
|
acc = np.zeros((H, W), dtype=np.float32)
|
||||||
|
if N == 0:
|
||||||
|
return acc
|
||||||
|
min_req = greediness * min_score * N
|
||||||
|
inv_N = nb.float32(1.0 / N)
|
||||||
|
for y in nb.prange(H):
|
||||||
|
for x in range(W):
|
||||||
|
hits = 0
|
||||||
|
for i in range(N):
|
||||||
|
b = bins[i]
|
||||||
|
mask = np.uint8(1) << b
|
||||||
|
if (bit_active & mask) == 0:
|
||||||
|
# Nessun chance per questa feature
|
||||||
|
if hits + (N - i - 1) < min_req:
|
||||||
|
break
|
||||||
|
continue
|
||||||
|
ddy = dy[i]
|
||||||
|
yy = y + ddy
|
||||||
|
if yy < 0 or yy >= H:
|
||||||
|
if hits + (N - i - 1) < min_req:
|
||||||
|
break
|
||||||
|
continue
|
||||||
|
ddx = dx[i]
|
||||||
|
xx = x + ddx
|
||||||
|
if xx < 0 or xx >= W:
|
||||||
|
if hits + (N - i - 1) < min_req:
|
||||||
|
break
|
||||||
|
continue
|
||||||
|
if spread[yy, xx] & mask:
|
||||||
|
hits += 1
|
||||||
|
else:
|
||||||
|
if hits + (N - i - 1) < min_req:
|
||||||
|
break
|
||||||
|
acc[y, x] = nb.float32(hits) * inv_N
|
||||||
|
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)
|
||||||
@@ -185,6 +241,10 @@ 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(
|
||||||
|
spread, dx, dy, b, np.uint8(0xFF),
|
||||||
|
np.float32(0.5), np.float32(0.8),
|
||||||
|
)
|
||||||
_jit_popcount_density(spread)
|
_jit_popcount_density(spread)
|
||||||
|
|
||||||
else: # pragma: no cover
|
else: # pragma: no cover
|
||||||
@@ -198,6 +258,9 @@ 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):
|
||||||
|
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")
|
||||||
|
|
||||||
@@ -246,6 +309,28 @@ def score_bitmap_rescored(
|
|||||||
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))
|
||||||
|
|||||||
+13
-22
@@ -40,6 +40,7 @@ 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,
|
||||||
)
|
)
|
||||||
@@ -239,8 +240,6 @@ class LineShapeMatcher:
|
|||||||
self._train_mask = mask_full.copy()
|
self._train_mask = mask_full.copy()
|
||||||
|
|
||||||
self.variants.clear()
|
self.variants.clear()
|
||||||
# Invalida cache feature di refine: il template e cambiato.
|
|
||||||
self._refine_feat_cache = {}
|
|
||||||
for s in self._scale_list():
|
for s in self._scale_list():
|
||||||
sw = max(16, int(round(w * s)))
|
sw = max(16, int(round(w * s)))
|
||||||
sh = max(16, int(round(h * s)))
|
sh = max(16, int(round(h * s)))
|
||||||
@@ -435,24 +434,9 @@ class LineShapeMatcher:
|
|||||||
H, W = spread0.shape
|
H, W = spread0.shape
|
||||||
margin = 3
|
margin = 3
|
||||||
|
|
||||||
# Cache template features per angolo (chiave: int(round(ang*20)) =
|
|
||||||
# bucket di 0.05°). Golden-search ricontratto puo richiedere lo
|
|
||||||
# stesso bucket piu volte; evita re-warp+gradient+extract (costoso).
|
|
||||||
# Cache a livello matcher per riusare tra chiamate find() su scene
|
|
||||||
# diverse: la rotazione del template non dipende dalla scena.
|
|
||||||
if not hasattr(self, '_refine_feat_cache'):
|
|
||||||
self._refine_feat_cache = {}
|
|
||||||
feat_cache = self._refine_feat_cache
|
|
||||||
cache_scale_key = round(scale * 1000)
|
|
||||||
|
|
||||||
def _score_at_angle(off: float) -> tuple[float, float, float]:
|
def _score_at_angle(off: float) -> tuple[float, float, float]:
|
||||||
"""Ritorna (score, best_cx, best_cy) per angolo = angle_deg + off."""
|
"""Ritorna (score, best_cx, best_cy) per angolo = angle_deg + off."""
|
||||||
ang = angle_deg + off
|
ang = angle_deg + off
|
||||||
ck = (round(ang * 20), cache_scale_key)
|
|
||||||
cached = feat_cache.get(ck)
|
|
||||||
if cached is not None:
|
|
||||||
fx, fy, fb = cached
|
|
||||||
else:
|
|
||||||
M = cv2.getRotationMatrix2D(center, ang, 1.0)
|
M = cv2.getRotationMatrix2D(center, ang, 1.0)
|
||||||
gray_r = cv2.warpAffine(gray_p, M, (diag, diag),
|
gray_r = cv2.warpAffine(gray_p, M, (diag, diag),
|
||||||
flags=cv2.INTER_LINEAR,
|
flags=cv2.INTER_LINEAR,
|
||||||
@@ -461,10 +445,6 @@ class LineShapeMatcher:
|
|||||||
flags=cv2.INTER_NEAREST, borderValue=0)
|
flags=cv2.INTER_NEAREST, borderValue=0)
|
||||||
mag, bins = self._gradient(gray_r)
|
mag, bins = self._gradient(gray_r)
|
||||||
fx, fy, fb = self._extract_features(mag, bins, mask_r)
|
fx, fy, fb = self._extract_features(mag, bins, mask_r)
|
||||||
# LRU semplice: limita cache a ~256 angoli (8 angoli * 32 candidati)
|
|
||||||
if len(feat_cache) > 256:
|
|
||||||
feat_cache.pop(next(iter(feat_cache)))
|
|
||||||
feat_cache[ck] = (fx, fy, fb)
|
|
||||||
if len(fx) < 8:
|
if len(fx) < 8:
|
||||||
return (0.0, cx, cy)
|
return (0.0, cx, cy)
|
||||||
dx = (fx - center[0]).astype(np.int32)
|
dx = (fx - center[0]).astype(np.int32)
|
||||||
@@ -595,6 +575,7 @@ class LineShapeMatcher:
|
|||||||
verify_threshold: float = 0.4,
|
verify_threshold: float = 0.4,
|
||||||
coarse_angle_factor: int = 2,
|
coarse_angle_factor: int = 2,
|
||||||
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:
|
||||||
@@ -666,10 +647,20 @@ 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) - solo coarse.
|
||||||
|
# greediness > 0: usa kernel greedy con early-exit (no rescore bg)
|
||||||
|
# per il pruning. ~2-4x speed-up sul top con greediness=0.8.
|
||||||
|
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_greedy(
|
||||||
|
spread_top, lvl.dx, lvl.dy, lvl.bin, bit_active_top,
|
||||||
|
top_thresh, greediness,
|
||||||
|
)
|
||||||
|
else:
|
||||||
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],
|
bg_cache_top[var.scale],
|
||||||
|
|||||||
Reference in New Issue
Block a user