mindspore.dataset.vision.ToType

View Source On Gitee
class mindspore.dataset.vision.ToType(data_type)[source]

Cast the input to a given MindSpore data type or NumPy data type.

It is the same as that of mindspore.dataset.transforms.TypeCast .

Note

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

Parameters

data_type (Union[mindspore.dtype, numpy.dtype]) – The desired data type of the output image, such as numpy.float32 .

Raises

TypeError – If data_type is not of type mindspore.dtype or numpy.dtype .

Supported Platforms:

CPU GPU Ascend

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>> import numpy as np
>>> 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"])
>>> transforms_list = Compose([vision.RandomHorizontalFlip(0.5),
...                            vision.ToTensor(),
...                            vision.ToType(np.float32)])
>>> # 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.array([2.71606445312564e-03, 6.3476562564e-03]).astype(np.float64)
>>> output = vision.ToType(np.float32)(data)
>>> print(output, output.dtype)
[0.00271606 0.00634766] float32
Tutorial Examples: