mindspore.dataset.transforms.RandomChoice

View Source On Gitee
class mindspore.dataset.transforms.RandomChoice(transforms)[source]

Randomly select one transform from a list to apply.

Parameters

transforms (list) – List of transforms to be selected from.

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 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.RandomChoice(transforms_list),
...                               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.array([1, 2, 3])
>>> output = transforms.RandomChoice([transforms.Fill(100)])(data)
>>> print(output.shape, output.dtype)
(3,) int64
>>> ds.config.set_seed(seed)