π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(image): read a channels-last DLPack capsule in Image.from_dlpack (#6041)
Saksham Sharma95cd35980b27
diff --git a/changelog.d/6041.fixed.md b/changelog.d/6041.fixed.mdnew file mode 100644index 000000000..b57709c4a--- /dev/null+++ b/changelog.d/6041.fixed.md@@ -0,0 +1 @@+`Image.from_dlpack` takes `color_space` and `channels_order` arguments, as `Image.from_numpy` does, so a channels-last `(H, W, C)` capsule is read with the right height, width and channels, and `Image.from_dlpack(img.to_dlpack(), img.pixel_format.color_space, img.channels_order)` round-trips any image. Previously `from_dlpack` always read the capsule as `(C, H, W)` and labelled it by its channel count, so the round trip of a channels-last image, the `from_numpy` default, silently swapped its height, width and channels, and a `BGR` image came back labelled `RGB`. The defaults are unchanged, and `to_dlpack` now documents that the capsule keeps the image's own layout.diff --git a/kornia/image/image.py b/kornia/image/image.pyindex 45445e3fb..53f7856d4 100644--- a/kornia/image/image.py+++ b/kornia/image/image.py@@ -64,8 +64,12 @@ class Image:data: ``bit_depth`` is the storage size, ``8 * data.element_size()``, and the color space must fit thechannel count, 1 for ``GRAY``, 3 for ``RGB`` and ``BGR``, any for ``UNKNOWN``.- :meth:`from_numpy`, :meth:`to_numpy`, :meth:`from_dlpack` and :meth:`to_dlpack` share memory with the- array or capsule of a CPU image. :meth:`to_numpy` returns the stored layout, so an image from- :meth:`from_file` gives :math:`(C, H, W)`.+ array or capsule of a CPU image. :meth:`to_numpy` and :meth:`to_dlpack` return the stored layout, so an+ image from :meth:`from_file` gives :math:`(C, H, W)`. Neither the array nor the capsule carries the layout+ or the color space: :meth:`from_numpy` reads :math:`(H, W, C)` and :meth:`from_dlpack` reads+ :math:`(C, H, W)` unless ``channels_order`` says otherwise, and both label the color space by channel count+ unless ``color_space`` is given. Pass ``img.pixel_format.color_space`` and ``img.channels_order`` to read an+ exported image back.- :meth:`to_gray`, :meth:`to_rgb` and :meth:`to_bgr` return the image itself when it is already in that colorspace, an image with new data when it is in another of the three, and raise ``ValueError`` for``UNKNOWN``. Converting RGB or BGR to gray requires ``uint8`` or a floating dtype supported by@@ -376,14 +380,28 @@ class Image:return self.data.cpu().detach().numpy()@classmethod- def from_dlpack(cls, data: DLPack) -> Image:+ def from_dlpack(+ cls,+ data: DLPack,+ color_space: Optional[ColorSpace] = None,+ channels_order: ChannelsOrder = ChannelsOrder.CHANNELS_FIRST,+ ) -> Image:"""Construct an ``Image`` from a DLPack capsule, sharing its memory.+ A capsule carries neither the layout nor the color space, so read ``img.to_dlpack()`` back with+ ``Image.from_dlpack(capsule, img.pixel_format.color_space, img.channels_order)``.+Args:- data: a DLPack capsule from numpy, tvm or jax. The capsule must hold a ``(C, H, W)`` image; it is- labelled ``GRAY`` for 1 channel, ``RGB`` for 3 and ``UNKNOWN`` otherwise.+ data: a DLPack capsule from numpy, tvm or jax, holding a ``(C, H, W)`` image, or ``(H, W, C)`` with+ ``channels_order=CHANNELS_LAST``.+ color_space: the color space of the image. If ``None``, a 1-channel image is ``GRAY``, a 3-channel+ image is ``RGB`` and any other channel count is ``UNKNOWN``. An explicit color space that does not+ match the channel count raises.+ channels_order: the layout of the capsule.Raises:+ ValueError: if ``channels_order`` is neither ``CHANNELS_LAST`` nor ``CHANNELS_FIRST``.+ ValueCheckError: if an explicit ``color_space`` does not match the channel count (also a ``ValueError``).ShapeError: if the capsule has more than three dimensions. It is also a ``ValueError``.IndexError: if the capsule has fewer than three dimensions.@@ -392,23 +410,40 @@ class Image:>>> img = Image.from_dlpack(x.__dlpack__())>>> img.channels, img.height, img.width(3, 4, 5)+ >>> hwc = Image.from_numpy(np.ones((4, 5, 3), dtype=np.uint8)) # (H, W, C), CHANNELS_LAST+ >>> back = Image.from_dlpack(hwc.to_dlpack(), hwc.pixel_format.color_space, hwc.channels_order)+ >>> back.channels, back.height, back.width+ (3, 4, 5)"""+ # check before consuming the capsule, which can be read only once+ if channels_order not in (ChannelsOrder.CHANNELS_FIRST, ChannelsOrder.CHANNELS_LAST):+ raise ValueError("channels_order must be either `CHANNELS_LAST` or `CHANNELS_FIRST`")_data: torch.Tensor = from_dlpack(data)- pixel_format = PixelFormat(color_space=_default_color_space(_data.shape[0]), bit_depth=_data.element_size() * 8)+ if channels_order == ChannelsOrder.CHANNELS_FIRST:+ image_size = ImageSize(height=_data.shape[1], width=_data.shape[2])+ channels = _data.shape[0]+ else:+ image_size = ImageSize(height=_data.shape[0], width=_data.shape[1])+ channels = _data.shape[2]++ if color_space is None:+ color_space = _default_color_space(channels)+ pixel_format = PixelFormat(color_space=color_space, bit_depth=_data.element_size() * 8)# create the image layout based on the input data- layout = ImageLayout(- image_size=ImageSize(height=_data.shape[1], width=_data.shape[2]),- channels=_data.shape[0],- channels_order=ChannelsOrder.CHANNELS_FIRST,- )+ layout = ImageLayout(image_size=image_size, channels=channels, channels_order=channels_order)return cls(_data, pixel_format, layout)def to_dlpack(self) -> DLPack:- """Return a DLPack capsule from the image torch.Tensor."""+ """Return a DLPack capsule from the image torch.Tensor, sharing its memory.++ The capsule holds the data in the image's own :attr:`channels_order`, ``(C, H, W)`` or ``(H, W, C)``, and+ carries no color space; read it back with+ ``Image.from_dlpack(capsule, img.pixel_format.color_space, img.channels_order)``.+ """return to_dlpack(self.data)@classmethoddiff --git a/tests/image/test_image.py b/tests/image/test_image.pyindex 0f3aad7e8..56677f543 100644--- a/tests/image/test_image.py+++ b/tests/image/test_image.py@@ -261,6 +261,58 @@ class TestImage(BaseTester):multi = Image.from_dlpack(np.zeros((4, 4, 5), dtype=np.uint8).__dlpack__())assert multi.pixel_format.color_space == ColorSpace.UNKNOWN+ @pytest.mark.parametrize(+ ("channels", "color_space"),+ [+ (1, ColorSpace.GRAY),+ (3, ColorSpace.RGB),+ (3, ColorSpace.BGR),+ (3, ColorSpace.UNKNOWN),+ (4, ColorSpace.UNKNOWN),+ ],+ )+ @pytest.mark.parametrize("channels_order", [ChannelsOrder.CHANNELS_LAST, ChannelsOrder.CHANNELS_FIRST])+ def test_dlpack_round_trip_keeps_the_layout_and_color_space_6039(+ self, channels, color_space, channels_order, device, dtype+ ):+ # to_dlpack keeps the image's own layout and carries no color space, so a channels-last image used to come+ # back from from_dlpack with its height, width and channels swapped, and a BGR image came back as RGB.+ chw = torch.arange(channels * 6 * 8, device=device).reshape(channels, 6, 8).to(dtype)+ data = chw.permute(1, 2, 0).contiguous() if channels_order == ChannelsOrder.CHANNELS_LAST else chw+ img = make_image(data, color_space, channels_order)++ back = Image.from_dlpack(img.to_dlpack(), img.pixel_format.color_space, img.channels_order)++ assert (back.height, back.width, back.channels) == (6, 8, channels)+ assert back.layout == img.layout+ assert back.pixel_format == img.pixel_format+ assert back.data.data_ptr() == img.data.data_ptr()+ self.assert_close(back.data, data, rtol=0, atol=0)++ def test_from_dlpack_reads_a_channels_last_capsule_6039(self):+ hwc = np.zeros((6, 8, 3), dtype=np.uint8)+ img = Image.from_dlpack(hwc.__dlpack__(), channels_order=ChannelsOrder.CHANNELS_LAST)+ assert (img.height, img.width, img.channels) == (6, 8, 3)+ assert img.pixel_format.color_space == ColorSpace.RGB+ # The default is unchanged: a (C, H, W) capsule.+ chw = Image.from_dlpack(np.zeros((3, 6, 8), dtype=np.uint8).__dlpack__())+ assert (chw.channels_order, chw.height, chw.width, chw.channels) == (ChannelsOrder.CHANNELS_FIRST, 6, 8, 3)++ def test_from_dlpack_takes_an_explicit_color_space_6039(self):+ bgr = Image.from_dlpack(np.zeros((3, 6, 8), dtype=np.uint8).__dlpack__(), ColorSpace.BGR)+ assert bgr.pixel_format.color_space == ColorSpace.BGR+ with pytest.raises(ValueCheckError, match="color space"):+ Image.from_dlpack(+ np.zeros((6, 8, 4), dtype=np.uint8).__dlpack__(), ColorSpace.RGB, ChannelsOrder.CHANNELS_LAST+ )++ def test_from_dlpack_rejects_an_unknown_channels_order_6039(self):+ capsule = np.zeros((3, 6, 8), dtype=np.uint8).__dlpack__()+ with pytest.raises(ValueError, match="channels_order must be either"):+ Image.from_dlpack(capsule, channels_order="HWC")+ # the order is checked before the capsule, which can be read only once, is consumed+ assert Image.from_dlpack(capsule).channels == 3+@pytest.mark.parametrize(("dtype", "bits", "ok"),[(torch.uint8, 8, True), (torch.uint8, 12, False), (torch.uint16, 16, True), (torch.uint16, 12, False)],