This repository contains a minimal, self-contained demo that reproduces the core idea of "Explaining Bayesian Neural Networks" on a single ImageNet example (a tiger cat image).
We take a pretrained VGG16 network, interpret its dropout layers as an approximate Bayesian posterior (MC Dropout), sample multiple explanations with LRP-CMP, and aggregate them into:
- Mean explanation: average relevance over MC samples
- Intersection (low percentile): features that are consistently relevant
- Union (high percentile): features that are relevant in at least some samples
- UAI⁺: pixelwise stability of positive relevance across samples
Everything is designed to run locally, without any Colab specific code.
git clone https://github.com/your-user/explaining-bnns-demo.git
cd explaining-bnns-demopython -m venv .venv
source .venv/bin/activate # on Windows: .venv\Scripts\activatepip install -r requirements.txtNote: You need a PyTorch build that matches your system. The simplest way is usually to install from pytorch.org and then add the remaining packages from
requirements.txt.
Create the assets folder (if it does not exist yet) and place an ImageNet style
tiger cat image there:
mkdir -p assets
cp /path/to/your/tiger_cat.jpg assets/tiger_cat.jpgThe notebook assumes the file exists at:
assets/tiger_cat.jpg
Start Jupyter from the repository root:
jupyter lab
# or
jupyter notebookThen open:
notebooks/01_bnn_uai_tiger_cat.ipynb
The notebook is structured into the following steps:
- Load a pretrained VGG16 model and send it to CPU or GPU.
- Load and preprocess
assets/tiger_cat.jpg. - Compute a baseline explanation with LRP-CMP (no dropout at inference time).
- Enable MC Dropout and sample multiple explanations for the same image.
- Aggregate these explanations into:
- Mean map
- Intersection map (5th percentile)
- Union map (95th percentile)
- UAI⁺ stability map
- Visualize all maps side by side.
-
src/models.py
Utilities for loading the pretrained VGG16 model. -
src/explanations.py
Implementation of LRP-CMP, normalization helpers, and an UnNormalize transform. -
notebooks/01_bnn_uai_tiger_cat.ipynb
The main demo notebook, with explanatory text and a simple, linear workflow.
If you use this code in academic work, please consider citing:
Bykov, K. et al., "Explaining Bayesian Neural Networks" (TMLR, 2025).
This repository is a small convenience wrapper around the core ideas from that paper, focused on a single, easy to run example.