Uncertainty Estimation for Medical Imaging

Benchmarking uncertainty-quantification methods, then separating aleatoric from epistemic uncertainty with a Gaussian mixture model over ResNet features and test-time augmentation, on skin-lesion and chest X-ray images.

Code on GitHub · MeDAL Lab, IIT Bombay (2024–2025) · Advisor: Prof. Amit Sethi

Joint work with Gouranga Bala.

Overview

Deep classifiers are often confidently wrong, which limits their use in safety-critical settings such as medical imaging. A deployed model should flag two kinds of inputs: images it cannot classify reliably because they are noisy or ambiguous (high aleatoric uncertainty), and images unlike anything it was trained on (high epistemic uncertainty, usually out-of-distribution or OOD inputs). Rejecting these images and passing them to a human expert should raise the accuracy on the rest.

The project had two parts.

  1. Benchmarking UQ methods (2024). We implemented Deterministic Uncertainty Quantification (DUQ) [1] and Quantification of Uncertainty with Adversarial Models (QUAM) [2] on MNIST and proposed simple ways to compare UQ methods on a common scale. The write-up is the R&D report (PDF).
  2. An aleatoric-uncertainty metric for medical images (2025). Building on Deep Deterministic Uncertainty (DDU) [3], we proposed a metric that combines a Gaussian mixture model (GMM) over the network’s features with test-time augmentation (TTA). On a test set that mixes skin-lesion and chest X-ray images, it improves the accuracy after rejection by up to 6.5 points over DDU. We also proposed an active-learning benchmark that compares aleatoric-uncertainty estimates.

Part 1: Benchmarking UQ methods

Methods

DUQ [1] replaces the softmax layer with one centroid per class in feature space. For each class \(c\), it computes a kernel value between the feature vector \(f_\theta(x)\) and the class centroid \(e_c\):

\[K_c\big(f_\theta(x), e_c\big) = \exp\left(-\frac{\frac{1}{n}\lVert W_c f_\theta(x) - e_c \rVert_2^2}{2\sigma^2}\right)\]

Training raises the kernel value of the correct class and lowers the others, so the class clusters are pushed apart. The prediction is the class with the largest kernel value. An input that is far from every centroid is uncertain, either because it is OOD or because the model cannot decide between classes.

QUAM [2] starts from a trained model and, for each test input, fine-tunes several copies of it. Each copy tries to predict a different class for the input while keeping almost the same loss on the training data:

\[\max_{\delta \in \Delta} \; D\big(p(y \mid x, w),\, p(y \mid x, w + \delta)\big) + c\,\big(\log p(w + \delta \mid \mathcal{D}) - \log p(w \mid \mathcal{D}) + \gamma\big)\]

where \(D\) is usually the KL divergence. If such adversarial models can disagree on an input, the input is uncertain. The estimates are very good, but the method needs several fine-tuning runs for every test input.

Setup

A LeNet classifier (about 60k parameters) was trained on 10,000 randomly sampled MNIST training images. The in-distribution (ID) test set has 1,000 MNIST test images. The OOD sets have 500 images of non-MNIST digits and 1,000 ImageNet images.

Results

With DUQ, the learned features of the digit classes form well-separated clusters (Figure 1, left). The mean uncertainty score is 14.27 for ID images and 24,313.89 for OOD images. Rejecting the most uncertain test images raises the accuracy steadily, from 79% with no rejection to almost 100% when 70% of the images are rejected (Figure 1, right).

t-SNE plot of the LeNet features of MNIST images, coloured by digit class
DUQ accuracy against the percentage of the most uncertain test images excluded
Figure 1: DUQ on MNIST. Left: t-SNE of the learned features. Right: accuracy after excluding the most uncertain test images.

When zero-mean Gaussian noise of increasing standard deviation is added to test images, both the mean and the spread of the DUQ uncertainty grow (Figure 2). Adding more and more noise and following the uncertainty is therefore also a way to compare methods.

Mean DUQ uncertainty against the standard deviation of added Gaussian noise
Box plots of DUQ uncertainty for noise standard deviations from 0 to 1
Figure 2: DUQ uncertainty under added Gaussian noise: mean (left) and distribution (right).

QUAM can start from a pretrained model, so its accuracy is high (about 97.7% before any rejection). Its uncertainty, the entropy of the output distribution, separates the input types clearly: the mean is 0.232 for MNIST images, 0.456 for other digit images (OOD but the same classes) and 2.251 for ImageNet images (OOD and different classes).

