π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
feat(image): add KORNIA_CHECK_IMAGE_LAYOUT for layout validation (#3549)
Yashhaf4419f63d60
diff --git a/kornia/image/base.py b/kornia/image/base.pyindex 8ea91522d..413abfa50 100644--- a/kornia/image/base.py+++ b/kornia/image/base.py@@ -22,6 +22,8 @@ from enum import Enumimport torch+from kornia.core.check import KORNIA_CHECK_SHAPE+@dataclass(frozen=True)class ImageSize:@@ -106,4 +108,34 @@ class ImageLayout:channels_order: ChannelsOrder+def KORNIA_CHECK_IMAGE_LAYOUT(+ x: torch.Tensor,+ layout: ImageLayout,+ msg: str | None = None,+ raises: bool = True,+) -> bool:+ """Check tensor shape matches the expected ImageLayout.++ Args:+ x: tensor to validate.+ layout: expected image layout.+ msg: custom error message.+ raises: if True, raise ShapeError on mismatch.++ Returns:+ True if shape matches, False otherwise (when raises=False).++ """+ if layout.channels_order == ChannelsOrder.CHANNELS_FIRST:+ shape = [str(layout.channels), str(layout.image_size.height), str(layout.image_size.width)]+ elif layout.channels_order == ChannelsOrder.CHANNELS_LAST:+ shape = [str(layout.image_size.height), str(layout.image_size.width), str(layout.channels)]+ else:+ if raises:+ raise NotImplementedError(f"Layout {layout.channels_order} not implemented.")+ return False++ return KORNIA_CHECK_SHAPE(x, shape, msg, raises)++# TODO: define CompressedImagediff --git a/kornia/image/image.py b/kornia/image/image.pyindex c5ceec90c..40b8ba69b 100644--- a/kornia/image/image.py+++ b/kornia/image/image.py@@ -24,8 +24,15 @@ import torchfrom torch.utils.dlpack import from_dlpack, to_dlpackimport kornia.color-from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SHAPE-from kornia.image.base import ChannelsOrder, ColorSpace, ImageLayout, ImageSize, PixelFormat+from kornia.core.check import KORNIA_CHECK+from kornia.image.base import (+ KORNIA_CHECK_IMAGE_LAYOUT,+ ChannelsOrder,+ ColorSpace,+ ImageLayout,+ ImageSize,+ PixelFormat,+)from kornia.image.image_print import image_to_stringfrom kornia.io.io import ImageLoadType, load_image, write_image@@ -84,15 +91,7 @@ class Image:layout: a dataclass containing the image layout information."""- # TODO: move this to a function KORNIA_CHECK_IMAGE_LAYOUT- if layout.channels_order == ChannelsOrder.CHANNELS_FIRST:- shape = [str(layout.channels), str(layout.image_size.height), str(layout.image_size.width)]- elif layout.channels_order == ChannelsOrder.CHANNELS_LAST:- shape = [str(layout.image_size.height), str(layout.image_size.width), str(layout.channels)]- else:- raise NotImplementedError(f"Layout {layout.channels_order} not implemented.")-- KORNIA_CHECK_SHAPE(data, shape)+ KORNIA_CHECK_IMAGE_LAYOUT(data, layout)KORNIA_CHECK(data.element_size() == pixel_format.bit_depth // 8, "Invalid bit depth.")self._data = datadiff --git a/tests/image/test_image.py b/tests/image/test_image.pyindex 7e5123b78..375c6cde1 100644--- a/tests/image/test_image.py+++ b/tests/image/test_image.py@@ -22,7 +22,8 @@ import pytestimport torchfrom kornia.core._compat import torch_version_le-from kornia.image.base import ChannelsOrder, ColorSpace, ImageLayout, ImageSize, PixelFormat+from kornia.core.exceptions import ShapeError+from kornia.image.base import KORNIA_CHECK_IMAGE_LAYOUT, ChannelsOrder, ColorSpace, ImageLayout, ImageSize, PixelFormatfrom kornia.image.image import Imagefrom testing.base import BaseTester@@ -203,3 +204,26 @@ 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)+++class TestCheckImageLayout(BaseTester):+ def test_channels_first_valid(self, device):+ data = torch.rand(3, 4, 5, device=device)+ layout = ImageLayout(ImageSize(4, 5), 3, ChannelsOrder.CHANNELS_FIRST)+ assert KORNIA_CHECK_IMAGE_LAYOUT(data, layout)++ def test_channels_last_valid(self, device):+ data = torch.rand(4, 5, 3, device=device)+ layout = ImageLayout(ImageSize(4, 5), 3, ChannelsOrder.CHANNELS_LAST)+ assert KORNIA_CHECK_IMAGE_LAYOUT(data, layout)++ def test_invalid_shape_raises(self, device):+ data = torch.rand(3, 4, 5, device=device)+ layout = ImageLayout(ImageSize(10, 10), 3, ChannelsOrder.CHANNELS_FIRST)+ with pytest.raises(ShapeError):+ KORNIA_CHECK_IMAGE_LAYOUT(data, layout)++ def test_invalid_shape_no_raise(self, device):+ data = torch.rand(3, 4, 5, device=device)+ layout = ImageLayout(ImageSize(10, 10), 3, ChannelsOrder.CHANNELS_FIRST)+ assert not KORNIA_CHECK_IMAGE_LAYOUT(data, layout, raises=False)