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 in mindspore.dataset.transforms / mindspore.dataset.vision and user-defined Python callable objects to combine as single data augmentation. For user-defined Python callable objects, the return value is required to be 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
>>> import mindspore.dataset as ds >>> import mindspore.dataset.transforms as transforms >>> import mindspore.dataset.vision as vision >>> >>> # create a dataset that reads all files in dataset_dir with 8 threads >>> image_folder_dataset_dir = "/path/to/image_folder_dataset_directory" >>> 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 = image_folder_dataset.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"])