Downsampling#
- class deepinv.physics.Downsampling(img_size=None, filter='warn', factor=2, device='cpu', padding='circular', **kwargs)[source]#
Bases:
LinearPhysicsDownsampling 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 customtorch.Tensorfilter. IfNone, 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). Iftuple, use this fixed image size. IfNone, override on-the-fly using input data size andfactor(note that here,A_adjointwill only produce even img shapes).factor (int) β downsampling factor
padding (str) β options are
'valid','circular','replicate'and'reflect'. Ifpadding='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 usephysics.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
factoris passed,filtermust also be passed as astrorTensor, 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
factoris passed,filtermust also be passed as astrorTensor, 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. Iftorch.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:
- 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
Noneand iffilteris astr`, specifies the device where the new filter will be created. When``self.filterisNoneandfilteris atorch.Tensor, the device is inferred from the providedfiltertensor. Ignored otherwise.
Examples using Downsampling:#
Regularization by Denoising (RED) for Super-Resolution.