Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): draw So2.random from a uniform angle on [-pi, pi) instead of uniform real and imaginary parts (#4973)

εŒΊζ’“η4216dbf0bc8d
diff --git a/changelog.d/4973.fixed.md b/changelog.d/4973.fixed.md
new file mode 100644
index 000000000..a2938a204
--- /dev/null
+++ b/changelog.d/4973.fixed.md
@@ -0,0 +1,3 @@
+`So2.random` and `Se2.random` now draw a unit rotation with a uniform angle on [-pi, pi) (#4930). They built
+`z = complex(rand, rand)`, so `|z|` ranged over (0, sqrt 2), `matrix()` was a rotation scaled by `|z|` with
+`det = |z|**2` down to 3.5e-6, and every angle was in [0, pi / 2]. The random values under a fixed seed change.
diff --git a/kornia/geometry/liegroup/so2.py b/kornia/geometry/liegroup/so2.py
index 8e0b8189c..fe3638017 100644
--- a/kornia/geometry/liegroup/so2.py
+++ b/kornia/geometry/liegroup/so2.py
@@ -311,14 +311,15 @@ class So2(nn.Module):
>>> s = So2.random(batch_size=3)
"""
+ shape: tuple[int, ...] = ()
if batch_size is not None:
KORNIA_CHECK(batch_size >= 1, msg="batch_size must be positive")
- real_data = torch.rand((batch_size,), device=device, dtype=dtype)
- imag_data = torch.rand((batch_size,), device=device, dtype=dtype)
- else:
- real_data = torch.rand((), device=device, dtype=dtype)
- imag_data = torch.rand((), device=device, dtype=dtype)
- return cls(torch.complex(real_data, imag_data))
+ shape = (batch_size,)
+ # a uniform rotation has a uniform angle on [-pi, pi). Independent uniform real and imaginary parts on
+ # [0, 1) gave |z| anywhere in (0, sqrt 2), so matrix() was a rotation scaled by |z| (determinant |z|**2),
+ # and every angle was in the first quadrant (#4930).
+ theta = (2 * torch.rand(shape, device=device, dtype=dtype) - 1) * torch.pi
+ return cls.exp(theta)
def adjoint(self) -> torch.Tensor:
"""Return the adjoint matrix of shape :math:`(B, 2, 2)`.
diff --git a/tests/geometry/liegroup/test_se2.py b/tests/geometry/liegroup/test_se2.py
index 6fb83acdc..9933a47fd 100644
--- a/tests/geometry/liegroup/test_se2.py
+++ b/tests/geometry/liegroup/test_se2.py
@@ -352,6 +352,16 @@ class TestSe2(BaseTester):
self.assert_close(se2_in_se2.so2.z.imag, i.so2.z.imag)
self.assert_close(se2_in_se2.t, i.t)
+ def test_random_rotation_is_a_uniform_unit_rotation_4930(self, device, dtype):
+ # #4930: Se2.random takes its rotation from So2.random, which was neither unit nor uniform in angle.
+ if dtype not in (torch.float32, torch.float64):
+ pytest.skip("torch.complex has no bfloat16 overload and ComplexHalf support is experimental")
+ torch.manual_seed(0)
+ z = Se2.random(1000, device=device, dtype=dtype).so2.z
+ self.assert_close(z.abs(), torch.ones(1000, device=device, dtype=dtype))
+ theta = z.imag.atan2(z.real)
+ assert theta.min() < -math.pi / 2 and theta.max() > math.pi / 2, (theta.min(), theta.max())
+
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_trans(self, device, dtype, batch_size):
trans = self._make_rand_data(device, dtype, (batch_size, 2))
diff --git a/tests/geometry/liegroup/test_so2.py b/tests/geometry/liegroup/test_so2.py
index c03eb6ca3..f8f644db2 100644
--- a/tests/geometry/liegroup/test_so2.py
+++ b/tests/geometry/liegroup/test_so2.py
@@ -235,6 +235,26 @@ class TestSo2(BaseTester):
self.assert_close(s_in_s.z.real, i.z.real)
self.assert_close(s_in_s.z.imag, i.z.imag)
+ def test_random_is_a_uniform_unit_rotation_4930(self, device, dtype):
+ # #4930: random drew independent uniform real and imaginary parts on [0, 1), so |z| ranged over (0, sqrt 2),
+ # matrix() was a rotation scaled by |z| with det = |z|**2 down to 3.5e-6, and every angle was in [0, pi / 2].
+ if dtype not in (torch.float32, torch.float64):
+ pytest.skip("torch.complex has no bfloat16 overload and linalg.det has no float16 CPU kernel")
+ torch.manual_seed(0)
+ s = So2.random(1000, device=device, dtype=dtype)
+ self.assert_close(s.z.abs(), torch.ones(1000, device=device, dtype=dtype))
+ self.assert_close(torch.linalg.det(s.matrix()), torch.ones(1000, device=device, dtype=dtype))
+ # a uniform angle on [-pi, pi) puts about 250 of 1000 draws in each quadrant (standard deviation 14)
+ theta = s.log()
+ counts = [
+ int(((lo <= theta) & (theta < lo + torch.pi / 2)).sum())
+ for lo in (-torch.pi, -torch.pi / 2, 0.0, torch.pi / 2)
+ ]
+ assert min(counts) > 150, counts
+ # the draws reach both ends of the range: P(none of 1000 within 0.05 of -pi, or of pi) = 3e-4 each
+ assert theta.min() < -torch.pi + 0.05 and theta.max() > torch.pi - 0.05, (theta.min(), theta.max())
+ self.assert_close(So2.random(device=device, dtype=dtype).z.abs(), torch.tensor(1.0, device=device, dtype=dtype))
+
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_adjoint(self, device, dtype, batch_size):
s = So2.identity(batch_size, device=device, dtype=dtype)