paddle gaussian_blur2d

参考代码
https://github.com/kornia/kornia/blob/master/kornia/filters/kernels.py
https://kornia.readthedocs.io/en/latest/_modules/kornia/filters/gaussian.html?highlight=gaussian_blur2d#
转成paddle版本

实现以下函数

  1. _compute_padding
  2. normalize_kernel2d
  3. get_gaussian_kernel1d
  4. get_gaussian_kernel2d
  5. filter2d
  6. filter2d_separable
  7. gaussian_blur2d
 import paddle
from typing import List, Optional, Tuple
import paddle.nn.functional as F

def _compute_padding(kernel_size: List[int]) -> List[int]:
    """Compute padding tuple."""
    # 4 or 6 ints:  (padding_left, padding_right,padding_top,padding_bottom)
    if len(kernel_size) < 2:
        raise AssertionError(kernel_size)
    computed = [k - 1 for k in kernel_size]

    # for even kernels we need to do asymmetric padding :(
    out_padding = 2 * len(kernel_size) * [0]

    for i in range(len(kernel_size)):
        computed_tmp = computed[-(i + 1)]

        pad_front = computed_tmp // 2
        pad_rear = computed_tmp - pad_front

        out_padding[2 * i + 0] = pad_front
        out_padding[2 * i + 1] = pad_rear

    return out_padding
    

def normalize_kernel2d(input: paddle.Tensor) -> paddle.Tensor:
    r"""Normalize both derivative and smoothing kernel."""
    if len(input.size()) < 2:
        raise TypeError(f"input should be at least 2D tensor. Got {input.size()}")
    input = paddle.abs(input)
    norm: paddle.Tensor = paddle.sum(paddle.sum(input, axis=-1), axis=-1)
    return input / paddle.unsqueeze(paddle.unsqueeze(norm, axis=-1), axis=-1)


def gaussian(window_size: int, sigma: float) -> paddle.Tensor:
    device, dtype = None, None
    if isinstance(sigma, paddle.Tensor):
         dtype =   sigma.dtype
        # paddle.arange(start=0, end=None, step=1, dtype=None, name=None)
    x = paddle.arange(window_size, dtype=dtype) - window_size // 2
    if window_size % 2 == 0:
        x = x + 0.5
    gauss = paddle.exp((-paddle.pow(x, 2.0) / (2 * sigma ** 2)))
    return gauss / paddle.sum(gauss)


def get_gaussian_kernel1d(kernel_size: int, sigma: float, force_even: bool = False) -> paddle.Tensor:
    r"""Function that returns Gaussian filter coefficients.
    Args:
        kernel_size: filter size. It should be odd and positive.
        sigma: gaussian standard deviation.
        force_even: overrides requirement for odd kernel size.
    Returns:
        1D tensor with gaussian filter coefficients.
    Shape:
        - Output: :math:`(\text{kernel_size})`
    Examples:
        >>> get_gaussian_kernel1d(3, 2.5)
        tensor([0.3243, 0.3513, 0.3243])
        >>> get_gaussian_kernel1d(5, 1.5)
        tensor([0.1201, 0.2339, 0.2921, 0.2339, 0.1201])
    """
    if not isinstance(kernel_size, int) or ((kernel_size % 2 == 0) and not force_even) or (kernel_size <= 0):
        raise TypeError("kernel_size must be an odd positive integer. " "Got {}".format(kernel_size))
    window_1d: paddle.Tensor = gaussian(kernel_size, sigma)
    return window_1d

