π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): distinguish rotation matrix device and dtype errors (#5767)
peng xiang64f59f06079a
diff --git a/changelog.d/5767.fixed.md b/changelog.d/5767.fixed.mdnew file mode 100644index 000000000..e07cd39fa--- /dev/null+++ b/changelog.d/5767.fixed.md@@ -0,0 +1 @@+`get_rotation_matrix2d` reports mismatched input dtypes separately from mismatched devices, instead of describing both as a device mismatch. Both checks retain `ValueError` and identify the values for `center`, `angle` and `scale`.diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.pyindex 17898dc30..b463d9cfd 100644--- a/kornia/geometry/transform/imgwarp.py+++ b/kornia/geometry/transform/imgwarp.py@@ -749,6 +749,10 @@ def get_rotation_matrix2d(center: torch.Tensor, angle: torch.Tensor, scale: torcReturns:the affine matrix of 2D rotation with shape :math:`(B, 2, 3)`.+ Raises:+ TypeError: If any input is not a tensor.+ ValueError: If input shapes are invalid, or batch sizes, devices or dtypes do not match.+Example:>>> center = torch.zeros(1, 2)>>> scale = torch.ones((1, 2))@@ -785,10 +789,14 @@ def get_rotation_matrix2d(center: torch.Tensor, angle: torch.Tensor, scale: torcf"{scale.shape}")- if not (center.device == angle.device == scale.device) or not (center.dtype == angle.dtype == scale.dtype):+ if not (center.device == angle.device == scale.device):+ raise ValueError(+ f"Inputs must have same device. Got center {center.device}, angle {angle.device} and scale {scale.device}."+ )++ if not (center.dtype == angle.dtype == scale.dtype):raise ValueError(- f"Inputs must have same device Got center ({center.device}, {center.dtype}), angle ({angle.device}, "- f"{angle.dtype}) and scale ({scale.device}, {scale.dtype})"+ f"Inputs must have same dtype. Got center {center.dtype}, angle {angle.dtype} and scale {scale.dtype}.")shift_m = eye_like(3, center)diff --git a/tests/geometry/transform/test_imgwarp.py b/tests/geometry/transform/test_imgwarp.pyindex 074c92819..76a6cde6c 100644--- a/tests/geometry/transform/test_imgwarp.py+++ b/tests/geometry/transform/test_imgwarp.py@@ -365,6 +365,41 @@ class TestGetPerspectiveTransform(BaseTester):class TestRotationMatrix2d(BaseTester):+ @pytest.mark.parametrize("argument", ["center", "angle", "scale"])+ def test_dtype_mismatch_message_5662(self, argument, device, dtype):+ if device.type == "mps" and dtype == torch.float64:+ pytest.skip("MPS does not support float64")+ inputs = {+ "center": torch.zeros(1, 2, device=device, dtype=dtype),+ "angle": torch.zeros(1, device=device, dtype=dtype),+ "scale": torch.ones(1, 2, device=device, dtype=dtype),+ }+ other_dtype = torch.float32 if dtype != torch.float32 else torch.float16+ inputs[argument] = inputs[argument].to(dtype=other_dtype)++ with pytest.raises(ValueError, match="Inputs must have same dtype") as exc:+ kornia.geometry.get_rotation_matrix2d(**inputs)+ for name, value in inputs.items():+ assert f"{name} {value.dtype}" in str(exc.value)++ @pytest.mark.parametrize("argument", ["center", "angle", "scale"])+ def test_device_mismatch_message_5662(self, argument, device, dtype):+ if device.type == "mps" and dtype == torch.float64:+ pytest.skip("MPS does not support float64")+ inputs = {+ "center": torch.zeros(1, 2, device=device, dtype=dtype),+ "angle": torch.zeros(1, device=device, dtype=dtype),+ "scale": torch.ones(1, 2, device=device, dtype=dtype),+ }+ # Meta tensors exercise the CPU device guard without requiring an accelerator.+ other_device = torch.device("meta") if device.type == "cpu" else torch.device("cpu")+ inputs[argument] = inputs[argument].to(device=other_device)++ with pytest.raises(ValueError, match="Inputs must have same device") as exc:+ kornia.geometry.get_rotation_matrix2d(**inputs)+ for name, value in inputs.items():+ assert f"{name} {value.device}" in str(exc.value)+@pytest.mark.parametrize("batch_size", [1, 2, 5])def test_90deg_rotation(self, batch_size, device, dtype):# generate input data