autoaugment.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/DeepVoltaire/AutoAugment/blob/master/autoaugment.py
  15. from PIL import Image, ImageEnhance, ImageOps
  16. import numpy as np
  17. import random
  18. class ImageNetPolicy(object):
  19. """ Randomly choose one of the best 24 Sub-policies on ImageNet.
  20. Example:
  21. >>> policy = ImageNetPolicy()
  22. >>> transformed = policy(image)
  23. Example as a PyTorch Transform:
  24. >>> transform=transforms.Compose([
  25. >>> transforms.Resize(256),
  26. >>> ImageNetPolicy(),
  27. >>> transforms.ToTensor()])
  28. """
  29. def __init__(self, fillcolor=(128, 128, 128)):
  30. self.policies = [
  31. SubPolicy(0.4, "posterize", 8, 0.6, "rotate", 9, fillcolor),
  32. SubPolicy(0.6, "solarize", 5, 0.6, "autocontrast", 5, fillcolor),
  33. SubPolicy(0.8, "equalize", 8, 0.6, "equalize", 3, fillcolor),
  34. SubPolicy(0.6, "posterize", 7, 0.6, "posterize", 6, fillcolor),
  35. SubPolicy(0.4, "equalize", 7, 0.2, "solarize", 4, fillcolor),
  36. SubPolicy(0.4, "equalize", 4, 0.8, "rotate", 8, fillcolor),
  37. SubPolicy(0.6, "solarize", 3, 0.6, "equalize", 7, fillcolor),
  38. SubPolicy(0.8, "posterize", 5, 1.0, "equalize", 2, fillcolor),
  39. SubPolicy(0.2, "rotate", 3, 0.6, "solarize", 8, fillcolor),
  40. SubPolicy(0.6, "equalize", 8, 0.4, "posterize", 6, fillcolor),
  41. SubPolicy(0.8, "rotate", 8, 0.4, "color", 0, fillcolor),
  42. SubPolicy(0.4, "rotate", 9, 0.6, "equalize", 2, fillcolor),
  43. SubPolicy(0.0, "equalize", 7, 0.8, "equalize", 8, fillcolor),
  44. SubPolicy(0.6, "invert", 4, 1.0, "equalize", 8, fillcolor),
  45. SubPolicy(0.6, "color", 4, 1.0, "contrast", 8, fillcolor),
  46. SubPolicy(0.8, "rotate", 8, 1.0, "color", 2, fillcolor),
  47. SubPolicy(0.8, "color", 8, 0.8, "solarize", 7, fillcolor),
  48. SubPolicy(0.4, "sharpness", 7, 0.6, "invert", 8, fillcolor),
  49. SubPolicy(0.6, "shearX", 5, 1.0, "equalize", 9, fillcolor),
  50. SubPolicy(0.4, "color", 0, 0.6, "equalize", 3, fillcolor),
  51. SubPolicy(0.4, "equalize", 7, 0.2, "solarize", 4, fillcolor),
  52. SubPolicy(0.6, "solarize", 5, 0.6, "autocontrast", 5, fillcolor),
  53. SubPolicy(0.6, "invert", 4, 1.0, "equalize", 8, fillcolor),
  54. SubPolicy(0.6, "color", 4, 1.0, "contrast", 8, fillcolor),
  55. SubPolicy(0.8, "equalize", 8, 0.6, "equalize", 3, fillcolor)
  56. ]
  57. def __call__(self, img, policy_idx=None):
  58. if policy_idx is None or not isinstance(policy_idx, int):
  59. policy_idx = random.randint(0, len(self.policies) - 1)
  60. else:
  61. policy_idx = policy_idx % len(self.policies)
  62. return self.policies[policy_idx](img)
  63. def __repr__(self):
  64. return "AutoAugment ImageNet Policy"
  65. class CIFAR10Policy(object):
  66. """ Randomly choose one of the best 25 Sub-policies on CIFAR10.
  67. Example:
  68. >>> policy = CIFAR10Policy()
  69. >>> transformed = policy(image)
  70. Example as a PyTorch Transform:
  71. >>> transform=transforms.Compose([
  72. >>> transforms.Resize(256),
  73. >>> CIFAR10Policy(),
  74. >>> transforms.ToTensor()])
  75. """
  76. def __init__(self, fillcolor=(128, 128, 128)):
  77. self.policies = [
  78. SubPolicy(0.1, "invert", 7, 0.2, "contrast", 6, fillcolor),
  79. SubPolicy(0.7, "rotate", 2, 0.3, "translateX", 9, fillcolor),
  80. SubPolicy(0.8, "sharpness", 1, 0.9, "sharpness", 3, fillcolor),
  81. SubPolicy(0.5, "shearY", 8, 0.7, "translateY", 9, fillcolor),
  82. SubPolicy(0.5, "autocontrast", 8, 0.9, "equalize", 2, fillcolor),
  83. SubPolicy(0.2, "shearY", 7, 0.3, "posterize", 7, fillcolor),
  84. SubPolicy(0.4, "color", 3, 0.6, "brightness", 7, fillcolor),
  85. SubPolicy(0.3, "sharpness", 9, 0.7, "brightness", 9, fillcolor),
  86. SubPolicy(0.6, "equalize", 5, 0.5, "equalize", 1, fillcolor),
  87. SubPolicy(0.6, "contrast", 7, 0.6, "sharpness", 5, fillcolor),
  88. SubPolicy(0.7, "color", 7, 0.5, "translateX", 8, fillcolor),
  89. SubPolicy(0.3, "equalize", 7, 0.4, "autocontrast", 8, fillcolor),
  90. SubPolicy(0.4, "translateY", 3, 0.2, "sharpness", 6, fillcolor),
  91. SubPolicy(0.9, "brightness", 6, 0.2, "color", 8, fillcolor),
  92. SubPolicy(0.5, "solarize", 2, 0.0, "invert", 3, fillcolor),
  93. SubPolicy(0.2, "equalize", 0, 0.6, "autocontrast", 0, fillcolor),
  94. SubPolicy(0.2, "equalize", 8, 0.8, "equalize", 4, fillcolor),
  95. SubPolicy(0.9, "color", 9, 0.6, "equalize", 6, fillcolor),
  96. SubPolicy(0.8, "autocontrast", 4, 0.2, "solarize", 8, fillcolor),
  97. SubPolicy(0.1, "brightness", 3, 0.7, "color", 0, fillcolor),
  98. SubPolicy(0.4, "solarize", 5, 0.9, "autocontrast", 3, fillcolor),
  99. SubPolicy(0.9, "translateY", 9, 0.7, "translateY", 9, fillcolor),
  100. SubPolicy(0.9, "autocontrast", 2, 0.8, "solarize", 3, fillcolor),
  101. SubPolicy(0.8, "equalize", 8, 0.1, "invert", 3, fillcolor),
  102. SubPolicy(0.7, "translateY", 9, 0.9, "autocontrast", 1, fillcolor)
  103. ]
  104. def __call__(self, img, policy_idx=None):
  105. if policy_idx is None or not isinstance(policy_idx, int):
  106. policy_idx = random.randint(0, len(self.policies) - 1)
  107. else:
  108. policy_idx = policy_idx % len(self.policies)
  109. return self.policies[policy_idx](img)
  110. def __repr__(self):
  111. return "AutoAugment CIFAR10 Policy"
  112. class SVHNPolicy(object):
  113. """ Randomly choose one of the best 25 Sub-policies on SVHN.
  114. Example:
  115. >>> policy = SVHNPolicy()
  116. >>> transformed = policy(image)
  117. Example as a PyTorch Transform:
  118. >>> transform=transforms.Compose([
  119. >>> transforms.Resize(256),
  120. >>> SVHNPolicy(),
  121. >>> transforms.ToTensor()])
  122. """
  123. def __init__(self, fillcolor=(128, 128, 128)):
  124. self.policies = [
  125. SubPolicy(0.9, "shearX", 4, 0.2, "invert", 3, fillcolor),
  126. SubPolicy(0.9, "shearY", 8, 0.7, "invert", 5, fillcolor),
  127. SubPolicy(0.6, "equalize", 5, 0.6, "solarize", 6, fillcolor),
  128. SubPolicy(0.9, "invert", 3, 0.6, "equalize", 3, fillcolor),
  129. SubPolicy(0.6, "equalize", 1, 0.9, "rotate", 3, fillcolor),
  130. SubPolicy(0.9, "shearX", 4, 0.8, "autocontrast", 3, fillcolor),
  131. SubPolicy(0.9, "shearY", 8, 0.4, "invert", 5, fillcolor),
  132. SubPolicy(0.9, "shearY", 5, 0.2, "solarize", 6, fillcolor),
  133. SubPolicy(0.9, "invert", 6, 0.8, "autocontrast", 1, fillcolor),
  134. SubPolicy(0.6, "equalize", 3, 0.9, "rotate", 3, fillcolor),
  135. SubPolicy(0.9, "shearX", 4, 0.3, "solarize", 3, fillcolor),
  136. SubPolicy(0.8, "shearY", 8, 0.7, "invert", 4, fillcolor),
  137. SubPolicy(0.9, "equalize", 5, 0.6, "translateY", 6, fillcolor),
  138. SubPolicy(0.9, "invert", 4, 0.6, "equalize", 7, fillcolor),
  139. SubPolicy(0.3, "contrast", 3, 0.8, "rotate", 4, fillcolor),
  140. SubPolicy(0.8, "invert", 5, 0.0, "translateY", 2, fillcolor),
  141. SubPolicy(0.7, "shearY", 6, 0.4, "solarize", 8, fillcolor),
  142. SubPolicy(0.6, "invert", 4, 0.8, "rotate", 4, fillcolor),
  143. SubPolicy(
  144. 0.3, "shearY", 7, 0.9, "translateX", 3, fillcolor), SubPolicy(
  145. 0.1, "shearX", 6, 0.6, "invert", 5, fillcolor), SubPolicy(
  146. 0.7, "solarize", 2, 0.6, "translateY", 7,
  147. fillcolor), SubPolicy(0.8, "shearY", 4, 0.8, "invert",
  148. 8, fillcolor), SubPolicy(
  149. 0.7, "shearX", 9, 0.8,
  150. "translateY", 3,
  151. fillcolor), SubPolicy(
  152. 0.8, "shearY", 5, 0.7,
  153. "autocontrast", 3,
  154. fillcolor),
  155. SubPolicy(0.7, "shearX", 2, 0.1, "invert", 5, fillcolor)
  156. ]
  157. def __call__(self, img, policy_idx=None):
  158. if policy_idx is None or not isinstance(policy_idx, int):
  159. policy_idx = random.randint(0, len(self.policies) - 1)
  160. else:
  161. policy_idx = policy_idx % len(self.policies)
  162. return self.policies[policy_idx](img)
  163. def __repr__(self):
  164. return "AutoAugment SVHN Policy"
  165. class SubPolicy(object):
  166. def __init__(self,
  167. p1,
  168. operation1,
  169. magnitude_idx1,
  170. p2,
  171. operation2,
  172. magnitude_idx2,
  173. fillcolor=(128, 128, 128)):
  174. ranges = {
  175. "shearX": np.linspace(0, 0.3, 10),
  176. "shearY": np.linspace(0, 0.3, 10),
  177. "translateX": np.linspace(0, 150 / 331, 10),
  178. "translateY": np.linspace(0, 150 / 331, 10),
  179. "rotate": np.linspace(0, 30, 10),
  180. "color": np.linspace(0.0, 0.9, 10),
  181. "posterize": np.round(np.linspace(8, 4, 10), 0).astype(np.int),
  182. "solarize": np.linspace(256, 0, 10),
  183. "contrast": np.linspace(0.0, 0.9, 10),
  184. "sharpness": np.linspace(0.0, 0.9, 10),
  185. "brightness": np.linspace(0.0, 0.9, 10),
  186. "autocontrast": [0] * 10,
  187. "equalize": [0] * 10,
  188. "invert": [0] * 10
  189. }
  190. # from https://stackoverflow.com/questions/5252170/specify-image-filling-color-when-rotating-in-python-with-pil-and-setting-expand
  191. def rotate_with_fill(img, magnitude):
  192. rot = img.convert("RGBA").rotate(magnitude)
  193. return Image.composite(rot,
  194. Image.new("RGBA", rot.size, (128, ) * 4),
  195. rot).convert(img.mode)
  196. func = {
  197. "shearX": lambda img, magnitude: img.transform(
  198. img.size, Image.AFFINE, (1, magnitude * random.choice([-1, 1]), 0, 0, 1, 0),
  199. Image.BICUBIC, fillcolor=fillcolor),
  200. "shearY": lambda img, magnitude: img.transform(
  201. img.size, Image.AFFINE, (1, 0, 0, magnitude * random.choice([-1, 1]), 1, 0),
  202. Image.BICUBIC, fillcolor=fillcolor),
  203. "translateX": lambda img, magnitude: img.transform(
  204. img.size, Image.AFFINE, (1, 0, magnitude * img.size[0] * random.choice([-1, 1]), 0, 1, 0),
  205. fillcolor=fillcolor),
  206. "translateY": lambda img, magnitude: img.transform(
  207. img.size, Image.AFFINE, (1, 0, 0, 0, 1, magnitude * img.size[1] * random.choice([-1, 1])),
  208. fillcolor=fillcolor),
  209. "rotate": lambda img, magnitude: rotate_with_fill(img, magnitude),
  210. # "rotate": lambda img, magnitude: img.rotate(magnitude * random.choice([-1, 1])),
  211. "color": lambda img, magnitude: ImageEnhance.Color(img).enhance(1 + magnitude * random.choice([-1, 1])),
  212. "posterize": lambda img, magnitude: ImageOps.posterize(img, magnitude),
  213. "solarize": lambda img, magnitude: ImageOps.solarize(img, magnitude),
  214. "contrast": lambda img, magnitude: ImageEnhance.Contrast(img).enhance(
  215. 1 + magnitude * random.choice([-1, 1])),
  216. "sharpness": lambda img, magnitude: ImageEnhance.Sharpness(img).enhance(
  217. 1 + magnitude * random.choice([-1, 1])),
  218. "brightness": lambda img, magnitude: ImageEnhance.Brightness(img).enhance(
  219. 1 + magnitude * random.choice([-1, 1])),
  220. "autocontrast": lambda img, magnitude: ImageOps.autocontrast(img),
  221. "equalize": lambda img, magnitude: ImageOps.equalize(img),
  222. "invert": lambda img, magnitude: ImageOps.invert(img)
  223. }
  224. self.p1 = p1
  225. self.operation1 = func[operation1]
  226. self.magnitude1 = ranges[operation1][magnitude_idx1]
  227. self.p2 = p2
  228. self.operation2 = func[operation2]
  229. self.magnitude2 = ranges[operation2][magnitude_idx2]
  230. def __call__(self, img):
  231. if random.random() < self.p1:
  232. img = self.operation1(img, self.magnitude1)
  233. if random.random() < self.p2:
  234. img = self.operation2(img, self.magnitude2)
  235. return img