incerto.ood.CutMix#

class incerto.ood.CutMix(alpha=1.0)[source]#

Bases: object

CutMix augmentation for improved robustness and OOD detection.

Instead of linearly interpolating images (mixup), cuts and pastes patches between images:

x_tilde = M ⊙ x_i + (1-M) ⊙ x_j y_tilde = λ*y_i + (1-λ)*y_j

where M is a binary mask and λ is the area ratio.

Reference:

Yun et al. “CutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features” (ICCV 2019)

Parameters:

alpha (float) – Beta distribution parameter (default: 1.0)

Example

>>> cutmix = CutMix(alpha=1.0)
>>> mixed_x, y_a, y_b, lam = cutmix(x, y)
>>> outputs = model(mixed_x)
>>> loss = lam * criterion(outputs, y_a) + (1-lam) * criterion(outputs, y_b)
__init__(alpha=1.0)[source]#
Parameters:

alpha (float)

Methods

__init__([alpha])

__init__(alpha=1.0)[source]#
Parameters:

alpha (float)

__call__(x, y)[source]#

Apply CutMix augmentation.

Parameters:
  • x (Tensor) – Input batch (N, C, H, W)

  • y (Tensor) – Target labels (N,)

Return type:

Tuple[Tensor, Tensor, Tensor, float]

Returns:

Tuple of (mixed_x, y_a, y_b, lambda)