DPSDataFidelity#

class deepinv.sampling.DPSDataFidelity(denoiser=None, weight=1.0, clip=None, *args, **kwargs)[source]#

Bases: NoisyDataFidelity

Diffusion posterior sampling data-fidelity term.

This corresponds to the \(p(y|x_t)\) approximation proposed in Diffusion Posterior Sampling for General Noisy Inverse Problems.

\[\begin{aligned} \nabla_x \log p_t(y|x) &= \nabla_x \frac{\lambda}{2\sqrt{m}} \| \forw{\denoiser{x}{\sigma}} - y \| \end{aligned}\]

where \(\sigma = \sigma(t)\) is the noise level, \(m\) is the number of measurements (size of \(y\)), and \(\lambda\) controls the strength of the approximation.

See also

This class can be used for building custom DPS-based diffusion models. A self-contained implementation of the original DPS algorithm can be find in deepinv.sampling.DPS.

Parameters:
  • denoiser (deepinv.models.Denoiser) – Denoiser network

  • weight (float) – Weighting factor for the data fidelity term. Default to 100.

  • clip (tuple[float]) – If not None, clip the denoised output into [clip[0], clip[1]] interval. Default to None.

forward(x, y, physics, sigma, *args, get_model_outputs=False, **kwargs)[source]#

Returns the loss term \(\frac{\lambda}{2\sqrt{m}} \| \forw{\denoiser{x}{\sigma}} - y \|\).

Parameters:
  • x (torch.Tensor) – input image

  • y (torch.Tensor) – measurements

  • physics (deepinv.physics.Physics) – forward operator

  • sigma (float) – standard deviation of the noise.

  • get_model_outputs (bool) – If True, also return the denoised output along with the loss. Default to False.

Returns:

(torch.Tensor or tuple of torch.Tensor) loss term (and denoised output if get_model_outputs is True).

Return type:

Tensor | tuple[Tensor, Tensor]

grad(x, y, physics, sigma, *args, get_model_outputs=False, **kwargs)[source]#
Parameters:
  • x (torch.Tensor) – Current iterate.

  • y (torch.Tensor) – Input data.

  • physics (deepinv.physics.Physics) – physics model

  • sigma (float) – Standard deviation of the noise.

  • get_model_outputs (bool) – If True, also return the denoised output along with the score. Default to False.

Returns:

(torch.Tensor or tuple of torch.Tensor) score term (and denoised output if get_model_outputs is True).

Return type:

Tensor | tuple[Tensor, Tensor]

Examples using DPSDataFidelity:#

Using state-of-the-art diffusion models from HuggingFace Diffusers with DeepInverse

Using state-of-the-art diffusion models from HuggingFace Diffusers with DeepInverse

Building your diffusion posterior sampling method using SDEs

Building your diffusion posterior sampling method using SDEs

Flow-Matching for posterior sampling and unconditional generation

Flow-Matching for posterior sampling and unconditional generation