randaugment.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from __future__ import unicode_literals
  18. from PIL import Image, ImageEnhance, ImageOps
  19. import numpy as np
  20. import random
  21. import six
  22. class RawRandAugment(object):
  23. def __init__(self,
  24. num_layers=2,
  25. magnitude=5,
  26. fillcolor=(128, 128, 128),
  27. **kwargs):
  28. self.num_layers = num_layers
  29. self.magnitude = magnitude
  30. self.max_level = 10
  31. abso_level = self.magnitude / self.max_level
  32. self.level_map = {
  33. "shearX": 0.3 * abso_level,
  34. "shearY": 0.3 * abso_level,
  35. "translateX": 150.0 / 331 * abso_level,
  36. "translateY": 150.0 / 331 * abso_level,
  37. "rotate": 30 * abso_level,
  38. "color": 0.9 * abso_level,
  39. "posterize": int(4.0 * abso_level),
  40. "solarize": 256.0 * abso_level,
  41. "contrast": 0.9 * abso_level,
  42. "sharpness": 0.9 * abso_level,
  43. "brightness": 0.9 * abso_level,
  44. "autocontrast": 0,
  45. "equalize": 0,
  46. "invert": 0
  47. }
  48. # from https://stackoverflow.com/questions/5252170/
  49. # specify-image-filling-color-when-rotating-in-python-with-pil-and-setting-expand
  50. def rotate_with_fill(img, magnitude):
  51. rot = img.convert("RGBA").rotate(magnitude)
  52. return Image.composite(rot,
  53. Image.new("RGBA", rot.size, (128, ) * 4),
  54. rot).convert(img.mode)
  55. rnd_ch_op = random.choice
  56. self.func = {
  57. "shearX": lambda img, magnitude: img.transform(
  58. img.size,
  59. Image.AFFINE,
  60. (1, magnitude * rnd_ch_op([-1, 1]), 0, 0, 1, 0),
  61. Image.BICUBIC,
  62. fillcolor=fillcolor),
  63. "shearY": lambda img, magnitude: img.transform(
  64. img.size,
  65. Image.AFFINE,
  66. (1, 0, 0, magnitude * rnd_ch_op([-1, 1]), 1, 0),
  67. Image.BICUBIC,
  68. fillcolor=fillcolor),
  69. "translateX": lambda img, magnitude: img.transform(
  70. img.size,
  71. Image.AFFINE,
  72. (1, 0, magnitude * img.size[0] * rnd_ch_op([-1, 1]), 0, 1, 0),
  73. fillcolor=fillcolor),
  74. "translateY": lambda img, magnitude: img.transform(
  75. img.size,
  76. Image.AFFINE,
  77. (1, 0, 0, 0, 1, magnitude * img.size[1] * rnd_ch_op([-1, 1])),
  78. fillcolor=fillcolor),
  79. "rotate": lambda img, magnitude: rotate_with_fill(img, magnitude),
  80. "color": lambda img, magnitude: ImageEnhance.Color(img).enhance(
  81. 1 + magnitude * rnd_ch_op([-1, 1])),
  82. "posterize": lambda img, magnitude:
  83. ImageOps.posterize(img, magnitude),
  84. "solarize": lambda img, magnitude:
  85. ImageOps.solarize(img, magnitude),
  86. "contrast": lambda img, magnitude:
  87. ImageEnhance.Contrast(img).enhance(
  88. 1 + magnitude * rnd_ch_op([-1, 1])),
  89. "sharpness": lambda img, magnitude:
  90. ImageEnhance.Sharpness(img).enhance(
  91. 1 + magnitude * rnd_ch_op([-1, 1])),
  92. "brightness": lambda img, magnitude:
  93. ImageEnhance.Brightness(img).enhance(
  94. 1 + magnitude * rnd_ch_op([-1, 1])),
  95. "autocontrast": lambda img, magnitude:
  96. ImageOps.autocontrast(img),
  97. "equalize": lambda img, magnitude: ImageOps.equalize(img),
  98. "invert": lambda img, magnitude: ImageOps.invert(img)
  99. }
  100. def __call__(self, img):
  101. avaiable_op_names = list(self.level_map.keys())
  102. for layer_num in range(self.num_layers):
  103. op_name = np.random.choice(avaiable_op_names)
  104. img = self.func[op_name](img, self.level_map[op_name])
  105. return img
  106. class RandAugment(RawRandAugment):
  107. """ RandAugment wrapper to auto fit different img types """
  108. def __init__(self, prob=0.5, *args, **kwargs):
  109. self.prob = prob
  110. if six.PY2:
  111. super(RandAugment, self).__init__(*args, **kwargs)
  112. else:
  113. super().__init__(*args, **kwargs)
  114. def __call__(self, data):
  115. if np.random.rand() > self.prob:
  116. return data
  117. img = data['image']
  118. if not isinstance(img, Image.Image):
  119. img = np.ascontiguousarray(img)
  120. img = Image.fromarray(img)
  121. if six.PY2:
  122. img = super(RandAugment, self).__call__(img)
  123. else:
  124. img = super().__call__(img)
  125. if isinstance(img, Image.Image):
  126. img = np.asarray(img)
  127. data['image'] = img
  128. return data