kornia.contrib

extract_tensor_patches(input: torch.Tensor, window_size: Union[int, Tuple[int, int]], stride: Union[int, Tuple[int, int]] = 1, padding: Union[int, Tuple[int, int]] = 0) → torch.Tensor[source]

Function that extract patches from tensors and stack them.

See ExtractTensorPatches for details.

max_blur_pool2d(input: torch.Tensor, kernel_size: int, ceil_mode: bool = False) → torch.Tensor[source]

Creates a module that computes pools and blurs and downsample a given feature map.

See MaxBlurPool2d for details.

Module

class ExtractTensorPatches(window_size: Union[int, Tuple[int, int]], stride: Optional[Union[T, Tuple[T, T]]] = 1, padding: Optional[Union[T, Tuple[T, T]]] = 0)[source]

Module that extract patches from tensors and stack them.

In the simplest case, the output value of the operator with input size \((B, C, H, W)\) is \((B, N, C, H_{out}, W_{out})\).

where
  • \(B\) is the batch size.

  • \(N\) denotes the total number of extracted patches stacked in

  • \(C\) denotes the number of input channels.

  • \(H\), \(W\) the input height and width of the input in pixels.

  • \(H_{out}\), \(W_{out}\) denote to denote to the patch size defined in the function signature. left-right and top-bottom order.

  • window_size is the size of the sliding window and controls the shape of the output tensor and defines the shape of the output patch.

  • stride controls the stride to apply to the sliding window and regulates the overlapping between the extracted patches.

  • padding controls the amount of implicit zeros-paddings on both sizes at each dimension.

The parameters window_size, stride and padding can be either:

  • a single int – in which case the same value is used for the height and width dimension.

  • a tuple of two ints – in which case, the first int is used for the height dimension, and the second int for the width dimension.

Parameters
  • window_size (Union[int, Tuple[int, int]]) – the size of the sliding window and the output patch size.

  • stride (Optional[Union[int, Tuple[int, int]]]) – stride of the sliding window. Default is 1.

  • padding (Optional[Union[int, Tuple[int, int]]]) – Zero-padding added to both side of the input. Default is 0.

Shape:
  • Input: \((B, C, H, W)\)

  • Output: \((B, N, C, H_{out}, W_{out})\)

Returns

the tensor with the extracted patches.

Return type

torch.Tensor

Examples

>>> input = torch.arange(9.).view(1, 1, 3, 3)
>>> patches = extract_tensor_patches(input, (2, 3))
>>> input
tensor([[[[0., 1., 2.],
          [3., 4., 5.],
          [6., 7., 8.]]]])
>>> patches[:, -1]
tensor([[[[3., 4., 5.],
          [6., 7., 8.]]]])
class MaxBlurPool2d(kernel_size: int, ceil_mode: bool = False)[source]

Compute pools and blurs and downsample a given feature map.

Equivalent to `nn.Sequential(nn.MaxPool2d(...), BlurPool2D(...))` See [Zha19] for more details.

Parameters
  • kernel_size (int) – the kernel size for max pooling.

  • stride (int) – stride for pooling.

  • max_pool_size (int) – the kernel size for max pooling.

  • ceil_mode (bool) – should be true to match output size of conv2d with same kernel size.

Shape:
  • Input: \((B, C, H, W)\)

  • Output: \((B, C, H / stride, W / stride)\)

Returns

the transformed tensor.

Return type

torch.Tensor

Examples

>>> input = torch.eye(5)[None, None]
>>> mbp = MaxBlurPool2D(kernel_size=3, stride=2, max_pool_size=2, ceil_mode=False)
>>> mbp(input)
tensor([[[[0.5625, 0.3125],
          [0.3125, 0.8750]]]])
>>> seq = nn.Sequential(nn.MaxPool2d(kernel_size=2, stride=1), BlurPool2D(kernel_size=3, stride=2))
>>> seq(input)
tensor([[[[0.5625, 0.3125],
          [0.3125, 0.8750]]]])