π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Correct input checker
Pau Riba6f27666c1670
diff --git a/kornia/augmentation/augmentations.py b/kornia/augmentation/augmentations.pyindex e701c4b9e..8b829f5da 100644--- a/kornia/augmentation/augmentations.py+++ b/kornia/augmentation/augmentations.py@@ -99,7 +99,8 @@ class ColorJitter(nn.Module):self.return_transform: bool = return_transformdef __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__ + reprdef forward(self, input: UnionType) -> UnionType: # type: ignore@@ -175,8 +176,9 @@ def random_hflip(input: torch.Tensor, p: float = 0.5, return_transform: bool = Freturn flipped-def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0., contrast: FloatUnionType = 0.,- saturation: FloatUnionType = 0., hue: FloatUnionType = 0., return_transform: bool = False) -> UnionType:+def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0.,+ contrast: FloatUnionType = 0., saturation: FloatUnionType = 0.,+ hue: FloatUnionType = 0., return_transform: bool = False) -> UnionType:r"""Random color jiter of an image or batch of images.See :class:`~kornia.augmentation.ColorJitter` for details.@@ -187,20 +189,42 @@ def color_jitter(input: torch.Tensor, brightness: FloatUnionType = 0., contrast:r"""Check inputs and compute the corresponding factor bounds"""- if isinstance(factor, float) or (isinstance(factor, torch.Tensor) and factor.size() == torch.Size([])):+ if isinstance(factor, float):+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] = 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:++ factor_bound = torch.tensor([center - factor, center + factor])+ factor_bound = torch.clamp(factor_bound, bounds[0], bounds[1])++ elif (isinstance(factor, torch.Tensor) and factor.dim() == 0):++ if factor < 0:+ raise ValueError(f"If {name} is a single number number, it must be non negative. Got {factor}")++ factor_bound = torch.tensor([torch.tensor(center) - factor, torch.tensor(center) + factor])+ factor_bound = torch.clamp(factor_bound, bounds[0], bounds[1])++ elif isinstance(factor, (tuple, list)) 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)++ factor_bound = torch.tensor(factor)++ elif isinstance(factor, torch.Tensor) and factor.shape[0] == 2 and factor.dim() == 1:++ 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 = 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 torch.Tensor(factor_bound)+ f"The {name} should be a float number or a tuple with length 2 whose values move between {bounds}.")++ return factor_boundif not torch.is_tensor(input):raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}")diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.pyindex 0c5922f39..b934099be 100644--- a/test/augmentation/test_augmentation.py+++ b/test/augmentation/test_augmentation.py@@ -147,7 +147,7 @@ class TestRandomHorizontalFlip:class TestColorJitter:def smoke_test(self):- f = ColorJitter(brightness=0.5, contrast=0.3, saturation=[0.2,1.2], hue=0.1)+ 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@@ -156,7 +156,7 @@ class TestColorJitter:f = ColorJitter()f1 = ColorJitter(return_transform=True)- input = torch.rand(3,5,5) # 3 x 5 x 5+ input = torch.rand(3, 5, 5) # 3 x 5 x 5expected = input@@ -170,10 +170,10 @@ class TestColorJitter:f = ColorJitter()f1 = ColorJitter(return_transform=True)- input = torch.rand(2,3,5,5) # 2 x 3 x 5 x 5+ input = torch.rand(2, 3, 5, 5) # 2 x 3 x 5 x 5expected = input- expected_transform = torch.eye(3).unsqueeze(0).expand((2,3,3)) # 2 x 3 x 3+ expected_transform = torch.eye(3).unsqueeze(0).expand((2, 3, 3)) # 2 x 3 x 3assert_allclose(f(input), expected, atol=1e-4, rtol=1e-5)assert_allclose(f1(input)[0], expected, atol=1e-4, rtol=1e-5)@@ -213,12 +213,11 @@ class TestColorJitter:[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))+ f = ColorJitter(brightness=(-0.2, 0.2))input = torch.tensor([[[[0.1, 0.2, 0.3],[0.6, 0.5, 0.4],@@ -414,7 +413,7 @@ class TestColorJitter:def test_random_saturation_tuple(self):torch.manual_seed(42)- f = ColorJitter(saturation=(0.8,1.2))+ f = ColorJitter(saturation=(0.8, 1.2))input = torch.tensor([[[[0.1, 0.2, 0.3],[0.6, 0.5, 0.4],@@ -502,7 +501,7 @@ class TestColorJitter:def test_random_hue_list(self):torch.manual_seed(42)- f = ColorJitter(hue=[-0.2,0.2])+ f = ColorJitter(hue=[-0.2, 0.2])input = torch.tensor([[[[0.1, 0.2, 0.3],[0.6, 0.5, 0.4],@@ -546,7 +545,7 @@ class TestColorJitter:def test_random_hue_tensor(self):torch.manual_seed(42)- f = ColorJitter(hue=torch.tensor([-0.2,0.2]))+ f = ColorJitter(hue=torch.tensor([-0.2, 0.2]))input = torch.tensor([[[[0.1, 0.2, 0.3],[0.6, 0.5, 0.4],