To compare the two methods on one scale, we proposed the area under the accuracy-versus-rejection curve (Figure 3, right). QUAM’s area is 7,361.92 and DUQ’s is 6,773.83, so QUAM gives the better uncertainty estimates.

QUAM accuracy against the percentage of the most uncertain test images removed
Accuracy against the percentage of the most uncertain points excluded, for QUAM and DUQ
Figure 3: Left: QUAM accuracy after removing the most uncertain test images. Right: QUAM and DUQ on the same axes; the area under each curve is the proposed benchmark.

Part 2: Aleatoric uncertainty with a GMM and test-time augmentation

Background

For a model \(\hat{p}_{Y \mid X}\) trained on data \(\mathcal{D}\), with mean prediction \(\hat{y}\), the expected squared error at an input \(x\) splits into three terms:

\[\mathbb{E}_{Y \mid X}\Big[\mathbb{E}_{\mathcal{D}}\big[(Y - \hat{y})^2\big] \,\Big|\, x\Big] = \underbrace{\operatorname{Var}_{Y \mid X}(Y \mid x)}_{\text{aleatoric}} + \underbrace{\operatorname{Var}_{\mathcal{D}}(\hat{y} \mid x)}_{\text{epistemic}} + \operatorname{bias}(\hat{y} \mid x)^2\]

More data and larger models can reduce the epistemic term and the bias, but not the aleatoric term, which comes from noise in the data itself.

DDU [3] estimates both kinds of uncertainty with a single deterministic network. A residual network is trained with spectral normalisation [4], which keeps its feature space smooth and sensitive to the input. A GMM with one Gaussian per class is then fitted to the features of the training images. The negative log-density of an image’s features under the GMM, \(I(x)\), measures epistemic uncertainty. The predictive entropy \(H[p(y \mid x)]\) measures aleatoric uncertainty, but it is only reliable when the epistemic uncertainty is low. Test sets that also contain unlabelled OOD images are common in medical imaging, and for those images the entropy is unreliable.

Proposed metric

We train a ResNet-18 and fit the GMM exactly as in DDU, and we add test-time augmentation. Each test image \(x_o\) is rotated in steps of 4° between −20° and 20°, which gives 10 rotated copies; together with the original they form the set \(x_r\). The aleatoric uncertainty is then

\[U_{\text{alea}}(x) = H[p(y \mid x_o)] \cdot \frac{\operatorname{Std}_r\, I(x_r)}{\big\lvert\, I(x_o) - \max_{x'} I(x') - \epsilon \,\big\rvert}\]

where the maximum is taken over the test set and \(\epsilon\) is a small constant.

  • The numerator grows when small rotations make the GMM density of an image unstable, which happens for noisy and ambiguous images.
  • The denominator is small for images whose epistemic uncertainty is close to the largest in the test set. Likely OOD images therefore also get a large score, and they are rejected early instead of being judged by their unreliable entropy.

Datasets

We trained models on the HAM10000 skin-lesion images [5] and on CheXpert chest X-rays [6]. For HAM we used three classes (nv, bkl and mel), since the other classes have too few training images to estimate a class covariance matrix. CIFAR-10 and MNIST serve as further OOD data, and CIFAR-10 is also the target task of the active-learning experiment. All images are resized to 32 × 32.

Epistemic uncertainty

For a model trained on one dataset, the epistemic uncertainty is clearly higher on average for every other dataset than for its own test images (Figure 4). Adding Gaussian noise of increasing standard deviation to ID images moves them away from the training distribution, and their epistemic uncertainty grows accordingly (Figure 5).

Box plots of epistemic uncertainty for CheXpert, CIFAR-10, MNIST and HAM test images, for a model trained on CheXpert
Box plots of epistemic uncertainty for CheXpert, CIFAR-10, MNIST and HAM test images, for a model trained on HAM
Figure 4: Epistemic uncertainty (negative log GMM density) of the test images of each dataset, for a model trained on CheXpert (left) and on HAM (right).
Epistemic uncertainty of CheXpert images against the standard deviation of added Gaussian noise
Epistemic uncertainty of HAM images against the standard deviation of added Gaussian noise
Figure 5: Epistemic uncertainty of ID images with added Gaussian noise, for CheXpert (left) and HAM (right).

Aleatoric uncertainty

