mindspore.dataset.vision.ToTensor

View Source On Gitee
class mindspore.dataset.vision.ToTensor(output_type=np.float32)[source]

Convert the input PIL Image or numpy.ndarray to numpy.ndarray of the desired dtype, rescale the pixel value range from [0, 255] to [0.0, 1.0] and change the shape from <H, W, C> to <C, H, W>.

Parameters

output_type (Union[mindspore.dtype, numpy.dtype], optional) – The desired dtype of the output image. Default: np.float32 .

Raises
Supported Platforms:

CPU

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>> from mindspore.dataset.transforms import Compose
>>>
>>> # 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"])
>>> # create a list of transformations to be applied to the "image" column of each data row
>>> transforms_list = Compose([vision.RandomHorizontalFlip(0.5),
...                            vision.ToTensor()])
>>> # apply the transform to dataset through map function
>>> 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, 100, 100) float32
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> output = vision.ToTensor()(data)
>>> print(output.shape, output.dtype)
(3, 100, 100) float32
Tutorial Examples: