class mindspore.dataset.vision.py_transforms.ToTensor(output_type=np.float32)[source]

Convert the input PIL Image or numpy.ndarray of shape (H, W, C) in the range [0, 255] to numpy.ndarray of shape (C, H, W) in the range [0.0, 1.0] with the desired dtype.


The values in the input image will be rescaled from [0, 255] to [0.0, 1.0]. The dtype will be cast to output_type. The number of channels remains the same.


output_type (numpy.dtype, optional) – The dtype of the numpy.ndarray output (default=np.float32).

  • TypeError – If the input is not PIL Image or numpy.ndarray.

  • TypeError – If the dimension of input is not 2 or 3.

Supported Platforms:



>>> from mindspore.dataset.transforms.py_transforms import Compose
>>> # create a list of transformations to be applied to the "image" column of each data row
>>> transforms_list = Compose([py_vision.Decode(),
...                            py_vision.RandomHorizontalFlip(0.5),
...                            py_vision.ToTensor()])
>>> # apply the transform to dataset through map function
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns="image")