π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Add missing fixes, tests and example use for subsampled yuv (#1367)
Oskar Flordal0ea50860380d
diff --git a/kornia/color/yuv.py b/kornia/color/yuv.pyindex c1cdc5845..905a2057d 100644--- a/kornia/color/yuv.py+++ b/kornia/color/yuv.py@@ -50,8 +50,8 @@ def rgb_to_yuv420(image: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:image: RGB Image to be converted to YUV with shape :math:`(*, 3, H, W)`.Returns:- Tensor containing the Y plane with shape :math:`(*, 1, H, W)- Tensor containing the UV planes with shape :math:`(*, 2, H/2, W/2)+ A Tensor containing the Y plane with shape :math:`(*, 1, H, W)`+ A Tensor containing the UV planes with shape :math:`(*, 2, H/2, W/2)`Example:>>> input = torch.rand(2, 3, 4, 6)@@ -67,9 +67,8 @@ def rgb_to_yuv420(image: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:raise ValueError(f"Input H&W must be evenly disible by 2. Got {image.shape}")yuvimage = rgb_to_yuv(image)- yuvchunks = torch.chunk(yuvimage, 3, dim=-3)- return (yuvchunks[0], torch.nn.functional.avg_pool2d(torch.cat(yuvchunks[1:3], dim=-3), (2, 2)))+ return (yuvimage[..., :1, :, :], torch.nn.functional.avg_pool2d(yuvimage[..., 1:3, :, :], (2, 2)))def rgb_to_yuv422(image: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:@@ -82,8 +81,8 @@ def rgb_to_yuv422(image: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:image: RGB Image to be converted to YUV with shape :math:`(*, 3, H, W)`.Returns:- Tensor containing the Y plane with shape :math:`(*, 1, H, W)- Tensor containing the UV planes with shape :math:`(*, 2, H, W/2)+ A Tensor containing the Y plane with shape :math:`(*, 1, H, W)`+ A Tensor containing the UV planes with shape :math:`(*, 2, H, W/2)`Example:>>> input = torch.rand(2, 3, 4, 6)@@ -100,10 +99,7 @@ def rgb_to_yuv422(image: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:yuvimage = rgb_to_yuv(image)- # use chunk to handle 3 and 4 dimensions- yuvchunks = torch.chunk(yuvimage, 3, dim=-3)-- return (yuvchunks[0], torch.nn.functional.avg_pool2d(torch.cat(yuvchunks[1:3], dim=-3), (1, 2)))+ return (yuvimage[..., :1, :, :], torch.nn.functional.avg_pool2d(yuvimage[..., 1:3, :, :], (1, 2)))def yuv_to_rgb(image: torch.Tensor) -> torch.Tensor:@@ -149,7 +145,7 @@ def yuv420_to_rgb(imagey: torch.Tensor, imageuv: torch.Tensor) -> torch.Tensor:Args:imagey: Y (luma) Image plane to be converted to RGB with shape :math:`(*, 1, H, W)`.- imageuv: UV (luma) Image planes to be converted to RGB with shape :math:`(*, 2, H/2, W/2)`.+ imageuv: UV (chroma) Image planes to be converted to RGB with shape :math:`(*, 2, H/2, W/2)`.Returns:RGB version of the image with shape :math:`(*, 3, H, W)`.diff --git a/test/color/test_yuv.py b/test/color/test_yuv.pyindex 9a599db23..8a51180d8 100644--- a/test/color/test_yuv.py+++ b/test/color/test_yuv.py@@ -107,9 +107,62 @@ class TestRgbToYuv420(BaseTester):img = torch.ones(3, 1, 2, device=device, dtype=dtype)assert kornia.color.rgb_to_yuv420(img)- # TODO: investigate and implement me- # def test_unit(self, device, dtype):- # pass+ # Test max/min values. This is essentially testing the transform rather than the subsampling+ # ref values manually checked vs rec 601+ def test_unit_white(self, device, dtype): # skipcq: PYL-R0201+ rgb = torch.tensor([[[255, 255], [255, 255]], [[255, 255], [255, 255]], [[255, 255], [255, 255]]],+ device=device, dtype=torch.uint8).type(dtype) / 255.0+ refy = torch.tensor([[[255, 255], [255, 255]]], device=device, dtype=torch.uint8)+ refuv = torch.tensor([[[0]], [[0]]], device=device, dtype=torch.int8)++ resy = (kornia.color.rgb_to_yuv420(rgb)[0] * 255.0).round().type(torch.uint8)+ resuv = (kornia.color.rgb_to_yuv420(rgb)[1] * 255.0).round().clamp(-128, 127).type(torch.int8)+ assert_close(refy, resy)+ assert_close(refuv, resuv)++ def test_unit_black(self, device, dtype): # skipcq: PYL-R0201+ rgb = torch.tensor([[[0, 0], [0, 0]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]],+ device=device, dtype=torch.uint8).type(dtype) / 255.0+ refy = torch.tensor([[[0, 0], [0, 0]]], device=device, dtype=torch.uint8)+ refuv = torch.tensor([[[0]], [[0]]], device=device, dtype=torch.int8)++ resy = (kornia.color.rgb_to_yuv420(rgb)[0] * 255.0).round().type(torch.uint8)+ resuv = (kornia.color.rgb_to_yuv420(rgb)[1] * 255.0).round().clamp(-128, 127).type(torch.int8)+ assert_close(refy, resy)+ assert_close(refuv, resuv)++ def test_unit_gray(self, device, dtype): # skipcq: PYL-R0201+ rgb = torch.tensor([[[127, 127], [127, 127]], [[127, 127], [127, 127]], [[127, 127], [127, 127]]],+ device=device, dtype=torch.uint8).type(dtype) / 255.0+ refy = torch.tensor([[[127, 127], [127, 127]]], device=device, dtype=torch.uint8)+ refuv = torch.tensor([[[0]], [[0]]], device=device, dtype=torch.int8)++ resy = (kornia.color.rgb_to_yuv420(rgb)[0] * 255.0).round().type(torch.uint8)+ resuv = (kornia.color.rgb_to_yuv420(rgb)[1] * 255.0).round().clamp(-128, 127).type(torch.int8)+ assert_close(refy, resy)+ assert_close(refuv, resuv)++ def test_unit_red(self, device, dtype): # skipcq: PYL-R0201+ rgb = torch.tensor([[[255, 255], [255, 255]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]],+ device=device, dtype=torch.uint8).type(dtype) / 255.0+ refy = torch.tensor([[[76, 76], [76, 76]]], device=device, dtype=torch.uint8)+ refuv = torch.tensor([[[-37]], [[127]]], device=device, dtype=torch.int8)++ resy = (kornia.color.rgb_to_yuv420(rgb)[0] * 255.0).round().type(torch.uint8)+ resuv = (kornia.color.rgb_to_yuv420(rgb)[1] * 255.0).round().clamp(-128, 127).type(torch.int8)+ assert_close(refy, resy)+ assert_close(refuv, resuv)++ def test_unit_blue(self, device, dtype): # skipcq: PYL-R0201+ rgb = torch.tensor([[[0, 0], [0, 0]], [[0, 0], [0, 0]], [[255, 255], [255, 255]]],+ device=device, dtype=torch.uint8).type(dtype) / 255.0+ refy = torch.tensor([[[29, 29], [29, 29]]], device=device, dtype=torch.uint8)+ refuv = torch.tensor([[[111]], [[-25]]], device=device, dtype=torch.int8)++ resy = (kornia.color.rgb_to_yuv420(rgb)[0] * 255.0).type(torch.uint8)+ resuv = (kornia.color.rgb_to_yuv420(rgb)[1] * 255.0).clamp(-128, 127).type(torch.int8)+ assert_close(refy, resy)+ assert_close(refuv, resuv)# This measures accuracy, given the impact of the subsampling we will avoid the issue by# repeating a 2x2 pattern for which mean will match what we get from upscaling again@@ -182,10 +235,6 @@ class TestRgbToYuv422(BaseTester):img = torch.ones(3, 2, 1, device=device, dtype=dtype)assert kornia.color.rgb_to_yuv422(img)- # TODO: investigate and implement me- # def test_unit(self, device, dtype):- # pass-# This measures accuracy, given the impact of the subsampling we will avoid the issue by# repeating a 2x2 pattern for which mena will match what we get from upscaling againdef test_forth_and_back(self, device, dtype): # skipcq: PYL-R0201@@ -327,9 +376,25 @@ class TestYuv420ToRgb(BaseTester):imguv = torch.ones(3, 0, 1, device=device, dtype=dtype)assert kornia.color.yuv420_to_rgb(imgy, imguv)- # TODO: investigate and implement me- # def test_unit(self, device, dtype):- # pass+ # Test max/min values. This is essentially testing the transform rather than the subsampling+ # ref values manually checked vs rec 601+ def test_unit_white(self, device, dtype): # skipcq: PYL-R0201+ refrgb = torch.tensor([[[255, 255], [255, 255]], [[255, 255], [255, 255]], [[255, 255], [255, 255]]],+ device=device, dtype=torch.uint8)+ y = torch.tensor([[[255, 255], [255, 255]]], device=device, dtype=torch.uint8).type(dtype) / 255.0+ uv = torch.tensor([[[0]], [[0]]], device=device, dtype=torch.int8).type(torch.float) / 255.0++ resrgb = (kornia.color.yuv420_to_rgb(y, uv) * 255.0).round().type(torch.uint8)+ assert_close(refrgb, resrgb)++ def test_unit_red(self, device, dtype): # skipcq: PYL-R0201+ refrgb = torch.tensor([[[221, 221], [221, 221]], [[17, 17], [17, 17]], [[1, 1], [1, 1]]],+ device=device, dtype=torch.uint8)+ y = torch.tensor([[[76, 76], [76, 76]]], device=device, dtype=torch.uint8).type(dtype) / 255.0+ uv = torch.tensor([[[-37]], [[127]]], device=device, dtype=torch.int8).type(torch.float) / 255.0++ resrgb = (kornia.color.yuv420_to_rgb(y, uv) * 255.0).round().type(torch.uint8)+ assert_close(refrgb, resrgb)# TODO: improve accuracydef test_forth_and_back(self, device, dtype): # skipcq: PYL-R0201