Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Test color jitter

Pau Ribab10b471b9231
diff --git a/kornia/__init__.py b/kornia/__init__.py
index 737185242..3ecfb1ae5 100644
--- a/kornia/__init__.py
+++ b/kornia/__init__.py
@@ -73,6 +73,7 @@ from kornia.utils import (
)
from kornia.augmentation import (
random_hflip,
+ color_jitter,
)
from kornia.geometry import *
diff --git a/kornia/augmentation/__init__.py b/kornia/augmentation/__init__.py
index 782197c48..6fc3aa11b 100644
--- a/kornia/augmentation/__init__.py
+++ b/kornia/augmentation/__init__.py
@@ -1,3 +1,3 @@
from kornia.augmentation.augmentations import *
-__all__ = ["random_hflip", "RandomHorizontalFlip"]
+__all__ = ["random_hflip", "RandomHorizontalFlip", "color_jitter", "ColorJitter"]
diff --git a/kornia/augmentation/augmentations.py b/kornia/augmentation/augmentations.py
index e68dc233b..e0628369d 100644
--- a/kornia/augmentation/augmentations.py
+++ b/kornia/augmentation/augmentations.py
@@ -1,12 +1,13 @@
from typing import Tuple, List, Union, cast
import torch
import torch.nn as nn
+from torch.distributions import Uniform
from kornia.geometry.transform.flips import hflip
from kornia.color.adjust import AdjustBrightness, AdjustContrast, AdjustSaturation, AdjustHue
UnionType = Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]
-FloatUnionType = Union[float, Tuple[float, float], List[float]]
+FloatUnionType = Union[torch.Tensor, float, Tuple[float, float], List[float]]
class RandomHorizontalFlip(nn.Module):
@@ -91,21 +92,21 @@ class ColorJitter(nn.Module):
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
+ self.brightness: FloatUnionType = brightness
+ self.contrast: FloatUnionType = contrast
+ self.saturation: FloatUnionType = saturation
+ self.hue: FloatUnionType = hue
+ self.return_transform: bool = 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})"
+ 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 = cast(torch.Tensor, color_jitter(input[0], self.brightness, self.contrast, self.saturation, self.hue, return_transform=False))
+ jittered: torch.Tensor = cast(torch.Tensor, color_jitter(input[0], brightness=self.brightness, contrast=self.contrast, saturation=self.saturation, hue=self.hue, return_transform=False))
return jittered, input[1]
@@ -141,18 +142,19 @@ def random_hflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = F
input = input.unsqueeze(0)
input = input.view((-1, (*input.shape[-3:])))
+
probs: torch.Tensor = torch.empty(input.shape[0], device=device).uniform_(0, 1)
to_flip: torch.Tensor = probs < p
flipped: torch.Tensor = input.clone()
- trans_mat: torch.Tensor = torch.eye(3, device=device, dtype=dtype).expand(input.shape[0], -1, -1)
-
flipped[to_flip] = hflip(input[to_flip])
flipped.squeeze_()
if return_transform:
+ trans_mat: torch.Tensor = torch.eye(3, device=device, dtype=dtype).expand(input.shape[0], -1, -1)
+
w: int = input.shape[-2]
flip_mat: torch.Tensor = torch.tensor([[-1, 0, w],
[0, 1, 0],
@@ -177,20 +179,20 @@ def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0., contrast:
r"""Check inputs and compute the corresponding factor bounds
"""
- if isinstance(factor, float):
+ if isinstance(factor, float) or (isinstance(factor, torch.Tensor) and factor.size() == 0):
if factor < 0:
raise ValueError(f"If {name} is a single number number, it must be non negative. Got {factor}")
factor_bound = [center - factor, center + factor]
- factor_bound[0] = max(factor_bound[0], bounds[0])
- factor_bound[1] = min(factor_bound[1], bounds[1])
- elif isinstance(factor, (tuple, list)) and len(factor) == 2:
+ factor_bound[0] = torch.max(torch.tensor([factor_bound[0], bounds[0]]))
+ factor_bound[1] = torch.min(torch.tensor([factor_bound[1], bounds[1]]))
+ elif isinstance(factor, (tuple, list, torch.Tensor)) and len(factor) == 2:
if not bounds[0] <= factor[0] <= factor[1] <= bounds[1]:
raise ValueError(f"{name}[0] should be smaller than {name}[1] got {factor}")
factor_bound = list(factor)
else:
raise TypeError(
f"The {name} should be a float number or a tuple with length 2 whose values move between [-Inf, Inf].")
- return factor_bound
+ return torch.Tensor(factor_bound)
if not torch.is_tensor(input):
raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}")
@@ -209,21 +211,17 @@ def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0., contrast:
input = input.unsqueeze(0)
input = input.view((-1, (*input.shape[-3:])))
- brightness_factor: torch.Tensor = torch.empty(
- input.shape[0], device=device).uniform_(
- brightness_bound[0], brightness_bound[1])
+ brightness_distribution = Uniform(brightness_bound[0], brightness_bound[1])
+ brightness_factor = brightness_distribution.rsample([input.shape[0]])
- contrast_factor: torch.Tensor = torch.empty(
- input.shape[0], device=device).uniform_(
- contrast_bound[0], contrast_bound[1])
+ contrast_distribution = Uniform(contrast_bound[0], contrast_bound[1])
+ contrast_factor = contrast_distribution.rsample([input.shape[0]])
- hue_factor: torch.Tensor = torch.empty(
- input.shape[0], device=device).uniform_(
- hue_bound[0], hue_bound[1])
+ hue_distribution = Uniform(hue_bound[0], hue_bound[1])
+ hue_factor = hue_distribution.rsample([input.shape[0]])
- saturation_factor: torch.Tensor = torch.empty(
- input.shape[0], device=device).uniform_(
- saturation_bound[0], saturation_bound[1])
+ saturation_distribution = Uniform(saturation_bound[0], saturation_bound[1])
+ saturation_factor = saturation_distribution.rsample([input.shape[0]])
transforms = nn.ModuleList([AdjustBrightness(brightness_factor),
AdjustContrast(contrast_factor),
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index e8594fa8d..2557e2bc0 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -7,7 +7,7 @@ from torch.autograd import gradcheck
import kornia
import kornia.testing as utils # test utils
-from kornia.augmentation import RandomHorizontalFlip
+from kornia.augmentation import RandomHorizontalFlip, ColorJitter
class TestRandomHorizontalFlip:
@@ -142,3 +142,369 @@ class TestRandomHorizontalFlip:
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(RandomHorizontalFlip(p=1.), (input, ), raise_exception=True)
+
+
+class TestColorJitter:
+
+ def smoke_test(self):
+ f = ColorJitter(brightness=0.5, contrast=0.3, saturation=[0.2,1.2], hue=0.1)
+ repr = "ColorJitter(brightness=0.5, contrast=0.3, saturation=[0.2, 1.2], hue=0.1, return_transform=False)"
+ assert str(f) == repr
+
+ def test_color_jitter(self):
+
+ f = ColorJitter()
+ f1 = ColorJitter(return_transform=True)
+
+ input = torch.rand(3,5,5) # 3 x 5 x 5
+
+ expected = input
+
+ expected_transform = torch.eye(3).unsqueeze(0) # 3 x 3
+
+ assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(f1(input)[0], expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(f1(input)[1], expected_transform, atol=1e-4, rtol=1e-5)
+
+ def test_color_jitter_batch(self):
+ f = ColorJitter()
+ f1 = ColorJitter(return_transform=True)
+
+ input = torch.rand(2,3,5,5) # 2 x 3 x 5 x 5
+ expected = input
+
+ expected_transform = torch.eye(3).unsqueeze(0).expand((2,3,3)) # 2 x 3 x 3
+
+ assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(f1(input)[0], expected, atol=1e-4, rtol=1e-5)
+ assert_allclose(f1(input)[1], expected_transform, atol=1e-4, rtol=1e-5)
+
+ def test_random_brightness(self):
+ torch.manual_seed(42)
+ f = ColorJitter(brightness=0.2)
+
+ input = torch.tensor([[[[0.1, 0.2, 0.3],
+ [0.6, 0.5, 0.4],
+ [0.7, 0.8, 1.]]]]) # 1 x 1 x 3 x 3
+ input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3
+
+ expected = torch.tensor([[[[0.2529, 0.3529, 0.4529],
+ [0.7529, 0.6529, 0.5529],
+ [0.8529, 0.9529, 1.0000]],
+
+ [[0.2529, 0.3529, 0.4529],
+ [0.7529, 0.6529, 0.5529],
+ [0.8529, 0.9529, 1.0000]],
+
+ [[0.2529, 0.3529, 0.4529],
+ [0.7529, 0.6529, 0.5529],
+ [0.8529, 0.9529, 1.0000]]],
+
+
+ [[[0.2660, 0.3660, 0.4660],
+ [0.7660, 0.6660, 0.5660],
+ [0.8660, 0.9660, 1.0000]],
+
+ [[0.2660, 0.3660, 0.4660],
+ [0.7660, 0.6660, 0.5660],
+ [0.8660, 0.9660, 1.0000]],
+
+ [[0.2660, 0.3660, 0.4660],
+ [0.7660, 0.6660, 0.5660],
+ [0.8660, 0.9660, 1.0000]]]]) # 1 x 1 x 3 x 3
+
+
+ assert_allclose(f(input), expected)
+
+ def test_random_brightness_tuple(self):
+ torch.manual_seed(42)
+ f = ColorJitter(brightness=(-0.2,0.2))
+
+ input = torch.tensor([[[[0.1, 0.2, 0.3],
+ [0.6, 0.5, 0.4],
+ [0.7, 0.8, 1.]]]]) # 1 x 1 x 3 x 3
+ input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3
+
+ expected = torch.tensor([[[[0.2529, 0.3529, 0.4529],
+ [0.7529, 0.6529, 0.5529],
+ [0.8529, 0.9529, 1.0000]],
+
+ [[0.2529, 0.3529, 0.4529],
+ [0.7529, 0.6529, 0.5529],
+ [0.8529, 0.9529, 1.0000]],
+
+ [[0.2529, 0.3529, 0.4529],
+ [0.7529, 0.6529, 0.5529],
+ [0.8529, 0.9529, 1.0000]]],
+
+
+ [[[0.2660, 0.3660, 0.4660],
+ [0.7660, 0.6660, 0.5660],
+ [0.8660, 0.9660, 1.0000]],
+
+ [[0.2660, 0.3660, 0.4660],
+ [0.7660, 0.6660, 0.5660],
+ [0.8660, 0.9660, 1.0000]],
+
+ [[0.2660, 0.3660, 0.4660],
+ [0.7660, 0.6660, 0.5660],
+ [0.8660, 0.9660, 1.0000]]]]) # 1 x 1 x 3 x 3
+
+ assert_allclose(f(input), expected)
+
+ def test_random_contrast(self):
+ torch.manual_seed(42)
+ f = ColorJitter(contrast=0.2)
+
+ input = torch.tensor([[[[0.1, 0.2, 0.3],
+ [0.6, 0.5, 0.4],
+ [0.7, 0.8, 1.]]]]) # 1 x 1 x 3 x 3
+ input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3
+
+ expected = torch.tensor([[[[0.0953, 0.1906, 0.2859],
+ [0.5719, 0.4766, 0.3813],
+ [0.6672, 0.7625, 0.9531]],
+
+ [[0.0953, 0.1906, 0.2859],
+ [0.5719, 0.4766, 0.3813],
+ [0.6672, 0.7625, 0.9531]],
+
+ [[0.0953, 0.1906, 0.2859],
+ [0.5719, 0.4766, 0.3813],
+ [0.6672, 0.7625, 0.9531]]],
+
+
+ [[[0.1184, 0.2367, 0.3551],
+ [0.7102, 0.5919, 0.4735],
+ [0.8286, 0.9470, 1.0000]],
+
+ [[0.1184, 0.2367, 0.3551],
+ [0.7102, 0.5919, 0.4735],
+ [0.8286, 0.9470, 1.0000]],
+
+ [[0.1184, 0.2367, 0.3551],
+ [0.7102, 0.5919, 0.4735],
+ [0.8286, 0.9470, 1.0000]]]])
+
+ assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)
+
+ def test_random_contrast_list(self):
+ torch.manual_seed(42)
+ f = ColorJitter(contrast=[0.8, 1.2])
+
+ input = torch.tensor([[[[0.1, 0.2, 0.3],
+ [0.6, 0.5, 0.4],
+ [0.7, 0.8, 1.]]]]) # 1 x 1 x 3 x 3
+ input = input.repeat(2, 3, 1, 1) # 2 x 3 x 3
+
+ expected = torch.tensor([[[[0.0953, 0.1906, 0.2859],
+ [0.5719, 0.4766, 0.3813],
+ [0.6672, 0.7625, 0.9531]],
+
+ [[0.0953, 0.1906, 0.2859],
+ [0.5719, 0.4766, 0.3813],
+ [0.6672, 0.7625, 0.9531]],
+
+ [[0.0953, 0.1906, 0.2859],
+ [0.5719, 0.4766, 0.3813],
+ [0.6672, 0.7625, 0.9531]]],
+
+
+ [[[0.1184, 0.2367, 0.3551],
+ [0.7102, 0.5919, 0.4735],
+ [0.8286, 0.9470, 1.0000]],
+
+ [[0.1184, 0.2367, 0.3551],
+ [0.7102, 0.5919, 0.4735],
+ [0.8286, 0.9470, 1.0000]],
+
+ [[0.1184, 0.2367, 0.3551],
+ [0.7102, 0.5919, 0.4735],
+ [0.8286, 0.9470, 1.0000]]]])
+
+ assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)
+
+ def test_random_saturation(self):
+ torch.manual_seed(42)
+ f = ColorJitter(saturation=0.2)
+
+ input = torch.tensor([[[[0.1, 0.2, 0.3],
+ [0.6, 0.5, 0.4],
+ [0.7, 0.8, 1.]],
+
+ [[1.0, 0.5, 0.6],
+ [0.6, 0.3, 0.2],
+ [0.8, 0.1, 0.2]],
+
+ [[0.6, 0.8, 0.7],
+ [0.9, 0.3, 0.2],
+ [0.8, 0.4, .5]]]]) # 1 x 1 x 3 x 3
+ input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3
+
+ expected = torch.tensor([[[[1.8763e-01, 2.5842e-01, 3.3895e-01],
+ [6.2921e-01, 5.0000e-01, 4.0000e-01],
+ [7.0974e-01, 8.0000e-01, 1.0000e+00]],
+
+ [[1.0000e+00, 5.2921e-01, 6.0974e-01],
+ [6.2921e-01, 3.1947e-01, 2.1947e-01],
+ [8.0000e-01, 1.6816e-01, 2.7790e-01]],
+
+ [[6.3895e-01, 8.0000e-01, 7.0000e-01],
+ [9.0000e-01, 3.1947e-01, 2.1947e-01],
+ [8.0000e-01, 4.3895e-01, 5.4869e-01]]],
+
+
+ [[[1.1921e-07, 1.2953e-01, 2.5302e-01],
+ [5.6476e-01, 5.0000e-01, 4.0000e-01],
+ [6.8825e-01, 8.0000e-01, 1.0000e+00]],
+
+ [[1.0000e+00, 4.6476e-01, 5.8825e-01],
+ [5.6476e-01, 2.7651e-01, 1.7651e-01],
+ [8.0000e-01, 1.7781e-02, 1.0603e-01]],
+
+ [[5.5556e-01, 8.0000e-01, 7.0000e-01],
+ [9.0000e-01, 2.7651e-01, 1.7651e-01],
+ [8.0000e-01, 3.5302e-01, 4.4127e-01]]]])
+
+ assert_allclose(f(input), expected)
+
+ def test_random_saturation_tuple(self):
+ torch.manual_seed(42)
+ f = ColorJitter(saturation=(0.8,1.2))
+
+ input = torch.tensor([[[[0.1, 0.2, 0.3],
+ [0.6, 0.5, 0.4],
+ [0.7, 0.8, 1.]],
+
+ [[1.0, 0.5, 0.6],
+ [0.6, 0.3, 0.2],
+ [0.8, 0.1, 0.2]],
+
+ [[0.6, 0.8, 0.7],
+ [0.9, 0.3, 0.2],
+ [0.8, 0.4, .5]]]]) # 1 x 1 x 3 x 3
+ input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3
+
+ expected = torch.tensor([[[[1.8763e-01, 2.5842e-01, 3.3895e-01],
+ [6.2921e-01, 5.0000e-01, 4.0000e-01],
+ [7.0974e-01, 8.0000e-01, 1.0000e+00]],
+
+ [[1.0000e+00, 5.2921e-01, 6.0974e-01],
+ [6.2921e-01, 3.1947e-01, 2.1947e-01],
+ [8.0000e-01, 1.6816e-01, 2.7790e-01]],
+
+ [[6.3895e-01, 8.0000e-01, 7.0000e-01],
+ [9.0000e-01, 3.1947e-01, 2.1947e-01],
+ [8.0000e-01, 4.3895e-01, 5.4869e-01]]],
+
+
+ [[[1.1921e-07, 1.2953e-01, 2.5302e-01],
+ [5.6476e-01, 5.0000e-01, 4.0000e-01],
+ [6.8825e-01, 8.0000e-01, 1.0000e+00]],
+
+ [[1.0000e+00, 4.6476e-01, 5.8825e-01],
+ [5.6476e-01, 2.7651e-01, 1.7651e-01],
+ [8.0000e-01, 1.7781e-02, 1.0603e-01]],
+
+ [[5.5556e-01, 8.0000e-01, 7.0000e-01],
+ [9.0000e-01, 2.7651e-01, 1.7651e-01],
+ [8.0000e-01, 3.5302e-01, 4.4127e-01]]]])
+
+ assert_allclose(f(input), expected)
+
+ def test_random_hue(self):
+ torch.manual_seed(42)
+ f = ColorJitter(hue=0.2)
+
+ input = torch.tensor([[[[0.1, 0.2, 0.3],
+ [0.6, 0.5, 0.4],
+ [0.7, 0.8, 1.]],
+
+ [[1.0, 0.5, 0.6],
+ [0.6, 0.3, 0.2],
+ [0.8, 0.1, 0.2]],
+
+ [[0.6, 0.8, 0.7],
+ [0.9, 0.3, 0.2],
+ [0.8, 0.4, .5]]]]) # 1 x 1 x 3 x 3
+ input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3
+
+ expected = torch.tensor([[[[0.1000, 0.2000, 0.3000],
+ [0.6000, 0.5000, 0.4000],
+ [0.7000, 0.8000, 1.0000]],
+
+ [[1.0000, 0.6578, 0.7000],
+ [0.6789, 0.3000, 0.2000],
+ [0.8000, 0.1000, 0.2000]],
+
+ [[0.3634, 0.8000, 0.6948],
+ [0.9000, 0.3526, 0.2526],
+ [0.7737, 0.5840, 0.7103]]],
+
+
+ [[[0.1000, 0.2000, 0.3000],
+ [0.6726, 0.5000, 0.4000],
+ [0.7000, 0.8000, 1.0000]],
+
+ [[1.0000, 0.3547, 0.5031],
+ [0.6000, 0.3484, 0.2484],
+ [0.7758, 0.1000, 0.2000]],
+
+ [[0.8179, 0.8000, 0.7000],
+ [0.9000, 0.3000, 0.2000],
+ [0.8000, 0.2305, 0.3063]]]])
+
+ assert_allclose(f(input), expected)
+
+
+ def test_random_hue_list(self):
+ torch.manual_seed(42)
+ f = ColorJitter(hue=[-0.2,0.2])
+
+ input = torch.tensor([[[[0.1, 0.2, 0.3],
+ [0.6, 0.5, 0.4],
+ [0.7, 0.8, 1.]],
+
+ [[1.0, 0.5, 0.6],
+ [0.6, 0.3, 0.2],
+ [0.8, 0.1, 0.2]],
+
+ [[0.6, 0.8, 0.7],
+ [0.9, 0.3, 0.2],
+ [0.8, 0.4, .5]]]]) # 1 x 1 x 3 x 3
+ input = input.repeat(2, 1, 1, 1) # 2 x 3 x 3
+
+ expected = torch.tensor([[[[0.1000, 0.2000, 0.3000],
+ [0.6000, 0.5000, 0.4000],
+ [0.7000, 0.8000, 1.0000]],
+
+ [[1.0000, 0.6578, 0.7000],
+ [0.6789, 0.3000, 0.2000],
+ [0.8000, 0.1000, 0.2000]],
+
+ [[0.3634, 0.8000, 0.6948],
+ [0.9000, 0.3526, 0.2526],
+ [0.7737, 0.5840, 0.7103]]],
+
+
+ [[[0.1000, 0.2000, 0.3000],
+ [0.6726, 0.5000, 0.4000],
+ [0.7000, 0.8000, 1.0000]],
+
+ [[1.0000, 0.3547, 0.5031],
+ [0.6000, 0.3484, 0.2484],
+ [0.7758, 0.1000, 0.2000]],
+
+ [[0.8179, 0.8000, 0.7000],
+ [0.9000, 0.3000, 0.2000],
+ [0.8000, 0.2305, 0.3063]]]])
+
+ assert_allclose(f(input), expected)
+
+ def test_gradcheck(self):
+
+ input = torch.rand((3, 5, 5)).double() # 3 x 3
+
+ input = utils.tensor_to_gradcheck_var(input) # to var
+
+ assert gradcheck(kornia.color_jitter, (input, ), raise_exception=True)