Repository navigation
Keep achromatic HSL gradients finite - #132
sylvesterkaczmarek wants to merge 3 commits into
Conversation
| import numpy as np | ||
|
|
||
|
|
||
| class HslGradientsTest(parameterized.TestCase): |
There was a problem hiding this comment.
Please move these tests under color_conversion_test.py. We already have a color transformation test, so you can put them there, thanks!
There was a problem hiding this comment.
Moved the tests into color_conversion_test.py. All 9 new cases and 20 adjacent tests pass on current master.
| np.testing.assert_array_equal(grey(x), np.array([0.0, 0.0, level])) | ||
| np.testing.assert_allclose(reverse(x), [0.0, 0.0, 1.0], atol=1e-3) | ||
| np.testing.assert_allclose(forward(x), [0.0, 0.0, 1.0], atol=1e-3) | ||
| jacobian = jax.jacrev(color_conversion.rgb_to_hsl)(jnp.repeat(x[None], 3)) |
There was a problem hiding this comment.
Pre-prepare this outside the loop, so we can clearly see an arrange, act, and assert pattern in each test. Check this also for other tests too, thanks!
There was a problem hiding this comment.
The RGB Jacobian and its JIT wrapper are now prepared once before the greyscale-level loop.
378e83c to
52a5c62
Compare
| def test_chromatic_gradients_match_finite_differences(self): | ||
| x = jnp.array([0.2, 0.6, 0.9]) | ||
| numerical = np.zeros((3, 3)) | ||
| x64 = np.asarray(x).astype(np.float64) |
There was a problem hiding this comment.
Nit: Do this one directly in the line above.
| np.testing.assert_array_equal(jacobian[:2], np.zeros((2, 3))) | ||
|
|
||
| @parameterized.parameters(False, True) | ||
| def test_mixed_image_values_and_gradients(self, compiled): |
There was a problem hiding this comment.
Nit: Lets leave a blank line between the arrange, act and assert part of the test.
pabloduque0
left a comment
There was a problem hiding this comment.
Left a couple nits, otherwise LGTM
Signed-off-by: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com>
|
Thanks for the review, Pablo. I addressed both nits in |
…adients PiperOrigin-RevId: 1008326399
|
@sylvesterkaczmarek something went wrong with merging back some changes I did internally with yours. I guess the code was at an old commit, sorry about that. Can you please look at the current code again? I'll then redo a review, see internally what happens and hopefully we'll close the loop on this PR. |
|
Thanks for flagging this, Claudio. I checked current |
Problem
rgb_to_hslmasks achromatic hue/saturation outputs after dividing by the zero colour range. The forward value for a grey pixel is correct, but reverse differentiation produces NaNs, including in the lightness component. Low-precision black/white pixels also need a safe denominator for the inactive saturation branch.Change
Replace inactive denominators before division, preserving the existing achromatic output branches and chromatic formula. This follows the JAX guidance on masked undefined operations.
Validation
dm_pix/_src/color_conversion_test.py. Unchanged production code fails eight cases and passes the chromatic finite-difference control.colorsysfinite differences.git diff --checkpass on the current head.Hue is mathematically undefined on the achromatic axis. The tests preserve the existing constant hue/saturation branches and verify the lightness derivative along the greyscale path; they do not assert a unique full colour-space derivative there.
Python 3.12.14 on macOS arm64 CPU. GPU/TPU and the complete image-processing suite were not run. Channel-axis handling in #125 is separate and unchanged. No epsilon, colour convention, dependency, or public API changes.
Reproduced against
ffb65074b39b67e1a686d1348f2d461c7ff3ab45.