annotation_cropper.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. import copy
  15. import math
  16. import random
  17. import numpy as np
  18. from copy import deepcopy
  19. from typing import List, Tuple
  20. from collections import defaultdict
  21. from .chip_box_utils import nms, transform_chip_boxes2image_boxes
  22. from .chip_box_utils import find_chips_to_cover_overlaped_boxes
  23. from .chip_box_utils import transform_chip_box
  24. from .chip_box_utils import intersection_over_box
  25. class AnnoCropper(object):
  26. def __init__(self, image_target_sizes: List[int],
  27. valid_box_ratio_ranges: List[List[float]],
  28. chip_target_size: int, chip_target_stride: int,
  29. use_neg_chip: bool = False,
  30. max_neg_num_per_im: int = 8,
  31. max_per_img: int = -1,
  32. nms_thresh: int = 0.5
  33. ):
  34. """
  35. Generate chips by chip_target_size and chip_target_stride.
  36. These two parameters just like kernel_size and stride in cnn.
  37. Each image has its raw size. After resizing, then get its target size.
  38. The resizing scale = target_size / raw_size.
  39. So are chips of the image.
  40. box_ratio = box_raw_size / image_raw_size = box_target_size / image_target_size
  41. The 'size' above mentioned is the size of long-side of image, box or chip.
  42. :param image_target_sizes: [2000, 1000]
  43. :param valid_box_ratio_ranges: [[-1, 0.1],[0.08, -1]]
  44. :param chip_target_size: 500
  45. :param chip_target_stride: 200
  46. """
  47. self.target_sizes = image_target_sizes
  48. self.valid_box_ratio_ranges = valid_box_ratio_ranges
  49. assert len(self.target_sizes) == len(self.valid_box_ratio_ranges)
  50. self.scale_num = len(self.target_sizes)
  51. self.chip_target_size = chip_target_size # is target size
  52. self.chip_target_stride = chip_target_stride # is target stride
  53. self.use_neg_chip = use_neg_chip
  54. self.max_neg_num_per_im = max_neg_num_per_im
  55. self.max_per_img = max_per_img
  56. self.nms_thresh = nms_thresh
  57. def crop_anno_records(self, records: List[dict]):
  58. """
  59. The main logic:
  60. # foreach record(image):
  61. # foreach scale:
  62. # 1 generate chips by chip size and stride for each scale
  63. # 2 get pos chips
  64. # - validate boxes: current scale; h,w >= 1
  65. # - find pos chips greedily by valid gt boxes in each scale
  66. # - for every valid gt box, find its corresponding pos chips in each scale
  67. # 3 get neg chips
  68. # - If given proposals, find neg boxes in them which are not in pos chips
  69. # - If got neg boxes in last step, we find neg chips and assign neg boxes to neg chips such as 2.
  70. # 4 sample neg chips if too much each image
  71. # transform this image-scale annotations to chips(pos chips&neg chips) annotations
  72. :param records, standard coco_record but with extra key `proposals`(Px4), which are predicted by stage1
  73. model and maybe have neg boxes in them.
  74. :return: new_records, list of dict like
  75. {
  76. 'im_file': 'fake_image1.jpg',
  77. 'im_id': np.array([1]), # new _global_chip_id as im_id
  78. 'h': h, # chip height
  79. 'w': w, # chip width
  80. 'is_crowd': is_crowd, # Nx1 -> Mx1
  81. 'gt_class': gt_class, # Nx1 -> Mx1
  82. 'gt_bbox': gt_bbox, # Nx4 -> Mx4, 4 represents [x1,y1,x2,y2]
  83. 'gt_poly': gt_poly, # [None]xN -> [None]xM
  84. 'chip': [x1, y1, x2, y2] # added
  85. }
  86. Attention:
  87. ------------------------------>x
  88. |
  89. | (x1,y1)------
  90. | | |
  91. | | |
  92. | | |
  93. | | |
  94. | | |
  95. | ----------
  96. | (x2,y2)
  97. |
  98. y
  99. If we use [x1, y1, x2, y2] to represent boxes or chips,
  100. (x1,y1) is the left-top point which is in the box,
  101. but (x2,y2) is the right-bottom point which is not in the box.
  102. So x1 in [0, w-1], x2 in [1, w], y1 in [0, h-1], y2 in [1,h].
  103. And you can use x2-x1 to get width, and you can use image[y1:y2, x1:x2] to get the box area.
  104. """
  105. self.chip_records = []
  106. self._global_chip_id = 1
  107. for r in records:
  108. self._cur_im_pos_chips = [] # element: (chip, boxes_idx), chip is [x1, y1, x2, y2], boxes_ids is List[int]
  109. self._cur_im_neg_chips = [] # element: (chip, neg_box_num)
  110. for scale_i in range(self.scale_num):
  111. self._get_current_scale_parameters(scale_i, r)
  112. # Cx4
  113. chips = self._create_chips(r['h'], r['w'], self._cur_scale)
  114. # # dict: chipid->[box_id, ...]
  115. pos_chip2boxes_idx = self._get_valid_boxes_and_pos_chips(r['gt_bbox'], chips)
  116. # dict: chipid->neg_box_num
  117. neg_chip2box_num = self._get_neg_boxes_and_chips(chips, list(pos_chip2boxes_idx.keys()), r.get('proposals', None))
  118. self._add_to_cur_im_chips(chips, pos_chip2boxes_idx, neg_chip2box_num)
  119. cur_image_records = self._trans_all_chips2annotations(r)
  120. self.chip_records.extend(cur_image_records)
  121. return self.chip_records
  122. def _add_to_cur_im_chips(self, chips, pos_chip2boxes_idx, neg_chip2box_num):
  123. for pos_chipid, boxes_idx in pos_chip2boxes_idx.items():
  124. chip = np.array(chips[pos_chipid]) # copy chips slice
  125. self._cur_im_pos_chips.append((chip, boxes_idx))
  126. if neg_chip2box_num is None:
  127. return
  128. for neg_chipid, neg_box_num in neg_chip2box_num.items():
  129. chip = np.array(chips[neg_chipid])
  130. self._cur_im_neg_chips.append((chip, neg_box_num))
  131. def _trans_all_chips2annotations(self, r):
  132. gt_bbox = r['gt_bbox']
  133. im_file = r['im_file']
  134. is_crowd = r['is_crowd']
  135. gt_class = r['gt_class']
  136. # gt_poly = r['gt_poly'] # [None]xN
  137. # remaining keys: im_id, h, w
  138. chip_records = self._trans_pos_chips2annotations(im_file, gt_bbox, is_crowd, gt_class)
  139. if not self.use_neg_chip:
  140. return chip_records
  141. sampled_neg_chips = self._sample_neg_chips()
  142. neg_chip_records = self._trans_neg_chips2annotations(im_file, sampled_neg_chips)
  143. chip_records.extend(neg_chip_records)
  144. return chip_records
  145. def _trans_pos_chips2annotations(self, im_file, gt_bbox, is_crowd, gt_class):
  146. chip_records = []
  147. for chip, boxes_idx in self._cur_im_pos_chips:
  148. chip_bbox, final_boxes_idx = transform_chip_box(gt_bbox, boxes_idx, chip)
  149. x1, y1, x2, y2 = chip
  150. chip_h = y2 - y1
  151. chip_w = x2 - x1
  152. rec = {
  153. 'im_file': im_file,
  154. 'im_id': np.array([self._global_chip_id]),
  155. 'h': chip_h,
  156. 'w': chip_w,
  157. 'gt_bbox': chip_bbox,
  158. 'is_crowd': is_crowd[final_boxes_idx].copy(),
  159. 'gt_class': gt_class[final_boxes_idx].copy(),
  160. # 'gt_poly': [None] * len(final_boxes_idx),
  161. 'chip': chip
  162. }
  163. self._global_chip_id += 1
  164. chip_records.append(rec)
  165. return chip_records
  166. def _sample_neg_chips(self):
  167. pos_num = len(self._cur_im_pos_chips)
  168. neg_num = len(self._cur_im_neg_chips)
  169. sample_num = min(pos_num + 2, self.max_neg_num_per_im)
  170. assert sample_num >= 1
  171. if neg_num <= sample_num:
  172. return self._cur_im_neg_chips
  173. candidate_num = int(sample_num * 1.5)
  174. candidate_neg_chips = sorted(self._cur_im_neg_chips, key=lambda x: -x[1])[:candidate_num]
  175. random.shuffle(candidate_neg_chips)
  176. sampled_neg_chips = candidate_neg_chips[:sample_num]
  177. return sampled_neg_chips
  178. def _trans_neg_chips2annotations(self, im_file: str, sampled_neg_chips: List[Tuple]):
  179. chip_records = []
  180. for chip, neg_box_num in sampled_neg_chips:
  181. x1, y1, x2, y2 = chip
  182. chip_h = y2 - y1
  183. chip_w = x2 - x1
  184. rec = {
  185. 'im_file': im_file,
  186. 'im_id': np.array([self._global_chip_id]),
  187. 'h': chip_h,
  188. 'w': chip_w,
  189. 'gt_bbox': np.zeros((0, 4), dtype=np.float32),
  190. 'is_crowd': np.zeros((0, 1), dtype=np.int32),
  191. 'gt_class': np.zeros((0, 1), dtype=np.int32),
  192. # 'gt_poly': [],
  193. 'chip': chip
  194. }
  195. self._global_chip_id += 1
  196. chip_records.append(rec)
  197. return chip_records
  198. def _get_current_scale_parameters(self, scale_i, r):
  199. im_size = max(r['h'], r['w'])
  200. im_target_size = self.target_sizes[scale_i]
  201. self._cur_im_size, self._cur_im_target_size = im_size, im_target_size
  202. self._cur_scale = self._get_current_scale(im_target_size, im_size)
  203. self._cur_valid_ratio_range = self.valid_box_ratio_ranges[scale_i]
  204. def _get_current_scale(self, im_target_size, im_size):
  205. return im_target_size / im_size
  206. def _create_chips(self, h: int, w: int, scale: float):
  207. """
  208. Generate chips by chip_target_size and chip_target_stride.
  209. These two parameters just like kernel_size and stride in cnn.
  210. :return: chips, Cx4, xy in raw size dimension
  211. """
  212. chip_size = self.chip_target_size # omit target for simplicity
  213. stride = self.chip_target_stride
  214. width = int(scale * w)
  215. height = int(scale * h)
  216. min_chip_location_diff = 20 # in target size
  217. assert chip_size >= stride
  218. chip_overlap = chip_size - stride
  219. if (width - chip_overlap) % stride > min_chip_location_diff: # 不能被stride整除的部分比较大,则保留
  220. w_steps = max(1, int(math.ceil((width - chip_overlap) / stride)))
  221. else: # 不能被stride整除的部分比较小,则丢弃
  222. w_steps = max(1, int(math.floor((width - chip_overlap) / stride)))
  223. if (height - chip_overlap) % stride > min_chip_location_diff:
  224. h_steps = max(1, int(math.ceil((height - chip_overlap) / stride)))
  225. else:
  226. h_steps = max(1, int(math.floor((height - chip_overlap) / stride)))
  227. chips = list()
  228. for j in range(h_steps):
  229. for i in range(w_steps):
  230. x1 = i * stride
  231. y1 = j * stride
  232. x2 = min(x1 + chip_size, width)
  233. y2 = min(y1 + chip_size, height)
  234. chips.append([x1, y1, x2, y2])
  235. # check chip size
  236. for item in chips:
  237. if item[2] - item[0] > chip_size * 1.1 or item[3] - item[1] > chip_size * 1.1:
  238. raise ValueError(item)
  239. chips = np.array(chips, dtype=np.float)
  240. raw_size_chips = chips / scale
  241. return raw_size_chips
  242. def _get_valid_boxes_and_pos_chips(self, gt_bbox, chips):
  243. valid_ratio_range = self._cur_valid_ratio_range
  244. im_size = self._cur_im_size
  245. scale = self._cur_scale
  246. # Nx4 N
  247. valid_boxes, valid_boxes_idx = self._validate_boxes(valid_ratio_range, im_size, gt_bbox, scale)
  248. # dict: chipid->[box_id, ...]
  249. pos_chip2boxes_idx = self._find_pos_chips(chips, valid_boxes, valid_boxes_idx)
  250. return pos_chip2boxes_idx
  251. def _validate_boxes(self, valid_ratio_range: List[float],
  252. im_size: int,
  253. gt_boxes: 'np.array of Nx4',
  254. scale: float):
  255. """
  256. :return: valid_boxes: Nx4, valid_boxes_idx: N
  257. """
  258. ws = (gt_boxes[:, 2] - gt_boxes[:, 0]).astype(np.int32)
  259. hs = (gt_boxes[:, 3] - gt_boxes[:, 1]).astype(np.int32)
  260. maxs = np.maximum(ws, hs)
  261. box_ratio = maxs / im_size
  262. mins = np.minimum(ws, hs)
  263. target_mins = mins * scale
  264. low = valid_ratio_range[0] if valid_ratio_range[0] > 0 else 0
  265. high = valid_ratio_range[1] if valid_ratio_range[1] > 0 else np.finfo(np.float).max
  266. valid_boxes_idx = np.nonzero((low <= box_ratio) & (box_ratio < high) & (target_mins >= 2))[0]
  267. valid_boxes = gt_boxes[valid_boxes_idx]
  268. return valid_boxes, valid_boxes_idx
  269. def _find_pos_chips(self, chips: 'Cx4', valid_boxes: 'Bx4', valid_boxes_idx: 'B'):
  270. """
  271. :return: pos_chip2boxes_idx, dict: chipid->[box_id, ...]
  272. """
  273. iob = intersection_over_box(chips, valid_boxes) # overlap, CxB
  274. iob_threshold_to_find_chips = 1.
  275. pos_chip_ids, _ = self._find_chips_to_cover_overlaped_boxes(iob, iob_threshold_to_find_chips)
  276. pos_chip_ids = set(pos_chip_ids)
  277. iob_threshold_to_assign_box = 0.5
  278. pos_chip2boxes_idx = self._assign_boxes_to_pos_chips(
  279. iob, iob_threshold_to_assign_box, pos_chip_ids, valid_boxes_idx)
  280. return pos_chip2boxes_idx
  281. def _find_chips_to_cover_overlaped_boxes(self, iob, overlap_threshold):
  282. return find_chips_to_cover_overlaped_boxes(iob, overlap_threshold)
  283. def _assign_boxes_to_pos_chips(self, iob, overlap_threshold, pos_chip_ids, valid_boxes_idx):
  284. chip_ids, box_ids = np.nonzero(iob >= overlap_threshold)
  285. pos_chip2boxes_idx = defaultdict(list)
  286. for chip_id, box_id in zip(chip_ids, box_ids):
  287. if chip_id not in pos_chip_ids:
  288. continue
  289. raw_gt_box_idx = valid_boxes_idx[box_id]
  290. pos_chip2boxes_idx[chip_id].append(raw_gt_box_idx)
  291. return pos_chip2boxes_idx
  292. def _get_neg_boxes_and_chips(self, chips: 'Cx4', pos_chip_ids: 'D', proposals: 'Px4'):
  293. """
  294. :param chips:
  295. :param pos_chip_ids:
  296. :param proposals:
  297. :return: neg_chip2box_num, None or dict: chipid->neg_box_num
  298. """
  299. if not self.use_neg_chip:
  300. return None
  301. # train proposals maybe None
  302. if proposals is None or len(proposals) < 1:
  303. return None
  304. valid_ratio_range = self._cur_valid_ratio_range
  305. im_size = self._cur_im_size
  306. scale = self._cur_scale
  307. valid_props, _ = self._validate_boxes(valid_ratio_range, im_size, proposals, scale)
  308. neg_boxes = self._find_neg_boxes(chips, pos_chip_ids, valid_props)
  309. neg_chip2box_num = self._find_neg_chips(chips, pos_chip_ids, neg_boxes)
  310. return neg_chip2box_num
  311. def _find_neg_boxes(self, chips: 'Cx4', pos_chip_ids: 'D', valid_props: 'Px4'):
  312. """
  313. :return: neg_boxes: Nx4
  314. """
  315. if len(pos_chip_ids) == 0:
  316. return valid_props
  317. pos_chips = chips[pos_chip_ids]
  318. iob = intersection_over_box(pos_chips, valid_props)
  319. overlap_per_prop = np.max(iob, axis=0)
  320. non_overlap_props_idx = overlap_per_prop < 0.5
  321. neg_boxes = valid_props[non_overlap_props_idx]
  322. return neg_boxes
  323. def _find_neg_chips(self, chips: 'Cx4', pos_chip_ids: 'D', neg_boxes: 'Nx4'):
  324. """
  325. :return: neg_chip2box_num, dict: chipid->neg_box_num
  326. """
  327. neg_chip_ids = np.setdiff1d(np.arange(len(chips)), pos_chip_ids)
  328. neg_chips = chips[neg_chip_ids]
  329. iob = intersection_over_box(neg_chips, neg_boxes)
  330. iob_threshold_to_find_chips = 0.7
  331. chosen_neg_chip_ids, chip_id2overlap_box_num = \
  332. self._find_chips_to_cover_overlaped_boxes(iob, iob_threshold_to_find_chips)
  333. neg_chipid2box_num = {}
  334. for cid in chosen_neg_chip_ids:
  335. box_num = chip_id2overlap_box_num[cid]
  336. raw_chip_id = neg_chip_ids[cid]
  337. neg_chipid2box_num[raw_chip_id] = box_num
  338. return neg_chipid2box_num
  339. def crop_infer_anno_records(self, records: List[dict]):
  340. """
  341. transform image record to chips record
  342. :param records:
  343. :return: new_records, list of dict like
  344. {
  345. 'im_file': 'fake_image1.jpg',
  346. 'im_id': np.array([1]), # new _global_chip_id as im_id
  347. 'h': h, # chip height
  348. 'w': w, # chip width
  349. 'chip': [x1, y1, x2, y2] # added
  350. 'ori_im_h': ori_im_h # added, origin image height
  351. 'ori_im_w': ori_im_w # added, origin image width
  352. 'scale_i': 0 # added,
  353. }
  354. """
  355. self.chip_records = []
  356. self._global_chip_id = 1 # im_id start from 1
  357. self._global_chip_id2img_id = {}
  358. for r in records:
  359. for scale_i in range(self.scale_num):
  360. self._get_current_scale_parameters(scale_i, r)
  361. # Cx4
  362. chips = self._create_chips(r['h'], r['w'], self._cur_scale)
  363. cur_img_chip_record = self._get_chips_records(r, chips, scale_i)
  364. self.chip_records.extend(cur_img_chip_record)
  365. return self.chip_records
  366. def _get_chips_records(self, rec, chips, scale_i):
  367. cur_img_chip_records = []
  368. ori_im_h = rec["h"]
  369. ori_im_w = rec["w"]
  370. im_file = rec["im_file"]
  371. ori_im_id = rec["im_id"]
  372. for id, chip in enumerate(chips):
  373. chip_rec = {}
  374. x1, y1, x2, y2 = chip
  375. chip_h = y2 - y1
  376. chip_w = x2 - x1
  377. chip_rec["im_file"] = im_file
  378. chip_rec["im_id"] = self._global_chip_id
  379. chip_rec["h"] = chip_h
  380. chip_rec["w"] = chip_w
  381. chip_rec["chip"] = chip
  382. chip_rec["ori_im_h"] = ori_im_h
  383. chip_rec["ori_im_w"] = ori_im_w
  384. chip_rec["scale_i"] = scale_i
  385. self._global_chip_id2img_id[self._global_chip_id] = int(ori_im_id)
  386. self._global_chip_id += 1
  387. cur_img_chip_records.append(chip_rec)
  388. return cur_img_chip_records
  389. def aggregate_chips_detections(self, results, records=None):
  390. """
  391. # 1. transform chip dets to image dets
  392. # 2. nms boxes per image;
  393. # 3. format output results
  394. :param results:
  395. :param roidb:
  396. :return:
  397. """
  398. results = deepcopy(results)
  399. records = records if records else self.chip_records
  400. img_id2bbox = self._transform_chip2image_bboxes(results, records)
  401. nms_img_id2bbox = self._nms_dets(img_id2bbox)
  402. aggregate_results = self._reformat_results(nms_img_id2bbox)
  403. return aggregate_results
  404. def _transform_chip2image_bboxes(self, results, records):
  405. # 1. Transform chip dets to image dets;
  406. # 2. Filter valid range;
  407. # 3. Reformat and Aggregate chip dets to Get scale_cls_dets
  408. img_id2bbox = defaultdict(list)
  409. for result in results:
  410. bbox_locs = result['bbox']
  411. bbox_nums = result['bbox_num']
  412. if len(bbox_locs) == 1 and bbox_locs[0][0] == -1: # current batch has no detections
  413. # bbox_locs = array([[-1.]], dtype=float32); bbox_nums = [[1]]
  414. # MultiClassNMS output: If there is no detected boxes for all images, lod will be set to {1} and Out only contains one value which is -1.
  415. continue
  416. im_ids = result['im_id'] # replace with range(len(bbox_nums))
  417. last_bbox_num = 0
  418. for idx, im_id in enumerate(im_ids):
  419. cur_bbox_len = bbox_nums[idx]
  420. bboxes = bbox_locs[last_bbox_num: last_bbox_num + cur_bbox_len]
  421. last_bbox_num += cur_bbox_len
  422. # box: [num_id, score, xmin, ymin, xmax, ymax]
  423. if len(bboxes) == 0: # current image has no detections
  424. continue
  425. chip_rec = records[int(im_id) - 1] # im_id starts from 1, type is np.int64
  426. image_size = max(chip_rec["ori_im_h"], chip_rec["ori_im_w"])
  427. bboxes = transform_chip_boxes2image_boxes(bboxes, chip_rec["chip"], chip_rec["ori_im_h"], chip_rec["ori_im_w"])
  428. scale_i = chip_rec["scale_i"]
  429. cur_scale = self._get_current_scale(self.target_sizes[scale_i], image_size)
  430. _, valid_boxes_idx = self._validate_boxes(self.valid_box_ratio_ranges[scale_i], image_size,
  431. bboxes[:, 2:], cur_scale)
  432. ori_img_id = self._global_chip_id2img_id[int(im_id)]
  433. img_id2bbox[ori_img_id].append(bboxes[valid_boxes_idx])
  434. return img_id2bbox
  435. def _nms_dets(self, img_id2bbox):
  436. # 1. NMS on each image-class
  437. # 2. Limit number of detections to MAX_PER_IMAGE if requested
  438. max_per_img = self.max_per_img
  439. nms_thresh = self.nms_thresh
  440. for img_id in img_id2bbox:
  441. box = img_id2bbox[img_id] # list of np.array of shape [N, 6], 6 is [label, score, x1, y1, x2, y2]
  442. box = np.concatenate(box, axis=0)
  443. nms_dets = nms(box, nms_thresh)
  444. if max_per_img > 0:
  445. if len(nms_dets) > max_per_img:
  446. keep = np.argsort(-nms_dets[:, 1])[:max_per_img]
  447. nms_dets = nms_dets[keep]
  448. img_id2bbox[img_id] = nms_dets
  449. return img_id2bbox
  450. def _reformat_results(self, img_id2bbox):
  451. """reformat results"""
  452. im_ids = img_id2bbox.keys()
  453. results = []
  454. for img_id in im_ids: # output by original im_id order
  455. if len(img_id2bbox[img_id]) == 0:
  456. bbox = np.array([[-1., 0., 0., 0., 0., 0.]]) # edge case: no detections
  457. bbox_num = np.array([0])
  458. else:
  459. # np.array of shape [N, 6], 6 is [label, score, x1, y1, x2, y2]
  460. bbox = img_id2bbox[img_id]
  461. bbox_num = np.array([len(bbox)])
  462. res = dict(
  463. im_id=np.array([[img_id]]),
  464. bbox=bbox,
  465. bbox_num=bbox_num
  466. )
  467. results.append(res)
  468. return results