Towards Visually Interpreting Variational Autoencoders

Runze Li, WenQian Liu, Meng Zheng, Max Torop, Milind Rajadhyaksha, Jennifer Dy, Kıvanç Köse, Srikrishna Karanam, Ziyan Wu, Bir Bhanu, Richard J. Radke, Octavia CampsResearch Square 2024

Abstract Recent advances in convolutional neural network (CNN) interpretability have led to a wide-variety of gradient-based visual attention techniques for generating visual attention maps. However, most of these methods require a classification-type design architecture, and consequently concentrate on classification/categorization-type tasks. Extending these methods to generate visual attention maps for other kinds of computer vision models, e.g., variational autoencoders (VAE) is not trivial. In this paper, we present a method that helps bridge this crucial gap, proposing to compute VAE attention as a means for interpreting the latent space learned by a VAE. We first present methods to generate visual attention maps from the learned latent space, and then show how they can be used in a variety of applications: localizing anomalies in images, including medical imagery, and improved latent space disentanglement. We conduct extensive experiments on a wide-variety of benchmark datasets to demonstrate the efficacy of the proposed VAE attention.

Introduction: Opening the Black Box

While deep learning has become highly proficient at answering "What is in this image?", the follow-up question—"Why do you think that?"—remains a significant challenge. For classification models, tools like Grad-CAM have become the gold standard for generating "attention maps" that highlight the pixels driving a decision. However, generative models like Variational Autoencoders (VAEs) have largely remained "black boxes" due to their lack of a final classification layer.

A new study by Runze Li and colleagues, "Towards Visually Interpreting Variational Autoencoders," bridges this gap by introducing a method to generate gradient-based attention maps directly from the latent space. This approach allows researchers to visualize which parts of an image correspond to specific dimensions in a VAE's bottleneck, providing much-needed transparency to generative architectures.

Methodology: VAE Attention

The authors introduce two primary techniques for generating VAE Attention, moving beyond simple reconstruction error to gradient-based localization:

  1. Sample Attention (SA): This direct approach calculates the gradient of each latent dimension $z_i$ with respect to the feature maps of the last convolutional layer. These gradients are aggregated to create a heatmap showing which pixels "support" that specific latent dimension.
  2. Distribution Attention (DA): Recognizing that VAEs deal with probability distributions, DA samples multiple latent codes from the predicted distribution. By fitting a pixel-wise Gaussian to the resulting SA maps, the model produces a mean map (average attention) and a standard deviation map (uncertainty).

For anomaly localization, the researchers define a "Normal Difference Distribution" (NDD). When an abnormal image is processed by a VAE trained only on normal data, the NDD highlights the mathematical deviation in the latent space, which the attention maps then translate into a visual highlight on the image.

Visualizations of anomaly localization using VAE Attention across various textures and objects from the MVTec-AD dataset.

Applications and Experiments

The efficacy of VAE Attention was validated across diverse benchmarks, including MNIST for sanity checks and the MVTec-AD dataset for industrial quality control. The method was also applied to medical imaging (ISIC 2018/2019) for skin lesion analysis and dSprites for evaluating latent space disentanglement.

Attention as a Training Constraint

A key innovation in this work is the Attention Disentanglement Loss ($L_{AD}$). Rather than just using attention for visualization, the authors use it as a training signal. By penalizing the model when attention maps from different latent dimensions overlap, they force the VAE to learn more distinct, disentangled features. On the dSprites dataset, their "AD-FactorVAE" reached a disentanglement score of ~0.93, significantly outperforming the baseline FactorVAE (~0.82).

Key Results

The proposed method achieves competitive results in anomaly localization without requiring complex, task-specific architectures. As shown in the performance table below, the VAE Attention approach (Ours SA/DA) matches or exceeds several specialized baselines across various object and texture categories.

Quantitative comparison of anomaly localization performance (AUROC) across different categories and baseline models.

In the "Hazelnut" category of the MVTec-AD dataset, the method achieved a 0.98 AUROC, matching the performance of state-of-the-art specialized models. In medical tasks, the DA maps consistently outperformed simple reconstruction difference maps, proving that gradient-based attention is a more robust indicator of anomalies like skin lesions.

Conclusion

By providing a "lens" into the latent space of VAEs, this research moves generative modeling toward greater interpretability. The ability to visualize uncertainty through Distribution Attention and improve feature separation via Attention Disentanglement Loss suggests that the information required for high-quality localization was always present within standard VAEs. Future work may extend these techniques to other generative architectures, such as GANs or Diffusion Models, to further demystify the internal logic of generative AI.

Summary updated

BibTeX
@inproceedings{li2024towards,
  author = {Runze Li and WenQian Liu and Meng Zheng and Max Torop and Milind Rajadhyaksha and Jennifer Dy and Kıvanç Köse and Srikrishna Karanam and Ziyan Wu and Bir Bhanu and Richard J. Radke and Octavia Camps},
  title = {{Towards Visually Interpreting Variational Autoencoders}},
  booktitle = {Research Square},
  year = {2024},
  doi = {10.21203/rs.3.rs-4331222/v1},
}
Figure