π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Merge branch 'master' into feat/gpu_tests
Edgar Riba09c075e5172e
diff --cc test/color/test_adjust.pyindex 3f1f7ec30,2cf9845ce..c8d5c1f74--- a/test/color/test_adjust.py+++ b/test/color/test_adjust.py@@@ -2,7 -2,8 +2,8 @@@ import pytesimport korniaimport kornia.testing as utils # test utils+ from kornia.geometry import pi-from test.common import device_type+from test.common import deviceimport torchfrom torch.autograd import gradcheck@@@ -26,9 -25,39 +27,39 @@@ class TestAdjustSaturationf = kornia.color.AdjustSaturation(1.)assert_allclose(f(data), expected)+ def test_saturation_one_batch(self):+ data = torch.tensor([[[[.5, .5],+ [.5, .5]],++ [[.5, .5],+ [.5, .5]],++ [[.25, .25],+ [.25, .25]]],++ [[[.5, .5],+ [.5, .5]],++ [[.5, .5],+ [.5, .5]],++ [[.25, .25],+ [.25, .25]]]]) # 2x3x2x2++ expected = data+ f = kornia.color.AdjustSaturation(torch.ones(2))+ assert_allclose(f(data), expected)++ def test_gradcheck(self):+ batch_size, channels, height, width = 2, 3, 4, 5+ img = torch.rand(batch_size, channels, height, width)+ img = utils.tensor_to_gradcheck_var(img) # to var+ assert gradcheck(kornia.adjust_saturation, (img, 2.),+ raise_exception=True)+class TestAdjustHue:- def test_hue_one(self):+ def test_hue_one(self, device):data = torch.tensor([[[.5, .5],[.5, .5]],@@@ -38,15 -67,66 +69,68 @@@[[.25, .25],[.25, .25]]]) # 3x2x2- expected = data+ data = data.to(device)+ expected = data.clone()+- f = kornia.color.AdjustHue(1.)+ f = kornia.color.AdjustHue(0.)assert_allclose(f(data), expected)+ def test_hue_one_batch(self):+ data = torch.tensor([[[[.5, .5],+ [.5, .5]],++ [[.5, .5],+ [.5, .5]],++ [[.25, .25],+ [.25, .25]]],++ [[[.5, .5],+ [.5, .5]],++ [[.5, .5],+ [.5, .5]],++ [[.25, .25],+ [.25, .25]]]]) # 2x3x2x2++ expected = data+ f = kornia.color.AdjustHue(torch.tensor([0, 0]))+ assert_allclose(f(data), expected)++ def test_hue_flip_batch(self):+ data = torch.tensor([[[[.5, .5],+ [.5, .5]],++ [[.5, .5],+ [.5, .5]],++ [[.25, .25],+ [.25, .25]]],++ [[[.5, .5],+ [.5, .5]],++ [[.5, .5],+ [.5, .5]],++ [[.25, .25],+ [.25, .25]]]]) # 2x3x2x2++ f = kornia.color.AdjustHue(torch.tensor([-pi, pi]))+ result = f(data)+ assert_allclose(result, result.flip(0))++ def test_gradcheck(self):+ batch_size, channels, height, width = 2, 3, 4, 5+ img = torch.rand(batch_size, channels, height, width)+ img = utils.tensor_to_gradcheck_var(img) # to var+ assert gradcheck(kornia.adjust_hue, (img, 2.),+ raise_exception=True)+class TestAdjustGamma:- def test_gamma_zero(self):+ def test_gamma_zero(self, device):data = torch.tensor([[[1., 1.],[1., 1.]],@@@ -128,10 -198,49 +212,50 @@@f = kornia.color.AdjustGamma(2.)assert_allclose(f(data), expected)- def test_gradcheck(self, device):+ def test_gamma_two_batch(self):+ data = torch.tensor([[[[1., 1.],+ [1., 1.]],++ [[.5, .5],+ [.5, .5]],++ [[.25, .25],+ [.25, .25]]],++ [[[1., 1.],+ [1., 1.]],++ [[.5, .5],+ [.5, .5]],++ [[.25, .25],+ [.25, .25]]]]) # 2x3x2x2++ expected = torch.tensor([[[[1., 1.],+ [1., 1.]],++ [[.25, .25],+ [.25, .25]],++ [[.0625, .0625],+ [.0625, .0625]]],++ [[[1., 1.],+ [1., 1.]],++ [[.25, .25],+ [.25, .25]],++ [[.0625, .0625],+ [.0625, .0625]]]]) # 2x3x2x2++ f = kornia.color.AdjustGamma(torch.tensor([2., 2.]), gain=torch.ones(2))+ assert_allclose(f(data), expected)++ def test_gradcheck(self):batch_size, channels, height, width = 2, 3, 4, 5img = torch.ones(batch_size, channels, height, width)+ img = img.to(device)img = utils.tensor_to_gradcheck_var(img) # to varassert gradcheck(kornia.adjust_gamma, (img, 1., 2.),raise_exception=True)diff --cc test/color/test_hls.pyindex 107d38c1f,37fe07667..a5088e838--- a/test/color/test_hls.py+++ b/test/color/test_hls.py@@@ -2,8 -2,13 +2,13 @@@ import pytesimport korniaimport kornia.testing as utils # test utils+ from kornia.geometry import pi-from test.common import device_type+from test.common import device+ import cv2+ import math+ import numpy as np+import torchfrom torch.autograd import gradcheckfrom torch.testing import assert_allclose@@@ -11,84 -16,65 +16,65 @@@class TestRgbToHls:- def test_rgb_to_hls(self):+ def test_rgb_to_hls(self, device):- data = torch.tensor([[[21., 22.],- [22., 22.]],-- [[13., 14.],- [14., 14.]],-- [[8., 8.],- [8., 8.]]])-- expected = torch.tensor([[[0.0641, 0.07138],- [0.07138, 0.07138]],- data = torch.rand(3, 5, 5)++ data = torch.rand(3, 5, 5).to(device)- [[0.0569, 0.0588],- [0.0588, 0.0588]],-- [[0.4483, 0.4667],- [0.4667, 0.4667]]])-- data = data.to(device)- expected = expected.to(device)+ # OpenCV- data_cv = data.numpy().transpose(1, 2, 0)++ data_cv = data.cpu().detach().numpy().transpose(1, 2, 0)+ expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HLS)++ expected = torch.from_numpy(expected).to(device)+ h_expected = 2 * math.pi * expected[:, :, 0] / 360.+ l_expected = expected[:, :, 1]+ s_expected = expected[:, :, 2]-- # Kornia++f = kornia.color.RgbToHls()- assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-7)-- def test_batch_rgb_to_hls(self, device):+ result = f(data)- data = torch.tensor([[[21., 22.],- [22., 22.]],+ h = result[0, :, :]+ l = result[1, :, :]+ s = result[2, :, :]- [[13., 14.],- [14., 14.]],+ assert_allclose(h, h_expected)+ assert_allclose(l, l_expected)+ assert_allclose(s, s_expected)- [[8., 8.],- [8., 8.]]]) # 3x2x2+ def test_batch_rgb_to_hls(self):- expected = torch.tensor([[[0.0641, 0.07138],- [0.07138, 0.07138]],+ data = torch.rand(3, 5, 5)- [[0.0569, 0.0588],- [0.0588, 0.0588]],+ # OpenCV+ data_cv = data.numpy().transpose(1, 2, 0)+ expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HLS)- [[0.4483, 0.4667],- [0.4667, 0.4667]]]) # 3x2x2+ expected[:, :, 0] = 2 * math.pi * expected[:, :, 0] / 360.+ expected = expected.transpose(2, 0, 1)+ data = data.to(device)+ expected = expected.to(device)+- data = data.repeat(2, 1, 1, 1) # 2x3x2x2- expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2-+ # Korniaf = kornia.color.RgbToHls()- assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-7)- def test_gradcheck(self, device):+ data = data.repeat(2, 1, 1, 1) # 2x3x5x5-- expected = np.expand_dims(expected, 0)- expected = expected.repeat(2, 0) # 2x3x5x5++ expected = expected[None].repeat(2, 1, 1, 1) # 2x3x5x5+ assert_allclose(f(data), expected)- data = torch.tensor([[[[21., 22.],- [22., 22.]],-- [[13., 14.],- [14., 14.]],- def test_gradcheck(self):--- [[8., 8.],- [8., 8.]]]]) # 3x2x2- data = torch.rand(3, 5, 5) # 3x5x5++ def test_gradcheck(self, device):- data = data.to(device)++ data = torch.rand(3, 5, 5).to(device) # 3x5x5data = utils.tensor_to_gradcheck_var(data) # to varassert gradcheck(kornia.color.RgbToHls(), (data,),raise_exception=True)@pytest.mark.skip(reason="turn off all jit for a while")- def test_jit(self):+ def test_jit(self, device):@torch.jit.scriptdef op_script(data: torch.Tensor) -> torch.Tensor:--return kornia.rgb_to_hls(data)++data = torch.tensor([[[[21., 22.],[22., 22.]],@@@ -106,60 -91,72 +92,73 @@@class TestHlsToRgb:- def test_hls_to_rgb(self):+ def test_hls_to_rgb(self, device):- expected = torch.tensor([[[21., 22.],- [22., 22.]],- data = torch.rand(3, 5, 5)++ data = torch.rand(3, 5, 5).to(device)- [[13., 14.],- [14., 14.]],+ # OpenCV- data_cv = data.numpy().transpose(1, 2, 0).copy()++ data_cv = data.cpu().detach().numpy().transpose(1, 2, 0).copy()+ data_cv[:, :, 0] = 360 * data_cv[:, :, 0]+ expected = cv2.cvtColor(data_cv, cv2.COLOR_HLS2RGB)++ expected = torch.from_numpy(expected).to(device)- [[8., 8.],- [8., 8.]]])+ r_expected = expected[:, :, 0]+ g_expected = expected[:, :, 1]+ b_expected = expected[:, :, 2]- data = torch.tensor([[[0.0641, 0.07138],- [0.07138, 0.07138]],+ # Kornia+ f = kornia.color.HlsToRgb()+ data[0] = 2 * pi * data[0]+ result = f(data)- [[0.0569, 0.0588],- [0.0588, 0.0588]],+ r = result[0, :, :]+ g = result[1, :, :]+ b = result[2, :, :]- [[0.4483, 0.4667],- [0.4667, 0.4667]]])+ assert_allclose(r, r_expected)+ assert_allclose(g, g_expected)+ assert_allclose(b, b_expected)- f = kornia.color.HlsToRgb()- assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-7)+ def test_batch_hls_to_rgb(self):- def test_batch_hls_to_rgb(self, device):+ data = torch.rand(3, 5, 5) # 3x5x5- expected = torch.tensor([[[21., 22.],- [22., 22.]],+ # OpenCV+ data_cv = data.numpy().transpose(1, 2, 0).copy()+ data_cv[:, :, 0] = 360 * data_cv[:, :, 0]+ expected = cv2.cvtColor(data_cv, cv2.COLOR_HLS2RGB)- [[13., 14.],- [14., 14.]],+ expected = expected.transpose(2, 0, 1)- [[8., 8.],- [8., 8.]]]) # 3x2x2+ # Kornia+ f = kornia.color.HlsToRgb()- data = torch.tensor([[[0.0641, 0.07138],- [0.07138, 0.07138]],+ data[0] = 2 * pi * data[0]+ data = data.repeat(2, 1, 1, 1) # 2x3x5x5- [[0.0569, 0.0588],- [0.0588, 0.0588]],+ expected = np.expand_dims(expected, 0)+ expected = expected.repeat(2, 0) # 2x3x2x2- [[0.4483, 0.4667],- [0.4667, 0.4667]]]) # 3x2x2+ assert_allclose(f(data), expected)- data = data.to(device)- expected = expected.to(device)+ data[:, 0] += 2 * pi+ assert_allclose(f(data), expected)- data = data.repeat(2, 1, 1, 1) # 2x3x2x2- expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2+ data[:, 0] -= 4 * pi+ assert_allclose(f(data), expected)- f = kornia.color.HlsToRgb()- assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-7)+ def test_gradcheck(self):++ data = torch.rand(3, 5, 5) # 3x5x5+ data[0] = 2 * pi * data[0]++ data = utils.tensor_to_gradcheck_var(data) # to var++ assert gradcheck(kornia.color.HlsToRgb(), (data,),+ raise_exception=True)@pytest.mark.skip(reason="turn off all jit for a while")- def test_jit(self):+ def test_jit(self, device):@torch.jit.scriptdef op_script(data: torch.Tensor) -> torch.Tensor:return kornia.hls_to_rgb(data)diff --cc test/color/test_hsv.pyindex ec38df9b2,0aa0a9caf..b6b507b35--- a/test/color/test_hsv.py+++ b/test/color/test_hsv.py@@@ -1,9 -1,14 +1,14 @@@import pytestimport kornia-import kornia.testing as utils # test utils-from test.common import device_type+ from kornia.geometry import pi+import kornia.testing as utils # test utils+from test.common import device+ import cv2+ import numpy as np+ import math+import torchfrom torch.autograd import gradcheckfrom torch.testing import assert_allclose@@@ -11,73 -16,55 +16,58 @@@class TestRgbToHsv:- def test_rgb_to_hsv(self):+ def test_rgb_to_hsv(self, device):- data = torch.tensor([[[21., 22.],- [22., 22.]],-- [[13., 14.],- [14., 14.]],-- [[8., 8.],- [8., 8.]]])+ data = torch.rand(3, 5, 5)- expected = torch.tensor([[[0.0641, 0.0714],- [0.0714, 0.0714]],+ # OpenCV+ data_cv = data.numpy().transpose(1, 2, 0).copy()+ expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HSV)- [[0.6190, 0.6364],- [0.6364, 0.6364]],-- [[21.0000 / 255, 22.0000 / 255],- [22.0000 / 255, 22.0000 / 255]]])+ h_expected = 2 * math.pi * expected[:, :, 0] / 360.+ s_expected = expected[:, :, 1]+ v_expected = expected[:, :, 2]- # Kornia+ data = data.to(device)+ expected = expected.to(device)-++f = kornia.color.RgbToHsv()- assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-5)-- def test_batch_rgb_to_hsv(self, device):+ result = f(data)- data = torch.tensor([[[21., 22.],- [22., 22.]],+ h = result[0, :, :]+ s = result[1, :, :]+ v = result[2, :, :]- [[13., 14.],- [14., 14.]],+ assert_allclose(h, h_expected)+ assert_allclose(s, s_expected)+ assert_allclose(v, v_expected)- [[8., 8.],- [8., 8.]]]) # 3x2x2+ def test_batch_rgb_to_hls(self):- expected = torch.tensor([[[0.0641, 0.0714],- [0.0714, 0.0714]],+ data = torch.rand(3, 5, 5) # 3x5x5- [[0.6190, 0.6364],- [0.6364, 0.6364]],+ # OpenCV+ data_cv = data.numpy().transpose(1, 2, 0).copy()+ expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HSV)- [[21.0000 / 255, 22.0000 / 255],- [22.0000 / 255, 22.0000 / 255]]]) # 3x2x2-- data = data.to(device)- expected = expected.to(device)-- data = data.repeat(2, 1, 1, 1) # 2x3x2x2- expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2+ expected[:, :, 0] = 2 * math.pi * expected[:, :, 0] / 360.+ expected = expected.transpose(2, 0, 1)+ # Korniaf = kornia.color.RgbToHsv()- assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-5)- def test_gradcheck(self, device):+ data = data.repeat(2, 1, 1, 1) # 2x3x5x5++ expected = np.expand_dims(expected, 0)+ expected = expected.repeat(2, 0) # 2x3x5x5- data = torch.tensor([[[[21., 22.],- [22., 22.]],+ assert_allclose(f(data), expected)- [[13., 14.],- [14., 14.]],+ def test_gradcheck(self):- [[8., 8.],- [8., 8.]]]]) # 3x2x2+ data = torch.rand(3, 5, 5) # 3x2x2+ data = data.to(device)data = utils.tensor_to_gradcheck_var(data) # to varassert gradcheck(kornia.color.RgbToHsv(), (data,),@@@ -106,63 -92,72 +96,72 @@@class TestHsvToRgb:- def test_hsv_to_rgb(self):+ def test_hsv_to_rgb(self, device):- expected = torch.tensor([[[21., 22.],- [22., 22.]],+ data = torch.rand(3, 5, 5) # 3x5x5- [[13., 14.],- [14., 14.]],+ # OpenCV+ data_cv = data.numpy().transpose(1, 2, 0).copy()+ data_cv[:, :, 0] = 360 * data_cv[:, :, 0]+ expected = cv2.cvtColor(data_cv, cv2.COLOR_HSV2RGB)- [[8., 8.],- [8., 8.]]])+ r_expected = expected[:, :, 0]+ g_expected = expected[:, :, 1]+ b_expected = expected[:, :, 2]- data = torch.tensor([[[0.0641, 0.0714],- [0.0714, 0.0714]],+ # Kornia+ f = kornia.color.HsvToRgb()+ data[0] = 2 * pi * data[0]+ result = f(data)- [[0.6190, 0.6364],- [0.6364, 0.6364]],+ r = result[0, :, :]+ g = result[1, :, :]+ b = result[2, :, :]- [[21.0000 / 255, 22.0000 / 255],- [22.0000 / 255, 22.0000 / 255]]])+ assert_allclose(r, r_expected)+ assert_allclose(g, g_expected)+ assert_allclose(b, b_expected)- data = data.to(device)- expected = expected.to(device)+ def test_batch_hsv_to_rgb(self):- f = kornia.color.HsvToRgb()- assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-3)+ data = torch.rand(3, 5, 5) # 3x5x5- def test_batch_hsv_to_rgb(self, device):+ # OpenCV+ data_cv = data.numpy().transpose(1, 2, 0).copy()+ data_cv[:, :, 0] = 360 * data_cv[:, :, 0]+ expected = cv2.cvtColor(data_cv, cv2.COLOR_HSV2RGB)- expected = torch.tensor([[[21., 22.],- [22., 22.]],+ expected = expected.transpose(2, 0, 1)- [[13., 14.],- [14., 14.]],+ # Kornia+ f = kornia.color.HsvToRgb()- [[8., 8.],- [8., 8.]]]) # 3x2x2+ data[0] = 2 * pi * data[0]+ data = data.repeat(2, 1, 1, 1) # 2x3x5x5- data = torch.tensor([[[0.0641, 0.0714],- [0.0714, 0.0714]],+ expected = np.expand_dims(expected, 0)+ expected = expected.repeat(2, 0) # 2x3x5x5- [[0.6190, 0.6364],- [0.6364, 0.6364]],+ assert_allclose(f(data), expected)- [[21.0000 / 255, 22.0000 / 255],- [22.0000 / 255, 22.0000 / 255]]]) # 3x2x2+ data[:, 0] += 2 * pi+ assert_allclose(f(data), expected)- data = data.to(device)- expected = expected.to(device)+ data[:, 0] -= 4 * pi+ assert_allclose(f(data), expected)- data = data.repeat(2, 1, 1, 1) # 2x3x2x2- expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2+ def test_gradcheck(self):- f = kornia.color.HsvToRgb()- assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-3)+ data = torch.rand(3, 5, 5) # 3x5x5+ data[0] = 2 * pi * data[0]++ data = utils.tensor_to_gradcheck_var(data) # to var++ assert gradcheck(kornia.color.HsvToRgb(), (data,),+ raise_exception=True)@pytest.mark.skip(reason="turn off all jit for a while")- def test_jit(self):+ def test_jit(self, device):@torch.jit.scriptdef op_script(data: torch.Tensor) -> torch.Tensor:return kornia.hsv_to_rgb(data)