op_helper.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. # Copyright (c) 2019 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 file contains helper methods for BBOX processing
  15. from __future__ import absolute_import
  16. from __future__ import division
  17. from __future__ import print_function
  18. import numpy as np
  19. import random
  20. import math
  21. import cv2
  22. def meet_emit_constraint(src_bbox, sample_bbox):
  23. center_x = (src_bbox[2] + src_bbox[0]) / 2
  24. center_y = (src_bbox[3] + src_bbox[1]) / 2
  25. if center_x >= sample_bbox[0] and \
  26. center_x <= sample_bbox[2] and \
  27. center_y >= sample_bbox[1] and \
  28. center_y <= sample_bbox[3]:
  29. return True
  30. return False
  31. def clip_bbox(src_bbox):
  32. src_bbox[0] = max(min(src_bbox[0], 1.0), 0.0)
  33. src_bbox[1] = max(min(src_bbox[1], 1.0), 0.0)
  34. src_bbox[2] = max(min(src_bbox[2], 1.0), 0.0)
  35. src_bbox[3] = max(min(src_bbox[3], 1.0), 0.0)
  36. return src_bbox
  37. def bbox_area(src_bbox):
  38. if src_bbox[2] < src_bbox[0] or src_bbox[3] < src_bbox[1]:
  39. return 0.
  40. else:
  41. width = src_bbox[2] - src_bbox[0]
  42. height = src_bbox[3] - src_bbox[1]
  43. return width * height
  44. def is_overlap(object_bbox, sample_bbox):
  45. if object_bbox[0] >= sample_bbox[2] or \
  46. object_bbox[2] <= sample_bbox[0] or \
  47. object_bbox[1] >= sample_bbox[3] or \
  48. object_bbox[3] <= sample_bbox[1]:
  49. return False
  50. else:
  51. return True
  52. def filter_and_process(sample_bbox, bboxes, labels, scores=None,
  53. keypoints=None):
  54. new_bboxes = []
  55. new_labels = []
  56. new_scores = []
  57. new_keypoints = []
  58. new_kp_ignore = []
  59. for i in range(len(bboxes)):
  60. new_bbox = [0, 0, 0, 0]
  61. obj_bbox = [bboxes[i][0], bboxes[i][1], bboxes[i][2], bboxes[i][3]]
  62. if not meet_emit_constraint(obj_bbox, sample_bbox):
  63. continue
  64. if not is_overlap(obj_bbox, sample_bbox):
  65. continue
  66. sample_width = sample_bbox[2] - sample_bbox[0]
  67. sample_height = sample_bbox[3] - sample_bbox[1]
  68. new_bbox[0] = (obj_bbox[0] - sample_bbox[0]) / sample_width
  69. new_bbox[1] = (obj_bbox[1] - sample_bbox[1]) / sample_height
  70. new_bbox[2] = (obj_bbox[2] - sample_bbox[0]) / sample_width
  71. new_bbox[3] = (obj_bbox[3] - sample_bbox[1]) / sample_height
  72. new_bbox = clip_bbox(new_bbox)
  73. if bbox_area(new_bbox) > 0:
  74. new_bboxes.append(new_bbox)
  75. new_labels.append([labels[i][0]])
  76. if scores is not None:
  77. new_scores.append([scores[i][0]])
  78. if keypoints is not None:
  79. sample_keypoint = keypoints[0][i]
  80. for j in range(len(sample_keypoint)):
  81. kp_len = sample_height if j % 2 else sample_width
  82. sample_coord = sample_bbox[1] if j % 2 else sample_bbox[0]
  83. sample_keypoint[j] = (
  84. sample_keypoint[j] - sample_coord) / kp_len
  85. sample_keypoint[j] = max(min(sample_keypoint[j], 1.0), 0.0)
  86. new_keypoints.append(sample_keypoint)
  87. new_kp_ignore.append(keypoints[1][i])
  88. bboxes = np.array(new_bboxes)
  89. labels = np.array(new_labels)
  90. scores = np.array(new_scores)
  91. if keypoints is not None:
  92. keypoints = np.array(new_keypoints)
  93. new_kp_ignore = np.array(new_kp_ignore)
  94. return bboxes, labels, scores, (keypoints, new_kp_ignore)
  95. return bboxes, labels, scores
  96. def bbox_area_sampling(bboxes, labels, scores, target_size, min_size):
  97. new_bboxes = []
  98. new_labels = []
  99. new_scores = []
  100. for i, bbox in enumerate(bboxes):
  101. w = float((bbox[2] - bbox[0]) * target_size)
  102. h = float((bbox[3] - bbox[1]) * target_size)
  103. if w * h < float(min_size * min_size):
  104. continue
  105. else:
  106. new_bboxes.append(bbox)
  107. new_labels.append(labels[i])
  108. if scores is not None and scores.size != 0:
  109. new_scores.append(scores[i])
  110. bboxes = np.array(new_bboxes)
  111. labels = np.array(new_labels)
  112. scores = np.array(new_scores)
  113. return bboxes, labels, scores
  114. def generate_sample_bbox(sampler):
  115. scale = np.random.uniform(sampler[2], sampler[3])
  116. aspect_ratio = np.random.uniform(sampler[4], sampler[5])
  117. aspect_ratio = max(aspect_ratio, (scale**2.0))
  118. aspect_ratio = min(aspect_ratio, 1 / (scale**2.0))
  119. bbox_width = scale * (aspect_ratio**0.5)
  120. bbox_height = scale / (aspect_ratio**0.5)
  121. xmin_bound = 1 - bbox_width
  122. ymin_bound = 1 - bbox_height
  123. xmin = np.random.uniform(0, xmin_bound)
  124. ymin = np.random.uniform(0, ymin_bound)
  125. xmax = xmin + bbox_width
  126. ymax = ymin + bbox_height
  127. sampled_bbox = [xmin, ymin, xmax, ymax]
  128. return sampled_bbox
  129. def generate_sample_bbox_square(sampler, image_width, image_height):
  130. scale = np.random.uniform(sampler[2], sampler[3])
  131. aspect_ratio = np.random.uniform(sampler[4], sampler[5])
  132. aspect_ratio = max(aspect_ratio, (scale**2.0))
  133. aspect_ratio = min(aspect_ratio, 1 / (scale**2.0))
  134. bbox_width = scale * (aspect_ratio**0.5)
  135. bbox_height = scale / (aspect_ratio**0.5)
  136. if image_height < image_width:
  137. bbox_width = bbox_height * image_height / image_width
  138. else:
  139. bbox_height = bbox_width * image_width / image_height
  140. xmin_bound = 1 - bbox_width
  141. ymin_bound = 1 - bbox_height
  142. xmin = np.random.uniform(0, xmin_bound)
  143. ymin = np.random.uniform(0, ymin_bound)
  144. xmax = xmin + bbox_width
  145. ymax = ymin + bbox_height
  146. sampled_bbox = [xmin, ymin, xmax, ymax]
  147. return sampled_bbox
  148. def data_anchor_sampling(bbox_labels, image_width, image_height, scale_array,
  149. resize_width):
  150. num_gt = len(bbox_labels)
  151. # np.random.randint range: [low, high)
  152. rand_idx = np.random.randint(0, num_gt) if num_gt != 0 else 0
  153. if num_gt != 0:
  154. norm_xmin = bbox_labels[rand_idx][0]
  155. norm_ymin = bbox_labels[rand_idx][1]
  156. norm_xmax = bbox_labels[rand_idx][2]
  157. norm_ymax = bbox_labels[rand_idx][3]
  158. xmin = norm_xmin * image_width
  159. ymin = norm_ymin * image_height
  160. wid = image_width * (norm_xmax - norm_xmin)
  161. hei = image_height * (norm_ymax - norm_ymin)
  162. range_size = 0
  163. area = wid * hei
  164. for scale_ind in range(0, len(scale_array) - 1):
  165. if area > scale_array[scale_ind] ** 2 and area < \
  166. scale_array[scale_ind + 1] ** 2:
  167. range_size = scale_ind + 1
  168. break
  169. if area > scale_array[len(scale_array) - 2]**2:
  170. range_size = len(scale_array) - 2
  171. scale_choose = 0.0
  172. if range_size == 0:
  173. rand_idx_size = 0
  174. else:
  175. # np.random.randint range: [low, high)
  176. rng_rand_size = np.random.randint(0, range_size + 1)
  177. rand_idx_size = rng_rand_size % (range_size + 1)
  178. if rand_idx_size == range_size:
  179. min_resize_val = scale_array[rand_idx_size] / 2.0
  180. max_resize_val = min(2.0 * scale_array[rand_idx_size],
  181. 2 * math.sqrt(wid * hei))
  182. scale_choose = random.uniform(min_resize_val, max_resize_val)
  183. else:
  184. min_resize_val = scale_array[rand_idx_size] / 2.0
  185. max_resize_val = 2.0 * scale_array[rand_idx_size]
  186. scale_choose = random.uniform(min_resize_val, max_resize_val)
  187. sample_bbox_size = wid * resize_width / scale_choose
  188. w_off_orig = 0.0
  189. h_off_orig = 0.0
  190. if sample_bbox_size < max(image_height, image_width):
  191. if wid <= sample_bbox_size:
  192. w_off_orig = np.random.uniform(xmin + wid - sample_bbox_size,
  193. xmin)
  194. else:
  195. w_off_orig = np.random.uniform(xmin,
  196. xmin + wid - sample_bbox_size)
  197. if hei <= sample_bbox_size:
  198. h_off_orig = np.random.uniform(ymin + hei - sample_bbox_size,
  199. ymin)
  200. else:
  201. h_off_orig = np.random.uniform(ymin,
  202. ymin + hei - sample_bbox_size)
  203. else:
  204. w_off_orig = np.random.uniform(image_width - sample_bbox_size, 0.0)
  205. h_off_orig = np.random.uniform(image_height - sample_bbox_size, 0.0)
  206. w_off_orig = math.floor(w_off_orig)
  207. h_off_orig = math.floor(h_off_orig)
  208. # Figure out top left coordinates.
  209. w_off = float(w_off_orig / image_width)
  210. h_off = float(h_off_orig / image_height)
  211. sampled_bbox = [
  212. w_off, h_off, w_off + float(sample_bbox_size / image_width),
  213. h_off + float(sample_bbox_size / image_height)
  214. ]
  215. return sampled_bbox
  216. else:
  217. return 0
  218. def jaccard_overlap(sample_bbox, object_bbox):
  219. if sample_bbox[0] >= object_bbox[2] or \
  220. sample_bbox[2] <= object_bbox[0] or \
  221. sample_bbox[1] >= object_bbox[3] or \
  222. sample_bbox[3] <= object_bbox[1]:
  223. return 0
  224. intersect_xmin = max(sample_bbox[0], object_bbox[0])
  225. intersect_ymin = max(sample_bbox[1], object_bbox[1])
  226. intersect_xmax = min(sample_bbox[2], object_bbox[2])
  227. intersect_ymax = min(sample_bbox[3], object_bbox[3])
  228. intersect_size = (intersect_xmax - intersect_xmin) * (
  229. intersect_ymax - intersect_ymin)
  230. sample_bbox_size = bbox_area(sample_bbox)
  231. object_bbox_size = bbox_area(object_bbox)
  232. overlap = intersect_size / (
  233. sample_bbox_size + object_bbox_size - intersect_size)
  234. return overlap
  235. def intersect_bbox(bbox1, bbox2):
  236. if bbox2[0] > bbox1[2] or bbox2[2] < bbox1[0] or \
  237. bbox2[1] > bbox1[3] or bbox2[3] < bbox1[1]:
  238. intersection_box = [0.0, 0.0, 0.0, 0.0]
  239. else:
  240. intersection_box = [
  241. max(bbox1[0], bbox2[0]), max(bbox1[1], bbox2[1]),
  242. min(bbox1[2], bbox2[2]), min(bbox1[3], bbox2[3])
  243. ]
  244. return intersection_box
  245. def bbox_coverage(bbox1, bbox2):
  246. inter_box = intersect_bbox(bbox1, bbox2)
  247. intersect_size = bbox_area(inter_box)
  248. if intersect_size > 0:
  249. bbox1_size = bbox_area(bbox1)
  250. return intersect_size / bbox1_size
  251. else:
  252. return 0.
  253. def satisfy_sample_constraint(sampler,
  254. sample_bbox,
  255. gt_bboxes,
  256. satisfy_all=False):
  257. if sampler[6] == 0 and sampler[7] == 0:
  258. return True
  259. satisfied = []
  260. for i in range(len(gt_bboxes)):
  261. object_bbox = [
  262. gt_bboxes[i][0], gt_bboxes[i][1], gt_bboxes[i][2], gt_bboxes[i][3]
  263. ]
  264. overlap = jaccard_overlap(sample_bbox, object_bbox)
  265. if sampler[6] != 0 and \
  266. overlap < sampler[6]:
  267. satisfied.append(False)
  268. continue
  269. if sampler[7] != 0 and \
  270. overlap > sampler[7]:
  271. satisfied.append(False)
  272. continue
  273. satisfied.append(True)
  274. if not satisfy_all:
  275. return True
  276. if satisfy_all:
  277. return np.all(satisfied)
  278. else:
  279. return False
  280. def satisfy_sample_constraint_coverage(sampler, sample_bbox, gt_bboxes):
  281. if sampler[6] == 0 and sampler[7] == 0:
  282. has_jaccard_overlap = False
  283. else:
  284. has_jaccard_overlap = True
  285. if sampler[8] == 0 and sampler[9] == 0:
  286. has_object_coverage = False
  287. else:
  288. has_object_coverage = True
  289. if not has_jaccard_overlap and not has_object_coverage:
  290. return True
  291. found = False
  292. for i in range(len(gt_bboxes)):
  293. object_bbox = [
  294. gt_bboxes[i][0], gt_bboxes[i][1], gt_bboxes[i][2], gt_bboxes[i][3]
  295. ]
  296. if has_jaccard_overlap:
  297. overlap = jaccard_overlap(sample_bbox, object_bbox)
  298. if sampler[6] != 0 and \
  299. overlap < sampler[6]:
  300. continue
  301. if sampler[7] != 0 and \
  302. overlap > sampler[7]:
  303. continue
  304. found = True
  305. if has_object_coverage:
  306. object_coverage = bbox_coverage(object_bbox, sample_bbox)
  307. if sampler[8] != 0 and \
  308. object_coverage < sampler[8]:
  309. continue
  310. if sampler[9] != 0 and \
  311. object_coverage > sampler[9]:
  312. continue
  313. found = True
  314. if found:
  315. return True
  316. return found
  317. def crop_image_sampling(img, sample_bbox, image_width, image_height,
  318. target_size):
  319. # no clipping here
  320. xmin = int(sample_bbox[0] * image_width)
  321. xmax = int(sample_bbox[2] * image_width)
  322. ymin = int(sample_bbox[1] * image_height)
  323. ymax = int(sample_bbox[3] * image_height)
  324. w_off = xmin
  325. h_off = ymin
  326. width = xmax - xmin
  327. height = ymax - ymin
  328. cross_xmin = max(0.0, float(w_off))
  329. cross_ymin = max(0.0, float(h_off))
  330. cross_xmax = min(float(w_off + width - 1.0), float(image_width))
  331. cross_ymax = min(float(h_off + height - 1.0), float(image_height))
  332. cross_width = cross_xmax - cross_xmin
  333. cross_height = cross_ymax - cross_ymin
  334. roi_xmin = 0 if w_off >= 0 else abs(w_off)
  335. roi_ymin = 0 if h_off >= 0 else abs(h_off)
  336. roi_width = cross_width
  337. roi_height = cross_height
  338. roi_y1 = int(roi_ymin)
  339. roi_y2 = int(roi_ymin + roi_height)
  340. roi_x1 = int(roi_xmin)
  341. roi_x2 = int(roi_xmin + roi_width)
  342. cross_y1 = int(cross_ymin)
  343. cross_y2 = int(cross_ymin + cross_height)
  344. cross_x1 = int(cross_xmin)
  345. cross_x2 = int(cross_xmin + cross_width)
  346. sample_img = np.zeros((height, width, 3))
  347. sample_img[roi_y1: roi_y2, roi_x1: roi_x2] = \
  348. img[cross_y1: cross_y2, cross_x1: cross_x2]
  349. sample_img = cv2.resize(
  350. sample_img, (target_size, target_size), interpolation=cv2.INTER_AREA)
  351. return sample_img
  352. def is_poly(segm):
  353. assert isinstance(segm, (list, dict)), \
  354. "Invalid segm type: {}".format(type(segm))
  355. return isinstance(segm, list)
  356. def gaussian_radius(bbox_size, min_overlap):
  357. height, width = bbox_size
  358. a1 = 1
  359. b1 = (height + width)
  360. c1 = width * height * (1 - min_overlap) / (1 + min_overlap)
  361. sq1 = np.sqrt(b1**2 - 4 * a1 * c1)
  362. radius1 = (b1 + sq1) / (2 * a1)
  363. a2 = 4
  364. b2 = 2 * (height + width)
  365. c2 = (1 - min_overlap) * width * height
  366. sq2 = np.sqrt(b2**2 - 4 * a2 * c2)
  367. radius2 = (b2 + sq2) / 2
  368. a3 = 4 * min_overlap
  369. b3 = -2 * min_overlap * (height + width)
  370. c3 = (min_overlap - 1) * width * height
  371. sq3 = np.sqrt(b3**2 - 4 * a3 * c3)
  372. radius3 = (b3 + sq3) / 2
  373. return min(radius1, radius2, radius3)
  374. def draw_gaussian(heatmap, center, radius, k=1, delte=6):
  375. diameter = 2 * radius + 1
  376. sigma = diameter / delte
  377. gaussian = gaussian2D((diameter, diameter), sigma_x=sigma, sigma_y=sigma)
  378. x, y = center
  379. height, width = heatmap.shape[0:2]
  380. left, right = min(x, radius), min(width - x, radius + 1)
  381. top, bottom = min(y, radius), min(height - y, radius + 1)
  382. masked_heatmap = heatmap[y - top:y + bottom, x - left:x + right]
  383. masked_gaussian = gaussian[radius - top:radius + bottom, radius - left:
  384. radius + right]
  385. np.maximum(masked_heatmap, masked_gaussian * k, out=masked_heatmap)
  386. def gaussian2D(shape, sigma_x=1, sigma_y=1):
  387. m, n = [(ss - 1.) / 2. for ss in shape]
  388. y, x = np.ogrid[-m:m + 1, -n:n + 1]
  389. h = np.exp(-(x * x / (2 * sigma_x * sigma_x) + y * y / (2 * sigma_y *
  390. sigma_y)))
  391. h[h < np.finfo(h.dtype).eps * h.max()] = 0
  392. return h
  393. def draw_umich_gaussian(heatmap, center, radius, k=1):
  394. """
  395. draw_umich_gaussian, refer to https://github.com/xingyizhou/CenterNet/blob/master/src/lib/utils/image.py#L126
  396. """
  397. diameter = 2 * radius + 1
  398. gaussian = gaussian2D(
  399. (diameter, diameter), sigma_x=diameter / 6, sigma_y=diameter / 6)
  400. x, y = int(center[0]), int(center[1])
  401. height, width = heatmap.shape[0:2]
  402. left, right = min(x, radius), min(width - x, radius + 1)
  403. top, bottom = min(y, radius), min(height - y, radius + 1)
  404. masked_heatmap = heatmap[y - top:y + bottom, x - left:x + right]
  405. masked_gaussian = gaussian[radius - top:radius + bottom, radius - left:
  406. radius + right]
  407. if min(masked_gaussian.shape) > 0 and min(masked_heatmap.shape) > 0:
  408. np.maximum(masked_heatmap, masked_gaussian * k, out=masked_heatmap)
  409. return heatmap
  410. def get_border(border, size):
  411. i = 1
  412. while size - border // i <= border // i:
  413. i *= 2
  414. return border // i