mindspore.dataset.transforms.RandomApply

class mindspore.dataset.transforms.RandomApply(transforms, prob=0.5)[source]

Randomly perform a series of transforms with a given probability.

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

  • prob (float, optional) – The probability to apply the transformation list. Default: 0.5.

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.

  • TypeError – If prob is not of type float.

  • ValueError – If prob is not in range [0.0, 1.0].

Supported Platforms:

CPU

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.transforms as transforms
>>> import mindspore.dataset.vision as vision
>>> from mindspore.dataset.transforms import Compose
>>>
>>> # Use the transform in dataset pipeline mode
>>> seed = ds.config.get_seed()
>>> ds.config.set_seed(12345)
>>> transforms_list = [vision.RandomHorizontalFlip(0.5),
...                    vision.Normalize((0.491, 0.482, 0.447), (0.247, 0.243, 0.262)),
...                    vision.RandomErasing()]
>>> composed_transform = Compose([transforms.RandomApply(transforms_list, prob=0.6),
...                               vision.ToTensor()])
>>>
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=composed_transform, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(3, 100, 100) float32
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> transform = [vision.HsvToRgb(is_hwc=True), vision.Crop((0, 0), 10), vision.ToTensor()]
>>> output = transforms.RandomApply(transform, prob=1.0)(data)
>>> print(output.shape, output.dtype)
(3, 10, 10) float32
>>> ds.config.set_seed(seed)