mindspore.dataset.transforms.Compose
- class mindspore.dataset.transforms.Compose(transforms)[source]
Compose a list of transforms into a single transform.
Note
Compose takes a list of transformations either provided in transforms.py 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.
- 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 elements of transforms are neither Python callable objects nor data processing operations in transforms.py.
- Supported Platforms:
CPU
Examples
>>> 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"])
- static decompose(operations)[source]
Remove all compose operation from the given list of operations.
- Parameters
operations – list of transforms
- Returns
list of operations without compose operations.