| 50 | self.alpha = alpha |
| 51 | |
| 52 | def __call__(self, batch): |
| 53 | images, labels = batch |
| 54 | batch_size = images.size(0) |
| 55 | index = torch.randperm(batch_size) |
| 56 | |
| 57 | if self.alpha > 0: |
| 58 | lam = torch.distributions.Beta(self.alpha, self.alpha).sample() |
| 59 | else: |
| 60 | lam = 1.0 |
| 61 | |
| 62 | _, _, H, W = images.shape |
| 63 | cut_rat = torch.sqrt(1.0 - lam) |
| 64 | cut_w = (W * cut_rat).int() |
| 65 | cut_h = (H * cut_rat).int() |
| 66 | |
| 67 | cx = torch.randint(W, (1,)).item() |
| 68 | cy = torch.randint(H, (1,)).item() |
| 69 | |
| 70 | bbx1 = torch.clamp(cx - cut_w // 2, 0, W) |
| 71 | bby1 = torch.clamp(cy - cut_h // 2, 0, H) |
| 72 | bbx2 = torch.clamp(cx + cut_w // 2, 0, W) |
| 73 | bby2 = torch.clamp(cy + cut_h // 2, 0, H) |
| 74 | |
| 75 | images[:, :, bby1:bby2, bbx1:bbx2] = images[index, :, bby1:bby2, bbx1:bbx2] |
| 76 | |
| 77 | lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (W * H)) |
| 78 | |
| 79 | return images, labels, labels[index], lam |
| 80 | |
| 81 | class GaussianNoise: |
| 82 | def __init__(self, mean: float = 0.0, std: float = 0.1): |