Downsampling#

class deepinv.physics.Downsampling(img_size=None, filter='warn', factor=2, device='cpu', padding='circular', **kwargs)[source]#

Bases: LinearPhysics

Downsampling operator for super-resolution problems.

It is defined as

\[y = S (h*x)\]

where \(h\) is a low-pass filter and \(S\) is a subsampling operator.

Parameters:
  • filter (torch.Tensor, str, None) – Downsampling filter. It can be 'gaussian', 'bilinear', 'bicubic' , 'sinc' or a custom torch.Tensor filter. If None, no filtering is applied. Bicubic downsampling is a sensible default if you are not sure which filter to use.

  • img_size (tuple[int], None) – optional size of the high resolution image (C, H, W). If tuple, use this fixed image size. If None, override on-the-fly using input data size and factor (note that here, A_adjoint will only produce even img shapes).

  • factor (int) – downsampling factor

  • padding (str) – options are 'valid', 'circular', 'replicate' and 'reflect'. If padding='valid' the blurred output is smaller than the image (no padding) otherwise the blurred output has the same size as the image.

  • device (torch.device, str) – Device on which the physics’ buffers will be created. If a buffer is updated via physics.update_parameters(), if not None, it will be automatically casted to the device of the replaced buffer, else, use the device of the provided value. To change the device of all buffers, please use physics.to(device).


Examples:

Downsampling operator with a gaussian filter:

>>> from deepinv.physics import Downsampling
>>> x = torch.zeros((1, 1, 32, 32)) # Define black image of size 32x32
>>> x[:, :, 16, 16] = 1 # Define one white pixel in the middle
>>> physics = Downsampling(filter = "gaussian", img_size=(1, 32, 32), factor=2)
>>> y = physics(x)
>>> y[:, :, 7:10, 7:10] # Display the center of the downsampled image
tensor([[[[0.0146, 0.0241, 0.0146],
          [0.0241, 0.0398, 0.0241],
          [0.0146, 0.0241, 0.0146]]]])
A(x, filter=None, factor=None, **kwargs)[source]#

Applies the downsampling operator to the input image.

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

  • filter (None, str, torch.Tensor, list[str]) – Filter \(h\) to be applied to the input image before downsampling. If not None, it uses this filter and stores it as the current filter.

  • factor (int, float, torch.Tensor) – downsampling factor. If not None, use this factor and store it as current factor.

Warning

If factor is passed, filter must also be passed as a str or Tensor, in order to update the filter to the new factor.

A_adjoint(y, filter=None, factor=None, **kwargs)[source]#

Adjoint operator of the downsampling operator.

Parameters:
  • y (torch.Tensor) – downsampled image.

  • filter (None, str, torch.Tensor, list[str]) – Filter \(h\) to be applied to the input image before downsampling. If not None, it uses this filter and stores it as the current filter.

  • factor (int, float, torch.Tensor) – downsampling factor. If not None, use this factor and store it as current factor.

Warning

If factor is passed, filter must also be passed as a str or Tensor, in order to update the filter to the new factor.

static check_factor(factor)[source]#

Check new downsampling factor.

Parameters:

factor (int, float, torch.Tensor) – downsampling factor to be checked and cast to int. If torch.Tensor, it must be 1D and all its elements must be the same, since downsampling only supports one factor per batch.

Returns:

int: factor

Return type:

int

static get_filter_parameters(img_size=None, filter=None, factor=None, device='cpu')[source]#

Create a filter tensor with specified downsampling factor

Parameters:
  • img_size (tuple[int]) – size of the high resolution image (C, H, W).

  • filter (torch.Tensor, str, list[str]) – Filter name or tensor to be applied to the input image before downsampling.

  • factor (int, float, torch.Tensor) – Downsampling factor to be applied to the input image.

  • device (torch.device, str) – Device where the filter tensor will be created or pushed to.

prox_l2(z, y, gamma, use_fft=True, **kwargs)[source]#

If the padding is circular, it computes the proximal operator with the closed-formula of Zhu et al.[1].

Otherwise, it computes it using the conjugate gradient algorithm which can be slow if applied many times.


References:

update_parameters(filter=None, factor=None, device=None, **kwargs)[source]#

Updates the current filter and/or factor.

Parameters:
  • filter (torch.Tensor, str, list[str]) – New filter to be applied to the input image.

  • factor (int, float, torch.Tensor) – New downsampling factor to be applied to the input image.

  • device (torch.device, str) – When``self.filter`` is None and if filter is a str`, specifies the device where the new filter will be created. When``self.filter is None and filter is a torch.Tensor, the device is inferred from the provided filter tensor. Ignored otherwise.

Examples using Downsampling:#

Tour of forward sensing operators

Tour of forward sensing operators

Remote sensing with satellite images

Remote sensing with satellite images

Regularization by Denoising (RED) for Super-Resolution.

Regularization by Denoising (RED) for Super-Resolution.

Vanilla Unfolded algorithm for super-resolution

Vanilla Unfolded algorithm for super-resolution