Uncertainty quantification with PnP-ULA.#

This code shows you how to use sampling algorithms to quantify uncertainty of a reconstruction from incomplete and noisy measurements.

ULA obtains samples by running the following iteration:

\[x_{k+1} = x_k + \alpha \eta \nabla \log p_{\sigma}(x_k) + \eta \nabla \log p(y|x_k) + \sqrt{2 \eta} z_k\]

where \(z_k \sim \mathcal{N}(0, I)\) is a Gaussian random variable, \(\eta\) is the step size and \(\alpha\) is a parameter controlling the regularization.

The PnP-ULA method is described in the paper Laumont et al.[1].

import deepinv as dinv
from deepinv.utils.plotting import plot
import torch
from deepinv.utils import load_example

Load image from the internet#

This example uses an image of Messi.

device = dinv.utils.get_freer_gpu() if torch.cuda.is_available() else "cpu"

x = load_example("messi.jpg", img_size=32).to(device)
Selected GPU 0 with 4949.25 MiB free memory

Define forward operator and noise model#

This example uses inpainting as the forward operator and Gaussian noise as the noise model.

sigma = 0.1  # noise level
physics = dinv.physics.Inpainting(mask=0.5, img_size=x.shape[1:], device=device)
physics.noise_model = dinv.physics.GaussianNoise(sigma=sigma)

# Set the global random seed from pytorch to ensure reproducibility of the example.
torch.manual_seed(0)
<torch._C.Generator object at 0x7f1228f32e90>

Define the likelihood#

Since the noise model is Gaussian, the negative log-likelihood is the L2 loss.

\[-\log p(y|x) \propto \frac{1}{2\sigma^2} \|y-Ax\|^2\]
# load Gaussian Likelihood
likelihood = dinv.optim.data_fidelity.L2(sigma=sigma)

Define the prior#

The score a distribution can be approximated using Tweedie’s formula via the deepinv.optim.ScorePrior class.

\[\nabla \log p_{\sigma}(x) \approx \frac{1}{\sigma^2} \left(D(x,\sigma)-x\right)\]

This example uses a pretrained DnCNN model. From a Bayesian point of view, the score plays the role of the gradient of the negative log prior The hyperparameter sigma_denoiser (\(sigma\)) controls the strength of the prior.

In this example, we use a pretrained DnCNN model using the deepinv.loss.FNEJacobianSpectralNorm loss, which makes sure that the denoiser is firmly non-expansive (see Terris et al.[2]), and helps to stabilize the sampling algorithm.

sigma_denoiser = 2 / 255
prior = dinv.optim.ScorePrior(
    denoiser=dinv.models.DnCNN(pretrained="download_lipschitz")
).to(device)

Create the MCMC sampler#

Here we use the Unadjusted Langevin Algorithm (ULA) to sample from the posterior defined in deepinv.sampling.ULAIterator. The hyperparameter step_size controls the step size of the MCMC sampler, regularization controls the strength of the prior and iterations controls the number of iterations of the sampler.

regularization = 0.9
step_size = 0.01 * (sigma**2)
iterations = int(5e3) if torch.cuda.is_available() else 10
params = {
    "step_size": step_size,
    "alpha": regularization,
    "sigma": sigma_denoiser,
}
f = dinv.sampling.sampling_builder(
    "ULA",
    prior=prior,
    data_fidelity=likelihood,
    max_iter=iterations,
    params_algo=params,
    thinning=1,
    verbose=True,
)

Generate the measurement#

We apply the forward model to generate the noisy measurement.

y = physics(x)

Run sampling algorithm and plot results#

The sampling algorithm returns the posterior mean and variance. We compare the posterior mean with a simple linear reconstruction.

mean, var = f.sample(y, physics)

# compute linear inverse
x_lin = physics.A_adjoint(y)

# compute PSNR
print(f"Linear reconstruction PSNR: {dinv.metric.PSNR()(x, x_lin).item():.2f} dB")
print(f"Posterior mean PSNR: {dinv.metric.PSNR()(x, mean).item():.2f} dB")

