|
| 1 | +import math |
| 2 | +import torch |
| 3 | + |
| 4 | +from typing import Tuple |
| 5 | +from torch import Tensor |
| 6 | +from torchvision.transforms import functional as F |
| 7 | + |
| 8 | + |
| 9 | +class RandomMixup(torch.nn.Module): |
| 10 | + """Randomly apply Mixup to the provided batch and targets. |
| 11 | + The class implements the data augmentations as described in the paper |
| 12 | + `"mixup: Beyond Empirical Risk Minimization" <https://arxiv.org/abs/1710.09412>`_. |
| 13 | +
|
| 14 | + Args: |
| 15 | + num_classes (int): number of classes used for one-hot encoding. |
| 16 | + p (float): probability of the batch being transformed. Default value is 0.5. |
| 17 | + alpha (float): hyperparameter of the Beta distribution used for mixup. |
| 18 | + Default value is 1.0. |
| 19 | + inplace (bool): boolean to make this transform inplace. Default set to False. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, num_classes: int, |
| 23 | + p: float = 0.5, alpha: float = 1.0, |
| 24 | + inplace: bool = False) -> None: |
| 25 | + super().__init__() |
| 26 | + assert num_classes > 0, "Please provide a valid positive value for the num_classes." |
| 27 | + assert alpha > 0, "Alpha param can't be zero." |
| 28 | + |
| 29 | + self.num_classes = num_classes |
| 30 | + self.p = p |
| 31 | + self.alpha = alpha |
| 32 | + self.inplace = inplace |
| 33 | + |
| 34 | + def forward(self, batch: Tensor, target: Tensor) -> Tuple[Tensor, Tensor]: |
| 35 | + """ |
| 36 | + Args: |
| 37 | + batch (Tensor): Float tensor of size (B, C, H, W) |
| 38 | + target (Tensor): Integer tensor of size (B, ) |
| 39 | +
|
| 40 | + Returns: |
| 41 | + Tensor: Randomly transformed batch. |
| 42 | + """ |
| 43 | + if batch.ndim != 4: |
| 44 | + raise ValueError("Batch ndim should be 4. Got {}".format(batch.ndim)) |
| 45 | + elif target.ndim != 1: |
| 46 | + raise ValueError("Target ndim should be 1. Got {}".format(target.ndim)) |
| 47 | + elif not batch.is_floating_point(): |
| 48 | + raise TypeError('Batch dtype should be a float tensor. Got {}.'.format(batch.dtype)) |
| 49 | + elif target.dtype != torch.int64: |
| 50 | + raise TypeError("Target dtype should be torch.int64. Got {}".format(target.dtype)) |
| 51 | + |
| 52 | + if not self.inplace: |
| 53 | + batch = batch.clone() |
| 54 | + target = target.clone() |
| 55 | + |
| 56 | + if target.ndim == 1: |
| 57 | + target = torch.nn.functional.one_hot(target, num_classes=self.num_classes).to(dtype=torch.float32) |
| 58 | + |
| 59 | + if torch.rand(1).item() >= self.p: |
| 60 | + return batch, target |
| 61 | + |
| 62 | + # It's faster to roll the batch by one instead of shuffling it to create image pairs |
| 63 | + batch_rolled = batch.roll(1, 0) |
| 64 | + target_rolled = target.roll(1) |
| 65 | + |
| 66 | + # Implemented as on mixup paper, page 3. |
| 67 | + lambda_param = float(torch._sample_dirichlet(torch.tensor([self.alpha, self.alpha]))[0]) |
| 68 | + batch_rolled.mul_(1.0 - lambda_param) |
| 69 | + batch.mul_(lambda_param).add_(batch_rolled) |
| 70 | + |
| 71 | + target_rolled.mul_(1.0 - lambda_param) |
| 72 | + target.mul_(lambda_param).add_(target_rolled) |
| 73 | + |
| 74 | + return batch, target |
| 75 | + |
| 76 | + def __repr__(self) -> str: |
| 77 | + s = self.__class__.__name__ + '(' |
| 78 | + s += 'num_classes={num_classes}' |
| 79 | + s += ', p={p}' |
| 80 | + s += ', alpha={alpha}' |
| 81 | + s += ', inplace={inplace}' |
| 82 | + s += ')' |
| 83 | + return s.format(**self.__dict__) |
| 84 | + |
| 85 | + |
| 86 | +class RandomCutmix(torch.nn.Module): |
| 87 | + """Randomly apply Cutmix to the provided batch and targets. |
| 88 | + The class implements the data augmentations as described in the paper |
| 89 | + `"CutMix: Regularization Strategy to Train Strong Classifiers with Localizable Features" |
| 90 | + <https://arxiv.org/abs/1905.04899>`_. |
| 91 | +
|
| 92 | + Args: |
| 93 | + num_classes (int): number of classes used for one-hot encoding. |
| 94 | + p (float): probability of the batch being transformed. Default value is 0.5. |
| 95 | + alpha (float): hyperparameter of the Beta distribution used for cutmix. |
| 96 | + Default value is 1.0. |
| 97 | + inplace (bool): boolean to make this transform inplace. Default set to False. |
| 98 | + """ |
| 99 | + |
| 100 | + def __init__(self, num_classes: int, |
| 101 | + p: float = 0.5, alpha: float = 1.0, |
| 102 | + inplace: bool = False) -> None: |
| 103 | + super().__init__() |
| 104 | + assert num_classes > 0, "Please provide a valid positive value for the num_classes." |
| 105 | + assert alpha > 0, "Alpha param can't be zero." |
| 106 | + |
| 107 | + self.num_classes = num_classes |
| 108 | + self.p = p |
| 109 | + self.alpha = alpha |
| 110 | + self.inplace = inplace |
| 111 | + |
| 112 | + def forward(self, batch: Tensor, target: Tensor) -> Tuple[Tensor, Tensor]: |
| 113 | + """ |
| 114 | + Args: |
| 115 | + batch (Tensor): Float tensor of size (B, C, H, W) |
| 116 | + target (Tensor): Integer tensor of size (B, ) |
| 117 | +
|
| 118 | + Returns: |
| 119 | + Tensor: Randomly transformed batch. |
| 120 | + """ |
| 121 | + if batch.ndim != 4: |
| 122 | + raise ValueError("Batch ndim should be 4. Got {}".format(batch.ndim)) |
| 123 | + elif target.ndim != 1: |
| 124 | + raise ValueError("Target ndim should be 1. Got {}".format(target.ndim)) |
| 125 | + elif not batch.is_floating_point(): |
| 126 | + raise TypeError('Batch dtype should be a float tensor. Got {}.'.format(batch.dtype)) |
| 127 | + elif target.dtype != torch.int64: |
| 128 | + raise TypeError("Target dtype should be torch.int64. Got {}".format(target.dtype)) |
| 129 | + |
| 130 | + if not self.inplace: |
| 131 | + batch = batch.clone() |
| 132 | + target = target.clone() |
| 133 | + |
| 134 | + if target.ndim == 1: |
| 135 | + target = torch.nn.functional.one_hot(target, num_classes=self.num_classes).to(dtype=torch.float32) |
| 136 | + |
| 137 | + if torch.rand(1).item() >= self.p: |
| 138 | + return batch, target |
| 139 | + |
| 140 | + # It's faster to roll the batch by one instead of shuffling it to create image pairs |
| 141 | + batch_rolled = batch.roll(1, 0) |
| 142 | + target_rolled = target.roll(1) |
| 143 | + |
| 144 | + # Implemented as on cutmix paper, page 12 (with minor corrections on typos). |
| 145 | + lambda_param = float(torch._sample_dirichlet(torch.tensor([self.alpha, self.alpha]))[0]) |
| 146 | + W, H = F.get_image_size(batch) |
| 147 | + |
| 148 | + r_x = torch.randint(W, (1,)) |
| 149 | + r_y = torch.randint(H, (1,)) |
| 150 | + |
| 151 | + r = 0.5 * math.sqrt(1.0 - lambda_param) |
| 152 | + r_w_half = int(r * W) |
| 153 | + r_h_half = int(r * H) |
| 154 | + |
| 155 | + x1 = int(torch.clamp(r_x - r_w_half, min=0)) |
| 156 | + y1 = int(torch.clamp(r_y - r_h_half, min=0)) |
| 157 | + x2 = int(torch.clamp(r_x + r_w_half, max=W)) |
| 158 | + y2 = int(torch.clamp(r_y + r_h_half, max=H)) |
| 159 | + |
| 160 | + batch[:, :, y1:y2, x1:x2] = batch_rolled[:, :, y1:y2, x1:x2] |
| 161 | + lambda_param = float(1.0 - (x2 - x1) * (y2 - y1) / (W * H)) |
| 162 | + |
| 163 | + target_rolled.mul_(1.0 - lambda_param) |
| 164 | + target.mul_(lambda_param).add_(target_rolled) |
| 165 | + |
| 166 | + return batch, target |
| 167 | + |
| 168 | + def __repr__(self) -> str: |
| 169 | + s = self.__class__.__name__ + '(' |
| 170 | + s += 'num_classes={num_classes}' |
| 171 | + s += ', p={p}' |
| 172 | + s += ', alpha={alpha}' |
| 173 | + s += ', inplace={inplace}' |
| 174 | + s += ')' |
| 175 | + return s.format(**self.__dict__) |
0 commit comments