Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Added tests for RandomResizedCrop

Jian S53fae3f5456e
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.py
index c330f109e..61cf99b76 100644
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@ -1,6 +1,4 @@
from typing import Callable, Tuple, Union, List, Optional, Dict, cast
-import random
-import math
import torch
import torch.nn as nn
@@ -464,7 +462,8 @@ class RandomCrop(AugmentationBase):
self.padding_mode = padding_mode
def __repr__(self) -> str:
- repr = f"(crop_size={self.size}, return_transform={self.return_transform})"
+ repr = f"RandomCrop(crop_size={self.size}, padding={self.padding}, fill={self.fill},\
+ pad_if_needed={self.pad_if_needed}, return_transform={self.return_transform})"
return self.__class__.__name__ + repr
@staticmethod
@@ -530,13 +529,15 @@ class RandomResizedCrop(AugmentationBase):
raise ValueError("Interpolation has not been implemented. Please set to None")
def __repr__(self) -> str:
- repr = f"(size={self.size}, resize_to={self.resize_to}, return_transform={self.return_transform})"
+ repr = f"RandomResizedCrop(size={self.size}, resize_to={self.scale}, resize_to={self.ratio}\
+ , return_transform={self.return_transform})"
return self.__class__.__name__ + repr
@staticmethod
def get_params(batch_size: int, input_size: Tuple[int, int], size: Tuple[int, int],
scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.)) -> Dict[str, torch.Tensor]:
- target_size = pd._random_crop_size_gen(size, scale, ratio)
+ target_size = pg._random_crop_size_gen(size, scale, ratio)
+ print(target_size)
# TODO: scale and aspect ratio were fixed for one batch for now. Need to be separated.
return pg._random_crop_gen(batch_size, input_size, target_size, resize_to=size)
diff --git a/kornia/augmentation/param_gen.py b/kornia/augmentation/param_gen.py
index ad28468ee..d52cffec3 100644
--- a/kornia/augmentation/param_gen.py
+++ b/kornia/augmentation/param_gen.py
@@ -1,4 +1,6 @@
from typing import Tuple, List, Union, Dict, Optional, cast
+import random
+import math
import numpy as np
import torch
@@ -350,8 +352,8 @@ def _random_crop_size_gen(size, scale, ratio):
w = int(round(math.sqrt(target_area * aspect_ratio)))
h = int(round(math.sqrt(target_area / aspect_ratio)))
- if 0 < w < size[0] and 0 < h < size[1]:
- return (w, h)
+ if 1 < w < size[0] and 1 < h < size[1]:
+ return (h, w)
# Fallback to center crop
in_ratio = float(size[0]) / float(size[1])
@@ -364,4 +366,4 @@ def _random_crop_size_gen(size, scale, ratio):
else: # whole image
w = size[0]
h = size[1]
- return (w, h)
+ return (h, w)
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index 9d7f9837e..d526cae68 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -8,7 +8,7 @@ from torch.autograd import gradcheck
import kornia
import kornia.testing as utils # test utils
from kornia.augmentation import RandomHorizontalFlip, RandomVerticalFlip, ColorJitter, \
- RandomRectangleErasing, RandomGrayscale, RandomRotation, RandomCrop
+ RandomRectangleErasing, RandomGrayscale, RandomRotation, RandomCrop, RandomResizedCrop
from kornia.augmentation.erasing import get_random_rectangles_params, erase_rectangles
from test.common import device
@@ -1266,17 +1266,23 @@ class TestRandomRotation:
class TestRandomCrop:
+ def smoke_test(self, device):
+ f = RandomCrop(size=(2, 3), padding=(0, 1), fill=10, pad_if_needed=False)
+ repr = "RandomCrop(crop_size=(2, 3), padding=(0, 1), fill=10, pad_if_needed=False,\
+ return_transform=False)"
+ assert str(f) == repr
+
def test_no_padding(self, device):
torch.manual_seed(0)
inp = torch.tensor([[[
[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]
- ]]])
+ ]]]).to(device)
expected = torch.tensor([[[
[3., 4., 5.],
[6., 7., 8.]
- ]]])
+ ]]]).to(device)
rc = RandomCrop(size=(2, 3), padding=None)
out = rc(inp)
@@ -1289,11 +1295,11 @@ class TestRandomCrop:
[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]
- ]]).repeat(batch_size, 1, 1, 1)
+ ]]).repeat(batch_size, 1, 1, 1).to(device)
expected = torch.tensor([[
[0., 1., 2.],
[3., 4., 5.],
- ]]).repeat(batch_size, 1, 1, 1)
+ ]]).repeat(batch_size, 1, 1, 1).to(device)
rc = RandomCrop(size=(2, 3), padding=None)
out = rc(inp)
@@ -1306,14 +1312,14 @@ class TestRandomCrop:
[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]
- ]]).repeat(batch_size, 1, 1, 1)
+ ]]).repeat(batch_size, 1, 1, 1).to(device)
expected = torch.tensor([[[
[0., 0., 0.],
[0., 1., 2.]
]], [[
[0., 0., 0.],
[1., 2., 0.]
- ]]])
+ ]]]).to(device)
rc = RandomCrop(size=(2, 3), padding=1)
out = rc(inp)
@@ -1326,14 +1332,14 @@ class TestRandomCrop:
[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]
- ]]).repeat(batch_size, 1, 1, 1)
+ ]]).repeat(batch_size, 1, 1, 1).to(device)
expected = torch.tensor([[[
[0., 1., 2.],
[3., 4., 5.]
]], [[
[1., 2., 10.],
[4., 5., 10.]
- ]]])
+ ]]]).to(device)
rc = RandomCrop(size=(2, 3), padding=(0, 1), fill=10)
out = rc(inp)
@@ -1346,14 +1352,14 @@ class TestRandomCrop:
[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]
- ]]).repeat(batch_size, 1, 1, 1)
+ ]]).repeat(batch_size, 1, 1, 1).to(device)
expected = torch.tensor([[[
[8., 8., 8.],
[8., 0., 1.]
]], [[
[8., 8., 8.],
[1., 2., 8.]
- ]]])
+ ]]]).to(device)
rc = RandomCrop(size=(2, 3), padding=(0, 1, 2, 3), fill=8)
out = rc(inp)
@@ -1364,12 +1370,74 @@ class TestRandomCrop:
batch_size = 2
inp = torch.tensor([[
[0., 1., 2.],
- ]]).repeat(batch_size, 1, 1, 1)
+ ]]).repeat(batch_size, 1, 1, 1).to(device)
expected = torch.tensor([[
[9., 9., 9.],
[0., 1., 2.]
- ]]).repeat(batch_size, 1, 1, 1)
+ ]]).repeat(batch_size, 1, 1, 1).to(device)
rc = RandomCrop(size=(2, 3), pad_if_needed=True, fill=9)
out = rc(inp)
assert_allclose(out, expected)
+
+
+class TestRandomResizedCrop:
+ def smoke_test(self, device):
+ f = RandomResizedCrop(size=(2, 3), scale=(1., 1.), ratio=(1.0, 1.0))
+ repr = "RandomResizedCrop(size=(2, 3), resize_to=(1., 1.), resize_to=(1., 1.)\
+ , return_transform=False)"
+ assert str(f) == repr
+
+ def test_no_resize(self, device):
+ torch.manual_seed(0)
+ inp = torch.tensor([[
+ [0., 1., 2.],
+ [3., 4., 5.],
+ [6., 7., 8.]
+ ]]).to(device)
+
+ expected = torch.tensor([[[
+ [3.0000, 3.5000, 4.0000],
+ [6.0000, 6.5000, 7.0000]
+ ]]]).to(device)
+ rrc = RandomResizedCrop(size=(2, 3), scale=(1., 1.), ratio=(1.0, 1.0))
+ # It will crop a size of (2, 2) from the aspect ratio implementation of torch
+ out = rrc(inp)
+ assert_allclose(out, expected)
+
+ def test_crop_scale_ratio(self, device):
+ torch.manual_seed(0)
+ inp = torch.tensor([[
+ [0., 1., 2.],
+ [3., 4., 5.],
+ [6., 7., 8.]
+ ]]).to(device)
+
+ expected = torch.tensor([[[
+ [3., 4., 5.],
+ [4.5, 5.5, 6.5],
+ [6., 7., 8.]
+ ]]]).to(device)
+ rrc = RandomResizedCrop(size=(3, 3), scale=(3., 3.), ratio=(2., 2.))
+ # It will crop a size of (2, 2) from the aspect ratio implementation of torch
+ out = rrc(inp)
+ assert_allclose(out, expected)
+
+ def test_crop_scale_ratio_batch(self, device):
+ torch.manual_seed(0)
+ batch_size = 2
+ inp = torch.tensor([[
+ [0., 1., 2.],
+ [3., 4., 5.],
+ [6., 7., 8.]
+ ]]).repeat(batch_size, 1, 1, 1).to(device)
+
+ expected = torch.tensor([[[
+ [0., 1., 2.],
+ [1.5, 2.5, 3.5],
+ [3., 4., 5.],
+ ]]]).repeat(batch_size, 1, 1, 1).to(device)
+ rrc = RandomResizedCrop(size=(3, 3), scale=(3., 3.), ratio=(2., 2.))
+ # It will crop a size of (2, 2) from the aspect ratio implementation of torch
+ out = rrc(inp)
+ assert_allclose(out, expected)