mindspore.dataset.transforms.py_transforms.Compose

class mindspore.dataset.transforms.py_transforms.Compose(transforms)[source]

Compose a list of transforms.

Note

Compose takes a list of transformations either provided in py_transforms or from user-defined implementation; each can be an initialized transformation class or a lambda function, as long as the output from the last transformation is a single tensor of type numpy.ndarray. See below for an example of how to use Compose with py_transforms classes and check out FiveCrop or TenCrop for the use of them in conjunction with lambda functions.

Parameters

transforms (list) – List of transformations to be applied.

Raises
  • TypeError – If transforms is not of type list.

  • ValueError – If transforms is empty.

  • TypeError – If transformations in transforms are not Python callable objects.

Supported Platforms:

CPU

Examples

>>> 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"])
static reduce(operations)[source]

Wraps adjacent Python operations in a Compose to allow mixing of Python and C++ operations.

Parameters

operations (list) – list of tensor operations.

Returns

list, the reduced list of operations.