mindspore.dataset.vision.HWC2CHW

View Source On AtomGit
class mindspore.dataset.vision.HWC2CHW[source]

Transposes the input image from shape <H, W, C> to <C, H, W>. If the input image is of shape <H, W>, it will remain unchanged.

Note

This operation is executed on the CPU by default, but it can also be executed on the GPU or Ascend via heterogeneous acceleration.

Raises

RuntimeError – If shape of the input image is not <H, W> or <H, W, C>.

Supported Platforms:

CPU GPU Ascend

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>>
>>> # Use the transform in dataset pipeline mode
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> transforms_list = [vision.RandomHorizontalFlip(0.75),
...                    vision.RandomCrop(64),
...                    vision.HWC2CHW()]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(3, 64, 64) uint8
>>>
>>> # Use the transform in eager mode
>>> data = np.array([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]], dtype=np.uint8).reshape((2, 2, 3))
>>> output = vision.HWC2CHW()(data)
>>> print(output.shape, output.dtype)
(3, 2, 2) uint8
Tutorial Examples: