Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix few jit and cuda errors in color (#767)

Edgar Ribabf5a74a791f5
diff --git a/kornia/color/gray.py b/kornia/color/gray.py
index eea5af407..d853389ed 100644
--- a/kornia/color/gray.py
+++ b/kornia/color/gray.py
@@ -27,7 +27,10 @@ def rgb_to_grayscale(image: torch.Tensor) -> torch.Tensor:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
.format(image.shape))
- r, g, b = torch.chunk(image, chunks=3, dim=-3)
+ r: torch.Tensor = image[..., 0:1, :, :]
+ g: torch.Tensor = image[..., 1:2, :, :]
+ b: torch.Tensor = image[..., 2:3, :, :]
+
gray: torch.Tensor = 0.299 * r + 0.587 * g + 0.114 * b
return gray
diff --git a/kornia/color/hsv.py b/kornia/color/hsv.py
index 0286a793b..511372003 100644
--- a/kornia/color/hsv.py
+++ b/kornia/color/hsv.py
@@ -27,7 +27,16 @@ def rgb_to_hsv(image: torch.Tensor) -> torch.Tensor:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
.format(image.shape))
- maxc, max_indices = image.max(-3)
+ # TODO: enable again for later versions than 1.6.0 or find a different implementation.
+ # It turns out that .max(...) does not return the index in the first position when
+ # all the inputs have the same value in CUDA.
+ # maxc, max_indices = image.max(-3)
+ if image.is_cuda and torch.__version__ == '1.6.0':
+ maxc, max_indices = image.cpu().max(-3)
+ maxc, max_indices = maxc.to(image), max_indices.to(image.device)
+ else:
+ maxc, max_indices = image.max(-3)
+
minc: torch.Tensor = image.min(-3)[0]
v: torch.Tensor = maxc # brightness
@@ -39,7 +48,10 @@ def rgb_to_hsv(image: torch.Tensor) -> torch.Tensor:
deltac = torch.where(
deltac == 0, torch.ones_like(deltac), deltac)
- rc, gc, bc = torch.unbind(maxc.unsqueeze(-3) - image, dim=-3)
+ maxc_tmp = maxc.unsqueeze(-3) - image
+ rc: torch.Tensor = maxc_tmp[..., 0, :, :]
+ gc: torch.Tensor = maxc_tmp[..., 1, :, :]
+ bc: torch.Tensor = maxc_tmp[..., 2, :, :]
h = torch.stack([
bc - gc,
diff --git a/kornia/color/luv.py b/kornia/color/luv.py
index a16babd56..a73455b0f 100644
--- a/kornia/color/luv.py
+++ b/kornia/color/luv.py
@@ -47,7 +47,7 @@ def rgb_to_luv(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
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)
+ image_s = torch.stack([rs, gs, bs], dim=-3)
xyz_im: torch.Tensor = rgb_to_xyz(image_s)
@@ -70,7 +70,7 @@ def rgb_to_luv(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
u: torch.Tensor = 13 * L * (u_p - u_w)
v: torch.Tensor = 13 * L * (v_p - v_w)
- out = torch.stack((L, u, v), dim=-3)
+ out = torch.stack([L, u, v], dim=-3)
return out
@@ -118,7 +118,7 @@ def luv_to_rgb(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
z: torch.Tensor = ((a - 4) * c - 15 * a * d * y) / (12 * d + eps)
x: torch.Tensor = -(c / (d + eps) + 3. * z)
- xyz_im: torch.Tensor = torch.stack((x, y, z), -3)
+ xyz_im: torch.Tensor = torch.stack([x, y, z], -3)
rgbs_im: torch.Tensor = xyz_to_rgb(xyz_im)
@@ -131,7 +131,7 @@ def luv_to_rgb(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
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)
+ rgb_im: torch.Tensor = torch.stack([r, g, b], dim=-3)
return rgb_im
diff --git a/kornia/color/yuv.py b/kornia/color/yuv.py
index 206d111fd..9451762b3 100644
--- a/kornia/color/yuv.py
+++ b/kornia/color/yuv.py
@@ -25,13 +25,15 @@ def rgb_to_yuv(image: torch.Tensor) -> torch.Tensor:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
.format(image.shape))
- r, g, b = torch.chunk(image, chunks=3, dim=-3)
+ r: torch.Tensor = image[..., 0, :, :]
+ g: torch.Tensor = image[..., 1, :, :]
+ b: torch.Tensor = image[..., 2, :, :]
y: torch.Tensor = 0.299 * r + 0.587 * g + 0.114 * b
u: torch.Tensor = -0.147 * r - 0.289 * g + 0.436 * b
v: torch.Tensor = 0.615 * r - 0.515 * g - 0.100 * b
- out: torch.Tensor = torch.cat([y, u, v], -3)
+ out: torch.Tensor = torch.stack([y, u, v], -3)
return out
@@ -59,13 +61,15 @@ def yuv_to_rgb(image: torch.Tensor) -> torch.Tensor:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
.format(image.shape))
- y, u, v = torch.chunk(image, chunks=3, dim=-3)
+ y: torch.Tensor = image[..., 0, :, :]
+ u: torch.Tensor = image[..., 1, :, :]
+ v: torch.Tensor = image[..., 2, :, :]
r: torch.Tensor = y + 1.14 * v # coefficient for g is 0
g: torch.Tensor = y + -0.396 * u - 0.581 * v
b: torch.Tensor = y + 2.029 * u # coefficient for b is 0
- out: torch.Tensor = torch.cat([r, g, b], -3)
+ out: torch.Tensor = torch.stack([r, g, b], -3)
return out
diff --git a/test/color/test_luv.py b/test/color/test_luv.py
index d8cf8bcc1..19f8fc319 100644
--- a/test/color/test_luv.py
+++ b/test/color/test_luv.py
@@ -86,7 +86,7 @@ class TestRgbToLuv(BaseTester):
img = torch.ones(B, C, H, W, device=device, dtype=dtype)
op = kornia.color.rgb_to_luv
op_jit = torch.jit.script(op)
- assert_allclose(op(img), op_jit(img))
+ assert_allclose(op(img), op_jit(img), rtol=1e-3, atol=1e-3)
@pytest.mark.nn
def test_module(self, device, dtype):