DDRM
- class deepinv.sampling.DDRM(self, denoiser, sigmas=np.linspace(1, 0, 100), eta=0.85, etab=1.0, verbose=False)[source]
Bases:
Module
Denoising Diffusion Restoration Models (DDRM).
This class implements the denoising diffusion restoration model (DDRM) described in https://arxiv.org/abs/2201.11793.
The DDRM is a sampling method that uses a denoiser to sample from the posterior distribution of the inverse problem.
It requires that the physics operator has a singular value decomposition, i.e., it is
deepinv.physics.DecomposablePhysics()
class.- Parameters:
denoiser (torch.nn.Module) – a denoiser model that can handle different noise levels.
sigmas (list[int], numpy.array) – a list of noise levels to use in the diffusion, they should be in decreasing order from 1 to 0.
eta (float) – hyperparameter
etab (float) – hyperparameter
verbose (bool) – if True, print progress
- Examples:
Denoising diffusion restoration model using a pretrained DRUNet denoiser:
>>> import deepinv as dinv >>> device = dinv.utils.get_freer_gpu(verbose=False) if torch.cuda.is_available() else 'cpu' >>> seed = torch.manual_seed(0) # Random seed for reproducibility >>> seed = torch.cuda.manual_seed(0) # Random seed for reproducibility on GPU >>> x = 0.5 * torch.ones(1, 3, 32, 32, device=device) # Define plain gray 32x32 image >>> physics = dinv.physics.Inpainting( ... mask=0.5, tensor_size=(3, 32, 32), ... noise_model=dinv.physics.GaussianNoise(0.1), ... device=device, ... ) >>> y = physics(x) # measurements >>> denoiser = dinv.models.DRUNet(pretrained="download").to(device) >>> model = dinv.sampling.DDRM(denoiser=denoiser, sigmas=np.linspace(1, 0, 10), verbose=True) # define the DDRM model >>> xhat = model(y, physics) # sample from the posterior distribution >>> dinv.metric.PSNR()(xhat, x) > dinv.metric.PSNR()(y, x) # Should be closer to the original tensor([True])
- forward(y, physics: DecomposablePhysics, seed=None)[source]
Runs the diffusion to obtain a random sample of the posterior distribution.
- Parameters:
y (torch.Tensor) – the measurements.
physics (deepinv.physics.DecomposablePhysics) – the physics operator, which must have a singular value decomposition.
seed (int) – the seed for the random number generator.
Examples using DDRM
:
Image reconstruction with a diffusion model