Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

flip module

dvd4203536b22f90f
diff --git a/docs/source/geometry.transform.rst b/docs/source/geometry.transform.rst
index 6037e0a48..d990f0f4a 100644
--- a/docs/source/geometry.transform.rst
+++ b/docs/source/geometry.transform.rst
@@ -22,6 +22,7 @@ The functions in this section perform various geometrical transformations of 2D
.. autofunction:: translate
.. autofunction:: scale
.. autofunction:: shear
+.. autofunction:: flip
.. autoclass:: Rotate
.. autoclass:: Translate
@@ -30,3 +31,5 @@ The functions in this section perform various geometrical transformations of 2D
.. autoclass:: PyrDown
.. autoclass:: PyrUp
.. autoclass:: ScalePyramid
+.. autoclass:: Flip
+
diff --git a/kornia/geometry/transform/__init__.py b/kornia/geometry/transform/__init__.py
index 56fc76096..61b3e8b85 100644
--- a/kornia/geometry/transform/__init__.py
+++ b/kornia/geometry/transform/__init__.py
@@ -2,3 +2,4 @@ from kornia.geometry.transform.crop import *
from kornia.geometry.transform.imgwarp import *
from kornia.geometry.transform.pyramid import *
from kornia.geometry.transform.affwarp import *
+from kornia.geometry.transform.flips import *
diff --git a/kornia/geometry/transform/flips.py b/kornia/geometry/transform/flips.py
new file mode 100644
index 000000000..109b60295
--- /dev/null
+++ b/kornia/geometry/transform/flips.py
@@ -0,0 +1,61 @@
+import torch
+import 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
+
+ Args:
+ input (torch.Tensor): input tensor
+ dims (list or tuple): axis to perform the flip on
+
+ Examples:
+ >>> input = torch.tensor([[[
+ [0., 0., 0.],
+ [0., 0., 0.],
+ [0., 1., 1.]]]])
+ >>> vflip = kornia.flip(input, -2)
+ 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, dims: Union[Tuple[int, ...], List[int], int]) -> None:
+
+ super(Flip, self).__init__()
+
+ self.dims = dims
+
+ def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore
+ return flip(input, self.dims)
+
+ def __repr__(self):
+ repr = "(axis={0})".format(self.dims)
+ return self.__class__.__name__ + repr
+
+
+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
+
+ Args:
+ input (torch.Tensor): input tensor
+ dims (list or tuple): axis to perform the flip on
+
+ return torch.flip(input, dims])
+ """
+
+ # Allow ints
+ if isinstance(dims, int):
+ dims = [dims]
+
+ return torch.flip(input, dims)
+
+
+# def rot180(input: torch.Tensor) -> torch.Tensor:
+# return torch.flip(input, [-2, -1])
diff --git a/test/geometry/transform/test_flip.py b/test/geometry/transform/test_flip.py
new file mode 100644
index 000000000..be8abf19c
--- /dev/null
+++ b/test/geometry/transform/test_flip.py
@@ -0,0 +1,105 @@
+import kornia
+import torch
+import pytest
+
+import kornia.testing as utils # test utils
+from torch.testing import assert_allclose
+from torch.autograd import gradcheck
+from typing import Union, Tuple, List
+
+
+class TestFlip:
+ def smoke_test(self):
+ f = kornia.Flip(dims=[-2, -1])
+ repr = "Flip(dims=[-2, -1])"
+ assert str(f) == repr
+
+ def test_flip(self):
+
+ f = kornia.Flip(-1)
+ input = torch.tensor([[0., 0., 0.],
+ [0., 0., 0.],
+ [0., 1., 1.]]) # 3 x 3
+
+ expected = torch.tensor([[0., 0., 0.],
+ [0., 0., 0.],
+ [1., 1., 0.]]) # 3 x 3
+
+ assert (f(input) == expected).all()
+
+ def test_batch_flip(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.Flip(-1)
+ expected = torch.tensor([[[0., 0., 0.],
+ [0., 0., 0.],
+ [1., 1., 0.]]]) # 3 x 3
+
+ expected = expected.repeat(2, 1, 1) # 2 x 3 x 3
+
+ assert (f(input) == expected).all()
+
+ def test_list_flip(self):
+
+ 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.]]])
+
+ f = kornia.Flip([-2, -1])
+
+ 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, dims=Union[Tuple[int, ...], List[int], int]) -> torch.Tensor:
+
+ return kornia.flip(data, dims)
+
+ input = torch.tensor([[0., 0., 0.],
+ [0., 0., 0.],
+ [0., 1., 1.]]) # 3 x 3
+
+ expected = torch.tensor([[[0., 0., 0.],
+ [0., 0., 0.],
+ [1., 1., 0.]]])
+
+ # Build jit trace
+ op_trace = torch.jit.trace(op_script, (input, -1))
+ dims = [-2, -1]
+
+ # Create new inputs
+ input = torch.tensor([[1., 1., 0.],
+ [0., 0., 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.],
+ [0., 1., 1.]]]) # 3 x 3
+
+ expected = expected.repeat(2, 1, 1)
+
+ actual = op_trace(input, dims)
+
+ 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.Flip([-2, -1]), (input,), raise_exception=True)