# CutMix¶

class mmcls.models.utils.batch_augments.CutMix(alpha, cutmix_minmax=None, correct_lam=True)[源代码]

CutMix batch agumentation.

CutMix is a method to improve the network’s generalization capability. It’s proposed in CutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features <https://arxiv.org/abs/1905.04899>

With this method, patches are cut and pasted among training images where the ground truth labels are also mixed proportionally to the area of the patches.

• alpha (float) – Parameters for Beta distribution to generate the mixing ratio. It should be a positive number. More details can be found in Mixup.

• cutmix_minmax (List[float], optional) – The min/max area ratio of the patches. If not None, the bounding-box of patches is uniform sampled within this ratio range, and the alpha will be ignored. Otherwise, the bounding-box is generated according to the alpha. Defaults to None.

• correct_lam (bool) – Whether to apply lambda correction when cutmix bbox clipped by image borders. Defaults to True.

If the cutmix_minmax is None, how to generate the bounding-box of patches according to the alpha?

First, generate a $$\lambda$$, details can be found in Mixup. And then, the area ratio of the bounding-box is calculated by:

$\text{ratio} = \sqrt{1-\lambda}$
cutmix_bbox_and_lam(img_shape, lam, count=None)[源代码]

Generate bbox and apply lambda correction.

• img_shape (tuple) – Image shape as tuple

• lam (float) – Cutmix lambda value

• count (int, optional) – Number of bbox to generate. Defaults to None

mix(batch_inputs, batch_scores)[源代码]

Mix the batch inputs and batch one-hot format ground truth.

• batch_inputs (Tensor) – A batch of images tensor in the shape of (N, C, H, W).

• batch_scores (Tensor) – A batch of one-hot format labels in the shape of (N, num_classes).

The mixed inputs and labels.

Tuple[Tensor, Tensor)

rand_bbox(img_shape, lam, margin=0.0, count=None)[源代码]

Standard CutMix bounding-box that generates a random square bbox based on lambda value. This implementation includes support for enforcing a border margin as percent of bbox dimensions.

• img_shape (tuple) – Image shape as tuple

• lam (float) – Cutmix lambda value

• margin (float) – Percentage of bbox dimension to enforce as margin (reduce amount of box outside image). Defaults to 0.

• count (int, optional) – Number of bbox to generate. Defaults to None

rand_bbox_minmax(img_shape, count=None)[源代码]

Min-Max CutMix bounding-box Inspired by Darknet cutmix implementation. It generates a random rectangular bbox based on min/max percent values applied to each dimension of the input image.

Typical defaults for minmax are usually in the .2-.3 for min and .8-.9 range for max.

• img_shape (tuple) – Image shape as tuple

• count (int, optional) – Number of bbox to generate. Defaults to None