Пример #1
0
def open(tensor: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:
    r"""Returns the opened image, (that means, dilation after an erosion) applying the same kernel in each channel.

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

    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: Dilated image with shape :math:`(B, C, H, W)`.

    Example:
        >>> tensor = torch.rand(1, 3, 5, 5)
        >>> kernel = torch.ones(3, 3)
        >>> opened_img = open(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 dilation(erosion(tensor, kernel), kernel)
Пример #2
0
def open(tensor: ivy.Array, kernel: ivy.Array) -> ivy.Array:
    r"""Returns the opened image, (that means, erosion after a dilation) applying the same kernel in each channel.

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

    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: Dilated image with shape :math:`(B, C, H, W)`.

    Example:
        >>> tensor = ivy.random_uniform(shape=(1, 3, 5, 5))
        >>> kernel = ivy.ones((3, 3))
        >>> opened_img = open(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 dilation(erosion(tensor, kernel), kernel)
Пример #3
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 erosion([0.], kernel)

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

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

        with pytest.raises(ValueError):
            test = torch.ones(2, 3, 4, device=device, dtype=dtype)
            assert erosion(input, test)
Пример #4
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 erosion(test, kernel)

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

        if call is not helpers.torch_call:
            return

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

        with pytest.raises(TypeError):
            assert erosion(input_, [0.])
Пример #5
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.5, 0.3, 0.3], [0.3, 0.3, 0.2], [0.4, 0.2, 0.2]],
         device=device,
         dtype=dtype)[None, None, :, :]
     assert_allclose(erosion(input, kernel), expected)
Пример #6
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 erosion(img, krnl).shape == shape
Пример #7
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 erosion(img, krnl).shape == shape