| 118 | self.num_classes = num_classes |
| 119 | |
| 120 | def __call__(self, images, labels): |
| 121 | batch_size = images.size(0) |
| 122 | |
| 123 | if self.alpha > 0: |
| 124 | lam = np.random.beta(self.alpha, self.alpha) |
| 125 | else: |
| 126 | lam = 1 |
| 127 | |
| 128 | index = torch.randperm(batch_size).to(images.device) |
| 129 | |
| 130 | _, _, H, W = images.shape |
| 131 | cut_rat = np.sqrt(1.0 - lam) |
| 132 | cut_w = int(W * cut_rat) |
| 133 | cut_h = int(H * cut_rat) |
| 134 | |
| 135 | cx = np.random.randint(W) |
| 136 | cy = np.random.randint(H) |
| 137 | |
| 138 | bbx1 = np.clip(cx - cut_w // 2, 0, W) |
| 139 | bby1 = np.clip(cy - cut_h // 2, 0, H) |
| 140 | bbx2 = np.clip(cx + cut_w // 2, 0, W) |
| 141 | bby2 = np.clip(cy + cut_h // 2, 0, H) |
| 142 | |
| 143 | images[:, :, bby1:bby2, bbx1:bbx2] = images[index, :, bby1:bby2, bbx1:bbx2] |
| 144 | |
| 145 | lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (W * H)) |
| 146 | |
| 147 | return images, labels, labels[index], lam |
| 148 | |
| 149 | class GridMask: |
| 150 | def __init__(self, d1: int = 96, d2: int = 224, rotate: float = 1, ratio: float = 0.5): |