---
title: Sparse-Projected Guides (SPG) for Anomaly Detection
url: https://www.emergentmind.com/topics/sparse-projected-guides-spg
type: topic
---

# Sparse-Projected Guides (SPG) for Anomaly Detection

Sparse-Projected Guides (SPG) is a prompt-free framework for zero-shot anomaly detection and segmentation that operates in the visual feature space of a frozen foundation model and learns normal and anomalous reference vectors as sparse linear combinations of dictionary atoms from a Sparse Autoencoder (SAE) [2604.02871]. In the formulation introduced for industrial visual inspection, all learnable parameters are trained only on a labeled auxiliary dataset and are transferred to unseen target categories with no target-domain adaptation; the method therefore replaces prompt embeddings with sparse-projected guides constructed from an interpretable dictionary over frozen patch-token features [2604.02871].

## 1. Definition and problem setting

SPG is defined in the context of zero-shot anomaly detection and segmentation (ZSAD) with two disjoint datasets, an auxiliary training dataset and a target test dataset, whose categories are disjoint [2604.02871]. The data are written as
\[
\mathcal{D}_{\text{train}} = \{(X_i, Y_i, y_i)\}_{i=1}^{N_{\text{train}}},
\qquad
\mathcal{D}_{\text{target}} = \{(X_i, Y_i, y_i)\}_{i=1}^{N_{\text{target}}},
\]
where \(X_i \in \mathbb{R}^{H \times W \times 3}\) is an image, \(Y_i \in \{0,1\}^{H \times W}\) is a pixel-level anomaly mask, and \(y_i \in \{0,1\}\) is an image-level anomaly label [2604.02871]. A frozen foundation model provides patch-token features
\[
\phi(X_i) = \{\mathbf{f}_{i,j}\}_{j=1}^{m}, \quad \mathbf{f}_{i,j} \in \mathbb{R}^d,
\]
with \(m = H_p W_p\) patches on a grid, and \(\phi\) is never updated [2604.02871].

The central constraint is that all learnable parameters are trained only on \(\mathcal{D}_{\text{train}}\), while ground-truth masks and labels on \(\mathcal{D}_{\text{target}}\) are used only for evaluation [2604.02871]. SPG was proposed against the background of CLIP-style ZSAD methods that define normal and anomalous reference vectors through text prompts, either manually specified or learned from auxiliary data [2604.02871]. In contrast, SPG defines its criterion purely in visual feature space and seeks a sparse, interpretable internal structure whose decisions can be traced back to a small set of dictionary atoms [2604.02871].

This design is explicitly backbone-agnostic in the sense stated by the source: SPG uses any frozen visual backbone, including DINOv3, DINOv2, SigLIP, and OpenCLIP [2604.02871]. The method introduces SAEs trained on patch features from the auxiliary dataset, learns sparse guide coefficients in the SAE latent space, and transfers the resulting guide vectors to unseen categories without further training [2604.02871]. This suggests that SPG is best understood not as a prompt-learning variant, but as a sparse dictionary-based criterion for anomaly discrimination in frozen feature geometry.

## 2. Representation learning with a Sparse Autoencoder

SPG begins by fitting an SAE to patch tokens extracted from the frozen backbone [2604.02871]. For each token \(\mathbf{f} \in \mathbb{R}^d\), the encoder maps to a high-dimensional latent
\[
\bar{\mathbf{z}} = \mathrm{Enc}(\mathbf{f}) \in \mathbb{R}^C,
\]
where \(\mathrm{Enc}\) is a single linear layer \(d \to C\), and \(C \gg d\) [2604.02871]. Sparsification is then imposed through TopK:
\[
\tilde{\mathbf{z}} = \mathrm{TopK}(\bar{\mathbf{z}}; k),
\]
which keeps the top-\(k\) entries and zeros the rest [2604.02871]. Decoding uses a linear dictionary \(D \in \mathbb{R}^{d \times C}\):
\[
\hat{\mathbf{f}} = \mathrm{Dec}(\tilde{\mathbf{z}}) = D \tilde{\mathbf{z}},
\]
so that
\[
\mathbf{f} \approx D \tilde{\mathbf{z}}, \quad \|\tilde{\mathbf{z}}\|_0 = k.
\]
The columns of \(D\) are the atoms [2604.02871].

