Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix: open BlobDoGSingle's repr parenthesis and list every shape in _validate_shape (#6357)

Parman Mohammadalizadeh361e0d99a052
diff --git a/changelog.d/6357.fixed.md b/changelog.d/6357.fixed.md
new file mode 100644
index 000000000..2ce2b6cc6
--- /dev/null
+++ b/changelog.d/6357.fixed.md
@@ -0,0 +1 @@
+`BlobDoGSingle.__repr__` now opens its parenthesis, and the augmentation input shape check names every allowed shape in its error instead of only the last one.
diff --git a/kornia/augmentation/utils/helpers.py b/kornia/augmentation/utils/helpers.py
index 994b4edfc..e47606851 100644
--- a/kornia/augmentation/utils/helpers.py
+++ b/kornia/augmentation/utils/helpers.py
@@ -345,7 +345,7 @@ def _validate_shape(shape: Union[Tuple[int, ...], torch.Size], required_shapes:
passed = True
break
if not passed:
- raise TypeError(f"Expected input shape in {required_shape}. Got {shape}.")
+ raise TypeError(f"Expected input shape in {required_shapes}. Got {shape}.")
def _validate_input_shape(input: torch.Tensor, channel_index: int, number: int) -> bool:
diff --git a/kornia/feature/responses.py b/kornia/feature/responses.py
index 756f1e00e..1c51e41f9 100644
--- a/kornia/feature/responses.py
+++ b/kornia/feature/responses.py
@@ -398,7 +398,7 @@ class BlobDoGSingle(nn.Module):
self.sigma2 = sigma2
def __repr__(self) -> str:
- return f"{self.__class__.__name__}, sigma1={self.sigma1}, sigma2={self.sigma2})"
+ return f"{self.__class__.__name__}(sigma1={self.sigma1}, sigma2={self.sigma2})"
def forward(self, input: torch.Tensor, sigmas: Optional[torch.Tensor] = None) -> torch.Tensor:
"""Compute single-scale Difference-of-Gaussians response.
diff --git a/tests/augmentation/test_augmentation_helpers.py b/tests/augmentation/test_augmentation_helpers.py
new file mode 100644
index 000000000..64795e702
--- /dev/null
+++ b/tests/augmentation/test_augmentation_helpers.py
@@ -0,0 +1,35 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import re
+
+import pytest
+import torch
+
+from kornia.augmentation.utils.helpers import _validate_shape
+
+
+class TestValidateShape:
+ def test_accepts_any_listed_rank(self):
+ _validate_shape(torch.Size([1, 2, 3, 4]), required_shapes=("BCHW", "BCDHW"))
+ _validate_shape(torch.Size([1, 2, 3, 4, 5]), required_shapes=("BCHW", "BCDHW"))
+
+ def test_error_lists_every_required_shape(self):
+ shape = torch.Size([3, 4])
+ message = f"Expected input shape in ('BCHW', 'BCDHW'). Got {shape}."
+ with pytest.raises(TypeError, match=re.escape(message)):
+ _validate_shape(shape, required_shapes=("BCHW", "BCDHW"))
diff --git a/tests/feature/test_responces_local_features.py b/tests/feature/test_responces_local_features.py
index c18a27b55..11b3a8142 100644
--- a/tests/feature/test_responces_local_features.py
+++ b/tests/feature/test_responces_local_features.py
@@ -465,6 +465,10 @@ class TestBlobDoGSingle(BaseTester):
img = torch.rand(batch_size, channels, height, width, device=device, dtype=torch.float64)
self.gradcheck(kornia.feature.dog_response_single, (img), nondet_tol=1e-4)
+ def test_repr(self):
+ assert repr(kornia.feature.BlobDoGSingle()) == "BlobDoGSingle(sigma1=1.0, sigma2=1.6)"
+ assert repr(kornia.feature.BlobDoGSingle(1.2, 2.0)) == "BlobDoGSingle(sigma1=1.2, sigma2=2.0)"
+
def _conv_grid(height: int, width: int) -> tuple[torch.Tensor, torch.Tensor]:
"""Return the float64 CPU ``(y, x)`` pixel-centre grids of a ``height x width`` image."""