π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): name size in the int-size errors of RandomCrop and RandomResizedCrop (#4512)
Dipak Chaudhari6eaaf3b657d9
diff --git a/changelog.d/4512.fixed.md b/changelog.d/4512.fixed.mdnew file mode 100644index 000000000..106dc6699--- /dev/null+++ b/changelog.d/4512.fixed.md@@ -0,0 +1,4 @@+`RandomCrop(4)` and `RandomResizedCrop(4)` raise errors that name `size`, the `(height, width)` form it needs and+the value passed, instead of an `AssertionError` about a `torch.Tensor` shape and a bare+`TypeError: object of type 'int' has no len()`. The exception types are unchanged, and both classes still reject+an `int`. (#4417)diff --git a/kornia/augmentation/random_generator/_2d/crop.py b/kornia/augmentation/random_generator/_2d/crop.pyindex aac0aa311..b439fac5b 100644--- a/kornia/augmentation/random_generator/_2d/crop.py+++ b/kornia/augmentation/random_generator/_2d/crop.py@@ -83,6 +83,10 @@ class CropGenerator(RandomGeneratorBase):input_size = (batch_shape[-2], batch_shape[-1])if not isinstance(self.size, torch.Tensor):size = torch.tensor(self.size, device=_device, dtype=_dtype).repeat(batch_size, 1)+ if size.shape != torch.Size([batch_size, 2]):+ raise AssertionError(+ f"`size` must be a (height, width) pair of integers or a (B, 2) tensor. Got {self.size!r}."+ )else:size = self.size.to(device=_device, dtype=_dtype)if size.shape != torch.Size([batch_size, 2]):@@ -200,6 +204,11 @@ class ResizedCropGenerator(CropGenerator):scale: Union[torch.Tensor, Tuple[float, float]],ratio: Union[torch.Tensor, Tuple[float, float]],) -> None:+ if not isinstance(output_size, (tuple, list)):+ raise TypeError(+ "`output_size` (`size` on RandomResizedCrop) must be a (height, width) tuple of 2 positive "+ f"integers. Got {output_size!r} of type {type(output_size).__name__}."+ )if not (len(output_size) == 2and isinstance(output_size[0], (int,))diff --git a/tests/augmentation/test_random_generator.py b/tests/augmentation/test_random_generator.pyindex 583f9cc46..c9030bd4a 100644--- a/tests/augmentation/test_random_generator.py+++ b/tests/augmentation/test_random_generator.py@@ -942,6 +942,31 @@ class TestRandomCropGen(RandomGeneratorBaseTests):assert_close(res["dst"], expected["dst"])+class TestCropIntSizeMessages:+ # #4417: `RandomCrop(4)` and `RandomResizedCrop(4)` are rejected (whether to accept an int is the window+ # decision), but the errors used to be about a torch.Tensor shape and `len()` of an int. They now name the+ # argument, the expected form and the value passed.+ def test_random_crop_int_size_names_the_argument(self, device, dtype):+ from kornia.augmentation import RandomCrop++ x = torch.rand(2, 1, 8, 8, device=device, dtype=dtype)+ with pytest.raises(AssertionError, match=r"`size` must be a \(height, width\) pair of integers.*Got 4\."):+ RandomCrop(4)(x)++ def test_random_crop_tensor_size_message_is_unchanged(self, device, dtype):+ gen = CropGenerator(torch.tensor([[4, 4, 4]], device=device, dtype=dtype))+ with pytest.raises(AssertionError, match=r"If `size` is a torch.Tensor, it must be shaped as \(B, 2\)"):+ gen(torch.Size([1, 1, 8, 8]))++ def test_random_resized_crop_int_size_names_the_argument(self):+ from kornia.augmentation import RandomResizedCrop++ with pytest.raises(+ TypeError, match=r"`size` on RandomResizedCrop\) must be a \(height, width\).*Got 4 of type int"+ ):+ RandomResizedCrop(4)++class TestRandomCropSizeGen(RandomGeneratorBaseTests):@pytest.mark.parametrize("batch_size", [0, 1, 8])@pytest.mark.parametrize("size", [(200, 200)])