Stage 1 minimizes pure reconstruction error over all patch tokens from the auxiliary dataset:
\[
\bar{\mathbf{z}}_{i,j} = \mathrm{Enc}(\mathbf{f}_{i,j}), \quad
\tilde{\mathbf{z}}_{i,j} = \mathrm{TopK}(\bar{\mathbf{z}}_{i,j}; k),
\]
\[
\hat{\mathbf{f}}_{i,j} = D \tilde{\mathbf{z}}_{i,j},
\]
\[
\mathcal{L}_{\mathrm{sae}} =
\frac{1}{N_{\mathrm{train}} m}
\sum_{i=1}^{N_{\mathrm{train}}}
\sum_{j=1}^{m}
\left\| \mathbf{f}_{i,j} - D \tilde{\mathbf{z}}_{i,j} \right\|_2^2.
\]
Sparsity is therefore controlled explicitly by TopK rather than by an \(\ell_1\) term [2604.02871].

The default hyperparameters reported for SAE training are a dictionary width \(C = 4096\), sparsity level \(k = 32\), 50 epochs, Adam, learning rate \(10^{-3}\), and batch size 16 [2604.02871]. After Stage 1, \(\mathrm{Enc}\) and \(D\) are frozen for all subsequent steps and at test time [2604.02871].

The SAE provides the structural substrate on which SPG operates. The dictionary \(D = [\mathbf{d}_1,\dots,\mathbf{d}_C]\) forms a basis over which both patch features and guides are expressed, each atom \(\mathbf{d}_c\) being a direction in backbone feature space [2604.02871]. Because patch representations are sparse linear combinations of atoms, the subsequent guide-learning phase can restrict anomaly decisions to a small active subset of these atoms.

## 3. Sparse-projected guides and the two-stage learning procedure

After SAE training, SPG introduces two learnable guide coefficient vectors,
\[
\bar{\mathbf{z}}_N, \bar{\mathbf{z}}_A \in \mathbb{R}^C,
\]
for normal and anomalous guides [2604.02871]. In the main formulation, these coefficients are passed through a non-negative transform,
\[
\tilde{\mathbf{z}}_N = \rho(\bar{\mathbf{z}}_N), \quad
\tilde{\mathbf{z}}_A = \rho(\bar{\mathbf{z}}_A),
\]
with \(\rho = \mathrm{ReLU}\), yielding non-negative coefficients [2604.02871]. The corresponding guide vectors in backbone feature space are
\[
\mathbf{g}_N = D \tilde{\mathbf{z}}_N, \quad
\mathbf{g}_A = D \tilde{\mathbf{z}}_A.
\]
The active atom sets are
\[
\mathcal{I}_N = \mathrm{supp}(\tilde{\mathbf{z}}_N), \quad
\mathcal{I}_A = \mathrm{supp}(\tilde{\mathbf{z}}_A).
\]
Because of sparsity, \(|\mathcal{I}_N|\) and \(|\mathcal{I}_A|\) are small, and decisions can be traced back to a few dictionary atoms [2604.02871].

Stage 2 freezes the backbone, encoder, and dictionary, and optimizes only the guide coefficients using auxiliary pixel-level masks [2604.02871]. Given an auxiliary image \(X\) and mask \(Y\), SPG extracts patch tokens, constructs \(\mathbf{g}_N\) and \(\mathbf{g}_A\), computes patch similarities and anomaly probabilities, upsamples to a pixel-level anomaly map \(\hat{p}_A \in [0,1]^{H \times W}\), and applies a segmentation loss plus sparsity penalty [2604.02871]. The overall loss is
\[
\mathcal{L} = \mathcal{L}_{\mathrm{seg}}(\hat{p}_A, Y)
+ \mathcal{L}_{\mathrm{sparse}}(\tilde{\mathbf{z}}_N, \tilde{\mathbf{z}}_A).
\]

The segmentation term is
\[
\mathcal{L}_{\mathrm{seg}}(\hat{p}_A, Y)
=
\mathcal{L}_{\mathrm{focal}}(\hat{p}_A, Y)
+
\lambda_{\mathrm{dice}}\,\mathcal{L}_{\mathrm{dice}}(\hat{p}_A, Y),
\]
with \(\lambda_{\mathrm{dice}} = 1.0\) [2604.02871]. The sparsity regularizer is
\[
\mathcal{L}_{\mathrm{sparse}}
=
\beta \left(
\|\tilde{\mathbf{z}}_N\|_1 + \|\tilde{\mathbf{z}}_A\|_1
\right),
\]
with \(\beta = 0.01\) [2604.02871]. The Stage-2 optimization is therefore
\[
\min_{\bar{\mathbf{z}}_N, \bar{\mathbf{z}}_A}
\mathcal{L}_{\mathrm{seg}}(\hat{p}_A, Y)
+
\beta \left(
\|\rho(\bar{\mathbf{z}}_N)\|_1 + \|\rho(\bar{\mathbf{z}}_A)\|_1
\right).
\]

Only \(\bar{\mathbf{z}}_N\) and \(\bar{\mathbf{z}}_A\) are updated, so Stage 2 optimizes two vectors of size \(C\) while all other components remain frozen [2604.02871]. An EMA with decay \(0.999\) is applied to these parameters for stability [2604.02871]. The appendix also reports a TopK projection variant in Stage 2, without \(\ell_1\), and finds that performance improves with larger TopK budgets but is less flexible than ReLU+\(\ell_1\) because the appropriate sparsity differs by transfer direction [2604.02871].

A common misconception is that SPG learns prompts indirectly. The stated formulation does not do so: the reference vectors are guide vectors in visual feature space, and no text encoder or prompts are used [2604.02871]. This suggests that the defining operation of SPG is not prompt substitution but sparse projection from SAE latent coefficients into the frozen backbone space.

## 4. Inference and anomaly scoring

At test time on the target dataset, SPG uses the fixed guide coefficients learned on the auxiliary dataset and performs no per-image or target-domain adaptation [2604.02871]. For a test image \(X\), the frozen backbone computes patch tokens
\[
\{\mathbf{f}_j\}_{j=1}^m = \phi(X).
\]
The learned coefficients then produce fixed guide vectors
\[
\mathbf{g}_N = D \tilde{\mathbf{z}}_N, \quad
\mathbf{g}_A = D \tilde{\mathbf{z}}_A
\]
for all target images [2604.02871].

Patch-level anomaly scoring is based on cosine similarity and a two-way softmax [2604.02871]. For each patch \(j\), SPG \(\ell_2\)-normalizes \(\mathbf{f}_j\), \(\mathbf{g}_N\), and \(\mathbf{g}_A\), computes
\[
\ell_N(j) = \cos(\mathbf{f}_j, \mathbf{g}_N), \quad
\ell_A(j) = \cos(\mathbf{f}_j, \mathbf{g}_A),
\]
and then evaluates
\[
p_A(j) =
\frac{\exp(\ell_A(j)/T)}
{\exp(\ell_N(j)/T) + \exp(\ell_A(j)/T)},
\]
with default temperature \(T = 0.07\) [2604.02871]. The patch scores are placed on the patch grid and upsampled by bilinear interpolation to image resolution,
\[
\hat{p}_A = \mathrm{Upsample}(p_A) \in [0,1]^{H \times W},
\]
yielding the anomaly segmentation map [2604.02871].

Image-level anomaly scoring is obtained through temperature-controlled log-sum-exp pooling:
\[
S_\tau(X) =
\tau \log \left(
\frac{1}{HW} \sum_{u,v}
\exp\left(\frac{\hat{p}_A(u,v)}{\tau}\right)
\right).
\]
As \(\tau \to 0\), this approaches max pooling, and as \(\tau \to \infty\), it approaches mean pooling; the default is effectively max pooling, which empirically works best [2604.02871]. The source attributes this to the benefit of preserving localized high anomaly responses rather than averaging them away [2604.02871].

This inference rule can be described, in the paper’s own framing, as a two-class classifier in similarity space whose only learnable parameters are the guide vectors [2604.02871]. A plausible implication is that SPG trades expressive class-conditional adaptation for a minimal parameterization whose discriminative content is concentrated in the sparse coefficients.

## 5. Empirical performance and ablations

SPG was evaluated under cross-dataset zero-shot transfer using MVTec AD and VisA, training on one dataset and testing on the other [2604.02871]. The reported metrics were image-level AUROC and AP, and pixel-level AUROC and AUPRO [2604.02871]. The auxiliary dataset provided pixel-level masks for Stage-2 supervision [2604.02871].

The main reported results are as follows.

| Setting | Image-level | Pixel-level |
|---|---|---|
| VisA \(\to\) MVTec, SPG(DINOv3) | 91.4% AUROC, 95.7% AP | 92.3% AUROC, 87.7% AUPRO |
| MVTec \(\to\) VisA, SPG(DINOv3) | 80.2% AUROC, 84.3% AP | 96.0% AUROC, 89.1% AUPRO |
| VisA \(\to\) MVTec, SPG(OpenCLIP) | 79.3% AUROC, 89.6% AP | 89.9% AUROC, 71.9% AUPRO |
| MVTec \(\to\) VisA, SPG(OpenCLIP) | 82.6% AUROC, 85.7% AP | 94.7% AUROC, 87.1% AUPRO |

With DINOv3, SPG achieved the highest pixel-level AUROC among the compared methods on both transfer directions: 92.3% AUROC and 87.7% AUPRO for VisA\(\to\)MVTec, and 96.0% AUROC and 89.1% AUPRO for MVTec\(\to\)VisA [2604.02871]. For AUPRO on MVTec\(\to\)VisA, SPG was slightly below the best prompt-adaptation baseline, with VCP-CLIP at 90.7% versus SPG at 89.1% [2604.02871]. At image level, SPG(DINOv3) was described as competitive rather than dominant, with 91.4% and 80.2% AUROC under the two transfer directions [2604.02871].

The reported ablations identify several dependencies [2604.02871]. Sweeping dictionary width \(C \in \{2048,3072,4096,6144,8192\}\) and sparsity \(k \in \{8,16,32,64,128\}\) showed that best settings differ by transfer direction and metric, although a broad range is robust [2604.02871]. For image-level aggregation, small \(\tau\) in log-sum-exp pooling outperformed large \(\tau\), consistent with the advantage of max-like behavior [2604.02871]. Backbone comparisons across OpenCLIP, SigLIP-L, DINOv2-L, and DINOv3-L found that DINOv3 consistently yielded the strongest segmentation performance in both transfer directions, while image-level detection was relatively stable across backbones [2604.02871].

The appendix comparison between TopK and ReLU+\(\ell_1\) for Stage-2 sparsity found that ReLU+\(\ell_1\) automatically adapts effective sparsity to transfer direction, with examples of 211 versus 644 active coefficients, and was therefore described as more flexible [2604.02871]. This suggests that the main empirical advantage of SPG is concentrated in pixel-level segmentation and in the ability to use a vision-only backbone such as DINOv3 without prompts.

## 6. Interpretability, relation to prompt-based methods, and limitations

SPG is explicitly positioned against prompt-based ZSAD systems that define reference vectors through text prompts passed through a CLIP text encoder [2604.02871]. In those methods, the anomaly criterion is tied to prompt semantics and usually restricted to CLIP-style vision-language backbones [2604.02871]. SPG removes both requirements: it uses no text and no prompts, and its reference vectors are sparse combinations of SAE atoms in visual space [2604.02871]. To compare fairly with CLIP-based baselines, the authors also instantiated SPG with OpenCLIP ViT-L/14@336px, but still used only visual features and no text encoder [2604.02871].

The SAE dictionary supports a specific form of diagnostic interpretability [2604.02871]. Patch features and guide vectors can be written as sparse sums over atoms,
\[
\mathbf{f}_{i,j} \approx \sum_{c \in \mathrm{supp}(\tilde{\mathbf{z}}_{i,j})}
\tilde{\mathbf{z}}_{i,j}[c]\,\mathbf{d}_c,
\]
\[
\mathbf{g}_A =
\sum_{c \in \mathcal{I}_A}
\tilde{\mathbf{z}}_A[c]\,\mathbf{d}_c.
\]
The paper analyzes atoms with the largest anomaly coefficients by retrieving the top-\(K\) auxiliary patches with highest activation and overlaying activation maps [2604.02871]. Qualitatively, some atoms appear category-general, activating on anomalies such as cracks, missing parts, and localized surface damage across multiple product categories, whereas others are category-specific and tied to particular object structures or parts [2604.02871]. The authors explicitly do not claim full monosemantic disentanglement, but show that individual atoms often have coherent visual patterns [2604.02871].

Several limitations are stated directly [2604.02871]. Stage 2 requires an auxiliary dataset with pixel-level masks, so although categories differ from the target, annotation cost remains [2604.02871]. Performance and interpretability depend on the underlying backbone representation, implying that domain shifts beyond MVTec and VisA could degrade performance [2604.02871]. The method also uses a single pair of guides for all categories and spatial locations, without per-category or per-instance adaptation [2604.02871]. SAE training on all patch tokens adds computational overhead relative to direct prompt-based CLIP methods, even though Stage-2 parameters are small [2604.02871]. Future directions suggested in the source include weaker supervision for Stage 2, extension to other modalities or tasks, more expressive guide parameterizations, and deeper interpretability analysis [2604.02871].

A further source of confusion is terminological rather than methodological. The acronym “SPG” is used in multiple research areas for unrelated methods, including the spectral projected-gradient solver used for basis-pursuit denoising in seismic blind deconvolution [1611.03754], stochastic proximal gradient for sparse discrete Markov random fields [2005.06083], smoothing proximal gradient for structured sparse regression [1005.4717], and spectral projected gradient methods for tensor eigenvalue complementarity problems [1601.01738]. In the anomaly-detection literature, however, SPG specifically denotes Sparse-Projected Guides, i.e., sparse guide coefficients in SAE latent space that generate normal and anomaly guide vectors through the SAE dictionary [2604.02871].

Source: https://www.emergentmind.com/topics/sparse-projected-guides-spg