def get_gaussian_kernel2d(
    kernel_size: Tuple[int, int], sigma: Tuple[float, float], force_even: bool = False
) -> paddle.Tensor:
    r"""Function that returns Gaussian filter matrix coefficients.
    Args:
        kernel_size: filter sizes in the x and y direction.
         Sizes should be odd and positive.
        sigma: gaussian standard deviation in the x and y
         direction.
        force_even: overrides requirement for odd kernel size.
    Returns:
        2D tensor with gaussian filter matrix coefficients.
    Shape:
        - Output: :math:`(\text{kernel_size}_x, \text{kernel_size}_y)`
    Examples:
        >>> get_gaussian_kernel2d((3, 3), (1.5, 1.5))
        tensor([[0.0947, 0.1183, 0.0947],
                [0.1183, 0.1478, 0.1183],
                [0.0947, 0.1183, 0.0947]])
        >>> get_gaussian_kernel2d((3, 5), (1.5, 1.5))
        tensor([[0.0370, 0.0720, 0.0899, 0.0720, 0.0370],
                [0.0462, 0.0899, 0.1123, 0.0899, 0.0462],
                [0.0370, 0.0720, 0.0899, 0.0720, 0.0370]])
    """
    if not isinstance(kernel_size, tuple) or len(kernel_size) != 2:
        raise TypeError(f"kernel_size must be a tuple of length two. Got {kernel_size}")
    if not isinstance(sigma, tuple) or len(sigma) != 2:
        raise TypeError(f"sigma must be a tuple of length two. Got {sigma}")
    ksize_x, ksize_y = kernel_size
    sigma_x, sigma_y = sigma
    kernel_x: paddle.Tensor = get_gaussian_kernel1d(ksize_x, sigma_x, force_even)
    kernel_y: paddle.Tensor = get_gaussian_kernel1d(ksize_y, sigma_y, force_even)
    kernel_x = paddle.unsqueeze(kernel_x, axis=-1)
    kernel_y = paddle.unsqueeze(kernel_y, axis=-1).t()
    kernel_2d: paddle.Tensor = paddle.matmul(kernel_x, kernel_y)
    return kernel_2d


