random_erasing.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """ Random Erasing (Cutout)
  2. Originally inspired by impl at https://github.com/zhunzhong07/Random-Erasing, Apache 2.0
  3. Copyright Zhun Zhong & Liang Zheng
  4. Hacked together by / Copyright 2019, Ross Wightman
  5. """
  6. import random
  7. import math
  8. import torch
  9. def _get_pixels(per_pixel, rand_color, patch_size, dtype=torch.float32, device='cuda'):
  10. # NOTE I've seen CUDA illegal memory access errors being caused by the normal_()
  11. # paths, flip the order so normal is run on CPU if this becomes a problem
  12. # Issue has been fixed in master https://github.com/pytorch/pytorch/issues/19508
  13. if per_pixel:
  14. return torch.empty(patch_size, dtype=dtype, device=device).normal_()
  15. elif rand_color:
  16. return torch.empty((patch_size[0], 1, 1), dtype=dtype, device=device).normal_()
  17. else:
  18. return torch.zeros((patch_size[0], 1, 1), dtype=dtype, device=device)
  19. class RandomErasing:
  20. """ Randomly selects a rectangle region in an image and erases its pixels.
  21. 'Random Erasing Data Augmentation' by Zhong et al.
  22. See https://arxiv.org/pdf/1708.04896.pdf
  23. This variant of RandomErasing is intended to be applied to either a batch
  24. or single image tensor after it has been normalized by dataset mean and std.
  25. Args:
  26. probability: Probability that the Random Erasing operation will be performed.
  27. min_area: Minimum percentage of erased area wrt input image area.
  28. max_area: Maximum percentage of erased area wrt input image area.
  29. min_aspect: Minimum aspect ratio of erased area.
  30. mode: pixel color mode, one of 'const', 'rand', or 'pixel'
  31. 'const' - erase block is constant color of 0 for all channels
  32. 'rand' - erase block is same per-channel random (normal) color
  33. 'pixel' - erase block is per-pixel random (normal) color
  34. max_count: maximum number of erasing blocks per image, area per box is scaled by count.
  35. per-image count is randomly chosen between 1 and this value.
  36. """
  37. def __init__(
  38. self,
  39. probability=0.5,
  40. min_area=0.02,
  41. max_area=1/3,
  42. min_aspect=0.3,
  43. max_aspect=None,
  44. mode='const',
  45. min_count=1,
  46. max_count=None,
  47. num_splits=0,
  48. device='cuda',
  49. ):
  50. self.probability = probability
  51. self.min_area = min_area
  52. self.max_area = max_area
  53. max_aspect = max_aspect or 1 / min_aspect
  54. self.log_aspect_ratio = (math.log(min_aspect), math.log(max_aspect))
  55. self.min_count = min_count
  56. self.max_count = max_count or min_count
  57. self.num_splits = num_splits
  58. self.mode = mode.lower()
  59. self.rand_color = False
  60. self.per_pixel = False
  61. if self.mode == 'rand':
  62. self.rand_color = True # per block random normal
  63. elif self.mode == 'pixel':
  64. self.per_pixel = True # per pixel random normal
  65. else:
  66. assert not self.mode or self.mode == 'const'
  67. self.device = device
  68. def _erase(self, img, chan, img_h, img_w, dtype):
  69. if random.random() > self.probability:
  70. return
  71. area = img_h * img_w
  72. count = self.min_count if self.min_count == self.max_count else \
  73. random.randint(self.min_count, self.max_count)
  74. for _ in range(count):
  75. for attempt in range(10):
  76. target_area = random.uniform(self.min_area, self.max_area) * area / count
  77. aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio))
  78. h = int(round(math.sqrt(target_area * aspect_ratio)))
  79. w = int(round(math.sqrt(target_area / aspect_ratio)))
  80. if w < img_w and h < img_h:
  81. top = random.randint(0, img_h - h)
  82. left = random.randint(0, img_w - w)
  83. img[:, top:top + h, left:left + w] = _get_pixels(
  84. self.per_pixel,
  85. self.rand_color,
  86. (chan, h, w),
  87. dtype=dtype,
  88. device=self.device,
  89. )
  90. break
  91. def __call__(self, input):
  92. if len(input.size()) == 3:
  93. self._erase(input, *input.size(), input.dtype)
  94. else:
  95. batch_size, chan, img_h, img_w = input.size()
  96. # skip first slice of batch if num_splits is set (for clean portion of samples)
  97. batch_start = batch_size // self.num_splits if self.num_splits > 1 else 0
  98. for i in range(batch_start, batch_size):
  99. self._erase(input[i], chan, img_h, img_w, input.dtype)
  100. return input
  101. def __repr__(self):
  102. # NOTE simplified state for repr
  103. fs = self.__class__.__name__ + f'(p={self.probability}, mode={self.mode}'
  104. fs += f', count=({self.min_count}, {self.max_count}))'
  105. return fs