Skip to content

[Feature proposal] Apply adjust_contrast transformation for grayscale tensors #3670

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

Open
ukky17 opened this issue Apr 14, 2021 · 4 comments
Open

Comments

@ukky17
Copy link

ukky17 commented Apr 14, 2021

Motivation

Currently, adjust_contrast function in torchvision.transformations.functional_tensor only works for 3-channel (=RGB) tensors. However, I would like to change the contrast of a 1-channel (= grayscale) tensor.

Pitch

Just like the 3-channel case, the desired function changes the image contrast. I.e., contrast_factor = 0 gives a uniform gray image, 1 gives the original image while 2 increases the contrast by a factor of 2.

Additional context

This feature can be implemented by slightly modifying the adjust_contrast function in torchvision.transformations.functional_tensor like this;

def adjust_contrast(img: Tensor, contrast_factor: float) -> Tensor:
    if contrast_factor < 0:
        raise ValueError('contrast_factor ({}) is not non-negative.'.format(contrast_factor))

    _assert_image_tensor(img)

    _assert_channels(img, [1, 3])

    dtype = img.dtype if torch.is_floating_point(img) else torch.float32

    num_channels = _get_image_num_channels(img)
    if num_channels == 1:
        gray_img = img.to(dtype)
    elif num_channels == 3:
        gray_img = rgb_to_grayscale(img).to(dtype)

    mean = torch.mean(gray_img, dim=(-3, -2, -1), keepdim=True)

    return _blend(img, mean, contrast_factor)

cc @vfdev-5

@ukky17 ukky17 changed the title [Feature proposal] Apply adjust_contrast transformation for grayscale tensors [Feature proposal] Apply adjust_contrast transformation for grayscale tensors Apr 14, 2021
@datumbox
Copy link
Contributor

Thanks for the proposal.

I added it in the list of known limitations being tracked at #3224

As a temporary workaround, convert the image to RGB by copying the same colours in all channels, apply the transforms you want and then covert it back to grayscale using rgb_to_grayscale(). Suboptimal but can temporarily help you workaround the problem until the limitations are lifted.

@voldemortX
Copy link
Contributor

@datumbox I think this kind of relate to the same "Tensor images do not have meta-data" problem. Since we assume RGB inputs for all tensor transforms. #3219

The problem is I'm not sure if we can make a good guess about the image mode on dtype & channel numbers, or if we can add an input variable about image mode (that would introduce inconsistency between the two backends). On the other hand, adding the meta-data on tensor images would be too big a change to the current framework. But maybe torchvision could need meta-data in the future, as there is native image I/O functions being provided in torchvision.io ?

@datumbox
Copy link
Contributor

Yes it's related. I've added it on your list on the ticket just to be safe.

One workaround in this case would be to convert the image to RGB, apply the transforms of TorchVision and convert back to grayscale just before modeling. This should be solved more naturally once we support a better solution in the transforms for different image types.

@ukky17
Copy link
Author

ukky17 commented Apr 23, 2021

Thank you for the comments!

Yes, temporarily converting the image to RGB is a workaround, but I believe that slightly changing functional_tensor.py (like the code snippet in my first comment) would be a natural solution. Let me know if it's OK to send a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants