Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add hls tests

HassanAlsamahia38ff5375925
diff --git a/kornia/color/hls.py b/kornia/color/hls.py
index 59b9361a6..5f959ebca 100644
--- a/kornia/color/hls.py
+++ b/kornia/color/hls.py
@@ -18,6 +18,12 @@ class HlsToRgb(nn.Module):
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 3, H, W)`
+ Examples::
+
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> rgb = kornia.color.HlsToRgb()
+ >>> output = rgb(input) # 2x3x4x5
+
"""
def __init__(self) -> None:
@@ -52,24 +58,26 @@ def hls_to_rgb(image):
s: torch.Tensor = image[..., 2, :, :]
c: torch.Tensor = (1 - torch.abs((2*l)-1)) * s #chroma
- hi: torch.Tensor = h/60
+
+ hi: torch.Tensor = (h/60)
x: torch.Tensor = c * (1 - torch.abs(hi%2 - 1))
- r: torch.Tensor = torch.zeros_like(h)
+ r: torch.Tensor = torch.zeros_like(l)
g: torch.Tensor = torch.zeros_like(l)
- b: torch.Tensor = torch.zeros_like(s)
+ b: torch.Tensor = torch.zeros_like(l)
- r[(hi>=0)&(hi<=1)], g[(hi>=0)&(hi<=1)], b[(hi>=0)&(hi<=1)] = c[(hi>=0)&(hi<=1)], x[(hi>=0)&(hi<=1)], 0
- r[(hi>=1)&(hi<=2)], g[(hi>=1)&(hi<=2)], b[(hi>=1)&(hi<=2)] = x[(hi>=1)&(hi<=2)], c[(hi>=1)&(hi<=2)], 0
- r[(hi>=2)&(hi<=3)], g[(hi>=2)&(hi<=3)], b[(hi>=2)&(hi<=3)] = 0, c[(hi>=2)&(hi<=3)], x[(hi>=2)&(hi<=3)]
- r[(hi>=3)&(hi<=4)], g[(hi>=3)&(hi<=4)], b[(hi>=3)&(hi<=4)] = 0, x[(hi>=3)&(hi<=4)], c[(hi>=3)&(hi<=4)]
- r[(hi>=4)&(hi<=5)], g[(hi>=4)&(hi<=5)], b[(hi>=4)&(hi<=5)] = x[(hi>=4)&(hi<=5)], 0, c[(hi>=4)&(hi<=5)]
- r[(hi>=5)&(hi<=6)], g[(hi>=5)&(hi<=6)], b[(hi>=5)&(hi<=6)] = c[(hi>=5)&(hi<=6)], 0, x[(hi>=5)&(hi<=6)]
+ r[(hi>=0)&(hi<=1)], g[(hi>=0)&(hi<=1)], b[(hi>=0)&(hi<=1)] = c[(hi>=0)&(hi<=1)], x[(hi>=0)&(hi<=1)], 0
+ r[(hi>=1)&(hi<=2)], g[(hi>=1)&(hi<=2)], b[(hi>=1)&(hi<=2)] = x[(hi>=1)&(hi<=2)], c[(hi>=1)&(hi<=2)], 0
+ r[(hi>=2)&(hi<=3)], g[(hi>=2)&(hi<=3)], b[(hi>=2)&(hi<=3)] = 0 , c[(hi>=2)&(hi<=3)], x[(hi>=2)&(hi<=3)]
+ r[(hi>=3)&(hi<=4)], g[(hi>=3)&(hi<=4)], b[(hi>=3)&(hi<=4)] = 0 , x[(hi>=3)&(hi<=4)], c[(hi>=3)&(hi<=4)]
+ r[(hi>=4)&(hi<=5)], g[(hi>=4)&(hi<=5)], b[(hi>=4)&(hi<=5)] = x[(hi>=4)&(hi<=5)], 0 , c[(hi>=4)&(hi<=5)]
+ r[(hi>=5)&(hi<=6)], g[(hi>=5)&(hi<=6)], b[(hi>=5)&(hi<=6)] = c[(hi>=5)&(hi<=6)], 0 , x[(hi>=5)&(hi<=6)]
m: torch.Tensor = l - c/2
- r: torch.Tensor , g: torch.Tensor , b: torch.Tensor = (r+m), (g+m), (b+m)
+ r , g , b = (r+m), (g+m), (b+m)
+
+ out: torch.Tensor = torch.stack([r, g, b],dim=-3)
- out = torch.stack([r,g,b],dim=-3)
return out
@@ -87,6 +95,12 @@ class RgbToHls(nn.Module):
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 3, H, W)`
+ Examples::
+
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> hsv = kornia.color.RgbToHls()
+ >>> output = hsv(input) # 2x3x4x5
+
"""
def __init__(self) -> None:
@@ -96,7 +110,6 @@ class RgbToHls(nn.Module):
return rgb_to_hls(image)
-
def rgb_to_hls(image):
r"""Convert an RGB image to HLS
The image data is assumed to be in the range of (0, 1).
@@ -130,22 +143,14 @@ def rgb_to_hls(image):
l: torch.Tensor = (maxc+minc)/2 # luminance
deltac: torch.Tensor = maxc - minc
- s: torch.Tensor = torch.zeros((image.shape[0],image.shape[2],image.shape[3]),dtype=deltac.dtype)
+ s: torch.Tensor = torch.zeros_like(deltac)
s[l<0.5]: torch.Tensor = (deltac[l<0.5]) / (maxc[l<0.5] + minc[l<0.5])
s[l>=0.5]: torch.Tensor = (deltac[l>=0.5]) / (2 - (maxc[l>=0.5] + minc[l>=0.5])) #Saturation
- rc: torch.Tensor = (maxc - r) / deltac
- gc: torch.Tensor = (maxc - g) / deltac
- bc: torch.Tensor = (maxc - b) / deltac
-
- maxg: torch.Tensor = g == maxc
- maxr: torch.Tensor = r == maxc
-
- h: 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: torch.Tensor = (h / 6.0) % 1.0 #Hue
+ hi = torch.zeros_like(deltac)
+ hi[Imax==0] = ((g[Imax==0] - b[Imax==0])/deltac[Imax==0]) % 6
+ hi[Imax==1] = ((b[Imax==1] - r[Imax==1])/deltac[Imax==1]) + 2
+ hi[Imax==2] = ((r[Imax==2] - g[Imax==2])/deltac[Imax==2]) + 4
+ h = (60 * hi)/360
return torch.stack([h, l, s], dim=-3)
diff --git a/test/color/test_hls.py b/test/color/test_hls.py
new file mode 100644
index 000000000..92548cb62
--- /dev/null
+++ b/test/color/test_hls.py
@@ -0,0 +1,164 @@
+import pytest
+
+import kornia
+import kornia.testing as utils # test utils
+from test.common import device_type
+
+import torch
+from torch.autograd import gradcheck
+from torch.testing import assert_allclose
+
+
+class TestRgbToHls:
+
+ def test_rgb_to_hls(self):
+
+ 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]],
+
+ [[0.0569, 0.0588],
+ [0.0588, 0.0588]],
+
+ [[0.4483, 0.4667],
+ [0.4667, 0.4667]]])
+
+ f = kornia.color.RgbToHls()
+ assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-7)
+
+ def test_batch_rgb_to_hls(self):
+
+ data = torch.tensor([[[21., 22.],
+ [22., 22.]],
+
+ [[13., 14.],
+ [14., 14.]],
+
+ [[8., 8.],
+ [8., 8.]]]) # 3x2x2
+
+ expected = torch.tensor([[[0.0641, 0.07138],
+ [0.07138, 0.07138]],
+
+ [[0.0569, 0.0588],
+ [0.0588, 0.0588]],
+
+ [[0.4483, 0.4667],
+ [0.4667, 0.4667]]]) # 3x2x2
+ f = kornia.color.RgbToHls()
+ data = data.repeat(2, 1, 1, 1) # 2x3x2x2
+ expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
+ assert_allclose(f(data / 255), expected, atol=1e-4, rtol=1e-7)
+
+ def test_gradcheck(self):
+
+ data = torch.tensor([[[[21., 22.],
+ [22., 22.]],
+
+ [[13., 14.],
+ [14., 14.]],
+
+ [[8., 8.],
+ [8., 8.]]]]) # 3x2x2
+
+ data = utils.tensor_to_gradcheck_var(data) # to var
+
+ assert gradcheck(kornia.color.RgbToHls(), (data,),
+ raise_exception=True)
+
+ @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:
+
+ return kornia.rgb_to_hls(data)
+ data = torch.tensor([[[[21., 22.],
+ [22., 22.]],
+
+ [[13., 14.],
+ [14., 14.]],
+
+ [[8., 8.],
+ [8., 8.]]]]) # 3x2x2
+
+ actual = op_script(data)
+ expected = kornia.rgb_to_hls(data)
+ assert_allclose(actual, expected)
+
+
+class TestHlsToRgb:
+
+ def test_hls_to_rgb(self):
+
+ expected = torch.tensor([[[21., 22.],
+ [22., 22.]],
+
+ [[13., 14.],
+ [14., 14.]],
+
+ [[8., 8.],
+ [8., 8.]]])
+
+ data = torch.tensor( [[[0.0641, 0.07138],
+ [0.07138, 0.07138]],
+
+ [[0.0569, 0.0588],
+ [0.0588, 0.0588]],
+
+ [[0.4483, 0.4667],
+ [0.4667, 0.4667]]])
+
+ f = kornia.color.HlsToRgb()
+ assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-7)
+
+ def test_batch_hls_to_rgb(self):
+
+ expected = torch.tensor([[[21., 22.],
+ [22., 22.]],
+
+ [[13., 14.],
+ [14., 14.]],
+
+ [[8., 8.],
+ [8., 8.]]]) # 3x2x2
+
+ data = torch.tensor([[[0.0641, 0.07138],
+ [0.07138, 0.07138]],
+
+ [[0.0569, 0.0588],
+ [0.0588, 0.0588]],
+
+ [[0.4483, 0.4667],
+ [0.4667, 0.4667]]]) # 3x2x2
+
+ f = kornia.color.HlsToRgb()
+ data = data.repeat(2, 1, 1, 1) # 2x3x2x2
+ expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
+ assert_allclose(f(data), expected / 255, atol=1e-3, rtol=1e-7)
+
+ @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:
+ return kornia.hls_to_rgb(data)
+
+ data = torch.tensor([[[[21., 22.],
+ [22., 22.]],
+
+ [[13., 14.],
+ [14., 14.]],
+
+ [[8., 8.],
+ [8., 8.]]]]) # 3x2x2
+
+ actual = op_script(data)
+ expected = kornia.hls_to_rgb(data)
+ assert_allclose(actual, expected)