Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand the channels to 3 if the user requested as such #8229

Merged
merged 10 commits into from
Jan 26, 2024
5 changes: 3 additions & 2 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4935,9 +4935,10 @@ def test_transform(self, transform, make_input):
check_transform(transform, make_input())

@pytest.mark.parametrize("num_output_channels", [1, 3])
@pytest.mark.parametrize("color_space", ["RGB", "GRAY"])
@pytest.mark.parametrize("fn", [F.rgb_to_grayscale, transform_cls_to_functional(transforms.Grayscale)])
def test_image_correctness(self, num_output_channels, fn):
image = make_image(dtype=torch.uint8, device="cpu")
def test_image_correctness(self, num_output_channels, color_space, fn):
image = make_image(dtype=torch.uint8, device="cpu", color_space=color_space)

actual = fn(image, num_output_channels=num_output_channels)
expected = F.to_image(F.rgb_to_grayscale(F.to_pil_image(image), num_output_channels=num_output_channels))
Expand Down
8 changes: 6 additions & 2 deletions torchvision/transforms/v2/functional/_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ def rgb_to_grayscale(inpt: torch.Tensor, num_output_channels: int = 1) -> torch.
def _rgb_to_grayscale_image(
image: torch.Tensor, num_output_channels: int = 1, preserve_dtype: bool = True
) -> torch.Tensor:
if image.shape[-3] == 1:
# TODO: Maybe move the validation that num_output_channels is 1 or 3 to this function instead of callers.
if image.shape[-3] == 1 and num_output_channels == 1:
return image.clone()

if image.shape[-3] == 1 and num_output_channels == 3:
s = [-1] * len(image.shape)
s[-3] = 3
return image.expand(s)
r, g, b = image.unbind(dim=-3)
l_img = r.mul(0.2989).add_(g, alpha=0.587).add_(b, alpha=0.114)
l_img = l_img.unsqueeze(dim=-3)
Expand Down
Loading