mindspore.dataset.vision.ToNumpy

查看源文件
class mindspore.dataset.vision.ToNumpy[源代码]

将输入PIL图像转换为numpy.ndarray图像。

支持平台:

CPU

样例:

>>> 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"])
>>> # Use ToNumpy to explicitly select C++ implementation of subsequent op
>>> transforms_list = Compose([vision.RandomHorizontalFlip(0.5),
...                            vision.ToNumpy(),
...                            vision.Resize((50, 60))])
>>> # 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
(50, 60, 3) uint8
>>>
>>> # Use the transform in eager mode
>>> data = list(np.random.randint(0, 255, size=(32, 32, 3, 3)).astype(np.int32))
>>> output = vision.ToNumpy()(data)
>>> print(type(output), output.shape, output.dtype)
<class 'numpy.ndarray'> (32, 32, 3, 3) int32
教程样例: