ops.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. import cv2
  15. import math
  16. import numpy as np
  17. from PIL import Image, ImageEnhance
  18. def normalize(im, mean, std, min_value=[0, 0, 0], max_value=[255, 255, 255]):
  19. # Rescaling (min-max normalization)
  20. range_value = [max_value[i] - min_value[i] for i in range(len(max_value))]
  21. im = (im - min_value) / range_value
  22. # Standardization (Z-score Normalization)
  23. im -= mean
  24. im /= std
  25. return im
  26. def permute(im, to_bgr=False):
  27. im = np.swapaxes(im, 1, 2)
  28. im = np.swapaxes(im, 1, 0)
  29. if to_bgr:
  30. im = im[[2, 1, 0], :, :]
  31. return im
  32. def resize_long(im, long_size=224, interpolation=cv2.INTER_LINEAR):
  33. value = max(im.shape[0], im.shape[1])
  34. scale = float(long_size) / float(value)
  35. resized_width = int(round(im.shape[1] * scale))
  36. resized_height = int(round(im.shape[0] * scale))
  37. im = cv2.resize(
  38. im, (resized_width, resized_height), interpolation=interpolation)
  39. return im
  40. def resize(im, target_size=608, interp=cv2.INTER_LINEAR):
  41. if isinstance(target_size, list) or isinstance(target_size, tuple):
  42. w = target_size[0]
  43. h = target_size[1]
  44. else:
  45. w = target_size
  46. h = target_size
  47. im = cv2.resize(im, (w, h), interpolation=interp)
  48. return im
  49. def random_crop(im,
  50. crop_size=224,
  51. lower_scale=0.08,
  52. lower_ratio=3. / 4,
  53. upper_ratio=4. / 3):
  54. scale = [lower_scale, 1.0]
  55. ratio = [lower_ratio, upper_ratio]
  56. aspect_ratio = math.sqrt(np.random.uniform(*ratio))
  57. w = 1. * aspect_ratio
  58. h = 1. / aspect_ratio
  59. bound = min((float(im.shape[0]) / im.shape[1]) / (h**2),
  60. (float(im.shape[1]) / im.shape[0]) / (w**2))
  61. scale_max = min(scale[1], bound)
  62. scale_min = min(scale[0], bound)
  63. target_area = im.shape[0] * im.shape[1] * np.random.uniform(scale_min,
  64. scale_max)
  65. target_size = math.sqrt(target_area)
  66. w = int(target_size * w)
  67. h = int(target_size * h)
  68. i = np.random.randint(0, im.shape[0] - h + 1)
  69. j = np.random.randint(0, im.shape[1] - w + 1)
  70. im = im[i:i + h, j:j + w, :]
  71. im = cv2.resize(im, (crop_size, crop_size))
  72. return im
  73. def center_crop(im, crop_size=224):
  74. height, width = im.shape[:2]
  75. w_start = (width - crop_size) // 2
  76. h_start = (height - crop_size) // 2
  77. w_end = w_start + crop_size
  78. h_end = h_start + crop_size
  79. im = im[h_start:h_end, w_start:w_end, :]
  80. return im
  81. def horizontal_flip(im):
  82. if len(im.shape) == 3:
  83. im = im[:, ::-1, :]
  84. elif len(im.shape) == 2:
  85. im = im[:, ::-1]
  86. return im
  87. def vertical_flip(im):
  88. if len(im.shape) == 3:
  89. im = im[::-1, :, :]
  90. elif len(im.shape) == 2:
  91. im = im[::-1, :]
  92. return im
  93. def bgr2rgb(im):
  94. return im[:, :, ::-1]
  95. def hue(im, hue_lower, hue_upper):
  96. delta = np.random.uniform(hue_lower, hue_upper)
  97. u = np.cos(delta * np.pi)
  98. w = np.sin(delta * np.pi)
  99. bt = np.array([[1.0, 0.0, 0.0], [0.0, u, -w], [0.0, w, u]])
  100. tyiq = np.array([[0.299, 0.587, 0.114], [0.596, -0.274, -0.321],
  101. [0.211, -0.523, 0.311]])
  102. ityiq = np.array([[1.0, 0.956, 0.621], [1.0, -0.272, -0.647],
  103. [1.0, -1.107, 1.705]])
  104. t = np.dot(np.dot(ityiq, bt), tyiq).T
  105. im = np.dot(im, t)
  106. return im
  107. def saturation(im, saturation_lower, saturation_upper):
  108. delta = np.random.uniform(saturation_lower, saturation_upper)
  109. gray = im * np.array([[[0.299, 0.587, 0.114]]], dtype=np.float32)
  110. gray = gray.sum(axis=2, keepdims=True)
  111. gray *= (1.0 - delta)
  112. im *= delta
  113. im += gray
  114. return im
  115. def contrast(im, contrast_lower, contrast_upper):
  116. delta = np.random.uniform(contrast_lower, contrast_upper)
  117. im *= delta
  118. return im
  119. def brightness(im, brightness_lower, brightness_upper):
  120. delta = np.random.uniform(brightness_lower, brightness_upper)
  121. im += delta
  122. return im
  123. def rotate(im, rotate_lower, rotate_upper):
  124. rotate_delta = np.random.uniform(rotate_lower, rotate_upper)
  125. im = im.rotate(int(rotate_delta))
  126. return im
  127. def resize_padding(im, max_side_len=2400):
  128. '''
  129. resize image to a size multiple of 32 which is required by the network
  130. :param im: the resized image
  131. :param max_side_len: limit of max image size to avoid out of memory in gpu
  132. :return: the resized image and the resize ratio
  133. '''
  134. h, w, _ = im.shape
  135. resize_w = w
  136. resize_h = h
  137. # limit the max side
  138. if max(resize_h, resize_w) > max_side_len:
  139. ratio = float(
  140. max_side_len) / resize_h if resize_h > resize_w else float(
  141. max_side_len) / resize_w
  142. else:
  143. ratio = 1.
  144. resize_h = int(resize_h * ratio)
  145. resize_w = int(resize_w * ratio)
  146. resize_h = resize_h if resize_h % 32 == 0 else (resize_h // 32 - 1) * 32
  147. resize_w = resize_w if resize_w % 32 == 0 else (resize_w // 32 - 1) * 32
  148. resize_h = max(32, resize_h)
  149. resize_w = max(32, resize_w)
  150. im = cv2.resize(im, (int(resize_w), int(resize_h)))
  151. #im = cv2.resize(im, (512, 512))
  152. ratio_h = resize_h / float(h)
  153. ratio_w = resize_w / float(w)
  154. _ratio = np.array([ratio_h, ratio_w]).reshape(-1, 2)
  155. return im, _ratio