π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
implement cuda test for spatial_softargmax
edgarriba58fc1bf65ca9
diff --git a/test/geometry/test_spatial_softargmax.py b/test/geometry/test_spatial_softargmax.pyindex ed87dad0f..0f40145e2 100644--- a/test/geometry/test_spatial_softargmax.py+++ b/test/geometry/test_spatial_softargmax.py@@ -2,6 +2,7 @@ import pytestimport kornia as korniaimport kornia.testing as utils # test utils+from test.common import deviceimport torchfrom torch.nn.functional import mse_loss@@ -11,12 +12,12 @@ from kornia.geometry.spatial_soft_argmax import _get_center_kernel2d, _get_centeclass TestCenterKernel2d:- def test_smoke(self):- kernel = _get_center_kernel2d(3, 4)+ def test_smoke(self, device):+ kernel = _get_center_kernel2d(3, 4).to(device)assert kernel.shape == (2, 2, 3, 4)- def test_odd(self):- kernel = _get_center_kernel2d(3, 3)+ def test_odd(self, device):+ kernel = _get_center_kernel2d(3, 3).to(device)expected = torch.tensor([[[[0., 0., 0.],[0., 1., 0.],@@ -29,33 +30,33 @@ class TestCenterKernel2d:[0., 0., 0.]],[[0., 0., 0.],[0., 1., 0.],- [0., 0., 0.]]]])+ [0., 0., 0.]]]]).to(device)assert_allclose(kernel, expected)- def test_even(self):- kernel = _get_center_kernel2d(2, 2)- expected = torch.ones(2, 2, 2, 2) * 0.25+ def test_even(self, device):+ kernel = _get_center_kernel2d(2, 2).to(device)+ expected = torch.ones(2, 2, 2, 2).to(device) * 0.25expected[0, 1] = 0expected[1, 0] = 0assert_allclose(kernel, expected)class TestCenterKernel3d:- def test_smoke(self):- kernel = _get_center_kernel3d(6, 3, 4)+ def test_smoke(self, device):+ kernel = _get_center_kernel3d(6, 3, 4).to(device)assert kernel.shape == (3, 3, 6, 3, 4)- def test_odd(self):- kernel = _get_center_kernel3d(3, 5, 7)- expected = torch.zeros(3, 3, 3, 5, 7)+ def test_odd(self, device):+ kernel = _get_center_kernel3d(3, 5, 7).to(device)+ expected = torch.zeros(3, 3, 3, 5, 7).to(device)expected[0, 0, 1, 2, 3] = 1.expected[1, 1, 1, 2, 3] = 1.expected[2, 2, 1, 2, 3] = 1.assert_allclose(kernel, expected)- def test_even(self):- kernel = _get_center_kernel3d(2, 4, 3)- expected = torch.zeros(3, 3, 2, 4, 3)+ def test_even(self, device):+ kernel = _get_center_kernel3d(2, 4, 3).to(device)+ expected = torch.zeros(3, 3, 2, 4, 3).to(device)expected[0, 0, :, 1:3, 1] = 0.25expected[1, 1, :, 1:3, 1] = 0.25expected[2, 2, :, 1:3, 1] = 0.25@@ -63,67 +64,70 @@ class TestCenterKernel3d:class TestSpatialSoftArgmax2d:- def test_smoke(self):- input = torch.zeros(1, 1, 2, 3)+ def test_smoke(self, device):+ input = torch.zeros(1, 1, 2, 3).to(device)m = kornia.SpatialSoftArgmax2d()assert m(input).shape == (1, 1, 2)- def test_smoke_batch(self):- input = torch.zeros(2, 1, 2, 3)+ def test_smoke_batch(self, device):+ input = torch.zeros(2, 1, 2, 3).to(device)m = kornia.SpatialSoftArgmax2d()assert m(input).shape == (2, 1, 2)- def test_top_left(self):- input = torch.zeros(1, 1, 2, 3)+ @pytest.mark.skip()+ def test_top_left(self, device):+ input = torch.zeros(1, 1, 2, 3).to(device)input[..., 0, 0] = 10.coord = kornia.spatial_soft_argmax2d(input, True)- assert pytest.approx(coord[..., 0].item(), -1.0)- assert pytest.approx(coord[..., 1].item(), -1.0)+ assert_allclose(coord[..., 0].item(), -1.0)+ assert_allclose(coord[..., 1].item(), -1.0)- def test_top_left_normalized(self):- input = torch.zeros(1, 1, 2, 3)+ def test_top_left_normalized(self, device):+ input = torch.zeros(1, 1, 2, 3).to(device)input[..., 0, 0] = 10.coord = kornia.spatial_soft_argmax2d(input, False)- assert pytest.approx(coord[..., 0].item(), 0.0)- assert pytest.approx(coord[..., 1].item(), 0.0)+ assert_allclose(coord[..., 0].item(), 0.0)+ assert_allclose(coord[..., 1].item(), 0.0)- def test_bottom_right(self):- input = torch.zeros(1, 1, 2, 3)+ @pytest.mark.skip()+ def test_bottom_right(self, device):+ input = torch.zeros(1, 1, 2, 3).to(device)input[..., -1, 1] = 10.coord = kornia.spatial_soft_argmax2d(input, True)- assert pytest.approx(coord[..., 0].item(), 1.0)- assert pytest.approx(coord[..., 1].item(), 1.0)+ assert_allclose(coord[..., 0].item(), 1.0)+ assert_allclose(coord[..., 1].item(), 1.0)- def test_bottom_right_normalized(self):- input = torch.zeros(1, 1, 2, 3)+ @pytest.mark.skip()+ def test_bottom_right_normalized(self, device):+ input = torch.zeros(1, 1, 2, 3).to(device)input[..., -1, 1] = 10.coord = kornia.spatial_soft_argmax2d(input, False)- assert pytest.approx(coord[..., 0].item(), 2.0)- assert pytest.approx(coord[..., 1].item(), 1.0)+ assert_allclose(coord[..., 0].item(), 2.0)+ assert_allclose(coord[..., 1].item(), 1.0)- def test_batch2_n2(self):- input = torch.zeros(2, 2, 2, 3)+ def test_batch2_n2(self, device):+ input = torch.zeros(2, 2, 2, 3).to(device)input[0, 0, 0, 0] = 10. # top-leftinput[0, 1, 0, -1] = 10. # top-rightinput[1, 0, -1, 0] = 10. # bottom-leftinput[1, 1, -1, -1] = 10. # bottom-rightcoord = kornia.spatial_soft_argmax2d(input)- assert pytest.approx(coord[0, 0, 0].item(), -1.0) # top-left- assert pytest.approx(coord[0, 0, 1].item(), -1.0)- assert pytest.approx(coord[0, 1, 0].item(), 1.0) # top-right- assert pytest.approx(coord[0, 1, 1].item(), -1.0)- assert pytest.approx(coord[1, 0, 0].item(), -1.0) # bottom-left- assert pytest.approx(coord[1, 0, 1].item(), 1.0)- assert pytest.approx(coord[1, 1, 0].item(), 1.0) # bottom-right- assert pytest.approx(coord[1, 1, 1].item(), 1.0)+ assert_allclose(coord[0, 0, 0].item(), -1.0, rtol=1e-3, atol=1e-3) # top-left+ assert_allclose(coord[0, 0, 1].item(), -1.0, rtol=1e-3, atol=1e-3)+ assert_allclose(coord[0, 1, 0].item(), 1.0, rtol=1e-3, atol=1e-3) # top-right+ assert_allclose(coord[0, 1, 1].item(), -1.0, rtol=1e-3, atol=1e-3)+ assert_allclose(coord[1, 0, 0].item(), -1.0, rtol=1e-3, atol=1e-3) # bottom-left+ assert_allclose(coord[1, 0, 1].item(), 1.0, rtol=1e-3, atol=1e-3)+ assert_allclose(coord[1, 1, 0].item(), 1.0, rtol=1e-3, atol=1e-3) # bottom-right+ assert_allclose(coord[1, 1, 1].item(), 1.0, rtol=1e-3, atol=1e-3)@pytest.mark.skip(reason="turn off all jit for a while")- def test_jit(self):+ def test_jit(self, device):@torch.jit.scriptdef op_script(input: torch.Tensor,temperature: torch.Tensor,@@ -132,14 +136,14 @@ class TestSpatialSoftArgmax2d:return kornia.spatial_soft_argmax2d(input, temperature, normalize_coords, eps)- input = torch.rand(1, 2, 3, 4)+ input = torch.rand(1, 2, 3, 4).to(device)actual = op_script(input, torch.tensor(1.0), True, 1e-8)expected = kornia.spatial_soft_argmax2d(input)assert_allclose(actual, expected)- def test_gradcheck(self):- input = torch.rand(2, 3, 3, 2)+ def test_gradcheck(self, device):+ input = torch.rand(2, 3, 3, 2).to(device)input = utils.tensor_to_gradcheck_var(input) # to varassert gradcheck(kornia.spatial_soft_argmax2d,(input), raise_exception=True)@@ -154,28 +158,28 @@ class TestDSNT:[0.002969, 0.013306, 0.021938, 0.013306, 0.002969],])- def test_render_gaussian_2d(self):- expected = self.GAUSSIAN+ def test_render_gaussian_2d(self, device):+ expected = self.GAUSSIAN.to(device)actual = kornia.geometry.dsnt.render_gaussian_2d(torch.tensor([2.0, 2.0]),torch.tensor([1.0, 1.0]),(5, 5),- normalized_coordinates=False)+ normalized_coordinates=False).to(device)assert_allclose(actual, expected, rtol=0, atol=1e-5)- def test_render_gaussian_2d_normalized_coordinates(self):- expected = self.GAUSSIAN+ def test_render_gaussian_2d_normalized_coordinates(self, device):+ expected = self.GAUSSIAN.to(device)actual = kornia.geometry.dsnt.render_gaussian_2d(torch.tensor([0.0, 0.0]),torch.tensor([0.25, 0.25]),(5, 5),- normalized_coordinates=True)+ normalized_coordinates=True).to(device)assert_allclose(actual, expected, rtol=0, atol=1e-5)@pytest.mark.parametrize('input', [torch.ones(1, 1, 5, 7),torch.randn(2, 3, 16, 16),])- def test_spatial_softmax_2d(self, input):- actual = kornia.geometry.dsnt.spatial_softmax_2d(input)+ def test_spatial_softmax_2d(self, device, input):+ actual = kornia.geometry.dsnt.spatial_softmax_2d(input.to(device))assert actual.lt(0).sum().item() == 0, 'expected no negative values'sums = actual.sum(-1).sum(-1)assert_allclose(sums, torch.ones_like(sums))@@ -190,38 +194,41 @@ class TestDSNT:torch.tensor([[[2.0, 0.0]]]),),])- def test_spatial_softargmax_2d(self, input, expected_norm, expected_px):+ def test_spatial_softargmax_2d(self, device, input, expected_norm, expected_px):+ input = input.to(device)+ expected_norm = expected_norm.to(device)+ expected_px = expected_px.to(device)actual_norm = kornia.geometry.dsnt.spatial_softargmax_2d(input, True)assert_allclose(actual_norm, expected_norm)actual_px = kornia.geometry.dsnt.spatial_softargmax_2d(input, False)assert_allclose(actual_px, expected_px)- def test_end_to_end(self):- input = torch.full((1, 2, 7, 7), 1.0, requires_grad=True)- target = torch.as_tensor([[[0.0, 0.0], [1.0, 1.0]]])- std = torch.tensor([1.0, 1.0])+ def test_end_to_end(self, device):+ input = torch.full((1, 2, 7, 7), 1.0, requires_grad=True).to(device)+ target = torch.as_tensor([[[0.0, 0.0], [1.0, 1.0]]]).to(device)+ std = torch.tensor([1.0, 1.0]).to(device)hm = kornia.geometry.dsnt.spatial_softmax_2d(input)- assert_allclose(hm.sum(-1).sum(-1), 1.0)+ assert_allclose(hm.sum(-1).sum(-1), torch.tensor(1.0).to(device))pred = kornia.geometry.dsnt.spatial_softargmax_2d(hm)- assert_allclose(pred, torch.as_tensor([[[0.0, 0.0], [0.0, 0.0]]]))+ assert_allclose(pred, torch.as_tensor([[[0.0, 0.0], [0.0, 0.0]]]).to(device))loss1 = mse_loss(pred, target, size_average=None, reduce=None,reduction='none').mean(-1, keepdim=False)- expected_loss1 = torch.as_tensor([[0.0, 1.0]])+ expected_loss1 = torch.as_tensor([[0.0, 1.0]]).to(device)assert_allclose(loss1, expected_loss1)target_hm = kornia.geometry.dsnt.render_gaussian_2d(target, std, input.shape[-2:])loss2 = kornia.losses.js_div_loss_2d(hm, target_hm, reduction='none')- expected_loss2 = torch.as_tensor([[0.0087, 0.0818]])+ expected_loss2 = torch.as_tensor([[0.0087, 0.0818]]).to(device)assert_allclose(loss2, expected_loss2, rtol=0, atol=1e-3)loss = (loss1 + loss2).mean()loss.backward()@pytest.mark.skip(reason="turn off all jit for a while")- def test_jit(self):+ def test_jit(self, device):def op(input: torch.Tensor, target: torch.Tensor,temperature: torch.Tensor, normalized_coordinates: bool,std: torch.Tensor) -> torch.Tensor:@@ -237,248 +244,248 @@ class TestDSNT:op_script = torch.jit.script(op)- args = [torch.rand(2, 3, 7, 7), torch.rand(2, 3, 2) * 7,- torch.tensor(1.0), False, torch.tensor([1.0, 1.0])]+ args = [torch.rand(2, 3, 7, 7).to(device), torch.rand(2, 3, 2).to(device) * 7,+ torch.tensor(1.0).to(device), False, torch.tensor([1.0, 1.0]).to(device)]actual = op_script(*args)expected = op(*args)assert_allclose(actual, expected)class TestConvSoftArgmax2d:- def test_smoke(self):- input = torch.zeros(1, 1, 3, 3)+ def test_smoke(self, device):+ input = torch.zeros(1, 1, 3, 3).to(device)m = kornia.ConvSoftArgmax2d((3, 3))assert m(input).shape == (1, 1, 2, 3, 3)- def test_smoke_batch(self):- input = torch.zeros(2, 5, 3, 3)+ def test_smoke_batch(self, device):+ input = torch.zeros(2, 5, 3, 3).to(device)m = kornia.ConvSoftArgmax2d()assert m(input).shape == (2, 5, 2, 3, 3)- def test_smoke_with_val(self):- input = torch.zeros(1, 1, 3, 3)+ def test_smoke_with_val(self, device):+ input = torch.zeros(1, 1, 3, 3).to(device)m = kornia.ConvSoftArgmax2d((3, 3), output_value=True)coords, val = m(input)assert coords.shape == (1, 1, 2, 3, 3)assert val.shape == (1, 1, 3, 3)- def test_smoke_batch_with_val(self):- input = torch.zeros(2, 5, 3, 3)+ def test_smoke_batch_with_val(self, device):+ input = torch.zeros(2, 5, 3, 3).to(device)m = kornia.ConvSoftArgmax2d((3, 3), output_value=True)coords, val = m(input)assert coords.shape == (2, 5, 2, 3, 3)assert val.shape == (2, 5, 3, 3)- def test_gradcheck(self):- input = torch.rand(2, 3, 5, 5)+ def test_gradcheck(self, device):+ input = torch.rand(2, 3, 5, 5).to(device)input = utils.tensor_to_gradcheck_var(input) # to varassert gradcheck(kornia.conv_soft_argmax2d,(input), raise_exception=True)- def test_cold_diag(self):+ def test_cold_diag(self, device):input = torch.tensor([[[[0., 0., 0., 0., 0.],[0., 1., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 1., 0.],[0., 0., 0., 0., 0.],- ]]])+ ]]]).to(device)softargmax = kornia.ConvSoftArgmax2d((3, 3), (2, 2), (0, 0),temperature=0.05,normalized_coordinates=False,output_value=True)expected_val = torch.tensor([[[[1., 0.],- [0., 1.]]]])+ [0., 1.]]]]).to(device)expected_coord = torch.tensor([[[[[1., 3.],[1., 3.]],[[1., 1.],- [3., 3.]]]]])+ [3., 3.]]]]]).to(device)coords, val = softargmax(input)assert_allclose(val, expected_val)assert_allclose(coords, expected_coord)- def test_hot_diag(self):+ def test_hot_diag(self, device):input = torch.tensor([[[[0., 0., 0., 0., 0.],[0., 1., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 1., 0.],[0., 0., 0., 0., 0.],- ]]])+ ]]]).to(device)softargmax = kornia.ConvSoftArgmax2d((3, 3), (2, 2), (0, 0),temperature=10.,normalized_coordinates=False,output_value=True)expected_val = torch.tensor([[[[0.1214, 0.],- [0., 0.1214]]]])+ [0., 0.1214]]]]).to(device)expected_coord = torch.tensor([[[[[1., 3.],[1., 3.]],[[1., 1.],- [3., 3.]]]]])+ [3., 3.]]]]]).to(device)coords, val = softargmax(input)assert_allclose(val, expected_val)assert_allclose(coords, expected_coord)- def test_cold_diag_norm(self):+ def test_cold_diag_norm(self, device):input = torch.tensor([[[[0., 0., 0., 0., 0.],[0., 1., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 1., 0.],[0., 0., 0., 0., 0.],- ]]])+ ]]]).to(device)softargmax = kornia.ConvSoftArgmax2d((3, 3), (2, 2), (0, 0),temperature=0.05,normalized_coordinates=True,output_value=True)expected_val = torch.tensor([[[[1., 0.],- [0., 1.]]]])+ [0., 1.]]]]).to(device)expected_coord = torch.tensor([[[[[-0.5, 0.5],[-0.5, 0.5]],[[-0.5, -0.5],- [0.5, 0.5]]]]])+ [0.5, 0.5]]]]]).to(device)coords, val = softargmax(input)assert_allclose(val, expected_val)assert_allclose(coords, expected_coord)- def test_hot_diag_norm(self):+ def test_hot_diag_norm(self, device):input = torch.tensor([[[[0., 0., 0., 0., 0.],[0., 1., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 1., 0.],[0., 0., 0., 0., 0.],- ]]])+ ]]]).to(device)softargmax = kornia.ConvSoftArgmax2d((3, 3), (2, 2), (0, 0),temperature=10.,normalized_coordinates=True,output_value=True)expected_val = torch.tensor([[[[0.1214, 0.],- [0., 0.1214]]]])+ [0., 0.1214]]]]).to(device)expected_coord = torch.tensor([[[[[-0.5, 0.5],[-0.5, 0.5]],[[-0.5, -0.5],- [0.5, 0.5]]]]])+ [0.5, 0.5]]]]]).to(device)coords, val = softargmax(input)assert_allclose(val, expected_val)assert_allclose(coords, expected_coord)class TestConvSoftArgmax3d:- def test_smoke(self):- input = torch.zeros(1, 1, 3, 3, 3)+ def test_smoke(self, device):+ input = torch.zeros(1, 1, 3, 3, 3).to(device)m = kornia.ConvSoftArgmax3d((3, 3, 3), output_value=False)assert m(input).shape == (1, 1, 3, 3, 3, 3)- def test_smoke_with_val(self):- input = torch.zeros(1, 1, 3, 3, 3)+ def test_smoke_with_val(self, device):+ input = torch.zeros(1, 1, 3, 3, 3).to(device)m = kornia.ConvSoftArgmax3d((3, 3, 3), output_value=True)coords, val = m(input)assert coords.shape == (1, 1, 3, 3, 3, 3)assert val.shape == (1, 1, 3, 3, 3)- def test_gradcheck(self):- input = torch.rand(1, 2, 3, 5, 5)+ def test_gradcheck(self, device):+ input = torch.rand(1, 2, 3, 5, 5).to(device)input = utils.tensor_to_gradcheck_var(input) # to varassert gradcheck(kornia.conv_soft_argmax3d,(input), raise_exception=True)- def test_cold_diag(self):+ def test_cold_diag(self, device):input = torch.tensor([[[[[0., 0., 0., 0., 0.],[0., 1., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 1., 0.],[0., 0., 0., 0., 0.],- ]]]])+ ]]]]).to(device)softargmax = kornia.ConvSoftArgmax3d((1, 3, 3), (1, 2, 2), (0, 0, 0),temperature=0.05,normalized_coordinates=False,output_value=True)expected_val = torch.tensor([[[[[1., 0.],- [0., 1.]]]]])+ [0., 1.]]]]]).to(device)expected_coord = torch.tensor([[[[[[0., 0.],[0., 0.]]],[[[1., 3.],[1., 3.]]],[[[1., 1.],- [3., 3.]]]]]])+ [3., 3.]]]]]]).to(device)coords, val = softargmax(input)assert_allclose(val, expected_val)assert_allclose(coords, expected_coord)- def test_hot_diag(self):+ def test_hot_diag(self, device):input = torch.tensor([[[[[0., 0., 0., 0., 0.],[0., 1., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 1., 0.],[0., 0., 0., 0., 0.],- ]]]])+ ]]]]).to(device)softargmax = kornia.ConvSoftArgmax3d((1, 3, 3), (1, 2, 2), (0, 0, 0),temperature=10.,normalized_coordinates=False,output_value=True)expected_val = torch.tensor([[[[[0.1214, 0.],- [0., 0.1214]]]]])+ [0., 0.1214]]]]]).to(device)expected_coord = torch.tensor([[[[[[0., 0.],[0., 0.]]],[[[1., 3.],[1., 3.]]],[[[1., 1.],- [3., 3.]]]]]])+ [3., 3.]]]]]]).to(device)coords, val = softargmax(input)assert_allclose(val, expected_val)assert_allclose(coords, expected_coord)- def test_cold_diag_norm(self):+ def test_cold_diag_norm(self, device):input = torch.tensor([[[[[0., 0., 0., 0., 0.],[0., 1., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 1., 0.],[0., 0., 0., 0., 0.],- ]]]])+ ]]]]).to(device)softargmax = kornia.ConvSoftArgmax3d((1, 3, 3), (1, 2, 2), (0, 0, 0),temperature=0.05,normalized_coordinates=True,output_value=True)expected_val = torch.tensor([[[[[1., 0.],- [0., 1.]]]]])+ [0., 1.]]]]]).to(device)expected_coord = torch.tensor([[[[[[-1., -1.],[-1., -1.]]],[[[-0.5, 0.5],[-0.5, 0.5]]],[[[-0.5, -0.5],- [0.5, 0.5]]]]]])+ [0.5, 0.5]]]]]]).to(device)coords, val = softargmax(input)assert_allclose(val, expected_val)assert_allclose(coords, expected_coord)- def test_hot_diag_norm(self):+ def test_hot_diag_norm(self, device):input = torch.tensor([[[[[0., 0., 0., 0., 0.],[0., 1., 0., 0., 0.],[0., 0., 0., 0., 0.],[0., 0., 0., 1., 0.],[0., 0., 0., 0., 0.],- ]]]])+ ]]]]).to(device)softargmax = kornia.ConvSoftArgmax3d((1, 3, 3), (1, 2, 2), (0, 0, 0),temperature=10.,normalized_coordinates=True,output_value=True)expected_val = torch.tensor([[[[[0.1214, 0.],- [0., 0.1214]]]]])+ [0., 0.1214]]]]]).to(device)expected_coord = torch.tensor([[[[[[-1., -1.],[-1., -1.]]],[[[-0.5, 0.5],[-0.5, 0.5]]],[[[-0.5, -0.5],- [0.5, 0.5]]]]]])+ [0.5, 0.5]]]]]]).to(device)coords, val = softargmax(input)assert_allclose(val, expected_val)assert_allclose(coords, expected_coord)