1. RandomErasing

Random Erasing randomly selects a rectangle region in an image and erases its pixels with random values(随机选择一个方形区域填充一个随机值)
image.png
code

2. CutOut

随机的将样本中的部分区域cut掉,并且填充0像素值
CutOutRandom Erasing最主要的区别在于在CutOut中,擦除矩形区域存在一定概率不完全在原图像中的。而在Random Erasing中,擦除矩形区域一定在原图像内。

  1. class Cutout(object):
  2. """Randomly mask out one or more patches from an image.
  3. Args:
  4. n_holes (int): Number of patches to cut out of each image.
  5. length (int): The length (in pixels) of each square patch.
  6. """
  7. def __init__(self, n_holes, length):
  8. self.n_holes = n_holes
  9. self.length = length
  10. def __call__(self, img):
  11. """
  12. Args:
  13. img (Tensor): Tensor image of size (C, H, W).
  14. Returns:
  15. Tensor: Image with n_holes of dimension length x length cut out of it.
  16. """
  17. h = img.size(1)
  18. w = img.size(2)
  19. mask = np.ones((h, w), np.float32)
  20. for n in range(self.n_holes):
  21. y = np.random.randint(h)
  22. x = np.random.randint(w)
  23. y1 = np.clip(y - self.length // 2, 0, h)
  24. y2 = np.clip(y + self.length // 2, 0, h)
  25. x1 = np.clip(x - self.length // 2, 0, w)
  26. x2 = np.clip(x + self.length // 2, 0, w)
  27. mask[y1: y2, x1: x2] = 0.
  28. mask = torch.from_numpy(mask)
  29. mask = mask.expand_as(img)
  30. img = img * mask
  31. return img

3. MixUp

对于图像分类:
深度学习模型训练预处理(一) - 图2
分类代码:

  1. def mixup_data(x, y, alpha=1.0, use_cuda=True):
  2. '''Returns mixed inputs, pairs of targets, and lambda'''
  3. if alpha > 0:
  4. lam = np.random.beta(alpha, alpha)
  5. else:
  6. lam = 1
  7. batch_size = x.size()[0]
  8. if use_cuda:
  9. index = torch.randperm(batch_size).cuda()
  10. else:
  11. index = torch.randperm(batch_size)
  12. mixed_x = lam * x + (1 - lam) * x[index, :]
  13. y_a, y_b = y, y[index]
  14. return mixed_x, y_a, y_b, lam
  15. def mixup_criterion(criterion, pred, y_a, y_b, lam):
  16. return lam * criterion(pred, y_a) + (1 - lam) * criterion(pred, y_b)

对于目标检测,mixup为如下图操作:
image.png
输出图像尺寸为较大w和较大h组合,新增区域填0即可。

4. CutMix

将一幅图像某块区域剪切贴到另一幅图上。
image.png

  1. def cutmix(batch, alpha):
  2. data, targets = batch
  3. indices = torch.randperm(data.size(0))
  4. shuffled_data = data[indices]
  5. shuffled_targets = targets[indices]
  6. lam = np.random.beta(alpha, alpha)
  7. image_h, image_w = data.shape[2:]
  8. cx = np.random.uniform(0, image_w)
  9. cy = np.random.uniform(0, image_h)
  10. w = image_w * np.sqrt(1 - lam)
  11. h = image_h * np.sqrt(1 - lam)
  12. x0 = int(np.round(max(cx - w / 2, 0)))
  13. x1 = int(np.round(min(cx + w / 2, image_w)))
  14. y0 = int(np.round(max(cy - h / 2, 0)))
  15. y1 = int(np.round(min(cy + h / 2, image_h)))
  16. data[:, :, y0:y1, x0:x1] = shuffled_data[:, :, y0:y1, x0:x1]
  17. targets = (targets, shuffled_targets, lam)
  18. return data, targets
  19. class CutMixCollator:
  20. def __init__(self, alpha):
  21. self.alpha = alpha
  22. def __call__(self, batch):
  23. batch = torch.utils.data.dataloader.default_collate(batch)
  24. batch = cutmix(batch, self.alpha)
  25. return batch
  26. class CutMixCriterion:
  27. def __init__(self, reduction):
  28. self.criterion = nn.CrossEntropyLoss(reduction=reduction)
  29. def __call__(self, preds, targets):
  30. targets1, targets2, lam = targets
  31. return lam * self.criterion(
  32. preds, targets1) + (1 - lam) * self.criterion(preds, targets2)

5. AugMix

augmix伪代码:
image.png
image.png

augmix.gif
AugMix包含增强和混合两个部分:

  1. 增强(利用一些不对图像做对比度改变、颜色改变、亮度改变、锐化改变、切块操作这些导致明显视觉变化的操作,例如小角度旋转对图像进行一系列变换)
  2. 融合:此过程则是,先使用深度学习模型训练预处理(一) - 图8分布随机抽取3个权值深度学习模型训练预处理(一) - 图9,根据Dirichlet分布的性质,权值和深度学习模型训练预处理(一) - 图10。之后按照权值深度学习模型训练预处理(一) - 图11将三条链按权相加得到深度学习模型训练预处理(一) - 图12。同时原图深度学习模型训练预处理(一) - 图13会跨接到最后一步与深度学习模型训练预处理(一) - 图14按权相加,权值来自从深度学习模型训练预处理(一) - 图15分布中取样,相加后得到最后的新样本深度学习模型训练预处理(一) - 图16,至此所有步骤完成。

为了使模型的输出更平滑更稳定,同时由于从同一个原始样本中使用AugMix得到的不同的新样本具有相似的语义,假设对深度学习模型训练预处理(一) - 图17做了两次AugMix,得到深度学习模型训练预处理(一) - 图18深度学习模型训练预处理(一) - 图19这三个样本输入模型后得到的结果的分布理应是相似,所以作者在损失函数中引入了深度学习模型训练预处理(一) - 图20散度,具体为:
深度学习模型训练预处理(一) - 图21
深度学习模型训练预处理(一) - 图22
深度学习模型训练预处理(一) - 图23
深度学习模型训练预处理(一) - 图24
深度学习模型训练预处理(一) - 图25
深度学习模型训练预处理(一) - 图26

  1. """Reference implementation of AugMix's data augmentation method in numpy."""
  2. import augmentations
  3. import numpy as np
  4. from PIL import Image
  5. # CIFAR-10 constants
  6. MEAN = [0.4914, 0.4822, 0.4465]
  7. STD = [0.2023, 0.1994, 0.2010]
  8. def normalize(image):
  9. """Normalize input image channel-wise to zero mean and unit variance."""
  10. image = image.transpose(2, 0, 1) # Switch to channel-first
  11. mean, std = np.array(MEAN), np.array(STD)
  12. image = (image - mean[:, None, None]) / std[:, None, None]
  13. return image.transpose(1, 2, 0)
  14. def apply_op(image, op, severity):
  15. image = np.clip(image * 255., 0, 255).astype(np.uint8)
  16. pil_img = Image.fromarray(image) # Convert to PIL.Image
  17. pil_img = op(pil_img, severity)
  18. return np.asarray(pil_img) / 255.
  19. def augment_and_mix(image, severity=3, width=3, depth=-1, alpha=1.):
  20. """Perform AugMix augmentations and compute mixture.
  21. Args:
  22. image: Raw input image as float32 np.ndarray of shape (h, w, c)
  23. severity: Severity of underlying augmentation operators (between 1 to 10).
  24. width: Width of augmentation chain
  25. depth: Depth of augmentation chain. -1 enables stochastic depth uniformly
  26. from [1, 3]
  27. alpha: Probability coefficient for Beta and Dirichlet distributions.
  28. Returns:
  29. mixed: Augmented and mixed image.
  30. """
  31. ws = np.float32(
  32. np.random.dirichlet([alpha] * width))
  33. m = np.float32(np.random.beta(alpha, alpha))
  34. mix = np.zeros_like(image)
  35. for i in range(width):
  36. image_aug = image.copy()
  37. depth = depth if depth > 0 else np.random.randint(1, 4)
  38. for _ in range(depth):
  39. op = np.random.choice(augmentations.augmentations)
  40. image_aug = apply_op(image_aug, op, severity)
  41. # Preprocessing commutes since all coefficients are convex
  42. mix += ws[i] * normalize(image_aug)
  43. mixed = (1 - m) * normalize(image) + m * mix
  44. return mixed

6. DropBlock

References

Random Erasing&Cutout——两种相似的数据增强方式 目标检测中图像增强,mixup 如何操作? 【论文阅读笔记】CutMix:数据增强 google-research/augmix https://zhuanlan.zhihu.com/p/101432423 https://zhuanlan.zhihu.com/p/100960934