From 7c4023199f6c1ee34a8278ae8f5c3446eb6fbe11 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 13 May 2026 19:57:53 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Vectorize=20BasicEstimator?= =?UTF-8?q?=20prediction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Vectorize BasicEstimator.predict using NumPy matrix operations and pre-calculated norms for significantly faster nearest-neighbor search. Includes handling for empty input and backward compatibility for loaded models. Co-authored-by: guesswh0 <10531675+guesswh0@users.noreply.github.com> --- .jules/bolt.md | 3 +++ face_engine/models/basic_estimator.py | 37 ++++++++++++++++++++------- 2 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..80130e2 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2025-05-15 - [Numerical Precision in Vectorized Distance Calculation] +**Learning:** Using the expansion formula ||a-b||^2 = ||a||^2 + ||b||^2 - 2ab for vectorized Euclidean distance is much faster but can produce small negative values due to floating-point precision limits. +**Action:** Always use np.maximum(dists_sq, 0) when calculating squared distances with this formula to ensure numerical stability. diff --git a/face_engine/models/basic_estimator.py b/face_engine/models/basic_estimator.py index fbbf2b9..2e46490 100644 --- a/face_engine/models/basic_estimator.py +++ b/face_engine/models/basic_estimator.py @@ -20,21 +20,40 @@ def __init__(self): self.class_names = None def fit(self, embeddings, class_names, **kwargs): - self.embeddings = embeddings + self.embeddings = np.asarray(embeddings) self.class_names = class_names + # Pre-calculate squared norms for faster distance calculation in predict + self.norms_sq = np.sum(self.embeddings**2, axis=1) def predict(self, embeddings): if self.class_names is None: raise TrainError("Model is not fitted yet!") - scores = [] - class_names = [] - for embedding in embeddings: - distances = np.linalg.norm(self.embeddings - embedding, axis=1) - index = np.argmin(distances) - score = np.exp(-0.5 * distances[index] ** 2) - scores.append(score) - class_names.append(self.class_names[index]) + embeddings = np.asarray(embeddings) + if len(embeddings) == 0: + return [], [] + + # Using expansion formula: ||a-b||^2 = ||a||^2 + ||b||^2 - 2ab + # to calculate squared Euclidean distances in a vectorized way. + input_norms_sq = np.sum(embeddings**2, axis=1, keepdims=True) + + # Handle cases where model might have been loaded without pre-calculated norms + fitted_norms_sq = getattr(self, "norms_sq", None) + if fitted_norms_sq is None: + fitted_norms_sq = np.sum(self.embeddings**2, axis=1) + + # Vectorized squared distance calculation: (M, 1) + (N,) - 2 * (M, N) -> (M, N) + dists_sq = input_norms_sq + fitted_norms_sq - 2 * np.dot(embeddings, self.embeddings.T) + + # Ensure numerical stability (prevent tiny negative values due to floating point precision) + dists_sq = np.maximum(dists_sq, 0) + + indices = np.argmin(dists_sq, axis=1) + min_dists_sq = dists_sq[np.arange(len(embeddings)), indices] + + scores = np.exp(-0.5 * min_dists_sq).tolist() + class_names = [self.class_names[i] for i in indices] + return scores, class_names def save(self, dirname):