π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): keep centered fisheye gradients finite (#6239)
A Aswanth Rajd14078eb467e
diff --git a/changelog.d/6239.fixed.md b/changelog.d/6239.fixed.mdnew file mode 100644index 000000000..63b6f5c7e--- /dev/null+++ b/changelog.d/6239.fixed.md@@ -0,0 +1 @@+`RandomFisheye` returns finite gradients with respect to its `center_x` and `center_y` parameters when the distortion center falls exactly on a pixel, as a centered distortion does on an odd-sized image. They were NaN there. The output values are unchanged.diff --git a/kornia/augmentation/_2d/geometric/fisheye.py b/kornia/augmentation/_2d/geometric/fisheye.pyindex 25f714460..040d56720 100644--- a/kornia/augmentation/_2d/geometric/fisheye.py+++ b/kornia/augmentation/_2d/geometric/fisheye.py@@ -23,6 +23,7 @@ from kornia.augmentation import random_generator as rgfrom kornia.augmentation._2d.base import AugmentationBase2Dfrom kornia.core.utils import _to_device_nonblockingfrom kornia.geometry.grid import create_meshgrid+from kornia.geometry.linalg import euclidean_distancefrom kornia.geometry.transform import remap@@ -111,7 +112,9 @@ class RandomFisheye(AugmentationBase2D):center_y = _to_device_nonblocking(params["center_y"].view(B, 1, 1), input.device, input.dtype)gamma = _to_device_nonblocking(params["gamma"].view(B, 1, 1), input.device, input.dtype)# compute and apply the distances respect to the camera optical center- distance = ((center_x - field_x) ** 2 + (center_y - field_y) ** 2) ** 0.5+ distance = euclidean_distance(+ torch.stack((center_x, center_y), dim=-1), torch.stack((field_x, field_y), dim=-1)+ )field_x = field_x + field_x * distance**gamma # BxHxwfield_y = field_y + field_y * distance**gamma # BxHxWreturn remap(input, field_x, field_y, normalized_coordinates=True, align_corners=True)diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.pyindex 5c408e5bf..aa400e3f1 100644--- a/tests/augmentation/test_augmentation.py+++ b/tests/augmentation/test_augmentation.py@@ -5314,6 +5314,35 @@ class TestDenormalize(BaseTester):class TestRandomFisheye(BaseTester):+ @pytest.mark.parametrize("gamma_value", [0.9, 1.0, 2.0])+ def test_center_parameter_gradients(self, device, dtype, gamma_value):+ center_x = torch.zeros(2, device=device, dtype=dtype, requires_grad=True)+ center_y = torch.zeros(2, device=device, dtype=dtype, requires_grad=True)+ gamma = torch.full((2,), gamma_value, device=device, dtype=dtype, requires_grad=True)+ image = torch.arange(63, device=device, dtype=dtype).reshape(1, 1, 7, 9) / 63++ output = RandomFisheye(center_x, center_y, gamma, p=1.0)(image)+ gradients = torch.autograd.grad(output.sum(), (center_x, center_y, gamma))+ assert torch.isfinite(output).all()+ for gradient in gradients:+ assert torch.isfinite(gradient).all()++ @pytest.mark.parametrize("gamma_value", [0.9, 1.0, 2.0])+ def test_center_parameter_gradcheck(self, device, gamma_value):+ image = torch.arange(63, device=device, dtype=torch.float64).reshape(1, 1, 7, 9) / 63+ bounds = torch.zeros(2, device=device)+ aug = RandomFisheye(bounds, bounds, torch.ones_like(bounds), p=1.0)++ def apply(center_x, center_y, gamma):+ params = {"center_x": center_x, "center_y": center_y, "gamma": gamma, "batch_prob": image.new_ones(1)}+ return aug(image, params=params)++ self.gradcheck(+ apply,+ (bounds[:1], bounds[:1], torch.tensor([gamma_value], device=device)),+ nondet_tol=1e-5,+ )+def test_smoke(self, device, dtype):torch.manual_seed(0)center_x = torch.tensor([-0.3, 0.3])