# plot results
error = (mean - x).abs().sum(dim=1).unsqueeze(1)  # per pixel average abs. error
std = var.sum(dim=1).unsqueeze(1).sqrt()  # per pixel average standard dev.
imgs = [x_lin, x, mean, std / std.flatten().max(), error / error.flatten().max()]
plot(
    imgs,
    titles=["measurement", "ground truth", "post. mean", "post. std", "abs. error"],
)
measurement, ground truth, post. mean, post. std, abs. error
  0%|          | 0/5000 [00:00<?, ?it/s]
  1%|          | 58/5000 [00:00<00:08, 577.23it/s]
  2%|β–Ž         | 125/5000 [00:00<00:07, 628.88it/s]
  4%|▍         | 192/5000 [00:00<00:07, 642.05it/s]
  5%|β–Œ         | 263/5000 [00:00<00:07, 666.67it/s]
  7%|β–‹         | 330/5000 [00:00<00:07, 661.81it/s]
  8%|β–Š         | 409/5000 [00:00<00:06, 704.75it/s]
 10%|β–‰         | 493/5000 [00:00<00:06, 748.00it/s]
 12%|β–ˆβ–        | 578/5000 [00:00<00:05, 777.70it/s]
 13%|β–ˆβ–Ž        | 662/5000 [00:00<00:05, 795.95it/s]
 15%|β–ˆβ–        | 746/5000 [00:01<00:05, 809.43it/s]
 17%|β–ˆβ–‹        | 834/5000 [00:01<00:05, 828.94it/s]
 18%|β–ˆβ–Š        | 924/5000 [00:01<00:04, 850.27it/s]
 20%|β–ˆβ–ˆ        | 1016/5000 [00:01<00:04, 868.52it/s]
 22%|β–ˆβ–ˆβ–       | 1103/5000 [00:01<00:04, 868.07it/s]
 24%|β–ˆβ–ˆβ–       | 1191/5000 [00:01<00:04, 869.27it/s]
 26%|β–ˆβ–ˆβ–Œ       | 1279/5000 [00:01<00:04, 870.78it/s]
 27%|β–ˆβ–ˆβ–‹       | 1367/5000 [00:01<00:04, 870.96it/s]
 29%|β–ˆβ–ˆβ–‰       | 1455/5000 [00:01<00:04, 856.78it/s]
 31%|β–ˆβ–ˆβ–ˆ       | 1541/5000 [00:01<00:04, 845.21it/s]
 33%|β–ˆβ–ˆβ–ˆβ–Ž      | 1626/5000 [00:02<00:04, 839.50it/s]
 34%|β–ˆβ–ˆβ–ˆβ–      | 1710/5000 [00:02<00:03, 834.59it/s]
 36%|β–ˆβ–ˆβ–ˆβ–Œ      | 1794/5000 [00:02<00:03, 827.98it/s]
 38%|β–ˆβ–ˆβ–ˆβ–Š      | 1880/5000 [00:02<00:03, 835.90it/s]
 39%|β–ˆβ–ˆβ–ˆβ–‰      | 1965/5000 [00:02<00:03, 839.85it/s]
 41%|β–ˆβ–ˆβ–ˆβ–ˆ      | 2051/5000 [00:02<00:03, 843.42it/s]
 43%|β–ˆβ–ˆβ–ˆβ–ˆβ–Ž     | 2136/5000 [00:02<00:03, 843.23it/s]
 44%|β–ˆβ–ˆβ–ˆβ–ˆβ–     | 2222/5000 [00:02<00:03, 846.16it/s]
 46%|β–ˆβ–ˆβ–ˆβ–ˆβ–Œ     | 2308/5000 [00:02<00:03, 848.62it/s]
 48%|β–ˆβ–ˆβ–ˆβ–ˆβ–Š     | 2394/5000 [00:02<00:03, 849.61it/s]
 50%|β–ˆβ–ˆβ–ˆβ–ˆβ–‰     | 2480/5000 [00:03<00:02, 850.35it/s]
 51%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–    | 2566/5000 [00:03<00:02, 851.54it/s]
 53%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Ž    | 2652/5000 [00:03<00:02, 851.38it/s]
 55%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–    | 2738/5000 [00:03<00:02, 850.12it/s]
 56%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‹    | 2824/5000 [00:03<00:02, 847.74it/s]
 58%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Š    | 2910/5000 [00:03<00:02, 849.92it/s]
 60%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‰    | 2996/5000 [00:03<00:02, 851.22it/s]
 62%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–   | 3082/5000 [00:03<00:02, 851.03it/s]
 63%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Ž   | 3168/5000 [00:03<00:02, 851.79it/s]
 65%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Œ   | 3254/5000 [00:03<00:02, 851.43it/s]
 67%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‹   | 3340/5000 [00:04<00:01, 843.52it/s]
 68%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Š   | 3425/5000 [00:04<00:01, 827.42it/s]
 70%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ   | 3508/5000 [00:04<00:01, 819.85it/s]
 72%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–  | 3591/5000 [00:04<00:01, 819.49it/s]
 74%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Ž  | 3676/5000 [00:04<00:01, 828.10it/s]
 75%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Œ  | 3762/5000 [00:04<00:01, 835.78it/s]
 77%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‹  | 3848/5000 [00:04<00:01, 841.25it/s]
 79%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Š  | 3934/5000 [00:04<00:01, 844.38it/s]
 80%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  | 4020/5000 [00:04<00:01, 846.73it/s]
 82%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ– | 4106/5000 [00:04<00:01, 848.58it/s]
 84%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ– | 4192/5000 [00:05<00:00, 849.61it/s]
 86%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Œ | 4278/5000 [00:05<00:00, 850.35it/s]
 87%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‹ | 4364/5000 [00:05<00:00, 851.84it/s]
 89%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‰ | 4450/5000 [00:05<00:00, 850.42it/s]
 91%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ | 4536/5000 [00:05<00:00, 851.45it/s]
 92%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–| 4622/5000 [00:05<00:00, 852.24it/s]
 94%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–| 4708/5000 [00:05<00:00, 851.99it/s]
 96%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Œ| 4794/5000 [00:05<00:00, 851.96it/s]
 98%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Š| 4880/5000 [00:05<00:00, 852.31it/s]
 99%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‰| 4966/5000 [00:05<00:00, 853.25it/s]
100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 5000/5000 [00:06<00:00, 830.71it/s]
Iteration 4999, current converge crit. = 1.43E-05, objective = 1.00E-03
Iteration 4999, current converge crit. = 3.42E-04, objective = 1.00E-03
Linear reconstruction PSNR: 8.55 dB
Posterior mean PSNR: 22.31 dB
References:

Total running time of the script: (0 minutes 6.472 seconds)

Gallery generated by Sphinx-Gallery