randaugment.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  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. # This code is based on https://github.com/heartInsert/randaugment
  15. from PIL import Image, ImageEnhance, ImageOps
  16. import numpy as np
  17. import random
  18. class RandAugment(object):
  19. def __init__(self, num_layers=2, magnitude=5, fillcolor=(128, 128, 128)):
  20. self.num_layers = num_layers
  21. self.magnitude = magnitude
  22. self.max_level = 10
  23. abso_level = self.magnitude / self.max_level
  24. self.level_map = {
  25. "shearX": 0.3 * abso_level,
  26. "shearY": 0.3 * abso_level,
  27. "translateX": 150.0 / 331 * abso_level,
  28. "translateY": 150.0 / 331 * abso_level,
  29. "rotate": 30 * abso_level,
  30. "color": 0.9 * abso_level,
  31. "posterize": int(4.0 * abso_level),
  32. "solarize": 256.0 * abso_level,
  33. "contrast": 0.9 * abso_level,
  34. "sharpness": 0.9 * abso_level,
  35. "brightness": 0.9 * abso_level,
  36. "autocontrast": 0,
  37. "equalize": 0,
  38. "invert": 0
  39. }
  40. # from https://stackoverflow.com/questions/5252170/
  41. # specify-image-filling-color-when-rotating-in-python-with-pil-and-setting-expand
  42. def rotate_with_fill(img, magnitude):
  43. rot = img.convert("RGBA").rotate(magnitude)
  44. return Image.composite(rot,
  45. Image.new("RGBA", rot.size, (128, ) * 4),
  46. rot).convert(img.mode)
  47. rnd_ch_op = random.choice
  48. self.func = {
  49. "shearX": lambda img, magnitude: img.transform(
  50. img.size,
  51. Image.AFFINE,
  52. (1, magnitude * rnd_ch_op([-1, 1]), 0, 0, 1, 0),
  53. Image.BICUBIC,
  54. fillcolor=fillcolor),
  55. "shearY": lambda img, magnitude: img.transform(
  56. img.size,
  57. Image.AFFINE,
  58. (1, 0, 0, magnitude * rnd_ch_op([-1, 1]), 1, 0),
  59. Image.BICUBIC,
  60. fillcolor=fillcolor),
  61. "translateX": lambda img, magnitude: img.transform(
  62. img.size,
  63. Image.AFFINE,
  64. (1, 0, magnitude * img.size[0] * rnd_ch_op([-1, 1]), 0, 1, 0),
  65. fillcolor=fillcolor),
  66. "translateY": lambda img, magnitude: img.transform(
  67. img.size,
  68. Image.AFFINE,
  69. (1, 0, 0, 0, 1, magnitude * img.size[1] * rnd_ch_op([-1, 1])),
  70. fillcolor=fillcolor),
  71. "rotate": lambda img, magnitude: rotate_with_fill(img, magnitude),
  72. "color": lambda img, magnitude: ImageEnhance.Color(img).enhance(
  73. 1 + magnitude * rnd_ch_op([-1, 1])),
  74. "posterize": lambda img, magnitude:
  75. ImageOps.posterize(img, magnitude),
  76. "solarize": lambda img, magnitude:
  77. ImageOps.solarize(img, magnitude),
  78. "contrast": lambda img, magnitude:
  79. ImageEnhance.Contrast(img).enhance(
  80. 1 + magnitude * rnd_ch_op([-1, 1])),
  81. "sharpness": lambda img, magnitude:
  82. ImageEnhance.Sharpness(img).enhance(
  83. 1 + magnitude * rnd_ch_op([-1, 1])),
  84. "brightness": lambda img, magnitude:
  85. ImageEnhance.Brightness(img).enhance(
  86. 1 + magnitude * rnd_ch_op([-1, 1])),
  87. "autocontrast": lambda img, magnitude:
  88. ImageOps.autocontrast(img),
  89. "equalize": lambda img, magnitude: ImageOps.equalize(img),
  90. "invert": lambda img, magnitude: ImageOps.invert(img)
  91. }
  92. def __call__(self, img):
  93. avaiable_op_names = list(self.level_map.keys())
  94. for layer_num in range(self.num_layers):
  95. op_name = np.random.choice(avaiable_op_names)
  96. img = self.func[op_name](img, self.level_map[op_name])
  97. return img