Repository: kornia/kornia

Issue:
RandomResizedCrop - Kornia vs. Torchvision
Thanks for the code.

I'm using kornia 0.4.0 and found major difference between torchvision and kornia while using _randomresizedcrop_ in _augmentation_. 

In case of torchvision the window to crop is **computed with a input size** of the image but in case of kornia is a  **output size** of the image.

Just wanted to report because I have no idea whether it is intended to do so or not. 



Discussion:
Thank you for reporting. We were expecting the similar outcome as TV, but we might not test enough on the random generators. We might need to come up a proper method to test the distribution from any random gens, @edgarriba, @anguelos.

@deepneuralnetworks, Are you willing to send a PR to fix this issue?

Best,
Jian


Resolving patch:
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.py
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@ -872,7 +872,8 @@ def generate_parameters(self, batch_shape: torch.Size) -> Dict[str, torch.Tensor
         scale = torch.as_tensor(self.scale, device=self._device, dtype=self._dtype)
         ratio = torch.as_tensor(self.ratio, device=self._device, dtype=self._dtype)
         target_size: torch.Tensor = rg.random_crop_size_generator(
-            batch_shape[0], self.size, scale, ratio, self.same_on_batch, self.device, self.dtype)['size']
+            batch_shape[0], (batch_shape[-2], batch_shape[-1]), scale, ratio, self.same_on_batch,
+            self.device, self.dtype)['size']
         return rg.random_crop_generator(batch_shape[0], (batch_shape[-2], batch_shape[-1]), target_size,
                                         resize_to=self.size, same_on_batch=self.same_on_batch,
                                         device=self.device, dtype=self.dtype)
diff --git a/kornia/augmentation/random_generator/random_generator.py b/kornia/augmentation/random_generator/random_generator.py
--- a/kornia/augmentation/random_generator/random_generator.py
+++ b/kornia/augmentation/random_generator/random_generator.py
@@ -490,7 +490,7 @@ def random_crop_size_generator(
     w = torch.sqrt(area * aspect_ratio).round().floor()
     h = torch.sqrt(area / aspect_ratio).round().floor()
     # Element-wise w, h condition
-    cond = ((0 < w) * (w < size[1]) * (0 < h) * (h < size[0])).int()
+    cond = ((0 < w) * (w < size[0]) * (0 < h) * (h < size[1])).int()
 
     # torch.argmax is not reproducible accross devices: https://github.com/pytorch/pytorch/issues/17738
     # Here, we will select the first occurance of the duplicated elements.


Test patch:
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -2123,8 +2123,8 @@ def test_no_resize(self, device, dtype):
         ]], device=device, dtype=dtype)
 
         expected = torch.tensor(
-            [[[[4.0000, 4.5000, 5.0000],
-               [7.0000, 7.5000, 8.0000]]]], device=device, dtype=dtype)
+            [[[[0.0000, 1.0000, 2.0000],
+               [6.0000, 7.0000, 8.0000]]]], device=device, dtype=dtype)
 
         rrc = RandomResizedCrop(
             size=(2, 3), scale=(1., 1.), ratio=(1.0, 1.0))