Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

add relevant test

kshitij1234531728e958180
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index dd519db3f..b3d191652 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -7,7 +7,7 @@ from torch.autograd import gradcheck
import kornia
import kornia.testing as utils # test utils
-from kornia.augmentation import RandomHorizontalFlip, ColorJitter, RandomRectangleErasing
+from kornia.augmentation import RandomHorizontalFlip, RandomVerticalFlip, ColorJitter, RandomRectangleErasing
from kornia.augmentation.random_erasing import get_random_rectangles_params, erase_rectangles
@@ -145,6 +145,140 @@ class TestRandomHorizontalFlip:
assert gradcheck(RandomHorizontalFlip(p=1.), (input, ), raise_exception=True)
+class TestRandomVerticalFlip:
+
+ def smoke_test(self):
+ f = RandomVerticalFlip(0.5)
+ repr = "RandomVerticalFlip(p=0.5, return_transform=False)"
+ assert str(f) == repr
+
+ def test_random_vflip(self):
+
+ f = RandomVerticalFlip(p=1.0, return_transform=True)
+ f1 = RandomVerticalFlip(p=0., return_transform=True)
+ f2 = RandomVerticalFlip(p=1.)
+ f3 = RandomVerticalFlip(p=0.)
+
+ input = torch.tensor([[0., 0., 0.],
+ [0., 0., 0.],
+ [0., 1., 1.]]) # 3 x 3
+
+ expected = torch.tensor([[0., 1., 1.],
+ [0., 0., 0.],
+ [0., 0., 0.]]) # 3 x 3
+
+ expected_transform = torch.tensor([[1., 0., 0.],
+ [0., -1., 3.],
+ [0., 0., 1.]]) # 3 x 3
+
+ identity = torch.tensor([[1., 0., 0.],
+ [0., 1., 0.],
+ [0., 0., 1.]]) # 3 x 3
+
+ assert (f(input)[0] == expected).all()
+ assert (f(input)[1] == expected_transform).all()
+ assert (f1(input)[0] == input).all()
+ assert (f1(input)[1] == identity).all()
+ assert (f2(input) == expected).all()
+ assert (f3(input) == input).all()
+
+ def test_batch_random_vflip(self):
+
+ f = RandomVerticalFlip(p=1.0, return_transform=True)
+ f1 = RandomVerticalFlip(p=0.0, return_transform=True)
+
+ input = torch.tensor([[[[0., 0., 0.],
+ [0., 0., 0.],
+ [0., 1., 1.]]]]) # 1 x 1 x 3 x 3
+
+ expected = torch.tensor([[[[0., 1., 1.],
+ [0., 0., 0.],
+ [0., 0., 0.]]]]) # 1 x 1 x 3 x 3
+
+ expected_transform = torch.tensor([[[1., 0., 0.],
+ [0., -1., 3.],
+ [0., 0., 1.]]]) # 1 x 3 x 3
+
+ identity = torch.tensor([[[1., 0., 0.],
+ [0., 1., 0.],
+ [0., 0., 1.]]]) # 1 x 3 x 3
+
+ input = input.repeat(5, 3, 1, 1) # 5 x 3 x 3 x 3
+ expected = expected.repeat(5, 3, 1, 1) # 5 x 3 x 3 x 3
+ expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 3 x 3
+ identity = identity.repeat(5, 1, 1) # 5 x 3 x 3
+
+ assert (f(input)[0] == expected).all()
+ assert (f(input)[1] == expected_transform).all()
+ assert (f1(input)[0] == input).all()
+ assert (f1(input)[1] == identity).all()
+
+ def test_sequential(self):
+
+ f = nn.Sequential(
+ RandomVerticalFlip(1.0, return_transform=True),
+ RandomVerticalFlip(1.0, return_transform=True),
+ )
+ f1 = nn.Sequential(
+ RandomVerticalFlip(1.0, return_transform=True),
+ RandomVerticalFlip(1.0),
+ )
+
+ input = torch.tensor([[[[0., 0., 0.],
+ [0., 0., 0.],
+ [0., 1., 1.]]]]) # 1 x 1 x 3 x 3
+
+ expected_transform = torch.tensor([[[1., 0., 0.],
+ [0., -1., 3.],
+ [0., 0., 1.]]]) # 1 x 3 x 3
+
+ expected_transform_1 = expected_transform @ expected_transform
+
+ assert(f(input)[0] == input).all()
+ assert(f(input)[1] == expected_transform_1).all()
+ assert(f1(input)[0] == input).all()
+ assert(f1(input)[1] == expected_transform).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.random_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.],
+ [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.],
+ [5., 5., 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.rand((3, 3)).double() # 3 x 3
+
+ input = utils.tensor_to_gradcheck_var(input) # to var
+
+ assert gradcheck(RandomVerticalFlip(p=1.), (input, ), raise_exception=True)
+
+
class TestColorJitter:
def smoke_test(self):