Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Feat] refactor tests for kornia.color (#759)

Edgar Ribaf064bfd6dd17
diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml
index bad71b443..7eb021fbb 100644
--- a/.github/workflows/docs.yml
+++ b/.github/workflows/docs.yml
@@ -9,7 +9,7 @@ on:
- cron: "0 4 * * *"
jobs:
- test-docs:
+ build-docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml
index d45e764d6..707b92437 100644
--- a/.github/workflows/format.yml
+++ b/.github/workflows/format.yml
@@ -9,7 +9,7 @@ on:
- cron: "0 4 * * *"
jobs:
- code-format:
+ linter:
runs-on: [ubuntu-latest]
steps:
diff --git a/Makefile b/Makefile
index 03f531d24..f23f61651 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,22 @@ test-cuda: FORCE
pytest -v --device cuda --dtype all --cov=kornia test/ --flake8 --mypy
test-module: FORCE
- pytest -v --device all --dtype all --cov=kornia test/$(module) --flake8 --mypy
+ pytest -v --device all --dtype all test/$(module) --flake8 --mypy
+
+test-jit: FORCE
+ pytest -v --device all --dtype all -m jit
+
+test-gradcheck: FORCE
+ pytest -v --device all --dtype all -m grad
+
+test-nn: FORCE
+ pytest -v --device all --dtype all -m nn
+
+test-quick: FORCE
+ pytest -v --device all --dtype all -m "not (jit or grad or nn)"
+
+test-slow: FORCE
+ pytest -v --device all --dtype all -m "(jit or grad or nn)"
lint: FORCE
pytest -v --flake8 -m flake8
@@ -23,6 +38,9 @@ mypy: FORCE
autopep8: FORCE
autopep8 --in-place --aggressive --recursive kornia/ test/ examples/
+doctest:
+ pytest -v --doctest-modules kornia/color #kornia/augmentation ... etc
+
docstyle: FORCE
pydocstyle kornia/
diff --git a/docs/source/conf.py b/docs/source/conf.py
index a5ec0b849..d9d4bb7df 100644
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -190,8 +190,8 @@ texinfo_documents = [
# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {
'python': ('https://docs.python.org/3/', None),
- 'numpy': ('http://docs.scipy.org/doc/numpy/', None),
- 'torch': ('http://pytorch.org/docs/master/', None),
+ 'numpy': ('http://numpy.org/doc/stable/', None),
+ 'torch': ('http://pytorch.org/docs/stable/', None),
}
examples_dir = os.path.join(current_path, "tutorials")
diff --git a/kornia/color/gray.py b/kornia/color/gray.py
index de09d2d34..eea5af407 100644
--- a/kornia/color/gray.py
+++ b/kornia/color/gray.py
@@ -1,119 +1,110 @@
import torch
import torch.nn as nn
-from .rgb import bgr_to_rgb
+from kornia.color.rgb import bgr_to_rgb
-class RgbToGrayscale(nn.Module):
- r"""convert RGB image to grayscale version of image.
-
- the image data is assumed to be in the range of (0, 1).
-
- args:
- input (torch.Tensor): RGB image to be converted to grayscale.
- returns:
- torch.Tensor: grayscale version of the image.
+def rgb_to_grayscale(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert a RGB image to grayscale version of image.
- shape:
- - input: :math:`(*, 3, H, W)`
- - output: :math:`(*, 1, H, W)`
+ The image data is assumed to be in the range of (0, 1).
- reference:
- https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
+ Args:
+ image (torch.Tensor): RGB image to be converted to grayscale with shape :math:`(*,3,H,W)`.
- Examples::
+ Returns:
+ torch.Tensor: grayscale version of the image with shape :math:`(*,1,H,W)`.
- >>> import torch
- >>> import kornia
+ Example:
>>> input = torch.rand(2, 3, 4, 5)
- >>> gray = kornia.color.RgbToGrayscale()
- >>> output = gray(input) # 2x1x4x5
+ >>> gray = rgb_to_grayscale(input) # 2x1x4x5
"""
+ if not isinstance(image, torch.Tensor):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
- def __init__(self) -> None:
- super(RgbToGrayscale, 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, input: torch.Tensor) -> torch.Tensor: # type: ignore
- return rgb_to_grayscale(input)
+ r, g, b = torch.chunk(image, chunks=3, dim=-3)
+ gray: torch.Tensor = 0.299 * r + 0.587 * g + 0.114 * b
+ return gray
-def rgb_to_grayscale(input: torch.Tensor) -> torch.Tensor:
- r"""Convert a RGB image to grayscale.
+def bgr_to_grayscale(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert a BGR image to grayscale.
- See :class:`~kornia.color.RgbToGrayscale` for details.
+ The image data is assumed to be in the range of (0, 1). First flips to RGB, then converts.
Args:
- input (torch.Tensor): RGB image to be converted to grayscale.
+ image (torch.Tensor): BGR image to be converted to grayscale with shape :math:`(*,3,H,W)`.
Returns:
- torch.Tensor: Grayscale version of the image.
+ torch.Tensor: grayscale version of the image with shape :math:`(*,1,H,W)`.
+
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> gray = bgr_to_grayscale(input) # 2x1x4x5
"""
- if not isinstance(input, torch.Tensor):
+ if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
- type(input)))
+ type(image)))
- if len(input.shape) < 3 and input.shape[-3] != 3:
+ if len(image.shape) < 3 or image.shape[-3] != 3:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
- .format(input.shape))
+ .format(image.shape))
- r, g, b = torch.chunk(input, chunks=3, dim=-3)
- gray: torch.Tensor = 0.299 * r + 0.587 * g + 0.114 * b
+ image_rgb = bgr_to_rgb(image)
+ gray: torch.Tensor = rgb_to_grayscale(image_rgb)
return gray
-class BgrToGrayscale(nn.Module):
- r"""convert BGR image to grayscale version of image.
-
- the image data is assumed to be in the range of (0, 1).
-
- args:
- input (torch.Tensor): BGR image to be converted to grayscale.
+class RgbToGrayscale(nn.Module):
+ r"""Module to convert a RGB image to grayscale version of image.
- returns:
- torch.Tensor: grayscale version of the image.
+ The image data is assumed to be in the range of (0, 1).
- shape:
- - input: :math:`(*, 3, H, W)`
+ Shape:
+ - image: :math:`(*, 3, H, W)`
- output: :math:`(*, 1, H, W)`
reference:
https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
- Examples::
-
- >>> import torch
- >>> import kornia
+ Example:
>>> input = torch.rand(2, 3, 4, 5)
- >>> gray = kornia.color.BgrToGrayscale()
+ >>> gray = RgbToGrayscale()
>>> output = gray(input) # 2x1x4x5
"""
def __init__(self) -> None:
- super(BgrToGrayscale, self).__init__()
+ super(RgbToGrayscale, self).__init__()
- def forward(self, input: torch.Tensor) -> torch.Tensor: # type: ignore
- return bgr_to_grayscale(input)
+ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ return rgb_to_grayscale(image)
-def bgr_to_grayscale(input: torch.Tensor) -> torch.Tensor:
- r"""Convert a BGR image to grayscale.
+class BgrToGrayscale(nn.Module):
+ r"""Module to convert a BGR image to grayscale version of image.
- See :class:`~kornia.color.BgrToGrayscale` for details.
+ The image data is assumed to be in the range of (0, 1). First flips to RGB, then converts.
- Args:
- input (torch.Tensor): BGR image to be converted to grayscale.
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 1, H, W)`
- Returns:
- torch.Tensor: Grayscale version of the image.
+ reference:
+ https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
+
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> gray = BgrToGrayscale()
+ >>> output = gray(input) # 2x1x4x5
"""
- if not torch.is_tensor(input):
- raise TypeError("Input type is not a torch.Tensor. Got {}".format(
- type(input)))
- if len(input.shape) < 3 and input.shape[-3] != 3:
- raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
- .format(input.shape))
+ def __init__(self) -> None:
+ super(BgrToGrayscale, self).__init__()
- input_rgb = bgr_to_rgb(input)
- gray: torch.Tensor = rgb_to_grayscale(input_rgb)
- return gray
+ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ return bgr_to_grayscale(image)
diff --git a/kornia/color/hls.py b/kornia/color/hls.py
index 9005f4ebe..c63d68ec1 100644
--- a/kornia/color/hls.py
+++ b/kornia/color/hls.py
@@ -1,56 +1,82 @@
+import math
+
import torch
import torch.nn as nn
+
import kornia
from kornia.constants import pi
-class HlsToRgb(nn.Module):
- r"""Convert image from HLS to Rgb
+def rgb_to_hls(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert a RGB image to HLS.
+
The image data is assumed to be in the range of (0, 1).
- args:
- image (torch.Tensor): HLS image to be converted to RGB.
+ Args:
+ image (torch.Tensor): RGB image to be converted to HLS with shape :math:`(*, 3, H, W)`.
- returns:
- torch.tensor: RGB version of the image.
+ Returns:
+ torch.Tensor: HLS version of the image with shape :math:`(*, 3, H, W)`.
- shape:
- - image: :math:`(*, 3, H, W)`
- - output: :math:`(*, 3, H, W)`
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = rgb_to_hls(input) # 2x3x4x5
+ """
+ if not isinstance(image, torch.Tensor):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
- reference:
- https://en.wikipedia.org/wiki/HSL_and_HSV
+ 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))
- Examples::
+ r: torch.Tensor = image[..., 0, :, :]
+ g: torch.Tensor = image[..., 1, :, :]
+ b: torch.Tensor = image[..., 2, :, :]
- >>> import torch
- >>> import kornia
- >>> input = torch.rand(2, 3, 4, 5)
- >>> rgb = kornia.color.HlsToRgb()
- >>> output = rgb(input) # 2x3x4x5
+ maxc: torch.Tensor = image.max(-3)[0]
+ minc: torch.Tensor = image.min(-3)[0]
- """
+ imax: torch.Tensor = image.max(-3)[1]
- def __init__(self) -> None:
- super(HlsToRgb, self).__init__()
+ l: torch.Tensor = (maxc + minc) / 2 # luminance
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
- return hls_to_rgb(image)
+ deltac: torch.Tensor = maxc - minc
+
+ s: torch.Tensor = torch.where(l < 0.5, deltac / (maxc + minc), deltac /
+ (torch.tensor(2.) - (maxc + minc))) # saturation
+
+ hi: torch.Tensor = torch.zeros_like(deltac)
+
+ hi[imax == 0] = (((g - b) / deltac) % 6)[imax == 0]
+ hi[imax == 1] = (((b - r) / deltac) + 2)[imax == 1]
+ hi[imax == 2] = (((r - g) / deltac) + 4)[imax == 2]
+
+ h: torch.Tensor = 2. * math.pi * (60. * hi) / 360. # hue [0, 2*pi]
+
+ image_hls: torch.Tensor = torch.stack([h, l, s], dim=-3)
+
+ image_hls[torch.isnan(image_hls)] = 0.
+
+ return image_hls
def hls_to_rgb(image: torch.Tensor) -> torch.Tensor:
- r"""Convert an HLS image to RGB
+ r"""Convert a HLS image to RGB.
+
The image data is assumed to be in the range of (0, 1).
Args:
- input (torch.Tensor): HLS Image to be converted to RGB.
-
+ image (torch.Tensor): HLS image to be converted to RGB woth shape :math:`(*, 3, H, W)`.
Returns:
- torch.Tensor: RGB version of the image.
- """
+ torch.Tensor: RGB version of the image with shape :math:`(*, 3, H, W)`.
- if not torch.is_tensor(image):
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = hls_to_rgb(input) # 2x3x4x5
+ """
+ if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(image)))
@@ -58,7 +84,7 @@ def hls_to_rgb(image: torch.Tensor) -> torch.Tensor:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
.format(image.shape))
- h: torch.Tensor = image[..., 0, :, :] * 360 / (2 * pi.to(image.device))
+ h: torch.Tensor = image[..., 0, :, :] * 360 / (2 * math.pi)
l: torch.Tensor = image[..., 1, :, :]
s: torch.Tensor = image[..., 2, :, :]
@@ -82,82 +108,53 @@ def hls_to_rgb(image: torch.Tensor) -> torch.Tensor:
class RgbToHls(nn.Module):
- r"""Convert image from RGB to HLS
- The image data is assumed to be in the range of (0, 1).
+ r"""Convert an image from RGB to HLS.
- args:
- image (torch.Tensor): RGB image to be converted to HLS.
+ The image data is assumed to be in the range of (0, 1).
- returns:
- torch.tensor: HLS version of the image.
+ Returns:
+ torch.Tensor: HLS version of the image.
- shape:
+ Shape:
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 3, H, W)`
- Examples::
-
- >>> import torch
- >>> import kornia
+ Examples:
>>> input = torch.rand(2, 3, 4, 5)
- >>> hls = kornia.color.RgbToHls()
+ >>> hls = RgbToHls()
>>> output = hls(input) # 2x3x4x5
-
"""
def __init__(self) -> None:
super(RgbToHls, self).__init__()
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
return rgb_to_hls(image)
-def rgb_to_hls(image: torch.Tensor) -> torch.Tensor:
- r"""Convert an RGB image to HLS
- The image data is assumed to be in the range of (0, 1).
-
- Args:
- input (torch.Tensor): RGB Image to be converted to HLS.
+class HlsToRgb(nn.Module):
+ r"""Convert an image from HLS to RGB.
+ The image data is assumed to be in the range of (0, 1).
Returns:
- torch.Tensor: HLS version of the image.
- """
-
- if not torch.is_tensor(image):
- 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))
-
- r: torch.Tensor = image[..., 0, :, :]
- g: torch.Tensor = image[..., 1, :, :]
- b: torch.Tensor = image[..., 2, :, :]
-
- maxc: torch.Tensor = image.max(-3)[0]
- minc: torch.Tensor = image.min(-3)[0]
-
- imax: torch.Tensor = image.max(-3)[1]
-
- l: torch.Tensor = (maxc + minc) / 2 # luminance
-
- deltac: torch.Tensor = maxc - minc
-
- s: torch.Tensor = torch.where(l < 0.5, deltac / (maxc + minc), deltac /
- (torch.tensor(2.) - (maxc + minc))) # saturation
-
- hi: torch.Tensor = torch.zeros_like(deltac)
+ torch.Tensor: RGB version of the image.
- hi[imax == 0] = (((g - b) / deltac) % 6)[imax == 0]
- hi[imax == 1] = (((b - r) / deltac) + 2)[imax == 1]
- hi[imax == 2] = (((r - g) / deltac) + 4)[imax == 2]
+ Shape:
+ - input: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
- h: torch.Tensor = 2. * pi.to(image.device) * (60. * hi) / 360. # hue [0, 2*pi]
+ Reference:
+ https://en.wikipedia.org/wiki/HSL_and_HSV
- image_hls: torch.Tensor = torch.stack([h, l, s], dim=-3)
+ Examples:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> rgb = HlsToRgb()
+ >>> output = rgb(input) # 2x3x4x5
+ """
- image_hls[torch.isnan(image_hls)] = 0.
+ def __init__(self) -> None:
+ super(HlsToRgb, self).__init__()
- return image_hls
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
+ return hls_to_rgb(image)
diff --git a/kornia/color/hsv.py b/kornia/color/hsv.py
index 5cdaf2be7..0286a793b 100644
--- a/kornia/color/hsv.py
+++ b/kornia/color/hsv.py
@@ -1,52 +1,79 @@
+import math
+
import torch
import torch.nn as nn
-from kornia.constants import pi
-class HsvToRgb(nn.Module):
- r"""Convert image from HSV to Rgb
+def rgb_to_hsv(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert an image from RGB to HSV.
+
The image data is assumed to be in the range of (0, 1).
- args:
- image (torch.Tensor): HSV image to be converted to RGB.
+ Args:
+ image (torch.Tensor): RGB Image to be converted to HSV with shape of :math:`(*, 3, H, W)`.
- returns:
- torch.tensor: RGB version of the image.
+ Returns:
+ torch.Tensor: HSV version of the image with shape of :math:`(*, 3, H, W)`.
- shape:
- - image: :math:`(*, 3, H, W)`
- - output: :math:`(*, 3, H, W)`
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = rgb_to_hsv(input) # 2x3x4x5
+ """
+ if not isinstance(image, torch.Tensor):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
- Examples::
+ 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))
- >>> import torch
- >>> import kornia
- >>> input = torch.rand(2, 3, 4, 5)
- >>> rgb = kornia.color.HsvToRgb()
- >>> output = rgb(input) # 2x3x4x5
+ maxc, max_indices = image.max(-3)
+ minc: torch.Tensor = image.min(-3)[0]
- """
+ v: torch.Tensor = maxc # brightness
- def __init__(self) -> None:
- super(HsvToRgb, self).__init__()
+ deltac: torch.Tensor = maxc - minc
+ s: torch.Tensor = deltac / (v + 1e-31)
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
- return hsv_to_rgb(image)
+ # avoid division by zero
+ deltac = torch.where(
+ deltac == 0, torch.ones_like(deltac), deltac)
+
+ rc, gc, bc = torch.unbind(maxc.unsqueeze(-3) - image, dim=-3)
+
+ h = torch.stack([
+ bc - gc,
+ 2.0 * deltac + rc - bc,
+ 4.0 * deltac + gc - rc,
+ ], dim=-3)
+
+ h = torch.gather(h, dim=-3, index=max_indices[..., None, :, :])
+ h = h.squeeze(-3)
+ h = h / deltac
+
+ h = (h / 6.0) % 1.0
+
+ h = 2 * math.pi * h
+
+ return torch.stack([h, s, v], dim=-3)
def hsv_to_rgb(image: torch.Tensor) -> torch.Tensor:
- r"""Convert an HSV image to RGB
+ r"""Convert an image from HSV to RGB.
+
The image data is assumed to be in the range of (0, 1).
Args:
- input (torch.Tensor): HSV Image to be converted to RGB.
-
+ image (torch.Tensor): HSV Image to be converted to HSV with shape of :math:`(*, 3, H, W)`.
Returns:
- torch.Tensor: RGB version of the image.
- """
+ torch.Tensor: RGB version of the image with shape of :math:`(*, 3, H, W)`.
- if not torch.is_tensor(image):
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = hsv_to_rgb(input) # 2x3x4x5
+ """
+ if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(image)))
@@ -54,7 +81,7 @@ def hsv_to_rgb(image: torch.Tensor) -> torch.Tensor:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"
.format(image.shape))
- h: torch.Tensor = image[..., 0, :, :] / (2 * pi.to(image.device))
+ h: torch.Tensor = image[..., 0, :, :] / (2 * math.pi)
s: torch.Tensor = image[..., 1, :, :]
v: torch.Tensor = image[..., 2, :, :]
@@ -78,80 +105,50 @@ def hsv_to_rgb(image: torch.Tensor) -> torch.Tensor:
class RgbToHsv(nn.Module):
- r"""Convert image from RGB to HSV.
+ r"""Convert an image from RGB to HSV.
The image data is assumed to be in the range of (0, 1).
- args:
- image (torch.Tensor): RGB image to be converted to HSV.
-
- returns:
+ Returns:
torch.tensor: HSV version of the image.
- shape:
+ Shape:
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 3, H, W)`
- Examples::
-
- >>> import torch
- >>> import kornia
+ Example:
>>> input = torch.rand(2, 3, 4, 5)
- >>> hsv = kornia.color.RgbToHsv()
+ >>> hsv = RgbToHsv()
>>> output = hsv(input) # 2x3x4x5
-
"""
def __init__(self) -> None:
super(RgbToHsv, self).__init__()
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
return rgb_to_hsv(image)
-def rgb_to_hsv(image: torch.Tensor) -> torch.Tensor:
- r"""Convert an RGB image to HSV.
+class HsvToRgb(nn.Module):
+ r"""Convert an image from HSV to RGB.
- Args:
- input (torch.Tensor): RGB Image to be converted to HSV.
+ The image data is assumed to be in the range of (0, 1).
Returns:
- torch.Tensor: HSV version of the image.
- """
-
- if not torch.is_tensor(image):
- 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))
-
- maxc, max_indices = image.max(-3)
- minc: torch.Tensor = image.min(-3)[0]
-
- v: torch.Tensor = maxc # brightness
-
- deltac: torch.Tensor = maxc - minc
- s: torch.Tensor = deltac / (v + 1e-31)
-
- # avoid division by zero
- deltac = torch.where(
- deltac == 0, torch.ones_like(deltac), deltac)
-
- rc, gc, bc = torch.unbind(maxc.unsqueeze(-3) - image, dim=-3)
+ torch.Tensor: RGB version of the image.
- h = torch.stack([
- bc - gc,
- 2.0 * deltac + rc - bc,
- 4.0 * deltac + gc - rc,
- ], dim=-3)
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
- h = torch.gather(h, dim=-3, index=max_indices[..., None, :, :])
- h = h.squeeze(-3)
- h = h / deltac
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> rgb = HsvToRgb()
+ >>> output = rgb(input) # 2x3x4x5
+ """
- h = (h / 6.0) % 1.0
+ def __init__(self) -> None:
+ super(HsvToRgb, self).__init__()
- h = 2 * pi.to(image.device) * h
- return torch.stack([h, s, v], dim=-3)
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
+ return hsv_to_rgb(image)
diff --git a/kornia/color/luv.py b/kornia/color/luv.py
index 60e4d093e..a16babd56 100644
--- a/kornia/color/luv.py
+++ b/kornia/color/luv.py
@@ -13,93 +13,24 @@ https://github.com/scikit-image/scikit-image/blob/a48bf6774718c64dade4548153ae16
"""
-class RgbToLuv(nn.Module):
- r"""Converts an image from RGB to Luv
-
- The image data is assumed to be in the range of :math:`[0, 1]`. Luv
- color is computed using the D65 illuminant and Observer 2.
-
- args:
- image (torch.Tensor): RGB image to be converted to Luv.
-
- returns:
- torch.Tensor: Luv version of the image.
-
- shape:
- - image: :math:`(*, 3, H, W)`
- - output: :math:`(*, 3, H, W)`
-
- Examples:
- >>> input = torch.rand(2, 3, 4, 5)
- >>> luv = kornia.color.RgbToLuv()
- >>> output = luv(input) # 2x3x4x5
-
- Reference:
- [1] https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
-
- [2] https://www.easyrgb.com/en/math.php
-
- [3] http://www.poynton.com/ColorFAQ.html
- """
-
- def __init__(self) -> None:
-
- super(RgbToLuv, self).__init__()
-
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
-
- return rgb_to_luv(image)
-
-
-class LuvToRgb(nn.Module):
- r"""Converts an image from Luv to RGB
-
- args:
- image (torch.Tensor): Luv image to be converted to RGB.
-
- returns:
- torch.Tensor: RGB version of the image.
-
- shape:
- - image: :math:`(*, 3, H, W)`
- - output: :math:`(*, 3, H, W)`
-
- Examples:
- >>> input = torch.rand(2, 3, 4, 5)
- >>> rgb = kornia.color.LuvToRgb()
- >>> output = rgb(input) # 2x3x4x5
-
- References:
- [1] https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
-
- [2] https://www.easyrgb.com/en/math.php
-
- [3] http://www.poynton.com/ColorFAQ.html
- """
-
- def __init__(self) -> None:
-
- super(LuvToRgb, self).__init__()
-
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
-
- return luv_to_rgb(image)
-
-
def rgb_to_luv(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
r"""Converts a RGB image to Luv.
- See :class:`~kornia.color.RgbToLuv` for details.
+ The image data is assumed to be in the range of :math:`[0, 1]`. Luv
+ color is computed using the D65 illuminant and Observer 2.
Args:
- image (torch.Tensor): RGB image
- eps (float): for numerically stability when dividing. Default: 1e-8.
+ image (torch.Tensor): RGB Image to be converted to Luv with shape :math:`(*, 3, H, W)`.
+ eps (float): for numerically stability when dividing. Default: 1e-12.
Returns:
- torch.Tensor : Luv image
- """
+ torch.Tensor: Luv version of the image with shape :math:`(*, 3, H, W)`.
- if not torch.is_tensor(image):
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = rgb_to_luv(input) # 2x3x4x5
+ """
+ if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(image)))
@@ -147,16 +78,18 @@ def rgb_to_luv(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
def luv_to_rgb(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
r"""Converts a Luv image to RGB.
- See :class:`~kornia.color.LuvToRgb` for details.
-
Args:
- image (torch.Tensor): Luv image
- eps (float): for numerically stability when dividing. Default: 1e-8.
+ image (torch.Tensor): Luv image to be converted to RGB with shape :math:`(*, 3, H, W)`.
+ eps (float): for numerically stability when dividing. Default: 1e-12.
Returns:
- torch.Tensor : RGB image
+ torch.Tensor: Luv version of the image with shape :math:`(*, 3, H, W)`.
+
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = luv_to_rgb(input) # 2x3x4x5
"""
- if not torch.is_tensor(image):
+ if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(image)))
@@ -167,10 +100,12 @@ def luv_to_rgb(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
L: torch.Tensor = image[..., 0, :, :]
u: torch.Tensor = image[..., 1, :, :]
v: torch.Tensor = image[..., 2, :, :]
+
# Convert from Luv to XYZ
y: torch.Tensor = torch.where(L > 7.999625,
torch.pow((L + 16) / 116, 3.0),
L / 903.3)
+
# Compute white point
xyz_ref_white: Tuple[float, float, float] = (0.95047, 1., 1.08883)
u_w: float = (4 * xyz_ref_white[0]) / (xyz_ref_white[0] + 15 * xyz_ref_white[1] + 3 * xyz_ref_white[2])
@@ -186,6 +121,7 @@ def luv_to_rgb(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
xyz_im: torch.Tensor = torch.stack((x, y, z), -3)
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, :, :]
@@ -198,3 +134,66 @@ def luv_to_rgb(image: torch.Tensor, eps: float = 1e-12) -> torch.Tensor:
rgb_im: torch.Tensor = torch.stack((r, g, b), dim=-3)
return rgb_im
+
+
+class RgbToLuv(nn.Module):
+ r"""Converts an image from RGB to Luv.
+
+ The image data is assumed to be in the range of :math:`[0, 1]`. Luv
+ color is computed using the D65 illuminant and Observer 2.
+
+ Returns:
+ torch.Tensor: Luv version of the image.
+
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ Examples:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> luv = RgbToLuv()
+ >>> output = luv(input) # 2x3x4x5
+
+ Reference:
+ [1] https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
+
+ [2] https://www.easyrgb.com/en/math.php
+
+ [3] http://www.poynton.com/ColorFAQ.html
+ """
+
+ def __init__(self) -> None:
+ super(RgbToLuv, self).__init__()
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
+ return rgb_to_luv(image)
+
+
+class LuvToRgb(nn.Module):
+ r"""Converts an image from Luv to RGB.
+
+ Returns:
+ torch.Tensor: RGB version of the image.
+
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ Examples:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> rgb = LuvToRgb()
+ >>> output = rgb(input) # 2x3x4x5
+
+ References:
+ [1] https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
+
+ [2] https://www.easyrgb.com/en/math.php
+
+ [3] http://www.poynton.com/ColorFAQ.html
+ """
+
+ def __init__(self) -> None:
+ super(LuvToRgb, self).__init__()
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
+ return luv_to_rgb(image)
diff --git a/kornia/color/rgb.py b/kornia/color/rgb.py
index f11a608c3..df29e0634 100644
--- a/kornia/color/rgb.py
+++ b/kornia/color/rgb.py
@@ -1,37 +1,39 @@
-from typing import cast
+from typing import Union, cast
import torch
import torch.nn as nn
-from typing import Union
def rgb_to_bgr(image: torch.Tensor) -> torch.Tensor:
r"""Convert a RGB image to BGR.
- See :class:`~kornia.color.RgbToBgr` for details.
-
Args:
- image (torch.Tensor): RGB Image to be converted to BGR.
+ image (torch.Tensor): RGB Image to be converted to BGRof of shape :math:`(*,3,H,W)`.
Returns:
- torch.Tensor: BGR version of the image.
- """
+ torch.Tensor: BGR version of the image with shape of shape :math:`(*,3,H,W)`.
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = rgb_to_bgr(input) # 2x3x4x5
+ """
return bgr_to_rgb(image)
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.
+ image (torch.Tensor): BGR Image to be converted to BGR of shape :math:`(*,3,H,W)`.
Returns:
- torch.Tensor: RGB version of the image.
+ torch.Tensor: RGB version of the image with shape of shape :math:`(*,3,H,W)`.
+
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = bgr_to_rgb(input) # 2x3x4x5
"""
- if not torch.is_tensor(image):
+ if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(image)))
@@ -41,24 +43,27 @@ def bgr_to_rgb(image: torch.Tensor) -> torch.Tensor:
# flip image channels
out: torch.Tensor = image.flip(-3)
-
return out
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.RgbToRgba` for details.
+ r"""Convert an image from RGB to RGBA.
Args:
- image (torch.Tensor): RGB Image to be converted to RGBA.
- alpha_val (float, torch.Tensor): A float number for the alpha value.
+ image (torch.Tensor): RGB Image to be converted to RGBA of shape :math:`(*,3,H,W)`.
+ alpha_val (float, torch.Tensor): A float number for the alpha value or a tensor
+ of shape :math:`(*,1,H,W)`.
Returns:
- torch.Tensor: RGBA version of the image.
- """
+ torch.Tensor: RGBA version of the image with shape :math:`(*,4,H,W)`.
+
+ .. note:: The current functionality is NOT supported by Torchscript.
- if not torch.is_tensor(image):
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = rgb_to_rgba(input, 1.) # 2x4x4x5
+ """
+ if not isinstance(image, torch.Tensor):
raise TypeError(f"Input type is not a torch.Tensor. Got {type(image)}")
if len(image.shape) < 3 or image.shape[-3] != 3:
@@ -79,19 +84,23 @@ def rgb_to_rgba(image: torch.Tensor, alpha_val: Union[float, torch.Tensor]) -> t
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.
+ r"""Convert an image from BGR to RGBA.
Args:
- image (torch.Tensor): BGR Image to be converted to RGBA.
- alpha_val (float, torch.Tensor): A float number for the alpha value.
+ image (torch.Tensor): BGR Image to be converted to RGBA of shape :math:`(*,3,H,W)`.
+ alpha_val (float, torch.Tensor): A float number for the alpha value or a tensor
+ of shape :math:`(*,1,H,W)`.
Returns:
- torch.Tensor: RGBA version of the image.
- """
+ torch.Tensor: RGBA version of the image with shape :math:`(*,4,H,W)`.
+
+ .. note:: The current functionality is NOT supported by Torchscript.
- if not torch.is_tensor(image):
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = bgr_to_rgba(input, 1.) # 2x4x4x5
+ """
+ if not isinstance(image, torch.Tensor):
raise TypeError(f"Input type is not a torch.Tensor. Got {type(image)}")
if len(image.shape) < 3 or image.shape[-3] != 3:
@@ -99,24 +108,26 @@ def bgr_to_rgba(image: torch.Tensor, alpha_val: Union[float, torch.Tensor]) -> t
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.
+ r"""Convert an image from RGBA to RGB.
Args:
- image (torch.Tensor): RGBA Image to be converted to RGB.
+ image (torch.Tensor): RGBA Image to be converted to RGB of shape :math:`(*,4,H,W)`.
Returns:
- torch.Tensor: RGB version of the image.
- """
+ torch.Tensor: RGB version of the image with shape :math:`(*,3,H,W)`.
- if not torch.is_tensor(image):
+ Example:
+ >>> input = torch.rand(2, 4, 4, 5)
+ >>> output = rgba_to_rgb(input) # 2x3x4x5
+ """
+ if not isinstance(image, torch.Tensor):
raise TypeError(f"Input type is not a torch.Tensor. Got {type(image)}")
if len(image.shape) < 3 or image.shape[-3] != 4:
@@ -135,18 +146,19 @@ def rgba_to_rgb(image: torch.Tensor) -> torch.Tensor:
def rgba_to_bgr(image: torch.Tensor) -> torch.Tensor:
- r"""Convert image from RGBA to BGR.
-
- See :class:`~kornia.color.RgbaToBgr` for details.
+ r"""Convert an image from RGBA to BGR.
Args:
- image (torch.Tensor): RGBA Image to be converted to BGR.
+ image (torch.Tensor): RGBA Image to be converted to BGR of shape :math:`(*,4,H,W)`.
Returns:
- torch.Tensor: BGR version of the image.
- """
+ torch.Tensor: RGB version of the image with shape :math:`(*,3,H,W)`.
- if not torch.is_tensor(image):
+ Example:
+ >>> input = torch.rand(2, 4, 4, 5)
+ >>> output = rgba_to_bgr(input) # 2x3x4x5
+ """
+ if not isinstance(image, torch.Tensor):
raise TypeError(f"Input type is not a torch.Tensor. Got {type(image)}")
if len(image.shape) < 3 or image.shape[-3] != 4:
@@ -169,25 +181,21 @@ class BgrToRgb(nn.Module):
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 3, H, W)`
- Examples::
-
- >>> import torch
- >>> import kornia
+ Example:
>>> input = torch.rand(2, 3, 4, 5)
- >>> rgb = kornia.color.BgrToRgb()
+ >>> rgb = BgrToRgb()
>>> output = rgb(input) # 2x3x4x5
-
"""
def __init__(self) -> None:
super(BgrToRgb, self).__init__()
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
return bgr_to_rgb(image)
class RgbToBgr(nn.Module):
- r"""Convert image from RGB to BGR.
+ r"""Convert an image from RGB to BGR.
The image data is assumed to be in the range of (0, 1).
@@ -198,42 +206,40 @@ class RgbToBgr(nn.Module):
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 3, H, W)`
- Examples::
-
- >>> import torch
- >>> import kornia
+ Example:
>>> input = torch.rand(2, 3, 4, 5)
- >>> bgr = kornia.color.RgbToBgr()
+ >>> bgr = RgbToBgr()
>>> output = bgr(input) # 2x3x4x5
-
"""
def __init__(self) -> None:
super(RgbToBgr, self).__init__()
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
return rgb_to_bgr(image)
class RgbToRgba(nn.Module):
- r"""Convert image from RGB to RGBA.
+ r"""Convert an 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.
+ alpha_val (float, torch.Tensor): A float number for the alpha value or a tensor
+ of shape :math:`(*,1,H,W)`.
Returns:
- torch.Tensor: RGBA version of the image.
+ torch.Tensor: RGBA version of the image with shape :math:`(*,4,H,W)`.
Shape:
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 4, H, W)`
- Examples::
+ .. note:: The current functionality is NOT supported by Torchscript.
+ Example:
>>> input = torch.rand(2, 3, 4, 5)
- >>> rgba = kornia.color.RgbToRgba(1.)
+ >>> rgba = RgbToRgba(1.)
>>> output = rgba(input) # 2x4x4x5
"""
@@ -241,29 +247,31 @@ class RgbToRgba(nn.Module):
super(RgbToRgba, self).__init__()
self.alpha_val = alpha_val
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
return rgb_to_rgba(image, self.alpha_val)
class BgrToRgba(nn.Module):
- r"""Convert image from BGR to RGBA.
+ r"""Convert an image from BGR to RGBA.
- Add an alpha channel to existing BGR image.
+ Add an alpha channel to existing RGB image.
Args:
- alpha_val (float, torch.Tensor): A float number for the alpha value.
+ alpha_val (float, torch.Tensor): A float number for the alpha value or a tensor
+ of shape :math:`(*,1,H,W)`.
Returns:
- torch.Tensor: RGBA version of the image.
+ torch.Tensor: RGBA version of the image with shape :math:`(*,4,H,W)`.
Shape:
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 4, H, W)`
- Examples::
+ .. note:: The current functionality is NOT supported by Torchscript.
+ Example:
>>> input = torch.rand(2, 3, 4, 5)
- >>> rgba = kornia.color.BgrToRgba(1.)
+ >>> rgba = BgrToRgba(1.)
>>> output = rgba(input) # 2x4x4x5
"""
@@ -271,57 +279,55 @@ class BgrToRgba(nn.Module):
super(BgrToRgba, self).__init__()
self.alpha_val = alpha_val
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
return rgb_to_rgba(image, self.alpha_val)
class RgbaToRgb(nn.Module):
- r"""Convert image from RGBA to RGB.
+ r"""Convert an image from RGBA to RGB.
Remove an alpha channel from RGB image.
- returns:
+ Returns:
torch.Tensor: RGB version of the image.
- shape:
+ Shape:
- image: :math:`(*, 4, H, W)`
- output: :math:`(*, 3, H, W)`
- Examples::
-
+ Example:
>>> input = torch.rand(2, 4, 4, 5)
- >>> rgba = kornia.color.RgbaToRgb()
+ >>> rgba = RgbaToRgb()
>>> output = rgba(input) # 2x3x4x5
"""
def __init__(self) -> None:
super(RgbaToRgb, self).__init__()
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
return rgba_to_rgb(image)
class RgbaToBgr(nn.Module):
- r"""Convert image from RGBA to BGR.
+ r"""Convert an image from RGBA to BGR.
Remove an alpha channel from BGR image.
- returns:
+ Returns:
torch.Tensor: BGR version of the image.
- shape:
+ Shape:
- image: :math:`(*, 4, H, W)`
- output: :math:`(*, 3, H, W)`
- Examples::
-
+ Example:
>>> input = torch.rand(2, 4, 4, 5)
- >>> rgba = kornia.color.RgbaToBgr()
+ >>> rgba = RgbaToBgr()
>>> output = rgba(input) # 2x3x4x5
"""
def __init__(self) -> None:
super(RgbaToBgr, self).__init__()
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
return rgba_to_bgr(image)
diff --git a/kornia/color/xyz.py b/kornia/color/xyz.py
index ce371fbb1..ddd3f12b1 100644
--- a/kornia/color/xyz.py
+++ b/kornia/color/xyz.py
@@ -1,82 +1,21 @@
-from typing import Union
-
import torch
import torch.nn as nn
-class RgbToXyz(nn.Module):
- r"""Converts an image from RGB to XYZ
-
- The image data is assumed to be in the range of (0, 1).
-
- args:
- image (torch.Tensor): RGB image to be converted to XYZ.
-
- returns:
- torch.Tensor: XYZ version of the image.
-
- shape:
- - image: :math:`(*, 3, H, W)`
- - output: :math:`(*, 3, H, W)`
-
- Examples:
- >>> input = torch.rand(2, 3, 4, 5)
- >>> xyz = kornia.color.RgbToXyz()
- >>> output = xyz(input) # 2x3x4x5
-
- Reference:
- [1] https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
- """
-
- def __init__(self) -> None:
- super(RgbToXyz, self).__init__()
-
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
- return rgb_to_xyz(image)
-
-
-class XyzToRgb(nn.Module):
- r"""Converts an image from XYZ to RGB
-
- args:
- image (torch.Tensor): XYZ image to be converted to RGB.
-
- returns:
- torch.Tensor: RGB version of the image.
-
- shape:
- - image: :math:`(*, 3, H, W)`
- - output: :math:`(*, 3, H, W)`
-
- Examples:
- >>> input = torch.rand(2, 3, 4, 5)
- >>> rgb = kornia.color.XyzToRgb()
- >>> output = rgb(input) # 2x3x4x5
-
- Reference:
- [1] https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
- """
-
- def __init__(self) -> None:
- super(XyzToRgb, self).__init__()
-
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
- return xyz_to_rgb(image)
-
-
def rgb_to_xyz(image: torch.Tensor) -> torch.Tensor:
r"""Converts a RGB image to XYZ.
- See :class:`~kornia.color.RgbToXyz` for details.
-
Args:
- image (torch.Tensor): RGB Image to be converted to XYZ.
+ image (torch.Tensor): RGB Image to be converted to XYZ with shape :math:`(*, 3, H, W)`.
Returns:
- torch.Tensor: XYZ version of the image.
- """
+ torch.Tensor: XYZ version of the image with shape :math:`(*, 3, H, W)`.
- if not torch.is_tensor(image):
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = rgb_to_xyz(input) # 2x3x4x5
+ """
+ if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(image)))
@@ -92,7 +31,7 @@ def rgb_to_xyz(image: torch.Tensor) -> torch.Tensor:
y: torch.Tensor = 0.212671 * r + 0.715160 * g + 0.072169 * b
z: torch.Tensor = 0.019334 * r + 0.119193 * g + 0.950227 * b
- out: torch.Tensor = torch.stack((x, y, z), -3)
+ out: torch.Tensor = torch.stack([x, y, z], -3)
return out
@@ -100,15 +39,17 @@ def rgb_to_xyz(image: torch.Tensor) -> torch.Tensor:
def xyz_to_rgb(image: torch.Tensor) -> torch.Tensor:
r"""Converts a XYZ image to RGB.
- See :class:`~kornia.color.XyzToRgb` for details.
-
Args:
- image (torch.Tensor): XYZ Image to be converted to RGB.
+ image (torch.Tensor): XYZ Image to be converted to RGB with shape :math:`(*, 3, H, W)`.
Returns:
- torch.Tensor: RGB version of the image.
+ torch.Tensor: RGB version of the image with shape :math:`(*, 3, H, W)`.
+
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = xyz_to_rgb(input) # 2x3x4x5
"""
- if not torch.is_tensor(image):
+ if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(image)))
@@ -124,6 +65,60 @@ def xyz_to_rgb(image: torch.Tensor) -> torch.Tensor:
g: torch.Tensor = -0.9692549499965682 * x + 1.8759900014898907 * y + 0.0415559265582928 * z
b: torch.Tensor = 0.0556466391351772 * x + -0.2040413383665112 * y + 1.0573110696453443 * z
- out: torch.Tensor = torch.stack((r, g, b), dim=-3)
+ out: torch.Tensor = torch.stack([r, g, b], dim=-3)
return out
+
+
+class RgbToXyz(nn.Module):
+ r"""Converts an image from RGB to XYZ.
+
+ The image data is assumed to be in the range of (0, 1).
+
+ Returns:
+ torch.Tensor: XYZ version of the image.
+
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ Examples:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> xyz = RgbToXyz()
+ >>> output = xyz(input) # 2x3x4x5
+
+ Reference:
+ [1] https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
+ """
+
+ def __init__(self) -> None:
+ super(RgbToXyz, self).__init__()
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
+ return rgb_to_xyz(image)
+
+
+class XyzToRgb(nn.Module):
+ r"""Converts an image from XYZ to RGB.
+
+ Returns:
+ torch.Tensor: RGB version of the image.
+
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ Examples:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> rgb = XyzToRgb()
+ >>> output = rgb(input) # 2x3x4x5
+
+ Reference:
+ [1] https://docs.opencv.org/4.0.1/de/d25/imgproc_color_conversions.html
+ """
+
+ def __init__(self) -> None:
+ super(XyzToRgb, self).__init__()
+
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
+ return xyz_to_rgb(image)
diff --git a/kornia/color/ycbcr.py b/kornia/color/ycbcr.py
index fdec85e69..22591bbff 100644
--- a/kornia/color/ycbcr.py
+++ b/kornia/color/ycbcr.py
@@ -2,50 +2,54 @@ import torch
import torch.nn as nn
-class YcbcrToRgb(nn.Module):
- r"""Convert image from YCbCr to Rgb
- The image data is assumed to be in the range of (0, 1).
-
- args:
- image (torch.Tensor): YCbCr image to be converted to RGB.
-
- returns:
- torch.tensor: RGB version of the image.
+def rgb_to_ycbcr(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert an RGB image to YCbCr.
- shape:
- - image: :math:`(*, 3, H, W)`
- - output: :math:`(*, 3, H, W)`
+ Args:
+ image (torch.Tensor): RGB Image to be converted to YCbCr with shape :math:`(*, 3, H, W)`.
- Examples::
+ Returns:
+ torch.Tensor: YCbCr version of the image with shape :math:`(*, 3, H, W)`.
- >>> import torch
- >>> import kornia
+ Examples:
>>> input = torch.rand(2, 3, 4, 5)
- >>> rgb = kornia.color.YcbcrToRgb()
- >>> output = rgb(input) # 2x3x4x5
-
+ >>> output = rgb_to_ycbcr(input) # 2x3x4x5
"""
+ if not isinstance(image, torch.Tensor):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
- def __init__(self) -> None:
- super(YcbcrToRgb, 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 ycbcr_to_rgb(image)
+ r: torch.Tensor = image[..., 0, :, :]
+ g: torch.Tensor = image[..., 1, :, :]
+ b: torch.Tensor = image[..., 2, :, :]
+
+ delta: float = 0.5
+ y: torch.Tensor = 0.299 * r + 0.587 * g + 0.114 * b
+ cb: torch.Tensor = (b - y) * 0.564 + delta
+ cr: torch.Tensor = (r - y) * 0.713 + delta
+ return torch.stack([y, cb, cr], -3)
def ycbcr_to_rgb(image: torch.Tensor) -> torch.Tensor:
- r"""Convert an YCbCr image to RGB
+ r"""Convert an YCbCr image to RGB.
+
The image data is assumed to be in the range of (0, 1).
Args:
- image (torch.Tensor): YCbCr Image to be converted to RGB.
-
+ image (torch.Tensor): YCbCr Image to be converted to RGB with shape :math:`(*, 3, H, W)`.
Returns:
- torch.Tensor: RGB version of the image.
- """
+ torch.Tensor: RGB version of the image with shape :math:`(*, 3, H, W)`.
- if not torch.is_tensor(image):
+ Examples:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = ycbcr_to_rgb(input) # 2x3x4x5
+ """
+ if not isinstance(image, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(image)))
@@ -57,71 +61,61 @@ def ycbcr_to_rgb(image: torch.Tensor) -> torch.Tensor:
cb: torch.Tensor = image[..., 1, :, :]
cr: torch.Tensor = image[..., 2, :, :]
- delta: float = .5
+ delta: float = 0.5
cb_shifted: torch.Tensor = cb - delta
cr_shifted: torch.Tensor = cr - delta
r: torch.Tensor = y + 1.403 * cr_shifted
- g: torch.Tensor = y - .714 * cr_shifted - .344 * cb_shifted
+ g: torch.Tensor = y - 0.714 * cr_shifted - 0.344 * cb_shifted
b: torch.Tensor = y + 1.773 * cb_shifted
- return torch.stack((r, g, b), -3)
+ return torch.stack([r, g, b], -3)
class RgbToYcbcr(nn.Module):
- r"""Convert image from RGB to YCbCr
- The image data is assumed to be in the range of (0, 1).
+ r"""Convert an image from RGB to YCbCr.
- args:
- image (torch.Tensor): RGB image to be converted to YCbCr.
+ The image data is assumed to be in the range of (0, 1).
- returns:
- torch.tensor: YCbCr version of the image.
+ Returns:
+ torch.Tensor: YCbCr version of the image.
- shape:
+ Shape:
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 3, H, W)`
- Examples::
-
- >>> import torch
- >>> import kornia
+ Examples:
>>> input = torch.rand(2, 3, 4, 5)
- >>> ycbcr = kornia.color.RgbToYcbcr()
+ >>> ycbcr = RgbToYcbcr()
>>> output = ycbcr(input) # 2x3x4x5
-
"""
def __init__(self) -> None:
super(RgbToYcbcr, self).__init__()
- def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
return rgb_to_ycbcr(image)
-def rgb_to_ycbcr(image: torch.Tensor) -> torch.Tensor:
- r"""Convert an RGB image to YCbCr.
+class YcbcrToRgb(nn.Module):
+ r"""Convert an image from YCbCr to Rgb.
- Args:
- image (torch.Tensor): RGB Image to be converted to YCbCr.
+ The image data is assumed to be in the range of (0, 1).
Returns:
- torch.Tensor: YCbCr version of the image.
- """
+ 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)))
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
- 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))
+ Examples:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> rgb = YcbcrToRgb()
+ >>> output = rgb(input) # 2x3x4x5
+ """
- r: torch.Tensor = image[..., 0, :, :]
- g: torch.Tensor = image[..., 1, :, :]
- b: torch.Tensor = image[..., 2, :, :]
+ def __init__(self) -> None:
+ super(YcbcrToRgb, self).__init__()
- delta = .5
- y: torch.Tensor = .299 * r + .587 * g + .114 * b
- cb: torch.Tensor = (b - y) * .564 + delta
- cr: torch.Tensor = (r - y) * .713 + delta
- return torch.stack((y, cb, cr), -3)
+ def forward(self, image: torch.Tensor) -> torch.Tensor:
+ return ycbcr_to_rgb(image)
diff --git a/kornia/color/yuv.py b/kornia/color/yuv.py
index bb583d671..206d111fd 100644
--- a/kornia/color/yuv.py
+++ b/kornia/color/yuv.py
@@ -1,109 +1,123 @@
-
import torch
import torch.nn as nn
-import torch.nn.functional as F
-from typing import Tuple
-class RgbToYuv(nn.Module):
- r"""Convert image from RGB to YUV
+def rgb_to_yuv(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert an RGB image to YUV.
+
The image data is assumed to be in the range of (0, 1).
- args:
- image (torch.Tensor): RGB image to be converted to YUV.
- returns:
- torch.tensor: YUV version of the image.
- shape:
- - image: :math:`(*, 3, H, W)`
- - output: :math:`(*, 3, H, W)`
- Examples::
+ Args:
+ image (torch.Tensor): RGB Image to be converted to YUV with shape :math:`(*, 3, H, W)`.
+
+ Returns:
+ torch.Tensor: YUV version of the image with shape :math:`(*, 3, H, W)`.
+
+ Example:
>>> input = torch.rand(2, 3, 4, 5)
- >>> yuv = kornia.color.RgbToYuv()
- >>> output = yuv(input) # 2x3x4x5
- Reference::
- [1] https://es.wikipedia.org/wiki/YUV#RGB_a_Y'UV
+ >>> output = rgb_to_yuv(input) # 2x3x4x5
"""
+ if not isinstance(image, torch.Tensor):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
- def __init__(self) -> None:
- super(RgbToYuv, 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( # type: ignore
- self, input: torch.Tensor) -> torch.Tensor:
- return rgb_to_yuv(input)
+ r, g, b = torch.chunk(image, chunks=3, dim=-3)
+
+ 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)
+
+ return out
-def rgb_to_yuv(input: torch.Tensor) -> torch.Tensor:
- r"""Convert an RGB image to YUV
+def yuv_to_rgb(image: torch.Tensor) -> torch.Tensor:
+ r"""Convert an YUV image to RGB.
+
The image data is assumed to be in the range of (0, 1).
Args:
- input (torch.Tensor): RGB Image to be converted to YUV.
+ image (torch.Tensor): YUV Image to be converted to RGB with shape :math:`(*, 3, H, W)`.
+
Returns:
- torch.Tensor: YUV version of the image.
- See :class:`~kornia.color.RgbToYuv` for details."""
- if not torch.is_tensor(input):
- raise TypeError("Input type is not a torch.Tensor. Got {type(input)}")
+ torch.Tensor: RGB version of the image with shape :math:`(*, 3, H, W)`.
- if not(len(input.shape) == 3 or len(input.shape) == 4):
- raise ValueError(f"Input size must have a shape of (*, 3, H, W) or (3, H, W). Got {input.shape}")
+ Example:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> output = yuv_to_rgb(input) # 2x3x4x5
+ """
+ if not isinstance(image, torch.Tensor):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
- if input.shape[-3] != 3:
- raise ValueError(f"Expected input to have 3 channels, got {input.shape[-3]}")
+ 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))
- r, g, b = torch.chunk(input, chunks=3, dim=-3)
- 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
- yuv_img: torch.Tensor = torch.cat((y, u, v), -3)
- return yuv_img
+ y, u, v = torch.chunk(image, chunks=3, dim=-3)
+ 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)
+
+ return out
+
+
+class RgbToYuv(nn.Module):
+ r"""Convert an image from RGB to YUV.
-class YuvToRgb(nn.Module):
- r"""Convert image from YUV to RGB
The image data is assumed to be in the range of (0, 1).
- args:
- image (torch.Tensor): YUV image to be converted to RGB.
- returns:
- torch.tensor: RGB version of the image.
- shape:
+ Returns:
+ torch.Tensor: YUV version of the image.
+
+ Shape:
- image: :math:`(*, 3, H, W)`
- output: :math:`(*, 3, H, W)`
- Examples::
+
+ Examples:
>>> input = torch.rand(2, 3, 4, 5)
- >>> rgb = kornia.color.YuvToRgb()
- >>> output = rgb(input) # 2x3x4x5
+ >>> yuv = RgbToYuv()
+ >>> output = yuv(input) # 2x3x4x5
+
+ Reference::
+ [1] https://es.wikipedia.org/wiki/YUV#RGB_a_Y'UV
"""
def __init__(self) -> None:
- super(YuvToRgb, self).__init__()
+ super(RgbToYuv, self).__init__()
+
+ def forward(self, input: torch.Tensor) -> torch.Tensor:
+ return rgb_to_yuv(input)
- def forward( # type: ignore
- self, input: torch.Tensor) -> torch.Tensor:
- return yuv_to_rgb(input)
+class YuvToRgb(nn.Module):
+ r"""Convert an image from YUV to RGB.
-def yuv_to_rgb(input: torch.Tensor) -> torch.Tensor:
- r"""Convert an YUV image to RGB
The image data is assumed to be in the range of (0, 1).
- Args:
- input (torch.Tensor): YUV Image to be converted to RGB.
Returns:
torch.Tensor: RGB version of the image.
- See :class:`~kornia.color.YuvToRgb` for details."""
- if not torch.is_tensor(input):
- raise TypeError("Input type is not a torch.Tensor. Got {type(input)}")
- if not(len(input.shape) == 3 or len(input.shape) == 4):
- raise ValueError(f"Input size must have a shape of (*, 3, H, W) or (3, H, W). Got {input.shape}")
+ Shape:
+ - image: :math:`(*, 3, H, W)`
+ - output: :math:`(*, 3, H, W)`
+
+ Examples:
+ >>> input = torch.rand(2, 3, 4, 5)
+ >>> rgb = YuvToRgb()
+ >>> output = rgb(input) # 2x3x4x5
+ """
- if input.shape[-3] != 3:
- raise ValueError(f"Expected input to have 3 channels, got {input.shape[-3]}")
+ def __init__(self) -> None:
+ super(YuvToRgb, self).__init__()
- y, u, v = torch.chunk(input, chunks=3, dim=-3)
- 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
- rgb_img: torch.Tensor = torch.cat((r, g, b), -3)
- return rgb_img
+ def forward(self, input: torch.Tensor) -> torch.Tensor:
+ return yuv_to_rgb(input)
diff --git a/kornia/jit/__init__.py b/kornia/jit/__init__.py
index 863d7b7ff..29b29fd0a 100644
--- a/kornia/jit/__init__.py
+++ b/kornia/jit/__init__.py
@@ -4,6 +4,7 @@ import kornia as K
# expose functions to torch.jit
# TODO: find an automatic way to do this
rgb_to_grayscale = torch.jit.script(K.color.rgb_to_grayscale)
+bgr_to_grayscale = torch.jit.script(K.color.bgr_to_grayscale)
spatial_soft_argmax2d = torch.jit.script(K.geometry.spatial_soft_argmax2d)
spatial_softmax2d = torch.jit.script(K.geometry.dsnt.spatial_softmax2d)
diff --git a/kornia/testing/__init__.py b/kornia/testing/__init__.py
index 36464160a..5e457bb7a 100644
--- a/kornia/testing/__init__.py
+++ b/kornia/testing/__init__.py
@@ -85,7 +85,7 @@ class BaseTester(ABC):
raise NotImplementedError("Implement a stupid routine.")
@abstractmethod
- def test_batch(self):
+ def test_cardinality(self):
raise NotImplementedError("Implement a stupid routine.")
@abstractmethod
@@ -95,3 +95,7 @@ class BaseTester(ABC):
@abstractmethod
def test_gradcheck(self):
raise NotImplementedError("Implement a stupid routine.")
+
+ @abstractmethod
+ def test_module(self):
+ raise NotImplementedError("Implement a stupid routine.")
diff --git a/pytest.ini b/pytest.ini
new file mode 100644
index 000000000..57ad24500
--- /dev/null
+++ b/pytest.ini
@@ -0,0 +1,5 @@
+[pytest]
+markers =
+ jit: mark a test as torchscript test
+ grad: mark a test as gradcheck test
+ nn: mark a test as module test
diff --git a/test/color/test_gray.py b/test/color/test_gray.py
index 463b0a3f7..109c26b4b 100644
--- a/test/color/test_gray.py
+++ b/test/color/test_gray.py
@@ -1,148 +1,168 @@
import pytest
import kornia
-import kornia.testing as utils # test utils
+from kornia.testing import BaseTester # test utils
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-class TestRgbToGrayscale:
- def test_rgb_to_grayscale(self, device, dtype):
- channels, height, width = 3, 4, 5
- img = torch.ones(channels, height, width).to(device, dtype)
- assert kornia.rgb_to_grayscale(img).shape == (1, height, width)
+class TestRgbToGrayscale(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.rgb_to_grayscale(img), torch.Tensor)
- def test_rgb_to_grayscale_batch(self, device, dtype):
- batch_size, channels, height, width = 2, 3, 4, 5
- img = torch.ones(batch_size, channels, height, width).to(device, dtype)
- assert kornia.rgb_to_grayscale(img).shape == \
- (batch_size, 1, height, width)
+ @pytest.mark.parametrize(
+ "batch_size, height, width", [(1, 3, 4), (2, 2, 4), (3, 4, 1)])
+ def test_cardinality(self, device, dtype, batch_size, height, width):
+ img = torch.ones(batch_size, 3, height, width, device=device, dtype=dtype)
+ assert kornia.rgb_to_grayscale(img).shape == (batch_size, 1, height, width)
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.rgb_to_grayscale([0.])
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_grayscale(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_grayscale(img)
def test_opencv(self, device, dtype):
- data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
- [0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],
- [0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],
- [0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],
- [0.2898048, 0.1282895, 0.7621747, 0.5657831, 0.9918593]],
-
- [[0.5414237, 0.9962701, 0.8947155, 0.5900949, 0.9483274],
- [0.0468036, 0.3933847, 0.8046577, 0.3640994, 0.0632100],
- [0.6171775, 0.8624780, 0.4126036, 0.7600935, 0.7279997],
- [0.4237089, 0.5365476, 0.5591233, 0.1523191, 0.1382165],
- [0.8932794, 0.8517839, 0.7152701, 0.8983801, 0.5905426]],
-
- [[0.2869580, 0.4700376, 0.2743714, 0.8135023, 0.2229074],
- [0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],
- [0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],
- [0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],
- [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])
- data = data.to(device, dtype)
+ data = torch.tensor([
+ [[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
+ [0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],
+ [0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],
+ [0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],
+ [0.2898048, 0.1282895, 0.7621747, 0.5657831, 0.9918593]],
+
+ [[0.5414237, 0.9962701, 0.8947155, 0.5900949, 0.9483274],
+ [0.0468036, 0.3933847, 0.8046577, 0.3640994, 0.0632100],
+ [0.6171775, 0.8624780, 0.4126036, 0.7600935, 0.7279997],
+ [0.4237089, 0.5365476, 0.5591233, 0.1523191, 0.1382165],
+ [0.8932794, 0.8517839, 0.7152701, 0.8983801, 0.5905426]],
+
+ [[0.2869580, 0.4700376, 0.2743714, 0.8135023, 0.2229074],
+ [0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],
+ [0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],
+ [0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],
+ [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]
+ ], device=device, dtype=dtype)
# Output data generated with OpenCV 4.1.1: cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
- expected = torch.tensor([[0.4684734, 0.8954562, 0.6064363, 0.5236061, 0.6106016],
- [0.1709944, 0.5133104, 0.7915002, 0.5745703, 0.1680204],
- [0.5279005, 0.6092287, 0.3034387, 0.5333768, 0.6064113],
- [0.3503858, 0.5720159, 0.7052018, 0.4558409, 0.3261529],
- [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805]])
- expected = expected.to(device, dtype)
+ expected = torch.tensor([
+ [0.4684734, 0.8954562, 0.6064363, 0.5236061, 0.6106016],
+ [0.1709944, 0.5133104, 0.7915002, 0.5745703, 0.1680204],
+ [0.5279005, 0.6092287, 0.3034387, 0.5333768, 0.6064113],
+ [0.3503858, 0.5720159, 0.7052018, 0.4558409, 0.3261529],
+ [0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805]
+ ], device=device, dtype=dtype)
img_gray = kornia.rgb_to_grayscale(data)
assert_allclose(img_gray, expected)
+ @pytest.mark.grad
def test_gradcheck(self, device, dtype):
- batch_size, channels, height, width = 2, 3, 4, 5
- img = torch.ones(batch_size, channels, height, width).to(device, dtype)
- img = utils.tensor_to_gradcheck_var(img) # to var
+ 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.rgb_to_grayscale, (img,), raise_exception=True)
+ @pytest.mark.jit
def test_jit(self, device, dtype):
- if 'cuda' in str(device):
- pytest.skip('skip this test for a while')
- batch_size, channels, height, width = 2, 3, 64, 64
- img = torch.ones(batch_size, channels, height, width).to(device, dtype)
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.ones(B, C, H, W, device=device, dtype=dtype)
op = kornia.rgb_to_grayscale
op_jit = kornia.jit.rgb_to_grayscale
assert_allclose(op(img), op_jit(img))
- def test_jit_trace(self, device, dtype):
- if 'cuda' in str(device):
- pytest.skip('skip this test for a while')
- batch_size, channels, height, width = 2, 3, 64, 64
- img = torch.ones(batch_size, channels, height, width).to(device, dtype)
- gray = kornia.color.RgbToGrayscale()
- gray_traced = torch.jit.trace(kornia.color.RgbToGrayscale(), img)
- assert_allclose(gray(img), gray_traced(img))
-
-
-class TestBgrToGrayscale:
- def test_bgr_to_grayscale(self, device, dtype):
- channels, height, width = 3, 4, 5
- img = torch.ones(channels, height, width).to(device, dtype)
- assert kornia.bgr_to_grayscale(img).shape == (1, height, width)
-
- def test_bgr_to_grayscale_batch(self, device, dtype):
- batch_size, channels, height, width = 2, 3, 4, 5
- img = torch.ones(batch_size, channels, height, width).to(device, dtype)
- assert kornia.bgr_to_grayscale(img).shape == \
- (batch_size, 1, height, width)
-
- # Output data generated with OpenCV 4.1.1: cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
- def test_opencv(self, device, dtype):
- data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
- [0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],
- [0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],
- [0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],
- [0.2898048, 0.1282895, 0.7621747, 0.5657831, 0.9918593]],
-
- [[0.5414237, 0.9962701, 0.8947155, 0.5900949, 0.9483274],
- [0.0468036, 0.3933847, 0.8046577, 0.3640994, 0.0632100],
- [0.6171775, 0.8624780, 0.4126036, 0.7600935, 0.7279997],
- [0.4237089, 0.5365476, 0.5591233, 0.1523191, 0.1382165],
- [0.8932794, 0.8517839, 0.7152701, 0.8983801, 0.5905426]],
-
- [[0.2869580, 0.4700376, 0.2743714, 0.8135023, 0.2229074],
- [0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],
- [0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],
- [0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],
- [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])
- data = data.to(device, dtype)
-
- expected = torch.tensor([[0.4485849, 0.8233618, 0.6262833, 0.6218331, 0.6341921],
- [0.3200093, 0.4340172, 0.7107211, 0.5454938, 0.2801398],
- [0.6149265, 0.7018101, 0.3503231, 0.4891168, 0.5292346],
- [0.5096100, 0.4336508, 0.6704276, 0.4525143, 0.2134447],
- [0.7878902, 0.6494595, 0.5211386, 0.6623823, 0.6660464]])
- expected = expected.to(device, dtype)
+ @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)
+ gray_ops = kornia.color.RgbToGrayscale().to(device, dtype)
+ gray_fcn = kornia.color.rgb_to_grayscale
+ assert_allclose(gray_ops(img), gray_fcn(img))
- img_gray = kornia.bgr_to_grayscale(data)
- assert_allclose(img_gray, expected)
- def test_gradcheck(self, device, dtype):
- batch_size, channels, height, width = 2, 3, 4, 5
- img = torch.ones(batch_size, channels, height, width).to(device, dtype)
- img = utils.tensor_to_gradcheck_var(img) # to var
- assert gradcheck(kornia.bgr_to_grayscale, (img,), raise_exception=True)
+class TestBgrToGrayscale(BaseTester):
+ def test_smoke(self, device, dtype):
+ C, H, W = 3, 4, 5
+ img = torch.rand(C, H, W, device=device, dtype=dtype)
+ assert kornia.bgr_to_grayscale(img) is not None
- def test_module(self, device, dtype):
- data = torch.tensor([[[[100., 73.],
- [200., 22.]],
+ @pytest.mark.parametrize(
+ "batch_size, height, width", [(1, 3, 4), (2, 2, 4), (3, 4, 1)])
+ def test_cardinality(self, device, dtype, batch_size, height, width):
+ img = torch.ones(batch_size, 3, height, width, device=device, dtype=dtype)
+ assert kornia.bgr_to_grayscale(img).shape == (batch_size, 1, height, width)
- [[50., 10.],
- [148, 14, ]],
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.bgr_to_grayscale([0.])
- [[225., 255.],
- [48., 8.]]]])
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.bgr_to_grayscale(img)
- data = data.to(device, dtype)
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.bgr_to_grayscale(img)
- assert_allclose(kornia.bgr_to_grayscale(data / 255), kornia.color.BgrToGrayscale()(data / 255))
+ def test_opencv(self, device, dtype):
+ data = torch.tensor([
+ [[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
+ [0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],
+ [0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],
+ [0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],
+ [0.2898048, 0.1282895, 0.7621747, 0.5657831, 0.9918593]],
+
+ [[0.5414237, 0.9962701, 0.8947155, 0.5900949, 0.9483274],
+ [0.0468036, 0.3933847, 0.8046577, 0.3640994, 0.0632100],
+ [0.6171775, 0.8624780, 0.4126036, 0.7600935, 0.7279997],
+ [0.4237089, 0.5365476, 0.5591233, 0.1523191, 0.1382165],
+ [0.8932794, 0.8517839, 0.7152701, 0.8983801, 0.5905426]],
+
+ [[0.2869580, 0.4700376, 0.2743714, 0.8135023, 0.2229074],
+ [0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],
+ [0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],
+ [0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],
+ [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]
+ ], device=device, dtype=dtype)
+
+ # Output data generated with OpenCV 4.1.1: cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
+ expected = torch.tensor([
+ [0.4485849, 0.8233618, 0.6262833, 0.6218331, 0.6341921],
+ [0.3200093, 0.4340172, 0.7107211, 0.5454938, 0.2801398],
+ [0.6149265, 0.7018101, 0.3503231, 0.4891168, 0.5292346],
+ [0.5096100, 0.4336508, 0.6704276, 0.4525143, 0.2134447],
+ [0.7878902, 0.6494595, 0.5211386, 0.6623823, 0.6660464],
+ ], device=device, dtype=dtype)
- @pytest.mark.skip(reason="turn off all jit for a while")
+ img_gray = kornia.bgr_to_grayscale(data)
+ assert_allclose(img_gray, 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.bgr_to_grayscale, (img,), raise_exception=True)
+
+ @pytest.mark.jit
def test_jit(self, device, dtype):
- batch_size, channels, height, width = 2, 3, 64, 64
- img = torch.ones(batch_size, channels, height, width).to(device, dtype)
- gray = kornia.color.BgrToGrayscale()
- gray_traced = torch.jit.trace(kornia.color.BgrToGrayscale(), img)
- assert_allclose(gray(img), gray_traced(img))
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.ones(B, C, H, W, device=device, dtype=dtype)
+ op = kornia.rgb_to_grayscale
+ op_jit = kornia.jit.bgr_to_grayscale
+ 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)
+ gray_ops = kornia.color.BgrToGrayscale().to(device, dtype)
+ gray_fcn = kornia.color.bgr_to_grayscale
+ assert_allclose(gray_ops(img), gray_fcn(img))
diff --git a/test/color/test_hls.py b/test/color/test_hls.py
index 3f6da2617..da0da49f9 100644
--- a/test/color/test_hls.py
+++ b/test/color/test_hls.py
@@ -1,303 +1,204 @@
import pytest
+import math
import kornia
-import kornia.testing as utils # test utils
-from kornia.constants import pi
-
-import math
+from kornia.testing import BaseTester
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-class TestRgbToHls:
-
- def test_rgb_to_hls(self, device):
-
- data = torch.tensor([[[0.4237059, 0.1935902, 0.8585021, 0.3790484, 0.1389151],
- [0.5933651, 0.0474544, 0.2801555, 0.1691061, 0.9221829],
- [0.2351739, 0.5852075, 0.5789326, 0.8411915, 0.5960411],
- [0.0290176, 0.6459382, 0.8581501, 0.4755400, 0.7735767],
- [0.9497226, 0.0919441, 0.5462211, 0.7836787, 0.6403612]],
-
- [[0.2280025, 0.1352853, 0.7999730, 0.6658246, 0.4910861],
- [0.3499791, 0.1250734, 0.6315800, 0.4785843, 0.8477826],
- [0.3646359, 0.2415122, 0.5301932, 0.0782518, 0.8710389],
- [0.6957581, 0.6162295, 0.6259052, 0.1753750, 0.6737530],
- [0.7678874, 0.9825978, 0.0234877, 0.2485284, 0.8159551]],
-
- [[0.7330830, 0.9015747, 0.0229067, 0.4280063, 0.5400181],
- [0.0037299, 0.3259412, 0.3467951, 0.9575506, 0.1525899],
- [0.9660432, 0.5287710, 0.6654660, 0.3797526, 0.4981400],
- [0.7422802, 0.9926301, 0.5334370, 0.7852844, 0.4397180],
- [0.2281681, 0.2560037, 0.5134379, 0.5800887, 0.8685090]]])
- data = data.to(device)
-
- # OpenCV
- h_expected = torch.tensor([[4.5945477, 4.268469, 0.9738468, 2.2731707, 3.269344],
- [0.6149417, 3.8969188, 2.292972, 3.7777472, 0.9459598],
- [4.003296, 5.4079432, 4.566101, 5.869351, 1.8194631],
- [3.2098956, 4.271444, 0.29820946, 4.7041655, 0.7340856],
- [0.78329855, 2.2872903, 5.3016634, 5.634379, 3.382815]])
- h_expected = h_expected.to(device)
-
- l_expected = torch.tensor([[0.48054275, 0.51843, 0.44070444, 0.5224365, 0.33946657],
- [0.2985475, 0.18669781, 0.45586777, 0.5633283, 0.5373864],
- [0.6006086, 0.41335985, 0.5978296, 0.45972168, 0.6845894],
- [0.38564888, 0.80442977, 0.6957935, 0.48032972, 0.6066474],
- [0.5889454, 0.53727096, 0.2848544, 0.5161035, 0.7544351]])
- l_expected = l_expected.to(device)
- s_expected = torch.tensor([[0.5255313, 0.79561585, 0.9480225, 0.30024928, 0.59078425],
- [0.98750657, 0.7458223, 0.3854456, 0.90278864, 0.8317882],
- [0.9149786, 0.4157338, 0.16817844, 0.82978433, 0.5911325],
- [0.9247565, 0.9623155, 0.53370523, 0.63488615, 0.4243758],
- [0.8776869, 0.96239233, 0.91754496, 0.55295944, 0.46453667]])
- s_expected = s_expected.to(device)
-
- f = kornia.color.RgbToHls()
- result = f(data)
-
- h = result[0, :, :]
- l = result[1, :, :]
- s = result[2, :, :]
-
- assert_allclose(h, h_expected)
- assert_allclose(l, l_expected)
- assert_allclose(s, s_expected)
-
- def test_batch_rgb_to_hls(self, device):
-
- data = torch.tensor([[[0.4237059, 0.1935902, 0.8585021, 0.3790484, 0.1389151],
- [0.5933651, 0.0474544, 0.2801555, 0.1691061, 0.9221829],
- [0.2351739, 0.5852075, 0.5789326, 0.8411915, 0.5960411],
- [0.0290176, 0.6459382, 0.8581501, 0.4755400, 0.7735767],
- [0.9497226, 0.0919441, 0.5462211, 0.7836787, 0.6403612]],
-
- [[0.2280025, 0.1352853, 0.7999730, 0.6658246, 0.4910861],
- [0.3499791, 0.1250734, 0.6315800, 0.4785843, 0.8477826],
- [0.3646359, 0.2415122, 0.5301932, 0.0782518, 0.8710389],
- [0.6957581, 0.6162295, 0.6259052, 0.1753750, 0.6737530],
- [0.7678874, 0.9825978, 0.0234877, 0.2485284, 0.8159551]],
-
- [[0.7330830, 0.9015747, 0.0229067, 0.4280063, 0.5400181],
- [0.0037299, 0.3259412, 0.3467951, 0.9575506, 0.1525899],
- [0.9660432, 0.5287710, 0.6654660, 0.3797526, 0.4981400],
- [0.7422802, 0.9926301, 0.5334370, 0.7852844, 0.4397180],
- [0.2281681, 0.2560037, 0.5134379, 0.5800887, 0.8685090]]])
- data = data.to(device)
+class TestRgbToHls(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_hls(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_hls(img).shape == shape
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.rgb_to_hls([0.])
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_hls(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_hls(img)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([
+ [[0.4237059, 0.1935902, 0.8585021, 0.3790484, 0.1389151],
+ [0.5933651, 0.0474544, 0.2801555, 0.1691061, 0.9221829],
+ [0.2351739, 0.5852075, 0.5789326, 0.8411915, 0.5960411],
+ [0.0290176, 0.6459382, 0.8581501, 0.4755400, 0.7735767],
+ [0.9497226, 0.0919441, 0.5462211, 0.7836787, 0.6403612]],
+ [[0.2280025, 0.1352853, 0.7999730, 0.6658246, 0.4910861],
+ [0.3499791, 0.1250734, 0.6315800, 0.4785843, 0.8477826],
+ [0.3646359, 0.2415122, 0.5301932, 0.0782518, 0.8710389],
+ [0.6957581, 0.6162295, 0.6259052, 0.1753750, 0.6737530],
+ [0.7678874, 0.9825978, 0.0234877, 0.2485284, 0.8159551]],
+ [[0.7330830, 0.9015747, 0.0229067, 0.4280063, 0.5400181],
+ [0.0037299, 0.3259412, 0.3467951, 0.9575506, 0.1525899],
+ [0.9660432, 0.5287710, 0.6654660, 0.3797526, 0.4981400],
+ [0.7422802, 0.9926301, 0.5334370, 0.7852844, 0.4397180],
+ [0.2281681, 0.2560037, 0.5134379, 0.5800887, 0.8685090]]
+ ], device=device, dtype=dtype)
# OpenCV
- expected = torch.tensor([[[4.5945477, 4.268469, 0.9738468, 2.2731707, 3.269344],
- [0.6149417, 3.8969188, 2.292972, 3.7777472, 0.9459598],
- [4.003296, 5.4079432, 4.566101, 5.869351, 1.8194631],
- [3.2098956, 4.271444, 0.29820946, 4.7041655, 0.7340856],
- [0.78329855, 2.2872903, 5.3016634, 5.634379, 3.382815]],
-
- [[0.48054275, 0.51843, 0.44070444, 0.5224365, 0.33946657],
- [0.2985475, 0.18669781, 0.45586777, 0.5633283, 0.5373864],
- [0.6006086, 0.41335985, 0.5978296, 0.45972168, 0.6845894],
- [0.38564888, 0.80442977, 0.6957935, 0.48032972, 0.6066474],
- [0.5889454, 0.53727096, 0.2848544, 0.5161035, 0.7544351]],
-
- [[0.5255313, 0.79561585, 0.9480225, 0.30024928, 0.59078425],
- [0.98750657, 0.7458223, 0.3854456, 0.90278864, 0.8317882],
- [0.9149786, 0.4157338, 0.16817844, 0.82978433, 0.5911325],
- [0.9247565, 0.9623155, 0.53370523, 0.63488615, 0.4243758],
- [0.8776869, 0.96239233, 0.91754496, 0.55295944, 0.46453667]]])
- expected = expected.to(device)
-
- # Kornia
- f = kornia.color.RgbToHls()
-
- data = data.repeat(2, 1, 1, 1) # 2x3x5x5
- expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5
-
- assert_allclose(f(data), expected)
-
- def test_nan_rgb_to_hls(self, device):
-
- data = torch.ones(1, 5, 5).to(device) # 3x5x5
- data = data.repeat(2, 3, 1, 1) # 2x3x5x5
-
- # OpenCV
- expected = torch.cat([torch.zeros(1, 5, 5), torch.ones(1, 5, 5), torch.zeros(1, 5, 5)], dim=0).to(device)
- expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5
-
- # Kornia
- f = kornia.color.RgbToHls()
-
- assert_allclose(f(data), expected)
-
- def test_gradcheck(self, device):
-
- data = torch.rand(3, 5, 5).to(device) # 3x5x5
- data = utils.tensor_to_gradcheck_var(data) # to var
-
- assert gradcheck(kornia.color.RgbToHls(), (data,),
- raise_exception=True)
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- @torch.jit.script
- def op_script(data: torch.Tensor) -> torch.Tensor:
-
- return kornia.rgb_to_hls(data)
-
- data = torch.tensor([[[[21., 22.],
- [22., 22.]],
-
- [[13., 14.],
- [14., 14.]],
-
- [[8., 8.],
- [8., 8.]]]]) # 3x2x2
-
- data = data.to(device)
- actual = op_script(data)
- expected = kornia.rgb_to_hls(data)
- assert_allclose(actual, expected)
-
-
-class TestHlsToRgb:
-
- def test_hls_to_rgb(self, device):
-
- data = torch.tensor([[[0.5513626, 0.8487718, 0.1822479, 0.2851745, 0.2669488],
- [0.7596772, 0.4565057, 0.6181599, 0.3852497, 0.7746902],
- [0.5742747, 0.1957062, 0.7530835, 0.2104362, 0.9449323],
- [0.9918052, 0.2437515, 0.4718738, 0.8502576, 0.1675640],
- [0.9210159, 0.0538564, 0.5801026, 0.6110542, 0.3768399]],
-
- [[0.4111853, 0.0183454, 0.7832276, 0.2975794, 0.1139528],
- [0.6207729, 0.1073406, 0.8335325, 0.5700451, 0.2594557],
- [0.7520493, 0.5097187, 0.4719872, 0.9477938, 0.1640292],
- [0.8973427, 0.6455371, 0.7567374, 0.3159562, 0.8135307],
- [0.0855004, 0.6645504, 0.9923756, 0.6209313, 0.2356791]],
-
- [[0.4734681, 0.0422099, 0.7405791, 0.9671807, 0.1793800],
- [0.8221875, 0.7219887, 0.3627397, 0.4403201, 0.0024084],
- [0.0803350, 0.9432759, 0.0241543, 0.8292291, 0.7745832],
- [0.3707901, 0.0851424, 0.5805428, 0.1098685, 0.4238486],
- [0.1058410, 0.0816052, 0.5792874, 0.9578886, 0.6281684]]]) # 3x5x5
- data = data.to(device)
+ expected = torch.tensor([
+ [[4.59454770, 4.26846900, 0.97384680, 2.27317070, 3.26934400],
+ [0.61494170, 3.89691880, 2.29297200, 3.77774720, 0.94595980],
+ [4.00329600, 5.40794320, 4.56610100, 5.86935100, 1.81946310],
+ [3.20989560, 4.27144400, 0.29820946, 4.70416550, 0.73408560],
+ [0.78329855, 2.28729030, 5.30166340, 5.63437900, 3.38281500]],
+ [[0.48054275, 0.51843000, 0.44070444, 0.52243650, 0.33946657],
+ [0.29854750, 0.18669781, 0.45586777, 0.56332830, 0.53738640],
+ [0.60060860, 0.41335985, 0.59782960, 0.45972168, 0.68458940],
+ [0.38564888, 0.80442977, 0.69579350, 0.48032972, 0.60664740],
+ [0.58894540, 0.53727096, 0.28485440, 0.51610350, 0.75443510]],
+ [[0.52553130, 0.79561585, 0.94802250, 0.30024928, 0.59078425],
+ [0.98750657, 0.74582230, 0.38544560, 0.90278864, 0.83178820],
+ [0.91497860, 0.41573380, 0.16817844, 0.82978433, 0.59113250],
+ [0.92475650, 0.96231550, 0.53370523, 0.63488615, 0.42437580],
+ [0.87768690, 0.96239233, 0.91754496, 0.55295944, 0.46453667]]
+ ], device=device, dtype=dtype)
+
+ assert_allclose(kornia.color.rgb_to_hls(data), expected)
+
+ def test_nan_rgb_to_hls(self, device, dtype):
+ data = torch.ones(2, 3, 5, 5, device=device, dtype=dtype)
# OpenCV
- r_expected = torch.tensor([[0.21650219, 0.01911971, 0.91374826, 0.1760952, 0.10979544],
- [0.6569808, 0.02984191, 0.77314806, 0.3807273, 0.25964087],
- [0.7321301, 0.8110298, 0.4724091, 0.96834683, 0.2910835],
- [0.935407, 0.6478001, 0.615513, 0.3506698, 0.89171433],
- [0.0945498, 0.6919248, 0.98795897, 0.25782573, 0.08763295]])
- r_expected = r_expected.to(device)
-
- g_expected = torch.tensor([[0.48587522, 0.017571, 0.9437648, 0.5853925, 0.13439366],
- [0.30897713, 0.18483935, 0.8082967, 0.75936294, 0.25883088],
- [0.7542145, 0.97218925, 0.46058673, 0.9910847, 0.03697497],
- [0.8592784, 0.675717, 0.8979618, 0.28124255, 0.8925654],
- [0.07645091, 0.6548674, 0.99254686, 0.500144, 0.38372523]])
- g_expected = g_expected.to(device)
-
- b_expected = torch.tensor([[0.60586834, 0.01897625, 0.62269044, 0.00976634, 0.09351197],
- [0.93256867, 0.14439031, 0.89391685, 0.49867177, 0.2600806],
- [0.7719684, 0.04724807, 0.48338777, 0.904503, 0.12093388],
- [0.8630215, 0.6153573, 0.85029656, 0.34361976, 0.73449594],
- [0.08502806, 0.6371759, 0.99679226, 0.9840369, 0.16492467]])
- b_expected = b_expected.to(device)
-
- # Kornia
- f = kornia.color.HlsToRgb()
- data[0] = 2 * pi * data[0]
- result = f(data)
-
- r = result[0, :, :]
- g = result[1, :, :]
- b = result[2, :, :]
-
- assert_allclose(r, r_expected)
- assert_allclose(g, g_expected)
- assert_allclose(b, b_expected)
-
- def test_batch_hls_to_rgb(self, device):
-
- data = torch.tensor([[[0.5513626, 0.8487718, 0.1822479, 0.2851745, 0.2669488],
- [0.7596772, 0.4565057, 0.6181599, 0.3852497, 0.7746902],
- [0.5742747, 0.1957062, 0.7530835, 0.2104362, 0.9449323],
- [0.9918052, 0.2437515, 0.4718738, 0.8502576, 0.1675640],
- [0.9210159, 0.0538564, 0.5801026, 0.6110542, 0.3768399]],
-
- [[0.4111853, 0.0183454, 0.7832276, 0.2975794, 0.1139528],
- [0.6207729, 0.1073406, 0.8335325, 0.5700451, 0.2594557],
- [0.7520493, 0.5097187, 0.4719872, 0.9477938, 0.1640292],
- [0.8973427, 0.6455371, 0.7567374, 0.3159562, 0.8135307],
- [0.0855004, 0.6645504, 0.9923756, 0.6209313, 0.2356791]],
-
- [[0.4734681, 0.0422099, 0.7405791, 0.9671807, 0.1793800],
- [0.8221875, 0.7219887, 0.3627397, 0.4403201, 0.0024084],
- [0.0803350, 0.9432759, 0.0241543, 0.8292291, 0.7745832],
- [0.3707901, 0.0851424, 0.5805428, 0.1098685, 0.4238486],
- [0.1058410, 0.0816052, 0.5792874, 0.9578886, 0.6281684]]]) # 3x5x5
- data = data.to(device)
- data[0] = 2 * pi * data[0]
- data = data.repeat(2, 1, 1, 1) # 2x3x5x5
+ expected = torch.cat([
+ torch.zeros(2, 1, 5, 5, device=device, dtype=dtype),
+ torch.ones(2, 1, 5, 5, device=device, dtype=dtype),
+ torch.zeros(2, 1, 5, 5, device=device, dtype=dtype)
+ ], dim=1)
+
+ assert_allclose(kornia.color.rgb_to_hls(data), expected)
+
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.rgb_to_hls, (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_hls
+ 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.RgbToHls().to(device, dtype)
+ fcn = kornia.color.rgb_to_hls
+ assert_allclose(ops(img), fcn(img))
+
+
+class TestHlsToRgb(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.hls_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.hls_to_rgb(img).shape == shape
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.hls_to_rgb([0.])
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.hls_to_rgb(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.hls_to_rgb(img)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([[
+ [[0.5513626, 0.8487718, 0.1822479, 0.2851745, 0.2669488],
+ [0.7596772, 0.4565057, 0.6181599, 0.3852497, 0.7746902],
+ [0.5742747, 0.1957062, 0.7530835, 0.2104362, 0.9449323],
+ [0.9918052, 0.2437515, 0.4718738, 0.8502576, 0.1675640],
+ [0.9210159, 0.0538564, 0.5801026, 0.6110542, 0.3768399]],
+ [[0.4111853, 0.0183454, 0.7832276, 0.2975794, 0.1139528],
+ [0.6207729, 0.1073406, 0.8335325, 0.5700451, 0.2594557],
+ [0.7520493, 0.5097187, 0.4719872, 0.9477938, 0.1640292],
+ [0.8973427, 0.6455371, 0.7567374, 0.3159562, 0.8135307],
+ [0.0855004, 0.6645504, 0.9923756, 0.6209313, 0.2356791]],
+ [[0.4734681, 0.0422099, 0.7405791, 0.9671807, 0.1793800],
+ [0.8221875, 0.7219887, 0.3627397, 0.4403201, 0.0024084],
+ [0.0803350, 0.9432759, 0.0241543, 0.8292291, 0.7745832],
+ [0.3707901, 0.0851424, 0.5805428, 0.1098685, 0.4238486],
+ [0.1058410, 0.0816052, 0.5792874, 0.9578886, 0.6281684]]
+ ]], device=device, dtype=dtype)
+
+ data[:, 0] = 2 * math.pi * data[:, 0]
# OpenCV
- expected = torch.tensor([[[0.21650219, 0.01911971, 0.91374826, 0.1760952, 0.10979544],
- [0.6569808, 0.02984191, 0.77314806, 0.3807273, 0.25964087],
- [0.7321301, 0.8110298, 0.4724091, 0.96834683, 0.2910835],
- [0.935407, 0.6478001, 0.615513, 0.3506698, 0.89171433],
- [0.0945498, 0.6919248, 0.98795897, 0.25782573, 0.08763295]],
-
- [[0.48587522, 0.017571, 0.9437648, 0.5853925, 0.13439366],
- [0.30897713, 0.18483935, 0.8082967, 0.75936294, 0.25883088],
- [0.7542145, 0.97218925, 0.46058673, 0.9910847, 0.03697497],
- [0.8592784, 0.675717, 0.8979618, 0.28124255, 0.8925654],
- [0.07645091, 0.6548674, 0.99254686, 0.500144, 0.38372523]],
-
- [[0.60586834, 0.01897625, 0.62269044, 0.00976634, 0.09351197],
- [0.93256867, 0.14439031, 0.89391685, 0.49867177, 0.2600806],
- [0.7719684, 0.04724807, 0.48338777, 0.904503, 0.12093388],
- [0.8630215, 0.6153573, 0.85029656, 0.34361976, 0.73449594],
- [0.08502806, 0.6371759, 0.99679226, 0.9840369, 0.16492467]]])
- expected = expected.to(device)
- expected = expected.repeat(2, 1, 1, 1) # 2x3x2x2
-
- # Kornia
- f = kornia.color.HlsToRgb()
-
+ expected = torch.tensor([[
+ [[0.21650219, 0.01911971, 0.91374826, 0.17609520, 0.10979544],
+ [0.65698080, 0.02984191, 0.77314806, 0.38072730, 0.25964087],
+ [0.73213010, 0.81102980, 0.47240910, 0.96834683, 0.29108350],
+ [0.93540700, 0.64780010, 0.61551300, 0.35066980, 0.89171433],
+ [0.09454980, 0.69192480, 0.98795897, 0.25782573, 0.08763295]],
+ [[0.48587522, 0.01757100, 0.94376480, 0.58539250, 0.13439366],
+ [0.30897713, 0.18483935, 0.80829670, 0.75936294, 0.25883088],
+ [0.75421450, 0.97218925, 0.46058673, 0.99108470, 0.03697497],
+ [0.85927840, 0.67571700, 0.89796180, 0.28124255, 0.89256540],
+ [0.07645091, 0.65486740, 0.99254686, 0.50014400, 0.38372523]],
+ [[0.60586834, 0.01897625, 0.62269044, 0.00976634, 0.09351197],
+ [0.93256867, 0.14439031, 0.89391685, 0.49867177, 0.26008060],
+ [0.77196840, 0.04724807, 0.48338777, 0.90450300, 0.12093388],
+ [0.86302150, 0.61535730, 0.85029656, 0.34361976, 0.73449594],
+ [0.08502806, 0.63717590, 0.99679226, 0.98403690, 0.16492467]]
+ ]], device=device, dtype=dtype)
+
+ f = kornia.color.hls_to_rgb
assert_allclose(f(data), expected)
- data[:, 0] += 2 * pi
+ data[:, 0] += 2 * math.pi
assert_allclose(f(data), expected)
- data[:, 0] -= 4 * pi
+ data[:, 0] -= 4 * math.pi
assert_allclose(f(data), expected)
- def test_gradcheck(self, device):
-
- data = torch.rand(3, 5, 5).to(device) # 3x5x5
- data[0] = 2 * pi * data[0]
-
- data = utils.tensor_to_gradcheck_var(data) # to var
-
- assert gradcheck(kornia.color.HlsToRgb(), (data,),
- raise_exception=True)
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- @torch.jit.script
- def op_script(data: torch.tensor) -> torch.Tensor:
- return kornia.hls_to_rgb(data)
-
- data = torch.tensor([[[[21., 22.],
- [22., 22.]],
-
- [[13., 14.],
- [14., 14.]],
-
- [[8., 8.],
- [8., 8.]]]]) # 3x2x2
-
- data = data.to(device)
- actual = op_script(data)
- expected = kornia.hls_to_rgb(data)
- assert_allclose(actual, expected)
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.hls_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.hls_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.HlsToRgb().to(device, dtype)
+ fcn = kornia.color.hls_to_rgb
+ assert_allclose(ops(img), fcn(img))
diff --git a/test/color/test_hsv.py b/test/color/test_hsv.py
index b74ad44a6..914bc080e 100644
--- a/test/color/test_hsv.py
+++ b/test/color/test_hsv.py
@@ -1,301 +1,197 @@
import pytest
+import math
import kornia
-from kornia.constants import pi
-import kornia.testing as utils # test utils
-
-import math
+from kornia.testing import BaseTester
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-class TestRgbToHsv:
-
- def test_rgb_to_hsv(self, device):
-
- data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
- [0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],
- [0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],
- [0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],
- [0.2898048, 0.1282895, 0.7621747, 0.5657831, 0.9918593]],
-
- [[0.5414237, 0.9962701, 0.8947155, 0.5900949, 0.9483274],
- [0.0468036, 0.3933847, 0.8046577, 0.3640994, 0.0632100],
- [0.6171775, 0.8624780, 0.4126036, 0.7600935, 0.7279997],
- [0.4237089, 0.5365476, 0.5591233, 0.1523191, 0.1382165],
- [0.8932794, 0.8517839, 0.7152701, 0.8983801, 0.5905426]],
-
- [[0.2869580, 0.4700376, 0.2743714, 0.8135023, 0.2229074],
- [0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],
- [0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],
- [0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],
- [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])
- data = data.to(device)
-
- # OpenCV
- h_expected = torch.tensor([[1.6519808, 1.3188975, 2.2487938, 3.582216, 2.250954],
- [4.28164, 0.04868213, 0.83454597, 5.533617, 4.319574],
- [3.4185164, 2.7919037, 2.8883224, 1.7474692, 1.3619272],
- [3.6837196, 0.6378961, 5.7213116, 5.2614374, 6.259687],
- [2.929221, 2.5614352, 0.97840965, 1.5729411, 6.0235224]])
- h_expected = h_expected.to(device)
-
- s_expected = torch.tensor([[0.46999356, 0.52820253, 0.8132473, 0.65267974, 0.899411],
- [0.9497089, 0.534381, 0.48878422, 0.60298723, 0.9163612],
- [0.6343409, 0.87112963, 0.8101612, 0.9500878, 0.8192622],
- [0.99010557, 0.9023306, 0.42042294, 0.8292772, 0.81847864],
- [0.6755719, 0.8493871, 0.93686795, 0.73741645, 0.40461043]])
- s_expected = s_expected.to(device)
-
- v_expected = torch.tensor([[0.5414237, 0.99627006, 0.89471555, 0.81350225, 0.9483274],
- [0.930656, 0.80207086, 0.8933256, 0.9170977, 0.75575125],
- [0.74157417, 0.86247796, 0.41260356, 0.76009345, 0.7279997],
- [0.86927235, 0.8288748, 0.9647092, 0.892202, 0.7614344],
- [0.8932794, 0.8517839, 0.7621747, 0.8983801, 0.99185926]])
- v_expected = v_expected.to(device)
-
- f = kornia.color.RgbToHsv()
- result = f(data)
-
- h = result[0, :, :]
- s = result[1, :, :]
- v = result[2, :, :]
-
- assert_allclose(h, h_expected)
- assert_allclose(s, s_expected)
- assert_allclose(v, v_expected)
-
- def test_batch_rgb_to_hsv(self, device):
-
- data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
- [0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],
- [0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],
- [0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],
- [0.2898048, 0.1282895, 0.7621747, 0.5657831, 0.9918593]],
-
- [[0.5414237, 0.9962701, 0.8947155, 0.5900949, 0.9483274],
- [0.0468036, 0.3933847, 0.8046577, 0.3640994, 0.0632100],
- [0.6171775, 0.8624780, 0.4126036, 0.7600935, 0.7279997],
- [0.4237089, 0.5365476, 0.5591233, 0.1523191, 0.1382165],
- [0.8932794, 0.8517839, 0.7152701, 0.8983801, 0.5905426]],
-
- [[0.2869580, 0.4700376, 0.2743714, 0.8135023, 0.2229074],
- [0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],
- [0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],
- [0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],
- [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])
- data = data.to(device)
+class TestRgbToHsv(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_hsv(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_hsv(img).shape == shape
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.rgb_to_hsv([0.])
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_hsv(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_hsv(img)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([
+ [[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],
+ [0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],
+ [0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],
+ [0.0086008, 0.8288748, 0.9647092, 0.8922020, 0.7614344],
+ [0.2898048, 0.1282895, 0.7621747, 0.5657831, 0.9918593]],
+ [[0.5414237, 0.9962701, 0.8947155, 0.5900949, 0.9483274],
+ [0.0468036, 0.3933847, 0.8046577, 0.3640994, 0.0632100],
+ [0.6171775, 0.8624780, 0.4126036, 0.7600935, 0.7279997],
+ [0.4237089, 0.5365476, 0.5591233, 0.1523191, 0.1382165],
+ [0.8932794, 0.8517839, 0.7152701, 0.8983801, 0.5905426]],
+ [[0.2869580, 0.4700376, 0.2743714, 0.8135023, 0.2229074],
+ [0.9306560, 0.3734594, 0.4566821, 0.7599275, 0.7557513],
+ [0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],
+ [0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],
+ [0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]
+ ], device=device, dtype=dtype)
# OpenCV
- expected = torch.tensor([[[1.6519808, 1.3188975, 2.2487938, 3.582216, 2.250954],
- [4.28164, 0.04868213, 0.83454597, 5.533617, 4.319574],
- [3.4185164, 2.7919037, 2.8883224, 1.7474692, 1.3619272],
- [3.6837196, 0.6378961, 5.7213116, 5.2614374, 6.259687],
- [2.929221, 2.5614352, 0.97840965, 1.5729411, 6.0235224]],
-
- [[0.46999356, 0.52820253, 0.8132473, 0.65267974, 0.899411],
- [0.9497089, 0.534381, 0.48878422, 0.60298723, 0.9163612],
- [0.6343409, 0.87112963, 0.8101612, 0.9500878, 0.8192622],
- [0.99010557, 0.9023306, 0.42042294, 0.8292772, 0.81847864],
- [0.6755719, 0.8493871, 0.93686795, 0.73741645, 0.40461043]],
-
- [[0.5414237, 0.99627006, 0.89471555, 0.81350225, 0.9483274],
- [0.930656, 0.80207086, 0.8933256, 0.9170977, 0.75575125],
- [0.74157417, 0.86247796, 0.41260356, 0.76009345, 0.7279997],
- [0.86927235, 0.8288748, 0.9647092, 0.892202, 0.7614344],
- [0.8932794, 0.8517839, 0.7621747, 0.8983801, 0.99185926]]])
- expected = expected.to(device)
-
- # Kornia
- f = kornia.color.RgbToHsv()
-
- data = data.repeat(2, 1, 1, 1) # 2x3x5x5
- expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5
-
- assert_allclose(f(data), expected)
-
- def test_nan_rgb_to_hsv(self):
-
- data = torch.zeros(1, 5, 5) # 3x5x5
+ expected = torch.tensor([
+ [[1.6519808, 1.31889750, 2.24879380, 3.58221600, 2.25095400],
+ [4.2816400, 0.04868213, 0.83454597, 5.53361700, 4.31957400],
+ [3.4185164, 2.79190370, 2.88832240, 1.74746920, 1.36192720],
+ [3.6837196, 0.63789610, 5.72131160, 5.26143740, 6.25968700],
+ [2.9292210, 2.56143520, 0.97840965, 1.57294110, 6.02352240]],
+ [[0.4699935, 0.52820253, 0.81324730, 0.65267974, 0.89941100],
+ [0.9497089, 0.53438100, 0.48878422, 0.60298723, 0.91636120],
+ [0.6343409, 0.87112963, 0.81016120, 0.95008780, 0.81926220],
+ [0.9901055, 0.90233060, 0.42042294, 0.82927720, 0.81847864],
+ [0.6755719, 0.84938710, 0.93686795, 0.73741645, 0.40461043]],
+ [[0.5414237, 0.99627006, 0.89471555, 0.81350225, 0.94832740],
+ [0.9306560, 0.80207086, 0.89332560, 0.91709770, 0.75575125],
+ [0.7415741, 0.86247796, 0.41260356, 0.76009345, 0.72799970],
+ [0.8692723, 0.82887480, 0.96470920, 0.89220200, 0.76143440],
+ [0.8932794, 0.85178390, 0.76217470, 0.89838010, 0.99185926]]
+ ], device=device, dtype=dtype)
+
+ assert_allclose(kornia.color.rgb_to_hsv(data), expected)
+
+ def test_nan_rgb_to_hsv(self, device, dtype):
+ data = torch.zeros(1, 5, 5, device=device, dtype=dtype) # 3x5x5
data = data.repeat(3, 1, 1) # 2x3x5x5
- # OpenCV
- expected = torch.zeros(1, 5, 5) # 3x5x5
- expected = expected.repeat(3, 1, 1) # 2x3x5x5
-
- # Kornia
- f = kornia.color.RgbToHsv()
-
- assert_allclose(f(data), expected)
-
- def test_gradcheck(self, device):
-
- data = torch.rand(3, 5, 5).to(device) # 3x2x2
- data = utils.tensor_to_gradcheck_var(data) # to var
-
- assert gradcheck(kornia.color.RgbToHsv(), (data,),
- raise_exception=True)
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- @torch.jit.script
- def op_script(data: torch.Tensor) -> torch.Tensor:
-
- return kornia.rgb_to_hsv(data)
- data = torch.tensor([[[[21., 22.],
- [22., 22.]],
-
- [[13., 14.],
- [14., 14.]],
-
- [[8., 8.],
- [8., 8.]]]]) # 3x2x2
-
- data = data.to(device)
- actual = op_script(data)
- expected = kornia.rgb_to_hsv(data)
- assert_allclose(actual, expected)
-
-
-class TestHsvToRgb:
-
- def test_hsv_to_rgb(self, device):
-
- data = torch.tensor([[[3.5433271, 5.6390061, 1.3766849, 2.5384088, 4.6848912],
- [5.7209363, 5.3262630, 6.2059994, 4.1164689, 2.3872600],
- [0.6370091, 3.6186798, 5.9170871, 2.8275447, 5.4289737],
- [0.2751994, 1.6632686, 1.0049511, 0.7046204, 1.3791083],
- [0.7863123, 4.4852505, 4.3064494, 2.5573561, 5.9083076]],
-
- [[0.5026655, 0.9453601, 0.5929778, 0.2632897, 0.4590443],
- [0.6201433, 0.5610679, 0.9653260, 0.0830478, 0.5000827],
- [0.6067343, 0.6422323, 0.6777940, 0.7705711, 0.6050767],
- [0.5495264, 0.5573426, 0.4683768, 0.2268902, 0.2116482],
- [0.6525245, 0.0022379, 0.4909980, 0.1682271, 0.6327152]],
-
- [[0.8471680, 0.9302199, 0.3265766, 0.7944570, 0.7038843],
- [0.4833369, 0.2088473, 0.1169234, 0.4966302, 0.6448684],
- [0.2713015, 0.5893380, 0.6015301, 0.6801558, 0.2322258],
- [0.5704236, 0.6797268, 0.4755683, 0.4811209, 0.5317836],
- [0.3236262, 0.0999796, 0.3614958, 0.5117705, 0.8194097]]]) # 3x5x5
- data = data.to(device)
+ expected = torch.zeros_like(data) # 3x5x5
+ assert_allclose(kornia.color.rgb_to_hsv(data), expected)
+
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.rgb_to_hsv, (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_hsv
+ 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.RgbToHsv().to(device, dtype)
+ fcn = kornia.color.rgb_to_hsv
+ assert_allclose(ops(img), fcn(img))
+
+
+class TestHsvToRgb(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.hsv_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.hsv_to_rgb(img).shape == shape
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.hsv_to_rgb([0.])
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.hsv_to_rgb(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.hsv_to_rgb(img)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([[
+ [[3.5433271, 5.6390061, 1.3766849, 2.5384088, 4.6848912],
+ [5.7209363, 5.3262630, 6.2059994, 4.1164689, 2.3872600],
+ [0.6370091, 3.6186798, 5.9170871, 2.8275447, 5.4289737],
+ [0.2751994, 1.6632686, 1.0049511, 0.7046204, 1.3791083],
+ [0.7863123, 4.4852505, 4.3064494, 2.5573561, 5.9083076]],
+ [[0.5026655, 0.9453601, 0.5929778, 0.2632897, 0.4590443],
+ [0.6201433, 0.5610679, 0.9653260, 0.0830478, 0.5000827],
+ [0.6067343, 0.6422323, 0.6777940, 0.7705711, 0.6050767],
+ [0.5495264, 0.5573426, 0.4683768, 0.2268902, 0.2116482],
+ [0.6525245, 0.0022379, 0.4909980, 0.1682271, 0.6327152]],
+ [[0.8471680, 0.9302199, 0.3265766, 0.7944570, 0.7038843],
+ [0.4833369, 0.2088473, 0.1169234, 0.4966302, 0.6448684],
+ [0.2713015, 0.5893380, 0.6015301, 0.6801558, 0.2322258],
+ [0.5704236, 0.6797268, 0.4755683, 0.4811209, 0.5317836],
+ [0.3236262, 0.0999796, 0.3614958, 0.5117705, 0.8194097]]
+ ]], device=device, dtype=dtype)
# OpenCV
- r_expected = torch.tensor([[0.4213259, 0.93021995, 0.26564622, 0.58528465, 0.5338429],
- [0.48333693, 0.20884734, 0.11692339, 0.45538613, 0.32238087],
- [0.2713015, 0.2108461, 0.60153013, 0.15604737, 0.23222584],
- [0.5704236, 0.4568531, 0.4755683, 0.48112088, 0.49611038],
- [0.32362622, 0.09981924, 0.20394461, 0.42567685, 0.81940967]])
- r_expected = r_expected.to(device)
-
- g_expected = torch.tensor([[0.6838029, 0.0508271, 0.3265766, 0.794457, 0.3807702],
- [0.18359877, 0.0916698, 0.00405421, 0.45823452, 0.6448684],
- [0.20682439, 0.41690278, 0.1938166, 0.68015575, 0.0917114],
- [0.33933756, 0.6797268, 0.4665822, 0.44541004, 0.5317836],
- [0.27101707, 0.09975589, 0.18400209, 0.51177055, 0.30095676]])
- g_expected = g_expected.to(device)
-
- b_expected = torch.tensor([[0.84716797, 0.5917818, 0.13292392, 0.6739741, 0.7038843],
- [0.34453064, 0.19874583, 0.01237347, 0.4966302, 0.41256943],
- [0.10669357, 0.589338, 0.3363524, 0.5229789, 0.20633064],
- [0.25696078, 0.30088606, 0.25282317, 0.37195927, 0.41923255],
- [0.11245217, 0.09997964, 0.3614958, 0.46373847, 0.4865534]])
- b_expected = b_expected.to(device)
-
- # Kornia
- f = kornia.color.HsvToRgb()
- result = f(data)
-
- r = result[0, :, :]
- g = result[1, :, :]
- b = result[2, :, :]
-
- assert_allclose(r, r_expected)
- assert_allclose(g, g_expected)
- assert_allclose(b, b_expected)
-
- def test_batch_hsv_to_rgb(self, device):
-
- data = torch.tensor([[[3.5433271, 5.6390061, 1.3766849, 2.5384088, 4.6848912],
- [5.7209363, 5.3262630, 6.2059994, 4.1164689, 2.3872600],
- [0.6370091, 3.6186798, 5.9170871, 2.8275447, 5.4289737],
- [0.2751994, 1.6632686, 1.0049511, 0.7046204, 1.3791083],
- [0.7863123, 4.4852505, 4.3064494, 2.5573561, 5.9083076]],
-
- [[0.5026655, 0.9453601, 0.5929778, 0.2632897, 0.4590443],
- [0.6201433, 0.5610679, 0.9653260, 0.0830478, 0.5000827],
- [0.6067343, 0.6422323, 0.6777940, 0.7705711, 0.6050767],
- [0.5495264, 0.5573426, 0.4683768, 0.2268902, 0.2116482],
- [0.6525245, 0.0022379, 0.4909980, 0.1682271, 0.6327152]],
-
- [[0.8471680, 0.9302199, 0.3265766, 0.7944570, 0.7038843],
- [0.4833369, 0.2088473, 0.1169234, 0.4966302, 0.6448684],
- [0.2713015, 0.5893380, 0.6015301, 0.6801558, 0.2322258],
- [0.5704236, 0.6797268, 0.4755683, 0.4811209, 0.5317836],
- [0.3236262, 0.0999796, 0.3614958, 0.5117705, 0.8194097]]]) # 3x5x5
- data = data.to(device)
- data = data.repeat(2, 1, 1, 1) # 2x3x5x5
-
- # OpenCV
- expected = torch.tensor([[[0.4213259, 0.93021995, 0.26564622, 0.58528465, 0.5338429],
- [0.48333693, 0.20884734, 0.11692339, 0.45538613, 0.32238087],
- [0.2713015, 0.2108461, 0.60153013, 0.15604737, 0.23222584],
- [0.5704236, 0.4568531, 0.4755683, 0.48112088, 0.49611038],
- [0.32362622, 0.09981924, 0.20394461, 0.42567685, 0.81940967]],
-
- [[0.6838029, 0.0508271, 0.3265766, 0.794457, 0.3807702],
- [0.18359877, 0.0916698, 0.00405421, 0.45823452, 0.6448684],
- [0.20682439, 0.41690278, 0.1938166, 0.68015575, 0.0917114],
- [0.33933756, 0.6797268, 0.4665822, 0.44541004, 0.5317836],
- [0.27101707, 0.09975589, 0.18400209, 0.51177055, 0.30095676]],
-
- [[0.84716797, 0.5917818, 0.13292392, 0.6739741, 0.7038843],
- [0.34453064, 0.19874583, 0.01237347, 0.4966302, 0.41256943],
- [0.10669357, 0.589338, 0.3363524, 0.5229789, 0.20633064],
- [0.25696078, 0.30088606, 0.25282317, 0.37195927, 0.41923255],
- [0.11245217, 0.09997964, 0.3614958, 0.46373847, 0.4865534]]])
- expected = expected.to(device)
- expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5
-
- # Kornia
- f = kornia.color.HsvToRgb()
-
+ expected = torch.tensor([[
+ [[0.42132590, 0.93021995, 0.26564622, 0.58528465, 0.53384290],
+ [0.48333693, 0.20884734, 0.11692339, 0.45538613, 0.32238087],
+ [0.27130150, 0.21084610, 0.60153013, 0.15604737, 0.23222584],
+ [0.57042360, 0.45685310, 0.47556830, 0.48112088, 0.49611038],
+ [0.32362622, 0.09981924, 0.20394461, 0.42567685, 0.81940967]],
+ [[0.68380290, 0.05082710, 0.32657660, 0.79445700, 0.38077020],
+ [0.18359877, 0.09166980, 0.00405421, 0.45823452, 0.64486840],
+ [0.20682439, 0.41690278, 0.19381660, 0.68015575, 0.09171140],
+ [0.33933756, 0.67972680, 0.46658220, 0.44541004, 0.53178360],
+ [0.27101707, 0.09975589, 0.18400209, 0.51177055, 0.30095676]],
+ [[0.84716797, 0.59178180, 0.13292392, 0.67397410, 0.70388430],
+ [0.34453064, 0.19874583, 0.01237347, 0.49663020, 0.41256943],
+ [0.10669357, 0.58933800, 0.33635240, 0.52297890, 0.20633064],
+ [0.25696078, 0.30088606, 0.25282317, 0.37195927, 0.41923255],
+ [0.11245217, 0.09997964, 0.36149580, 0.46373847, 0.48655340]]
+ ]], device=device, dtype=dtype)
+
+ f = kornia.color.hsv_to_rgb
assert_allclose(f(data), expected)
- data[:, 0] += 2 * pi
+ data[:, 0] += 2 * math.pi
assert_allclose(f(data), expected)
- data[:, 0] -= 4 * pi
+ data[:, 0] -= 4 * math.pi
assert_allclose(f(data), expected)
- def test_gradcheck(self, device):
-
- data = torch.rand(3, 5, 5).to(device) # 3x5x5
- data[0] = 2 * pi * data[0]
-
- data = utils.tensor_to_gradcheck_var(data) # to var
-
- assert gradcheck(kornia.color.HsvToRgb(), (data,),
- raise_exception=True)
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- @torch.jit.script
- def op_script(data: torch.Tensor) -> torch.Tensor:
- return kornia.hsv_to_rgb(data)
-
- data = torch.tensor([[[[21., 22.],
- [22., 22.]],
-
- [[13., 14.],
- [14., 14.]],
-
- [[8., 8.],
- [8., 8.]]]]) # 3x2x2
-
- data = data.to(device)
- actual = op_script(data)
- expected = kornia.hsv_to_rgb(data)
- assert_allclose(actual, expected)
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.hsv_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.hsv_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.HsvToRgb().to(device, dtype)
+ fcn = kornia.color.hsv_to_rgb
+ assert_allclose(ops(img), fcn(img))
diff --git a/test/color/test_luv.py b/test/color/test_luv.py
index 90658773e..d8cf8bcc1 100644
--- a/test/color/test_luv.py
+++ b/test/color/test_luv.py
@@ -1,161 +1,188 @@
import pytest
import kornia
-import kornia.testing as utils # test utils
+from kornia.testing import BaseTester
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-class TestRgbToLuv:
+class TestRgbToLuv(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_luv(img), torch.Tensor)
- @pytest.mark.parametrize("batch_size", [None, 1, 2, 5])
- def test_rgb_to_luv(self, batch_size, device):
+ @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_luv(img).shape == shape
- data = torch.tensor([[[[0.13088040, 0.54399723, 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]],
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.rgb_to_luv([0.])
- [[0.60354551, 0.76201361, 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.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]]]])
-
- # Reference output generated using skimage: rgb2luv(data)
-
- luv_ref = torch.tensor([[[[58.02612686, 72.48876190, 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],
- [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],
- [-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]]]])
-
- data.to(device)
- luv_ref.to(device)
-
- if batch_size is not None:
- data = data.repeat(batch_size, 1, 1, 1)
- luv_ref = luv_ref.repeat(batch_size, 1, 1, 1)
-
- luv = kornia.color.RgbToLuv()
- out = luv(data)
- assert_allclose(out, luv_ref)
-
- def test_grad(self, device):
-
- data = torch.rand(2, 3, 4, 5).to(device)
- data = utils.tensor_to_gradcheck_var(data)
- assert gradcheck(kornia.color.RgbToLuv(), (data,), raise_exception=True)
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_luv(img)
- @pytest.mark.parametrize("input_shape", [(2, 2), (3, 3, 5, 3, 3)])
- def test_shape(self, input_shape, device):
with pytest.raises(ValueError):
- luv = kornia.color.RgbToLuv()
- out = luv(torch.ones(*input_shape).to(device))
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_luv(img)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([
+ [[0.13088040, 0.54399723, 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.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.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)
- def test_inverse(self, device):
- data = torch.rand(3, 4, 5).to(device)
- luv = kornia.color.LuvToRgb()
- rgb = kornia.color.RgbToLuv()
+ # Reference output generated using skimage: rgb2luv(data)
+ expected = torch.tensor([
+ [[58.02612686, 72.48876190, 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],
+ [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],
+ [-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]]
+ ], device=device, dtype=dtype)
+
+ assert_allclose(kornia.color.rgb_to_luv(data), expected)
+
+ def test_forth_and_back(self, device, dtype):
+ data = torch.rand(3, 4, 5, device=device, dtype=dtype)
+ luv = kornia.color.rgb_to_luv
+ rgb = kornia.color.luv_to_rgb
data_out = luv(rgb(data))
assert_allclose(data_out, data, rtol=1e-4, atol=1e-4)
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
-
- data = torch.rand((2, 3, 4, 5)).to(device)
- luv = kornia.color.rgb_to_luv
- luv_jit = torch.jit.script(kornia.color.rgb_to_luv)
- assert_allclose(luv_jit(data), luv(data))
-
-
-class TestLuvToRgb:
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.rgb_to_luv, (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_luv
+ 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.RgbToLuv().to(device, dtype)
+ fcn = kornia.color.rgb_to_luv
+ assert_allclose(ops(img), fcn(img))
+
+
+class TestLuvToRgb(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.luv_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.luv_to_rgb(img).shape == shape
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.luv_to_rgb([0.])
- @pytest.mark.parametrize("batch_size", [None, 1, 2, 5])
- def test_rgb_to_luv(self, batch_size, device):
-
- data = torch.tensor([[[[50.21928787, 23.29810143, 14.98279190, 62.50927353, 72.78904724],
- [70.86846924, 68.75330353, 52.81696701, 76.17090607, 88.63134003],
- [46.87160873, 72.38699341, 37.71450806, 82.57386780, 74.79967499],
- [77.33016968, 47.39180374, 61.76217651, 90.83254242, 86.96239471]],
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.luv_to_rgb(img)
- [[65.81327057, -3.69859719, 0.16971001, 14.86583614, -65.54960632],
- [-41.03258133, -19.52661896, 64.16155243, -58.53935242, -71.78411102],
- [112.05227661, -60.13330460, 43.07910538, -51.01456833, -58.25787354],
- [-62.37575531, 50.88882065, -39.27450943, 17.00958824, -24.93779755]],
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.luv_to_rgb(img)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([[
+ [[50.21928787, 23.29810143, 14.98279190, 62.50927353, 72.78904724],
+ [70.86846924, 68.75330353, 52.81696701, 76.17090607, 88.63134003],
+ [46.87160873, 72.38699341, 37.71450806, 82.57386780, 74.79967499],
+ [77.33016968, 47.39180374, 61.76217651, 90.83254242, 86.96239471]],
+ [[65.81327057, -3.69859719, 0.16971001, 14.86583614, -65.54960632],
+ [-41.03258133, -19.52661896, 64.16155243, -58.53935242, -71.78411102],
+ [112.05227661, -60.13330460, 43.07910538, -51.01456833, -58.25787354],
+ [-62.37575531, 50.88882065, -39.27450943, 17.00958824, -24.93779755]],
+ [[-69.53346252, -73.34986877, -11.47461891, 66.73863220, 70.43983459],
+ [51.92737579, 58.77009583, 45.97863388, 24.44452858, 98.81991577],
+ [-7.60597992, 78.97976685, -69.31867218, 67.33953857, 14.28889370],
+ [92.31149292, -85.91405487, -32.83668518, -23.45091820, 69.99038696]]
+ ]], device=device, dtype=dtype)
- [[-69.53346252, -73.34986877, -11.47461891, 66.73863220, 70.43983459],
- [51.92737579, 58.77009583, 45.97863388, 24.44452858, 98.81991577],
- [-7.60597992, 78.97976685, -69.31867218, 67.33953857, 14.28889370],
- [92.31149292, -85.91405487, -32.83668518, -23.45091820, 69.99038696]]]])
# Reference output generated using skimage: luv2rgb(data)
- rgb_ref = torch.tensor([[[[0.78923208, 0.17048222, 0.14947766, 0.65528989, 0.07863078],
- [0.41649094, 0.55222923, 0.72673196, 0.21939684, 0.34298307],
- [0.82763243, 0.24021322, 0.58888060, 0.47255886, 0.16407511],
- [0.30320778, 0.72233224, 0.21593384, 0.98893607, 0.71707106]],
-
- [[0.20532851, 0.13188709, 0.13879408, 0.59964627, 0.80721593],
- [0.75411713, 0.70656943, 0.41770950, 0.82750136, 0.99659365],
- [0.12436169, 0.79804462, 0.10958754, 0.89803618, 0.81000644],
- [0.85726571, 0.17667055, 0.63285238, 0.85567462, 0.91538441]],
-
- [[0.73985511, 0.59308004, 0.21156698, 0.03804367, 0.32732114],
- [0.42489606, 0.33011687, 0.12804756, 0.64905322, 0.25216782],
- [0.41637793, 0.22158240, 0.63437861, 0.46121466, 0.68336427],
- [0.06325728, 0.78878325, 0.74280596, 0.99514300, 0.47176042]]]])
-
- data.to(device)
- rgb_ref.to(device)
-
- if batch_size is not None:
- data = data.repeat(batch_size, 1, 1, 1)
- rgb_ref = rgb_ref.repeat(batch_size, 1, 1, 1)
-
- rgb = kornia.color.LuvToRgb()
- out = rgb(data)
- assert_allclose(out, rgb_ref)
-
- def test_grad(self, device):
-
- data = kornia.rgb_to_luv(torch.rand(2, 3, 4, 5).to(device))
- data = utils.tensor_to_gradcheck_var(data)
- assert gradcheck(kornia.color.LuvToRgb(), (data,), raise_exception=True)
-
- @pytest.mark.parametrize("input_shape", [(2, 2), (3, 3, 5, 3, 3)])
- def test_shape(self, input_shape, device):
- with pytest.raises(ValueError):
- rgb = kornia.color.LuvToRgb()
- out = rgb(torch.ones(*input_shape).to(device))
-
- def test_inverse(self, device):
- data = torch.rand(3, 11, 15).to(device)
- luv = kornia.color.LuvToRgb()
- rgb = kornia.color.RgbToLuv()
+ expected = torch.tensor([[
+ [[0.78923208, 0.17048222, 0.14947766, 0.65528989, 0.07863078],
+ [0.41649094, 0.55222923, 0.72673196, 0.21939684, 0.34298307],
+ [0.82763243, 0.24021322, 0.58888060, 0.47255886, 0.16407511],
+ [0.30320778, 0.72233224, 0.21593384, 0.98893607, 0.71707106]],
+ [[0.20532851, 0.13188709, 0.13879408, 0.59964627, 0.80721593],
+ [0.75411713, 0.70656943, 0.41770950, 0.82750136, 0.99659365],
+ [0.12436169, 0.79804462, 0.10958754, 0.89803618, 0.81000644],
+ [0.85726571, 0.17667055, 0.63285238, 0.85567462, 0.91538441]],
+ [[0.73985511, 0.59308004, 0.21156698, 0.03804367, 0.32732114],
+ [0.42489606, 0.33011687, 0.12804756, 0.64905322, 0.25216782],
+ [0.41637793, 0.22158240, 0.63437861, 0.46121466, 0.68336427],
+ [0.06325728, 0.78878325, 0.74280596, 0.99514300, 0.47176042]]
+ ]], device=device, dtype=dtype)
+
+ assert_allclose(kornia.color.luv_to_rgb(data), expected)
+
+ def test_forth_and_back(self, device, dtype):
+ data = torch.rand(3, 4, 5, device=device, dtype=dtype)
+ luv = kornia.color.rgb_to_luv
+ rgb = kornia.color.luv_to_rgb
data_out = rgb(luv(data))
- assert_allclose(data_out, data)
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
+ assert_allclose(data_out, data, rtol=1e-4, atol=1e-4)
- data = torch.rand((2, 3, 4, 5)).to(device)
- rgb = kornia.color.luv_to_rgb
- rgb_jit = torch.jit.script(kornia.color.luv_to_rgb)
- assert_allclose(rgb_jit(data), rgb(data))
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ img = kornia.rgb_to_luv(img)
+ assert gradcheck(kornia.color.luv_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.luv_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.LuvToRgb().to(device, dtype)
+ fcn = kornia.color.luv_to_rgb
+ assert_allclose(ops(img), fcn(img))
diff --git a/test/color/test_rgb.py b/test/color/test_rgb.py
index 2befb3957..62cb4dc06 100644
--- a/test/color/test_rgb.py
+++ b/test/color/test_rgb.py
@@ -1,306 +1,236 @@
+import pytest
+
import kornia
-import kornia.testing as utils # test utils
+from kornia.testing import BaseTester
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-import pytest
-
-
-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.]],
-
- [[2., 2.],
- [2., 2.]],
-
- [[3., 3.],
- [3., 3.]]]) # 3x2x2
- data = data.to(device)
-
- aval: float = 0.4
- expected = torch.tensor([[[1.0, 1.0],
- [1.0, 1.0]],
-
- [[2.0, 2.0],
- [2.0, 2.0]],
-
- [[3.0, 3.0],
- [3.0, 3.0]],
-
- [[0.4, 0.4],
- [0.4, 0.4]]]) # 4x2x2
- expected = expected.to(device)
-
- assert_allclose(kornia.rgb_to_rgba(data, aval), expected)
- def test_batch(self, device):
- data = torch.tensor([[[[1., 1.],
- [1., 1.]],
+class TestRgbToBgr(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_bgr(img), torch.Tensor)
- [[2., 2.],
- [2., 2.]],
+ @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_bgr(img).shape == shape
- [[3., 3.],
- [3., 3.]]],
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.rgb_to_bgr([0.])
- [[[1., 1.],
- [1., 1.]],
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_bgr(img)
- [[2., 2.],
- [2., 2.]],
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_bgr(img)
- [[3., 3.],
- [3., 3.]]]]) # 2x3x2x2
- data = data.to(device)
-
- aval: float = 45.
-
- expected = torch.tensor([[[[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.]]],
-
- [[[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)
+ 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)
+ data_bgr_new = kornia.color.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):
+ def test_unit(self, device, dtype):
+ data = torch.tensor([
+ [[1., 1.], [1., 1.]],
+ [[2., 2.], [2., 2.]],
+ [[3., 3.], [3., 3.]]
+ ], device=device, dtype=dtype) # 3x2x2
- 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.]]
+ ], device=device, dtype=dtype) # 3x2x2
- 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)
- expected = expected.to(device)
-
- f = kornia.color.BgrToRgb()
- out = f(data)
- assert_allclose(out, expected)
-
- def test_gradcheck(self, device):
-
- 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)
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
- @torch.jit.script
- 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
- 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
- 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.RgbToBgr()
+ f = kornia.color.rgb_to_bgr
assert_allclose(f(data), expected)
- 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 = data.to(device)
- data = utils.tensor_to_gradcheck_var(data) # to var
-
- assert gradcheck(kornia.color.RgbToBgr(), (data,),
- raise_exception=True)
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
- @torch.jit.script
- 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.]]]) # 3x2x
- actual = op_script(data)
- expected = kornia.rgb_to_bgr(data)
- assert_allclose(actual, expected)
-
- def test_batch_rgb_to_bgr(self, device):
-
- # prepare input data
- 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.]]],
+ @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_bgr, (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_bgr
+ 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.RgbToBgr().to(device, dtype)
+ fcn = kornia.color.rgb_to_bgr
+ assert_allclose(ops(img), fcn(img))
+
+ @pytest.mark.nn
+ def test_module_bgr(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.BgrToRgb().to(device, dtype)
+ fcn = kornia.color.bgr_to_rgb
+ assert_allclose(ops(img), fcn(img))
+
+
+class TestRgbToRgba(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_rgba(img, 0.), 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):
+ out_shape = list(shape)
+ out_shape[-3] += 1
+ img = torch.ones(shape, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_rgba(img, 0.).shape == tuple(out_shape)
+
+ def test_exception(self, device, dtype):
+ # rgb to rgba
+ with pytest.raises(TypeError):
+ assert kornia.color.rgb_to_rgba([0.], 0.)
+
+ with pytest.raises(TypeError):
+ img = torch.ones(1, 3, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_rgba(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_rgba(img, 0.)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_rgba(img, 0.)
+
+ with pytest.raises(TypeError):
+ img = torch.ones(3, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_rgba(img, 'alpha_str')
+
+ # rgba to rgb
+ with pytest.raises(TypeError):
+ assert kornia.color.rgba_to_rgb(0.)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 3, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgba_to_rgb(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgba_to_rgb(img)
+
+ def test_back_and_forth_rgb(self, device, dtype):
+ a_val: float = 1.
+ x_rgb = torch.ones(3, 4, 4, device=device, dtype=dtype)
+ x_rgba = kornia.color.rgb_to_rgba(x_rgb, a_val)
+ x_rgb_new = kornia.color.rgba_to_rgb(x_rgba)
+ assert_allclose(x_rgb, x_rgb_new)
- [[[3., 3.],
- [3., 3.]],
+ def test_back_and_forth_bgr(self, device, dtype):
+ a_val: float = 1.
+ x_bgr = torch.ones(3, 4, 4, device=device, dtype=dtype)
+ x_rgba = kornia.color.bgr_to_rgba(x_bgr, a_val)
+ x_bgr_new = kornia.color.rgba_to_bgr(x_rgba)
+ assert_allclose(x_bgr, x_bgr_new)
- [[2., 2.],
- [2., 2.]],
+ @pytest.mark.parametrize("aval", [0.4, 45.])
+ def test_unit(self, device, dtype, aval):
+ data = torch.tensor([[
+ [[1., 1.],
+ [1., 1.]],
+ [[2., 2.],
+ [2., 2.]],
+ [[3., 3.],
+ [3., 3.]]
+ ]], device=device, dtype=dtype) # Bx3x2x2
+
+ expected = torch.tensor([[
+ [[1.0, 1.0],
+ [1.0, 1.0]],
+ [[2.0, 2.0],
+ [2.0, 2.0]],
+ [[3.0, 3.0],
+ [3.0, 3.0]],
+ [[aval, aval],
+ [aval, aval]]
+ ]], device=device, dtype=dtype) # Bx4x2x2
- [[1., 1.],
- [1., 1.]]]]) # 2x3x2x2
+ assert_allclose(kornia.rgb_to_rgba(data, aval), expected)
- # move data to the device
- data = data.to(device)
- expected = expected.to(device)
+ @pytest.mark.parametrize("aval", [0.4, 45.])
+ def test_unit_aval_th(self, device, dtype, aval):
+ data = torch.tensor([[
+ [[1., 1.],
+ [1., 1.]],
+ [[2., 2.],
+ [2., 2.]],
+ [[3., 3.],
+ [3., 3.]]
+ ]], device=device, dtype=dtype) # Bx3x2x2
+
+ expected = torch.tensor([[
+ [[1.0, 1.0],
+ [1.0, 1.0]],
+ [[2.0, 2.0],
+ [2.0, 2.0]],
+ [[3.0, 3.0],
+ [3.0, 3.0]],
+ [[aval, aval],
+ [aval, aval]]
+ ]], device=device, dtype=dtype) # Bx4x2x2
+
+ aval = torch.full_like(data[:, :1], aval) # Bx1xHxW
+ assert_allclose(kornia.rgb_to_rgba(data, aval), expected)
- f = kornia.color.RgbToBgr()
- out = f(data)
- assert_allclose(out, 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_rgba, (img, 1.), raise_exception=True)
+
+ @pytest.mark.grad
+ def test_gradcheck_th(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)
+ aval = torch.ones(B, 1, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.rgb_to_rgba, (img, aval), raise_exception=True)
+
+ @pytest.mark.skip(reason="unsupported Union type")
+ @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_rgba
+ op_jit = torch.jit.script(op)
+ assert_allclose(op(img, 1.), op_jit(img, 1.))
+ aval = torch.ones(B, 1, H, W, device=device, dtype=dtype)
+ assert_allclose(op(img, aval), op_jit(img, aval))
+
+ @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.RgbToRgba(1.).to(device, dtype)
+ fcn = kornia.color.rgb_to_rgba
+ assert_allclose(ops(img), fcn(img, 1.))
+
+ @pytest.mark.nn
+ def test_module_bgr(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.BgrToRgba(1.).to(device, dtype)
+ fcn = kornia.color.bgr_to_rgba
+ assert_allclose(ops(img), fcn(img, 1.))
diff --git a/test/color/test_xyz.py b/test/color/test_xyz.py
index f1d6ff8fb..107874048 100644
--- a/test/color/test_xyz.py
+++ b/test/color/test_xyz.py
@@ -1,160 +1,186 @@
import pytest
import kornia
-import kornia.testing as utils # test utils
+from kornia.testing import BaseTester
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-class TestRgbToXyz:
+class TestRgbToXyz(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_xyz(img), torch.Tensor)
- @pytest.mark.parametrize("batch_size", [None, 1, 2, 5])
- def test_rgb_to_xyz(self, batch_size, device):
+ @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_xyz(img).shape == shape
- data = torch.tensor([[[0.9637, 0.0586, 0.6470, 0.6212, 0.9622],
- [0.8293, 0.4858, 0.8953, 0.2607, 0.3250],
- [0.5314, 0.4189, 0.8388, 0.8065, 0.2211],
- [0.9682, 0.2928, 0.4118, 0.2533, 0.0455]],
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.rgb_to_xyz([0.])
- [[0.6936, 0.3457, 0.9466, 0.9937, 0.2692],
- [0.7485, 0.7320, 0.8323, 0.6889, 0.4831],
- [0.1865, 0.7439, 0.1366, 0.8858, 0.2077],
- [0.6227, 0.6140, 0.3936, 0.5024, 0.4157]],
-
- [[0.6477, 0.9269, 0.7531, 0.7349, 0.9485],
- [0.4264, 0.8539, 0.9830, 0.2269, 0.1138],
- [0.3988, 0.1605, 0.6220, 0.0546, 0.1106],
- [0.2128, 0.5673, 0.0781, 0.1431, 0.3310]]])
-
- # Reference output generated using OpenCV: cv2.cvtColor(data, cv2.COLOR_RGB2XYZ)
- xyz_ref = torch.tensor([[[0.7623584, 0.31501925, 0.7412189, 0.7441359, 0.66425407],
- [0.6866283, 0.61618143, 0.84423876, 0.39480132, 0.32732624],
- [0.3578189, 0.4677382, 0.50703406, 0.6592388, 0.18541752],
- [0.6603961, 0.44267434, 0.32468265, 0.30994105, 0.22713262]],
-
- [[0.7477299, 0.32658678, 0.86891913, 0.89580274, 0.4656054],
- [0.7424382, 0.6884378, 0.8565741, 0.5644922, 0.4228247],
- [0.2751717, 0.63267857, 0.3209684, 0.8089483, 0.20354219],
- [0.6665957, 0.5423198, 0.37470126, 0.42349333, 0.33085644]],
-
- [[0.7167665, 0.92310345, 0.8409531, 0.82877415, 0.95198023],
- [0.51042646, 0.9080406, 1.0505873, 0.30275896, 0.17200153],
- [0.4114541, 0.24927814, 0.62354034, 0.17305644, 0.13412625],
- [0.29514894, 0.6179093, 0.12908883, 0.20075734, 0.3649534]]])
-
- data.to(device)
- xyz_ref.to(device)
-
- if batch_size is not None:
- data = data.repeat(batch_size, 1, 1, 1)
- xyz_ref = xyz_ref.repeat(batch_size, 1, 1, 1)
-
- xyz = kornia.color.RgbToXyz()
- out = xyz(data)
- assert_allclose(out, xyz_ref)
-
- def test_grad(self, device):
-
- data = torch.rand(2, 3, 4, 5).to(device)
- data = utils.tensor_to_gradcheck_var(data)
- assert gradcheck(kornia.color.RgbToXyz(), (data,), raise_exception=True)
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_xyz(img)
- @pytest.mark.parametrize("input_shape", [(2, 2), (3, 3, 5, 3, 3)])
- def test_shape(self, input_shape, device):
with pytest.raises(ValueError):
- xyz = kornia.color.RgbToXyz()
- out = xyz(torch.ones(*input_shape).to(device))
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_xyz(img)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([[
+ [[0.9637, 0.0586, 0.6470, 0.6212, 0.9622],
+ [0.8293, 0.4858, 0.8953, 0.2607, 0.3250],
+ [0.5314, 0.4189, 0.8388, 0.8065, 0.2211],
+ [0.9682, 0.2928, 0.4118, 0.2533, 0.0455]],
+ [[0.6936, 0.3457, 0.9466, 0.9937, 0.2692],
+ [0.7485, 0.7320, 0.8323, 0.6889, 0.4831],
+ [0.1865, 0.7439, 0.1366, 0.8858, 0.2077],
+ [0.6227, 0.6140, 0.3936, 0.5024, 0.4157]],
+ [[0.6477, 0.9269, 0.7531, 0.7349, 0.9485],
+ [0.4264, 0.8539, 0.9830, 0.2269, 0.1138],
+ [0.3988, 0.1605, 0.6220, 0.0546, 0.1106],
+ [0.2128, 0.5673, 0.0781, 0.1431, 0.3310]]
+ ]], device=device, dtype=dtype)
- def test_inverse(self, device):
- data = torch.rand(3, 4, 5).to(device)
- xyz = kornia.color.XyzToRgb()
- rgb = kornia.color.RgbToXyz()
+ # Reference output generated using OpenCV: cv2.cvtColor(data, cv2.COLOR_RGB2XYZ)
+ expected = torch.tensor([[
+ [[0.7623584, 0.31501925, 0.7412189, 0.7441359, 0.66425407],
+ [0.6866283, 0.61618143, 0.84423876, 0.39480132, 0.32732624],
+ [0.3578189, 0.4677382, 0.50703406, 0.6592388, 0.18541752],
+ [0.6603961, 0.44267434, 0.32468265, 0.30994105, 0.22713262]],
+ [[0.7477299, 0.32658678, 0.86891913, 0.89580274, 0.4656054],
+ [0.7424382, 0.6884378, 0.8565741, 0.5644922, 0.4228247],
+ [0.2751717, 0.63267857, 0.3209684, 0.8089483, 0.20354219],
+ [0.6665957, 0.5423198, 0.37470126, 0.42349333, 0.33085644]],
+ [[0.7167665, 0.92310345, 0.8409531, 0.82877415, 0.95198023],
+ [0.51042646, 0.9080406, 1.0505873, 0.30275896, 0.17200153],
+ [0.4114541, 0.24927814, 0.62354034, 0.17305644, 0.13412625],
+ [0.29514894, 0.6179093, 0.12908883, 0.20075734, 0.3649534]]
+ ]], device=device, dtype=dtype)
+
+ assert_allclose(kornia.color.rgb_to_xyz(data), expected)
+
+ def test_forth_and_back(self, device, dtype):
+ data = torch.rand(3, 4, 5, device=device, dtype=dtype)
+ xyz = kornia.color.rgb_to_xyz
+ rgb = kornia.color.xyz_to_rgb
data_out = xyz(rgb(data))
assert_allclose(data_out, data)
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
-
- data = torch.rand((2, 3, 4, 5)).to(device)
- xyz = kornia.color.rgb_to_xyz
- xyz_jit = torch.jit.script(kornia.color.rgb_to_xyz)
- assert_allclose(xyz_jit(data), xyz(data))
-
-
-class TestXyzToRgb:
-
- @pytest.mark.parametrize("batch_size", [None, 1, 2, 5])
- def test_xyz_to_rgb(self, batch_size, device):
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.rgb_to_xyz, (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_xyz
+ 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.RgbToXyz().to(device, dtype)
+ fcn = kornia.color.rgb_to_xyz
+ assert_allclose(ops(img), fcn(img))
+
+
+class TestXyzToRgb(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.xyz_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.xyz_to_rgb(img).shape == shape
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.xyz_to_rgb([0.])
- data = torch.tensor([[[0.6315228, 0.4196397, 0.33123854, 0.3277169, 0.20501322],
- [0.44133404, 0.4823162, 0.528561, 0.51644784, 0.28001237],
- [0.31131047, 0.8453884, 0.4486181, 0.6015828, 0.42048606],
- [0.5472367, 0.48154795, 0.36668795, 0.39913517, 0.40271503]],
-
- [[0.79137707, 0.501063, 0.3700857, 0.57410157, 0.15295872],
- [0.570678, 0.76664513, 0.48567873, 0.47680324, 0.2583247],
- [0.38080955, 0.9315215, 0.4404478, 0.50659215, 0.5984908],
- [0.5388581, 0.76993656, 0.4027568, 0.5952581, 0.68663263]],
-
- [[0.86013114, 0.17629854, 0.83010703, 0.27881518, 0.30543375],
- [0.17009716, 0.61201245, 0.33521807, 0.15526368, 0.7401195],
- [0.34011865, 0.6541383, 0.96909684, 0.43090558, 0.70467836],
- [0.6738866, 0.47461915, 0.91508406, 0.44147202, 0.14099535]]])
- # Reference output generated using OpenCV: cv2.cvtColor(data, cv2.COLOR_XYZ2RGB)
- rgb_ref = torch.tensor([[[0.4011656, 0.5017336, 0.09065688, 0.04048038, 0.2769511],
- [0.46811658, 0.07937729, 0.79911184, 0.8632158, 0.1413149],
- [0.2538725, 0.98146427, 0.29357457, 0.95588684, 0.09129933],
- [0.6090472, 0.14032376, 0.11294556, 0.15829873, 0.1792411]],
-
- [[0.90825266, 0.54057753, 0.40771842, 0.7709542, 0.1009315],
- [0.64988965, 0.99616426, 0.41274834, 0.40036052, 0.24396756],
- [0.42678973, 0.95531154, 0.43172216, 0.3851813, 0.7444883],
- [0.5084846, 0.99737406, 0.4381809, 0.7481805, 0.9036418]],
-
- [[0.78309417, 0.10751611, 0.82060075, 0.19588977, 0.3031369],
- [0.08796211, 0.51749885, 0.28474382, 0.09561294, 0.74540937],
- [0.2992335, 0.5486014, 0.95973116, 0.38571155, 0.64634556],
- [0.6330101, 0.3715171, 0.90575427, 0.36752605, 0.03138365]]])
-
- data.to(device)
- rgb_ref.to(device)
-
- if batch_size is not None:
- data = data.repeat(batch_size, 1, 1, 1)
- rgb_ref = rgb_ref.repeat(batch_size, 1, 1, 1)
-
- rgb = kornia.color.XyzToRgb()
- rgb_out = rgb(data)
-
- assert_allclose(rgb_out, rgb_ref)
-
- def test_grad(self, device):
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.xyz_to_rgb(img)
- data = torch.rand(2, 3, 4, 5).to(device)
- data = utils.tensor_to_gradcheck_var(data)
- assert gradcheck(kornia.color.XyzToRgb(), (data,), raise_exception=True)
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.xyz_to_rgb(img)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([[
+ [[0.6315228, 0.4196397, 0.33123854, 0.3277169, 0.20501322],
+ [0.44133404, 0.4823162, 0.528561, 0.51644784, 0.28001237],
+ [0.31131047, 0.8453884, 0.4486181, 0.6015828, 0.42048606],
+ [0.5472367, 0.48154795, 0.36668795, 0.39913517, 0.40271503]],
+ [[0.79137707, 0.501063, 0.3700857, 0.57410157, 0.15295872],
+ [0.570678, 0.76664513, 0.48567873, 0.47680324, 0.2583247],
+ [0.38080955, 0.9315215, 0.4404478, 0.50659215, 0.5984908],
+ [0.5388581, 0.76993656, 0.4027568, 0.5952581, 0.68663263]],
+ [[0.86013114, 0.17629854, 0.83010703, 0.27881518, 0.30543375],
+ [0.17009716, 0.61201245, 0.33521807, 0.15526368, 0.7401195],
+ [0.34011865, 0.6541383, 0.96909684, 0.43090558, 0.70467836],
+ [0.6738866, 0.47461915, 0.91508406, 0.44147202, 0.14099535]]
+ ]], device=device, dtype=dtype)
- def test_inverse(self, device):
- data = torch.rand(3, 4, 5).to(device)
- xyz = kornia.color.XyzToRgb()
- rgb = kornia.color.RgbToXyz()
+ # Reference output generated using OpenCV: cv2.cvtColor(data, cv2.COLOR_RGB2XYZ)
+ expected = torch.tensor([[
+ [[0.4011656, 0.5017336, 0.09065688, 0.04048038, 0.2769511],
+ [0.46811658, 0.07937729, 0.79911184, 0.8632158, 0.1413149],
+ [0.2538725, 0.98146427, 0.29357457, 0.95588684, 0.09129933],
+ [0.6090472, 0.14032376, 0.11294556, 0.15829873, 0.1792411]],
+ [[0.90825266, 0.54057753, 0.40771842, 0.7709542, 0.1009315],
+ [0.64988965, 0.99616426, 0.41274834, 0.40036052, 0.24396756],
+ [0.42678973, 0.95531154, 0.43172216, 0.3851813, 0.7444883],
+ [0.5084846, 0.99737406, 0.4381809, 0.7481805, 0.9036418]],
+ [[0.78309417, 0.10751611, 0.82060075, 0.19588977, 0.3031369],
+ [0.08796211, 0.51749885, 0.28474382, 0.09561294, 0.74540937],
+ [0.2992335, 0.5486014, 0.95973116, 0.38571155, 0.64634556],
+ [0.6330101, 0.3715171, 0.90575427, 0.36752605, 0.03138365]]
+ ]], device=device, dtype=dtype)
+
+ assert_allclose(kornia.color.xyz_to_rgb(data), expected, atol=1e-4, rtol=1e-4)
+
+ def test_forth_and_back(self, device, dtype):
+ data = torch.rand(3, 4, 5, device=device, dtype=dtype)
+ xyz = kornia.color.rgb_to_xyz
+ rgb = kornia.color.xyz_to_rgb
data_out = rgb(xyz(data))
assert_allclose(data_out, data)
- @pytest.mark.parametrize("input_shape", [([2, 2],), ([3, 3, 7, 3, 3],)])
- def test_shape(self, input_shape, device):
- with pytest.raises(ValueError):
- rgb = kornia.color.XyzToRgb()
- out = rgb(torch.ones(*input_shape).to(device))
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self, device):
-
- data = torch.rand((2, 3, 4, 5)).to(device)
- rgb = kornia.color.xyz_to_rgb
- rgb_jit = torch.jit.script(kornia.color.xyz_to_rgb)
- assert_allclose(rgb_jit(data), rgb(data))
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.xyz_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.xyz_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.XyzToRgb().to(device, dtype)
+ fcn = kornia.color.xyz_to_rgb
+ assert_allclose(ops(img), fcn(img))
diff --git a/test/color/test_ycbcr.py b/test/color/test_ycbcr.py
index 24c66138f..31c7b904e 100644
--- a/test/color/test_ycbcr.py
+++ b/test/color/test_ycbcr.py
@@ -1,247 +1,190 @@
import pytest
import kornia
-import kornia.testing as utils # test utils
-
-import numpy as np
+from kornia.testing import BaseTester
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-class TestRgbToYcbcr:
-
- def test_rgb_to_ycbcr(self):
- data = torch.tensor([[[0.7925, 0.8704, 0.2771, 0.3279, 0.0193],
- [0.2483, 0.6212, 0.9859, 0.5044, 0.5621],
- [0.5762, 0.3959, 0.2931, 0.2669, 0.0243],
- [0.6989, 0.0529, 0.8344, 0.6523, 0.8980],
- [0.5181, 0.9341, 0.2172, 0.0520, 0.7266]],
- [[0.8413, 0.0284, 0.3625, 0.8864, 0.5595],
- [0.3791, 0.0235, 0.4251, 0.0619, 0.5270],
- [0.3516, 0.8005, 0.9571, 0.4113, 0.6119],
- [0.0632, 0.8836, 0.0261, 0.1550, 0.4923],
- [0.2332, 0.7044, 0.9514, 0.2443, 0.2818]],
- [[0.6899, 0.2063, 0.3179, 0.8989, 0.4378],
- [0.0384, 0.5230, 0.6416, 0.9749, 0.7863],
- [0.8577, 0.3115, 0.2375, 0.5446, 0.9837],
- [0.3213, 0.6618, 0.5977, 0.3999, 0.4962],
- [0.1385, 0.5831, 0.9756, 0.8714, 0.8017]]]) # 3x5x5
-
- y_expected = torch.tensor([[0.8094629, 0.30041942, 0.33188093, 0.7208117, 0.38409105],
- [0.30114722, 0.2591345, 0.6174494, 0.29830942, 0.5670594],
- [0.47643244, 0.623786, 0.6765313, 0.38332558, 0.47857922],
- [0.2826805, 0.60994685, 0.33298355, 0.3316514, 0.6140681],
- [0.30756146, 0.75926465, 0.7346588, 0.25828302, 0.4740529]])
- cr_expected = torch.tensor([[0.48791525, 0.9063825, 0.46094114, 0.21983841, 0.23989305],
- [0.46231723, 0.7581379, 0.7626976, 0.6469568, 0.496467],
- [0.5711212, 0.33752257, 0.22661471, 0.4169921, 0.17608929],
- [0.7967522, 0.10283372, 0.85753804, 0.72865105, 0.70245713],
- [0.65009415, 0.6246666, 0.13107029, 0.35291404, 0.6800583]])
- cb_expected = torch.tensor([[0.43258172, 0.4469004, 0.492101, 0.60042214, 0.53028214],
- [0.35180065, 0.64882994, 0.51362985, 0.8815981, 0.62366176],
- [0.71505237, 0.32387614, 0.2523859, 0.5909421, 0.78490996],
- [0.52178127, 0.529264, 0.64931786, 0.53848785, 0.43353173],
- [0.4046721, 0.40065458, 0.6358658, 0.8458253, 0.6847739]])
-
- # Kornia
- f = kornia.color.RgbToYcbcr()
- result = f(data)
-
- y = result[0, :, :]
- cb = result[1, :, :]
- cr = result[2, :, :]
-
- assert_allclose(y, y_expected)
- assert_allclose(cb, cb_expected)
- assert_allclose(cr, cr_expected)
-
- def test_batch_rgb_to_ycbcr(self):
- data = torch.tensor([[[0.7925, 0.8704, 0.2771, 0.3279, 0.0193],
- [0.2483, 0.6212, 0.9859, 0.5044, 0.5621],
- [0.5762, 0.3959, 0.2931, 0.2669, 0.0243],
- [0.6989, 0.0529, 0.8344, 0.6523, 0.8980],
- [0.5181, 0.9341, 0.2172, 0.0520, 0.7266]],
- [[0.8413, 0.0284, 0.3625, 0.8864, 0.5595],
- [0.3791, 0.0235, 0.4251, 0.0619, 0.5270],
- [0.3516, 0.8005, 0.9571, 0.4113, 0.6119],
- [0.0632, 0.8836, 0.0261, 0.1550, 0.4923],
- [0.2332, 0.7044, 0.9514, 0.2443, 0.2818]],
- [[0.6899, 0.2063, 0.3179, 0.8989, 0.4378],
- [0.0384, 0.5230, 0.6416, 0.9749, 0.7863],
- [0.8577, 0.3115, 0.2375, 0.5446, 0.9837],
- [0.3213, 0.6618, 0.5977, 0.3999, 0.4962],
- [0.1385, 0.5831, 0.9756, 0.8714, 0.8017]]]) # 3x5x5
-
- # OpenCV
- expected = torch.tensor([[[0.8094629, 0.30041942, 0.33188093, 0.7208117, 0.38409105],
- [0.30114722, 0.2591345, 0.6174494, 0.29830942, 0.5670594],
- [0.47643244, 0.623786, 0.6765313, 0.38332558, 0.47857922],
- [0.2826805, 0.60994685, 0.33298355, 0.3316514, 0.6140681],
- [0.30756146, 0.75926465, 0.7346588, 0.25828302, 0.4740529]],
- [[0.43258172, 0.4469004, 0.492101, 0.60042214, 0.53028214],
- [0.35180065, 0.64882994, 0.51362985, 0.8815981, 0.62366176],
- [0.71505237, 0.32387614, 0.2523859, 0.5909421, 0.78490996],
- [0.52178127, 0.529264, 0.64931786, 0.53848785, 0.43353173],
- [0.4046721, 0.40065458, 0.6358658, 0.8458253, 0.6847739]],
- [[0.48791525, 0.9063825, 0.46094114, 0.21983841, 0.23989305],
- [0.46231723, 0.7581379, 0.7626976, 0.6469568, 0.496467],
- [0.5711212, 0.33752257, 0.22661471, 0.4169921, 0.17608929],
- [0.7967522, 0.10283372, 0.85753804, 0.72865105, 0.70245713],
- [0.65009415, 0.6246666, 0.13107029, 0.35291404, 0.6800583]]])
-
- # Kornia
- f = kornia.color.RgbToYcbcr()
- data = data.repeat(2, 1, 1, 1) # 2x3x5x5
- expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5
- assert_allclose(f(data), expected)
-
- def test_gradcheck(self):
- data = torch.rand(3, 5, 5) # 3x2x2
-
- data = utils.tensor_to_gradcheck_var(data) # to var
-
- assert gradcheck(kornia.color.RgbToYcbcr(), (data,),
- raise_exception=True)
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
- @torch.jit.script
- def op_script(data: torch.Tensor) -> torch.Tensor:
- return kornia.rgb_to_ycbcr(data)
- data = torch.tensor([[[[21., 22.],
- [22., 22.]],
-
- [[13., 14.],
- [14., 14.]],
-
- [[8., 8.],
- [8., 8.]]]]) # 3x2x2
-
- actual = op_script(data)
- expected = kornia.rgb_to_ycbcr(data)
- assert_allclose(actual, expected)
-
-
-class TestYcbcrToRgb:
-
- def test_ycbcr_to_rgb(self):
- data = torch.tensor([[[0.9892, 0.2620, 0.4184, 0.5286, 0.2793],
- [0.0722, 0.8828, 0.8714, 0.0657, 0.7798],
- [0.8118, 0.7522, 0.0260, 0.8811, 0.5226],
- [0.0644, 0.3648, 0.4448, 0.4202, 0.7316],
- [0.9138, 0.1956, 0.4257, 0.6381, 0.1353]],
- [[0.7408, 0.8529, 0.5119, 0.0220, 0.0226],
- [0.8963, 0.5652, 0.9568, 0.6977, 0.8221],
- [0.4645, 0.0478, 0.4952, 0.5492, 0.4861],
- [0.9980, 0.9978, 0.0281, 0.5283, 0.8146],
- [0.7789, 0.2663, 0.6437, 0.6926, 0.5627]],
- [[0.7377, 0.7152, 0.3080, 0.8515, 0.4841],
- [0.7192, 0.3297, 0.7337, 0.0230, 0.2464],
- [0.6399, 0.8998, 0.3838, 0.3043, 0.3774],
- [0.1281, 0.6731, 0.4218, 0.3963, 0.8541],
- [0.2245, 0.2413, 0.2351, 0.9522, 0.8158]]]) # 3x5x5
-
- # OpenCV
- # expected = cv2.cvtColor(data_cv, cv2.COLOR_YCrCb2RGB)
- data_cv = data.numpy().transpose(1, 2, 0).copy()
- data_cv[..., 1:] = data_cv[..., -1:0:-1]
-
- r_expected = torch.tensor([[1.3226931, 0.5639256, 0.14902398, 1.0217545, 0.2569923],
- [0.37973762, 0.64386904, 1.1992811, -0.603531, 0.4239992],
- [1.0080798, 1.3131194, -0.1370286, 0.60653293, 0.3505922],
- [-0.4573757, 0.6076593, 0.33508536, 0.27470887, 1.2284023],
- [0.52727354, -0.1673561, 0.05404532, 1.2725366, 0.5783674]])
- g_expected = torch.tensor([[0.736647, -0.01305042, 0.55139434, 0.44206098, 0.4548782],
- [-0.22063601, 0.98196536, 0.54739904, 0.33826917, 0.850068],
- [0.72412336, 0.6222996, 0.110618, 1.0039049, 0.614918],
- [0.15862459, 0.0699634, 0.66296846, 0.4845066, 0.3705502],
- [1.0145653, 0.46070462, 0.5654058, 0.24897486, -0.11174999]])
- b_expected = torch.tensor([[1.4161384, 0.88769174, 0.4394987, -0.31889397, -0.5671302],
- [0.77483994, 0.99839956, 1.6813064, 0.41622213, 1.3508832],
- [0.7488585, -0.04955059, 0.01748962, 0.9683316, 0.49795526],
- [0.9473541, 1.2473994, -0.3918787, 0.47037587, 1.2893858],
- [1.4082898, -0.21875012, 0.6804801, 0.9795798, 0.24646705]])
-
- # Kornia
- f = kornia.color.YcbcrToRgb()
- result = f(data)
-
- r = result[0, :, :]
- g = result[1, :, :]
- b = result[2, :, :]
-
- assert_allclose(r, r_expected)
- assert_allclose(g, g_expected)
- assert_allclose(b, b_expected)
-
- def test_batch_ycbcr_to_rgb(self):
- data = torch.tensor([[[0.9892, 0.2620, 0.4184, 0.5286, 0.2793],
- [0.0722, 0.8828, 0.8714, 0.0657, 0.7798],
- [0.8118, 0.7522, 0.0260, 0.8811, 0.5226],
- [0.0644, 0.3648, 0.4448, 0.4202, 0.7316],
- [0.9138, 0.1956, 0.4257, 0.6381, 0.1353]],
- [[0.7408, 0.8529, 0.5119, 0.0220, 0.0226],
- [0.8963, 0.5652, 0.9568, 0.6977, 0.8221],
- [0.4645, 0.0478, 0.4952, 0.5492, 0.4861],
- [0.9980, 0.9978, 0.0281, 0.5283, 0.8146],
- [0.7789, 0.2663, 0.6437, 0.6926, 0.5627]],
- [[0.7377, 0.7152, 0.3080, 0.8515, 0.4841],
- [0.7192, 0.3297, 0.7337, 0.0230, 0.2464],
- [0.6399, 0.8998, 0.3838, 0.3043, 0.3774],
- [0.1281, 0.6731, 0.4218, 0.3963, 0.8541],
- [0.2245, 0.2413, 0.2351, 0.9522, 0.8158]]]) # 3x5x5
-
- # OpenCV
- # expected = cv2.cvtColor(data_cv, cv2.COLOR_YCrCb2RGB)
- data_cv = data.numpy().transpose(1, 2, 0).copy()
- data_cv[..., 1:] = data_cv[..., -1:0:-1]
-
- expected = torch.tensor([[[1.3226931, 0.5639256, 0.14902398, 1.0217545, 0.2569923],
- [0.37973762, 0.64386904, 1.1992811, -0.603531, 0.4239992],
- [1.0080798, 1.3131194, -0.1370286, 0.60653293, 0.3505922],
- [-0.4573757, 0.6076593, 0.33508536, 0.27470887, 1.2284023],
- [0.52727354, -0.1673561, 0.05404532, 1.2725366, 0.5783674]],
- [[0.736647, -0.01305042, 0.55139434, 0.44206098, 0.4548782],
- [-0.22063601, 0.98196536, 0.54739904, 0.33826917, 0.850068],
- [0.72412336, 0.6222996, 0.110618, 1.0039049, 0.614918],
- [0.15862459, 0.0699634, 0.66296846, 0.4845066, 0.3705502],
- [1.0145653, 0.46070462, 0.5654058, 0.24897486, -0.11174999]],
- [[1.4161384, 0.88769174, 0.4394987, -0.31889397, -0.5671302],
- [0.77483994, 0.99839956, 1.6813064, 0.41622213, 1.3508832],
- [0.7488585, -0.04955059, 0.01748962, 0.9683316, 0.49795526],
- [0.9473541, 1.2473994, -0.3918787, 0.47037587, 1.2893858],
- [1.4082898, -0.21875012, 0.6804801, 0.9795798, 0.24646705]]])
-
- # Kornia
- f = kornia.color.YcbcrToRgb()
- data = data.repeat(2, 1, 1, 1) # 2x3x5x5
- expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5
- assert_allclose(f(data), expected)
-
- def test_gradcheck(self):
- data = torch.rand(3, 5, 5) # 3x5x5
- data = utils.tensor_to_gradcheck_var(data) # to var
-
- assert gradcheck(kornia.color.YcbcrToRgb(), (data,),
- raise_exception=True)
-
- @pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
- @torch.jit.script
- def op_script(data: torch.Tensor) -> torch.Tensor:
- return kornia.ycbcr_to_rgb(data)
-
- data = torch.tensor([[[[21., 22.],
- [22., 22.]],
-
- [[13., 14.],
- [14., 14.]],
-
- [[8., 8.],
- [8., 8.]]]]) # 3x2x2
-
- actual = op_script(data)
- expected = kornia.ycbcr_to_rgb(data)
- assert_allclose(actual, expected)
+class TestRgbToYcbcr(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_ycbcr(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_ycbcr(img).shape == shape
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.rgb_to_ycbcr([0.])
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_ycbcr(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_ycbcr(img)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([[
+ [[0.7925, 0.8704, 0.2771, 0.3279, 0.0193],
+ [0.2483, 0.6212, 0.9859, 0.5044, 0.5621],
+ [0.5762, 0.3959, 0.2931, 0.2669, 0.0243],
+ [0.6989, 0.0529, 0.8344, 0.6523, 0.8980],
+ [0.5181, 0.9341, 0.2172, 0.0520, 0.7266]],
+ [[0.8413, 0.0284, 0.3625, 0.8864, 0.5595],
+ [0.3791, 0.0235, 0.4251, 0.0619, 0.5270],
+ [0.3516, 0.8005, 0.9571, 0.4113, 0.6119],
+ [0.0632, 0.8836, 0.0261, 0.1550, 0.4923],
+ [0.2332, 0.7044, 0.9514, 0.2443, 0.2818]],
+ [[0.6899, 0.2063, 0.3179, 0.8989, 0.4378],
+ [0.0384, 0.5230, 0.6416, 0.9749, 0.7863],
+ [0.8577, 0.3115, 0.2375, 0.5446, 0.9837],
+ [0.3213, 0.6618, 0.5977, 0.3999, 0.4962],
+ [0.1385, 0.5831, 0.9756, 0.8714, 0.8017]]
+ ]], device=device, dtype=dtype)
+
+ # Reference output generated using OpenCV: cv2.cvtColor(data, cv2.COLOR_RGB2XYZ)
+ expected = torch.tensor([[
+ [[0.8094629, 0.30041942, 0.33188093, 0.7208117, 0.38409105],
+ [0.30114722, 0.2591345, 0.6174494, 0.29830942, 0.5670594],
+ [0.47643244, 0.623786, 0.6765313, 0.38332558, 0.47857922],
+ [0.2826805, 0.60994685, 0.33298355, 0.3316514, 0.6140681],
+ [0.30756146, 0.75926465, 0.7346588, 0.25828302, 0.4740529]],
+ [[0.43258172, 0.4469004, 0.492101, 0.60042214, 0.53028214],
+ [0.35180065, 0.64882994, 0.51362985, 0.8815981, 0.62366176],
+ [0.71505237, 0.32387614, 0.2523859, 0.5909421, 0.78490996],
+ [0.52178127, 0.529264, 0.64931786, 0.53848785, 0.43353173],
+ [0.4046721, 0.40065458, 0.6358658, 0.8458253, 0.6847739]],
+ [[0.48791525, 0.9063825, 0.46094114, 0.21983841, 0.23989305],
+ [0.46231723, 0.7581379, 0.7626976, 0.6469568, 0.496467],
+ [0.5711212, 0.33752257, 0.22661471, 0.4169921, 0.17608929],
+ [0.7967522, 0.10283372, 0.85753804, 0.72865105, 0.70245713],
+ [0.65009415, 0.6246666, 0.13107029, 0.35291404, 0.6800583]]
+ ]], device=device, dtype=dtype)
+
+ assert_allclose(kornia.color.rgb_to_ycbcr(data), expected, atol=1e-4, rtol=1e-4)
+
+ # TODO: investigate and implement me
+ # def test_forth_and_back(self, device, dtype):
+ # pass
+
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.rgb_to_ycbcr, (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_ycbcr
+ 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.RgbToYcbcr().to(device, dtype)
+ fcn = kornia.color.rgb_to_ycbcr
+ assert_allclose(ops(img), fcn(img))
+
+
+class TestYcbcrToRgb(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.ycbcr_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.ycbcr_to_rgb(img).shape == shape
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.ycbcr_to_rgb([0.])
+
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.ycbcr_to_rgb(img)
+
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.ycbcr_to_rgb(img)
+
+ def test_unit(self, device, dtype):
+ data = torch.tensor([[
+ [[0.9892, 0.2620, 0.4184, 0.5286, 0.2793],
+ [0.0722, 0.8828, 0.8714, 0.0657, 0.7798],
+ [0.8118, 0.7522, 0.0260, 0.8811, 0.5226],
+ [0.0644, 0.3648, 0.4448, 0.4202, 0.7316],
+ [0.9138, 0.1956, 0.4257, 0.6381, 0.1353]],
+ [[0.7408, 0.8529, 0.5119, 0.0220, 0.0226],
+ [0.8963, 0.5652, 0.9568, 0.6977, 0.8221],
+ [0.4645, 0.0478, 0.4952, 0.5492, 0.4861],
+ [0.9980, 0.9978, 0.0281, 0.5283, 0.8146],
+ [0.7789, 0.2663, 0.6437, 0.6926, 0.5627]],
+ [[0.7377, 0.7152, 0.3080, 0.8515, 0.4841],
+ [0.7192, 0.3297, 0.7337, 0.0230, 0.2464],
+ [0.6399, 0.8998, 0.3838, 0.3043, 0.3774],
+ [0.1281, 0.6731, 0.4218, 0.3963, 0.8541],
+ [0.2245, 0.2413, 0.2351, 0.9522, 0.8158]]
+ ]], device=device, dtype=dtype)
+
+ # Reference output generated using OpenCV: cv2.cvtColor(data, cv2.COLOR_RGB2XYZ)
+ expected = torch.tensor([[
+ [[1.3226931, 0.5639256, 0.14902398, 1.0217545, 0.2569923],
+ [0.37973762, 0.64386904, 1.1992811, -0.603531, 0.4239992],
+ [1.0080798, 1.3131194, -0.1370286, 0.60653293, 0.3505922],
+ [-0.4573757, 0.6076593, 0.33508536, 0.27470887, 1.2284023],
+ [0.52727354, -0.1673561, 0.05404532, 1.2725366, 0.5783674]],
+ [[0.736647, -0.01305042, 0.55139434, 0.44206098, 0.4548782],
+ [-0.22063601, 0.98196536, 0.54739904, 0.33826917, 0.850068],
+ [0.72412336, 0.6222996, 0.110618, 1.0039049, 0.614918],
+ [0.15862459, 0.0699634, 0.66296846, 0.4845066, 0.3705502],
+ [1.0145653, 0.46070462, 0.5654058, 0.24897486, -0.11174999]],
+ [[1.4161384, 0.88769174, 0.4394987, -0.31889397, -0.5671302],
+ [0.77483994, 0.99839956, 1.6813064, 0.41622213, 1.3508832],
+ [0.7488585, -0.04955059, 0.01748962, 0.9683316, 0.49795526],
+ [0.9473541, 1.2473994, -0.3918787, 0.47037587, 1.2893858],
+ [1.4082898, -0.21875012, 0.6804801, 0.9795798, 0.24646705]]
+ ]], device=device, dtype=dtype)
+
+ assert_allclose(kornia.color.ycbcr_to_rgb(data), expected, atol=1e-4, rtol=1e-4)
+
+ # TODO: investigate and implement me
+ # def test_forth_and_back(self, device, dtype):
+ # pass
+
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.ycbcr_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.ycbcr_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.YcbcrToRgb().to(device, dtype)
+ fcn = kornia.color.ycbcr_to_rgb
+ assert_allclose(ops(img), fcn(img))
diff --git a/test/color/test_yuv.py b/test/color/test_yuv.py
index 503be6d4d..9a792510e 100644
--- a/test/color/test_yuv.py
+++ b/test/color/test_yuv.py
@@ -2,81 +2,128 @@ import pytest
import kornia
import kornia.testing as utils # test utils
+from kornia.testing import BaseTester
import torch
from torch.autograd import gradcheck
from torch.testing import assert_allclose
-class TestRgbYuvConversion:
- # Parameterize for CHW and NCHW shapes
- @pytest.mark.parametrize('shape', ((3, 4, 5), (2, 3, 4, 5)))
- # RGB to YUV and YUV to RGB should be inverse operations
- def test_inverse_operations(self, device, shape):
- input = torch.rand(*shape).to(device)
- yuv_to_rgb_converter = kornia.color.YuvToRgb()
- rgb_to_yuv_converter = kornia.color.RgbToYuv()
+class TestRgbToYuv(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_yuv(img), torch.Tensor)
- assert_allclose(input, yuv_to_rgb_converter(rgb_to_yuv_converter(input)), rtol=0.005, atol=0.005)
- assert_allclose(input, rgb_to_yuv_converter(yuv_to_rgb_converter(input)), rtol=0.005, atol=0.005)
+ @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_yuv(img).shape == shape
- def test_gradcheck(self, device):
-
- # prepare input data
- data = torch.tensor([[[0.1, 0.2],
- [0.1, 0.1]],
-
- [[0.2, 0.5],
- [0.4, 0.2]],
-
- [[0.3, 0.3],
- [0.5, 0.5]]]).to(device) # 3x2x2
-
- data = utils.tensor_to_gradcheck_var(data) # to var
-
- assert gradcheck(kornia.color.YuvToRgb(), (data,),
- raise_exception=True)
- assert gradcheck(kornia.color.RgbToYuv(), (data,),
- raise_exception=True)
-
- def test_rgb_to_yuv_shape(self, device):
- channels, height, width = 3, 4, 5
- img = torch.ones(channels, height, width).to(device)
- assert kornia.rgb_to_yuv(img).shape == (channels, height, width)
-
- def test_rgb_to_yuv_batch_shape(self, device):
- batch_size, channels, height, width = 2, 3, 4, 5
- img = torch.ones(batch_size, channels, height, width).to(device)
- assert kornia.rgb_to_yuv(img).shape == \
- (batch_size, channels, height, width)
-
- def test_yuv_to_rgb_shape(self, device):
- channels, height, width = 3, 4, 5
- img = torch.ones(channels, height, width).to(device)
- assert kornia.yuv_to_rgb(img).shape == (channels, height, width)
-
- def test_yuv_to_rgb_batch_shape(self, device):
- batch_size, channels, height, width = 2, 3, 4, 5
- img = torch.ones(batch_size, channels, height, width).to(device)
- assert kornia.yuv_to_rgb(img).shape == \
- (batch_size, channels, height, width)
-
- def test_rgb_to_yuv_type(self):
+ def test_exception(self, device, dtype):
with pytest.raises(TypeError):
- out = kornia.rgb_to_yuv(1)
+ assert kornia.color.rgb_to_yuv([0.])
- def test_yuv_to_rbg_type(self):
- with pytest.raises(TypeError):
- out = kornia.yuv_to_rgb(1)
+ with pytest.raises(ValueError):
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_yuv(img)
- @pytest.mark.parametrize("bad_input_shapes", [([2, 2],), ([3, 3, 3, 3, 3],), ([2, 2, 2],), ([2, 2, 2, 2],)])
- def test_rgb_to_yuv_shape_bad(self, bad_input_shapes):
with pytest.raises(ValueError):
- out = kornia.rgb_to_yuv(torch.ones(*bad_input_shapes))
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.rgb_to_yuv(img)
+
+ # TODO: investigate and implement me
+ # def test_unit(self, device, dtype):
+ # pass
+
+ # TODO: improve accuracy
+ def test_forth_and_back(self, device, dtype):
+ data = torch.rand(3, 4, 5, device=device, dtype=dtype)
+ yuv = kornia.color.rgb_to_yuv
+ rgb = kornia.color.yuv_to_rgb
+
+ data_out = rgb(yuv(data))
+ assert_allclose(data_out, data, rtol=1e-2, atol=1e-2)
+
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.rgb_to_yuv, (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_yuv
+ 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.RgbToYuv().to(device, dtype)
+ fcn = kornia.color.rgb_to_yuv
+ assert_allclose(ops(img), fcn(img))
+
+
+class TestYuvToRgb(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.yuv_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.yuv_to_rgb(img).shape == shape
+
+ def test_exception(self, device, dtype):
+ with pytest.raises(TypeError):
+ assert kornia.color.yuv_to_rgb([0.])
- @pytest.mark.parametrize("bad_input_shapes", [([2, 2],), ([3, 3, 3, 3, 3],), ([2, 2, 2],), ([2, 2, 2, 2],)])
- def test_yuv_to_rbg_shape_bad(self, bad_input_shapes):
with pytest.raises(ValueError):
- out = kornia.yuv_to_rgb(torch.ones(*bad_input_shapes))
+ img = torch.ones(1, 1, device=device, dtype=dtype)
+ assert kornia.color.yuv_to_rgb(img)
- # TODO add cv2 comparision test
+ with pytest.raises(ValueError):
+ img = torch.ones(2, 1, 1, device=device, dtype=dtype)
+ assert kornia.color.yuv_to_rgb(img)
+
+ # TODO: investigate and implement me
+ # def test_unit(self, device, dtype):
+ # pass
+
+ # TODO: improve accuracy
+ def test_forth_and_back(self, device, dtype):
+ data = torch.rand(3, 4, 5, device=device, dtype=dtype)
+ rgb = kornia.color.yuv_to_rgb
+ yuv = kornia.color.rgb_to_yuv
+
+ data_out = rgb(yuv(data))
+ assert_allclose(data_out, data, rtol=1e-2, atol=1e-2)
+
+ @pytest.mark.grad
+ def test_gradcheck(self, device, dtype):
+ B, C, H, W = 2, 3, 4, 4
+ img = torch.rand(B, C, H, W, device=device, dtype=torch.float64, requires_grad=True)
+ assert gradcheck(kornia.color.yuv_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.yuv_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.YuvToRgb().to(device, dtype)
+ fcn = kornia.color.yuv_to_rgb
+ assert_allclose(ops(img), fcn(img))
diff --git a/test/enhance/test_normalize.py b/test/enhance/test_normalize.py
index 9a0a53edc..4f6ae7a69 100644
--- a/test/enhance/test_normalize.py
+++ b/test/enhance/test_normalize.py
@@ -242,7 +242,7 @@ class TestNormalizeMinMax(BaseTester):
@pytest.mark.parametrize("input_shape", [
(1, 2, 3, 4), (2, 1, 4, 3), (1, 3, 2, 1)])
- def test_batch(self, device, dtype, input_shape):
+ def test_cardinality(self, device, dtype, input_shape):
x = torch.rand(input_shape, device=device, dtype=dtype)
assert kornia.normalize_min_max(x).shape == input_shape
@@ -270,12 +270,18 @@ class TestNormalizeMinMax(BaseTester):
actual = kornia.normalize_min_max(x, min_val=-1., max_val=1.)
assert_allclose(actual, expected, atol=1e-6, rtol=1e-6)
+ @pytest.mark.jit
def test_jit(self, device, dtype):
x = torch.ones(1, 1, 1, 1, device=device, dtype=dtype)
op = kornia.normalize_min_max
op_jit = torch.jit.script(op)
assert_allclose(op(x), op_jit(x))
+ @pytest.mark.grad
def test_gradcheck(self, device, dtype):
x = torch.ones(1, 1, 1, 1, device=device, dtype=torch.float64, requires_grad=True)
assert gradcheck(kornia.normalize_min_max, (x,), raise_exception=True)
+
+ @pytest.mark.nn
+ def test_module(self, device, dtype):
+ pass