pytorch / vision

Datasets, Transforms and Models specific to Computer Vision
https://pytorch.org/vision
BSD 3-Clause "New" or "Revised" License
15.76k stars 6.89k forks source link

Expose `hsv_to_rgb` and `rgb_to_hsv` #8179

Open rehno-lindeque opened 6 months ago

rehno-lindeque commented 6 months ago

🚀 The feature

Expose torchvision.transforms.functional.hsv_to_rgb and torchvision.transforms.functional.rgb_to_hsv in the public api.

Motivation, pitch

There is already a private _hsv_to_rgb and _rgb_to_hsv (and the older _hsv2rgb, _rgb2hsv).

I'd like to use hsv_to_rgb directly in my project in order to visualize complex number fields.

Alternatives

There exists matplotlib.colors.hsv_to_rgb, and PIL.Image.Image.convert.

However there are numerous advantages to working directly with the pytorch tensors instead of converting back and forth.

Additional context

This should also help with implementing other custom transforms on top of HSV.

NicolasHug commented 6 months ago

Thank you for the feature request @rehno-lindeque . There was also https://github.com/pytorch/vision/issues/4029 opened in the past. So far, we've preferred not exposing such conversion utils in torchvision (see e.g. https://github.com/pytorch/vision/issues/4029#issuecomment-865079727), mainly because every single transform and model in torchvision assumes images are RGB (or grayscale).

We do not have a way to tell distinguish between an RGB and HSV images and that could lead to wrong transformations being applied. (It is technically possible to distinguish via meta-data, but it's not something deemed valuable enough considering the high maintenance cost).

For now, the recommended way is simply to copy/paste the _hsv_to_rgb and _rgb_to_hsv utils into your own code-base. You could also rely on them directly, but since they're private, there is no backward-compatibility guarantee.

rehno-lindeque commented 6 months ago

Thanks Nicolas, and apologies for missing this discussion.

For now, the recommended way is simply to copy/paste the _hsv_to_rgb and _rgb_to_hsv utils into your own code-base.

I threw together a tiny gist package for my purposes shortly after I opened the issue and that solved my immediate problem.

Just to mention, I didn't copy-paste because I felt it could be a bit dubious wrt licensing. I suspect it might not be the best recommendation for commercial users with proprietary code since it would be easy for the code to end up somewhere without the license.