π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
rgb to bgr and jit tests
diegovd0296f6b008e49b84
diff --git a/test/test_image.py b/test/test_image.pyindex e004a2726..76de0ce1a 100644--- a/test/test_image.py+++ b/test/test_image.py@@ -109,6 +109,23 @@ class TestNormalize:f = image.Normalize(mean, std)assert_allclose(f(data), expected)+ def test_jit(self):+ @torch.jit.script+ def op_script(data: torch.Tensor, mean: torch.Tensor,+ std: torch.Tensor) -> torch.Tensor:++ return image.normalize(data, mean, std)++ data = torch.ones(2, 3, 1, 1)+ data += 2++ mean = torch.tensor([0.5, 1.0, 2.0]).repeat(2, 1)+ std = torch.tensor([2.0, 2.0, 2.0]).repeat(2, 1)++ actual = op.script(data, mean, std)+ expected = image.normalize(data, mean, std)+ assert_allclose(actual, expected)+def test_gradcheck(self):# prepare input data@@ -172,33 +189,106 @@ class TestBgRToRgb:def test_bgr_to_rgb(self):# prepare input data- data = torch.ones(3, 2, 2)+ 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.]]]) # 3x2x2f = image.BgrToRgb()- assert_allclose(f(data)[0], data[-1])- assert_allclose(f(data)[-1], data[0])+ assert_allclose(f(data), expected)def test_batch_bgr_to_rgb(self):# prepare input data- data = torch.ones(5, 3, 2, 2)+ data = torch.tensor([[[[1., 1.],+ [1., 1.]],++ [[2., 2.],+ [2., 2.]],++ [[3., 3.],+ [3., 3.]]],++ [[[1., 1.],+ [1., 1.]],++ [[2., 2.],+ [2., 2.]],++ [[3., 3.],+ [3., 3.]]]]) # 2x3x2x2++ expected = torch.tensor([[[[3., 3.],+ [3., 3.]],++ [[2., 2.],+ [2., 2.]],++ [[1., 1.],+ [1., 1.]]],++ [[[3., 3.],+ [3., 3.]],++ [[2., 2.],+ [2., 2.]],++ [[1., 1.],+ [1., 1.]]]]) # 2x3x2x2f = image.BgrToRgb()out = f(data)- for i in range(out.shape[0]):- assert_allclose(out[i][0], data[i][-1])- assert_allclose(out[i][-1], out[i][0])+ assert_allclose(out, expected)def test_gradcheck(self):# prepare input data- data = torch.ones(2, 3, 1, 1)+ data = torch.tensor([[[1., 1.],+ [1., 1.]],++ [[2., 2.],+ [2., 2.]],++ [[3., 3.],+ [3., 3.]]]) # 3x2x2data = utils.tensor_to_gradcheck_var(data) # to varassert gradcheck(image.BgrToRgb(), (data,),raise_exception=True)+ def test_jit(self):+ @torch.jit.script+ def op_script(data: torch.Tensor) -> torch.Tensor:++ return image.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 = image.bgr_to_rgb(data)+ assert_allclose(actual, expected)+class TestRgbToGrayscale:def test_rgb_to_grayscale(self):diff --git a/torchgeometry/image/__init__.py b/torchgeometry/image/__init__.pyindex 70c9b3096..490f2f283 100644--- a/torchgeometry/image/__init__.py+++ b/torchgeometry/image/__init__.py@@ -4,4 +4,4 @@ from .normalization import Normalize, normalizefrom .laplacian import get_laplacian_kernel, get_laplacian_kernel2dfrom .laplacian import Laplacian, laplacianfrom .color import rgb_to_grayscale, RgbToGrayscale, BgrToRgb, bgr_to_rgb-from .color import RgbToHsV, rgb_to_hsv+from .color import RgbToHsV, rgb_to_hsv, RgbToBgr, rgb_to_bgrdiff --git a/torchgeometry/image/color.py b/torchgeometry/image/color.pyindex eb5547b41..db6b450fb 100644--- a/torchgeometry/image/color.py+++ b/torchgeometry/image/color.py@@ -1,5 +1,6 @@import torchimport torch.nn as nn+import numpy as npclass RgbToHsV(nn.Module):@@ -26,7 +27,7 @@ class RgbToHsV(nn.Module):return rgb_to_hsv(image)-def rgb_to_hsv(image):+def rgb_to_hsv(rgb): # TODO"""Convert an RGB image to HSV@@ -37,6 +38,7 @@ def rgb_to_hsv(image):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)))@@ -45,44 +47,67 @@ def rgb_to_hsv(image):raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}".format(image.shape))- out = torch.empty_like(image)- maxc = image.max(0)[0]- minc = image.min(0)[0]+ input_shape = rgb.shape+ rgb = rgb.reshape(-1, 3)+ r, g, b = rgb[:, 0], rgb[:, 1], rgb[:, 2]- delta = torch.tensor(image.numpy().ptp(0))+ maxc = np.maximum(np.maximum(r, g), b)+ minc = np.minimum(np.minimum(r, g), b)+ v = maxc- r = image[..., 0, :, :]- g = image[..., 1, :, :]- b = image[..., 2, :, :]+ deltac = maxc - minc+ s = deltac / maxc+ deltac[deltac == 0] = 1+ rc = (maxc - r) / deltac+ gc = (maxc - g) / deltac+ bc = (maxc - b) / deltac- # brightness- v = maxc+ h = 4.0 + gc - rc+ h[g == maxc] = 2.0 + rc[g == maxc] - bc[g == maxc]+ h[r == maxc] = bc[r == maxc] - gc[r == maxc]+ h[minc == maxc] = 0.0- # saturation- s = delta / v- s[delta == 0.] = 0.+ h = (h / 6.0) % 1.0+ res = np.dstack([h, s, v])+ return res.reshape(input_shape)+ '''- # red is max- idx = r == maxc- out[..., 0, idx] = (g[idx] - b[idx]) / delta[idx]- # green is max- idx = g == maxc- out[..., 0, idx] = 2.0 + (b[idx] - r[idx]) / delta[idx]+class RgbToBgr(nn.Module):- # blue is max- idx = b == maxc- out[..., 0, idx] = 4.0 + (r[idx] - g[idx]) / delta[idx]+ """convert image from RGB to BGR+ The image data is assumed to be in the range of (0, 1).- # hue- h = (out[..., 0, :, :] / 6) % 1.- h[delta == 0.] = 0.+ args:+ image (torch.tensor): RGB image to be converted to BGR- out[..., 0, :, :] = h- out[..., 1, :, :] = s- out[..., 2, :, :] = v+ returns:+ torch.tensor: BGR version of the image.- return out+ shape:+ - image: :math:`(*, 3, h, w)`+ - output: :math:`(*, 3, h, w)`++ """++ def __init__(self) -> None:+ super(RgbToBgr, self).__init__()++ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore+ return rgb_to_bgr(image)+++def rgb_to_bgr(image: torch.Tensor) -> torch.Tensor:+ """Convert a RGB image to BGR.++ Args:+ input (torch.Tensor): RGB Image to be converted to BGR.++ Returns:+ torch.Tensor: BGR version of the image.+ """++ return bgr_to_rgb(image)class BgrToRgb(nn.Module):