Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

make apply group functions private

edgarriba056f443d8b7e
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.py
index 22bb5e42a..acb884e7d 100644
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@ -89,7 +89,7 @@ class RandomHorizontalFlip(AugmentationBase):
"""
def __init__(self, p: float = 0.5, return_transform: bool = False) -> None:
- super(RandomHorizontalFlip, self).__init__(F.apply_hflip, return_transform)
+ super(RandomHorizontalFlip, self).__init__(F._apply_hflip, return_transform)
self.p: float = p
@staticmethod
@@ -134,7 +134,7 @@ class RandomVerticalFlip(AugmentationBase):
"""
def __init__(self, p: float = 0.5, return_transform: bool = False) -> None:
- super(RandomVerticalFlip, self).__init__(F.apply_vflip, return_transform)
+ super(RandomVerticalFlip, self).__init__(F._apply_vflip, return_transform)
self.p: float = p
@staticmethod
@@ -166,7 +166,7 @@ class ColorJitter(AugmentationBase):
def __init__(self, brightness: FloatUnionType = 0., contrast: FloatUnionType = 0.,
saturation: FloatUnionType = 0., hue: FloatUnionType = 0., return_transform: bool = False) -> None:
- super(ColorJitter, self).__init__(F.apply_color_jitter, return_transform)
+ super(ColorJitter, self).__init__(F._apply_color_jitter, return_transform)
self.brightness: FloatUnionType = brightness
self.contrast: FloatUnionType = contrast
self.saturation: FloatUnionType = saturation
@@ -201,7 +201,7 @@ class RandomGrayscale(AugmentationBase):
"""
def __init__(self, p: float = 0.5, return_transform: bool = False) -> None:
- super(RandomGrayscale, self).__init__(F.apply_grayscale, return_transform)
+ super(RandomGrayscale, self).__init__(F._apply_grayscale, return_transform)
self.p = p
def __repr__(self) -> str:
@@ -266,7 +266,7 @@ class RandomPerspective(AugmentationBase):
"""
def __init__(self, distortion_scale: float = 0.5, p: float = 0.5, return_transform: bool = False) -> None:
- super(RandomPerspective, self).__init__(F.apply_perspective, return_transform)
+ super(RandomPerspective, self).__init__(F._apply_perspective, return_transform)
self.p: float = p
self.distortion_scale: float = distortion_scale
self.return_transform: bool = return_transform
@@ -322,7 +322,7 @@ class RandomAffine(AugmentationBase):
scale: Optional[TupleFloat] = None,
shear: Optional[UnionFloat] = None,
return_transform: bool = False) -> None:
- super(RandomAffine, self).__init__(F.apply_affine, return_transform)
+ super(RandomAffine, self).__init__(F._apply_affine, return_transform)
self.degrees = degrees
self.translate = translate
self.scale = scale
@@ -356,7 +356,7 @@ class CenterCrop(AugmentationBase):
"""
def __init__(self, size: Union[int, Tuple[int, int]], return_transform: bool = False) -> None:
- super(CenterCrop, self).__init__(F.apply_center_crop, return_transform)
+ super(CenterCrop, self).__init__(F._apply_center_crop, return_transform)
self.size = size
self.return_transform = return_transform
diff --git a/kornia/augmentation/functional.py b/kornia/augmentation/functional.py
index 42eb63dbe..b2903a5f3 100644
--- a/kornia/augmentation/functional.py
+++ b/kornia/augmentation/functional.py
@@ -22,7 +22,7 @@ def random_hflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = F
r"""Generate params and apply operation on input tensor.
See :func:`~kornia.augmentation.param_gen._random_prob_gen` for details.
- See :func:`~kornia.augmentation.functional.apply_hflip` for details.
+ See :func:`~kornia.augmentation.functional._apply_hflip` for details.
"""
if isinstance(input, tuple):
@@ -30,14 +30,14 @@ def random_hflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = F
else:
batch_size = input.shape[0] if len(input.shape) == 4 else 1
params = pg._random_prob_gen(batch_size, p=p)
- return apply_hflip(input, params, return_transform)
+ return _apply_hflip(input, params, return_transform)
def random_vflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = False) -> UnionType:
r"""Generate params and apply operation on input tensor.
See :func:`~kornia.augmentation.param_gen._random_prob_gen` for details.
- See :func:`~kornia.augmentation.functional.apply_vflip` for details.
+ See :func:`~kornia.augmentation.functional._apply_vflip` for details.
"""
if isinstance(input, tuple):
@@ -45,7 +45,7 @@ def random_vflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = F
else:
batch_size = input.shape[0] if len(input.shape) == 4 else 1
params = pg._random_prob_gen(batch_size, p=p)
- return apply_vflip(input, params, return_transform)
+ return _apply_vflip(input, params, return_transform)
def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0.,
@@ -54,7 +54,7 @@ def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0.,
r"""Generate params and apply operation on input tensor.
See :func:`~kornia.augmentation.param_gen._random_color_jitter_gen` for details.
- See :func:`~kornia.augmentation.functional.apply_color_jitter` for details.
+ See :func:`~kornia.augmentation.functional._apply_color_jitter` for details.
"""
if isinstance(input, tuple):
@@ -62,14 +62,14 @@ def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0.,
else:
batch_size = input.shape[0] if len(input.shape) == 4 else 1
params = pg._random_color_jitter_gen(batch_size, brightness, contrast, saturation, hue)
- return apply_color_jitter(input, params, return_transform)
+ return _apply_color_jitter(input, params, return_transform)
def random_grayscale(input: torch.Tensor, p: float = 0.5, return_transform: bool = False):
r"""Generate params and apply operation on input tensor.
See :func:`~kornia.augmentation.param_gen._random_prob_gen` for details.
- See :func:`~kornia.augmentation.functional.apply_grayscale` for details.
+ See :func:`~kornia.augmentation.functional._apply_grayscale` for details.
"""
if isinstance(input, tuple):
@@ -77,7 +77,7 @@ def random_grayscale(input: torch.Tensor, p: float = 0.5, return_transform: bool
else:
batch_size = input.shape[0] if len(input.shape) == 4 else 1
params = pg._random_prob_gen(batch_size, p=p)
- return apply_grayscale(input, params, return_transform)
+ return _apply_grayscale(input, params, return_transform)
def random_perspective(input: torch.Tensor,
@@ -87,13 +87,13 @@ def random_perspective(input: torch.Tensor,
r"""Performs Perspective transformation of the given torch.Tensor randomly with a given probability.
See :func:`~kornia.augmentation.param_gen._random_perspective_gen` for details.
- See :func:`~kornia.augmentation.functional.apply_perspective` for details.
+ See :func:`~kornia.augmentation.functional._apply_perspective` for details.
"""
batch_size, _, height, width = input.shape
params: Dict[str, torch.Tensor] = pg._random_perspective_gen(
batch_size, height, width, p, distortion_scale)
- return apply_perspective(input, params, return_transform)
+ return _apply_perspective(input, params, return_transform)
def random_affine(input: torch.Tensor,
@@ -105,16 +105,16 @@ def random_affine(input: torch.Tensor,
r"""Random affine transformation of the image keeping center invariant
See :func:`~kornia.augmentation.param_gen._random_affine_gen` for details.
- See :func:`~kornia.augmentation.functional.apply_affine` for details.
+ See :func:`~kornia.augmentation.functional._apply_affine` for details.
"""
batch_size, _, height, width = input.shape
params: Dict[str, torch.Tensor] = pg._random_affine_gen(
batch_size, height, width, degrees, translate, scale, shear)
- return apply_affine(input, params, return_transform)
+ return _apply_affine(input, params, return_transform)
-def apply_hflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
+def _apply_hflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
r"""Apply Horizontally flip on a tensor image or a batch of tensor images with given random parameters.
Input should be a tensor of shape (H, W), (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.
@@ -170,7 +170,7 @@ def apply_hflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_tra
return flipped
-def apply_vflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
+def _apply_vflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
r"""Apply vertically flip on a tensor image or a batch of tensor images with given random parameters.
Input should be a tensor of shape (H, W), (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.
@@ -225,8 +225,8 @@ def apply_vflip(input: torch.Tensor, params: Dict[str, torch.Tensor], return_tra
return flipped
-def apply_color_jitter(input: torch.Tensor, params: Dict[str, torch.Tensor],
- return_transform: bool = False) -> UnionType:
+def _apply_color_jitter(input: torch.Tensor, params: Dict[str, torch.Tensor],
+ return_transform: bool = False) -> UnionType:
r"""Apply Color Jitter on a tensor image or a batch of tensor images with given random parameters.
Input should be a tensor of shape (H, W), (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.
@@ -285,7 +285,7 @@ def apply_color_jitter(input: torch.Tensor, params: Dict[str, torch.Tensor],
return jittered
-def apply_grayscale(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
+def _apply_grayscale(input: torch.Tensor, params: Dict[str, torch.Tensor], return_transform: bool = False) -> UnionType:
r"""Apply Gray Scale on a tensor image or a batch of tensor images with given random parameters.
Input should be a tensor of shape (3, H, W) or a batch of tensors :math:`(*, 3, H, W)`.
@@ -371,9 +371,9 @@ def random_rectangle_erase(
return images
-def apply_perspective(input: torch.Tensor,
- params: Dict[str, torch.Tensor],
- return_transform: bool = False) -> UnionType:
+def _apply_perspective(input: torch.Tensor,
+ params: Dict[str, torch.Tensor],
+ return_transform: bool = False) -> UnionType:
r"""Perform perspective transform of the given torch.Tensor or batch of tensors.
Args:
@@ -419,9 +419,9 @@ def apply_perspective(input: torch.Tensor,
return out_data.view_as(input)
-def apply_affine(input: torch.Tensor,
- params: Dict[str, torch.Tensor],
- return_transform: bool = False) -> UnionType:
+def _apply_affine(input: torch.Tensor,
+ params: Dict[str, torch.Tensor],
+ return_transform: bool = False) -> UnionType:
if not torch.is_tensor(input):
raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}")
r"""Random affine transformation of the image keeping center invariant
@@ -467,9 +467,9 @@ def apply_affine(input: torch.Tensor,
return out_data.view_as(input)
-def apply_center_crop(input: torch.Tensor,
- params: Dict[str, torch.Tensor],
- return_transform: bool = False) -> UnionType:
+def _apply_center_crop(input: torch.Tensor,
+ params: Dict[str, torch.Tensor],
+ return_transform: bool = False) -> UnionType:
if not torch.is_tensor(input):
raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}")
diff --git a/test/augmentation/test_functional.py b/test/augmentation/test_functional.py
index 31f793fa4..505963c83 100644
--- a/test/augmentation/test_functional.py
+++ b/test/augmentation/test_functional.py
@@ -35,12 +35,12 @@ class TestHorizontalFlipFn:
[0., 1., 0.],
[0., 0., 1.]]) # 3 x 3
- assert (F.apply_hflip(input, params=flip_param_0, return_transform=True)[0] == input).all()
- assert (F.apply_hflip(input, params=flip_param_0, return_transform=True)[1] == identity).all()
- assert (F.apply_hflip(input, params=flip_param_1, return_transform=True)[0] == expected).all()
- assert (F.apply_hflip(input, params=flip_param_1, return_transform=True)[1] == expected_transform).all()
- assert (F.apply_hflip(input, params=flip_param_0, return_transform=False) == input).all()
- assert (F.apply_hflip(input, params=flip_param_1, return_transform=False) == expected).all()
+ assert (F._apply_hflip(input, params=flip_param_0, return_transform=True)[0] == input).all()
+ assert (F._apply_hflip(input, params=flip_param_0, return_transform=True)[1] == identity).all()
+ assert (F._apply_hflip(input, params=flip_param_1, return_transform=True)[0] == expected).all()
+ assert (F._apply_hflip(input, params=flip_param_1, return_transform=True)[1] == expected_transform).all()
+ assert (F._apply_hflip(input, params=flip_param_0, return_transform=False) == input).all()
+ assert (F._apply_hflip(input, params=flip_param_1, return_transform=False) == expected).all()
def test_batch_random_hflip(self):
batch_size = 5
@@ -68,10 +68,10 @@ class TestHorizontalFlipFn:
expected_transform = expected_transform.repeat(batch_size, 1, 1) # 5 x 3 x 3
identity = identity.repeat(batch_size, 1, 1) # 5 x 3 x 3
- assert (F.apply_hflip(input, params=flip_param_0, return_transform=True)[0] == input).all()
- assert (F.apply_hflip(input, params=flip_param_0, return_transform=True)[1] == identity).all()
- assert (F.apply_hflip(input, params=flip_param_1, return_transform=True)[0] == expected).all()
- assert (F.apply_hflip(input, params=flip_param_1, return_transform=True)[1] == expected_transform).all()
+ assert (F._apply_hflip(input, params=flip_param_0, return_transform=True)[0] == input).all()
+ assert (F._apply_hflip(input, params=flip_param_0, return_transform=True)[1] == identity).all()
+ assert (F._apply_hflip(input, params=flip_param_1, return_transform=True)[0] == expected).all()
+ assert (F._apply_hflip(input, params=flip_param_1, return_transform=True)[1] == expected_transform).all()
class TestVerticalFlipFn:
@@ -98,12 +98,12 @@ class TestVerticalFlipFn:
[0., 1., 0.],
[0., 0., 1.]]) # 3 x 3
- assert (F.apply_vflip(input, params=flip_param_0, return_transform=True)[0] == input).all()
- assert (F.apply_vflip(input, params=flip_param_0, return_transform=True)[1] == identity).all()
- assert (F.apply_vflip(input, params=flip_param_1, return_transform=True)[0] == expected).all()
- assert (F.apply_vflip(input, params=flip_param_1, return_transform=True)[1] == expected_transform).all()
- assert (F.apply_vflip(input, params=flip_param_0, return_transform=False) == input).all()
- assert (F.apply_vflip(input, params=flip_param_1, return_transform=False) == expected).all()
+ assert (F._apply_vflip(input, params=flip_param_0, return_transform=True)[0] == input).all()
+ assert (F._apply_vflip(input, params=flip_param_0, return_transform=True)[1] == identity).all()
+ assert (F._apply_vflip(input, params=flip_param_1, return_transform=True)[0] == expected).all()
+ assert (F._apply_vflip(input, params=flip_param_1, return_transform=True)[1] == expected_transform).all()
+ assert (F._apply_vflip(input, params=flip_param_0, return_transform=False) == input).all()
+ assert (F._apply_vflip(input, params=flip_param_1, return_transform=False) == expected).all()
def test_batch_random_vflip(self, device):
batch_size = 5
@@ -132,10 +132,10 @@ class TestVerticalFlipFn:
expected_transform = expected_transform.repeat(batch_size, 1, 1) # 5 x 3 x 3
identity = identity.repeat(batch_size, 1, 1) # 5 x 3 x 3
- assert (F.apply_vflip(input, params=flip_param_0, return_transform=True)[0] == input).all()
- assert (F.apply_vflip(input, params=flip_param_0, return_transform=True)[1] == identity).all()
- assert (F.apply_vflip(input, params=flip_param_1, return_transform=True)[0] == expected).all()
- assert (F.apply_vflip(input, params=flip_param_1, return_transform=True)[1] == expected_transform).all()
+ assert (F._apply_vflip(input, params=flip_param_0, return_transform=True)[0] == input).all()
+ assert (F._apply_vflip(input, params=flip_param_0, return_transform=True)[1] == identity).all()
+ assert (F._apply_vflip(input, params=flip_param_1, return_transform=True)[0] == expected).all()
+ assert (F._apply_vflip(input, params=flip_param_1, return_transform=True)[1] == expected_transform).all()
class TestColorJitter:
@@ -155,10 +155,10 @@ class TestColorJitter:
expected_transform = torch.eye(3).unsqueeze(0) # 3 x 3
- assert_allclose(F.apply_color_jitter(input, jitter_param), expected, atol=1e-4, rtol=1e-5)
- assert_allclose(F.apply_color_jitter(
+ assert_allclose(F._apply_color_jitter(input, jitter_param), expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(F._apply_color_jitter(
input, jitter_param, return_transform=True)[0], expected, atol=1e-4, rtol=1e-5)
- assert_allclose(F.apply_color_jitter(input, jitter_param, return_transform=True)[1], expected_transform)
+ assert_allclose(F._apply_color_jitter(input, jitter_param, return_transform=True)[1], expected_transform)
def test_color_jitter_batch(self):
batch_size = 2
@@ -174,10 +174,10 @@ class TestColorJitter:
expected_transform = torch.eye(3).unsqueeze(0).expand((batch_size, 3, 3)) # 2 x 3 x 3
- assert_allclose(F.apply_color_jitter(input, jitter_param), expected, atol=1e-4, rtol=1e-5)
- assert_allclose(F.apply_color_jitter(
+ assert_allclose(F._apply_color_jitter(input, jitter_param), expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(F._apply_color_jitter(
input, jitter_param, return_transform=True)[0], expected, atol=1e-4, rtol=1e-5)
- assert_allclose(F.apply_color_jitter(input, jitter_param, return_transform=True)[1], expected_transform)
+ assert_allclose(F._apply_color_jitter(input, jitter_param, return_transform=True)[1], expected_transform)
def test_random_brightness(self):
torch.manual_seed(42)
@@ -218,7 +218,7 @@ class TestColorJitter:
[0.7660, 0.6660, 0.5660],
[0.8660, 0.9660, 1.0000]]]]) # 1 x 1 x 3 x 3
- assert_allclose(F.apply_color_jitter(input, jitter_param), expected)
+ assert_allclose(F._apply_color_jitter(input, jitter_param), expected)
def test_random_contrast(self):
torch.manual_seed(42)
@@ -259,7 +259,7 @@ class TestColorJitter:
[0.7102, 0.5919, 0.4735],
[0.8286, 0.9470, 1.0000]]]])
- assert_allclose(F.apply_color_jitter(input, jitter_param), expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(F._apply_color_jitter(input, jitter_param), expected, atol=1e-4, rtol=1e-5)
def test_random_saturation(self):
torch.manual_seed(42)
@@ -308,7 +308,7 @@ class TestColorJitter:
[9.0000e-01, 2.7651e-01, 1.7651e-01],
[8.0000e-01, 3.5302e-01, 4.4127e-01]]]])
- assert_allclose(F.apply_color_jitter(input, jitter_param), expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(F._apply_color_jitter(input, jitter_param), expected, atol=1e-4, rtol=1e-5)
def test_random_hue(self):
torch.manual_seed(42)
@@ -355,7 +355,7 @@ class TestColorJitter:
[0.9000, 0.3000, 0.2000],
[0.8000, 0.3730, 0.4692]]]])
- assert_allclose(F.apply_color_jitter(input, jitter_param), expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(F._apply_color_jitter(input, jitter_param), expected, atol=1e-4, rtol=1e-5)
class TestRandomGrayscale:
@@ -369,8 +369,8 @@ class TestRandomGrayscale:
expected_transform = torch.eye(3).unsqueeze(0) # 3 x 3
- assert_allclose(F.apply_grayscale(input, grayscale_params_0, return_transform=True)[1], expected_transform)
- assert_allclose(F.apply_grayscale(input, grayscale_params_1, return_transform=True)[1], expected_transform)
+ assert_allclose(F._apply_grayscale(input, grayscale_params_0, return_transform=True)[1], expected_transform)
+ assert_allclose(F._apply_grayscale(input, grayscale_params_1, return_transform=True)[1], expected_transform)
def test_opencv_true(self, device):
grayscale_params = {'batch_prob': torch.tensor([True])}
@@ -412,7 +412,7 @@ class TestRandomGrayscale:
[0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805]]])
expected = expected.to(device)
- assert_allclose(F.apply_grayscale(data, grayscale_params), expected)
+ assert_allclose(F._apply_grayscale(data, grayscale_params), expected)
def test_opencv_false(self, device):
grayscale_params = {'batch_prob': torch.tensor([False])}
@@ -437,7 +437,7 @@ class TestRandomGrayscale:
expected = data
- assert_allclose(F.apply_grayscale(data, grayscale_params), expected)
+ assert_allclose(F._apply_grayscale(data, grayscale_params), expected)
def test_opencv_true_batch(self, device):
batch_size = 4
@@ -483,7 +483,7 @@ class TestRandomGrayscale:
expected = expected.to(device)
expected = expected.unsqueeze(0).repeat(batch_size, 1, 1, 1)
- assert_allclose(F.apply_grayscale(data, grayscale_params), expected)
+ assert_allclose(F._apply_grayscale(data, grayscale_params), expected)
def test_opencv_false_batch(self, device):
batch_size = 4
@@ -510,4 +510,4 @@ class TestRandomGrayscale:
expected = data
- assert_allclose(F.apply_grayscale(data, grayscale_params), expected)
+ assert_allclose(F._apply_grayscale(data, grayscale_params), expected)
diff --git a/test/augmentation/test_perspective.py b/test/augmentation/test_perspective.py
index 17e72c228..e29bc6ff7 100644
--- a/test/augmentation/test_perspective.py
+++ b/test/augmentation/test_perspective.py
@@ -19,7 +19,7 @@ class TestPerspective:
end_points = torch.rand(1, 4, 2).to(device)
params = dict(batch_prob=batch_prob, start_points=start_points, end_points=end_points)
- out_data = F.apply_perspective(x_data, params, return_transform=False)
+ out_data = F._apply_perspective(x_data, params, return_transform=False)
assert out_data.shape == x_data.shape
@@ -30,7 +30,7 @@ class TestPerspective:
end_points = torch.rand(1, 4, 2).to(device)
params = dict(batch_prob=batch_prob, start_points=start_points, end_points=end_points)
- out_data = F.apply_perspective(x_data, params, return_transform=True)
+ out_data = F._apply_perspective(x_data, params, return_transform=True)
assert isinstance(out_data, tuple)
assert len(out_data) == 2
@@ -50,7 +50,7 @@ class TestPerspective:
end_points = utils.tensor_to_gradcheck_var(end_points) # to var
params = dict(batch_prob=batch_prob, start_points=start_points, end_points=end_points)
- assert gradcheck(F.apply_perspective, (input, params,), raise_exception=True)
+ assert gradcheck(F._apply_perspective, (input, params,), raise_exception=True)
class TestRandomPerspective:
@@ -65,6 +65,14 @@ class TestRandomPerspective:
assert out_perspective.shape == x_data.shape
+ def test_smoke_no_transform_batch(self, device):
+ x_data = torch.rand(2, 2, 8, 9).to(device)
+
+ out_perspective = kornia.augmentation.functional.random_perspective(
+ x_data, 0.5, 0.5, return_transform=False)
+
+ assert out_perspective.shape == x_data.shape
+
def test_smoke_transform(self, device):
x_data = torch.rand(1, 2, 4, 5).to(device)