SANFlow: Semantic-Aware Normalizing Flow for Anomaly Detection

Daehyun Kim, Sungyong Baik, Tae Hyun Kim

Overview

Visual anomaly detection is the "needle in a haystack" problem of computer vision: identifying rare, unpredictable defects or events in images. Because anomalies are scarce, researchers often use Normalizing Flows (NF) to model the distribution of "normal" data. An NF is a generative model that learns a reversible mapping between a complex data distribution (like image features) and a simple, tractable one (like a unit Gaussian).

SANFlow improves visual anomaly detection by moving away from the "one-size-fits-all" approach of standard Normalizing Flows. Instead of forcing all image features into a single Gaussian distribution, it adaptively maps them to location-specific distributions based on local semantic complexity and uses synthetic anomalies to create a clear separation in the latent space.

The Bottleneck in Current Normalizing Flows

The authors identify a major bottleneck in current NF-based anomaly detection: learning difficulty. Existing methods try to force every pixel—whether it’s a simple flat background or a complex mechanical part—into the exact same bell curve.

The authors argue that this "forced transformation" limits the model's ability to distinguish subtle anomalies. If the network struggles just to squash diverse normal features into one shape, it won't have the capacity left to notice when something is truly off.

Concept of SANFlow mapping different regions to distinct distributions

Introducing SANFlow

To address this, the researchers introduce SANFlow (Semantic-Aware Normalizing Flow), which incorporates three novel components:

  1. Semantic-Aware Base Distributions: Instead of a fixed target distribution, SANFlow uses a "statistics prediction network" to estimate the variance for every location in the image. Simple regions get a lower variance, while complex regions get a higher one. This allows the NF to be "spatially aware" of how much information it should be squashing.
  2. Inverse Gamma Prior: To keep the variance estimation stable, they assume the variances follow an Inverse Gamma distribution—a mathematical trick often used in noise modeling to represent uncertainty.
  3. Latent Mean Shifting: Standard anomaly detection models only look at normal data. SANFlow uses synthetic anomalies (created via a modified "CutPaste" augmentation) during training. Crucially, it trains the NF to map normal features to a distribution with a mean of 0, while mapping abnormal features to a distribution with a mean of 1. This creates a physical "gap" in the latent space that makes anomalies much easier to spot.

Key Insights

  • Complexity-Adaptive Modeling: Not all "normality" is created equal. A pixel of a white wall is "more normal" (lower entropy/complexity) than a pixel of a complex circuit board. By letting the target distribution's variance float based on local semantics, the model doesn't have to work as hard to "fit" the data.
  • Discriminative Normalizing Flows: Usually, NFs are purely generative. By introducing synthetic anomalies and forcing them to a different latent mean, the authors turn the NF into a hybrid that is both a density estimator and a classifier.
  • Variance over Mean: Interestingly, the authors found that estimating only the variance was better than estimating both the mean and variance. Trying to predict both made the learning task too unstable, proving that in deep learning, sometimes "less is more" when it comes to parameter flexibility.

Datasets and Key Results

The authors evaluated SANFlow on industry-standard benchmarks, including MVTec AD (15 categories of industrial products), STC (video surveillance), VisA, and CIFAR-10.

SANFlow achieved state-of-the-art (SOTA) or near-SOTA performance across the board:

  • MVTec AD: Achieved an AUROC of 99.4% for anomaly detection and 98.8% for localization (using a WideResNet-101 backbone), outperforming previous heavyweights like PatchCore and CFLOW-AD.
  • STC: Reached 76.1% image-wise AUROC, a significant jump over the previous NF-based SOTA of 72.6%.
Qualitative results showing variance maps and anomaly localization

Qualitatively, the "variance maps" generated by the model clearly show it identifying complex edges and textures as high-variance areas, confirming that the "semantic awareness" is working as intended.

Limitations and Future Work

The model's ability to separate normal from abnormal data relies heavily on the quality of the synthetic anomalies. If the "CutPaste" augmentations don't look like real-world defects for a specific domain, the "mean shifting" trick might be less effective. The authors acknowledge that creating effective synthetic anomalies still requires some domain-specific knowledge. Future directions include exploring ways to reduce the dependence on these augmentations or developing more "universal" synthetic anomaly generators that work across vastly different image types.

Summary updated

BibTeX
@inproceedings{kim2023sanflow,
  author = {Daehyun Kim and Sungyong Baik and Tae Hyun Kim},
  title = {{SANFlow: Semantic-Aware Normalizing Flow for Anomaly Detection}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=BqZ70BEtuW},
}
Figure