π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(color): make YUV conversions invertible (#4048)
Dmytro Mishkin1442a352059c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ff023243b..a546052d7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -25,6 +25,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Bug fixes+* Make `yuv_to_rgb` the exact inverse of `rgb_to_yuv`, so an RGB β YUV β RGB round trip is now limited only by the+ input dtype instead of losing up to `1.36e-3` (in B, at `rgb = (1, 1, 0)`) at every precision, `float64` included+ (#4044). The inverse kernel was a separately rounded copy of the published BT.470-5 M/PAL inverse relations rather+ than the inverse of the rounded forward kernel kornia actually ships, and one of its literals β `2.029`, where the+ inverse of kornia's forward kernel is `2.03199968` β carried most of the error. `yuv_to_rgb` output therefore moves+ by up to `1.23e-4` in R, `9.13e-4` in G and `1.60e-3` in B over the documented YUV domain, and `yuv420_to_rgb` and+ `yuv422_to_rgb` move with it. The forward direction (`rgb_to_yuv`, `rgb_to_yuv420`, `rgb_to_yuv422`) is unchanged.+ Agreement of the inverse with the standard's own relations improves overall, from `1.54e-3` to `5.24e-4`, though R+ alone moves the other way (`1.54e-4` to `2.77e-4`). The `.. warning::` blocks that documented the defect on the six+ affected functions and classes are gone.+* Fix the inverted `SigLip2` attention-mask polarity, so every call that passes an `attention_mask` changes (#4043).All three mask branches of `SigLip2Attention` (2-D `(B, N)`, 3-D `(B, N, N)` and 4-D `(B, 1, N, N)`) negated themask before handing it to `torch.nn.functional.scaled_dot_product_attention`, whose boolean form reads `True` as@@ -76,13 +87,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0the input only had to be divisible by 2 "vertical" while the guard rejects odd height *and* odd width, so areader padding one axis hit a `ShapeError`, and `yuv422_to_rgb` labelled its chroma argument "UV (luma)".The four `ShapeError` messages those guards raise said "evenly disible by 2" and now say "divisible", as do- the two identical messages in `kornia/color/raw.py`. Every YUV-to-RGB form -- the three functions and the- three `nn.Module` classes -- also gained a `.. warning::` naming a known defect: the round trip through- `rgb_to_yuv` loses up to 1.356e-3 in float64 because the two kernels are rounded separately rather than- inverted, with `float16` and `bfloat16` adding their own representation error on top (#4044); and- `yuv422_to_rgb`/`Yuv422ToRgb` validate only the chroma *width*, so a wrong chroma height surfaces as a bare- `RuntimeError` from `torch.cat` (#4050). On the test side, both gradcheck skips now cover the XLA/TPU- fixture as well as MPS, since XLA lowers a float64 request to float32, where gradcheck's default `eps=1e-6`+ the two identical messages in `kornia/color/raw.py`. `yuv422_to_rgb`/`Yuv422ToRgb` also gained a+ `.. warning::` naming a known defect: they validate only the chroma *width*, so a wrong chroma height+ surfaces as a bare `RuntimeError` from `torch.cat` (#4050). The same entry gave every YUV-to-RGB form a+ second warning, for the round trip that #4044 has since fixed; those blocks are gone, see the entry+ above. On the test side, both gradcheck skips now cover the XLA/TPU fixture as well as MPS, since XLA+ lowers a float64 request to float32, where gradcheck's default `eps=1e-6`makes the numerical Jacobian invalid: the name-based marker in `conftest.py` for tests called `*gradcheck*`,and the device guard in `BaseTester.gradcheck` for the six callers named something else. No runtimebehavior changed.diff --git a/kornia/color/yuv.py b/kornia/color/yuv.pyindex 3857b076a..5b5606549 100644--- a/kornia/color/yuv.py+++ b/kornia/color/yuv.py@@ -153,13 +153,9 @@ def yuv_to_rgb(image: torch.Tensor) -> torch.Tensor:`BT.470-5 <https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf>`_, Table 2,items 2.5 and 2.6).- .. warning::- This is not an exact inverse of :func:`~kornia.color.rgb_to_yuv`. The two kernels are- rounded separately rather than inverted, so a round trip loses up to 1.356e-3 (in B, at- ``rgb = (1, 1, 0)``) even in float64. That figure is the systematic kernel mismatch alone;- ``float16`` and ``bfloat16`` add their own representation error, which at that corner- measures 5.86e-3 in ``bfloat16``. Tracked in- `#4044 <https://github.com/kornia/kornia/issues/4044>`_.+ This is the exact inverse of :func:`~kornia.color.rgb_to_yuv`: its kernel is that function's+ kernel inverted rather than a separately rounded copy of the published inverse relations, so+ an RGB -> YUV -> RGB round trip is limited only by the precision of the input dtype.Args:image: YUV Image to be converted to RGB with shape :math:`(*, 3, H, W)`.@@ -175,11 +171,20 @@ def yuv_to_rgb(image: torch.Tensor) -> torch.Tensor:"""KORNIA_CHECK_SHAPE(image, ["*", "3", "H", "W"])+ # The exact inverse of the rounded M/PAL kernel in ``rgb_to_yuv``, rather than a separately+ # rounded copy of the published inverse relations, so a round trip is lossless to the dtype.+ # Over the rationals that inverse is+ # [[1, -1/25344, 144439/126720],+ # [1, -10001/25344, -73561/126720],+ # [1, 51499/25344, -61/126720]]+ # and every literal below is that value correctly rounded to float64. ``torch.linalg.inv``+ # reproduces it only to 3.3e-16 -- its LU factorization does not even return an exact 1.0 in+ # the first column -- so the constants are rounded from the exact fractions, not from it.kernel = torch.tensor([- [1.0, 0.0, 1.14],- [1.0, -0.396, -0.581],- [1.0, 2.029, 0.0],+ [1.0, -3.945707070707071e-05, 1.139827967171717],+ [1.0, -0.39461016414141414, -0.5805003156565657],+ [1.0, 2.0319996843434343, -0.00048137626262626264],],device=image.device,dtype=image.dtype,@@ -200,14 +205,6 @@ def yuv420_to_rgb(imagey: torch.Tensor, imageuv: torch.Tensor) -> torch.Tensor:`BT.470-5 <https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf>`_, Table 2,items 2.5 and 2.6).- .. warning::- This is not an exact inverse of :func:`~kornia.color.rgb_to_yuv420`. The two kernels are- rounded separately rather than inverted, so a round trip loses up to 1.356e-3 (in B, at- ``rgb = (1, 1, 0)``) even in float64. That figure is the systematic kernel mismatch alone;- ``float16`` and ``bfloat16`` add their own representation error, which at that corner- measures 5.86e-3 in ``bfloat16``. Tracked in- `#4044 <https://github.com/kornia/kornia/issues/4044>`_.-Args:imagey: Y (luma) Image plane to be converted to RGB with shape :math:`(*, 1, H, W)`.imageuv: UV (chroma) Image planes to be converted to RGB with shape :math:`(*, 2, H/2, W/2)`.@@ -259,14 +256,6 @@ def yuv422_to_rgb(imagey: torch.Tensor, imageuv: torch.Tensor) -> torch.Tensor:`BT.470-5 <https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf>`_, Table 2,items 2.5 and 2.6).- .. warning::- This is not an exact inverse of :func:`~kornia.color.rgb_to_yuv422`. The two kernels are- rounded separately rather than inverted, so a round trip loses up to 1.356e-3 (in B, at- ``rgb = (1, 1, 0)``) even in float64. That figure is the systematic kernel mismatch alone;- ``float16`` and ``bfloat16`` add their own representation error, which at that corner- measures 5.86e-3 in ``bfloat16``. Tracked in- `#4044 <https://github.com/kornia/kornia/issues/4044>`_.-.. warning::Only the chroma *width* is validated against the luma plane. A chroma plane of thewrong height escapes the ``ShapeError`` below and surfaces as a bare ``RuntimeError``@@ -456,13 +445,8 @@ class YuvToRgb(nn.Module):`BT.470-5 <https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf>`_, Table 2,items 2.5 and 2.6).- .. warning::- This is not an exact inverse of :class:`~kornia.color.RgbToYuv`. The two kernels are- rounded separately rather than inverted, so a round trip loses up to 1.356e-3 (in B, at- ``rgb = (1, 1, 0)``) even in float64. That figure is the systematic kernel mismatch alone;- ``float16`` and ``bfloat16`` add their own representation error, which at that corner- measures 5.86e-3 in ``bfloat16``. Tracked in- `#4044 <https://github.com/kornia/kornia/issues/4044>`_.+ This is the exact inverse of :class:`~kornia.color.RgbToYuv`, so an RGB -> YUV -> RGB round+ trip is limited only by the precision of the input dtype.Returns:RGB version of the image.@@ -508,14 +492,6 @@ class Yuv420ToRgb(nn.Module):`BT.470-5 <https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf>`_, Table 2,items 2.5 and 2.6).- .. warning::- This is not an exact inverse of :class:`~kornia.color.RgbToYuv420`. The two kernels are- rounded separately rather than inverted, so a round trip loses up to 1.356e-3 (in B, at- ``rgb = (1, 1, 0)``) even in float64. That figure is the systematic kernel mismatch alone;- ``float16`` and ``bfloat16`` add their own representation error, which at that corner- measures 5.86e-3 in ``bfloat16``. Tracked in- `#4044 <https://github.com/kornia/kornia/issues/4044>`_.-Returns:RGB version of the image.@@ -563,14 +539,6 @@ class Yuv422ToRgb(nn.Module):`BT.470-5 <https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.470-5-199802-S!!PDF-E.pdf>`_, Table 2,items 2.5 and 2.6).- .. warning::- This is not an exact inverse of :class:`~kornia.color.RgbToYuv422`. The two kernels are- rounded separately rather than inverted, so a round trip loses up to 1.356e-3 (in B, at- ``rgb = (1, 1, 0)``) even in float64. That figure is the systematic kernel mismatch alone;- ``float16`` and ``bfloat16`` add their own representation error, which at that corner- measures 5.86e-3 in ``bfloat16``. Tracked in- `#4044 <https://github.com/kornia/kornia/issues/4044>`_.-.. warning::Only the chroma *width* is validated against the luma plane. A chroma plane of thewrong height escapes the ``ShapeError`` and surfaces as a bare ``RuntimeError`` fromdiff --git a/tests/color/test_yuv.py b/tests/color/test_yuv.pyindex 0eaa54ed3..6a69e18a1 100644--- a/tests/color/test_yuv.py+++ b/tests/color/test_yuv.py@@ -85,57 +85,44 @@ _REFERENCE_COLORS = {# (Y uses the standard's own three constants, so it is exact.) 5e-4 clears both with margin._FORWARD_ATOL = 5.0e-4-# The inverse kernel is *separately* rounded rather than inverted, and one literal is rounded-# badly -- ``2.029`` where the published value is ``2.03211``. Summing |kornia - exact| times the-# documented YUV domain (Y in [0, 1], U in [-0.436, 0.436], V in [-0.615, 0.615]) row by row-# bounds the disagreement with the exact inverse of the relations at-# R: |1.14 - 1.14025086| * 0.615 = 1.543e-4-# G: |0.396 - 0.39473137| * 0.436 + |0.581 - 0.58080921| * 0.615 = 6.705e-4-# B: |2.029 - 2.03252033| * 0.436 = 1.535e-3-# so B alone sets the tolerance. That excess is kornia#4044, pinned by-# TestYuvToRgb.test_convention_* / test_wart_* below; 1.7e-3 is what the current constants need.-_INVERSE_ATOL = 1.7e-3--# Per-dtype tolerances for every RGB <-> YUV round trip in this file. These are *not* float-# precision: ``K_yuv2rgb @ K_rgb2yuv`` is off the identity by up to 1.356e-3 (B, at rgb=(1,1,0))-# and ``K_rgb2yuv @ K_yuv2rgb`` by up to 6.472e-4 over the documented YUV domain, so a round trip-# cannot do better than that at any precision. See kornia#4044 -- when it lands these should fall-# to the dtype noise floor. float16/bfloat16 are widened to their own measured floors instead.+# ``kornia.color.yuv_to_rgb``'s kernel is the *exact* inverse of the rounded forward kernel+# above, so it inherits that rounding instead of adding a second, independent one. Summing+# |kornia - exact| times the documented YUV domain (Y in [0, 1], U in [-0.436, 0.436],+# V in [-0.615, 0.615]) row by row bounds its disagreement with the exact inverse of the+# relations at+# R: |3.9457e-5| * 0.436 + |1.13982797 - 1.14025086| * 0.615 = 2.773e-4+# G: |0.39461016 - 0.39473137| * 0.436 + |0.58050032 - 0.58080921| * 0.615 = 2.428e-4+# B: |2.03199968 - 2.03252033| * 0.436 + |4.81376e-4| * 0.615 = 5.231e-4+# so B still sets the tolerance, at a third of what the separately rounded kernel needed+# (1.535e-3, kornia#4044). 6e-4 clears all three with margin.+_INVERSE_ATOL = 6.0e-4++# Per-dtype tolerances for every RGB <-> YUV round trip in this file. Now that ``yuv_to_rgb``+# inverts ``rgb_to_yuv`` exactly, these are pure float precision: the two kernels compose to the+# identity to 1.1e-16 in float64 and 1.2e-7 in float32, and a round trip over the whole file's+# inputs measures 3.3e-16 / 1.3e-7 on cpu. The float32 entry is two orders above that measured+# floor rather than one, because off cpu ``_apply_linear_transformation`` runs a cuDNN conv2d+# instead of an einsum and no CUDA device is available to measure it; it still sits 100x below+# the 1.356e-3 of kornia#4044, which is what these bounds have to exclude. float16/bfloat16 keep+# their own representation floors, measured at 6.6e-4 and 7.8e-3 and unchanged by this fix._ROUND_TRIP_TOL = {- torch.float64: (1e-5, 1.5e-3),- torch.float32: (1e-4, 1.5e-3),+ torch.float64: (1e-12, 1e-12),+ torch.float32: (1e-5, 1e-5),torch.float16: (1e-3, 2.5e-3),torch.bfloat16: (8e-3, 1.5e-2),}def _round_trip_tol(dtype):- return _ROUND_TRIP_TOL.get(dtype, (1e-4, 1.5e-3))---# rgb = (1, 1, 0) is the analytic worst case of the #4044 round trip, and its expected B is-# exactly 0.0 -- so ``rtol`` contributes nothing there and the whole budget is ``atol``. The-# shared _ROUND_TRIP_TOL is sized for the seeded random input asserted beside it; this cell sits-# *at* the analytic maximum, where the measured error is 1.35604e-3 (cpu, float32 and float64-# alike -- the dtype contributes ~4e-8) and would consume 90% of that 1.5e-3. Its own 1.8e-3-# keeps it ~25% clear, so a regression that widened the defect stays distinguishable from one-# that merely reached the edge.-#-# That is looser than the three tightest assertions in the file, which do sit at ~10% free-# (TestYuvToRgb::test_unit[blue] 1.5346e-3, TestYuv422ToRgb::test_unit_upsampling 1.5322e-3,-# TestYuvToRgb::test_unit_matches_the_reference_relations 1.5097e-3, all against _INVERSE_ATOL's-# 1.7e-3). Those are deliberately not padded: they are one transform applied to exact decimal-# constants, so the derived 1.535e-3 bound above *is* the value they measure, and widening them-# would stop pinning it. Backend noise is not budgeted in anywhere -- see _cudnn_tf32_disabled.-_WORST_CORNER_ATOL = 1.8e-3+ return _ROUND_TRIP_TOL.get(dtype, (1e-4, 1e-4))def _skip_without_real_float64(device) -> None:- # The two #4044 constant pins below hardcode float64 and assert raw deviations at 1e-9, so- # they need a backend that actually computes in float64. MPS has none at all, and XLA- # executes a float64 request as float32 (the ``tpu`` fixture in conftest is- # ``xm.xla_device()``), which moves the pinned values by 3.9e-8 (round-trip error) and- # 4.4e-8 (U -> B coefficient) -- ~40x the tolerance -- for a pure precision reason.+ # The #4044 regression below hardcodes float64 and asserts a raw deviation at 1e-12, so it+ # needs a backend that actually computes in float64. MPS has none at all, and XLA executes a+ # float64 request as float32 (the ``tpu`` fixture in conftest is ``xm.xla_device()``), where+ # the round trip bottoms out around 1e-7 -- five orders above the tolerance -- for a pure+ # precision reason.## XLA breaks the rest of the file's contract too, for an unrelated reason:# ``BaseTester.assert_close`` *replaces* a caller-supplied ``rtol``/``atol`` with@@ -276,22 +263,21 @@ class TestRgbToYuv(BaseTester):self.assert_close(yuv[4], torch.zeros_like(yuv[4]), atol=chroma_atol, rtol=0.0)def test_round_trip_rgb_yuv_rgb(self, device, dtype):- # Seeded: with unseeded ``torch.rand`` this test failed on roughly one run in eight,- # because the systematic error of kornia#4044 (1.356e-3) sits *outside* the 1e-3 the- # test used to assert. The tolerance now states the defect instead of racing it, and- # the worst-case corner is checked explicitly rather than waited for.+ # Seeded so a failure is reproducible rather than one-run-in-eight; the analytic worst+ # case is asserted explicitly below rather than waited for in the random sample.rtol, atol = _round_trip_tol(dtype)rgb = _seeded_rand(3, 4, 5, seed=4045).to(device=device, dtype=dtype)self.assert_close(kornia.color.yuv_to_rgb(kornia.color.rgb_to_yuv(rgb)), rgb, atol=atol, rtol=rtol)- # rgb = (1, 1, 0) maximises the B-channel round-trip error over the whole unit cube. Its- # expected B is exactly zero, so this cell gets its own atol -- see _WORST_CORNER_ATOL.+ # rgb = (1, 1, 0) maximised the B-channel round-trip error of kornia#4044 over the whole+ # unit cube (1.356e-3, dtype-independent). Its expected B is exactly zero, so ``rtol``+ # contributes nothing here and the shared ``atol`` carries the assertion on its own.worst = torch.tensor([1.0, 1.0, 0.0], device=device, dtype=dtype).view(3, 1, 1)self.assert_close(kornia.color.yuv_to_rgb(kornia.color.rgb_to_yuv(worst)),worst,- atol=max(atol, _WORST_CORNER_ATOL),+ atol=atol,rtol=rtol,)@@ -394,7 +380,7 @@ class TestRgbToYuv420(BaseTester):def test_forth_and_back(self, device, dtype):# 2x2-constant input, so the chroma subsample-then-upsample is lossless and what is- # measured is the colour transform. Tolerance is bounded by kornia#4044.+ # measured is the colour transform.rtol, atol = _round_trip_tol(dtype)data = (_seeded_rand(3, 4, 5, seed=420)@@ -564,7 +550,8 @@ class TestYuvToRgb(BaseTester):@pytest.mark.parametrize("name", list(_REFERENCE_COLORS))def test_unit(self, device, dtype, name):# The YUV of each reference colour must map back to that colour. ``_INVERSE_ATOL`` is- # 1.7e-3 rather than the 5e-4 the forward direction manages, because of kornia#4044.+ # 6e-4 rather than the 5e-4 the forward direction manages, because the inverse kernel+ # inherits the forward kernel's rounding on top of its own.rgb_values, yuv_values = _REFERENCE_COLORS[name]yuv = torch.tensor(yuv_values, device=device, dtype=dtype).view(3, 1, 1)expected = torch.tensor(rgb_values, device=device, dtype=dtype).view(3, 1, 1)@@ -582,58 +569,47 @@ class TestYuvToRgb(BaseTester):rtol=0.0,)- @pytest.mark.xfail(- reason="kornia#4044: yuv_to_rgb's matrix is a separately rounded copy of the published "- "BT.470-5 inverse, not the inverse of rgb_to_yuv's matrix",- strict=True,- )def test_convention_yuv_to_rgb_inverts_rgb_to_yuv_4044(self, device):- # Intended behavior: yuv_to_rgb undoes rgb_to_yuv to the precision of the input dtype.- # It does not. ``K_yuv2rgb @ K_rgb2yuv - I`` is- # [[ 0.000100, -0.000100, 0.000000],- # [-0.000103, 0.000659, -0.000556],- # [ 0.000737, 0.000619, -0.001356]]- # so the round trip loses up to 1.356e-3 of blue at rgb = (1, 1, 0), independently of- # dtype -- float64 fails exactly as float32 does, because the error is in the constants.- # float64 is hardcoded and the dtype fixture dropped so that the mark cannot flip for a- # precision reason; 1e-6 sits far above the float64 noise floor of a 3x3 matmul and far- # below the 1.356e-3 being caught. Marked xfail(strict=True) so fixing #4044 makes this- # XPASS and forces the mark out. Companion wart: test_wart_yuv_to_rgb_kernel_is_not_the_- # inverse_4044.- _skip_without_real_float64(device)-- rgb = torch.tensor([1.0, 1.0, 0.0], device=device, dtype=torch.float64).view(3, 1, 1)- rgb_back = kornia.color.yuv_to_rgb(kornia.color.rgb_to_yuv(rgb))-- assert (rgb_back - rgb).abs().max().item() < 1e-6, "kornia#4044: yuv_to_rgb is not the inverse of rgb_to_yuv"-- def test_wart_yuv_to_rgb_kernel_is_not_the_inverse_4044(self, device):- # Wart pin for kornia#4044, companion to the strict xfail above: assert the CURRENT- # error, so a partial fix cannot land unnoticed. Two cells:- # (0) the round trip at rgb = (1, 1, 0) still loses 1.356e-3 of blue -- flips as soon- # as any of the four inverse literals moves;- # (1) the single worst literal is still 2.029 where the exact inverse of kornia's own- # forward kernel wants 2.03199968 -- flips on the minimal fix (2.029 -> 2.032).- # If either fails, #4044 was (partly) fixed -- flip/remove the strict xfail above. NOT a- # contract that yuv_to_rgb must keep its current constants.- # Snippet used to generate expected (torch only, cpu float64):- # K = [[0.299, 0.587, 0.114], [-0.147, -0.289, 0.436], [0.615, -0.515, -0.100]]- # Kinv = [[1.0, 0.0, 1.14], [1.0, -0.396, -0.581], [1.0, 2.029, 0.0]]- # M = torch.tensor(Kinv) @ torch.tensor(K) - torch.eye(3)- # M[2, 0] + M[2, 1] # B error at rgb=(1,1,0) -> 0.001356- # torch.linalg.inv(torch.tensor(K))[2, 1] # the U -> B coefficient -> 2.03199968...- # atol 1e-9 on the error magnitude sits six orders below the 1.356e-3 being pinned and- # well above the float64 noise of a 3x3 matmul.+ # Regression for kornia#4044. Two cells, both in float64 so a failure cannot be a+ # precision artefact:+ # (0) the round trip at rgb = (1, 1, 0) -- the analytic worst case over the unit cube,+ # where the separately rounded kernel lost 1.356e-3 of blue at every dtype;+ # (1) the U -> B coefficient itself, read back through a unit U impulse. It must be the+ # exact inverse of kornia's own forward kernel (2.0319996843...), not the published+ # inverse relation's 2.03252033 and not the 2.029 that shipped before this fix.+ # Snippet used to generate the expected coefficient (torch only, cpu float64):+ # K = [[0.299, 0.587, 0.114], [-0.147, -0.289, 0.436], [0.615, -0.515, -0.100]]+ # torch.linalg.inv(torch.tensor(K, dtype=torch.float64))[2, 1] # -> 2.0319996843434343+ # 1e-12 sits four orders above the float64 noise floor of a 3x3 matmul (1.1e-16 measured)+ # and nine orders below the 1.356e-3 defect being excluded._skip_without_real_float64(device)rgb = torch.tensor([1.0, 1.0, 0.0], device=device, dtype=torch.float64).view(3, 1, 1)rgb_back = kornia.color.yuv_to_rgb(kornia.color.rgb_to_yuv(rgb))- assert abs((rgb_back - rgb)[2].item() - 0.001356) < 1e-9+ assert (rgb_back - rgb).abs().max().item() < 1e-12- # The U -> B coefficient the library ships, read back through a unit U impulse.impulse = torch.tensor([0.0, 1.0, 0.0], device=device, dtype=torch.float64).view(3, 1, 1)u_to_b = kornia.color.yuv_to_rgb(impulse)[2].item()- assert abs(u_to_b - 2.029) < 1e-9+ assert abs(u_to_b - 2.0319996843434343) < 1e-12++ def test_wart_integer_input_truncates_the_kernel_4053(self):+ # NOT a contract that integer input is supported -- kornia#4053 is still deciding whether+ # it should be rejected outright. This pins the single interaction between #4053 and this+ # fix: ``yuv_to_rgb`` builds its kernel at the image dtype, and the exact inverse carries+ # two near-zero *negative* literals (-3.95e-5 in the R row, -4.81e-4 in the B row) where+ # the old kernel carried exact zeros. Truncation toward zero leaves the integer kernel at+ # [[1, 0, 1], [1, 0, 0], [1, 2, 0]] -- bit for bit what shipped before this fix -- while+ # rounding away from zero would make those cells -1 and turn the integer path from+ # useless (#4053) into actively wrong.+ #+ # U and V are both non-zero on purpose: at U = V = 0 the two changed literals are+ # multiplied by zero and the cell passes whatever they truncate to.+ # cpu-only and no ``device`` fixture on purpose: #4053 also records that the cpu einsum+ # branch keeps the integer kernel while the conv2d branch silently upcasts to float32,+ # so this cell would be asserting a different code path off cpu.+ yuv = torch.tensor([0, 1, 1], dtype=torch.int64).view(3, 1, 1)++ assert kornia.color.yuv_to_rgb(yuv).flatten().tolist() == [1, 0, 2]def test_forth_and_back(self, device, dtype):rtol, atol = _round_trip_tol(dtype)