Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix params computation for `LongestMaxSize` and `SmallestMaxSize` (#2131)

João Gustavo A. Amorim05bb33e84dad
diff --git a/kornia/augmentation/random_generator/_2d/resize.py b/kornia/augmentation/random_generator/_2d/resize.py
index 40db27b41..cabe5d40b 100644
--- a/kornia/augmentation/random_generator/_2d/resize.py
+++ b/kornia/augmentation/random_generator/_2d/resize.py
@@ -66,25 +66,27 @@ class ResizeGenerator(RandomGeneratorBase):
if isinstance(self.output_size, int):
aspect_ratio = w / h
- self.output_size = _side_to_image_size(self.output_size, aspect_ratio, self.side)
+ output_size = _side_to_image_size(self.output_size, aspect_ratio, self.side)
+ else:
+ output_size = self.output_size
if not (
- len(self.output_size) == 2
- and isinstance(self.output_size[0], (int,))
- and isinstance(self.output_size[1], (int,))
- and self.output_size[0] > 0
- and self.output_size[1] > 0
+ len(output_size) == 2
+ and isinstance(output_size[0], (int,))
+ and isinstance(output_size[1], (int,))
+ and output_size[0] > 0
+ and output_size[1] > 0
):
- raise AssertionError(f"`resize_to` must be a tuple of 2 positive integers. Got {self.output_size}.")
+ raise AssertionError(f"`resize_to` must be a tuple of 2 positive integers. Got {output_size}.")
dst = bbox_generator(
tensor(0, device=_device, dtype=_dtype),
tensor(0, device=_device, dtype=_dtype),
- tensor(self.output_size[1], device=_device, dtype=_dtype),
- tensor(self.output_size[0], device=_device, dtype=_dtype),
+ tensor(output_size[1], device=_device, dtype=_dtype),
+ tensor(output_size[0], device=_device, dtype=_dtype),
).repeat(batch_size, 1, 1)
_input_size = tensor(input_size, device=_device, dtype=torch.long).expand(batch_size, -1)
- _output_size = tensor(self.output_size, device=_device, dtype=torch.long).expand(batch_size, -1)
+ _output_size = tensor(output_size, device=_device, dtype=torch.long).expand(batch_size, -1)
return dict(src=src, dst=dst, input_size=_input_size, output_size=_output_size)
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index ab8119896..136fbf1f7 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -3727,18 +3727,30 @@ class TestResize:
class TestSmallestMaxSize:
def test_smoke(self, device, dtype):
- img = torch.rand(1, 1, 4, 6, device=device, dtype=dtype)
+ img_A = torch.rand(1, 1, 4, 6, device=device, dtype=dtype)
+ img_B = torch.rand(1, 1, 9, 6, device=device, dtype=dtype)
aug = SmallestMaxSize(max_size=2)
- out = aug(img)
- assert out.shape == (1, 1, 2, 3)
+
+ assert aug(img_A).shape == (1, 1, 2, 3)
+ assert aug(img_B).shape == (1, 1, 3, 2)
+
+ aug = SmallestMaxSize(max_size=2)
+ assert aug(img_B).shape == (1, 1, 3, 2)
+ assert aug(img_A).shape == (1, 1, 2, 3)
class TestLongestMaxSize:
def test_smoke(self, device, dtype):
- img = torch.rand(1, 1, 4, 6, device=device, dtype=dtype)
+ img_A = torch.rand(1, 1, 4, 6, device=device, dtype=dtype)
+ img_B = torch.rand(1, 1, 8, 6, device=device, dtype=dtype)
aug = LongestMaxSize(max_size=3)
- out = aug(img)
- assert out.shape == (1, 1, 2, 3)
+
+ assert aug(img_A).shape == (1, 1, 2, 3)
+ assert aug(img_B).shape == (1, 1, 3, 2)
+
+ aug = LongestMaxSize(max_size=3)
+ assert aug(img_B).shape == (1, 1, 3, 2)
+ assert aug(img_A).shape == (1, 1, 2, 3)
class TestRandomPosterize: