π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
HSV and HSL nan tests corrected. Always generate nan on both cases. Remove eps in HSV
Pau19b4fcbf6bb6
diff --git a/kornia/color/hls.py b/kornia/color/hls.pyindex 611f1ee2c..c74b511cb 100644--- a/kornia/color/hls.py+++ b/kornia/color/hls.py@@ -150,6 +150,7 @@ def rgb_to_hls(image):h: torch.Tensor = 2. * pi * (60. * hi) / 360. # hue [0, 2*pi]image_hls: torch.Tensor = torch.stack([h, l, s], dim=-3)+image_hls[torch.isnan(image_hls)] = 0.return image_hlsdiff --git a/kornia/color/hsv.py b/kornia/color/hsv.pyindex d34d5d38f..a83c8bc2b 100644--- a/kornia/color/hsv.py+++ b/kornia/color/hsv.py@@ -132,7 +132,9 @@ def rgb_to_hsv(image):v: torch.Tensor = maxc # brightnessdeltac: torch.Tensor = maxc - minc- s: torch.Tensor = deltac / (v + 1e-7) # saturation (avoid division by zero)+ s: torch.Tensor = deltac / v++ s[torch.isnan(s)] = 0.# avoid division by zerodeltac: torch.Tensor = torch.where(diff --git a/test/color/test_hls.py b/test/color/test_hls.pyindex 9b337853f..31406578a 100644--- a/test/color/test_hls.py+++ b/test/color/test_hls.py@@ -60,6 +60,23 @@ class TestRgbToHls:expected = expected.repeat(2, 0) # 2x3x5x5assert_allclose(f(data), expected)+ def test_nan_rgb_to_hls(self):++ data = torch.rand(1, 5, 5) # 3x5x5+ data = data.repeat(3, 1, 1) # 2x3x5x5++ # OpenCV+ data_cv = data.numpy().transpose(1, 2, 0).copy()+ expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HLS)+ expected[:, :, 0] = 2 * math.pi * expected[:, :, 0] / 360.++ expected = expected.transpose(2, 0, 1)++ # Kornia+ f = kornia.color.RgbToHls()++ assert_allclose(f(data), expected)+def test_gradcheck(self):data = torch.rand(3, 5, 5) # 3x5x5@@ -173,18 +190,3 @@ class TestHlsToRgb:actual = op_script(data)expected = kornia.hls_to_rgb(data)assert_allclose(actual, expected)-- def test_nan(self):- np.random.seed(0)-- img = (np.random.randint(0, 256, [1000, 1000, 3], np.uint8) / 255.0).astype(np.float32)-- cv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)-- k_img = torch.from_numpy(np.transpose(img, [2, 0, 1]))-- k_img = kornia.rgb_to_hls(k_img.float())- k_img[0] = k_img[0] * 360 / (2 * pi)- k_img = np.transpose(k_img.numpy(), [1, 2, 0])-- assert_allclose(cv_img, k_img)diff --git a/test/color/test_hsv.py b/test/color/test_hsv.pyindex 12a379bb8..714c516b8 100644--- a/test/color/test_hsv.py+++ b/test/color/test_hsv.py@@ -61,6 +61,23 @@ class TestRgbToHsv:assert_allclose(f(data), expected)+ def test_nan_rgb_to_hsv(self):++ data = torch.zeros(1, 5, 5) # 3x5x5+ data = data.repeat(3, 1, 1) # 2x3x5x5++ # OpenCV+ data_cv = data.numpy().transpose(1, 2, 0).copy()+ expected = cv2.cvtColor(data_cv, cv2.COLOR_RGB2HSV)+ expected[:, :, 0] = 2 * math.pi * expected[:, :, 0] / 360.++ expected = expected.transpose(2, 0, 1)++ # Kornia+ f = kornia.color.RgbToHsv()++ assert_allclose(f(data), expected)+def test_gradcheck(self):data = torch.rand(3, 5, 5) # 3x2x2@@ -174,18 +191,3 @@ class TestHsvToRgb:actual = op_script(data)expected = kornia.hsv_to_rgb(data)assert_allclose(actual, expected)-- def test_nan(self):- np.random.seed(0)-- img = (np.random.randint(0, 256, [1000, 1000, 3], np.uint8) / 255.0).astype(np.float32)-- cv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)-- k_img = torch.from_numpy(np.transpose(img, [2, 0, 1]))-- k_img = kornia.rgb_to_hsv(k_img.float())- k_img[0] = k_img[0] * 360 / (2 * pi)- k_img = np.transpose(k_img.numpy(), [1, 2, 0])-- assert_allclose(cv_img, k_img)