π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix tests and warnings (#1834)
Anton Shevtsov020bd1a31050
diff --git a/kornia/testing/__init__.py b/kornia/testing/__init__.pyindex 96a351b01..3fa83f754 100644--- a/kornia/testing/__init__.py+++ b/kornia/testing/__init__.py@@ -9,6 +9,7 @@ from typing import Any, Iterable, List, Optional, Tuple, Type, TypeVar, Union, cimport torchfrom torch import Tensor+from torch.nn import Parameter__all__ = ['tensor_to_gradcheck_var', 'create_eye_batch', 'xla_is_available', 'assert_close']@@ -77,7 +78,7 @@ def create_random_fundamental_matrix(batch_size, std_val=1e-3):class BaseTester(ABC):- DTYPE_PRECISIONS = {torch.float16: (1e-3, 1e-3), torch.float32: (1.3e-6, 1e-5), torch.float64: (1e-6, 1e-5)}+ DTYPE_PRECISIONS = {torch.float16: (1e-3, 1e-3), torch.float32: (1.3e-6, 1e-5), torch.float64: (1.3e-6, 1e-5)}@abstractmethoddef test_smoke(self, device, dtype):@@ -111,6 +112,22 @@ class BaseTester(ABC):atol: Optional[float] = None,low_tolerance: bool = False,) -> None:+ """Asserts that `actual` and `expected` are close.++ Args:+ actual: Actual input.+ expected: Expected input.+ rtol: Relative tolerance.+ atol: Absolute tolerance.+ low_tolerance:+ This parameter allows to reduce tolerance. Half the decimal places.+ Example, 1e-4 -> 1e-2 or 1e-6 -> 1e-3+ """+ if isinstance(actual, Parameter):+ actual = actual.data+ if isinstance(expected, Parameter):+ expected = expected.data+if 'xla' in actual.device.type or 'xla' in expected.device.type:rtol, atol = 1e-2, 1e-2@@ -123,7 +140,7 @@ class BaseTester(ABC):rtol = math.sqrt(rtol) if low_tolerance else rtolatol = math.sqrt(atol) if low_tolerance else atol- return _assert_close(actual, expected, rtol=rtol, atol=atol)+ return assert_close(actual, expected, rtol=rtol, atol=atol)def cartesian_product_of_parameters(**possible_parameters):diff --git a/test/enhance/test_normalize.py b/test/enhance/test_normalize.pyindex fe745d987..413d20c1a 100644--- a/test/enhance/test_normalize.py+++ b/test/enhance/test_normalize.py@@ -293,8 +293,8 @@ class TestNormalizeMinMax(BaseTester):def test_range(self, device, dtype, min_val, max_val):x = torch.rand(1, 2, 4, 5, device=device, dtype=dtype)out = kornia.enhance.normalize_min_max(x, min_val=min_val, max_val=max_val)- self.assert_close(out.min(), torch.tensor(min_val).to(device))- self.assert_close(out.max(), torch.tensor(max_val).to(device))+ self.assert_close(out.min(), torch.tensor(min_val, device=device, dtype=dtype), low_tolerance=True)+ self.assert_close(out.max(), torch.tensor(max_val, device=device, dtype=dtype), low_tolerance=True)def test_values(self, device, dtype):x = torch.tensor([[[[0.0, 1.0, 3.0], [-1.0, 4.0, 3.0], [9.0, 5.0, 2.0]]]], device=device, dtype=dtype)diff --git a/test/geometry/test_line.py b/test/geometry/test_line.pyindex a731dba0a..43b4ce908 100644--- a/test/geometry/test_line.py+++ b/test/geometry/test_line.py@@ -3,33 +3,33 @@ import torchfrom torch.autograd import gradcheckfrom kornia.geometry.line import ParametrizedLine, fit_line-from kornia.testing import assert_close+from kornia.testing import BaseTester-class TestParametrizedLine:+class TestParametrizedLine(BaseTester):def test_smoke(self, device, dtype):p0 = torch.tensor([0.0, 0.0], device=device, dtype=dtype)d0 = torch.tensor([1.0, 0.0], device=device, dtype=dtype)l0 = ParametrizedLine(p0, d0)- assert_close(torch.tensor(l0.origin), p0)- assert_close(torch.tensor(l0.direction), d0)- assert_close(l0.dim(), 2)+ self.assert_close(l0.origin, p0)+ self.assert_close(l0.direction, d0)+ assert l0.dim() == 2def test_through(self, device, dtype):p0 = torch.tensor([-1.0, -1.0], device=device, dtype=dtype)p1 = torch.tensor([1.0, 1.0], device=device, dtype=dtype)l1 = ParametrizedLine.through(p0, p1)direction_expected = torch.tensor([0.7071, 0.7071], device=device, dtype=dtype)- assert_close(torch.tensor(l1.origin), p0)- assert_close(torch.tensor(l1.direction), direction_expected)+ self.assert_close(l1.origin, p0)+ self.assert_close(l1.direction, direction_expected)def test_point_at(self, device, dtype):p0 = torch.tensor([0.0, 0.0], device=device, dtype=dtype)p1 = torch.tensor([1.0, 0.0], device=device, dtype=dtype)l1 = ParametrizedLine.through(p0, p1)- assert_close(l1.point_at(0.0), torch.tensor([0.0, 0.0], device=device, dtype=dtype))- assert_close(l1.point_at(0.5), torch.tensor([0.5, 0.0], device=device, dtype=dtype))- assert_close(l1.point_at(1.0), torch.tensor([1.0, 0.0], device=device, dtype=dtype))+ self.assert_close(l1.point_at(0.0), torch.tensor([0.0, 0.0], device=device, dtype=dtype))+ self.assert_close(l1.point_at(0.5), torch.tensor([0.5, 0.0], device=device, dtype=dtype))+ self.assert_close(l1.point_at(1.0), torch.tensor([1.0, 0.0], device=device, dtype=dtype))def test_projection1(self, device, dtype):p0 = torch.tensor([0.0, 0.0], device=device, dtype=dtype)@@ -38,7 +38,7 @@ class TestParametrizedLine:p3_expected = torch.tensor([0.5, 0.0], device=device, dtype=dtype)l1 = ParametrizedLine.through(p0, p1)p3 = l1.projection(p2)- assert_close(p3, p3_expected)+ self.assert_close(p3, p3_expected)def test_projection2(self, device, dtype):p0 = torch.tensor([0.0, 0.0], device=device, dtype=dtype)@@ -47,7 +47,7 @@ class TestParametrizedLine:p3_expected = torch.tensor([0.0, 0.5], device=device, dtype=dtype)l1 = ParametrizedLine.through(p0, p1)p3 = l1.projection(p2)- assert_close(p3, p3_expected)+ self.assert_close(p3, p3_expected)def test_projection(self, device, dtype):p0 = torch.tensor([0.0, 0.0], device=device, dtype=dtype)@@ -55,7 +55,7 @@ class TestParametrizedLine:l1 = ParametrizedLine.through(p0, p1)point = torch.tensor([1.0, 2.0], device=device, dtype=dtype)point_projection = torch.tensor([1.0, 0.0], device=device, dtype=dtype)- assert_close(l1.projection(point), point_projection)+ self.assert_close(l1.projection(point), point_projection)def test_distance(self, device, dtype):p0 = torch.tensor([0.0, 0.0], device=device, dtype=dtype)@@ -63,7 +63,7 @@ class TestParametrizedLine:l1 = ParametrizedLine.through(p0, p1)point = torch.tensor([1.0, 4.0], device=device, dtype=dtype)distance_expected = torch.tensor(4.0, device=device, dtype=dtype)- assert_close(l1.distance(point), distance_expected)+ self.assert_close(l1.distance(point), distance_expected)def test_squared_distance(self, device, dtype):p0 = torch.tensor([0.0, 0.0], device=device, dtype=dtype)@@ -71,10 +71,30 @@ class TestParametrizedLine:l1 = ParametrizedLine.through(p0, p1)point = torch.tensor([1.0, 4.0], device=device, dtype=dtype)distance_expected = torch.tensor(16.0, device=device, dtype=dtype)- assert_close(l1.squared_distance(point), distance_expected)+ self.assert_close(l1.squared_distance(point), distance_expected)+ @pytest.mark.skip(reason="not implemented yet")+ def test_cardinality(self, device, dtype):+ pass-class TestFitLine:+ @pytest.mark.skip(reason="not implemented yet")+ def test_jit(self, device, dtype):+ pass++ @pytest.mark.skip(reason="not implemented yet")+ def test_exception(self, device, dtype):+ pass++ @pytest.mark.skip(reason="not implemented yet")+ def test_module(self, device, dtype):+ pass++ @pytest.mark.skip(reason="not implemented yet")+ def test_gradcheck(self, device):+ pass+++class TestFitLine(BaseTester):@pytest.mark.parametrize("B", (1, 2))@pytest.mark.parametrize("D", (2, 3, 4))def test_smoke(self, device, dtype, B, D):@@ -111,7 +131,7 @@ class TestFitLine:angle_est = torch.nn.functional.cosine_similarity(line_est.direction, dir_exp, -1)angle_exp = torch.tensor([1.0], device=device, dtype=dtype)- assert_close(angle_est.abs(), angle_exp)+ self.assert_close(angle_est.abs(), angle_exp)def test_fit_line3(self, device, dtype):p0 = torch.tensor([0.0, 0.0, 0.0], device=device, dtype=dtype)@@ -131,7 +151,7 @@ class TestFitLine:# NOTE: result differs with the sign between cpu/cudaangle_est = torch.nn.functional.cosine_similarity(line_est.direction, dir_exp, -1)angle_exp = torch.tensor([1.0], device=device, dtype=dtype)- assert_close(angle_est.abs(), angle_exp)+ self.assert_close(angle_est.abs(), angle_exp)@pytest.mark.skip(reason="numerical do not match with analytical")def test_gradcheck(self, device):@@ -142,3 +162,19 @@ class TestFitLine:pts = torch.rand(1, 3, 2, device=device, dtype=torch.float64, requires_grad=True)weights = torch.rand(1, 3, device=device, dtype=torch.float64, requires_grad=False)assert gradcheck(proxy_func, (pts, weights), raise_exception=True)++ @pytest.mark.skip(reason="not implemented yet")+ def test_cardinality(self, device, dtype):+ pass++ @pytest.mark.skip(reason="not implemented yet")+ def test_jit(self, device, dtype):+ pass++ @pytest.mark.skip(reason="not implemented yet")+ def test_exception(self, device, dtype):+ pass++ @pytest.mark.skip(reason="not implemented yet")+ def test_module(self, device, dtype):+ passdiff --git a/test/test_contrib.py b/test/test_contrib.pyindex 270396dae..0bb052c92 100644--- a/test/test_contrib.py+++ b/test/test_contrib.py@@ -518,9 +518,9 @@ class TestConvDistanceTransform:output1 = kornia.contrib.distance_transform(sample1, kernel_size, h)assert_close(expected_output1, output1)- def test_gradcheck(self, device, dtype):+ def test_gradcheck(self, device):B, C, H, W = 1, 1, 32, 32- sample1 = torch.ones(B, C, H, W, device=device, dtype=dtype, requires_grad=True)+ sample1 = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)assert gradcheck(kornia.contrib.distance_transform, (sample1), raise_exception=True)def test_loss_grad(self, device, dtype):