Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Bug fix - Lab & Luv color conversion nans in gradient #900 (#904)

Edgar Riba2913a42fd15c
diff --git a/docs/source/color.rst b/docs/source/color.rst
index a629bd452..1f67b87f2 100644
--- a/docs/source/color.rst
+++ b/docs/source/color.rst
@@ -16,6 +16,9 @@ The functions in this section perform various color space conversions.
.. autofunction:: rgb_to_ycbcr
.. autofunction:: rgb_to_yuv
+.. autofunction:: rgb_to_linear_rgb
+.. autofunction:: linear_rgb_to_rgb
+
.. autofunction:: rgba_to_rgb
.. autofunction:: rgba_to_bgr
@@ -61,3 +64,5 @@ Modules
.. autoclass:: RgbToYcbcr
.. autoclass:: RgbToLab
.. autoclass:: LabToRgb
+.. autoclass:: LinearRgbToRgb
+.. autoclass:: RgbToLinearRgb
diff --git a/kornia/color/__init__.py b/kornia/color/__init__.py
index ec763d20b..f0816bef7 100644
--- a/kornia/color/__init__.py
+++ b/kornia/color/__init__.py
@@ -6,6 +6,8 @@ from .rgb import RgbToRgba, rgb_to_rgba
from .rgb import BgrToRgba, bgr_to_rgba
from .rgb import RgbaToRgb, rgba_to_rgb
from .rgb import RgbaToBgr, rgba_to_bgr
+from .rgb import RgbToLinearRgb, rgb_to_linear_rgb
+from .rgb import LinearRgbToRgb, linear_rgb_to_rgb
from .hsv import RgbToHsv, rgb_to_hsv
from .hsv import HsvToRgb, hsv_to_rgb
from .hls import RgbToHls, rgb_to_hls
diff --git a/kornia/color/lab.py b/kornia/color/lab.py
index 0e77f6872..91a698f7a 100644
--- a/kornia/color/lab.py
+++ b/kornia/color/lab.py
@@ -4,6 +4,7 @@ import torch
import torch.nn as nn
from .xyz import rgb_to_xyz, xyz_to_rgb
+from .rgb import rgb_to_linear_rgb, linear_rgb_to_rgb
"""
The RGB to Lab color transformations were translated from scikit image's rgb2lab and lab2rgb
@@ -37,27 +38,20 @@ def rgb_to_lab(image: torch.Tensor) -> torch.Tensor:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
.format(image.shape))
- # Convert from Linear RGB to sRGB
- r: torch.Tensor = image[..., 0, :, :]
- g: torch.Tensor = image[..., 1, :, :]
- b: torch.Tensor = image[..., 2, :, :]
+ # Convert from sRGB to Linear RGB
+ lin_rgb = rgb_to_linear_rgb(image)
- rs: torch.Tensor = torch.where(r > 0.04045, torch.pow(((r + 0.055) / 1.055), 2.4), r / 12.92)
- gs: torch.Tensor = torch.where(g > 0.04045, torch.pow(((g + 0.055) / 1.055), 2.4), g / 12.92)
- bs: torch.Tensor = torch.where(b > 0.04045, torch.pow(((b + 0.055) / 1.055), 2.4), b / 12.92)
-
- image_s = torch.stack([rs, gs, bs], dim=-3)
-
- xyz_im: torch.Tensor = rgb_to_xyz(image_s)
+ xyz_im: torch.Tensor = rgb_to_xyz(lin_rgb)
# normalize for D65 white point
xyz_ref_white = torch.tensor(
[0.95047, 1., 1.08883], device=xyz_im.device, dtype=xyz_im.dtype)[..., :, None, None]
xyz_normalized = torch.div(xyz_im, xyz_ref_white)
- power = torch.pow(xyz_normalized, 1 / 3)
+ threshold = 0.008856
+ power = torch.pow(xyz_normalized.clamp(min=threshold), 1 / 3.)
scale = 7.787 * xyz_normalized + 4. / 29.
- xyz_int = torch.where(xyz_normalized > 0.008856, power, scale)
+ xyz_int = torch.where(xyz_normalized > threshold, power, scale)
x: torch.Tensor = xyz_int[..., 0, :, :]
y: torch.Tensor = xyz_int[..., 1, :, :]
@@ -103,7 +97,7 @@ def lab_to_rgb(image: torch.Tensor, clip: bool = True) -> torch.Tensor:
fz = fy - (_b / 200.)
# if color data out of range: Z < 0
- fz = torch.where(fz < 0, torch.zeros_like(fz), fz)
+ fz = fz.clamp(min=0)
fxyz = torch.stack([fx, fy, fz], dim=-3)
@@ -122,16 +116,8 @@ def lab_to_rgb(image: torch.Tensor, clip: bool = True) -> torch.Tensor:
# https://github.com/richzhang/colorization-pytorch/blob/66a1cb2e5258f7c8f374f582acc8b1ef99c13c27/util/util.py#L107
# rgbs_im = torch.where(rgbs_im < 0, torch.zeros_like(rgbs_im), rgbs_im)
- # Convert from sRGB to RGB Linear
- rs: torch.Tensor = rgbs_im[..., 0, :, :]
- gs: torch.Tensor = rgbs_im[..., 1, :, :]
- bs: torch.Tensor = rgbs_im[..., 2, :, :]
-
- r: torch.Tensor = torch.where(rs > 0.0031308, 1.055 * torch.pow(rs, 1 / 2.4) - 0.055, 12.92 * rs)
- g: torch.Tensor = torch.where(gs > 0.0031308, 1.055 * torch.pow(gs, 1 / 2.4) - 0.055, 12.92 * gs)
- b: torch.Tensor = torch.where(bs > 0.0031308, 1.055 * torch.pow(bs, 1 / 2.4) - 0.055, 12.92 * bs)
-
- rgb_im: torch.Tensor = torch.stack([r, g, b], dim=-3)
+ # Convert from RGB Linear to sRGB
+ rgb_im = linear_rgb_to_rgb(rgbs_im)
# Clip to 0,1 https://www.w3.org/Graphics/Color/srgb
if clip:
diff --git a/kornia/color/luv.py b/kornia/color/luv.py
index a73455b0f..0fe7fcc9a 100644
--- a/kornia/color/luv.py
+++ b/kornia/color/luv.py
@@ -4,6 +4,7 @@ import torch
import torch.nn as nn
from .xyz import rgb_to_xyz, xyz_to_rgb
+from .rgb import rgb_to_linear_rgb, linear_rgb_to_rgb
"""
The RGB to Luv color transformations were translated from scikit image's rgb2luv and luv2rgb
@@ -38,25 +39,18 @@ def rgb_to_luv(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
.format(image.shape))
- # Convert from Linear RGB to sRGB
- r: torch.Tensor = image[..., 0, :, :]
- g: torch.Tensor = image[..., 1, :, :]
- b: torch.Tensor = image[..., 2, :, :]
+ # Convert from sRGB to Linear RGB
+ lin_rgb = rgb_to_linear_rgb(image)
- rs: torch.Tensor = torch.where(r > 0.04045, torch.pow(((r + 0.055) / 1.055), 2.4), r / 12.92)
- gs: torch.Tensor = torch.where(g > 0.04045, torch.pow(((g + 0.055) / 1.055), 2.4), g / 12.92)
- bs: torch.Tensor = torch.where(b > 0.04045, torch.pow(((b + 0.055) / 1.055), 2.4), b / 12.92)
-
- image_s = torch.stack([rs, gs, bs], dim=-3)
-
- xyz_im: torch.Tensor = rgb_to_xyz(image_s)
+ xyz_im: torch.Tensor = rgb_to_xyz(lin_rgb)
x: torch.Tensor = xyz_im[..., 0, :, :]
y: torch.Tensor = xyz_im[..., 1, :, :]
z: torch.Tensor = xyz_im[..., 2, :, :]
- L: torch.Tensor = torch.where(torch.gt(y, 0.008856),
- 116. * torch.pow(y, 1. / 3.) - 16.,
+ threshold = 0.008856
+ L: torch.Tensor = torch.where(y > threshold,
+ 116. * torch.pow(y.clamp(min=threshold), 1. / 3.) - 16.,
903.3 * y)
# Compute reference white point
@@ -122,16 +116,8 @@ def luv_to_rgb(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
rgbs_im: torch.Tensor = xyz_to_rgb(xyz_im)
- # Convert from sRGB to RGB Linear
- rs: torch.Tensor = rgbs_im[..., 0, :, :]
- gs: torch.Tensor = rgbs_im[..., 1, :, :]
- bs: torch.Tensor = rgbs_im[..., 2, :, :]
-
- r: torch.Tensor = torch.where(rs > 0.0031308, 1.055 * torch.pow(rs, 1 / 2.4) - 0.055, 12.92 * rs)
- g: torch.Tensor = torch.where(gs > 0.0031308, 1.055 * torch.pow(gs, 1 / 2.4) - 0.055, 12.92 * gs)
- b: torch.Tensor = torch.where(bs > 0.0031308, 1.055 * torch.pow(bs, 1 / 2.4) - 0.055, 12.92 * bs)
-
- rgb_im: torch.Tensor = torch.stack([r, g, b], dim=-3)
+ # Convert from RGB Linear to sRGB
+ rgb_im = linear_rgb_to_rgb(rgbs_im)
return rgb_im
diff --git a/kornia/color/rgb.py b/kornia/color/rgb.py
index ed11a8557..4699cceb2 100644
--- a/kornia/color/rgb.py
+++ b/kornia/color/rgb.py
@@ -17,6 +17,14 @@ def rgb_to_bgr(image: torch.Tensor) -> torch.Tensor:
>>> input = torch.rand(2, 3, 4, 5)
>>> output = rgb_to_bgr(input) # 2x3x4x5
"""
+ if not isinstance(image, torch.Tensor):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
+
+ if len(image.shape) < 3 or image.shape[-3] != 3:
+ raise ValueError("Input size must have a shape of (*, 3, H, W).Got {}"
+ .format(image.shape))
+
return bgr_to_rgb(image)
@@ -169,6 +177,71 @@ def rgba_to_bgr(image: torch.Tensor) -> torch.Tensor:
return rgb_to_bgr(x_rgb)
+def rgb_to_linear_rgb(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert an sRGB image to linear RGB. Used in colorspace conversions.
+
+ Args:
+ image (torch.Tensor): sRGB Image to be converted to linear RGB of shape :math:`(*,3,H,W)`.
+
+ Returns:
+ torch.Tensor: linear RGB version of the image with shape of :math:`(*,3,H,W)`.
+
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = rgb_to_linear_rgb(input) # 2x3x4x5
+ """
+
+ if not isinstance(image, torch.Tensor):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
+
+ if len(image.shape) < 3 or image.shape[-3] != 3:
+ raise ValueError("Input size must have a shape of (*, 3, H, W).Got {}"
+ .format(image.shape))
+
+ lin_rgb: torch.Tensor = torch.where(image > 0.04045, torch.pow(
+ ((image + 0.055) / 1.055), 2.4), image / 12.92)
+
+ return lin_rgb
+
+
+def linear_rgb_to_rgb(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert a linear RGB image to sRGB. Used in colorspace conversions.
+
+ Args:
+ image (torch.Tensor): linear RGB Image to be converted to sRGB of shape :math:`(*,3,H,W)`.
+
+ Returns:
+ torch.Tensor: sRGB version of the image with shape of shape :math:`(*,3,H,W)`.
+
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = linear_rgb_to_rgb(input) # 2x3x4x5
+ """
+
+ if not isinstance(image, torch.Tensor):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
+
+ if len(image.shape) < 3 or image.shape[-3] != 3:
+ raise ValueError("Input size must have a shape of (*, 3, H, W).Got {}"
+ .format(image.shape))
+
+ threshold = 0.0031308
+ rgb: torch.Tensor = torch.where(
+ image > threshold,
+ 1.055 *
+ torch.pow(
+ image.clamp(min=threshold),
+ 1 /
+ 2.4) -
+ 0.055,
+ 12.92 *
+ image)
+
+ return rgb
+
+
class BgrToRgb(nn.Module):
r"""Convert image from BGR to RGB.
@@ -331,3 +404,68 @@ class RgbaToBgr(nn.Module):
def forward(self, image: torch.Tensor) -> torch.Tensor:
return rgba_to_bgr(image)
+
+
+class RgbToLinearRgb(nn.Module):
+ r"""Convert an image from sRGB to linear RGB.
+
+ Reverses the gamma correction of sRGB to get linear RGB values for colorspace conversions.
+ The image data is assumed to be in the range of :math:`[0, 1]`
+
+ Returns:
+ torch.Tensor: Linear RGB version of the image.
+
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> rgb_lin = RgbToLinearRgb()
+ >>> output = rgb_lin(input) # 2x3x4x5
+
+ References:
+ [1] https://stackoverflow.com/questions/35952564/convert-rgb-to-srgb
+
+ [2] https://www.cambridgeincolour.com/tutorials/gamma-correction.htm
+
+ [3] https://en.wikipedia.org/wiki/SRGB
+ """
+
+ def __init__(self) -> None:
+ super(RgbToLinearRgb, self).__init__()
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
+ return rgb_to_linear_rgb(image)
+
+
+class LinearRgbToRgb(nn.Module):
+ r"""Convert a linear RGB image to sRGB.
+
+ Applies gamma correction to linear RGB values, at the end of colorspace conversions, to get sRGB.
+
+ Returns:
+ torch.Tensor: sRGB version of the image.
+
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> srgb = LinearRgbToRgb()
+ >>> output = srgb(input) # 2x3x4x5
+
+ References:
+ [1] https://stackoverflow.com/questions/35952564/convert-rgb-to-srgb
+
+ [2] https://www.cambridgeincolour.com/tutorials/gamma-correction.htm
+
+ [3] https://en.wikipedia.org/wiki/SRGB
+ """
+
+ def __init__(self) -> None:
+ super(LinearRgbToRgb, self).__init__()
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
+ return linear_rgb_to_rgb(image)
diff --git a/test/color/test_lab.py b/test/color/test_lab.py
index c648cdf66..6e5f1e758 100644
--- a/test/color/test_lab.py
+++ b/test/color/test_lab.py
@@ -35,22 +35,22 @@ class TestRgbToLab(BaseTester):
def test_unit(self, device, dtype):
data = torch.tensor([
- [[0.13088040, 0.54399723, 0.69396782, 0.63581685, 0.09902618],
+ [[0., 1., 0.69396782, 0.63581685, 0.09902618],
[0.59459005, 0.74215373, 0.89662376, 0.25920381, 0.89937686],
[0.29857584, 0.28139791, 0.16441015, 0.55507519, 0.06124221],
[0.40908658, 0.10261389, 0.01691456, 0.76006799, 0.32971736]],
- [[0.60354551, 0.76201361, 0.79009938, 0.91742945, 0.60044175],
+ [[0., 1., 0.79009938, 0.91742945, 0.60044175],
[0.42812678, 0.18552390, 0.04186043, 0.38030245, 0.15420346],
[0.13552373, 0.53955473, 0.79102736, 0.49050815, 0.75271446],
[0.39861023, 0.80680277, 0.82823833, 0.54438462, 0.22063386]],
- [[0.63231256, 0.18316011, 0.84317145, 0.59529881, 0.15297393],
+ [[0., 1., 0.84317145, 0.59529881, 0.15297393],
[0.59235313, 0.36617295, 0.34600773, 0.40304737, 0.61720451],
[0.46040250, 0.42006640, 0.54765106, 0.48982632, 0.13914755],
[0.58402964, 0.89597990, 0.98276161, 0.25019163, 0.69285921]]
], device=device, dtype=dtype)
# Reference output generated using skimage: rgb2lab(data)
- expected = torch.tensor([[[58.02612517, 72.48876064, 79.75208576, 86.38913217,
+ expected = torch.tensor([[[0., 100., 79.75208576, 86.38913217,
55.25164186],
[51.66668553, 43.81214392, 48.93865503, 39.03804484,
52.55152607],
@@ -59,7 +59,7 @@ class TestRgbToLab(BaseTester):
[45.02897165, 75.98315061, 78.257619, 61.85069778,
33.77972627]],
- [[-28.63524124, -39.21783796, -5.40909568, -37.74958445,
+ [[0., -0.002454937, -5.40909568, -37.74958445,
-55.02172792],
[24.16049084, 58.53088654, 75.33566652, -9.65827726,
76.94753157],
@@ -68,7 +68,7 @@ class TestRgbToLab(BaseTester):
[12.32708756, -33.04781428, -29.29282657, 13.46090338,
42.98737069]],
- [[-12.99941502, 63.48788307, -9.49591204, 32.9931831,
+ [[0., 0.00465342, -9.49591204, 32.9931831,
47.80929165],
[-16.11189945, 7.72083678, 19.17820444, -6.90801653,
-17.46468994],
diff --git a/test/color/test_luv.py b/test/color/test_luv.py
index 05c2e34a0..8d7526a35 100644
--- a/test/color/test_luv.py
+++ b/test/color/test_luv.py
@@ -34,15 +34,15 @@ class TestRgbToLuv(BaseTester):
def test_unit(self, device, dtype):
data = torch.tensor([
- [[0.13088040, 0.54399723, 0.69396782, 0.63581685, 0.09902618],
+ [[0., 1., 0.69396782, 0.63581685, 0.09902618],
[0.59459005, 0.74215373, 0.89662376, 0.25920381, 0.89937686],
[0.29857584, 0.28139791, 0.16441015, 0.55507519, 0.06124221],
[0.40908658, 0.10261389, 0.01691456, 0.76006799, 0.32971736]],
- [[0.60354551, 0.76201361, 0.79009938, 0.91742945, 0.60044175],
+ [[0., 1., 0.79009938, 0.91742945, 0.60044175],
[0.42812678, 0.18552390, 0.04186043, 0.38030245, 0.15420346],
[0.13552373, 0.53955473, 0.79102736, 0.49050815, 0.75271446],
[0.39861023, 0.80680277, 0.82823833, 0.54438462, 0.22063386]],
- [[0.63231256, 0.18316011, 0.84317145, 0.59529881, 0.15297393],
+ [[0., 1., 0.84317145, 0.59529881, 0.15297393],
[0.59235313, 0.36617295, 0.34600773, 0.40304737, 0.61720451],
[0.46040250, 0.42006640, 0.54765106, 0.48982632, 0.13914755],
[0.58402964, 0.89597990, 0.98276161, 0.25019163, 0.69285921]]
@@ -50,15 +50,15 @@ class TestRgbToLuv(BaseTester):
# Reference output generated using skimage: rgb2luv(data)
expected = torch.tensor([
- [[58.02612686, 72.48876190, 79.75208282, 86.38912964, 55.25164032],
+ [[0., 100., 79.75208282, 86.38912964, 55.25164032],
[51.66668701, 43.81214523, 48.93865585, 39.03804398, 52.55152512],
[23.71140671, 52.38661957, 72.54607391, 53.89587402, 67.94892883],
[45.02897263, 75.98315430, 78.25762177, 61.85069656, 33.77972794]],
- [[-41.55438232, -28.05948639, -13.54032803, -35.42317200, -49.27433014],
+ [[-0., -0.00054950, -13.54032803, -35.42317200, -49.27433014],
[21.34596062, 94.13956451, 137.11340332, -14.69241238, 102.94833374],
[9.55611229, -30.01761436, -58.94236755, 9.83261871, -62.96137619],
[-1.55336237, -55.22497559, -56.21067810, 43.76751328, 1.46367633]],
- [[-15.29427338, 77.81495667, -13.74480152, 52.17128372, 60.92724228],
+ [[-0., 0.00766720, -13.74480152, 52.17128372, 60.92724228],
[-27.01125526, -1.72837746, 6.57535267, -7.83582020, -38.45543289],
[-50.89970779, 17.65329361, 36.54148102, 2.25501800, 78.93702698],
[-38.39783859, -31.71204376, -46.63606644, 50.16629410, -84.74416351]]
diff --git a/test/color/test_rgb.py b/test/color/test_rgb.py
index 219524a28..b1f20e3d0 100644
--- a/test/color/test_rgb.py
+++ b/test/color/test_rgb.py
@@ -32,6 +32,17 @@ class TestRgbToBgr(BaseTester):
img = torch.ones(2, 1, 1, device=device, dtype=dtype)
assert kornia.color.rgb_to_bgr(img)
+ with pytest.raises(TypeError):
+ assert kornia.color.bgr_to_rgb([0.])
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.bgr_to_rgb(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.bgr_to_rgb(img)
+
def test_back_and_forth(self, device, dtype):
data_bgr = torch.rand(1, 3, 3, 2, device=device, dtype=dtype)
data_rgb = kornia.color.bgr_to_rgb(data_bgr)
@@ -250,3 +261,116 @@ class TestRgbToRgba(BaseTester):
ops = kornia.color.RgbaToBgr().to(device, dtype)
fcn = kornia.color.rgba_to_bgr
assert_allclose(ops(img), fcn(img))
+
+
+class TestLinearRgb(BaseTester):
+ def test_smoke(self, device, dtype):
+ C, H, W = 3, 4, 5
+ img = torch.rand(C, H, W, device=device, dtype=dtype)
+ assert isinstance(kornia.color.rgb_to_linear_rgb(img), torch.Tensor)
+ assert isinstance(kornia.color.linear_rgb_to_rgb(img), torch.Tensor)
+
+ @pytest.mark.parametrize(
+ "shape", [(1, 3, 4, 4), (2, 3, 2, 4), (3, 3, 4, 1), (3, 2, 1)])
+ def test_cardinality(self, device, dtype, shape):
+ img = torch.ones(shape, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_linear_rgb(img).shape == shape
+ assert kornia.color.linear_rgb_to_rgb(img).shape == shape
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.rgb_to_linear_rgb([0.])
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_linear_rgb(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_linear_rgb(img)
+
+ with pytest.raises(TypeError):
+ assert kornia.color.linear_rgb_to_rgb([0.])
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.linear_rgb_to_rgb(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.linear_rgb_to_rgb(img)
+
+ def test_back_and_forth(self, device, dtype):
+ data_bgr = torch.rand(1, 3, 3, 2, device=device, dtype=dtype)
+ data_rgb = kornia.color.rgb_to_linear_rgb(data_bgr)
+ data_bgr_new = kornia.color.linear_rgb_to_rgb(data_rgb)
+ assert_allclose(data_bgr, data_bgr_new)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([
+ [[1., 0.], [0.5, 0.1]],
+ [[1., 0.], [0.5, 0.2]],
+ [[1., 0.], [0.5, 0.3]]
+ ], device=device, dtype=dtype) # 3x2x2
+
+ expected = torch.tensor([[[1.00000000, 0.00000000], [0.21404116, 0.01002283]],
+ [[1.00000000, 0.00000000], [0.21404116, 0.03310477]],
+ [[1.00000000, 0.00000000], [0.21404116, 0.07323898]]
+ ], device=device, dtype=dtype) # 3x2x2
+
+ f = kornia.color.rgb_to_linear_rgb
+ assert_allclose(f(data), expected)
+
+ def test_unit_linear(self, device, dtype):
+ data = torch.tensor([[[1.00000000, 0.00000000], [0.21404116, 0.01002283]],
+ [[1.00000000, 0.00000000], [0.21404116, 0.03310477]],
+ [[1.00000000, 0.00000000], [0.21404116, 0.07323898]]
+ ], device=device, dtype=dtype) # 3x2x2
+
+ expected = torch.tensor([
+ [[1., 0.], [0.5, 0.1]],
+ [[1., 0.], [0.5, 0.2]],
+ [[1., 0.], [0.5, 0.3]]
+ ], device=device, dtype=dtype) # 3x2x2
+
+ f = kornia.color.linear_rgb_to_rgb
+ assert_allclose(f(data), expected)
+
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.ones(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.rgb_to_linear_rgb, (img,), raise_exception=True)
+ assert gradcheck(kornia.color.linear_rgb_to_rgb, (img,), raise_exception=True)
+
+ @pytest.mark.jit
+ def test_jit(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.ones(B, C, H, W, device=device, dtype=dtype)
+ op = kornia.color.rgb_to_linear_rgb
+ op_jit = torch.jit.script(op)
+ assert_allclose(op(img), op_jit(img))
+
+ @pytest.mark.jit
+ def test_jit_linear(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.ones(B, C, H, W, device=device, dtype=dtype)
+ op = kornia.color.linear_rgb_to_rgb
+ op_jit = torch.jit.script(op)
+ assert_allclose(op(img), op_jit(img))
+
+ @pytest.mark.nn
+ def test_module(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.ones(B, C, H, W, device=device, dtype=dtype)
+ ops = kornia.color.RgbToLinearRgb().to(device, dtype)
+ fcn = kornia.color.rgb_to_linear_rgb
+ assert_allclose(ops(img), fcn(img))
+
+ @pytest.mark.nn
+ def test_module_linear(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.ones(B, C, H, W, device=device, dtype=dtype)
+ ops = kornia.color.LinearRgbToRgb().to(device, dtype)
+ fcn = kornia.color.linear_rgb_to_rgb
+ assert_allclose(ops(img), fcn(img))