π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix augmentation gpu tests
edgarribad5a551f9447c
diff --git a/kornia/augmentation/functional.py b/kornia/augmentation/functional.pyindex bf1ed045b..d4bbb5db6 100644--- a/kornia/augmentation/functional.py+++ b/kornia/augmentation/functional.py@@ -426,9 +426,12 @@ def apply_perspective(input: torch.Tensor,# process valid samplesmask = params['batch_prob'].to(device)- # apply the computed transform- height, width = x_data.shape[-2:]- out_data[mask] = warp_perspective(x_data[mask], transform[mask], (height, width))+ # TODO: look for a workaround for this hack. In CUDA it fails when no elements found.++ if bool(mask.sum() > 0):+ # apply the computed transform+ height, width = x_data.shape[-2:]+ out_data[mask] = warp_perspective(x_data[mask], transform[mask], (height, width))if return_transform:return out_data.view_as(input), transformdiff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.pyindex 25febe537..802de454d 100644--- a/test/augmentation/test_augmentation.py+++ b/test/augmentation/test_augmentation.py@@ -31,18 +31,22 @@ class TestRandomHorizontalFlip:input = torch.tensor([[0., 0., 0., 0.],[0., 0., 0., 0.],[0., 0., 1., 2.]]) # 3 x 4+ input = input.to(device)expected = torch.tensor([[0., 0., 0., 0.],[0., 0., 0., 0.],[2., 1., 0., 0.]]) # 3 x 4+ expected = expected.to(device)expected_transform = torch.tensor([[-1., 0., 4.],[0., 1., 0.],[0., 0., 1.]]) # 3 x 3+ expected_transform = expected_transform.to(device)identity = torch.tensor([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]]) # 3 x 3+ identity = identity.to(device)assert (f(input)[0] == expected).all()assert (f(input)[1] == expected_transform).all()@@ -59,18 +63,22 @@ class TestRandomHorizontalFlip:input = torch.tensor([[[[0., 0., 0.],[0., 0., 0.],[0., 1., 1.]]]]) # 1 x 1 x 3 x 3+ input = input.to(device)expected = torch.tensor([[[[0., 0., 0.],[0., 0., 0.],[1., 1., 0.]]]]) # 1 x 1 x 3 x 3+ expected = expected.to(device)expected_transform = torch.tensor([[[-1., 0., 3.],[0., 1., 0.],[0., 0., 1.]]]) # 1 x 3 x 3+ expected_transform = expected_transform.to(device)identity = torch.tensor([[[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]]]) # 1 x 3 x 3+ identity = identity.to(device)input = input.repeat(5, 3, 1, 1) # 5 x 3 x 3 x 3expected = expected.repeat(5, 3, 1, 1) # 5 x 3 x 3 x 3@@ -96,12 +104,15 @@ class TestRandomHorizontalFlip:input = torch.tensor([[[[0., 0., 0.],[0., 0., 0.],[0., 1., 1.]]]]) # 1 x 1 x 3 x 3+ input = input.to(device)expected_transform = torch.tensor([[[-1., 0., 3.],[0., 1., 0.],[0., 0., 1.]]]) # 1 x 3 x 3+ expected_transform = expected_transform.to(device)expected_transform_1 = expected_transform @ expected_transform+ expected_transform_1 = expected_transform_1.to(device)assert(f(input)[0] == input).all()assert(f(input)[1] == expected_transform_1).all()@@ -140,11 +151,8 @@ class TestRandomHorizontalFlip:assert_allclose(actual, expected)def test_gradcheck(self, device):-- input = torch.rand((3, 3)) # 3 x 3-+ input = torch.rand((3, 3)).to(device) # 3 x 3input = utils.tensor_to_gradcheck_var(input) # to var-assert gradcheck(RandomHorizontalFlip(p=1.), (input, ), raise_exception=True)@@ -165,19 +173,22 @@ class TestRandomVerticalFlip:input = torch.tensor([[0., 0., 0.],[0., 0., 0.],[0., 1., 1.]]) # 3 x 3- input.to(device)+ input = input.to(device)expected = torch.tensor([[0., 1., 1.],[0., 0., 0.],[0., 0., 0.]]) # 3 x 3+ expected = expected.to(device)expected_transform = torch.tensor([[1., 0., 0.],[0., -1., 3.],[0., 0., 1.]]) # 3 x 3+ expected_transform = expected_transform.to(device)identity = torch.tensor([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]]) # 3 x 3+ identity = identity.to(device)assert_allclose(f(input)[0], expected)assert_allclose(f(input)[1], expected_transform)@@ -194,19 +205,22 @@ class TestRandomVerticalFlip:input = torch.tensor([[[[0., 0., 0.],[0., 0., 0.],[0., 1., 1.]]]]) # 1 x 1 x 3 x 3- input.to(device)+ input = input.to(device)expected = torch.tensor([[[[0., 1., 1.],[0., 0., 0.],[0., 0., 0.]]]]) # 1 x 1 x 3 x 3+ expected = expected.to(device)expected_transform = torch.tensor([[[1., 0., 0.],[0., -1., 3.],[0., 0., 1.]]]) # 1 x 3 x 3+ expected_transform = expected_transform.to(device)identity = torch.tensor([[[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]]]) # 1 x 3 x 3+ identity = identity.to(device)input = input.repeat(5, 3, 1, 1) # 5 x 3 x 3 x 3expected = expected.repeat(5, 3, 1, 1) # 5 x 3 x 3 x 3@@ -232,11 +246,12 @@ class TestRandomVerticalFlip:input = torch.tensor([[[[0., 0., 0.],[0., 0., 0.],[0., 1., 1.]]]]) # 1 x 1 x 3 x 3- input.to(device)+ input = input.to(device)expected_transform = torch.tensor([[[1., 0., 0.],[0., -1., 3.],[0., 0., 1.]]]) # 1 x 3 x 3+ expected_transform = expected_transform.to(device)expected_transform_1 = expected_transform @ expected_transform@@ -277,11 +292,8 @@ class TestRandomVerticalFlip:assert_allclose(actual, expected)def test_gradcheck(self, device):-- input = torch.rand((3, 3)) # 3 x 3-+ input = torch.rand((3, 3)).to(device) # 3 x 3input = utils.tensor_to_gradcheck_var(input) # to var-assert gradcheck(RandomVerticalFlip(p=1.), (input, ), raise_exception=True)@@ -297,11 +309,10 @@ class TestColorJitter:f = ColorJitter()f1 = ColorJitter(return_transform=True)- input = torch.rand(3, 5, 5) # 3 x 5 x 5-+ input = torch.rand(3, 5, 5).to(device) # 3 x 5 x 5expected = input- expected_transform = torch.eye(3).unsqueeze(0) # 3 x 3+ expected_transform = torch.eye(3).unsqueeze(0).to(device) # 3 x 3assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)assert_allclose(f1(input)[0], expected, atol=1e-4, rtol=1e-5)@@ -311,10 +322,10 @@ class TestColorJitter:f = ColorJitter()f1 = ColorJitter(return_transform=True)- input = torch.rand(2, 3, 5, 5) # 2 x 3 x 5 x 5+ input = torch.rand(2, 3, 5, 5).to(device) # 2 x 3 x 5 x 5expected = input- expected_transform = torch.eye(3).unsqueeze(0).expand((2, 3, 3)) # 2 x 3 x 3+ expected_transform = torch.eye(3).unsqueeze(0).expand((2, 3, 3)).to(device) # 2 x 3 x 3assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)assert_allclose(f1(input)[0], expected, atol=1e-4, rtol=1e-5)@@ -328,6 +339,7 @@ class TestColorJitter:[0.6, 0.5, 0.4],[0.7, 0.8, 1.]]]]) # 1 x 1 x 3 x 3input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3+ input = input.to(device)expected = torch.tensor([[[[0.2529, 0.3529, 0.4529],[0.7529, 0.6529, 0.5529],@@ -353,6 +365,7 @@ class TestColorJitter:[[0.2660, 0.3660, 0.4660],[0.7660, 0.6660, 0.5660],[0.8660, 0.9660, 1.0000]]]]) # 1 x 1 x 3 x 3+ expected = expected.to(device)assert_allclose(f(input), expected)@@ -364,6 +377,7 @@ class TestColorJitter:[0.6, 0.5, 0.4],[0.7, 0.8, 1.]]]]) # 1 x 1 x 3 x 3input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3+ input = input.to(device)expected = torch.tensor([[[[0.2529, 0.3529, 0.4529],[0.7529, 0.6529, 0.5529],@@ -389,6 +403,7 @@ class TestColorJitter:[[0.2660, 0.3660, 0.4660],[0.7660, 0.6660, 0.5660],[0.8660, 0.9660, 1.0000]]]]) # 1 x 1 x 3 x 3+ expected = expected.to(device)assert_allclose(f(input), expected)@@ -400,6 +415,7 @@ class TestColorJitter:[0.6, 0.5, 0.4],[0.7, 0.8, 1.]]]]) # 1 x 1 x 3 x 3input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3+ input = input.to(device)expected = torch.tensor([[[[0.0953, 0.1906, 0.2859],[0.5719, 0.4766, 0.3813],@@ -425,6 +441,7 @@ class TestColorJitter:[[0.1184, 0.2367, 0.3551],[0.7102, 0.5919, 0.4735],[0.8286, 0.9470, 1.0000]]]])+ expected = expected.to(device)assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)@@ -435,7 +452,7 @@ class TestColorJitter:input = torch.tensor([[[[0.1, 0.2, 0.3],[0.6, 0.5, 0.4],[0.7, 0.8, 1.]]]]) # 1 x 1 x 3 x 3- input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3+ input = input.repeat(2, 3, 1, 1).to(device) # 2 x 3 x 3expected = torch.tensor([[[[0.0953, 0.1906, 0.2859],[0.5719, 0.4766, 0.3813],@@ -461,6 +478,7 @@ class TestColorJitter:[[0.1184, 0.2367, 0.3551],[0.7102, 0.5919, 0.4735],[0.8286, 0.9470, 1.0000]]]])+ expected = expected.to(device)assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)@@ -479,7 +497,7 @@ class TestColorJitter:[[0.6, 0.8, 0.7],[0.9, 0.3, 0.2],[0.8, 0.4, .5]]]]) # 1 x 1 x 3 x 3- input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3+ input = input.repeat(2, 1, 1, 1).to(device) # 2 x 3 x 3expected = torch.tensor([[[[1.8763e-01, 2.5842e-01, 3.3895e-01],[6.2921e-01, 5.0000e-01, 4.0000e-01],@@ -505,6 +523,7 @@ class TestColorJitter:[[5.5556e-01, 8.0000e-01, 7.0000e-01],[9.0000e-01, 2.7651e-01, 1.7651e-01],[8.0000e-01, 3.5302e-01, 4.4127e-01]]]])+ expected = expected.to(device)assert_allclose(f(input), expected)@@ -523,7 +542,7 @@ class TestColorJitter:[[0.6, 0.8, 0.7],[0.9, 0.3, 0.2],[0.8, 0.4, .5]]]]) # 1 x 1 x 3 x 3- input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3+ input = input.repeat(2, 1, 1, 1).to(device) # 2 x 3 x 3expected = torch.tensor([[[[1.8763e-01, 2.5842e-01, 3.3895e-01],[6.2921e-01, 5.0000e-01, 4.0000e-01],@@ -549,6 +568,7 @@ class TestColorJitter:[[5.5556e-01, 8.0000e-01, 7.0000e-01],[9.0000e-01, 2.7651e-01, 1.7651e-01],[8.0000e-01, 3.5302e-01, 4.4127e-01]]]])+ expected = expected.to(device)assert_allclose(f(input), expected)@@ -567,7 +587,7 @@ class TestColorJitter:[[0.6, 0.8, 0.7],[0.9, 0.3, 0.2],[0.8, 0.4, .5]]]]) # 1 x 1 x 3 x 3- input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3+ input = input.repeat(2, 1, 1, 1).to(device) # 2 x 3 x 3expected = torch.tensor([[[[1.8763e-01, 2.5842e-01, 3.3895e-01],[6.2921e-01, 5.0000e-01, 4.0000e-01],@@ -593,6 +613,7 @@ class TestColorJitter:[[5.5556e-01, 8.0000e-01, 7.0000e-01],[9.0000e-01, 2.7651e-01, 1.7651e-01],[8.0000e-01, 3.5302e-01, 4.4127e-01]]]])+ expected = expected.to(device)assert_allclose(f(input), expected)@@ -611,7 +632,7 @@ class TestColorJitter:[[0.6, 0.8, 0.7],[0.9, 0.3, 0.2],[0.8, 0.4, .5]]]]) # 1 x 1 x 3 x 3- input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3+ input = input.repeat(2, 1, 1, 1).to(device) # 2 x 3 x 3expected = torch.tensor([[[[0.1000, 0.2000, 0.3000],[0.6000, 0.5000, 0.4000],@@ -636,6 +657,7 @@ class TestColorJitter:[[0.6347, 0.8000, 0.7000],[0.9000, 0.3000, 0.2000],[0.8000, 0.3730, 0.4692]]]])+ expected = expected.to(device)assert_allclose(f(input), expected)@@ -654,7 +676,7 @@ class TestColorJitter:[[0.6, 0.8, 0.7],[0.9, 0.3, 0.2],[0.8, 0.4, .5]]]]) # 1 x 1 x 3 x 3- input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3+ input = input.repeat(2, 1, 1, 1).to(device) # 2 x 3 x 3expected = torch.tensor([[[[0.1000, 0.2000, 0.3000],[0.6000, 0.5000, 0.4000],@@ -680,6 +702,7 @@ class TestColorJitter:[[0.6347, 0.8000, 0.7000],[0.9000, 0.3000, 0.2000],[0.8000, 0.3730, 0.4692]]]])+ expected = expected.to(device)assert_allclose(f(input), expected)@@ -698,7 +721,7 @@ class TestColorJitter:[[0.6, 0.8, 0.7],[0.9, 0.3, 0.2],[0.8, 0.4, .5]]]]) # 1 x 1 x 3 x 3- input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3+ input = input.repeat(2, 1, 1, 1).to(device) # 2 x 3 x 3expected = torch.tensor([[[[0.1000, 0.2000, 0.3000],[0.6000, 0.5000, 0.4000],@@ -724,6 +747,7 @@ class TestColorJitter:[[0.6347, 0.8000, 0.7000],[0.9000, 0.3000, 0.2000],[0.8000, 0.3730, 0.4692]]]])+ expected = expected.to(device)assert_allclose(f(input), expected)@@ -734,11 +758,12 @@ class TestColorJitter:ColorJitter(return_transform=True),)- input = torch.rand(3, 5, 5) # 3 x 5 x 5+ input = torch.rand(3, 5, 5).to(device) # 3 x 5 x 5expected = inputexpected_transform = torch.eye(3).unsqueeze(0) # 3 x 3+ expected_transform = expected_transform.to(device)assert_allclose(f(input)[0], expected, atol=1e-4, rtol=1e-5)assert_allclose(f(input)[1], expected_transform)@@ -749,21 +774,19 @@ class TestColorJitter:ColorJitter(return_transform=True),)- input = torch.rand(2, 3, 5, 5) # 2 x 3 x 5 x 5+ input = torch.rand(2, 3, 5, 5).to(device) # 2 x 3 x 5 x 5expected = inputexpected_transform = torch.eye(3).unsqueeze(0).expand((2, 3, 3)) # 2 x 3 x 3+ expected_transform = expected_transform.to(device)assert_allclose(f(input)[0], expected, atol=1e-4, rtol=1e-5)assert_allclose(f(input)[0], expected)assert_allclose(f(input)[1], expected_transform)def test_gradcheck(self, device):-- input = torch.rand((3, 5, 5)) # 3 x 3-+ input = torch.rand((3, 5, 5)).to(device) # 3 x 3input = utils.tensor_to_gradcheck_var(input) # to var-assert gradcheck(kornia.color_jitter, (input, ), raise_exception=True)@@ -778,7 +801,7 @@ class TestRectangleRandomErasing:assert rand_rec(input).shape == batch_shapedef test_rectangle_erasing1(self, device):- inputs = torch.ones(1, 1, 10, 10)+ inputs = torch.ones(1, 1, 10, 10).to(device)rect_params = (torch.tensor([5]), torch.tensor([5]),torch.tensor([5]), torch.tensor([5])@@ -794,11 +817,11 @@ class TestRectangleRandomErasing:[1., 1., 1., 1., 1., 0., 0., 0., 0., 0.],[1., 1., 1., 1., 1., 0., 0., 0., 0., 0.],[1., 1., 1., 1., 1., 0., 0., 0., 0., 0.]- ]]])+ ]]]).to(device)assert_allclose(erase_rectangles(inputs, rect_params), expected)def test_rectangle_erasing2(self, device):- inputs = torch.ones(3, 3, 3, 3)+ inputs = torch.ones(3, 3, 3, 3).to(device)rect_params = (torch.tensor([3, 2, 1]), torch.tensor([3, 2, 1]),torch.tensor([0, 1, 2]), torch.tensor([0, 1, 2])@@ -839,7 +862,7 @@ class TestRectangleRandomErasing:[[1., 1., 1.],[1., 1., 1.],[1., 1., 0.]]]]- )+ ).to(device)assert_allclose(erase_rectangles(inputs, rect_params), expected)@@ -853,7 +876,7 @@ class TestRectangleRandomErasing:)# evaluate function gradient- input = torch.rand(batch_shape)+ input = torch.rand(batch_shape).to(device)input = utils.tensor_to_gradcheck_var(input) # to varassert gradcheck(erase_rectangles,@@ -887,9 +910,10 @@ class TestRandomGrayscale:f = RandomGrayscale(return_transform=True)- input = torch.rand(3, 5, 5) # 3 x 5 x 5+ input = torch.rand(3, 5, 5).to(device) # 3 x 5 x 5expected_transform = torch.eye(3).unsqueeze(0) # 3 x 3+ expected_transform = expected_transform.to(device)assert_allclose(f(input)[1], expected_transform)@@ -1038,20 +1062,18 @@ class TestRandomGrayscale:RandomGrayscale(p=0., return_transform=True),)- input = torch.rand(2, 3, 5, 5) # 2 x 3 x 5 x 5+ input = torch.rand(2, 3, 5, 5).to(device) # 2 x 3 x 5 x 5expected = inputexpected_transform = torch.eye(3).unsqueeze(0).expand((2, 3, 3)) # 2 x 3 x 3+ expected_transform = expected_transform.to(device)assert_allclose(f(input)[0], expected)assert_allclose(f(input)[1], expected_transform)def test_gradcheck(self, device):-- input = torch.rand((3, 5, 5)) # 3 x 3-+ input = torch.rand((3, 5, 5)).to(device) # 3 x 3input = utils.tensor_to_gradcheck_var(input) # to var-assert gradcheck(kornia.random_grayscale, (input, 0.), raise_exception=True)assert gradcheck(kornia.random_grayscale, (input, 1.), raise_exception=True)@@ -1101,20 +1123,24 @@ class TestRandomRotation:[0., 0., 0., 0.],[0., 1., 2., 0.],[0., 0., 1., 2.]]) # 4 x 4+ input = input.to(device)expected = torch.tensor([[[0.9824, 0.0088, 0.0000, 1.9649],[0.0000, 0.0029, 0.0000, 0.0176],[0.0029, 1.0000, 1.9883, 0.0000],[0.0000, 0.0088, 1.0117, 1.9649]]]) # 1 x 4 x 4+ expected = expected.to(device)expected_transform = torch.tensor([[[1.0000, -0.0059, 0.0088],[0.0059, 1.0000, -0.0088],[0.0000, 0.0000, 1.0000]]]) # 1 x 3 x 3+ expected_transform = expected_transform.to(device)expected_2 = torch.tensor([[0.1322, 0.0000, 0.7570, 0.2644],[0.3785, 0.0000, 0.4166, 0.0000],[0.0000, 0.6309, 1.5910, 1.2371],[0.0000, 0.1444, 0.3177, 0.6499]]) # 1 x 4 x 4+ expected_2 = expected_2.to(device)out, mat = f(input)assert_allclose(out, expected, rtol=1e-6, atol=1e-4)@@ -1131,6 +1157,7 @@ class TestRandomRotation:[0., 0., 0., 0.],[0., 1., 2., 0.],[0., 0., 1., 2.]]]]) # 1 x 1 x 4 x 4+ input = input.to(device)expected = torch.tensor([[[[0.9824, 0.0088, 0.0000, 1.9649],[0.0000, 0.0029, 0.0000, 0.0176],@@ -1140,6 +1167,7 @@ class TestRandomRotation:[0.3785, 0.0000, 0.4166, 0.0000],[0.0000, 0.6309, 1.5910, 1.2371],[0.0000, 0.1444, 0.3177, 0.6499]]]]) # 2 x 1 x 4 x 4+ expected = expected.to(device)expected_transform = torch.tensor([[[1.0000, -0.0059, 0.0088],[0.0059, 1.0000, -0.0088],@@ -1148,6 +1176,7 @@ class TestRandomRotation:[[0.9125, 0.4090, -0.4823],[-0.4090, 0.9125, 0.7446],[0.0000, 0.0000, 1.0000]]]) # 2 x 3 x 3+ expected_transform = expected_transform.to(device)input = input.repeat(2, 1, 1, 1) # 5 x 3 x 3 x 3@@ -1172,19 +1201,23 @@ class TestRandomRotation:[0., 0., 0., 0.],[0., 1., 2., 0.],[0., 0., 1., 2.]]) # 4 x 4+ input = input.to(device)expected = torch.tensor([[[0.1314, 0.1050, 0.6649, 0.2628],[0.3234, 0.0202, 0.4256, 0.1671],[0.0525, 0.5976, 1.5199, 1.1306],[0.0000, 0.1453, 0.3224, 0.5796]]]) # 1 x 4 x 4+ expected = expected.to(device)expected_transform = torch.tensor([[[0.8864, 0.4629, -0.5240],[-0.4629, 0.8864, 0.8647],[0.0000, 0.0000, 1.0000]]]) # 1 x 3 x 3+ expected_transform = expected_transform.to(device)expected_transform_2 = torch.tensor([[[0.8381, -0.5455, 1.0610],[0.5455, 0.8381, -0.5754],[0.0000, 0.0000, 1.0000]]]) # 1 x 3 x 3+ expected_transform_2 = expected_transform_2.to(device)out, mat = f(input)_, mat_2 = f1(input)@@ -1227,8 +1260,6 @@ class TestRandomRotation:torch.manual_seed(0) # for random reproductibility- input = torch.rand((3, 3)) # 3 x 3-+ input = torch.rand((3, 3)).to(device) # 3 x 3input = utils.tensor_to_gradcheck_var(input) # to var-assert gradcheck(RandomRotation(degrees=(15.0, 15.0)), (input, ), raise_exception=True)