mindspore.dataset.transforms.Compose

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

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

Note

Compose可以将 mindspore.dataset.transforms / mindspore.dataset.vision 等模块中的数据增强算子以及用户自定义的Python可调用对象 合并成单个数据增强。对于用户定义的Python可调用对象,要求其返回值是numpy.ndarray类型。

参数:

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

异常:

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

  • ValueError - 参数 transforms 是空的list。

  • TypeError - 参数 transforms 的元素不是Python的可调用对象或audio/text/transforms/vision模块中的数据增强方法。

支持平台:

CPU

样例:

>>> compose = transforms.Compose([vision.Decode(), vision.RandomCrop(512)])
>>> image_folder_dataset = image_folder_dataset.map(operations=compose)
>>> 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 = transforms.Compose([vision.Decode(to_pil=True),
...                                vision.RandomHorizontalFlip(0.5),
...                                vision.ToTensor(),
...                                vision.Normalize((0.491, 0.482, 0.447), (0.247, 0.243, 0.262), is_hwc=False),
...                                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 = [vision.Decode(to_pil=True),
...                    vision.RandomHorizontalFlip(0.5),
...                    vision.ToTensor(),
...                    vision.Normalize((0.491, 0.482, 0.447), (0.247, 0.243, 0.262), is_hwc=False),
...                    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 = [transforms.OneHot(2),
...                     transforms.Mask(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=[vision.Decode(),
...          vision.Resize((224, 244)),
...          vision.ToPIL(),
...          np.array, # need to convert PIL image to a NumPy array to pass it to C++ operation
...          vision.Resize((24, 24))]
>>> image_folder_dataset = image_folder_dataset.map(operations=op_list,  input_columns=["image"])
decompose(operations)[源代码]

从给定的操作列表中删除所有 compose 操作。

参数:

  • operations (list) - 变换列表。

返回:

没有组合操作的操作列表。

reduce(operations)[源代码]

在 Compose 中包装相邻的 Python 操作,以允许混合 Python 和 C++ 操作。

参数:

  • operations (list) - Tensor操作列表。

返回:

list,简化的操作列表。