π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
tests(image): migrate test_image to BaseTester (#3407)
Keshav Singh29f826175310
diff --git a/tests/image/test_image.py b/tests/image/test_image.pyindex 1026330c6..4ea324fe7 100644--- a/tests/image/test_image.py+++ b/tests/image/test_image.py@@ -25,10 +25,10 @@ from kornia.image.base import ChannelsOrder, ColorSpace, ImageLayout, ImageSize,from kornia.image.image import Imagefrom kornia.utils._compat import torch_version_le-from testing.base import assert_close+from testing.base import BaseTester-class TestImage:+class TestImage(BaseTester):def test_smoke(self, device):data = torch.randint(0, 255, (3, 4, 5), device=device, dtype=torch.uint8)pixel_format = PixelFormat(color_space=ColorSpace.RGB, bit_depth=8)@@ -60,7 +60,8 @@ class TestImage:assert img.shape == (4, 5, 3)assert img.device == deviceassert img.dtype == torch.uint8- assert_close(data, img.to_numpy())+ np_img = np.asarray(img.to_numpy())+ np.testing.assert_array_equal(data, np_img)# check cloneimg2 = img.clone()@@ -78,7 +79,99 @@ class TestImage:pixel_format = PixelFormat(color_space=ColorSpace.RGB, bit_depth=data.element_size() * 8)layout = ImageLayout(image_size=ImageSize(4, 5), channels=3, channels_order=ChannelsOrder.CHANNELS_FIRST)img = Image(data, pixel_format=pixel_format, layout=layout)- assert_close(data, Image.from_dlpack(img.to_dlpack()).data)+ self.assert_close(data, Image.from_dlpack(img.to_dlpack()).data)++ # Channel first+ def test_rgb_gray_rgb_channels_first(self):+ rgb_val = torch.tensor([0.5, 0.2, 0.1], dtype=torch.float32)+ rgb_data = rgb_val.view(3, 1, 1)++ img_rgb = make_image(rgb_data, ColorSpace.RGB, ChannelsOrder.CHANNELS_FIRST)++ gray = img_rgb.to_gray()+ rgb_back = gray.to_rgb()++ expected_gray = img_rgb.to_gray().data.squeeze()+ self.assert_close(gray.data.squeeze(), expected_gray)++ # RGB reconstructed from gray should repeat luminance across channels+ expected_rgb = expected_gray.repeat(3)+ self.assert_close(rgb_back.data.squeeze(), expected_rgb)++ def test_bgr_gray_bgr_channels_first(self):+ rgb_val = torch.tensor([0.5, 0.2, 0.1], dtype=torch.float32)+ bgr_val = rgb_val.flip(0)+ bgr_data = bgr_val.view(3, 1, 1)++ img_bgr = make_image(bgr_data, ColorSpace.BGR, ChannelsOrder.CHANNELS_FIRST)++ gray = img_bgr.to_gray()+ bgr_back = gray.to_bgr()++ expected_gray = img_bgr.to_gray().data.squeeze()+ self.assert_close(gray.data.squeeze(), expected_gray)++ expected_bgr = expected_gray.repeat(3).flip(0)+ self.assert_close(bgr_back.data.squeeze(), expected_bgr)++ def test_rgb_bgr_swap_channels_first(self):+ rgb_val = torch.tensor([0.5, 0.2, 0.1], dtype=torch.float32)+ bgr_val = rgb_val.flip(0)++ rgb_data = rgb_val.view(3, 1, 1)+ bgr_data = bgr_val.view(3, 1, 1)++ img_rgb = make_image(rgb_data, ColorSpace.RGB, ChannelsOrder.CHANNELS_FIRST)+ img_bgr = make_image(bgr_data, ColorSpace.BGR, ChannelsOrder.CHANNELS_FIRST)++ self.assert_close(img_rgb.to_bgr().data.squeeze(), bgr_val)+ self.assert_close(img_bgr.to_rgb().data.squeeze(), rgb_val)++ # Channel last+ def test_rgb_gray_rgb_channels_last(self):+ rgb_val = torch.tensor([0.5, 0.2, 0.1], dtype=torch.float32)+ rgb_data = rgb_val.view(1, 1, 3)++ img_rgb = make_image(rgb_data, ColorSpace.RGB, ChannelsOrder.CHANNELS_LAST)++ gray = img_rgb.to_gray()+ rgb_back = gray.to_rgb()++ expected_gray = img_rgb.to_gray().data.squeeze()+ self.assert_close(gray.data.squeeze(), expected_gray)++ # RGB reconstructed from gray should repeat luminance across channels+ expected_rgb = expected_gray.repeat(3)+ self.assert_close(rgb_back.data.squeeze(), expected_rgb)++ def test_bgr_gray_bgr_channels_last(self):+ rgb_val = torch.tensor([0.5, 0.2, 0.1], dtype=torch.float32)+ bgr_val = rgb_val.flip(0)+ bgr_data = bgr_val.view(1, 1, 3)++ img_bgr = make_image(bgr_data, ColorSpace.BGR, ChannelsOrder.CHANNELS_LAST)++ gray = img_bgr.to_gray()+ bgr_back = gray.to_bgr()++ expected_gray = img_bgr.to_gray().data.squeeze()+ self.assert_close(gray.data.squeeze(), expected_gray)++ expected_bgr = expected_gray.repeat(3).flip(0)+ self.assert_close(bgr_back.data.squeeze(), expected_bgr)++ def test_rgb_bgr_swap_channels_last(self):+ rgb_val = torch.tensor([0.5, 0.2, 0.1], dtype=torch.float32)+ bgr_val = rgb_val.flip(0)++ rgb_data = rgb_val.view(1, 1, 3)+ bgr_data = bgr_val.view(1, 1, 3)++ img_rgb = make_image(rgb_data, ColorSpace.RGB, ChannelsOrder.CHANNELS_LAST)+ img_bgr = make_image(bgr_data, ColorSpace.BGR, ChannelsOrder.CHANNELS_LAST)++ self.assert_close(img_rgb.to_bgr().data.squeeze(), bgr_val)+ self.assert_close(img_bgr.to_rgb().data.squeeze(), rgb_val)@pytest.mark.skipif(torch_version_le(1, 9, 1), reason="dlpack is broken in torch<=1.9.1")@pytest.mark.xfail(reason="This may fail some time due to jpeg compression assertion")@@ -110,39 +203,3 @@ def make_image(data: torch.Tensor, cs: ColorSpace, order: ChannelsOrder) -> Imagpf = PixelFormat(color_space=cs, bit_depth=data.element_size() * 8)layout = ImageLayout(image_size=ImageSize(H, W), channels=C, channels_order=order)return Image(data.clone(), pf, layout)--[email protected]("order", [ChannelsOrder.CHANNELS_FIRST, ChannelsOrder.CHANNELS_LAST])-def test_color_space_conversions(order):- rgb_val = torch.tensor([0.5, 0.2, 0.1], dtype=torch.float32)- bgr_val = rgb_val.flip(0)-- if order == ChannelsOrder.CHANNELS_FIRST:- rgb_data = rgb_val.view(3, 1, 1)- bgr_data = bgr_val.view(3, 1, 1)- else:- rgb_data = rgb_val.view(1, 1, 3)- bgr_data = bgr_val.view(1, 1, 3)-- # Create images- img_rgb = make_image(rgb_data, ColorSpace.RGB, order)- img_bgr = make_image(bgr_data, ColorSpace.BGR, order)-- # 1) RGB -> Gray -> RGB- gray = img_rgb.to_gray()- lum = 0.299 * rgb_val[0] + 0.587 * rgb_val[1] + 0.114 * rgb_val[2]- assert pytest.approx(gray.data.squeeze().item(), rel=1e-3) == lum- rgb_back = gray.to_rgb().data.squeeze()- expected = torch.tensor([lum, lum, lum])- assert torch.allclose(rgb_back, expected)-- # 2) BGR -> Gray -> BGR- gray2 = img_bgr.to_gray()- assert pytest.approx(gray2.data.squeeze().item(), rel=1e-3) == lum- bgr_back = gray2.to_bgr().data.squeeze()- expected_bgr = expected.flip(0)- assert torch.allclose(bgr_back, expected_bgr)-- # 3) RGB <-> BGR swap- assert torch.allclose(img_rgb.to_bgr().data.squeeze(), bgr_val)- assert torch.allclose(img_bgr.to_rgb().data.squeeze(), rgb_val)