Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Refactored typings, imports, exports

Jian Sc847b7e26401
diff --git a/kornia/augmentation/__init__.py b/kornia/augmentation/__init__.py
index 182628485..b0f93b3b4 100644
--- a/kornia/augmentation/__init__.py
+++ b/kornia/augmentation/__init__.py
@@ -1,15 +1,20 @@
from kornia.augmentation.augmentations import *
+from kornia.augmentation.functional import *
from kornia.augmentation.random_erasing import *
__all__ = [
"random_hflip",
+ "apply_hflip",
"RandomHorizontalFlip",
"random_vflip",
+ "apply_vflip",
"RandomVerticalFlip",
"color_jitter",
+ "apply_color_jitter",
"ColorJitter",
"random_rectangle_erase",
"RandomRectangleErasing",
"random_grayscale",
+ "apply_grayscale",
"RandomGrayscale",
]
diff --git a/kornia/augmentation/augmentations.py b/kornia/augmentation/augmentations.py
index db6530ee3..d32109b4c 100644
--- a/kornia/augmentation/augmentations.py
+++ b/kornia/augmentation/augmentations.py
@@ -1,16 +1,16 @@
-from typing import Callable, Tuple, Union, cast
+from typing import Callable, Tuple, Union, List, Optional, Dict, cast
import torch
import torch.nn as nn
-from kornia.augmentation.functional import *
+import kornia.augmentation.functional as F
UnionType = Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]
FloatUnionType = Union[torch.Tensor, float, Tuple[float, float], List[float]]
class AugmentationBase(nn.Module):
- def __init__(self, return_transform: bool = False):
+ def __init__(self, return_transform: bool = False) -> None:
super(AugmentationBase, self).__init__()
self.return_transform = return_transform
@@ -38,10 +38,11 @@ class RandomFlip(AugmentationBase):
return self.__class__.__name__ + repr
@staticmethod
- def get_params(batch_size: int, p: float = 0.5):
- return get_random_p_params(batch_size, p)
+ def get_params(batch_size: int, p: float = 0.5) -> Optional[Dict[str, torch.Tensor]]:
+ return F._get_random_p_params(batch_size, p)
- def forward_flip(self, input: UnionType, flip_func: Callable, params: dict = None) -> UnionType: # type: ignore
+ def forward_flip(self, input: UnionType, flip_func: Callable,
+ params: Optional[Dict[str, torch.Tensor]]= None) -> UnionType: # type: ignore
if params is None:
if isinstance(input, tuple):
@@ -111,8 +112,8 @@ class RandomHorizontalFlip(RandomFlip):
def __init__(self, p: float = 0.5, return_transform: bool = False) -> None:
super(RandomHorizontalFlip, self).__init__(p=p, return_transform=return_transform)
- def forward(self, input: UnionType, params: dict = None) -> UnionType: # type: ignore
- return self.forward_flip(input, apply_hflip, params)
+ def forward(self, input: UnionType, params: Optional[Dict[str, torch.Tensor]] = None) -> UnionType: # type: ignore
+ return self.forward_flip(input, F.apply_hflip, params)
class RandomVerticalFlip(RandomFlip):
@@ -149,8 +150,8 @@ class RandomVerticalFlip(RandomFlip):
def __init__(self, p: float = 0.5, return_transform: bool = False) -> None:
super(RandomVerticalFlip, self).__init__(p=p, return_transform=return_transform)
- def forward(self, input: UnionType, params: dict = None) -> UnionType: # type: ignore
- return self.forward_flip(input, apply_vflip, params)
+ def forward(self, input: UnionType, params: Optional[Dict[str, torch.Tensor]] = None) -> UnionType: # type: ignore
+ return self.forward_flip(input, F.apply_vflip, params)
class ColorJitter(AugmentationBase):
@@ -182,10 +183,10 @@ class ColorJitter(AugmentationBase):
@staticmethod
def get_params(batch_size: int, brightness: FloatUnionType = 0., contrast: FloatUnionType = 0.,
- saturation: FloatUnionType = 0., hue: FloatUnionType = 0.) -> dict:
- return get_color_jitter_params(batch_size, brightness, contrast, saturation, hue)
+ saturation: FloatUnionType = 0., hue: FloatUnionType = 0.) -> Dict[str, torch.Tensor]:
+ return F._get_color_jitter_params(batch_size, brightness, contrast, saturation, hue)
- def forward(self, input: UnionType, params: dict = None) -> UnionType: # type: ignore
+ def forward(self, input: UnionType, params: Optional[Dict[str, torch.Tensor]]= None) -> UnionType: # type: ignore
if params is None:
if isinstance(input, tuple):
batch_size = input[0].shape[0] if len(input[0].shape) == 4 else 1
@@ -197,14 +198,14 @@ class ColorJitter(AugmentationBase):
jittered: torch.Tensor = cast(
torch.Tensor,
- apply_color_jitter(
+ F.apply_color_jitter(
input[0],
params=params,
return_transform=False))
return jittered, input[1]
- return apply_color_jitter(input, params, self.return_transform)
+ return F.apply_color_jitter(input, params, self.return_transform)
class RandomGrayscale(AugmentationBase):
@@ -226,10 +227,10 @@ class RandomGrayscale(AugmentationBase):
return self.__class__.__name__ + repr
@staticmethod
- def get_params(batch_size: int, p: float = .5) -> dict:
- return get_random_p_params(batch_size, p)
+ def get_params(batch_size: int, p: float = .5) -> Dict[str, torch.Tensor]:
+ return F._get_random_p_params(batch_size, p)
- def forward(self, input: UnionType, params: dict = None) -> UnionType: # type: ignore
+ def forward(self, input: UnionType, params: Optional[Dict[str, torch.Tensor]] = None) -> UnionType: # type: ignore
if params is None:
if isinstance(input, tuple):
batch_size = input[0].shape[0] if len(input[0].shape) == 4 else 1
@@ -241,11 +242,11 @@ class RandomGrayscale(AugmentationBase):
output: torch.Tensor = cast(
torch.Tensor,
- apply_grayscale(
+ F.apply_grayscale(
input[0],
params=params,
return_transform=False))
return output, input[1]
- return apply_grayscale(input, params=params, return_transform=self.return_transform)
+ return F.apply_grayscale(input, params=params, return_transform=self.return_transform)
diff --git a/kornia/augmentation/functional.py b/kornia/augmentation/functional.py
index 701e2c41f..6468a108d 100644
--- a/kornia/augmentation/functional.py
+++ b/kornia/augmentation/functional.py
@@ -1,4 +1,5 @@
-from typing import Tuple, List, Union
+from typing import Tuple, List, Union, Dict
+import warnings
import torch
import torch.nn as nn
@@ -15,41 +16,48 @@ FloatUnionType = Union[torch.Tensor, float, Tuple[float, float], List[float]]
def random_hflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = False) -> UnionType:
+ warnings.warn("random_hflip is going to be deprecated. Please use kornia.augmentation.RandomHorizontalFlip instead.",
+ DeprecationWarning, stacklevel=1)
if isinstance(input, tuple):
batch_size = input[0].shape[0] if len(input[0].shape) == 4 else 1
else:
batch_size = input.shape[0] if len(input.shape) == 4 else 1
- params = get_random_p_params(batch_size, p=p)
+ params = _get_random_p_params(batch_size, p=p)
return apply_hflip(input, params, return_transform)
def random_vflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = False) -> UnionType:
+ warnings.warn("random_vflip is going to be deprecated. Please use kornia.augmentation.RandomVerticalFlip instead.",
+ DeprecationWarning, stacklevel=1)
if isinstance(input, tuple):
batch_size = input[0].shape[0] if len(input[0].shape) == 4 else 1
else:
batch_size = input.shape[0] if len(input.shape) == 4 else 1
- params = get_random_p_params(batch_size, p=p)
+ params = _get_random_p_params(batch_size, p=p)
return apply_vflip(input, params, return_transform)
def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0.,
contrast: FloatUnionType = 0., saturation: FloatUnionType = 0.,
hue: FloatUnionType = 0., return_transform: bool = False) -> UnionType:
+ warnings.warn("color_jitter is going to be deprecated. Please use kornia.augmentation.ColorJitter instead.",
+ DeprecationWarning, stacklevel=1)
if isinstance(input, tuple):
batch_size = input[0].shape[0] if len(input[0].shape) == 4 else 1
else:
batch_size = input.shape[0] if len(input.shape) == 4 else 1
- params = get_color_jitter_params(batch_size, brightness, contrast, saturation, hue)
+ params = _get_color_jitter_params(batch_size, brightness, contrast, saturation, hue)
return apply_color_jitter(input, params, return_transform)
def random_grayscale(input: torch.Tensor, p: float = 0.5, return_transform: bool = False):
- print()
+ warnings.warn("random_grayscale is going to be deprecated. Please use kornia.augmentation.RandomGrayScale instead.",
+ DeprecationWarning, stacklevel=1)
if isinstance(input, tuple):
batch_size = input[0].shape[0] if len(input[0].shape) == 4 else 1
else:
batch_size = input.shape[0] if len(input.shape) == 4 else 1
- params = get_random_p_params(batch_size, p=p)
+ params = _get_random_p_params(batch_size, p=p)
return apply_grayscale(input, params, return_transform)
@@ -209,9 +217,9 @@ def apply_color_jitter(input: torch.Tensor, params: dict, return_transform: bool
return jittered
-def get_color_jitter_params(batch_size: int, brightness: FloatUnionType = 0.,
+def _get_color_jitter_params(batch_size: int, brightness: FloatUnionType = 0.,
contrast: FloatUnionType = 0., saturation: FloatUnionType = 0.,
- hue: FloatUnionType = 0.) -> dict:
+ hue: FloatUnionType = 0.) -> Dict[str, torch.Tensor]:
r"""Random color jiter of an image or batch of images.
See :class:`~kornia.augmentation.ColorJitter` for details.
@@ -326,7 +334,7 @@ def apply_grayscale(input: torch.Tensor, params: dict, return_transform: bool =
return grayscale
-def get_random_p_params(batch_size: int, p: float = .5) -> dict:
+def _get_random_p_params(batch_size: int, p: float = .5) -> Dict[str, torch.Tensor]:
r"""Random grayscale of an image or batch of images.
See :class:`~kornia.augmentation.RandomGrayscale` for details.