- composer.functional.cutmix_batch(input, target, length=None, alpha=1.0, bbox=None, indices=None, uniform_sampling=False)#
Create new samples using combinations of pairs of samples.
This is done by masking a region of each image in
inputand filling the masked region with the corresponding content from a random different image in``input``.
The position of the masked region is determined by drawing a center point uniformly at random from all spatial positions.
The area of the masked region is computed using either
lengthis provided, it directly determines the size of the masked region. If it is not provided, the fraction of the input area to mask is drawn from a
Beta(alpha, alpha)distribution. The original paper uses a fixed value of
alpha = 1.
Alternatively, one may provide a bounding box to mask directly, in which case
alphais ignored and
lengthmust not be provided.
The same masked region is used for the whole batch.
The masked region is clipped at the spatial boundaries of the inputs. This means that there is no padding required, but the actual region used may be smaller than the nominal size computed using
input (Tensor) – input tensor of shape
(N, C, H, W).
target (Tensor) – target tensor of either shape
(N, num_classes). In the former case, elements of
targetmust be integer class ids in the range
0..num_classes. In the latter case, rows of
targetmay be arbitrary vectors of targets, including, e.g., one-hot encoded class labels, smoothed class labels, or multi-output regression targets.
length (float, optional) – Relative side length of the masked region. If specified,
lengthis interpreted as a fraction of
W, and the resulting box is of size
(length * H, length * W). Default:
alpha (float, optional) – parameter for the Beta distribution over the fraction of the input to mask. Ignored if
lengthis provided. Default:
bbox (tuple, optional) – predetermined
(x1, y1, x2, y2)coordinates of the bounding box. Default:
indices (Tensor, optional) – Permutation of the samples to use. Default:
uniform_sampling (bool, optional) – If
True, sample the bounding box such that each pixel has an equal probability of being mixed. If
False, defaults to the sampling used in the original paper implementation. Default:
input_mixed (torch.Tensor) – batch of inputs after cutmix has been applied.
target_perm (torch.Tensor) – The labels of the mixed-in examples
area (float) – The fractional area of the unmixed region.
bounding_box (tuple) – the
(left, top, right, bottom)coordinates of the bounding box that defines the mixed region.
ValueError – If both
import torch from composer.functional import cutmix_batch N, C, H, W = 2, 3, 4, 5 num_classes = 10 X = torch.randn(N, C, H, W) y = torch.randint(num_classes, size=(N,)) X_mixed, target_perm, area, _ = cutmix_batch(X, y, alpha=0.2)