mindspore.dataset.transforms.py_transforms.Compose

class mindspore.dataset.transforms.py_transforms.Compose(transforms)[源代码]

将多个数据增强算子组合使用。

Note

Compose可以将 mindspore.dataset.transforms.py_transforms 模块中的数据增强算子以及用户自定义的Python可调用对象 合并成单个数据增强。对于用户定义的Python可调用对象,要求其返回值是numpy.ndarray类型。有关如何使用,请参阅Compose的示例,或阅读 mindspore.dataset.vision.py_transforms.FiveCrop 的示例,学习如何与用户自定义Python可调用对象配合使用。

参数:

  • transforms (list) - 一个数据增强的列表。

异常:

  • TypeError - 参数 transforms 类型不为list。

  • ValueError - 参数 transforms 是空的list。

  • TypeError - 参数 transforms 的元素不是Python的可调用对象。

支持平台:

CPU

样例:

>>> image_folder_dataset_dir = "/path/to/image_folder_dataset_directory"
>>> # create a dataset that reads all files in dataset_dir with 8 threads
>>> image_folder_dataset = ds.ImageFolderDataset(image_folder_dataset_dir, num_parallel_workers=8)
>>> # create a list of transformations to be applied to the image data
>>> transform = py_transforms.Compose([py_vision.Decode(),
...                                    py_vision.RandomHorizontalFlip(0.5),
...                                    py_vision.ToTensor(),
...                                    py_vision.Normalize((0.491, 0.482, 0.447), (0.247, 0.243, 0.262)),
...                                    py_vision.RandomErasing()])
>>> # apply the transform to the dataset through dataset.map function
>>> image_folder_dataset = image_folder_dataset.map(operations=transform, input_columns=["image"])
>>>
>>> # Compose is also be invoked implicitly, by just passing in a list of ops
>>> # the above example then becomes:
>>> transforms_list = [py_vision.Decode(),
...                    py_vision.RandomHorizontalFlip(0.5),
...                    py_vision.ToTensor(),
...                    py_vision.Normalize((0.491, 0.482, 0.447), (0.247, 0.243, 0.262)),
...                    py_vision.RandomErasing()]
>>>
>>> # apply the transform to the dataset through dataset.map()
>>> image_folder_dataset_1 = image_folder_dataset_1.map(operations=transforms_list, input_columns=["image"])
>>>
>>> # Certain C++ and Python ops can be combined, but not all of them
>>> # An example of combined operations
>>> arr = [0, 1]
>>> dataset = ds.NumpySlicesDataset(arr, column_names=["cols"], shuffle=False)
>>> transformed_list = [py_transforms.OneHotOp(2), c_transforms.Mask(c_transforms.Relational.EQ, 1)]
>>> dataset = dataset.map(operations=transformed_list, input_columns=["cols"])
>>>
>>> # Here is an example of mixing vision ops
>>> import numpy as np
>>> op_list=[c_vision.Decode(),
...          c_vision.Resize((224, 244)),
...          py_vision.ToPIL(),
...          np.array, # need to convert PIL image to a NumPy array to pass it to C++ operation
...          c_vision.Resize((24, 24))]
>>> image_folder_dataset = image_folder_dataset.map(operations=op_list,  input_columns=["image"])
reduce(operations)[源代码]

使用Compose将指定数据增强操作列表中相邻的Python操作组合,以允许混用Python和C++操作。

参数:

  • operations (list) - 数据增强的列表。

返回:

list,组合后的数据增强操作列表。