TokenCLIP: Token-wise Prompt Learning for Zero-shot Anomaly Detection

Qihang Zhou, Bin-Bin Gao, Guansong Pang, Xin Wang, Jiming Chen, Shibo He · Submission to ICLR 2026 · Rejected

TL;DR

Existing vision-language models for zero-shot anomaly detection indiscriminately match all image patches against a single text prompt embedding, sacrificing fine-grained and rare defect details. TokenCLIP solves this by projecting the textual space into a small set of orthogonal subspaces and dynamically assigning visual patch tokens to combinations of these subspaces using Optimal Transport (OT). This achieves granular, token-level supervision and state-of-the-art anomaly localization with minimal computational overhead.

What Problem Does This Paper Address?

Adapting CLIP for zero-shot anomaly detection (ZSAD)—detecting unseen defects in industrial parts or medical scans without training on anomalous targets—typically relies on aligning visual features with fixed or learnable text prompts (e.g., "normal" vs. "damaged").

However, existing frameworks force every visual patch token in an image to align with a single, token-agnostic textual space. This "indiscriminate alignment" forces the prompt to compromise across diverse visual structures, favoring dominant, common defects while missing subtle, rare anomalies. While assigning a dedicated textual prompt to every patch token would resolve this, it is computationally intractable (over a thousand text encodings per image) and prone to severe underfitting.

Methods

TokenCLIP resolves this dilemma by dynamically mapping image patches to a learned mixture of specialized textual subspaces:

  1. Multi-Head Orthogonal Subspaces: Starting from base text prompts, TokenCLIP uses multi-head projections to derive $Q$ textual subspaces ($Q \in \{3, 4\}$). An orthogonality regularization loss ($\mathcal{L}_{\text{reg}}$) penalizes redundancy to ensure the subspaces span distinct semantic directions.
  2. Dynamic Alignment via Optimal Transport (OT): Instead of greedy nearest-neighbor matching, the assignment between patch tokens (source distribution) and textual subspaces (target distribution) is formulated as an entropic-regularized OT problem using the Sinkhorn-Knopp algorithm. The transport cost is based on cosine distance.
  3. Top-$K$ Sparsification: The resulting dense transport plan is sparsified by retaining only the top-$K$ ($K=2$) subspaces per token above a threshold ($\epsilon=0.2$) and row-normalizing them into soft assignment weights.
  4. Decoupled Training Objective: The model optimizes both base global/local semantics and token-wise dynamic alignment via a composite loss including Focal loss, Dice loss, a margin-enforcing hinge loss, and orthogonality regularization.
Visual Patch Tokens ──┐
                      ├──> Cost Matrix ──> Sinkhorn OT ──> Top-K Sparsity ──> Dynamic Alignment
Text Subspaces (Q)  ──┘

Datasets

The authors evaluate TokenCLIP across 15 standard, publicly available benchmarks spanning two domains:

  • Industrial Inspection (7 datasets): MVTec AD, VisA, MPDD, BTAD, SDD, DAGM, and DTD-Synthetic.
  • Medical Analysis (8 datasets): ISIC (skin lesion), CVC-ClinicDB, CVC-ColonDB, Kvasir, Endo (colon polyps), and HeadCT, BrainMRI, Br35H (brain tumors).

No new benchmarks were introduced. Following standard zero-shot protocols, models trained on VisA are tested on MVTec AD, and models trained on MVTec AD are evaluated across all other industrial and medical benchmarks without fine-tuning.

Key Insights and Ideas

  • Why OT over Greedy Matching? A greedy similarity baseline ("TokenCLIP-Van") causes "subspace collapse," where one dominant subspace absorbs most tokens while others underfit. OT's marginal constraints guarantee that every subspace receives sufficient mass, ensuring all subspaces are optimized.
  • Theoretical Specialization Guarantee: The authors prove (Theorem 3.1) that transporting two distinct visual clusters into the same subspace incurs an explicit geometric penalty proportional to cluster variance and distance. Consequently, minimizing OT transport cost mathematically compels subspaces to specialize.
  • Emergent Semantic Roles: Without explicit location or attribute supervision, subspaces self-organize: one subspace automatically specializes in foreground objects and high-variation defect regions, while the remaining subspaces capture smooth background and context textures.

Key Results

  • Industrial Benchmarks: On MVTec AD, TokenCLIP achieves 92.2% pixel AUROC and 87.9% PRO (per-region overlap), notably outperforming AnomalyCLIP (91.1% AUROC, 81.4% PRO; +6.5% PRO gain). On VisA, it reaches 95.9% pixel AUROC and 88.5% PRO.
  • Cross-Domain Medical Transfer: When transferring directly to medical scans without re-training, TokenCLIP achieves 91.6% pixel AUROC and 83.4% PRO on ISIC skin lesions, and 96.0% image AUROC on HeadCT.
  • Efficiency: Unlike prompt-ensembling methods (e.g., FAPrompt, which runs at 4.67 FPS and uses 4.2 GB VRAM), TokenCLIP processes images at 7.39 FPS with 3.18 GB VRAM, offering near-parity with baseline single-prompt architectures (8.04 FPS, 2.23 GB VRAM).

Limitations and Future Work

  • Spatial Inconsistencies: Because patch tokens are assigned independently during OT, occasional isolated tokens within an object region switch to a background subspace, leading to spot-level false alarms.
  • Fixed Hyperparameters: The number of subspaces $Q$ and sparsity parameter $K$ are statically chosen per dataset family rather than dynamically predicted per sample.
  • Overhead: Solving the Sinkhorn algorithm introduces modest additional latency compared to static single-space models. Future iterations could explore spatially smoothed transport plans or adaptive subspace allocation.

Summary updated

BibTeX
@inproceedings{zhou2026tokenclip,
  author = {Qihang Zhou and Bin-Bin Gao and Guansong Pang and Xin Wang and Jiming Chen and Shibo He},
  title = {{TokenCLIP: Token-wise Prompt Learning for Zero-shot Anomaly Detection}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=Z0PkjOVabt},
}
Figure