ops.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.astype('float32')
  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_dims = im.ndim
  38. im = cv2.resize(
  39. im, (resized_width, resized_height), interpolation=interpolation)
  40. if im_dims >= 3 and im.ndim < 3:
  41. im = np.expand_dims(im, axis=-1)
  42. return im
  43. def resize(im, target_size=608, interp=cv2.INTER_LINEAR):
  44. if isinstance(target_size, list) or isinstance(target_size, tuple):
  45. w = target_size[0]
  46. h = target_size[1]
  47. else:
  48. w = target_size
  49. h = target_size
  50. im = cv2.resize(im, (w, h), interpolation=interp)
  51. if im.ndim < 3:
  52. im = np.expand_dims(im, axis=-1)
  53. return im
  54. def random_crop(im,
  55. crop_size=224,
  56. lower_scale=0.08,
  57. lower_ratio=3. / 4,
  58. upper_ratio=4. / 3):
  59. scale = [lower_scale, 1.0]
  60. ratio = [lower_ratio, upper_ratio]
  61. aspect_ratio = math.sqrt(np.random.uniform(*ratio))
  62. w = 1. * aspect_ratio
  63. h = 1. / aspect_ratio
  64. bound = min((float(im.shape[0]) / im.shape[1]) / (h**2),
  65. (float(im.shape[1]) / im.shape[0]) / (w**2))
  66. scale_max = min(scale[1], bound)
  67. scale_min = min(scale[0], bound)
  68. target_area = im.shape[0] * im.shape[1] * np.random.uniform(scale_min,
  69. scale_max)
  70. target_size = math.sqrt(target_area)
  71. w = int(target_size * w)
  72. h = int(target_size * h)
  73. i = np.random.randint(0, im.shape[0] - h + 1)
  74. j = np.random.randint(0, im.shape[1] - w + 1)
  75. im = im[i:i + h, j:j + w, :]
  76. im = cv2.resize(im, (crop_size, crop_size))
  77. return im
  78. def center_crop(im, crop_size=224):
  79. height, width = im.shape[:2]
  80. w_start = (width - crop_size) // 2
  81. h_start = (height - crop_size) // 2
  82. w_end = w_start + crop_size
  83. h_end = h_start + crop_size
  84. im = im[h_start:h_end, w_start:w_end, :]
  85. return im
  86. def horizontal_flip(im):
  87. if len(im.shape) == 3:
  88. im = im[:, ::-1, :]
  89. elif len(im.shape) == 2:
  90. im = im[:, ::-1]
  91. return im
  92. def vertical_flip(im):
  93. if len(im.shape) == 3:
  94. im = im[::-1, :, :]
  95. elif len(im.shape) == 2:
  96. im = im[::-1, :]
  97. return im
  98. def bgr2rgb(im):
  99. return im[:, :, ::-1]
  100. def hue(im, hue_lower, hue_upper, is_rgb=False, dtype=np.uint8):
  101. delta = np.random.uniform(hue_lower, hue_upper)
  102. if is_rgb:
  103. im = cv2.cvtColor(im, cv2.COLOR_RGB2HSV)
  104. else:
  105. im = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
  106. im[:, :, 0] = im[:, :, 0] + delta
  107. im[:, :, 0] = np.clip(im[:, :, 0], 0, 360.)
  108. if is_rgb:
  109. im = cv2.cvtColor(im, cv2.COLOR_HSV2RGB)
  110. else:
  111. im = cv2.cvtColor(im, cv2.COLOR_HSV2BGR)
  112. if dtype == np.uint8:
  113. im = np.clip(im, 0., 255.)
  114. elif dtype == np.uint16:
  115. im = np.clip(im, 0., 65535.)
  116. elif dtype == np.float32:
  117. im = np.clip(im, 0., 1.)
  118. return im
  119. def saturation(im,
  120. saturation_lower,
  121. saturation_upper,
  122. is_rgb=False,
  123. dtype=np.uint8):
  124. if is_rgb:
  125. gray_scale = np.array([[[0.299, 0.587, 0.114]]], dtype=np.float32)
  126. else:
  127. gray_scale = np.array([[[0.114, 0.587, 0.299]]], dtype=np.float32)
  128. delta = np.random.uniform(saturation_lower, saturation_upper)
  129. gray = im * gray_scale
  130. gray = gray.sum(axis=2, keepdims=True)
  131. gray *= (1.0 - delta)
  132. im *= delta
  133. im += gray
  134. if dtype == np.uint8:
  135. im = np.clip(im, 0., 255.)
  136. elif dtype == np.uint16:
  137. im = np.clip(im, 0., 65535.)
  138. elif dtype == np.float32:
  139. im = np.clip(im, 0., 1.)
  140. return im
  141. def contrast(im, contrast_lower, contrast_upper, dtype=np.uint8):
  142. delta = np.random.uniform(contrast_lower, contrast_upper)
  143. im_mean = im.mean() + 0.5
  144. im1 = np.full_like(im, im_mean)
  145. im *= delta
  146. im += im1 * (1 - delta)
  147. if dtype == np.uint8:
  148. im = np.clip(im, 0., 255.)
  149. elif dtype == np.uint16:
  150. im = np.clip(im, 0., 65535.)
  151. elif dtype == np.float32:
  152. im = np.clip(im, 0., 1.)
  153. return im
  154. def brightness(im, brightness_lower, brightness_upper, dtype=np.uint8):
  155. delta = np.random.uniform(brightness_lower, brightness_upper)
  156. im *= delta
  157. if dtype == np.uint8:
  158. im = np.clip(im, 0., 255.)
  159. elif dtype == np.uint16:
  160. im = np.clip(im, 0., 65535.)
  161. elif dtype == np.float32:
  162. im = np.clip(im, 0., 1.)
  163. return im
  164. def rotate(im, rotate_lower, rotate_upper):
  165. rotate_delta = np.random.uniform(rotate_lower, rotate_upper)
  166. im = im.rotate(int(rotate_delta))
  167. return im
  168. def resize_padding(im, max_side_len=2400):
  169. '''
  170. resize image to a size multiple of 32 which is required by the network
  171. :param im: the resized image
  172. :param max_side_len: limit of max image size to avoid out of memory in gpu
  173. :return: the resized image and the resize ratio
  174. '''
  175. h, w, _ = im.shape
  176. resize_w = w
  177. resize_h = h
  178. # limit the max side
  179. if max(resize_h, resize_w) > max_side_len:
  180. ratio = float(
  181. max_side_len) / resize_h if resize_h > resize_w else float(
  182. max_side_len) / resize_w
  183. else:
  184. ratio = 1.
  185. resize_h = int(resize_h * ratio)
  186. resize_w = int(resize_w * ratio)
  187. resize_h = resize_h if resize_h % 32 == 0 else (resize_h // 32 - 1) * 32
  188. resize_w = resize_w if resize_w % 32 == 0 else (resize_w // 32 - 1) * 32
  189. resize_h = max(32, resize_h)
  190. resize_w = max(32, resize_w)
  191. im = cv2.resize(im, (int(resize_w), int(resize_h)))
  192. #im = cv2.resize(im, (512, 512))
  193. ratio_h = resize_h / float(h)
  194. ratio_w = resize_w / float(w)
  195. _ratio = np.array([ratio_h, ratio_w]).reshape(-1, 2)
  196. return im, _ratio