mindspore.dataset.transforms.RandomChoice

查看源文件
class mindspore.dataset.transforms.RandomChoice(transforms)[源代码]

从一组数据增强变换中随机选择一个进行应用。

参数:
  • transforms (list) - 可供选择的数据增强变换列表。

异常:
  • TypeError - 参数 transforms 类型不为list。

  • ValueError - 参数 transforms 为空。

  • TypeError - 参数 transforms 的元素不是Python可调用对象或audio/text/transforms/vision模块中的数据处理操作。

支持平台:

CPU

样例:

>>> 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)