def filter2d(
    input: paddle.Tensor, kernel: paddle.Tensor, border_type: str = 'reflect', normalized: bool = False,
    padding: str = 'same'
) -> paddle.Tensor:
    r"""Convolve a tensor with a 2d kernel.

    The function applies a given kernel to a tensor. The kernel is applied
    independently at each depth channel of the tensor. Before applying the
    kernel, the function applies padding according to the specified mode so
    that the output remains in the same shape.

    Args:
        input: the input tensor with shape of
          :math:`(B, C, H, W)`.
        kernel: the kernel to be convolved with the input
          tensor. The kernel shape must be :math:`(1, kH, kW)` or :math:`(B, kH, kW)`.
        border_type: the padding mode to be applied before convolving.
          The expected modes are: ``'constant'``, ``'reflect'``,
          ``'replicate'`` or ``'circular'``.
        normalized: If True, kernel will be L1 normalized.
        padding: This defines the type of padding.
          2 modes available ``'same'`` or ``'valid'``.

    Return:
        paddle.Tensor: the convolved tensor of same size and numbers of channels
        as the input with shape :math:`(B, C, H, W)`.

    Example:
        >>> input = paddle.tensor([[[
        ...    [0., 0., 0., 0., 0.],
        ...    [0., 0., 0., 0., 0.],
        ...    [0., 0., 5., 0., 0.],
        ...    [0., 0., 0., 0., 0.],
        ...    [0., 0., 0., 0., 0.],]]])
        >>> kernel = paddle.ones(1, 3, 3)
        >>> filter2d(input, kernel, padding='same')
        tensor([[[[0., 0., 0., 0., 0.],
                  [0., 5., 5., 5., 0.],
                  [0., 5., 5., 5., 0.],
                  [0., 5., 5., 5., 0.],
                  [0., 0., 0., 0., 0.]]]])
    """
    if not isinstance(input, paddle.Tensor):
        raise TypeError(f"Input input is not paddle.Tensor. Got {type(input)}")

    if not isinstance(kernel, paddle.Tensor):
        raise TypeError(f"Input kernel is not paddle.Tensor. Got {type(kernel)}")

    if not isinstance(border_type, str):
        raise TypeError(f"Input border_type is not string. Got {type(border_type)}")

    if border_type not in ['constant', 'reflect', 'replicate', 'circular']:
        raise ValueError(f"Invalid border type, we expect 'constant', \
        'reflect', 'replicate', 'circular'. Got:{border_type}")

    if not isinstance(padding, str):
        raise TypeError(f"Input padding is not string. Got {type(padding)}")

    if padding not in ['valid', 'same']:
        raise ValueError(f"Invalid padding mode, we expect 'valid' or 'same'. Got: {padding}")

    if not len(paddle.shape(input)) == 4:
        raise ValueError(f"Invalid input shape, we expect BxCxHxW. Got: {input.shape}")

    if (not len(kernel.shape) == 3) and not ((kernel.shape[0] == 0) or (kernel.shape[0] == input.shape[0])):
        raise ValueError(f"Invalid kernel shape, we expect 1xHxW or BxHxW. Got: {kernel.shape}")

    # prepare kernel   
    b, c, h, w = paddle.shape(input)
    tmp_kernel: paddle.Tensor = paddle.unsqueeze(kernel, axis=1) 

    if normalized:
        tmp_kernel = normalize_kernel2d(tmp_kernel)

    tmp_kernel =  paddle.expand(tmp_kernel, shape=[-1, c, -1, -1])  

    height, width = paddle.shape(tmp_kernel)[-2:]

      # pad the input tensor
    if padding == 'same':
        padding_shape: List[int] = _compute_padding([height, width])
        input = F.pad(input, pad=padding_shape, mode=border_type)

    # kernel and input tensor reshape to align element-wise or batch-wise params
    tmp_kernel = paddle.reshape(tmp_kernel,[-1, 1, height, width])
    #input = input.view(-1, tmp_kernel.size(0), input.size(-2), input.size(-1))

    input =  paddle.reshape(input ,[-1, tmp_kernel.shape[0], input.shape[-2], input.shape[-1]])
    # convolve the tensor with the kernel.
    output = F.conv2d(input, weight=tmp_kernel, padding=0, stride=1, groups=tmp_kernel.shape[0])

    if padding == 'same':
        out = paddle.reshape(output,[b, c, h, w]) #out = output.view(b, c, h, w)
    else:
        out = paddle.reshape(output,[b, c, h - height + 1, w - width + 1]) #out = output.view(b, c, h - height + 1, w - width + 1)

    return out


def filter2d_separable(input: paddle.Tensor,
                       kernel_x: paddle.Tensor,
                       kernel_y: paddle.Tensor,
                       border_type: str = 'reflect',
                       normalized: bool = False,
                       padding: str = 'same') -> paddle.Tensor:
    r"""Convolve a tensor with two 1d kernels, in x and y directions.

    The function applies a given kernel to a tensor. The kernel is applied
    independently at each depth channel of the tensor. Before applying the
    kernel, the function applies padding according to the specified mode so
    that the output remains in the same shape.

    Args:
        input: the input tensor with shape of
          :math:`(B, C, H, W)`.
        kernel_x: the kernel to be convolved with the input
          tensor. The kernel shape must be :math:`(1, kW)` or :math:`(B, kW)`.
        kernel_y: the kernel to be convolved with the input
          tensor. The kernel shape must be :math:`(1, kH)` or :math:`(B, kH)`.
        border_type: the padding mode to be applied before convolving.
          The expected modes are: ``'constant'``, ``'reflect'``,
          ``'replicate'`` or ``'circular'``.
        normalized: If True, kernel will be L1 normalized.
        padding: This defines the type of padding.
          2 modes available ``'same'`` or ``'valid'``.

    Return:
        paddle.Tensor: the convolved tensor of same size and numbers of channels
        as the input with shape :math:`(B, C, H, W)`.

    Example:
        >>> input = paddle.tensor([[[
        ...    [0., 0., 0., 0., 0.],
        ...    [0., 0., 0., 0., 0.],
        ...    [0., 0., 5., 0., 0.],
        ...    [0., 0., 0., 0., 0.],
        ...    [0., 0., 0., 0., 0.],]]])
        >>> kernel = paddle.ones(1, 3)

        >>> filter2d_separable(input, kernel, kernel, padding='same')
        tensor([[[[0., 0., 0., 0., 0.],
                  [0., 5., 5., 5., 0.],
                  [0., 5., 5., 5., 0.],
                  [0., 5., 5., 5., 0.],
                  [0., 0., 0., 0., 0.]]]])
    """
    # paddle.unsqueeze(kernel_x, axis=0)
    out_x = filter2d(input, paddle.unsqueeze(kernel_x, axis=0), border_type, normalized, padding)
    out = filter2d(out_x,  paddle.unsqueeze(kernel_y, axis=-1), border_type, normalized, padding)
    return out

def gaussian_blur2d(input: paddle.Tensor,
                    kernel_size: Tuple[int, int],
                    sigma: Tuple[float, float],
                    border_type: str = 'reflect',
                    separable: bool = True) -> paddle.Tensor:
    r"""Create an operator that blurs a tensor using a Gaussian filter.

    .. image:: _static/img/gaussian_blur2d.png

    The operator smooths the given tensor with a gaussian kernel by convolving
    it to each channel. It supports batched operation.

    Arguments:
        input: the input tensor with shape :math:`(B,C,H,W)`.
        kernel_size: the size of the kernel.
        sigma: the standard deviation of the kernel.
        border_type: the padding mode to be applied before convolving.
          The expected modes are: ``'constant'``, ``'reflect'``,
          ``'replicate'`` or ``'circular'``. Default: ``'reflect'``.
        separable: run as composition of two 1d-convolutions.

    Returns:
        the blurred tensor with shape :math:`(B, C, H, W)`.

    .. note::
       See a working example `here <https://kornia-tutorials.readthedocs.io/en/latest/
       gaussian_blur.html>`__.

    Examples:
        >>> input = torch.rand(2, 4, 5, 5)
        >>> output = gaussian_blur2d(input, (3, 3), (1.5, 1.5))
        >>> output.shape
        torch.Size([2, 4, 5, 5])
    """
    if separable:
        kernel_x: paddle.Tensor = get_gaussian_kernel1d(kernel_size[1], sigma[1])
        kernel_y: paddle.Tensor = get_gaussian_kernel1d(kernel_size[0], sigma[0])
        out = filter2d_separable(input, kernel_x[None], kernel_y[None], border_type)
    else:
        kernel: paddle.Tensor = get_gaussian_kernel2d(kernel_size, sigma)
        out = filter2d(input, kernel[None], border_type)
    return out


if __name__ == "__main__":

    result1 = get_gaussian_kernel1d(5, 1.5)
    print("\n get_gaussian_kernel1d: \n tensor([0.1201, 0.2339, 0.2921, 0.2339, 0.1201]) \n ",result1)

    result2 = get_gaussian_kernel2d((3, 3), (1.5, 1.5))
    print("\n get_gaussian_kernel2d : \n tensor([[0.0947, 0.1183, 0.0947],[0.1183, 0.1478, 0.1183],[0.0947, 0.1183, 0.0947]])  \n",result2)
    input = paddle.to_tensor([[[  
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 5., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],]]])
    kernel =paddle.ones(shape=[1, 3, 3])
    result3 = filter2d(input, kernel, padding='same')
    print("\nfilter2d\n", result3)


    input = paddle.to_tensor([[[
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 5., 0., 0.],
         [0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0.],]]])
    kernel = paddle.ones(shape=[1, 3])
    result4 = filter2d_separable(input, kernel, kernel, padding='same')
    print("\nfilter2d_separable\n", result4)

    input = paddle.rand(shape=[2, 4, 5, 5])
    output = gaussian_blur2d(input, (3, 3), (1.5, 1.5))
    print("\ngaussian_blur2d\n", output)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值