π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Add typing
Pauc4764f988f80
diff --git a/kornia/color/hls.py b/kornia/color/hls.pyindex c74b511cb..74eedde01 100644--- a/kornia/color/hls.py+++ b/kornia/color/hls.py@@ -37,7 +37,7 @@ class HlsToRgb(nn.Module):return hls_to_rgb(image)-def hls_to_rgb(image):+def hls_to_rgb(image: torch.Tensor) -> torch.Tensor:r"""Convert an HLS image to RGBThe image data is assumed to be in the range of (0, 1).@@ -64,13 +64,16 @@ def hls_to_rgb(image):kr = (0 + h / 30) % 12kg = (8 + h / 30) % 12kb = (4 + h / 30) % 12- a = s * torch.min(l, 1 - l)+ a = s * torch.min(l, torch.tensor(1.) - l)ones_k = torch.ones_like(kr)- fr = l - a * torch.max(torch.min(torch.min(kr - 3, 9 - kr), ones_k), -1 * ones_k)- fg = l - a * torch.max(torch.min(torch.min(kg - 3, 9 - kg), ones_k), -1 * ones_k)- fb = l - a * torch.max(torch.min(torch.min(kb - 3, 9 - kb), ones_k), -1 * ones_k)+ fr: torch.Tensor = l - a * torch.max(torch.min(torch.min(kr - torch.tensor(3.),+ torch.tensor(9.) - kr), ones_k), -1 * ones_k)+ fg: torch.Tensor = l - a * torch.max(torch.min(torch.min(kg - torch.tensor(3.),+ torch.tensor(9.) - kg), ones_k), -1 * ones_k)+ fb: torch.Tensor = l - a * torch.max(torch.min(torch.min(kb - torch.tensor(3.),+ torch.tensor(9.) - kb), ones_k), -1 * ones_k)out: torch.Tensor = torch.stack([fr, fg, fb], dim=-3)@@ -106,7 +109,7 @@ class RgbToHls(nn.Module):return rgb_to_hls(image)-def rgb_to_hls(image):+def rgb_to_hls(image: torch.Tensor) -> torch.Tensor:r"""Convert an RGB image to HLSThe image data is assumed to be in the range of (0, 1).@@ -139,7 +142,8 @@ def rgb_to_hls(image):deltac: torch.Tensor = maxc - minc- s: torch.Tensor = torch.where(l < 0.5, deltac / (maxc + minc), deltac / (2 - (maxc + minc))) # saturation+ s: torch.Tensor = torch.where(l < 0.5, deltac / (maxc + minc), deltac /+ (torch.tensor(2.) - (maxc + minc))) # saturationhi: torch.Tensor = torch.zeros_like(deltac)diff --git a/kornia/color/hsv.py b/kornia/color/hsv.pyindex a83c8bc2b..b06081a51 100644--- a/kornia/color/hsv.py+++ b/kornia/color/hsv.py@@ -32,7 +32,7 @@ class HsvToRgb(nn.Module):return hsv_to_rgb(image)-def hsv_to_rgb(image):+def hsv_to_rgb(image: torch.Tensor) -> torch.Tensor:r"""Convert an HSV image to RGBThe image data is assumed to be in the range of (0, 1).@@ -58,18 +58,18 @@ def hsv_to_rgb(image):hi: torch.Tensor = torch.floor(h * 6) % 6f: torch.Tensor = ((h * 6) % 6) - hi- p: torch.Tensor = v * (1 - s)- q: torch.Tensor = v * (1 - f * s)- t: torch.Tensor = v * (1 - (1 - f) * s)+ p: torch.Tensor = v * (torch.tensor(1.) - s)+ q: torch.Tensor = v * (torch.tensor(1.) - f * s)+ t: torch.Tensor = v * (torch.tensor(1.) - (torch.tensor(1.) - f) * s)out: torch.Tensor = torch.stack([hi, hi, hi], dim=-3)- out[out == 0]: torch.Tensor = torch.stack((v, t, p), dim=-3)[out == 0]- out[out == 1]: torch.Tensor = torch.stack((q, v, p), dim=-3)[out == 1]- out[out == 2]: torch.Tensor = torch.stack((p, v, t), dim=-3)[out == 2]- out[out == 3]: torch.Tensor = torch.stack((p, q, v), dim=-3)[out == 3]- out[out == 4]: torch.Tensor = torch.stack((t, p, v), dim=-3)[out == 4]- out[out == 5]: torch.Tensor = torch.stack((v, p, q), dim=-3)[out == 5]+ out[out == 0] = torch.stack((v, t, p), dim=-3)[out == 0]+ out[out == 1] = torch.stack((q, v, p), dim=-3)[out == 1]+ out[out == 2] = torch.stack((p, v, t), dim=-3)[out == 2]+ out[out == 3] = torch.stack((p, q, v), dim=-3)[out == 3]+ out[out == 4] = torch.stack((t, p, v), dim=-3)[out == 4]+ out[out == 5] = torch.stack((v, p, q), dim=-3)[out == 5]return out@@ -104,7 +104,7 @@ class RgbToHsv(nn.Module):return rgb_to_hsv(image)-def rgb_to_hsv(image):+def rgb_to_hsv(image: torch.Tensor) -> torch.Tensor:r"""Convert an RGB image to HSV.Args:@@ -137,7 +137,7 @@ def rgb_to_hsv(image):s[torch.isnan(s)] = 0.# avoid division by zero- deltac: torch.Tensor = torch.where(+ deltac = torch.where(deltac == 0, torch.ones_like(deltac), deltac)rc: torch.Tensor = (maxc - r) / deltac@@ -148,11 +148,11 @@ def rgb_to_hsv(image):maxr: torch.Tensor = r == maxch: torch.Tensor = 4.0 + gc - rc- h[maxg]: torch.Tensor = 2.0 + rc[maxg] - bc[maxg]- h[maxr]: torch.Tensor = bc[maxr] - gc[maxr]- h[minc == maxc]: torch.Tensor = 0.0+ h[maxg] = 2.0 + rc[maxg] - bc[maxg]+ h[maxr] = bc[maxr] - gc[maxr]+ h[minc == maxc] = 0.0- h: torch.Tensor = (h / 6.0) % 1.0+ h = (h / 6.0) % 1.0- h: torch.Tensor = 2 * pi * h+ h = 2 * pi * hreturn torch.stack([h, s, v], dim=-3)diff --git a/test/color/test_hls.py b/test/color/test_hls.pyindex e4e29fd0b..bd5070217 100644--- a/test/color/test_hls.py+++ b/test/color/test_hls.py@@ -16,7 +16,7 @@ class TestRgbToHls:def test_rgb_to_hls(self):- data = torch.Tensor([[[0.4237059, 0.1935902, 0.8585021, 0.3790484, 0.1389151],+ data = torch.tensor([[[0.4237059, 0.1935902, 0.8585021, 0.3790484, 0.1389151],[0.5933651, 0.0474544, 0.2801555, 0.1691061, 0.9221829],[0.2351739, 0.5852075, 0.5789326, 0.8411915, 0.5960411],[0.0290176, 0.6459382, 0.8581501, 0.4755400, 0.7735767],@@ -35,17 +35,17 @@ class TestRgbToHls:[0.2281681, 0.2560037, 0.5134379, 0.5800887, 0.8685090]]])# OpenCV- h_expected = torch.Tensor([[4.5945477, 4.268469, 0.9738468, 2.2731707, 3.269344],+ h_expected = torch.tensor([[4.5945477, 4.268469, 0.9738468, 2.2731707, 3.269344],[0.6149417, 3.8969188, 2.292972, 3.7777472, 0.9459598],[4.003296, 5.4079432, 4.566101, 5.869351, 1.8194631],[3.2098956, 4.271444, 0.29820946, 4.7041655, 0.7340856],[0.78329855, 2.2872903, 5.3016634, 5.634379, 3.382815]])- l_expected = torch.Tensor([[0.48054275, 0.51843, 0.44070444, 0.5224365, 0.33946657],+ l_expected = torch.tensor([[0.48054275, 0.51843, 0.44070444, 0.5224365, 0.33946657],[0.2985475, 0.18669781, 0.45586777, 0.5633283, 0.5373864],[0.6006086, 0.41335985, 0.5978296, 0.45972168, 0.6845894],[0.38564888, 0.80442977, 0.6957935, 0.48032972, 0.6066474],[0.5889454, 0.53727096, 0.2848544, 0.5161035, 0.7544351]])- s_expected = torch.Tensor([[0.5255313, 0.79561585, 0.9480225, 0.30024928, 0.59078425],+ s_expected = torch.tensor([[0.5255313, 0.79561585, 0.9480225, 0.30024928, 0.59078425],[0.98750657, 0.7458223, 0.3854456, 0.90278864, 0.8317882],[0.9149786, 0.4157338, 0.16817844, 0.82978433, 0.5911325],[0.9247565, 0.9623155, 0.53370523, 0.63488615, 0.4243758],@@ -65,7 +65,7 @@ class TestRgbToHls:def test_batch_rgb_to_hls(self):- data = torch.Tensor([[[0.4237059, 0.1935902, 0.8585021, 0.3790484, 0.1389151],+ data = torch.tensor([[[0.4237059, 0.1935902, 0.8585021, 0.3790484, 0.1389151],[0.5933651, 0.0474544, 0.2801555, 0.1691061, 0.9221829],[0.2351739, 0.5852075, 0.5789326, 0.8411915, 0.5960411],[0.0290176, 0.6459382, 0.8581501, 0.4755400, 0.7735767],@@ -84,7 +84,7 @@ class TestRgbToHls:[0.2281681, 0.2560037, 0.5134379, 0.5800887, 0.8685090]]])# OpenCV- expected = torch.Tensor([[[4.5945477, 4.268469, 0.9738468, 2.2731707, 3.269344],+ expected = torch.tensor([[[4.5945477, 4.268469, 0.9738468, 2.2731707, 3.269344],[0.6149417, 3.8969188, 2.292972, 3.7777472, 0.9459598],[4.003296, 5.4079432, 4.566101, 5.869351, 1.8194631],[3.2098956, 4.271444, 0.29820946, 4.7041655, 0.7340856],@@ -135,7 +135,7 @@ class TestRgbToHls:@pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self):@torch.jit.script- def op_script(data: torch.Tensor) -> torch.Tensor:+ def op_script(data: torch.tensor) -> torch.Tensor:return kornia.rgb_to_hls(data)data = torch.tensor([[[[21., 22.],@@ -156,7 +156,7 @@ class TestHlsToRgb:def test_hls_to_rgb(self):- data = torch.Tensor([[[0.5513626, 0.8487718, 0.1822479, 0.2851745, 0.2669488],+ data = torch.tensor([[[0.5513626, 0.8487718, 0.1822479, 0.2851745, 0.2669488],[0.7596772, 0.4565057, 0.6181599, 0.3852497, 0.7746902],[0.5742747, 0.1957062, 0.7530835, 0.2104362, 0.9449323],[0.9918052, 0.2437515, 0.4718738, 0.8502576, 0.1675640],@@ -175,17 +175,17 @@ class TestHlsToRgb:[0.1058410, 0.0816052, 0.5792874, 0.9578886, 0.6281684]]]) # 3x5x5# OpenCV- r_expected = torch.Tensor([[0.21650219, 0.01911971, 0.91374826, 0.1760952, 0.10979544],+ r_expected = torch.tensor([[0.21650219, 0.01911971, 0.91374826, 0.1760952, 0.10979544],[0.6569808, 0.02984191, 0.77314806, 0.3807273, 0.25964087],[0.7321301, 0.8110298, 0.4724091, 0.96834683, 0.2910835],[0.935407, 0.6478001, 0.615513, 0.3506698, 0.89171433],[0.0945498, 0.6919248, 0.98795897, 0.25782573, 0.08763295]])- g_expected = torch.Tensor([[0.48587522, 0.017571, 0.9437648, 0.5853925, 0.13439366],+ g_expected = torch.tensor([[0.48587522, 0.017571, 0.9437648, 0.5853925, 0.13439366],[0.30897713, 0.18483935, 0.8082967, 0.75936294, 0.25883088],[0.7542145, 0.97218925, 0.46058673, 0.9910847, 0.03697497],[0.8592784, 0.675717, 0.8979618, 0.28124255, 0.8925654],[0.07645091, 0.6548674, 0.99254686, 0.500144, 0.38372523]])- b_expected = torch.Tensor([[0.60586834, 0.01897625, 0.62269044, 0.00976634, 0.09351197],+ b_expected = torch.tensor([[0.60586834, 0.01897625, 0.62269044, 0.00976634, 0.09351197],[0.93256867, 0.14439031, 0.89391685, 0.49867177, 0.2600806],[0.7719684, 0.04724807, 0.48338777, 0.904503, 0.12093388],[0.8630215, 0.6153573, 0.85029656, 0.34361976, 0.73449594],@@ -206,7 +206,7 @@ class TestHlsToRgb:def test_batch_hls_to_rgb(self):- data = torch.Tensor([[[0.5513626, 0.8487718, 0.1822479, 0.2851745, 0.2669488],+ data = torch.tensor([[[0.5513626, 0.8487718, 0.1822479, 0.2851745, 0.2669488],[0.7596772, 0.4565057, 0.6181599, 0.3852497, 0.7746902],[0.5742747, 0.1957062, 0.7530835, 0.2104362, 0.9449323],[0.9918052, 0.2437515, 0.4718738, 0.8502576, 0.1675640],@@ -225,7 +225,7 @@ class TestHlsToRgb:[0.1058410, 0.0816052, 0.5792874, 0.9578886, 0.6281684]]]) # 3x5x5# OpenCV- expected = torch.Tensor([[[0.21650219, 0.01911971, 0.91374826, 0.1760952, 0.10979544],+ expected = torch.tensor([[[0.21650219, 0.01911971, 0.91374826, 0.1760952, 0.10979544],[0.6569808, 0.02984191, 0.77314806, 0.3807273, 0.25964087],[0.7321301, 0.8110298, 0.4724091, 0.96834683, 0.2910835],[0.935407, 0.6478001, 0.615513, 0.3506698, 0.89171433],@@ -272,7 +272,7 @@ class TestHlsToRgb:@pytest.mark.skip(reason="turn off all jit for a while")def test_jit(self):@torch.jit.script- def op_script(data: torch.Tensor) -> torch.Tensor:+ def op_script(data: torch.tensor) -> torch.Tensor:return kornia.hls_to_rgb(data)data = torch.tensor([[[[21., 22.],diff --git a/test/color/test_hsv.py b/test/color/test_hsv.pyindex 904d91000..8d611bbba 100644--- a/test/color/test_hsv.py+++ b/test/color/test_hsv.py@@ -16,7 +16,7 @@ class TestRgbToHsv:def test_rgb_to_hsv(self):- data = torch.Tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],+ data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],[0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],[0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],[0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],@@ -35,17 +35,17 @@ class TestRgbToHsv:[0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])# OpenCV- h_expected = torch.Tensor([[1.6519808, 1.3188975, 2.2487938, 3.582216, 2.250954],+ h_expected = torch.tensor([[1.6519808, 1.3188975, 2.2487938, 3.582216, 2.250954],[4.28164, 0.04868213, 0.83454597, 5.533617, 4.319574],[3.4185164, 2.7919037, 2.8883224, 1.7474692, 1.3619272],[3.6837196, 0.6378961, 5.7213116, 5.2614374, 6.259687],[2.929221, 2.5614352, 0.97840965, 1.5729411, 6.0235224]])- s_expected = torch.Tensor([[0.46999356, 0.52820253, 0.8132473, 0.65267974, 0.899411],+ s_expected = torch.tensor([[0.46999356, 0.52820253, 0.8132473, 0.65267974, 0.899411],[0.9497089, 0.534381, 0.48878422, 0.60298723, 0.9163612],[0.6343409, 0.87112963, 0.8101612, 0.9500878, 0.8192622],[0.99010557, 0.9023306, 0.42042294, 0.8292772, 0.81847864],[0.6755719, 0.8493871, 0.93686795, 0.73741645, 0.40461043]])- v_expected = torch.Tensor([[0.5414237, 0.99627006, 0.89471555, 0.81350225, 0.9483274],+ v_expected = torch.tensor([[0.5414237, 0.99627006, 0.89471555, 0.81350225, 0.9483274],[0.930656, 0.80207086, 0.8933256, 0.9170977, 0.75575125],[0.74157417, 0.86247796, 0.41260356, 0.76009345, 0.7279997],[0.86927235, 0.8288748, 0.9647092, 0.892202, 0.7614344],@@ -65,7 +65,7 @@ class TestRgbToHsv:def test_batch_rgb_to_hls(self):- data = torch.Tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],+ data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],[0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],[0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],[0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],@@ -84,7 +84,7 @@ class TestRgbToHsv:[0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])# OpenCV- expected = torch.Tensor([[[1.6519808, 1.3188975, 2.2487938, 3.582216, 2.250954],+ expected = torch.tensor([[[1.6519808, 1.3188975, 2.2487938, 3.582216, 2.250954],[4.28164, 0.04868213, 0.83454597, 5.533617, 4.319574],[3.4185164, 2.7919037, 2.8883224, 1.7474692, 1.3619272],[3.6837196, 0.6378961, 5.7213116, 5.2614374, 6.259687],@@ -157,7 +157,7 @@ class TestHsvToRgb:def test_hsv_to_rgb(self):- data = torch.Tensor([[[3.5433271, 5.6390061, 1.3766849, 2.5384088, 4.6848912],+ data = torch.tensor([[[3.5433271, 5.6390061, 1.3766849, 2.5384088, 4.6848912],[5.7209363, 5.3262630, 6.2059994, 4.1164689, 2.3872600],[0.6370091, 3.6186798, 5.9170871, 2.8275447, 5.4289737],[0.2751994, 1.6632686, 1.0049511, 0.7046204, 1.3791083],@@ -176,17 +176,17 @@ class TestHsvToRgb:[0.3236262, 0.0999796, 0.3614958, 0.5117705, 0.8194097]]]) # 3x5x5# OpenCV- r_expected = torch.Tensor([[0.4213259, 0.93021995, 0.26564622, 0.58528465, 0.5338429],+ r_expected = torch.tensor([[0.4213259, 0.93021995, 0.26564622, 0.58528465, 0.5338429],[0.48333693, 0.20884734, 0.11692339, 0.45538613, 0.32238087],[0.2713015, 0.2108461, 0.60153013, 0.15604737, 0.23222584],[0.5704236, 0.4568531, 0.4755683, 0.48112088, 0.49611038],[0.32362622, 0.09981924, 0.20394461, 0.42567685, 0.81940967]])- g_expected = torch.Tensor([[0.6838029, 0.0508271, 0.3265766, 0.794457, 0.3807702],+ g_expected = torch.tensor([[0.6838029, 0.0508271, 0.3265766, 0.794457, 0.3807702],[0.18359877, 0.0916698, 0.00405421, 0.45823452, 0.6448684],[0.20682439, 0.41690278, 0.1938166, 0.68015575, 0.0917114],[0.33933756, 0.6797268, 0.4665822, 0.44541004, 0.5317836],[0.27101707, 0.09975589, 0.18400209, 0.51177055, 0.30095676]])- b_expected = torch.Tensor([[0.84716797, 0.5917818, 0.13292392, 0.6739741, 0.7038843],+ b_expected = torch.tensor([[0.84716797, 0.5917818, 0.13292392, 0.6739741, 0.7038843],[0.34453064, 0.19874583, 0.01237347, 0.4966302, 0.41256943],[0.10669357, 0.589338, 0.3363524, 0.5229789, 0.20633064],[0.25696078, 0.30088606, 0.25282317, 0.37195927, 0.41923255],@@ -206,7 +206,7 @@ class TestHsvToRgb:def test_batch_hsv_to_rgb(self):- data = torch.Tensor([[[3.5433271, 5.6390061, 1.3766849, 2.5384088, 4.6848912],+ data = torch.tensor([[[3.5433271, 5.6390061, 1.3766849, 2.5384088, 4.6848912],[5.7209363, 5.3262630, 6.2059994, 4.1164689, 2.3872600],[0.6370091, 3.6186798, 5.9170871, 2.8275447, 5.4289737],[0.2751994, 1.6632686, 1.0049511, 0.7046204, 1.3791083],@@ -225,7 +225,7 @@ class TestHsvToRgb:[0.3236262, 0.0999796, 0.3614958, 0.5117705, 0.8194097]]]) # 3x5x5# OpenCV- expected = torch.Tensor([[[0.4213259, 0.93021995, 0.26564622, 0.58528465, 0.5338429],+ expected = torch.tensor([[[0.4213259, 0.93021995, 0.26564622, 0.58528465, 0.5338429],[0.48333693, 0.20884734, 0.11692339, 0.45538613, 0.32238087],[0.2713015, 0.2108461, 0.60153013, 0.15604737, 0.23222584],[0.5704236, 0.4568531, 0.4755683, 0.48112088, 0.49611038],