We evaluate the metric by rejecting the test images with the highest uncertainty and measuring the accuracy on the rest.

ID images only. On HAM test images, our metric and DDU’s predictive entropy behave similarly, with ours slightly ahead: 96.9% against 95.9% accuracy when 90% of the images are rejected (Figure 6).

Accuracy against the percentage of HAM test images rejected, for predictive entropy (DDU) and the proposed metric
Figure 6: Accuracy after rejecting the most uncertain HAM test images, for DDU's predictive entropy and our metric.

ID and OOD images. The model trained on HAM is tested on a 50:50 mix of HAM and CheXpert images (Figure 7, left). We compare our metric with:

  • DDU’s predictive entropy;
  • first removing likely OOD images by epistemic uncertainty and then rejecting by entropy (“OOD → entropy”);
  • the product of entropy and epistemic uncertainty, which uses the epistemic value of the original image instead of its spread under TTA.

Our metric is the most robust of these. At 90% rejection it reaches 91.7%, against 85.2% for DDU (+6.5 points), which comes second. The product of entropy and epistemic uncertainty peaks at 82% and then drops to 79%, and “OOD → entropy” fails, falling to 13–21% at high rejection rates. For the model trained on CheXpert (Figure 7, right), our metric is about 2 points better than DDU (95.4% vs 93.5% at 90% rejection).

Accuracy against the percentage of rejected images for a HAM model on a mix of HAM and CheXpert images, for four uncertainty metrics
Accuracy against the percentage of rejected images for a CheXpert model, for DDU and the proposed metric
Figure 7: Accuracy after rejection when the test set mixes ID and OOD images. Left: model trained on HAM, with a 50:50 mix of HAM and CheXpert images. Right: model trained on CheXpert.

Number of rotations. For the HAM model on the HAM + CheXpert mix, 5 rotated copies already give most of the improvement over DDU, and 10 are enough:

Rotated copies (N) Accuracy gain over DDU (points)
5 5.57
10 6.5
20 6.5
40 6.5

Active-learning benchmark

A good aleatoric-uncertainty metric should single out training examples that do not help the model. We turned this into a benchmark for comparing aleatoric-uncertainty estimates.

  • The unlabelled pool mixes CIFAR-10 and HAM images. An oracle labels CIFAR-10 images correctly but gives HAM images a random label, so the HAM images act as label noise for a CIFAR-10 classifier.
  • Two models are trained in rounds on growing labelled sets, for 100 epochs per round. One model receives randomly chosen pool images. The other receives the images with the highest aleatoric uncertainty.
  • If the metric is good, the second model receives mostly the noisy images and learns less.

At 10,000 labelled images, the model trained on random samples reaches 65.1% and the model trained on the most uncertain samples 57.4%, a gap of almost 8 points (Figure 8). This confirms that our metric picks out the noisy images.

CIFAR-10 accuracy against the number of labelled samples, for random sampling and for sampling the images with the highest aleatoric uncertainty
Figure 8: Active learning on CIFAR-10 with noisy HAM images in the pool: accuracy of a model trained on random samples and of a model trained on the samples with the highest aleatoric uncertainty.

References

[1] J. van Amersfoort, L. Smith, Y. W. Teh and Y. Gal, “Uncertainty estimation using a single deep deterministic neural network,” ICML 2020, arXiv:2003.02037.

[2] K. Schweighofer, L. Aichberger, M. Ielanskyi, G. Klambauer and S. Hochreiter, “Quantification of uncertainty with adversarial models,” NeurIPS 2023, arXiv:2307.03217.

[3] J. Mukhoti, A. Kirsch, J. van Amersfoort, P. H. S. Torr and Y. Gal, “Deep deterministic uncertainty: A new simple baseline,” CVPR 2023, arXiv:2102.11582.

[4] T. Miyato, T. Kataoka, M. Koyama and Y. Yoshida, “Spectral normalization for generative adversarial networks,” ICLR 2018, arXiv:1802.05957.

[5] P. Tschandl, C. Rosendahl and H. Kittler, “The HAM10000 dataset, a large collection of multi-source dermatoscopic images of common pigmented skin lesions,” Scientific Data, vol. 5, 180161, 2018, doi:10.1038/sdata.2018.161.

[6] J. Irvin et al., “CheXpert: A large chest radiograph dataset with uncertainty labels and expert comparison,” AAAI 2019, arXiv:1901.07031.