Skip to content

Instantly share code, notes, and snippets.

@rehno-lindeque
Last active September 4, 2024 16:44
Show Gist options
  • Save rehno-lindeque/08789535710cd2b0ff455bcc15234537 to your computer and use it in GitHub Desktop.
Save rehno-lindeque/08789535710cd2b0ff455bcc15234537 to your computer and use it in GitHub Desktop.
hsv_to_rgb from torchvision/transforms/v2/functional/_color.py
{ buildPythonPackage, python }:
buildPythonPackage {
pname = "torchvision_color";
version = "v2";
src = ./.;
format = "other";
phases = ["unpackPhase" "installPhase"];
installPhase = ''
mkdir -p $out/${python.sitePackages}
cp $src/*.py $out/${python.sitePackages}
'';
}
{
description = "hsv_to_rgb from torchvision";
outputs = { self }: {
overlays.default = final: prev: {
python = prev.python.override (oldAttrs: {
self = final.python;
packageOverrides = prev.lib.composeExtensions oldAttrs.packageOverrides self.pythonOverrides.default;
});
};
pythonOverrides.default = final: prev: {
torchvision_color = final.callPackage ./. {};
};
};
}
BSD 3-Clause License
Copyright (c) Soumith Chintala 2016,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import torch
# Export of _hsv_to_rgb code
# https://github.com/pytorch/vision/blob/ffc58ef43afa0d40da66bd95a4f9e2f97c2153f0/torchvision/transforms/v2/functional/_color.py#L312-L339
# See https://github.com/pytorch/vision/issues/8179
def hsv_to_rgb(img: torch.Tensor) -> torch.Tensor:
h, s, v = img.unbind(dim=-3)
h6 = h.mul(6)
i = torch.floor(h6)
f = h6.sub_(i)
i = i.to(dtype=torch.int32)
sxf = s * f
one_minus_s = 1.0 - s
q = (1.0 - sxf).mul_(v).clamp_(0.0, 1.0)
t = sxf.add_(one_minus_s).mul_(v).clamp_(0.0, 1.0)
p = one_minus_s.mul_(v).clamp_(0.0, 1.0)
i.remainder_(6)
vpqt = torch.stack((v, p, q, t), dim=-3)
# vpqt -> rgb mapping based on i
select = torch.tensor([[0, 2, 1, 1, 3, 0], [3, 0, 0, 2, 1, 1], [1, 1, 3, 0, 0, 2]], dtype=torch.long)
select = select.to(device=img.device, non_blocking=True)
select = select[:, i]
if select.ndim > 3:
# if input.shape is (B, ..., C, H, W) then
# select.shape is (C, B, ..., H, W)
# thus we move C axis to get (B, ..., C, H, W)
select = select.moveaxis(0, -3)
return vpqt.gather(-3, select)
@rehno-lindeque
Copy link
Author

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