Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Feat/rgba (#401)

Edgar Riba3a3d6a5e5cdc
diff --git a/kornia/__init__.py b/kornia/__init__.py
index 436c9d2e1..5a00924bf 100644
--- a/kornia/__init__.py
+++ b/kornia/__init__.py
@@ -22,6 +22,10 @@ from kornia.color import (
bgr_to_grayscale,
bgr_to_rgb,
rgb_to_bgr,
+ rgb_to_rgba,
+ bgr_to_rgba,
+ rgba_to_rgb,
+ rgba_to_bgr,
rgb_to_hsv,
hsv_to_rgb,
rgb_to_hls,
diff --git a/kornia/color/__init__.py b/kornia/color/__init__.py
index 9e50bae90..a83d92c65 100644
--- a/kornia/color/__init__.py
+++ b/kornia/color/__init__.py
@@ -2,6 +2,10 @@ from .gray import rgb_to_grayscale, RgbToGrayscale
from .gray import bgr_to_grayscale, BgrToGrayscale
from .rgb import BgrToRgb, bgr_to_rgb
from .rgb import RgbToBgr, rgb_to_bgr
+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 .hsv import RgbToHsv, rgb_to_hsv
from .hsv import HsvToRgb, hsv_to_rgb
from .hls import RgbToHls, rgb_to_hls
@@ -24,6 +28,7 @@ __all__ = [
"bgr_to_grayscale",
"bgr_to_rgb",
"rgb_to_bgr",
+ "rgb_to_rgba",
"rgb_to_hsv",
"hsv_to_rgb",
"rgb_to_hls",
@@ -47,6 +52,7 @@ __all__ = [
"BgrToGrayscale",
"BgrToRgb",
"RgbToBgr",
+ "RgbToRgba",
"RgbToHsv",
"HsvToRgb",
"RgbToHls",
diff --git a/kornia/color/rgb.py b/kornia/color/rgb.py
index 902079876..f11a608c3 100644
--- a/kornia/color/rgb.py
+++ b/kornia/color/rgb.py
@@ -1,52 +1,160 @@
+from typing import cast
+
import torch
import torch.nn as nn
+from typing import Union
-class RgbToBgr(nn.Module):
- r"""Convert image from RGB to BGR.
+def rgb_to_bgr(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert a RGB image to BGR.
- The image data is assumed to be in the range of (0, 1).
+ See :class:`~kornia.color.RgbToBgr` for details.
- args:
- image (torch.Tensor): RGB image to be converted to BGR
+ Args:
+ image (torch.Tensor): RGB Image to be converted to BGR.
- returns:
+ Returns:
torch.Tensor: BGR version of the image.
+ """
- shape:
- - image: :math:`(*, 3, H, W)`
- - output: :math:`(*, 3, H, W)`
+ return bgr_to_rgb(image)
- Examples::
- >>> import torch
- >>> import kornia
- >>> input = torch.rand(2, 3, 4, 5)
- >>> bgr = kornia.color.RgbToBgr()
- >>> output = bgr(input) # 2x3x4x5
+def bgr_to_rgb(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert a BGR image to RGB.
+ See :class:`~kornia.color.BgrToRgb` for details.
+
+ Args:
+ image (torch.Tensor): BGR Image to be converted to RGB.
+
+ Returns:
+ torch.Tensor: RGB version of the image.
"""
+ if not torch.is_tensor(image):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
- def __init__(self) -> None:
- super(RgbToBgr, self).__init__()
+ 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))
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
- return rgb_to_bgr(image)
+ # flip image channels
+ out: torch.Tensor = image.flip(-3)
+
+ return out
-def rgb_to_bgr(image: torch.Tensor) -> torch.Tensor:
- r"""Convert a RGB image to BGR.
+def rgb_to_rgba(image: torch.Tensor, alpha_val: Union[float, torch.Tensor]) -> torch.Tensor:
+ r"""Convert image from RGB to RGBA.
- See :class:`~kornia.color.RgbToBgr` for details.
+ See :class:`~kornia.color.RgbToRgba` for details.
Args:
- image (torch.Tensor): RGB Image to be converted to BGR.
+ image (torch.Tensor): RGB Image to be converted to RGBA.
+ alpha_val (float, torch.Tensor): A float number for the alpha value.
+
+ Returns:
+ torch.Tensor: RGBA version of the image.
+ """
+
+ if not torch.is_tensor(image):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(image)}")
+
+ if len(image.shape) < 3 or image.shape[-3] != 3:
+ raise ValueError(f"Input size must have a shape of (*, 3, H, W).Got {image.shape}")
+
+ if not isinstance(alpha_val, (float, torch.Tensor)):
+ raise TypeError(f"alpha_val type is not a float or torch.Tensor. Got {type(alpha_val)}")
+
+ # add one channel
+ r, g, b = torch.chunk(image, image.shape[-3], dim=-3)
+
+ a: torch.Tensor = cast(torch.Tensor, alpha_val)
+
+ if isinstance(alpha_val, float):
+ a = torch.full_like(r, fill_value=float(alpha_val))
+
+ return torch.cat([r, g, b, a], dim=-3)
+
+
+def bgr_to_rgba(image: torch.Tensor, alpha_val: Union[float, torch.Tensor]) -> torch.Tensor:
+ r"""Convert image from BGR to RGBA.
+
+ See :class:`~kornia.color.BgrToRgba` for details.
+
+ Args:
+ image (torch.Tensor): BGR Image to be converted to RGBA.
+ alpha_val (float, torch.Tensor): A float number for the alpha value.
+
+ Returns:
+ torch.Tensor: RGBA version of the image.
+ """
+
+ if not torch.is_tensor(image):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(image)}")
+
+ if len(image.shape) < 3 or image.shape[-3] != 3:
+ raise ValueError(f"Input size must have a shape of (*, 3, H, W).Got {image.shape}")
+
+ if not isinstance(alpha_val, (float, torch.Tensor)):
+ raise TypeError(f"alpha_val type is not a float or torch.Tensor. Got {type(alpha_val)}")
+ # convert first to RGB, then add alpha channel
+ x_rgb: torch.Tensor = bgr_to_rgb(image)
+ return rgb_to_rgba(x_rgb, alpha_val)
+
+
+def rgba_to_rgb(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert image from RGBA to RGB.
+
+ See :class:`~kornia.color.RgbaToRgb` for details.
+
+ Args:
+ image (torch.Tensor): RGBA Image to be converted to RGB.
+
+ Returns:
+ torch.Tensor: RGB version of the image.
+ """
+
+ if not torch.is_tensor(image):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(image)}")
+
+ if len(image.shape) < 3 or image.shape[-3] != 4:
+ raise ValueError(f"Input size must have a shape of (*, 3, H, W).Got {image.shape}")
+
+ # unpack channels
+ r, g, b, a = torch.chunk(image, image.shape[-3], dim=-3)
+
+ # compute new channels
+ a_one = torch.tensor(1.) - a
+ r_new: torch.Tensor = a_one * r + a * r
+ g_new: torch.Tensor = a_one * g + a * g
+ b_new: torch.Tensor = a_one * b + a * b
+
+ return torch.cat([r, g, b], dim=-3)
+
+
+def rgba_to_bgr(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert image from RGBA to BGR.
+
+ See :class:`~kornia.color.RgbaToBgr` for details.
+
+ Args:
+ image (torch.Tensor): RGBA Image to be converted to BGR.
Returns:
torch.Tensor: BGR version of the image.
"""
- return bgr_to_rgb(image)
+ if not torch.is_tensor(image):
+ raise TypeError(f"Input type is not a torch.Tensor. Got {type(image)}")
+
+ if len(image.shape) < 3 or image.shape[-3] != 4:
+ raise ValueError(f"Input size must have a shape of (*, 3, H, W).Got {image.shape}")
+
+ # convert to RGB first, then to BGR
+ x_rgb: torch.Tensor = rgba_to_rgb(image)
+ return rgb_to_bgr(x_rgb)
class BgrToRgb(nn.Module):
@@ -54,13 +162,10 @@ class BgrToRgb(nn.Module):
The image data is assumed to be in the range of (0, 1).
- args:
- image (torch.Tensor): BGR image to be converted to RGB.
-
- returns:
+ Returns:
torch.Tensor: RGB version of the image.
- shape:
+ Shape:
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 3, H, W)`
@@ -81,27 +186,142 @@ class BgrToRgb(nn.Module):
return bgr_to_rgb(image)
-def bgr_to_rgb(image: torch.Tensor) -> torch.Tensor:
- r"""Convert a BGR image to RGB.
+class RgbToBgr(nn.Module):
+ r"""Convert image from RGB to BGR.
+
+ The image data is assumed to be in the range of (0, 1).
+
+ Returns:
+ torch.Tensor: BGR version of the image.
+
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ Examples::
+
+ >>> import torch
+ >>> import kornia
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> bgr = kornia.color.RgbToBgr()
+ >>> output = bgr(input) # 2x3x4x5
+
+ """
+
+ def __init__(self) -> None:
+ super(RgbToBgr, self).__init__()
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ return rgb_to_bgr(image)
+
+
+class RgbToRgba(nn.Module):
+ r"""Convert image from RGB to RGBA.
+
+ Add an alpha channel to existing RGB image.
+
+ Args:
+ alpha_val (float, torch.Tensor): A float number for the alpha value.
+
+ Returns:
+ torch.Tensor: RGBA version of the image.
+
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 4, H, W)`
+
+ Examples::
+
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> rgba = kornia.color.RgbToRgba(1.)
+ >>> output = rgba(input) # 2x4x4x5
+ """
+
+ def __init__(self, alpha_val: Union[float, torch.Tensor]) -> None:
+ super(RgbToRgba, self).__init__()
+ self.alpha_val = alpha_val
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ return rgb_to_rgba(image, self.alpha_val)
- See :class:`~kornia.color.BgrToRgb` for details.
+
+class BgrToRgba(nn.Module):
+ r"""Convert image from BGR to RGBA.
+
+ Add an alpha channel to existing BGR image.
Args:
- input (torch.Tensor): BGR Image to be converted to RGB.
+ alpha_val (float, torch.Tensor): A float number for the alpha value.
Returns:
+ torch.Tensor: RGBA version of the image.
+
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 4, H, W)`
+
+ Examples::
+
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> rgba = kornia.color.BgrToRgba(1.)
+ >>> output = rgba(input) # 2x4x4x5
+ """
+
+ def __init__(self, alpha_val: Union[float, torch.Tensor]) -> None:
+ super(BgrToRgba, self).__init__()
+ self.alpha_val = alpha_val
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ return rgb_to_rgba(image, self.alpha_val)
+
+
+class RgbaToRgb(nn.Module):
+ r"""Convert image from RGBA to RGB.
+
+ Remove an alpha channel from RGB image.
+
+ returns:
torch.Tensor: RGB version of the image.
+
+ shape:
+ - image: :math:`(*, 4, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ Examples::
+
+ >>> input = torch.rand(2, 4, 4, 5)
+ >>> rgba = kornia.color.RgbaToRgb()
+ >>> output = rgba(input) # 2x3x4x5
"""
- if not torch.is_tensor(image):
- raise TypeError("Input type is not a torch.Tensor. Got {}".format(
- type(image)))
+ def __init__(self) -> None:
+ super(RgbaToRgb, self).__init__()
- 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))
+ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ return rgba_to_rgb(image)
- # flip image channels
- out: torch.Tensor = image.flip(-3)
- return out
+class RgbaToBgr(nn.Module):
+ r"""Convert image from RGBA to BGR.
+
+ Remove an alpha channel from BGR image.
+
+ returns:
+ torch.Tensor: BGR version of the image.
+
+ shape:
+ - image: :math:`(*, 4, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ Examples::
+
+ >>> input = torch.rand(2, 4, 4, 5)
+ >>> rgba = kornia.color.RgbaToBgr()
+ >>> output = rgba(input) # 2x3x4x5
+ """
+
+ def __init__(self) -> None:
+ super(RgbaToBgr, self).__init__()
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ return rgba_to_bgr(image)
diff --git a/kornia/feature/nms.py b/kornia/feature/nms.py
index 4043bede4..9acd0b522 100644
--- a/kornia/feature/nms.py
+++ b/kornia/feature/nms.py
@@ -7,6 +7,7 @@ import torch.nn as nn
class NonMaximaSuppression2d(nn.Module):
r"""Applies non maxima suppression to filter.
"""
+
def __init__(self, kernel_size: Tuple[int, int]):
super(NonMaximaSuppression2d, self).__init__()
self.kernel_size: Tuple[int, int] = kernel_size
@@ -38,6 +39,7 @@ class NonMaximaSuppression2d(nn.Module):
class NonMaximaSuppression3d(nn.Module):
r"""Applies non maxima suppression to filter.
"""
+
def __init__(self, kernel_size: Tuple[int, int, int]):
super(NonMaximaSuppression3d, self).__init__()
self.kernel_size: Tuple[int, int, int] = kernel_size
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index cea1df356..0af3c25a5 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -2,7 +2,6 @@ import torch
import torch.nn as nn
import torch.nn.functional as F
-
__all__ = [
# functional api
"pi",
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.py
index 45c0c59e4..82beef5c8 100644
--- a/kornia/geometry/linalg.py
+++ b/kornia/geometry/linalg.py
@@ -215,7 +215,6 @@ def transform_points(trans_01: torch.Tensor,
def transform_boxes(trans_mat: torch.Tensor, boxes: torch.Tensor, mode: str = "xyxy") -> torch.Tensor:
-
r""" Function that applies a transformation matrix to a box or batch of boxes. Boxes must
be a tensor of the shape (N, 4) or a batch of boxes (B, N, 4) and trans_mat must be a (3, 3)
transformation matrix or a batch of transformation matrices (B, 3, 3)
diff --git a/test/color/test_rgb.py b/test/color/test_rgb.py
index 231f303e0..79427e377 100644
--- a/test/color/test_rgb.py
+++ b/test/color/test_rgb.py
@@ -1,5 +1,3 @@
-import pytest
-
import kornia
import kornia.testing as utils # test utils
from test.common import device
@@ -7,13 +5,37 @@ from test.common import device
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
+import pytest
-class TestBgrToRgb:
-
- def test_bgr_to_rgb(self, device):
-
- # prepare input data
+class TestRgbToRgba:
+ def test_smoke(self, device):
+ data = torch.rand(3, 4, 4).to(device)
+ assert kornia.rgb_to_rgba(data, 0.).shape == (4, 4, 4)
+
+ def test_back_and_forth_rgb(self, device):
+ a_val: float = 1.
+ x_rgb = torch.rand(3, 4, 4).to(device)
+ x_rgba = kornia.rgb_to_rgba(x_rgb, a_val)
+ x_rgb_new = kornia.rgba_to_rgb(x_rgba)
+ assert_allclose(x_rgb, x_rgb_new)
+
+ def test_back_and_forth_bgr(self, device):
+ a_val: float = 1.
+ x_bgr = torch.rand(3, 4, 4).to(device)
+ x_rgba = kornia.bgr_to_rgba(x_bgr, a_val)
+ x_bgr_new = kornia.rgba_to_bgr(x_rgba)
+ assert_allclose(x_bgr, x_bgr_new)
+
+ def test_bgr(self, device):
+ a_val: float = 1.
+ x_rgb = torch.rand(3, 4, 4).to(device)
+ x_bgr = kornia.rgb_to_bgr(x_rgb)
+ x_rgba = kornia.rgb_to_rgba(x_rgb, a_val)
+ x_rgba_new = kornia.bgr_to_rgba(x_bgr, a_val)
+ assert_allclose(x_rgba, x_rgba_new)
+
+ def test_single(self, device):
data = torch.tensor([[[1., 1.],
[1., 1.]],
@@ -22,26 +44,26 @@ class TestBgrToRgb:
[[3., 3.],
[3., 3.]]]) # 3x2x2
+ data = data.to(device)
- expected = torch.tensor([[[3., 3.],
- [3., 3.]],
+ aval: float = 0.4
+ expected = torch.tensor([[[1.0, 1.0],
+ [1.0, 1.0]],
- [[2., 2.],
- [2., 2.]],
+ [[2.0, 2.0],
+ [2.0, 2.0]],
- [[1., 1.],
- [1., 1.]]]) # 3x2x2
+ [[3.0, 3.0],
+ [3.0, 3.0]],
- # move data to the device
- data = data.to(device)
+ [[0.4, 0.4],
+ [0.4, 0.4]]]) # 4x2x2
expected = expected.to(device)
- f = kornia.color.BgrToRgb()
- assert_allclose(f(data), expected)
+ assert_allclose(kornia.rgb_to_rgba(data, aval), expected)
- def test_batch_bgr_to_rgb(self, device):
+ def test_batch(self, device):
- # prepare input data
data = torch.tensor([[[[1., 1.],
[1., 1.]],
@@ -59,24 +81,83 @@ class TestBgrToRgb:
[[3., 3.],
[3., 3.]]]]) # 2x3x2x2
+ data = data.to(device)
- expected = torch.tensor([[[[3., 3.],
- [3., 3.]],
+ aval: float = 45.
- [[2., 2.],
- [2., 2.]],
+ expected = torch.tensor([[[[1.0, 1.0],
+ [1.0, 1.0]],
- [[1., 1.],
- [1., 1.]]],
+ [[2.0, 2.0],
+ [2.0, 2.0]],
- [[[3., 3.],
- [3., 3.]],
+ [[3.0, 3.0],
+ [3.0, 3.0]],
- [[2., 2.],
- [2., 2.]],
+ [[45., 45.],
+ [45., 45.]]],
- [[1., 1.],
- [1., 1.]]]]) # 2x3x2x2
+ [[[1.0, 1.0],
+ [1.0, 1.0]],
+
+ [[2.0, 2.0],
+ [2.0, 2.0]],
+
+ [[3.0, 3.0],
+ [3.0, 3.0]],
+
+ [[45., 45.],
+ [45., 45.]]]])
+ expected = expected.to(device)
+
+ assert_allclose(kornia.rgb_to_rgba(data, aval), expected)
+
+ def test_gradcheck(self, device):
+ data = torch.rand(1, 3, 2, 2).to(device)
+ data = utils.tensor_to_gradcheck_var(data) # to var
+ assert gradcheck(kornia.color.RgbToRgba(1.), (data,), raise_exception=True)
+
+
+class TestBgrToRgb:
+
+ def test_back_and_forth(self, device):
+ data_bgr = torch.rand(1, 3, 3, 2).to(device)
+ data_rgb = kornia.bgr_to_rgb(data_bgr)
+ data_bgr_new = kornia.rgb_to_bgr(data_rgb)
+ assert_allclose(data_bgr, data_bgr_new)
+
+ def test_bgr_to_rgb(self, device):
+
+ data = torch.tensor([[[1., 1.], [1., 1.]],
+ [[2., 2.], [2., 2.]],
+ [[3., 3.], [3., 3.]]]) # 3x2x2
+
+ expected = torch.tensor([[[3., 3.], [3., 3.]],
+ [[2., 2.], [2., 2.]],
+ [[1., 1.], [1., 1.]]]) # 3x2x2
+
+ # move data to the device
+ data = data.to(device)
+ expected = expected.to(device)
+
+ f = kornia.color.BgrToRgb()
+ assert_allclose(f(data), expected)
+
+ def test_batch_bgr_to_rgb(self, device):
+
+ data = torch.tensor([[[[1., 1.], [1., 1.]],
+ [[2., 2.], [2., 2.]],
+ [[3., 3.], [3., 3.]]],
+ [[[1., 1.], [1., 1.]],
+ [[2., 2.], [2., 2.]],
+ [[3., 3.], [3., 3.]]]]) # 2x3x2x2
+
+ expected = torch.tensor([[[[3., 3.], [3., 3.]],
+ [[2., 2.], [2., 2.]],
+ [[1., 1.], [1., 1.]]],
+ [[[3., 3.], [3., 3.]],
+ [[2., 2.], [2., 2.]],
+ [[1., 1.], [1., 1.]]]]) # 2x3x2x2
# move data to the device
data = data.to(device)
@@ -88,21 +169,14 @@ class TestBgrToRgb:
def test_gradcheck(self, device):
- # prepare input data
- data = torch.tensor([[[1., 1.],
- [1., 1.]],
-
- [[2., 2.],
- [2., 2.]],
-
- [[3., 3.],
- [3., 3.]]]) # 3x2x2
+ data = torch.tensor([[[1., 1.], [1., 1.]],
+ [[2., 2.], [2., 2.]],
+ [[3., 3.], [3., 3.]]]) # 3x2x2
data = data.to(device)
data = utils.tensor_to_gradcheck_var(data) # to var
- assert gradcheck(kornia.color.BgrToRgb(), (data,),
- raise_exception=True)
+ assert gradcheck(kornia.color.BgrToRgb(), (data,), raise_exception=True)
@pytest.mark.skip(reason="turn off all jit for a while")
def test_jit(self, device):
@@ -110,24 +184,22 @@ class TestBgrToRgb:
def op_script(data: torch.Tensor) -> torch.Tensor:
return kornia.bgr_to_rgb(data)
- data = torch.tensor([[[1., 1.],
- [1., 1.]],
-
- [[2., 2.],
- [2., 2.]],
-
- [[3., 3.],
- [3., 3.]]]) # 3x2x2
-
- data = data.to(device)
-
- actual = op_script(data)
- expected = kornia.bgr_to_rgb(data)
- assert_allclose(actual, expected)
+ data = torch.Tensor([[[1., 1.], [1., 1.]],
+ [[2., 2.], [2., 2.]],
+ [[3., 3.], [3., 3.]]]) # 3x2x2
+ actual = op_script(data)
+ expected = kornia.bgr_to_rgb(data)
+ assert_allclose(actual, expected)
class TestRgbToBgr:
+ def test_back_and_forth(self, device):
+ data_rgb = torch.rand(1, 3, 3, 2).to(device)
+ data_bgr = kornia.rgb_to_bgr(data_rgb)
+ data_rgb_new = kornia.bgr_to_rgb(data_bgr)
+ assert_allclose(data_rgb, data_rgb_new)
+
def test_rgb_to_bgr(self, device):
# prepare input data
@@ -180,20 +252,12 @@ class TestRgbToBgr:
def op_script(data: torch.Tensor) -> torch.Tensor:
return kornia.rgb_to_bgr(data)
- data = torch.tensor([[[1., 1.],
- [1., 1.]],
-
- [[2., 2.],
- [2., 2.]],
-
- [[3., 3.],
- [3., 3.]]]) # 3x2x2
-
- data = data.to(device)
-
- actual = op_script(data)
- expected = kornia.rgb_to_bgr(data)
- assert_allclose(actual, expected)
+ data = torch.Tensor([[[1., 1.], [1., 1.]],
+ [[2., 2.], [2., 2.]],
+ [[3., 3.], [3., 3.]]]) # 3x2x
+ actual = op_script(data)
+ expected = kornia.rgb_to_bgr(data)
+ assert_allclose(actual, expected)
def test_batch_rgb_to_bgr(self, device):
diff --git a/test/feature/test_nms.py b/test/feature/test_nms.py
index 9c1093ff7..77aee5514 100644
--- a/test/feature/test_nms.py
+++ b/test/feature/test_nms.py
@@ -78,20 +78,20 @@ class TestNMS3d:
]]]).to(device)
expected = torch.tensor([[[[[0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.]],
- [[0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 2., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.]],
- [[0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.],
- [0., 0., 0., 0., 0.]]]]])
+ [0., 0., 0., 0., 0.],
+ [0., 0., 0., 0., 0.],
+ [0., 0., 0., 0., 0.],
+ [0., 0., 0., 0., 0.]],
+ [[0., 0., 0., 0., 0.],
+ [0., 0., 0., 0., 0.],
+ [0., 0., 2., 0., 0.],
+ [0., 0., 0., 0., 0.],
+ [0., 0., 0., 0., 0.]],
+ [[0., 0., 0., 0., 0.],
+ [0., 0., 0., 0., 0.],
+ [0., 0., 0., 0., 0.],
+ [0., 0., 0., 0., 0.],
+ [0., 0., 0., 0., 0.]]]]])
nms = kornia.feature.NonMaximaSuppression3d((3, 3, 3)).to(device)
scores = nms(inp)
assert_allclose(scores, expected, atol=1e-4, rtol=1e-3)
diff --git a/test/filters/test_sobel.py b/test/filters/test_sobel.py
index a768e2e25..8b376b99a 100644
--- a/test/filters/test_sobel.py
+++ b/test/filters/test_sobel.py
@@ -181,50 +181,50 @@ class TestSpatialGradient3d:
]]]).to(device)
expected = torch.tensor([[[[[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.5000, 0.0000, -0.5000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
- [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.5000, 0.0000, -0.5000, 0.0000],
- [0.5000, 0.5000, 0.0000, -0.5000, -0.5000],
- [0.0000, 0.5000, 0.0000, -0.5000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
- [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.5000, 0.0000, -0.5000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]],
- [[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.5000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, -0.5000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
- [[0.0000, 0.0000, 0.5000, 0.0000, 0.0000],
- [0.0000, 0.5000, 0.5000, 0.5000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, -0.5000, -0.5000, -0.5000, 0.0000],
- [0.0000, 0.0000, -0.5000, 0.0000, 0.0000]],
- [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.5000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, -0.5000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]],
- [[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.5000, 0.0000, 0.0000],
- [0.0000, 0.5000, 0.0000, 0.5000, 0.0000],
- [0.0000, 0.0000, 0.5000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
- [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
- [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, -0.5000, 0.0000, 0.0000],
- [0.0000, -0.5000, 0.0000, -0.5000, 0.0000],
- [0.0000, 0.0000, -0.5000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]]]])
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.5000, 0.0000, -0.5000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
+ [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.5000, 0.0000, -0.5000, 0.0000],
+ [0.5000, 0.5000, 0.0000, -0.5000, -0.5000],
+ [0.0000, 0.5000, 0.0000, -0.5000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
+ [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.5000, 0.0000, -0.5000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]],
+ [[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.5000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, -0.5000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
+ [[0.0000, 0.0000, 0.5000, 0.0000, 0.0000],
+ [0.0000, 0.5000, 0.5000, 0.5000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, -0.5000, -0.5000, -0.5000, 0.0000],
+ [0.0000, 0.0000, -0.5000, 0.0000, 0.0000]],
+ [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.5000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, -0.5000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]],
+ [[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.5000, 0.0000, 0.0000],
+ [0.0000, 0.5000, 0.0000, 0.5000, 0.0000],
+ [0.0000, 0.0000, 0.5000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
+ [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
+ [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, -0.5000, 0.0000, 0.0000],
+ [0.0000, -0.5000, 0.0000, -0.5000, 0.0000],
+ [0.0000, 0.0000, -0.5000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]]]])
edges = kornia.filters.spatial_gradient3d(inp)
assert_allclose(edges, expected)
diff --git a/test/geometry/test_spatial_softargmax.py b/test/geometry/test_spatial_softargmax.py
index 555d66986..5e731f2f3 100644
--- a/test/geometry/test_spatial_softargmax.py
+++ b/test/geometry/test_spatial_softargmax.py
@@ -544,60 +544,60 @@ class TestConvQuadInterp3d:
[0., 0., 0, 0, 0.],
[0., 0., 0., 0, 0.]]]]]).to(device)
expected_coord = torch.tensor([[[[[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
-
- [[1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
- [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
- [1.0000, 1.0000, 1.0688, 1.0000, 1.0000],
- [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
- [1.0000, 1.0000, 1.0000, 1.0000, 1.0000]],
-
- [[2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
- [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
- [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
- [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
- [2.0000, 2.0000, 2.0000, 2.0000, 2.0000]]],
-
-
- [[[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000]],
-
- [[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 1.8366, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000]],
-
- [[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
- [0.0000, 1.0000, 2.0000, 3.0000, 4.0000]]],
-
-
- [[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
- [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
- [3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
- [4.0000, 4.0000, 4.0000, 4.0000, 4.0000]],
-
- [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
- [2.0000, 2.0000, 2.0244, 2.0000, 2.0000],
- [3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
- [4.0000, 4.0000, 4.0000, 4.0000, 4.0000]],
-
- [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
- [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
- [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
- [3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
- [4.0000, 4.0000, 4.0000, 4.0000, 4.0000]]]]]]).to(device)
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000, 0.0000]],
+
+ [[1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
+ [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
+ [1.0000, 1.0000, 1.0688, 1.0000, 1.0000],
+ [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
+ [1.0000, 1.0000, 1.0000, 1.0000, 1.0000]],
+
+ [[2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
+ [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
+ [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
+ [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
+ [2.0000, 2.0000, 2.0000, 2.0000, 2.0000]]],
+
+
+ [[[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000]],
+
+ [[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 1.8366, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000]],
+
+ [[0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000],
+ [0.0000, 1.0000, 2.0000, 3.0000, 4.0000]]],
+
+
+ [[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
+ [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
+ [3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
+ [4.0000, 4.0000, 4.0000, 4.0000, 4.0000]],
+
+ [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
+ [2.0000, 2.0000, 2.0244, 2.0000, 2.0000],
+ [3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
+ [4.0000, 4.0000, 4.0000, 4.0000, 4.0000]],
+
+ [[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
+ [1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
+ [2.0000, 2.0000, 2.0000, 2.0000, 2.0000],
+ [3.0000, 3.0000, 3.0000, 3.0000, 3.0000],
+ [4.0000, 4.0000, 4.0000, 4.0000, 4.0000]]]]]]).to(device)
coords, val = softargmax(input)
assert_allclose(val, expected_val)
assert_allclose(coords, expected_coord)