π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
vflip, hflip and rot180 functionality
dvd42f8002b288893
diff --git a/docs/source/geometry.transform.rst b/docs/source/geometry.transform.rstindex d990f0f4a..0025cbfbb 100644--- a/docs/source/geometry.transform.rst+++ b/docs/source/geometry.transform.rst@@ -22,7 +22,9 @@ The functions in this section perform various geometrical transformations of 2D.. autofunction:: translate.. autofunction:: scale.. autofunction:: shear-.. autofunction:: flip+.. autofunction:: hflip+.. autofunction:: vflip+.. autofunction:: rot180.. autoclass:: Rotate.. autoclass:: Translate@@ -31,5 +33,7 @@ The functions in this section perform various geometrical transformations of 2D.. autoclass:: PyrDown.. autoclass:: PyrUp.. autoclass:: ScalePyramid-.. autoclass:: Flip+.. autoclass:: Hflip+.. autoclass:: Vflip+.. autoclass:: Rot180diff --git a/kornia/geometry/transform/flips.py b/kornia/geometry/transform/flips.pyindex 109b60295..7ee7eae3a 100644--- a/kornia/geometry/transform/flips.py+++ b/kornia/geometry/transform/flips.py@@ -1,14 +1,16 @@import torchimport torch.nn as nn-from typing import Union, Tuple, List-class Flip(nn.Module):- r"""Reverse the order of a tensor along a given axis+class Vflip(nn.Module):+ r"""Vertically flip a tensor image or a batch of tensor images. Input must+ be a tensor of shape (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.Args:input (torch.Tensor): input tensor- dims (list or tuple): axis to perform the flip on++ Returns:+ torch.Tensor: The vertically flipped image tensorExamples:>>> input = torch.tensor([[[@@ -19,43 +21,123 @@ class Flip(nn.Module):tensor([[[0, 1, 1],[0, 0, 0],[0, 0, 0]]])- >>> rot180 = kornia.flip(input, [-2, -1])- tensor([[[1, 1, 0],- [0, 0, 0],- [0, 0, 0]]])+ """++ def __init__(self) -> None:++ super(Vflip, self).__init__()++ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore+ return vflip(input)++ def __repr__(self):+ return self.__class__.__name__+++class Hflip(nn.Module):+ r"""Horizontally flip a tensor image or a batch of tensor images. Input must+ be a tensor of shape (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.+ Args:+ input (torch.Tensor): input tensor++ Returns:+ torch.Tensor: The horizontally flipped image tensor++ Examples:+ >>> input = torch.tensor([[[+ [0., 0., 0.],+ [0., 0., 0.],+ [0., 1., 1.]]]])+ >>> hflip = kornia.flip(input, -1)+ tensor([[[0, 0, 0],+ [0, 0, 0],+ [1, 1, 0]]])"""- def __init__(self, dims: Union[Tuple[int, ...], List[int], int]) -> None:+ def __init__(self) -> None:++ super(Hflip, self).__init__()++ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore+ return hflip(input)++ def __repr__(self):+ return self.__class__.__name__+++class Rot180(nn.Module):+ r"""Rotate a tensor image or a batch of tensor images+ 180 degrees. Input must be a tensor of shape (C, H, W)+ or a batch of tensors :math:`(*, C, H, W)`.++ Args:+ input (torch.Tensor): input tensor- super(Flip, self).__init__()+ Examples:+ >>> input = torch.tensor([[[+ [0., 0., 0.],+ [0., 0., 0.],+ [0., 1., 1.]]]])+ >>> rot180 = kornia.rot180(input)+ tensor([[[1, 1, 0],+ [0, 0, 0],+ [0, 0, 0]]])+ """- self.dims = dims+ def __init__(self) -> None:++ super(Rot180, self).__init__()def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore- return flip(input, self.dims)+ return rot180(input)def __repr__(self):- repr = "(axis={0})".format(self.dims)- return self.__class__.__name__ + repr+ return self.__class__.__name__+++def rot180(input: torch.Tensor) -> torch.Tensor:++ r"""Rotate a tensor image or a batch of tensor images+ 180 degrees. Input must be a tensor of shape (C, H, W)+ or a batch of tensors :math:`(*, C, H, W)`.++ Args:+ input (torch.Tensor): input tensor++ Returns:+ torch.Tensor: The rotated image tensor++ """++ return torch.flip(input, [-2, -1])-def flip(input: torch.Tensor, dims: Union[Tuple[int, ...], List[int], int]) -> torch.Tensor:- r"""Reverse the order of a tensor along a given axis+def hflip(input: torch.Tensor) -> torch.Tensor:+ r"""Horizontally flip a tensor image or a batch of tensor images. Input must+ be a tensor of shape (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.Args:input (torch.Tensor): input tensor- dims (list or tuple): axis to perform the flip on- return torch.flip(input, dims])+ Returns:+ torch.Tensor: The horizontally flipped image tensor+"""- # Allow ints- if isinstance(dims, int):- dims = [dims]+ return torch.flip(input, [-1])- return torch.flip(input, dims)+def vflip(input: torch.Tensor) -> torch.Tensor:+ r"""Vertically flip a tensor image or a batch of tensor images. Input must+ be a tensor of shape (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.++ Args:+ input (torch.Tensor): input tensor++ Returns:+ torch.Tensor: The vertically flipped image tensor++ """-# def rot180(input: torch.Tensor) -> torch.Tensor:-# return torch.flip(input, [-2, -1])+ return torch.flip(input, [-2])diff --git a/test/geometry/transform/test_flip.py b/test/geometry/transform/test_flip.pyindex be8abf19c..da6b43941 100644--- a/test/geometry/transform/test_flip.py+++ b/test/geometry/transform/test_flip.py@@ -5,29 +5,28 @@ import pytestimport kornia.testing as utils # test utilsfrom torch.testing import assert_allclosefrom torch.autograd import gradcheck-from typing import Union, Tuple, List-class TestFlip:+class TestVflip:def smoke_test(self):- f = kornia.Flip(dims=[-2, -1])- repr = "Flip(dims=[-2, -1])"+ f = kornia.Vflip()+ repr = "Vflip()"assert str(f) == repr- def test_flip(self):+ def test_vflip(self):- f = kornia.Flip(-1)+ f = kornia.Vflip()input = torch.tensor([[0., 0., 0.],[0., 0., 0.],[0., 1., 1.]]) # 3 x 3- expected = torch.tensor([[0., 0., 0.],+ expected = torch.tensor([[0., 1., 1.],[0., 0., 0.],- [1., 1., 0.]]) # 3 x 3+ [0., 0., 0.]]) # 3 x 3assert (f(input) == expected).all()- def test_batch_flip(self):+ def test_batch_vflip(self):input = torch.tensor([[0., 0., 0.],[0., 0., 0.],@@ -35,62 +34,201 @@ class TestFlip:input = input.repeat(2, 1, 1) # 2 x 3 x 3- f = kornia.Flip(-1)- expected = torch.tensor([[[0., 0., 0.],+ f = kornia.Vflip()+ expected = torch.tensor([[[0., 1., 1.],[0., 0., 0.],- [1., 1., 0.]]]) # 3 x 3+ [0., 0., 0.]]]) # 1 x 3 x 3expected = expected.repeat(2, 1, 1) # 2 x 3 x 3assert (f(input) == expected).all()- def test_list_flip(self):+ @pytest.mark.skip(reason="turn off all jit for a while")+ def test_jit(self):+ @torch.jit.script+ def op_script(data: torch.Tensor) -> torch.Tensor:++ return kornia.vflip(data)++ input = torch.tensor([[0., 0., 0.],+ [0., 0., 0.],+ [0., 1., 1.]]) # 3 x 3++ # Build jit trace+ op_trace = torch.jit.trace(op_script, (input, ))++ # Create new inputs+ input = torch.tensor([[0., 0., 0.],+ [0., 0., 0.],+ [5., 5., 0.]]) # 3 x 3++ input = input.repeat(2, 1, 1) # 2 x 3 x 3++ expected = torch.tensor([[[5., 5., 0.],+ [0., 0., 0.],+ [0., 0., 0.]]]) # 3 x 3++ expected = expected.repeat(2, 1, 1)++ actual = op_trace(input)++ assert_allclose(actual, expected)++ def test_gradcheck(self):++ input = torch.tensor([[0., 0., 0.],+ [0., 0., 0.],+ [0., 1., 1.]]).double() # 3 x 3++ input = utils.tensor_to_gradcheck_var(input) # to var++ assert gradcheck(kornia.Vflip(), (input,), raise_exception=True)+++class TestHflip:+ def smoke_test(self):+ f = kornia.Hflip()+ repr = "Hflip()"+ assert str(f) == repr++ def test_hflip(self):++ f = kornia.Hflip()input = torch.tensor([[0., 0., 0.],[0., 0., 0.],[0., 1., 1.]]) # 3 x 3- expected = torch.tensor([[[1., 1., 0.],+ expected = torch.tensor([[0., 0., 0.],+ [0., 0., 0.],+ [1., 1., 0.]]) # 3 x 3++ assert (f(input) == expected).all()++ def test_batch_hflip(self):++ input = torch.tensor([[0., 0., 0.],+ [0., 0., 0.],+ [0., 1., 1.]]) # 1 x 3 x 3++ input = input.repeat(2, 1, 1) # 2 x 3 x 3++ f = kornia.Hflip()+ expected = torch.tensor([[[0., 0., 0.],[0., 0., 0.],- [0., 0., 0.]]])+ [1., 1., 0.]]]) # 3 x 3- f = kornia.Flip([-2, -1])+ expected = expected.repeat(2, 1, 1) # 2 x 3 x 3assert (f(input) == expected).all()@pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self):@torch.jit.script- def op_script(data: torch.Tensor, dims=Union[Tuple[int, ...], List[int], int]) -> torch.Tensor:+ def op_script(data: torch.Tensor) -> torch.Tensor:- return kornia.flip(data, dims)+ return kornia.Hflip(data)input = torch.tensor([[0., 0., 0.],[0., 0., 0.],[0., 1., 1.]]) # 3 x 3+ # Build jit trace+ op_trace = torch.jit.trace(op_script, (input, ))++ # Create new inputs+ input = torch.tensor([[0., 0., 0.],+ [5., 5., 0.],+ [0., 0., 0.]]) # 3 x 3++ input = input.repeat(2, 1, 1) # 2 x 3 x 3+expected = torch.tensor([[[0., 0., 0.],- [0., 0., 0.],- [1., 1., 0.]]])+ [0., 5., 5.],+ [0., 0., 0.]]]) # 3 x 3++ expected = expected.repeat(2, 1, 1)++ actual = op_trace(input)++ assert_allclose(actual, expected)++ def test_gradcheck(self):++ input = torch.tensor([[0., 0., 0.],+ [0., 0., 0.],+ [0., 1., 1.]]).double() # 3 x 3++ input = utils.tensor_to_gradcheck_var(input) # to var++ assert gradcheck(kornia.Hflip(), (input,), raise_exception=True)+++class TestRot180:++ def smoke_test(self):+ f = kornia.Rot180()+ repr = "Rot180()"+ assert str(f) == repr++ def test_rot180(self):++ f = kornia.Rot180()+ input = torch.tensor([[0., 0., 0.],+ [0., 0., 0.],+ [0., 1., 1.]]) # 3 x 3++ expected = torch.tensor([[1., 1., 0.],+ [0., 0., 0.],+ [0., 0., 0.]]) # 3 x 3++ assert (f(input) == expected).all()++ def test_batch_rot180(self):++ input = torch.tensor([[0., 0., 0.],+ [0., 0., 0.],+ [0., 1., 1.]]) # 3 x 3++ input = input.repeat(2, 1, 1) # 2 x 3 x 3++ f = kornia.Rot180()+ expected = torch.tensor([[1., 1., 0.],+ [0., 0., 0.],+ [0., 0., 0.]]) # 1 x 3 x 3++ expected = expected.repeat(2, 1, 1) # 2 x 3 x 3++ assert (f(input) == expected).all()++ @pytest.mark.skip(reason="turn off all jit for a while")+ def test_jit(self):+ @torch.jit.script+ def op_script(data: torch.Tensor) -> torch.Tensor:++ return kornia.Rot180(data)++ input = torch.tensor([[0., 0., 0.],+ [0., 0., 0.],+ [0., 1., 1.]]) # 3 x 3# Build jit trace- op_trace = torch.jit.trace(op_script, (input, -1))- dims = [-2, -1]+ op_trace = torch.jit.trace(op_script, (input, ))# Create new inputs- input = torch.tensor([[1., 1., 0.],+ input = torch.tensor([[0., 0., 0.],[0., 0., 0.],- [0., 0., 0.]]) # 3 x 3+ [5., 5., 0.]]) # 3 x 3input = input.repeat(2, 1, 1) # 2 x 3 x 3- expected = torch.tensor([[[0., 0., 0.],+ expected = torch.tensor([[[0., 5., 5.],[0., 0., 0.],- [0., 1., 1.]]]) # 3 x 3+ [0., 0., 0.]]]) # 3 x 3expected = expected.repeat(2, 1, 1)- actual = op_trace(input, dims)+ actual = op_trace(input)assert_allclose(actual, expected)@@ -102,4 +240,4 @@ class TestFlip:input = utils.tensor_to_gradcheck_var(input) # to var- assert gradcheck(kornia.Flip([-2, -1]), (input,), raise_exception=True)+ assert gradcheck(kornia.Rot180(), (input,), raise_exception=True)