Abstract
We propose the first Bayesian encoder for metric learning. Rather than relying on neural amortization as done in prior works, we learn a distribution over the network weights with the Laplace Approximation. We actualize this by first proving that the contrastive loss is a valid log-posterior. We then propose three methods that ensure a positive definite Hessian. Lastly, we present a novel decomposition of the Generalized Gauss-Newton approximation. Empirically, we show that our Laplacian Metric Learner (LAM) estimates well-calibrated uncertainties, reliably detects out-of-distribution examples, and yields state-of-the-art predictive performance.
Introduction
In image retrieval tasks such as face recognition or reverse image search, metric learning is employed to map images into an embedding space where semantic similarity corresponds to geometric proximity. However, conventional deterministic models often fail silently when encountering out-of-distribution (OOD) data, projecting unfamiliar inputs into the embedding space with high confidence.
Previous attempts to quantify uncertainty in this domain have largely relied on amortized inference, such as Probabilistic Face Embeddings (PFE). These methods use an auxiliary network branch to predict uncertainty directly from input features. Unfortunately, because these auxiliary branches are themselves neural networks, they often fail to extrapolate to truly novel data, leading to poor OOD detection.
Methodology: The Laplacian Metric Learner (LAM)
We introduce the Laplacian Metric Learner (LAM), which shifts the source of uncertainty from input features to the network weights themselves. By treating the standard contrastive loss as a valid unnormalized log-posterior, we can apply the Laplace Approximation (LA) to model the weight distribution.
Key Components of LAM:
- Hessian Approximation: We approximate the curvature of the loss landscape using the Hessian. To ensure the resulting uncertainty estimates are mathematically valid (positive definite), we propose three variants: Positive, Fixed, and Full. The "Fixed" Hessian, which ignores certain cross-derivatives, proved most effective for high-dimensional retrieval data.
- Spherical Geometry: Since retrieval embeddings are typically $\ell_2$-normalized, we represent uncertainty on the hyper-sphere using the von Mises-Fisher (vMF) distribution rather than a standard Gaussian.
- Weight Distribution: By learning $\theta \sim \mathcal{N}(\mu_\theta, \sigma_\theta^2)$, LAM captures the model's epistemic uncertainty—its own lack of knowledge about the optimal mapping for a given input.
Experimental Results
LAM was evaluated across diverse benchmarks, including FashionMNIST, CIFAR10, and the massive Mapillary Street-Level Sequences (MSLS) dataset.
Out-of-Distribution Detection and Calibration
LAM significantly outperforms amortized methods in identifying OOD samples. On the FashionMNIST (ID) vs. MNIST (OOD) task, LAM achieved an AUROC of 0.98, compared to just 0.53 for PFE. Furthermore, LAM's uncertainty is better calibrated; on CIFAR10, it achieved an Expected Calibration Error (ECE) of 0.01, whereas PFE scored 0.11.
As shown in the figure above, LAM's uncertainty reliably increases as image quality degrades through blur, noise, or occlusion. In contrast, amortized methods like PFE often show inconsistent or even decreasing uncertainty as the input becomes less recognizable.
Performance and Robustness
Beyond uncertainty quantification, LAM maintains or slightly improves predictive performance (mAP) compared to deterministic baselines. In large-scale place recognition (MSLS), LAM successfully flagged ambiguous images—such as those dominated by motion blur or nondescript pavement—while maintaining high retrieval accuracy for clear landmarks.
Conclusion
LAM represents a significant step forward for reliable metric learning. By deriving uncertainty from the network's weight distribution via the Laplace Approximation, it provides a robust mechanism for OOD detection and well-calibrated confidence scores. While Bayesian sampling introduces some computational overhead, the gains in system reliability and predictive performance make it a compelling choice for safety-critical retrieval applications. Future work will focus on scaling Hessian computations to deeper network architectures and applying these uncertainties to active learning pipelines.
Summary updated