mindspore.dataset.vision.MixUpBatch

View Source On Gitee
class mindspore.dataset.vision.MixUpBatch(alpha=1.0)[source]

Apply MixUp transformation on input batch of images and labels. Each image is multiplied by a random weight (lambda) and then added to a randomly selected image from the batch multiplied by (1 - lambda). The same formula is also applied to the one-hot labels.

The lambda is generated based on the specified alpha value. Two coefficients x1, x2 are randomly generated in the range [alpha, 1], and lambda = (x1 / (x1 + x2)).

Note that you need to make labels into one-hot format and batched before calling this operation.

Parameters

alpha (float, optional) – Hyperparameter of beta distribution. The value must be positive. Default: 1.0.

Raises
  • TypeError – If alpha is not of type float.

  • ValueError – If alpha is not positive.

  • RuntimeError – If given tensor shape is not <N, H, W, C> or <N, C, H, W>.

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"])
>>> onehot_op = transforms.OneHot(num_classes=10)
>>> numpy_slices_dataset= numpy_slices_dataset.map(operations=onehot_op,
...                                                input_columns=["label"])
>>> mixup_batch_op = vision.MixUpBatch(alpha=0.9)
>>> numpy_slices_dataset = numpy_slices_dataset.batch(5)
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=mixup_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, 64, 64, 3) uint8
(5, 3, 10) float32
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, (2, 10, 10, 3)).astype(np.uint8)
>>> label = np.array([[0, 1], [1, 0]])
>>> output = vision.MixUpBatch(1)(data, label)
>>> print(output[0].shape, output[0].dtype)
(2, 10, 10, 3) uint8
>>> print(output[1].shape, output[1].dtype)
(2, 2) float32
Tutorial Examples: