Пример #1
0
 def test_value(self, device, dtype):
     input = torch.tensor([[0.5, 1., 0.3], [0.7, 0.3, 0.8], [0.4, 0.9, 0.2]],
                          device=device, dtype=dtype)[None, None, :, :]
     kernel = torch.tensor([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]], device=device, dtype=dtype)
     expected = torch.tensor([[0.7, 1., 0.8], [0.7, 0.7, 0.8], [0.7, 0.9, 0.8]],
                             device=device, dtype=dtype)[None, None, :, :]
     assert_allclose(close(input, kernel), expected)
Пример #2
0
    def test_exception(self, device, dtype):
        input = torch.ones(1, 1, 3, 4, device=device, dtype=dtype)
        kernel = torch.ones(3, 3, device=device, dtype=dtype)

        with pytest.raises(TypeError):
            assert close([0.], kernel)

        with pytest.raises(TypeError):
            assert close(input, [0.])

        with pytest.raises(ValueError):
            test = torch.ones(2, 3, 4, device=device, dtype=dtype)
            assert close(test, kernel)

        with pytest.raises(ValueError):
            test = torch.ones(2, 3, 4, device=device, dtype=dtype)
            assert close(input, test)
Пример #3
0
    def test_exception(self, dev_str, dtype_str, call):
        input_ = ivy.ones((1, 1, 3, 4), dev_str=dev_str, dtype_str=dtype_str)
        kernel = ivy.ones((3, 3), dev_str=dev_str, dtype_str=dtype_str)

        with pytest.raises(ValueError):
            test = ivy.ones((2, 3, 4), dev_str=dev_str, dtype_str=dtype_str)
            assert close(test, kernel)

        with pytest.raises(ValueError):
            test = ivy.ones((2, 3, 4), dev_str=dev_str, dtype_str=dtype_str)
            assert close(input_, test)

        if call is not helpers.torch_call:
            return

        with pytest.raises(TypeError):
            assert close([0.], kernel)

        with pytest.raises(TypeError):
            assert close(input_, [0.])
Пример #4
0
def black_hat(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:

    r"""

        Returns the black hat tranformation of an image,
        (that means, closed_image - image) applying the same kernel in each channel.

        The kernel must have 2 dimensions, each one defined by an odd number.

        See :class:`~kornia.morphology.close` for details.

        Args

           tensor (torch.Tensor): Image with shape :math:`(B, C, H, W)`.
           kernel (torch.Tensor): Structuring element with shape :math:`(H, W)`.

        Returns:

           torch.Tensor: Top hat transformated image with shape :math:`(B, C, H, W)`.

        Example:

            >>> tensor = torch.rand(1, 3, 5, 5)
            >>> kernel = torch.ones(3, 3)
            >>> black_hat_img = black_hat(tensor, kernel)

        """

    if not isinstance(tensor, torch.Tensor):
        raise TypeError("Input type is not a torch.Tensor. Got {}".format(
            type(tensor)))

    if len(tensor.shape) != 4:
        raise ValueError("Input size must have 4 dimensions. Got {}".format(
            tensor.dim()))

    if not isinstance(kernel, torch.Tensor):
        raise TypeError("Kernel type is not a torch.Tensor. Got {}".format(
            type(kernel)))

    if len(kernel.shape) != 2:
        raise ValueError("Kernel size must have 2 dimensions. Got {}".format(
            kernel.dim()))

    return close(tensor, kernel) - tensor
Пример #5
0
def black_hat(tensor: ivy.Array, kernel: ivy.Array) -> ivy.Array:
    r"""Returns the black hat tranformation of an image.

    That means, (closed_image - image) applying the same kernel in each channel.
    The kernel must have 2 dimensions, each one defined by an odd number.

    See :class:`~kornia.morphology.close` for details.

    Args:
       tensor (ivy.Array): Image with shape :math:`(B, C, H, W)`.
       kernel (ivy.Array): Structuring element with shape :math:`(H, W)`.

    Returns:
       ivy.Array: Top hat transformated image with shape :math:`(B, C, H, W)`.

    Example:
        >>> tensor = ivy.random_uniform(shape=(1, 3, 5, 5))
        >>> kernel = ivy.ones((3, 3))
        >>> black_hat_img = black_hat(tensor, kernel)
    """

    if ivy.backend == 'torch' and not isinstance(tensor, ivy.Array):
        raise TypeError("Input type is not a ivy.Array. Got {}".format(
            type(tensor)))

    if len(tensor.shape) != 4:
        raise ValueError("Input size must have 4 dimensions. Got {}".format(
            ivy.get_num_dims(tensor)))

    if ivy.backend == 'torch' and not isinstance(kernel, ivy.Array):
        raise TypeError("Kernel type is not a ivy.Array. Got {}".format(
            type(kernel)))

    if len(kernel.shape) != 2:
        raise ValueError("Kernel size must have 2 dimensions. Got {}".format(
            ivy.get_num_dims(kernel)))

    return close(tensor, kernel) - tensor
Пример #6
0
 def test_cardinality(self, device, dtype, shape, kernel):
     img = torch.ones(shape, device=device, dtype=dtype)
     krnl = torch.ones(kernel, device=device, dtype=dtype)
     assert close(img, krnl).shape == shape
Пример #7
0
 def test_cardinality(self, dev_str, dtype_str, call, shape, kernel):
     img = ivy.ones(shape, dtype_str=dtype_str, dev_str=dev_str)
     krnl = ivy.ones(kernel, dtype_str=dtype_str, dev_str=dev_str)
     assert close(img, krnl).shape == shape