Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix float16 HSV saturation and gradients at black pixels (#4561)

Sunhy873f8ee1edda
diff --git a/changelog.d/4561.fixed.md b/changelog.d/4561.fixed.md
new file mode 100644
index 000000000..5dcb4ecdc
--- /dev/null
+++ b/changelog.d/4561.fixed.md
@@ -0,0 +1 @@
+`rgb_to_hsv` and hue-based color augmentation now keep black pixels and their gradients finite in float16.
diff --git a/kornia/augmentation/_2d/intensity/color_jiggle.py b/kornia/augmentation/_2d/intensity/color_jiggle.py
index e16fbc698..fae81fa25 100644
--- a/kornia/augmentation/_2d/intensity/color_jiggle.py
+++ b/kornia/augmentation/_2d/intensity/color_jiggle.py
@@ -85,12 +85,6 @@ class ColorJiggle(IntensityAugmentationBase2D):
all-zero default is the identity). Tracked in
`#4430 <https://github.com/kornia/kornia/issues/4430>`_.
- .. warning::
- In ``float16`` a black pixel reaching the saturation or hue step comes back as NaN, because both
- steps go through ``rgb_to_hsv``, whose ``eps`` underflows there. The hue step does the same for any
- pixel whose largest channel is ``0``. Tracked in
- `#4560 <https://github.com/kornia/kornia/issues/4560>`_.
-
.. note::
This function internally uses :func:`kornia.enhance.adjust_brightness`,
:func:`kornia.enhance.adjust_contrast`. :func:`kornia.enhance.adjust_saturation`,
diff --git a/kornia/augmentation/_2d/intensity/color_jitter.py b/kornia/augmentation/_2d/intensity/color_jitter.py
index c242d25e8..7a2d3a6a2 100644
--- a/kornia/augmentation/_2d/intensity/color_jitter.py
+++ b/kornia/augmentation/_2d/intensity/color_jitter.py
@@ -95,11 +95,6 @@ class ColorJitter(IntensityAugmentationBase2D):
draw: a contrast or saturation factor above ``1`` applied before the brightness step can lift part of the image
above zero first. Tracked in `#4430 <https://github.com/kornia/kornia/issues/4430>`_.
- .. warning::
- In ``float16`` a pixel whose largest channel is ``0`` -- for an input in ``[0, 1]``, a black pixel --
- comes back as NaN when it reaches the hue step, because ``rgb_to_hsv``'s ``eps`` underflows there. Tracked in
- `#4560 <https://github.com/kornia/kornia/issues/4560>`_.
-
.. note::
This function internally uses :func:`kornia.enhance.adjust_brightness_accumulative`,
:func:`kornia.enhance.adjust_contrast_with_mean_subtraction`,
diff --git a/kornia/augmentation/_2d/intensity/hue.py b/kornia/augmentation/_2d/intensity/hue.py
index 521911789..ac19f4dec 100644
--- a/kornia/augmentation/_2d/intensity/hue.py
+++ b/kornia/augmentation/_2d/intensity/hue.py
@@ -55,11 +55,7 @@ class RandomHue(IntensityAugmentationBase2D):
straight to that primitive shifts the hue by a different amount.
- the result is not clamped, so a pixel outside ``[0, 1]`` keeps a channel outside it, up to the
rounding of the HSV round trip. The exception is a pixel whose largest channel is exactly ``0``,
- which comes back as zeros, or as NaN in ``float16``.
-
- .. warning::
- In ``float16`` a pixel whose largest channel is ``0`` -- for an input in ``[0, 1]``, a black pixel --
- comes back as NaN, because ``rgb_to_hsv``'s ``eps`` underflows there. Tracked in `#4560 <https://github.com/kornia/kornia/issues/4560>`_.
+ which comes back as zeros in every dtype.
.. note::
This function internally uses :func:`kornia.enhance.adjust_hue`
diff --git a/kornia/augmentation/_2d/intensity/saturation.py b/kornia/augmentation/_2d/intensity/saturation.py
index 960e88271..c15bf4c30 100644
--- a/kornia/augmentation/_2d/intensity/saturation.py
+++ b/kornia/augmentation/_2d/intensity/saturation.py
@@ -57,10 +57,6 @@ class RandomSaturation(IntensityAugmentationBase2D):
with a negative channel does not: ``(-0.1, 0.5, 0.5)`` becomes ``(0.0, 0.5, 0.5)``, and an
all-negative pixel becomes gray at its largest channel.
- .. warning::
- In ``float16`` a black pixel comes back as NaN, because ``rgb_to_hsv``'s ``eps`` underflows
- there. Tracked in `#4560 <https://github.com/kornia/kornia/issues/4560>`_.
-
.. note::
This function internally uses :func:`kornia.enhance.adjust_saturation`
diff --git a/kornia/color/hsv.py b/kornia/color/hsv.py
index 736935f6e..5e202448a 100644
--- a/kornia/color/hsv.py
+++ b/kornia/color/hsv.py
@@ -58,7 +58,9 @@ def rgb_to_hsv(image: torch.Tensor, eps: float = 1e-8) -> torch.Tensor:
deltac = max_rgb - min_rgb
v = max_rgb
- s = deltac / (max_rgb + eps)
+ # Guard black pixels before division: eps can underflow in float16, including in backward.
+ s_divisor = torch.where(max_rgb == 0, torch.ones_like(max_rgb), max_rgb + eps)
+ s = deltac / s_divisor
deltac = torch.where(deltac == 0, torch.ones_like(deltac), deltac)
rc, gc, bc = torch.unbind((max_rgb.unsqueeze(-3) - image), dim=-3)
diff --git a/testing/half_precision_xfails/cpu_float16.txt b/testing/half_precision_xfails/cpu_float16.txt
index 26953f1bd..3667f3961 100644
--- a/testing/half_precision_xfails/cpu_float16.txt
+++ b/testing/half_precision_xfails/cpu_float16.txt
@@ -27,39 +27,11 @@ call builtins.RuntimeError tests/augmentation/container/test_augmentation_sequen
call builtins.AssertionError tests/augmentation/container/test_augmentation_sequential.py::TestAugmentationSequential::test_random_crops[cpu-float16]
call builtins.AssertionError tests/augmentation/container/test_augmentation_sequential.py::TestAugmentationSequential::test_random_erasing[cpu-float16]
call builtins.RuntimeError tests/augmentation/container/test_patch_sequential.py::TestPatchSequential::test_autocast[cpu-float16]
-call builtins.AssertionError tests/augmentation/container/test_sequential_aug.py::TestSequential::test_forward[cpu-float16-10]
-call builtins.AssertionError tests/augmentation/container/test_sequential_aug.py::TestSequential::test_forward[cpu-float16-False]
-call builtins.AssertionError tests/augmentation/container/test_sequential_aug.py::TestSequential::test_forward[cpu-float16-True]
-call builtins.AssertionError tests/augmentation/container/test_sequential_aug.py::TestSequential::test_forward[cpu-float16-random_apply3]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_against_sequential[cpu-float16-BCTHW-augmentations2]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_against_sequential[cpu-float16-BTCHW-augmentations2]
call builtins.RuntimeError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_autocast[cpu-float16-BCTHW]
call builtins.RuntimeError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_autocast[cpu-float16-BTCHW]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-1-BCTHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-1-BTCHW-augmentations0]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-1-BTCHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-10-BCTHW-augmentations0]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-10-BCTHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-10-BTCHW-augmentations0]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-10-BTCHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-False-BCTHW-augmentations0]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-False-BCTHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-False-BTCHW-augmentations0]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-False-BTCHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-True-BCTHW-augmentations0]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-True-BCTHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-True-BTCHW-augmentations0]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-True-BTCHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-random_apply1-BCTHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-random_apply1-BTCHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-random_apply2-BCTHW-augmentations0]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-random_apply2-BCTHW-augmentations1]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-random_apply2-BTCHW-augmentations0]
-call builtins.AssertionError tests/augmentation/container/test_video_sequential.py::TestVideoSequential::test_same_on_frame[cpu-float16-random_apply2-BTCHW-augmentations1]
call builtins.AssertionError tests/augmentation/test_augmentation.py::TestRandomGaussianIllumination::test_smoke[cpu-float16]
call builtins.AssertionError tests/augmentation/test_augmentation.py::TestRandomRGBShift::test_random_rgb_shift[cpu-float16]
call builtins.AssertionError tests/augmentation/test_augmentation.py::TestRandomRGBShift::test_random_rgb_shift_same_batch[cpu-float16]
-call builtins.AssertionError tests/augmentation/test_auto_operation.py::TestAutoAugment::test_reproduce[cpu-float16]
call builtins.AssertionError tests/augmentation/test_backward_3d.py::TestRandomMotionBlur3DBackward::test_param[cpu-float16-False-bilinear-circular-direction0-angle1]
call builtins.AssertionError tests/augmentation/test_backward_3d.py::TestRandomMotionBlur3DBackward::test_param[cpu-float16-False-bilinear-circular-direction0-angle2]
call builtins.AssertionError tests/augmentation/test_backward_3d.py::TestRandomMotionBlur3DBackward::test_param[cpu-float16-False-bilinear-circular-direction1-angle1]
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index c41bb10e3..3b70da5f7 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -1277,6 +1277,20 @@ class TestRandomVerticalFlip(BaseTester):
self.gradcheck(RandomVerticalFlip(p=1.0), (input,))
+class TestHueAugmentationBlackPixels(BaseTester):
+ @pytest.mark.parametrize("augmentation", [ColorJiggle, ColorJitter])
+ def test_black_pixels(self, device, dtype, augmentation):
+ data = torch.tensor([0.75, 0.5, 0.25], device=device, dtype=dtype).view(1, 3, 1, 1).repeat(1, 1, 6, 8)
+ data[..., :2, :3] = 0.0
+ data.requires_grad_()
+
+ result = augmentation(hue=(0.1, 0.1), p=1.0)(data)
+ assert torch.isfinite(result).all()
+ self.assert_close(result[..., :2, :3], torch.zeros_like(result[..., :2, :3]))
+ (gradient,) = torch.autograd.grad(result.sum(), data)
+ assert torch.isfinite(gradient).all()
+
+
class TestColorJiggle(BaseTester):
# TODO: improve and implement more meaningful smoke tests e.g check for a consistent
# return values such a Tensor variable.
diff --git a/tests/augmentation/test_conventions_intensity_values.py b/tests/augmentation/test_conventions_intensity_values.py
index 7214aa510..06901ca3e 100644
--- a/tests/augmentation/test_conventions_intensity_values.py
+++ b/tests/augmentation/test_conventions_intensity_values.py
@@ -24,6 +24,7 @@ import torch
import kornia.augmentation as K
from kornia.augmentation.random_generator import RectangleEraseGenerator
+from kornia.color import rgb_to_hsv
from kornia.core._compat import torch_version_ge
from kornia.core.exceptions import BaseError, ImageError, ShapeError
from kornia.enhance import (
@@ -235,13 +236,6 @@ class TestIntensityValueRangeConventions(BaseTester):
# the context for every later test in the process. On MPS the check is skipped by design
# and the raw histogram gather raises instead (an AcceleratorError, a RuntimeError subclass).
pytest.skip("CUDA: the value assert is a device-side assert that invalidates the context")
- if dtype == torch.float16 and name in ("ColorJiggle", "ColorJitter"):
- pytest.skip(
- "float16 only (#4560): the contrast step collapses the [-1, 0] fixture to exact zeros "
- "and rgb_to_hsv's eps=1e-8 then underflows for a black pixel, so adjust_hue returns "
- "NaN (adjust_hue(torch.zeros(1, 3, 2, 2, dtype=torch.float16), 0.1) is already NaN); "
- "float32, float64 and bfloat16 are finite"
- )
if name in ("RandomBoxBlur", "RandomGaussianBlur") and not supports_reflect_padding(device, dtype):
pytest.skip("reflection_pad2d is unavailable for this device/dtype")
fixtures = _out_of_range_fixtures(device, dtype)
@@ -395,11 +389,6 @@ class TestIntensityValueRangeConventions(BaseTester):
# cpu) -- all nine print `min=0 max=0` in both runs.
@pytest.mark.parametrize("name", _COLLAPSES_ON_NEGATIVE_FIXTURE)
def test_wart_intensity_negative_input_collapses_to_zero_4430(self, device, dtype, name):
- if dtype == torch.float16 and name == "ColorJitter":
- pytest.skip(
- "float16 only (#4560): the collapsed image reaches adjust_hue, whose rgb_to_hsv gives a "
- "NaN saturation for a black pixel there (eps=1e-8 underflows in float16)"
- )
image = _out_of_range_fixtures(device, dtype)["[-1, 0]"]
assert float(_run(name, image, seed=0).abs().max()) == 0.0
@@ -443,15 +432,13 @@ class TestIntensityValueRangeConventions(BaseTester):
image = torch.full((2, 3, 6, 8), -1.0, device=device, dtype=dtype)
every, some = [], []
for name in sorted(_INTENSITY_FACTORIES):
- if name == "RandomEqualize" or (dtype == torch.float16 and name == "ColorJitter"):
- continue # raises out of range; float16 ColorJitter (#4560) is NaN there
+ if name == "RandomEqualize":
+ continue
if name in ("RandomBoxBlur", "RandomGaussianBlur") and not supports_reflect_padding(device, dtype):
continue # torch 2.5.1 has no half reflection_pad2d on the CPU
zeros = sum(float(_run(name, image, seed=seed).abs().max()) == 0.0 for seed in range(5))
(every if zeros == 5 else some if zeros else []).append(name)
expected = set(self._COLLAPSES_ON_CONSTANT_MINUS_ONE)
- if dtype == torch.float16:
- expected -= {"ColorJitter"}
assert set(every) == expected
assert set(some) <= {"ColorJiggle", "RandomPlasmaContrast"}
# The two constant-image artefacts do not collapse the non-constant audit fixture on any of 20 seeds.
@@ -845,8 +832,6 @@ class TestIntensityColourConventions(BaseTester):
],
)
def test_convention_color_jiggle_negative_collapse_depends_on_which_steps_run(self, device, dtype, args, lo, hi):
- if dtype == torch.float16 and args[3] > 0.0:
- pytest.skip("float16 only (#4560): the hue step returns NaN for a black pixel")
torch.manual_seed(_FIXTURE_SEED)
image = (torch.rand(2, 3, 6, 8) - 1.0).to(device=device, dtype=dtype)
assert torch.equal(K.ColorJiggle(p=1.0)(image), image)
@@ -959,8 +944,7 @@ class TestIntensityColourConventions(BaseTester):
# Row 6c-10, the range half: RandomHue has no clamp, and a hue rotation keeps each pixel's largest and
# smallest channel values, so a pixel outside [0, 1] keeps a channel outside it. The exception is a
# pixel whose largest channel is exactly 0, whose HSV saturation divides by that zero: the round trip
- # returns zeros, and NaN in float16 (the #4560 underflow, which reaches a zero-max pixel that is not
- # black as well).
+ # returns zeros, in every dtype since the #4560 guard.
# Snippet used to generate expected:
# x = torch.tensor([[1.5, 0.2, 0.3], [0.5, -0.1, 0.2], [-0.2, -0.5, -0.9], [0.0, -0.5, -0.5]])
# torch.manual_seed(0); print(K.RandomHue((0.25, 0.25), p=1.0)(x.reshape(4, 3, 1, 1)).reshape(4, 3))
@@ -975,10 +959,8 @@ class TestIntensityColourConventions(BaseTester):
zero_max = torch.tensor([0.0, -0.5, -0.5], device=device, dtype=dtype).reshape(1, 3, 1, 1)
torch.manual_seed(_FORWARD_SEED)
collapsed = K.RandomHue((0.25, 0.25), p=1.0)(zero_max)
- if dtype == torch.float16:
- assert bool(collapsed.isnan().any())
- else:
- self.assert_close(collapsed, torch.zeros_like(collapsed))
+ # Zeros in every dtype since the #4560 guard; float16 used to be NaN here.
+ self.assert_close(collapsed, torch.zeros_like(collapsed))
# Row 6c-11: RandomGrayscale keeps the channel count and writes the same value into every
# channel; the default weights are the ITU-R BT.601 luma weights, so a pure red pixel becomes
@@ -1988,38 +1970,52 @@ class TestIntensityColourConventions(BaseTester):
assert torch.equal(out[far : far + 1], equalize_clahe(image[far : far + 1], float(clip[0]), (2, 2)))
assert not torch.equal(out[far : far + 1], equalize_clahe(image[far : far + 1], float(clip[far]), (2, 2)))
- # Issue #4560: every class whose path goes through rgb_to_hsv returns NaN for a black pixel in
- # float16, because the conversion's `eps=1e-8` underflows to 0 there; bfloat16 keeps the exponent
- # range of float32 and is finite. Pinned here so the four warnings that cite #4560 have an
- # executable anchor, and so the skip reasons above stay honest if the NaN ever disappears.
+ # Every class whose path goes through rgb_to_hsv is finite for a black pixel in every dtype,
+ # float16 included (the fix for #4560: the saturation divisor was `max_rgb + eps` with `eps=1e-8`,
+ # which underflows to `0` in float16, so a black pixel divided `0 / 0`). The guard substitutes a
+ # safe divisor through `torch.where` rather than clamping, so it bounds the derivative too and not
+ # just the value. Pinned here so the four docstrings that used to cite #4560 have an executable
+ # anchor, and so the skip branches this file used to carry stay deleted.
# Snippet used to generate expected:
- # for dt in (torch.float16, torch.float32, torch.bfloat16):
+ # for dt in (torch.float16, torch.float32, torch.bfloat16, torch.float64):
# print(dt, K.RandomHue((0.1, 0.1), p=1.0)(torch.zeros(1, 3, 4, 4, dtype=dt)).isnan().any())
- # executed 2026-09-15 (torch 2.14.0, cpu) -> `True`, `False`, `False`; the same for
- # RandomSaturation((1.5, 1.5)), ColorJiggle(0, 0, 0, (0.1, 0.1)) and ColorJitter(0, 0, 0, (0.1, 0.1)).
- # The pixel (0, -0.5, -0.5) is NaN in float16 through the three hue-step configurations and finite
- # through the two saturation-only ones (measured on cpu in all four dtypes and on mps float32).
+ # executed 2026-09-16 (torch 2.14.0, cpu) -> `False` in every dtype, and the same for
+ # RandomSaturation((1.5, 1.5)), ColorJiggle(0, 0, 0, (0.1, 0.1)), ColorJiggle(0, 0, (1.5, 1.5), 0)
+ # and ColorJitter(0, 0, 0, (0.1, 0.1)), for the black pixel and for (0, -0.5, -0.5) alike.
@pytest.mark.parametrize(
"name", ["RandomHue", "RandomSaturation", "ColorJiggle", "ColorJiggleSaturation", "ColorJitter"]
)
- def test_wart_hsv_path_black_pixel_is_nan_in_float16_4560(self, device, dtype, name):
+ def test_convention_hsv_path_zero_max_pixel_is_finite_4560(self, device, dtype, name):
factories = {
"RandomHue": lambda: K.RandomHue((0.1, 0.1), p=1.0),
"RandomSaturation": lambda: K.RandomSaturation((1.5, 1.5), p=1.0),
"ColorJiggle": lambda: K.ColorJiggle(0.0, 0.0, 0.0, (0.1, 0.1), p=1.0),
- # ColorJiggle's saturation step is adjust_saturation, an HSV round trip too, so the NaN needs
- # no hue step (ColorJitter's gray-subtraction saturation does not have it).
+ # ColorJiggle's saturation step is adjust_saturation, an HSV round trip too, so it reached the
+ # NaN without a hue step (ColorJitter's gray-subtraction saturation never did).
"ColorJiggleSaturation": lambda: K.ColorJiggle(0.0, 0.0, (1.5, 1.5), 0.0, p=1.0),
"ColorJitter": lambda: K.ColorJitter(0.0, 0.0, 0.0, (0.1, 0.1), p=1.0),
}
- # A black pixel, and a pixel whose largest channel is 0 without being black: the hue step divides
- # by that zero maximum too, while the saturation step's NaN needs `max - min` to be 0 as well.
+ # A black pixel, and a pixel whose largest channel is 0 without being black: the hue step divided
+ # by that zero maximum too, while the saturation step's NaN also needed `max - min` to be 0.
pixels = torch.tensor([[0.0, 0.0, 0.0], [0.0, -0.5, -0.5]], device=device, dtype=dtype)
torch.manual_seed(_FORWARD_SEED)
out = factories[name]()(pixels.T.reshape(1, 3, 1, 2))
- half = dtype == torch.float16
- hue_path = name not in ("RandomSaturation", "ColorJiggleSaturation")
- assert out.isnan().any(1)[0, 0].tolist() == [half, half and hue_path]
+ assert bool(out.isfinite().all()), (name, dtype, out.tolist())
+
+ # The guard is a `torch.where` and not a clamp, so the BACKWARD pass is finite too. A clamp would
+ # bound the value while leaving the unbounded derivative to reach the backward pass -- the trap
+ # AGENTS.md documents under #4229 -- and nothing in the forward pins above would notice.
+ # Snippet used to generate expected:
+ # for dt in (torch.float16, torch.float32):
+ # x = torch.zeros(1, 3, 2, 2, dtype=dt, requires_grad=True)
+ # rgb_to_hsv(x).sum().backward(); print(dt, x.grad.isfinite().all())
+ # executed 2026-09-16 (torch 2.14.0, cpu) -> `True` for both.
+ @pytest.mark.device_agnostic
+ def test_convention_rgb_to_hsv_black_pixel_gradient_is_finite_4560(self):
+ for torch_dtype in (torch.float16, torch.float32, torch.float64):
+ image = torch.zeros(1, 3, 2, 2, dtype=torch_dtype, requires_grad=True)
+ rgb_to_hsv(image).sum().backward()
+ assert bool(image.grad.isfinite().all()), torch_dtype
# Row 6c-46: an unbatched (C, H, W) input is promoted to (1, C, H, W), and `keepdim=True`
# returns the unbatched shape again. Checked across four classes of the sub-batch so the claim
diff --git a/tests/color/test_hsv.py b/tests/color/test_hsv.py
index c341f5226..eb1ff6c88 100644
--- a/tests/color/test_hsv.py
+++ b/tests/color/test_hsv.py
@@ -127,13 +127,23 @@ class TestRgbToHsv(BaseTester):
hsv = kornia.color.rgb_to_hsv(data)
self.assert_close(hsv[..., 0, :, :], expected_h)
- def test_nan_rgb_to_hsv(self, device, dtype):
- if dtype == torch.float16:
- pytest.skip("not work for half-precision")
-
- data = torch.zeros(3, 5, 5, device=device, dtype=dtype) # 3x5x5
+ @pytest.mark.parametrize("eps", [0.0, 1e-8, 1e-6, 1e-4])
+ def test_nan_rgb_to_hsv(self, device, dtype, eps):
+ data = torch.zeros(3, 5, 5, device=device, dtype=dtype, requires_grad=True) # 3x5x5
expected = torch.zeros_like(data) # 3x5x5
- self.assert_close(kornia.color.rgb_to_hsv(data), expected)
+ hsv = kornia.color.rgb_to_hsv(data, eps=eps)
+ self.assert_close(hsv, expected)
+ (gradient,) = torch.autograd.grad(hsv.sum(), data)
+ assert torch.isfinite(gradient).all()
+
+ @pytest.mark.parametrize("eps", [0.0, 1e-8, 1e-4])
+ def test_dark_red_saturation(self, device, dtype, eps):
+ # Dark nonzero colors must retain S = (max - min) / (max + eps).
+ # A blanket dtype-epsilon floor would desaturate this red pixel.
+ value = 2.0**-16
+ data = torch.tensor([[[value]], [[0.0]], [[0.0]]], device=device, dtype=dtype)
+ expected = torch.tensor([[[0.0]], [[value / (value + eps)]], [[value]]], device=device, dtype=dtype)
+ self.assert_close(kornia.color.rgb_to_hsv(data, eps=eps), expected)
def test_gradcheck(self, device, dtype):
B, C, H, W = 2, 3, 4, 4
diff --git a/tests/enhance/test_adjust.py b/tests/enhance/test_adjust.py
index 4c76e8623..189a57e73 100644
--- a/tests/enhance/test_adjust.py
+++ b/tests/enhance/test_adjust.py
@@ -144,6 +144,17 @@ class TestAdjustSaturation(BaseTester):
class TestAdjustHue(BaseTester):
+ def test_black_pixels(self, device, dtype):
+ data = torch.tensor([0.75, 0.5, 0.25], device=device, dtype=dtype).view(1, 3, 1, 1).repeat(1, 1, 2, 2)
+ data[..., 0, 0] = 0.0
+ data.requires_grad_()
+
+ result = kornia.enhance.adjust_hue(data, 0.1)
+ assert torch.isfinite(result).all()
+ self.assert_close(result[..., 0, 0], torch.zeros_like(result[..., 0, 0]))
+ (gradient,) = torch.autograd.grad(result.sum(), data)
+ assert torch.isfinite(gradient).all()
+
@pytest.mark.parametrize("shape", [(3, 4, 4), (2, 3, 3, 3), (4, 3, 3, 1, 1)])
def test_cardinality(self, device, dtype, shape):
img = torch.rand(shape, device=device, dtype=dtype)