.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/demo_sampling.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note New to DeepInverse? Get started with the basics with the :ref:`5 minute quickstart tutorial `. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_sampling_demo_sampling.py: 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: .. math:: 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 :math:`z_k \sim \mathcal{N}(0, I)` is a Gaussian random variable, :math:`\eta` is the step size and :math:`\alpha` is a parameter controlling the regularization. The PnP-ULA method is described in the paper :footcite:t:`laumont2022bayesian`. .. GENERATED FROM PYTHON SOURCE LINES 21-26 .. code-block:: Python import deepinv as dinv from deepinv.utils.plotting import plot import torch from deepinv.utils.demo import load_example .. GENERATED FROM PYTHON SOURCE LINES 27-31 Load image from the internet -------------------------------------------- This example uses an image of Messi. .. GENERATED FROM PYTHON SOURCE LINES 31-36 .. code-block:: Python device = dinv.utils.get_freer_gpu() if torch.cuda.is_available() else "cpu" x = load_example("messi.jpg", img_size=32).to(device) .. GENERATED FROM PYTHON SOURCE LINES 37-41 Define forward operator and noise model -------------------------------------------------------------- This example uses inpainting as the forward operator and Gaussian noise as the noise model. .. GENERATED FROM PYTHON SOURCE LINES 41-49 .. code-block:: Python 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) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 50-57 Define the likelihood -------------------------------------------------------------- Since the noise model is Gaussian, the negative log-likelihood is the L2 loss. .. math:: -\log p(y|x) \propto \frac{1}{2\sigma^2} \|y-Ax\|^2 .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python # load Gaussian Likelihood likelihood = dinv.optim.data_fidelity.L2(sigma=sigma) .. GENERATED FROM PYTHON SOURCE LINES 62-80 Define the prior ------------------------------------------- The score a distribution can be approximated using Tweedie's formula via the :class:`deepinv.optim.ScorePrior` class. .. math:: \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`` (:math:`sigma`) controls the strength of the prior. In this example, we use a pretrained DnCNN model using the :class:`deepinv.loss.FNEJacobianSpectralNorm` loss, which makes sure that the denoiser is firmly non-expansive (see :footcite:t:`terris2020building`), and helps to stabilize the sampling algorithm. .. GENERATED FROM PYTHON SOURCE LINES 80-86 .. code-block:: Python sigma_denoiser = 2 / 255 prior = dinv.optim.ScorePrior( denoiser=dinv.models.DnCNN(pretrained="download_lipschitz") ).to(device) .. rst-class:: sphx-glr-script-out .. code-block:: none Downloading: "https://huggingface.co/deepinv/dncnn/resolve/main/dncnn_sigma2_lipschitz_color.pth?download=true" to /home/runner/.cache/torch/hub/checkpoints/dncnn_sigma2_lipschitz_color.pth 0%| | 0.00/2.56M [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_sampling.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_sampling.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_