grid.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/akuxcw/GridMask
  15. import numpy as np
  16. from PIL import Image
  17. import pdb
  18. # curr
  19. CURR_EPOCH = 0
  20. # epoch for the prob to be the upper limit
  21. NUM_EPOCHS = 240
  22. class GridMask(object):
  23. def __init__(self, d1=96, d2=224, rotate=1, ratio=0.5, mode=0, prob=1.):
  24. self.d1 = d1
  25. self.d2 = d2
  26. self.rotate = rotate
  27. self.ratio = ratio
  28. self.mode = mode
  29. self.st_prob = prob
  30. self.prob = prob
  31. self.last_prob = -1
  32. def set_prob(self):
  33. global CURR_EPOCH
  34. global NUM_EPOCHS
  35. self.prob = self.st_prob * min(1, 1.0 * CURR_EPOCH / NUM_EPOCHS)
  36. def __call__(self, img):
  37. self.set_prob()
  38. if abs(self.last_prob - self.prob) > 1e-10:
  39. global CURR_EPOCH
  40. global NUM_EPOCHS
  41. print(
  42. "self.prob is updated, self.prob={}, CURR_EPOCH: {}, NUM_EPOCHS: {}".
  43. format(self.prob, CURR_EPOCH, NUM_EPOCHS))
  44. self.last_prob = self.prob
  45. # print("CURR_EPOCH: {}, NUM_EPOCHS: {}, self.prob is set as: {}".format(CURR_EPOCH, NUM_EPOCHS, self.prob) )
  46. if np.random.rand() > self.prob:
  47. return img
  48. _, h, w = img.shape
  49. hh = int(1.5 * h)
  50. ww = int(1.5 * w)
  51. d = np.random.randint(self.d1, self.d2)
  52. #d = self.d
  53. self.l = int(d * self.ratio + 0.5)
  54. mask = np.ones((hh, ww), np.float32)
  55. st_h = np.random.randint(d)
  56. st_w = np.random.randint(d)
  57. for i in range(-1, hh // d + 1):
  58. s = d * i + st_h
  59. t = s + self.l
  60. s = max(min(s, hh), 0)
  61. t = max(min(t, hh), 0)
  62. mask[s:t, :] *= 0
  63. for i in range(-1, ww // d + 1):
  64. s = d * i + st_w
  65. t = s + self.l
  66. s = max(min(s, ww), 0)
  67. t = max(min(t, ww), 0)
  68. mask[:, s:t] *= 0
  69. r = np.random.randint(self.rotate)
  70. mask = Image.fromarray(np.uint8(mask))
  71. mask = mask.rotate(r)
  72. mask = np.asarray(mask)
  73. mask = mask[(hh - h) // 2:(hh - h) // 2 + h, (ww - w) // 2:(ww - w) //
  74. 2 + w]
  75. if self.mode == 1:
  76. mask = 1 - mask
  77. mask = np.expand_dims(mask, axis=0)
  78. img = (img * mask).astype(img.dtype)
  79. return img