gridmask_utils.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (c) 2020 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. # The code is based on:
  15. # https://github.com/dvlab-research/GridMask/blob/master/detection_grid/maskrcnn_benchmark/data/transforms/grid.py
  16. from __future__ import absolute_import
  17. from __future__ import print_function
  18. from __future__ import division
  19. import numpy as np
  20. from PIL import Image
  21. class Gridmask(object):
  22. def __init__(self,
  23. use_h=True,
  24. use_w=True,
  25. rotate=1,
  26. offset=False,
  27. ratio=0.5,
  28. mode=1,
  29. prob=0.7,
  30. upper_iter=360000):
  31. super(Gridmask, self).__init__()
  32. self.use_h = use_h
  33. self.use_w = use_w
  34. self.rotate = rotate
  35. self.offset = offset
  36. self.ratio = ratio
  37. self.mode = mode
  38. self.prob = prob
  39. self.st_prob = prob
  40. self.upper_iter = upper_iter
  41. def __call__(self, x, curr_iter):
  42. self.prob = self.st_prob * min(1, 1.0 * curr_iter / self.upper_iter)
  43. if np.random.rand() > self.prob:
  44. return x
  45. h, w, _ = x.shape
  46. hh = int(1.5 * h)
  47. ww = int(1.5 * w)
  48. d = np.random.randint(2, h)
  49. self.l = min(max(int(d * self.ratio + 0.5), 1), d - 1)
  50. mask = np.ones((hh, ww), np.float32)
  51. st_h = np.random.randint(d)
  52. st_w = np.random.randint(d)
  53. if self.use_h:
  54. for i in range(hh // d):
  55. s = d * i + st_h
  56. t = min(s + self.l, hh)
  57. mask[s:t, :] *= 0
  58. if self.use_w:
  59. for i in range(ww // d):
  60. s = d * i + st_w
  61. t = min(s + self.l, ww)
  62. mask[:, s:t] *= 0
  63. r = np.random.randint(self.rotate)
  64. mask = Image.fromarray(np.uint8(mask))
  65. mask = mask.rotate(r)
  66. mask = np.asarray(mask)
  67. mask = mask[(hh - h) // 2:(hh - h) // 2 + h, (ww - w) // 2:(ww - w) // 2
  68. + w].astype(np.float32)
  69. if self.mode == 1:
  70. mask = 1 - mask
  71. mask = np.expand_dims(mask, axis=-1)
  72. if self.offset:
  73. offset = (2 * (np.random.rand(h, w) - 0.5)).astype(np.float32)
  74. x = (x * mask + offset * (1 - mask)).astype(x.dtype)
  75. else:
  76. x = (x * mask).astype(x.dtype)
  77. return x