Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix: same_on_batch behavior in _adapted_rsampling for shapes of size 1 (#2699)

Georgy Gunkin0ed9e6e1d882
diff --git a/kornia/augmentation/utils/helpers.py b/kornia/augmentation/utils/helpers.py
index c2352958b..0693be1e3 100644
--- a/kornia/augmentation/utils/helpers.py
+++ b/kornia/augmentation/utils/helpers.py
@@ -204,7 +204,9 @@ def _adapted_rsampling(
shape = torch.Size(shape)
if same_on_batch:
- return dist.rsample(torch.Size((1, *shape[1:]))).repeat(shape[0], *[1] * (len(shape) - 1))
+ rsample_size = torch.Size((1, *shape[1:]))
+ rsample = dist.rsample(rsample_size)
+ return rsample.repeat(shape[0], *[1] * (len(rsample.shape) - 1))
return dist.rsample(shape)
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index 7254d232c..d24e76ece 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -3726,6 +3726,15 @@ class TestRandomFisheye:
aug = RandomFisheye(center_x, center_y, gamma, p=1.0)
assert img.shape == aug(img).shape
+ def test_same_on_batch(self, device, dtype):
+ torch.manual_seed(0)
+ center_x = torch.tensor([-0.3, 0.3], device=device, dtype=dtype)
+ center_y = torch.tensor([-0.3, 0.3], device=device, dtype=dtype)
+ gamma = torch.tensor([-1.0, 1.0], device=device, dtype=dtype)
+ img = torch.rand(1, 1, 2, 2, device=device, dtype=dtype)
+ aug = RandomFisheye(center_x, center_y, gamma, same_on_batch=True, p=1.0)
+ assert img.shape == aug(img).shape
+
@pytest.mark.skip(reason="RuntimeError: Jacobian mismatch for output 0 with respect to input 0")
def test_gradcheck(self, device, dtype):
img = torch.rand(1, 1, 3, 3, device=device, dtype=dtype)