Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(color): align RgbToHsv eps default (#5309) (#5343)

NiteeshGowda720d8078face1b
diff --git a/changelog.d/5343.breaking.md b/changelog.d/5343.breaking.md
new file mode 100644
index 000000000..6ae07ec66
--- /dev/null
+++ b/changelog.d/5343.breaking.md
@@ -0,0 +1,5 @@
+`RgbToHsv` now defaults to `eps=1e-8`, the default of `rgb_to_hsv`, so the module and the function
+return the same saturation. The module used to default to `eps=1e-6`, which lowered the saturation of
+dark pixels: a pure red pixel of value `1e-4` had saturation `0.9901` through the module and
+`0.9999` through the function; both now give `0.9999`. Pass `RgbToHsv(eps=1e-6)` to keep the old
+module output.
diff --git a/kornia/color/hsv.py b/kornia/color/hsv.py
index 5e202448a..338e3ff99 100644
--- a/kornia/color/hsv.py
+++ b/kornia/color/hsv.py
@@ -159,7 +159,7 @@ class RgbToHsv(nn.Module):
ONNX_DEFAULT_INPUTSHAPE: ClassVar[list[int]] = [-1, 3, -1, -1]
ONNX_DEFAULT_OUTPUTSHAPE: ClassVar[list[int]] = [-1, 3, -1, -1]
- def __init__(self, eps: float = 1e-6) -> None:
+ def __init__(self, eps: float = 1e-8) -> None:
super().__init__()
self.eps = eps
diff --git a/tests/color/test_hsv.py b/tests/color/test_hsv.py
index 54d3af816..8870b499d 100644
--- a/tests/color/test_hsv.py
+++ b/tests/color/test_hsv.py
@@ -158,11 +158,10 @@ class TestRgbToHsv(BaseTester):
self.assert_close(op(img), op_jit(img))
def test_module(self, device, dtype):
- B, C, H, W = 2, 3, 4, 4
- img = torch.ones(B, C, H, W, device=device, dtype=dtype)
+ data = torch.tensor([[[1e-4]], [[0.0]], [[0.0]]], device=device, dtype=dtype)
ops = kornia.color.RgbToHsv().to(device, dtype)
fcn = kornia.color.rgb_to_hsv
- self.assert_close(ops(img), fcn(img))
+ self.assert_close(ops(data), fcn(data))
def test_dynamo(self, device, dtype, torch_optimizer):
B, C, H, W = 2, 3, 4, 4