mindspore.dataset.vision.ToTensor

查看源文件
class mindspore.dataset.vision.ToTensor(output_type=np.float32)[源代码]

将输入PIL图像或numpy.ndarray图像转换为指定类型的numpy.ndarray图像,图像的像素值范围将从[0, 255]放缩为[0.0, 1.0],shape将从<H, W, C>调整为<C, H, W>。

参数:
  • output_type (Union[mindspore.dtype, numpy.dtype],可选) - 输出图像的数据类型。默认值: np.float32

异常:
  • TypeError - 当输入图像的类型不为 PIL.Image.Imagenumpy.ndarray

  • TypeError - 输入图像的维度不为2或3。

支持平台:

CPU

样例:

>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>> from mindspore.dataset.transforms import Compose
>>>
>>> # create a list of transformations to be applied to the "image" column of each data row
>>> transforms_list = Compose([vision.Decode(to_pil=True),
...                            vision.RandomHorizontalFlip(0.5),
...                            vision.ToTensor()])
>>> # apply the transform to dataset through map function
>>> image_folder_dataset = ds.ImageFolderDataset("/path/to/image_folder_dataset_directory")
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns="image")
教程样例: