mindspore.dataset.vision.CutMixBatch

View Source On Gitee
class mindspore.dataset.vision.CutMixBatch(image_batch_format, alpha=1.0, prob=1.0)[source]

Apply CutMix transformation on input batch of images and labels. Note that you need to make labels into one-hot format and batched before calling this operation.

Parameters
  • image_batch_format (ImageBatchFormat) – The method of padding. Can be any of [ImageBatchFormat.NHWC, ImageBatchFormat.NCHW].

  • alpha (float, optional) – Hyperparameter of beta distribution, must be larger than 0. Default: 1.0.

  • prob (float, optional) – The probability by which CutMix is applied to each image, which must be in range: [0.0, 1.0]. Default: 1.0.

Raises
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.vision import ImageBatchFormat
>>>
>>> # Use the transform in dataset pipeline mode
>>> data = np.random.randint(0, 255, size=(28, 28, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(
...     operations=lambda img: (data, np.random.randint(0, 5, (3, 1))),
...     input_columns=["image"],
...     output_columns=["image", "label"])
>>> onehot_op = transforms.OneHot(num_classes=10)
>>> numpy_slices_dataset= numpy_slices_dataset.map(operations=onehot_op, input_columns=["label"])
>>> cutmix_batch_op = vision.CutMixBatch(ImageBatchFormat.NHWC, 1.0, 0.5)
>>> numpy_slices_dataset = numpy_slices_dataset.batch(5)
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=cutmix_batch_op,
...                                                 input_columns=["image", "label"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     print(item["label"].shape, item["label"].dtype)
...     break
(5, 28, 28, 3) uint8
(5, 3, 10) float32
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, (3, 3, 10, 10)).astype(np.uint8)
>>> label = np.array([[0, 1], [1, 0], [1, 0]])
>>> output = vision.CutMixBatch(vision.ImageBatchFormat.NCHW, 1.0, 1.0)(data, label)
>>> print(output[0].shape, output[0].dtype)
(3, 3, 10, 10) uint8
>>> print(output[1].shape, output[1].dtype)
(3, 2) float32
Tutorial Examples: