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 1952.125 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 0x7fec5bb46ed0>

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]
  2%|▏         | 93/5000 [00:00<00:05, 926.69it/s]
  4%|▍         | 195/5000 [00:00<00:04, 979.43it/s]
  6%|β–Œ         | 296/5000 [00:00<00:04, 992.89it/s]
  8%|β–Š         | 399/5000 [00:00<00:04, 1007.23it/s]
 10%|β–ˆ         | 502/5000 [00:00<00:04, 1015.22it/s]
 12%|β–ˆβ–        | 604/5000 [00:00<00:04, 1012.45it/s]
 14%|β–ˆβ–        | 707/5000 [00:00<00:04, 1018.06it/s]
 16%|β–ˆβ–Œ        | 810/5000 [00:00<00:04, 1021.79it/s]
 18%|β–ˆβ–Š        | 913/5000 [00:00<00:03, 1023.31it/s]
 20%|β–ˆβ–ˆ        | 1016/5000 [00:01<00:03, 1021.73it/s]
 22%|β–ˆβ–ˆβ–       | 1119/5000 [00:01<00:03, 1007.11it/s]
 24%|β–ˆβ–ˆβ–       | 1220/5000 [00:01<00:03, 997.31it/s]
 26%|β–ˆβ–ˆβ–‹       | 1320/5000 [00:01<00:03, 990.47it/s]
 28%|β–ˆβ–ˆβ–Š       | 1420/5000 [00:01<00:03, 985.78it/s]
 30%|β–ˆβ–ˆβ–ˆ       | 1519/5000 [00:01<00:03, 982.84it/s]
 32%|β–ˆβ–ˆβ–ˆβ–      | 1618/5000 [00:01<00:03, 980.75it/s]
 34%|β–ˆβ–ˆβ–ˆβ–      | 1717/5000 [00:01<00:03, 976.67it/s]
 36%|β–ˆβ–ˆβ–ˆβ–‹      | 1815/5000 [00:01<00:03, 951.72it/s]
 38%|β–ˆβ–ˆβ–ˆβ–Š      | 1913/5000 [00:01<00:03, 957.66it/s]
 40%|β–ˆβ–ˆβ–ˆβ–ˆ      | 2009/5000 [00:02<00:03, 955.80it/s]
 42%|β–ˆβ–ˆβ–ˆβ–ˆβ–     | 2106/5000 [00:02<00:03, 958.46it/s]
 44%|β–ˆβ–ˆβ–ˆβ–ˆβ–     | 2202/5000 [00:02<00:02, 958.40it/s]
 46%|β–ˆβ–ˆβ–ˆβ–ˆβ–Œ     | 2298/5000 [00:02<00:02, 958.24it/s]
 48%|β–ˆβ–ˆβ–ˆβ–ˆβ–Š     | 2396/5000 [00:02<00:02, 963.77it/s]
 50%|β–ˆβ–ˆβ–ˆβ–ˆβ–‰     | 2494/5000 [00:02<00:02, 968.01it/s]
 52%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–    | 2592/5000 [00:02<00:02, 970.39it/s]
 54%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–    | 2690/5000 [00:02<00:02, 971.65it/s]
 56%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Œ    | 2788/5000 [00:02<00:02, 972.95it/s]
 58%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Š    | 2886/5000 [00:02<00:02, 973.47it/s]
 60%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‰    | 2984/5000 [00:03<00:02, 973.98it/s]
 62%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–   | 3082/5000 [00:03<00:01, 974.57it/s]
 64%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Ž   | 3180/5000 [00:03<00:01, 975.03it/s]
 66%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Œ   | 3278/5000 [00:03<00:01, 975.31it/s]
 68%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Š   | 3376/5000 [00:03<00:01, 975.53it/s]
 69%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‰   | 3474/5000 [00:03<00:01, 975.72it/s]
 71%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–  | 3572/5000 [00:03<00:01, 975.05it/s]
 73%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Ž  | 3670/5000 [00:03<00:01, 974.97it/s]
 75%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Œ  | 3768/5000 [00:03<00:01, 974.55it/s]
 77%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‹  | 3866/5000 [00:03<00:01, 973.39it/s]
 79%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‰  | 3964/5000 [00:04<00:01, 966.76it/s]
 81%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  | 4061/5000 [00:04<00:00, 964.16it/s]
 83%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Ž | 4158/5000 [00:04<00:00, 958.46it/s]
 85%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Œ | 4254/5000 [00:04<00:00, 957.58it/s]
 87%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‹ | 4352/5000 [00:04<00:00, 961.48it/s]
 89%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‰ | 4449/5000 [00:04<00:00, 953.74it/s]
 91%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ | 4546/5000 [00:04<00:00, 956.86it/s]
 93%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Ž| 4642/5000 [00:04<00:00, 956.68it/s]
 95%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–| 4739/5000 [00:04<00:00, 960.18it/s]
 97%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‹| 4837/5000 [00:04<00:00, 963.85it/s]
 99%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–Š| 4934/5000 [00:05<00:00, 965.08it/s]
100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 5000/5000 [00:05<00:00, 975.51it/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 5.468 seconds)

Gallery generated by Sphinx-Gallery