Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Merge master

Pau Ribad969d937f11e
diff --cc kornia/augmentation/augmentations.py
index 817cd6e98,d904acbad..e68dc233b
--- a/kornia/augmentation/augmentations.py
+++ b/kornia/augmentation/augmentations.py
@@@ -1,4 -1,4 +1,4 @@@
- from typing import Tuple, List, Union
-from typing import Tuple, Union, cast
++from typing import Tuple, List, Union, cast
import torch
import torch.nn as nn
@@@ -16,61 -18,62 +20,99 @@@ class RandomHorizontalFlip(nn.Module)
Args:
p (float): probability of the image being flipped. Default value is 0.5
- return_transformation (bool): if ``True`` return the matrix describing the transformation applied to each
- input tensor.
+ return_transform (bool): if ``True`` return the matrix describing the transformation applied to each
+ input tensor. If ``False`` and the input is a tuple the applied transformation
+ wont be concatenated
+
+ Examples:
+ >>> input = torch.tensor([[[
+ [0., 0., 0.],
+ [0., 0., 0.],
+ [0., 1., 1.]]]])
+ >>> seq = nn.Sequential(kornia.augmentation.RandomHorizontalFlip(p=1.0, return_transform=True),
+ kornia.augmentation.RandomHorizontalFlip(p=1.0, return_transform=True)
+ )
+ >>> seq(input)
+ (tensor([[0., 0., 0.],
+ [0., 0., 0.],
+ [0., 1., 1.]]),
+ tensor([[[1., 0., 0.],
+ [0., 1., 0.],
+ [0., 0., 1.]]]))
+
"""
- def __init__(self, p: float = 0.5, return_transformation: bool = False) -> None:
+ def __init__(self, p: float = 0.5, return_transform: bool = False) -> None:
super(RandomHorizontalFlip, self).__init__()
self.p = p
- self.return_transformation = return_transformation
+ self.return_transform = return_transform
def __repr__(self) -> str:
- repr = f"(p={self.p})"
+ repr = f"(p={self.p}, return_transform={self.return_transform})"
return self.__class__.__name__ + repr
- def forward(self, input: torch.Tensor) -> UnionType: # type: ignore
- return random_hflip(input, self.p, self.return_transformation)
+ def forward(self, input: UnionType) -> UnionType: # type: ignore
+
+ if isinstance(input, tuple):
+
+ inp: torch.Tensor = input[0]
+ prev_trans: torch.Tensor = input[1]
+
+ if self.return_transform:
+
+ out = random_hflip(inp, p=self.p, return_transform=True)
+ img: torch.Tensor = out[0]
+ trans_mat: torch.Tensor = out[1]
+
+ return img, prev_trans @ trans_mat
+
+ # https://mypy.readthedocs.io/en/latest/casts.html cast the return type to please mypy gods
+ img = cast(torch.Tensor, random_hflip(inp, p=self.p, return_transform=False))
+
+ # Transform image but pass along the previous transformation
+ return img, prev_trans
+
+ return random_hflip(input, p=self.p, return_transform=self.return_transform)
+class ColorJitter(nn.Module):
+
+ r"""Change the brightness, contrast, saturation and hue randomly given tensor image or a batch of tensor images.
+
+ Input should be a tensor of shape (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.
+
+ Args:
+ brightness (float or tuple): Default value is 0
+ contrast (float or tuple): Default value is 0
+ saturation (float or tuple): Default value is 0
+ hue (float or tuple): Default value is 0
+ """
+
+ def __init__(self, brightness: FloatUnionType = 0., contrast: FloatUnionType = 0.,
+ saturation: FloatUnionType = 0., hue: FloatUnionType = 0., return_transform: bool = False) -> None:
+ super(ColorJitter, self).__init__()
+ self.brightness = brightness
+ self.contrast = contrast
+ self.saturation = saturation
+ self.hue = hue
+ self.return_transform = return_transform
+
+ def __repr__(self) -> str:
+ repr = f"(brightness={self.brightness}, contrast={self.contrast}, saturation={self.saturation}, hue={self.hue}), return_transform={self.return_transform})"
+ return self.__class__.__name__ + repr
+
+ def forward(self, input: UnionType) -> UnionType: # type: ignore
+
+ if isinstance(input, tuple):
+
- jittered: torch.Tensor = color_jitter(input[0], self.brightness, self.contrast, self.saturation, self.hue) # type: ignore
++ jittered: torch.Tensor = cast(torch.Tensor, color_jitter(input[0], self.brightness, self.contrast, self.saturation, self.hue, return_transform=False))
+
+ return jittered, input[1]
+
+ return color_jitter(input, self.brightness, self.contrast, self.saturation, self.hue, self.return_transform)
+
+
- def random_hflip(input: torch.Tensor, p: float = 0.5, return_transformation: bool = False) -> UnionType:
+ def random_hflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = False) -> UnionType:
r"""Horizontally flip a tensor image or a batch of tensor images randomly with a given probability.
Input should be a tensor of shape (C, H, W) or a batch of tensors :math:`(*, C, H, W)`.
diff --cc kornia/color/adjust.py
index 8c9aa73e0,029651a58..bf4cc7fa4
--- a/kornia/color/adjust.py
+++ b/kornia/color/adjust.py
@@@ -217,9 -214,9 +214,9 @@@ class AdjustSaturation(nn.Module)
torch.Tensor: Adjusted image.
"""
-- def __init__(self, saturation_factor: float) -> None:
++ def __init__(self, saturation_factor: Union[float, torch.Tensor]) -> None:
super(AdjustSaturation, self).__init__()
-- self.saturation_factor: float = saturation_factor
++ self.saturation_factor: Union[float, torch.Tensor] = saturation_factor
def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore
return adjust_saturation(input, self.saturation_factor)
@@@ -241,9 -238,9 +238,9 @@@ class AdjustHue(nn.Module)
torch.Tensor: Adjusted image.
"""
-- def __init__(self, hue_factor: float) -> None:
++ def __init__(self, hue_factor: Union[float, torch.Tensor]) -> None:
super(AdjustHue, self).__init__()
-- self.hue_factor: float = hue_factor
++ self.hue_factor: Union[float, torch.Tensor] = hue_factor
def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore
return adjust_hue(input, self.hue_factor)
@@@ -265,10 -262,10 +262,10 @@@ class AdjustGamma(nn.Module)
torch.Tensor: Adjusted image.
"""
-- def __init__(self, gamma: float, gain: float = 1.) -> None:
++ def __init__(self, gamma: Union[float, torch.Tensor], gain: Union[float, torch.Tensor] = 1.) -> None:
super(AdjustGamma, self).__init__()
-- self.gamma: float = gamma
-- self.gain: float = gain
++ self.gamma: Union[float, torch.Tensor] = gamma
++ self.gain: Union[float, torch.Tensor] = gain
def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore
return adjust_gamma(input, self.gamma, self.gain)