π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(color): keep RgbToGrayscale's rgb_weights=None so uint8 images are not all zeros (#5111)
εΊζ’η3315084be5d7
diff --git a/changelog.d/5111.breaking.md b/changelog.d/5111.breaking.mdnew file mode 100644index 000000000..8be0e7445--- /dev/null+++ b/changelog.d/5111.breaking.md@@ -0,0 +1,3 @@+`RgbToGrayscale()` without `rgb_weights` now stores `rgb_weights = None` instead of the float32 tensor+`[0.299, 0.587, 0.114]`, and raises `TypeError` for an integer or bool image other than uint8, as `rgb_to_grayscale`+does. It used to return all zeros for int8, int16, int32 and int64 images.diff --git a/changelog.d/5111.fixed.md b/changelog.d/5111.fixed.mdnew file mode 100644index 000000000..307601779--- /dev/null+++ b/changelog.d/5111.fixed.md@@ -0,0 +1,5 @@+`RgbToGrayscale()` without `rgb_weights` now leaves the weights to `rgb_to_grayscale`, which picks them for the image+dtype. A uint8 image used to come back all zeros, because the module's float32 default weights were cast to uint8 as+`[0, 0, 0]`; it now gets the functional's uint8 result, which matches `cv2.cvtColor` (#5090). A float64 image used to+get float32-rounded weights (up to 1.2e-8 off); outside TorchScript it now gets float64 weights. float32, float16 and+bfloat16 outputs, and modules built with explicit weights, are unchanged (#5109).diff --git a/kornia/color/gray.py b/kornia/color/gray.pyindex f07cde333..3cffad309 100644--- a/kornia/color/gray.py+++ b/kornia/color/gray.py@@ -198,8 +198,9 @@ class RgbToGrayscale(nn.Module):def __init__(self, rgb_weights: Optional[torch.Tensor] = None) -> None:super().__init__()- if rgb_weights is None:- rgb_weights = torch.Tensor([0.299, 0.587, 0.114])+ # None stays None: rgb_to_grayscale then picks the weights for the image dtype. Float32 defaults stored here+ # were cast to the image, so a uint8 image got the weights [0, 0, 0] and came back all zeros, and a float64+ # image got float32-rounded weights (#5109).self.rgb_weights = rgb_weightsdef forward(self, image: torch.Tensor) -> torch.Tensor:diff --git a/tests/color/test_gray.py b/tests/color/test_gray.pyindex 9eac4cebe..11bdcd256 100644--- a/tests/color/test_gray.py+++ b/tests/color/test_gray.py@@ -270,6 +270,31 @@ class TestRgbToGrayscale(BaseTester):gray_fcn = kornia.color.rgb_to_grayscaleassert_close(gray_ops(img), gray_fcn(img))+ def test_module_default_weights_are_the_functional_defaults_5109(self, device, dtype):+ # The module stored float32 default weights and passed them explicitly, so rgb_to_grayscale cast them to the+ # image: a float64 image got float32-rounded weights (up to 1.2e-8 off). It now leaves None to the functional,+ # so the two outputs are identical (#5109).+ img = torch.rand(2, 3, 4, 5, device=device, dtype=dtype)+ assert torch.equal(kornia.color.RgbToGrayscale()(img), kornia.color.rgb_to_grayscale(img))+ rgb_weights = torch.tensor([0.5, 0.25, 0.25], device=device, dtype=dtype)+ out = kornia.color.RgbToGrayscale(rgb_weights)(img)+ assert torch.equal(out, kornia.color.rgb_to_grayscale(img, rgb_weights=rgb_weights))++ def test_module_uint8_is_not_all_zeros_5109(self, device):+ # The float32 default weights cast to uint8 were [0, 0, 0], so every uint8 image came back all zeros (#5109).+ img = torch.arange(120, device=device, dtype=torch.uint8).reshape(2, 3, 4, 5)+ out = kornia.color.RgbToGrayscale()(img)+ assert out.dtype == torch.uint8+ assert torch.equal(out, kornia.color.rgb_to_grayscale(img))+ assert out.count_nonzero() > 0++ def test_module_other_integer_dtype_raises_like_the_functional_5109(self, device):+ # Default weights exist for uint8 and floating images only. The module used to return all zeros here.+ img = torch.arange(120, device=device, dtype=torch.int32).reshape(2, 3, 4, 5)+ gray = kornia.color.RgbToGrayscale()+ with pytest.raises(TypeError, match="Unknown data type"):+ gray(img)+class TestBgrToGrayscale(BaseTester):def test_smoke(self, device, dtype):