Your Diffusion Model is Secretly a Zero-Shot Classifier

Alexander C. Li, Mihir Prabhudesai, Shivam Duggal, Ellis Brown, Deepak Pathak

TL;DR

Diffusion models like Stable Diffusion are widely known for their creative generation, but this paper reveals they are also powerful zero-shot classifiers. By treating the model's noise-prediction error as a proxy for class-conditional density, the authors introduce Diffusion Classifier, a method that achieves state-of-the-art compositional reasoning and remarkable robustness to distribution shifts without any additional training.

What Problem Does This Paper Address?

For years, the computer vision community has debated the merits of generative versus discriminative models. Discriminative models (like CLIP or ResNet) learn to draw boundaries between classes, while generative models learn the underlying distribution of the data itself.

While discriminative models are the standard for classification, they often suffer from "bag of concepts" behavior—they might recognize a "dog" and a "car" but struggle to understand the relationship between them (e.g., "a dog sitting in a car"). Furthermore, these models can be fragile when faced with distribution shifts. The authors investigate whether the rich, high-fidelity representations learned by modern diffusion models can be repurposed to solve these classification and reasoning challenges more effectively.

Methods

The core idea is to use Bayes' theorem to turn a conditional generative model into a classifier. To find the probability of a class $c$ given an image $x$, we need the likelihood $p(x|c)$.

In diffusion models, this likelihood is intractable to compute exactly, but it can be approximated using the Evidence Lower Bound (ELBO). In practice, this translates to a simple procedure:

  1. Take an input image and add a specific amount of noise $\epsilon$ at a timestep $t$.
  2. Ask the diffusion model to predict that noise, conditioned on a text prompt for a specific class (e.g., "a photo of a dog").
  3. Measure the reconstruction error (the difference between the added noise and the predicted noise).
  4. Repeat this for all possible classes. The class that yields the lowest average error is the predicted label.
Figure 1. Overview of our Diffusion Classifier approach: Given an input image x and a set of possible conditioning inputs (e.g., text for Stable Diffusion or class index for DiT, an ImageNet class-conditional model), we use a diffusion model to choose the one that best fits this image. Diffusion Classifier is theoretically motivated through the variational view of diffusion models and uses the ELBO to approximate log pθ(x | c). Diffusion Classifier chooses the conditioning c that best predicts the noise added to the input image. Diffusion Classifier can be used to extract a zero-shot classifier from Stable Diffusion and a standard classifier from DiT without any additional training.

To make this computationally feasible, the authors introduced two key optimizations:

  • Difference Testing: They found that while the absolute noise error varies wildly, the relative difference between classes is very stable if you use the same noise and timestep for every class evaluation.
  • Adaptive Evaluation: They implemented a multi-stage pruning strategy (Algorithm 2) that quickly discards unlikely classes, focusing computational budget on the top contenders.

Datasets

The authors evaluated Diffusion Classifier across a broad suite of benchmarks:

  • Zero-Shot Classification: Food-101, CIFAR-10, FGVC-Aircraft, Oxford-IIIT Pets, Flowers102, STL-10, ImageNet, and ObjectNet.
  • Compositional Reasoning: The Winoground benchmark, which features image pairs with captions containing the same words in different orders (e.g., "some grass in a mug" vs "a mug in some grass").
  • Robustness: ImageNet-A (natural adversarial examples) and ImageNetV2.

Key Insights and Ideas

  • Generative Models are Better Reasoners: Unlike contrastive models like CLIP, which often ignore word order, Diffusion Classifier must understand how objects relate to each other to "reconstruct" the image accurately. This leads to significantly better performance on complex linguistic swaps.
  • The "Sweet Spot" for Noise: Classification accuracy isn't uniform across all noise levels. As shown in Figure 3, accuracy peaks at intermediate timesteps ($t \approx 500$). Too little noise provides no challenge; too much noise destroys the image structure entirely.
Figure 3. Pets accuracy, evaluating only a single timestep per class. Small t corresponds to less noise added, and large t corre- sponds to significant noise. Accuracy is highest when an interme- diate amount of noise is added (t = 500).
  • Effective Robustness: When using a Diffusion Transformer (DiT) trained on ImageNet, the model showed a unique ability to handle distribution shifts. It achieved much higher accuracy on ImageNet-A than predicted by its standard ImageNet performance—a feat rarely seen in purely discriminative models.

Key Results

  • Zero-Shot Performance: Diffusion Classifier (using Stable Diffusion) outperformed CLIP ResNet-50 on 6 out of 8 datasets and remained competitive with the much larger OpenCLIP ViT-H/14.
  • Winoground: It achieved a text score of 38.5%, significantly outperforming OpenCLIP (33.0%). Notably, on the difficult "Relation" category, it beat CLIP by nearly 3 points, proving its superior compositional understanding.
  • ImageNet-A Robustness: The model demonstrated an "effective robustness" of 15-25%, far exceeding the linear trend followed by hundreds of standard discriminative models.

Limitations and Future Work

The primary drawback is inference speed. Because the model must perform multiple U-Net forward passes for every potential class, classifying a single ImageNet image (1000 classes) can take up to 1000 seconds. While the authors' adaptive pruning helps, it remains far slower than standard classifiers.

Future work suggested by the authors includes:

  • Using faster, smaller diffusion models or decreasing resolution to speed up inference.
  • Exploring gradient-based search to find the best class prompt rather than iterating through a list.
  • Training diffusion models on even larger, less curated datasets to improve general recognition abilities.

Summary updated

BibTeX
@inproceedings{li2023your,
  author = {Alexander C. Li and Mihir Prabhudesai and Shivam Duggal and Ellis Brown and Deepak Pathak},
  title = {{Your Diffusion Model is Secretly a Zero-Shot Classifier}},
  booktitle = {IEEE/CVF International Conference on Computer Vision},
  year = {2023},
  url = {https://openreview.net/forum?id=DhKoTSrRuK},
}
Figure