π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix bugs shift_rgb (#1861)
duc121111895d5328353
diff --git a/kornia/enhance/shift_rgb.py b/kornia/enhance/shift_rgb.pyindex 3599f96ed..e68b84385 100644--- a/kornia/enhance/shift_rgb.py+++ b/kornia/enhance/shift_rgb.py@@ -14,6 +14,6 @@ def shift_rgb(image: torch.Tensor, r_shift: torch.Tensor, g_shift: torch.Tensor,shifts = [r_shift, g_shift, b_shift]- shifted = (image + torch.Tensor(shifts).view(1, 3, 1, 1).to(image)).clamp_(min=0, max=1)+ shifted = (image + torch.stack(shifts).view(-1, 3, 1, 1).to(image)).clamp_(min=0, max=1)return shifteddiff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.pyindex ca23c739d..194920c4b 100644--- a/test/augmentation/test_augmentation.py+++ b/test/augmentation/test_augmentation.py@@ -3339,7 +3339,50 @@ class TestPlanckianJitter(BaseTester):class TestRandomRGBShift:def test_smoke(self, device, dtype):- img = torch.rand(1, 3, 4, 5, device=device, dtype=dtype)+ img = torch.rand(2, 3, 4, 5, device=device, dtype=dtype)aug = RandomRGBShift(p=1.0).to(device)out = aug(img)- assert out.shape == (1, 3, 4, 5)+ assert out.shape == (2, 3, 4, 5)++ def test_onnx_export(self, device, dtype):+ img = torch.rand(1, 3, 4, 5, device=device, dtype=dtype)+ aug = RandomRGBShift(p=1.0).to(device)+ torch.onnx.export(aug, img, "temp.onnx", export_params=True)++ def test_random_rgb_shift(self, device, dtype):+ torch.manual_seed(0)+ input = torch.tensor(+ [[[[0.2, 0.0]], [[0.3, 0.5]], [[0.4, 0.7]]], [[[0.2, 0.7]], [[0.0, 0.8]], [[0.2, 0.3]]]],+ device=device,+ dtype=dtype,+ )++ f = RandomRGBShift(p=1.0).to(device)+ expected = torch.tensor(+ [+ [[[0.19625, 0.00000]], [[0.56822, 0.76822]], [[0.00000, 0.28847]]],+ [[[0.00000, 0.33203]], [[0.00000, 0.60742]], [[0.33407, 0.43407]]],+ ],+ device=device,+ dtype=dtype,+ )+ utils.assert_close(f(input), expected, rtol=1e-4, atol=1e-4)++ def test_random_rgb_shift_same_batch(self, device, dtype):+ torch.manual_seed(0)+ input = torch.tensor(+ [[[[0.2, 0.0]], [[0.3, 0.5]], [[0.4, 0.7]]], [[[0.2, 0.7]], [[0.0, 0.8]], [[0.2, 0.3]]]],+ device=device,+ dtype=dtype,+ )++ f = RandomRGBShift(p=1.0, same_on_batch=True).to(device)+ expected = torch.tensor(+ [+ [[[0.19626, 0.00000]], [[0.29626, 0.49626]], [[0.66822, 0.96822]]],+ [[[0.46822, 0.96822]], [[0.00000, 0.38848]], [[0.00000, 0.00000]]],+ ],+ device=device,+ dtype=dtype,+ )+ utils.assert_close(f(input), expected, rtol=1e-4, atol=1e-4)diff --git a/test/enhance/test_shift_rgb.py b/test/enhance/test_shift_rgb.pyindex 7a8dcc0d5..484675e03 100644--- a/test/enhance/test_shift_rgb.py+++ b/test/enhance/test_shift_rgb.py@@ -8,7 +8,7 @@ import kornia.testing as utils # test utilsclass TestRGBShift:def test_rgb_shift_no_shift(self, device, dtype):- r_shift, g_shift, b_shift = 0, 0, 0+ r_shift, g_shift, b_shift = torch.Tensor([0]), torch.Tensor([0]), torch.Tensor([0])image = torch.rand(2, 3, 5, 5, device=device, dtype=dtype)expected = imageshifted = kornia.enhance.shift_rgb(image, r_shift, g_shift, b_shift)@@ -16,7 +16,7 @@ class TestRGBShift:utils.assert_close(shifted, expected)def test_rgb_shift_all_zeros(self, device, dtype):- r_shift, g_shift, b_shift = -0.1, -0.1, -0.1+ r_shift, g_shift, b_shift = torch.Tensor([-0.1]), torch.Tensor([-0.1]), torch.Tensor([-0.1])image = torch.zeros(2, 3, 5, 5, device=device, dtype=dtype)expected = imageshifted = kornia.enhance.shift_rgb(image, r_shift, g_shift, b_shift)@@ -24,7 +24,7 @@ class TestRGBShift:utils.assert_close(shifted, expected)def test_rgb_shift_all_ones(self, device, dtype):- r_shift, g_shift, b_shift = 1, 1, 1+ r_shift, g_shift, b_shift = torch.Tensor([1]), torch.Tensor([1]), torch.Tensor([1])image = torch.rand(2, 3, 5, 5, device=device, dtype=dtype)expected = torch.ones(2, 3, 5, 5, device=device, dtype=dtype)shifted = kornia.enhance.shift_rgb(image, r_shift, g_shift, b_shift)@@ -32,21 +32,29 @@ class TestRGBShift:utils.assert_close(shifted, expected)def test_rgb_shift_invalid_parameter_shape(self, device, dtype):- r_shift, g_shift, b_shift = 0.5, 0.5, 0.5+ r_shift, g_shift, b_shift = torch.Tensor([0.5]), torch.Tensor([0.5]), torch.Tensor([0.5])image = torch.randn(3, 3, device=device, dtype=dtype)with pytest.raises(TypeError):kornia.enhance.shift_rgb(image, r_shift, g_shift, b_shift)def test_rgb_shift_gradcheck(self, device, dtype):- r_shift, g_shift, b_shift = 0.4, 0.5, 0.2+ r_shift, g_shift, b_shift = torch.Tensor([0.4]), torch.Tensor([0.5]), torch.Tensor([0.2])image = torch.randn(2, 3, 5, 5, device=device, dtype=dtype)image = utils.tensor_to_gradcheck_var(image) # to varassert gradcheck(kornia.enhance.shift_rgb, (image, r_shift, g_shift, b_shift), raise_exception=True)def test_rgb_shift(self, device, dtype):- r_shift, g_shift, b_shift = 0.1, 0.2, -0.3- image = torch.tensor([[[[0.2, 0.0]], [[0.3, 0.5]], [[0.4, 0.7]]]], device=device, dtype=dtype)+ r_shift, g_shift, b_shift = torch.Tensor([0.1]), torch.Tensor([0.3]), torch.Tensor([-0.3])+ image = torch.tensor(+ [[[[0.2, 0.0]], [[0.3, 0.5]], [[0.4, 0.7]]], [[[0.2, 0.7]], [[0.0, 0.8]], [[0.2, 0.3]]]],+ device=device,+ dtype=dtype,+ )shifted = kornia.enhance.shift_rgb(image, r_shift, g_shift, b_shift)- expected = torch.tensor([[[[0.3, 0.1]], [[0.5, 0.7]], [[0.1, 0.4]]]], device=device, dtype=dtype)+ expected = torch.tensor(+ [[[[0.3, 0.1]], [[0.6, 0.8]], [[0.1, 0.4]]], [[[0.3, 0.8]], [[0.3, 1.0]], [[0.0, 0.0]]]],+ device=device,+ dtype=dtype,+ )utils.assert_close(shifted, expected)