mindspore.dataset.vision.MixUp

View Source On Gitee
class mindspore.dataset.vision.MixUp(batch_size, alpha, is_single=True)[source]

Randomly mix up a batch of numpy.ndarray images together with its labels.

Each image will be multiplied by a random weight lambda generated from the Beta distribution and then added to another image multiplied by 1lambda. The same transformation will be applied to their labels with the same value of lambda. Make sure that the labels are one-hot encoded in advance.

Parameters
  • batch_size (int) – The number of images in a batch.

  • alpha (float) – The alpha and beta parameter for the Beta distribution.

  • is_single (bool, optional) – If True, it will randomly mix up [img0, …, img(n-1), img(n)] with [img1, …, img(n), img0] in each batch. Otherwise, it will randomly mix up images with the output of the previous batch. Default: True.

Raises
  • TypeError – If batch_size is not of type integer.

  • TypeError – If alpha is not of type float.

  • TypeError – If is_single is not of type boolean.

  • ValueError – If batch_size is not positive.

  • ValueError – If alpha is not positive.

Supported Platforms:

CPU

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>> import mindspore.dataset.transforms as transforms
>>>
>>> # Use the transform in dataset pipeline mode
>>> data = np.random.randint(0, 255, size=(64, 64, 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"])
>>> # ont hot decode the label
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms.OneHot(10), input_columns="label")
>>> # batch the samples
>>> numpy_slices_dataset = numpy_slices_dataset.batch(batch_size=4)
>>> # finally mix up the images and labels
>>> numpy_slices_dataset = numpy_slices_dataset.map(
...     operations=vision.MixUp(batch_size=1, alpha=0.2),
...     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
(4, 64, 64, 3) float64
(4, 3, 10) float64
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> label = np.array([[0, 1]])
>>> output = vision.MixUp(batch_size=2, alpha=0.2, is_single=False)(data, label)
>>> print(output[0].shape, output[0].dtype)
(2, 100, 100, 3) float64
>>> print(output[1].shape, output[1].dtype)
(2, 2) float64
Tutorial Examples: