def sample_class_v2(self, name, num, gt_boxes): ''' This func aims to select fixed number of gt boxes from gt database with collision (bev iou) test performed. ''' # sample num gt_boxes from gt_database sampled = self._sampler_dict[name].sample(num) sampled = copy.deepcopy(sampled) num_sampled = len(sampled) num_gt = gt_boxes.shape[0] # get all boxes: gt_boxes + sp_boxes sp_boxes = np.stack([i["box3d_lidar"] for i in sampled], axis=0) # todo: need modification here boxes = np.concatenate([gt_boxes, sp_boxes], axis=0).copy() offset = [0.0, 0.0] if self.gt_aug_with_context > 0.0: offset = [self.gt_aug_with_context, self.gt_aug_with_context] # get all boxes_bev: gt_boxes_bev + sampled_boxes_bev sp_boxes_new = boxes[num_gt:] gt_boxes_bv = box_np_ops.center_to_corner_box2d( gt_boxes[:, 0:2], gt_boxes[:, 3:5], gt_boxes[:, -1]) sp_boxes_bv = box_np_ops.center_to_corner_box2d( sp_boxes_new[:, 0:2], sp_boxes_new[:, 3:5] + offset, sp_boxes_new[:, -1]) total_bv = np.concatenate([gt_boxes_bv, sp_boxes_bv], axis=0) # collision test on bev (stricter than 3d) coll_mat = prep.box_collision_test(total_bv, total_bv) # todo: too slow here diag = np.arange(total_bv.shape[0]) coll_mat[diag, diag] = False # get valid samples valid_samples = [] for i in range( num_gt, num_gt + num_sampled ): # todo: the overall box num may not meet the requirement if coll_mat[i].any(): # i-th sampled box is not considered into auged gt-boxes. coll_mat[i] = False coll_mat[:, i] = False else: # i-th sampled box is considered into auged gt-boxes. valid_samples.append(sampled[i - num_gt]) return valid_samples
def sample_class_v2(self, name, num, gt_boxes): sampled = self._sampler_dict[name].sample(num) sampled = copy.deepcopy(sampled) num_gt = gt_boxes.shape[0] num_sampled = len(sampled) gt_boxes_bv = box_np_ops.center_to_corner_box2d( gt_boxes[:, 0:2], gt_boxes[:, 3:5], gt_boxes[:, -1]) sp_boxes = np.stack([i["box3d_lidar"] for i in sampled], axis=0) valid_mask = np.zeros([gt_boxes.shape[0]], dtype=np.bool_) valid_mask = np.concatenate( [valid_mask, np.ones([sp_boxes.shape[0]], dtype=np.bool_)], axis=0) boxes = np.concatenate([gt_boxes, sp_boxes], axis=0).copy() if self._enable_global_rot: # place samples to any place in a circle. prep.noise_per_object_v3_(boxes, None, valid_mask, 0, 0, self._global_rot_range, num_try=100) sp_boxes_new = boxes[gt_boxes.shape[0]:] sp_boxes_bv = box_np_ops.center_to_corner_box2d( sp_boxes_new[:, 0:2], sp_boxes_new[:, 3:5], sp_boxes_new[:, -1]) total_bv = np.concatenate([gt_boxes_bv, sp_boxes_bv], axis=0) # coll_mat = collision_test_allbox(total_bv) coll_mat = prep.box_collision_test(total_bv, total_bv) diag = np.arange(total_bv.shape[0]) coll_mat[diag, diag] = False valid_samples = [] for i in range(num_gt, num_gt + num_sampled): if coll_mat[i].any(): coll_mat[i] = False coll_mat[:, i] = False else: if self._enable_global_rot: sampled[i - num_gt]["box3d_lidar"][:2] = boxes[i, :2] sampled[i - num_gt]["box3d_lidar"][-1] = boxes[i, -1] sampled[i - num_gt]["rot_transform"] = ( boxes[i, -1] - sp_boxes[i - num_gt, -1]) valid_samples.append(sampled[i - num_gt]) return valid_samples
def rotate_nms_cc(dets, thresh): scores = dets[:, 5] order = scores.argsort()[::-1].astype(np.int32) # highest->lowest dets_corners = box_np_ops.center_to_corner_box2d( dets[:, :2], dets[:, 2:4], dets[:, 4] ) dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) standup_iou = box_np_ops.iou_jit(dets_standup, dets_standup, eps=0.0) # print(dets_corners.shape, order.shape, standup_iou.shape) return rotate_non_max_suppression_cpu(dets_corners, order, standup_iou, thresh)
def filter_gt_box_outside_range(gt_boxes, limit_range): """remove gtbox outside training range. this function should be applied after other prep functions Args: gt_boxes ([type]): [description] limit_range ([type]): [description] """ gt_boxes_bv = box_np_ops.center_to_corner_box2d(gt_boxes[:, [0, 1]], gt_boxes[:, [3, 3 + 1]], gt_boxes[:, -1]) bounding_box = box_np_ops.minmax_to_corner_2d( np.asarray(limit_range)[np.newaxis, ...]) ret = points_in_convex_polygon_jit(gt_boxes_bv.reshape(-1, 2), bounding_box) return np.any(ret.reshape(-1, 4), axis=1)
def rotate_weighted_nms_cc(box, dets, thresh, iou_preds, labels, dirs, anchors=None, nms_cnt_thresh=2.6, nms_sigma_dist_interval=(0, 20, 40, 60), nms_sigma_square=(0.0009, 0.009, 0.1, 1), suppressed_thresh=0.3, ): scores = dets[:, 5] dets_corners = box_np_ops.center_to_corner_box2d( dets[:, :2], dets[:, 2:4], dets[:, 4] ) dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) standup_iou = box_np_ops.iou_jit(dets_standup, dets_standup, eps=0.0) if anchors is None: centerness_c = 0 anchors = np.zeros((1,1)) else: centerness_c = 1 result = IOU_weighted_rotate_non_max_suppression_cpu(box, dets_corners, standup_iou, thresh, scores, iou_preds, labels, dirs, anchors, nms_cnt_thresh, nms_sigma_dist_interval, nms_sigma_square, suppressed_thresh, centerness_c) return result
def rotate_weighted_nms_cc( box, dets, thresh, iou_preds, labels, dirs, anchors=None, ): scores = dets[:, 5] order = scores.argsort()[::-1].astype(np.int32) # highest->lowest dets_corners = box_np_ops.center_to_corner_box2d(dets[:, :2], dets[:, 2:4], dets[:, 4]) dets_standup = box_np_ops.corner_to_standup_nd(dets_corners) standup_iou = box_np_ops.iou_jit(dets_standup, dets_standup, eps=0.0) result = IOU_weighted_rotate_non_max_suppression_cpu( box, dets_corners, standup_iou, thresh, scores, iou_preds, labels, dirs, anchors) return result
def sample_group(self, name, num, gt_boxes, gt_group_ids): sampled, group_num = self.sample(name, num) sampled = copy.deepcopy(sampled) # rewrite sampled group id to avoid duplicated with gt group ids gid_map = {} max_gt_gid = np.max(gt_group_ids) sampled_gid = max_gt_gid + 1 for s in sampled: gid = s["group_id"] if gid in gid_map: s["group_id"] = gid_map[gid] else: gid_map[gid] = sampled_gid s["group_id"] = sampled_gid sampled_gid += 1 num_gt = gt_boxes.shape[0] gt_boxes_bv = box_np_ops.center_to_corner_box2d( gt_boxes[:, 0:2], gt_boxes[:, 3:5], gt_boxes[:, -1]) sp_boxes = np.stack([i["box3d_lidar"] for i in sampled], axis=0) sp_group_ids = np.stack([i["group_id"] for i in sampled], axis=0) valid_mask = np.zeros([gt_boxes.shape[0]], dtype=np.bool_) valid_mask = np.concatenate( [valid_mask, np.ones([sp_boxes.shape[0]], dtype=np.bool_)], axis=0) boxes = np.concatenate([gt_boxes, sp_boxes], axis=0).copy() group_ids = np.concatenate([gt_group_ids, sp_group_ids], axis=0) if self._enable_global_rot: # place samples to any place in a circle. prep.noise_per_object_v3_( boxes, None, valid_mask, 0, 0, self._global_rot_range, group_ids=group_ids, num_try=100, ) sp_boxes_new = boxes[gt_boxes.shape[0]:] sp_boxes_bv = box_np_ops.center_to_corner_box2d( sp_boxes_new[:, 0:2], sp_boxes_new[:, 3:5], sp_boxes_new[:, -1]) total_bv = np.concatenate([gt_boxes_bv, sp_boxes_bv], axis=0) # coll_mat = collision_test_allbox(total_bv) coll_mat = prep.box_collision_test(total_bv, total_bv) diag = np.arange(total_bv.shape[0]) coll_mat[diag, diag] = False valid_samples = [] idx = num_gt for num in group_num: if coll_mat[idx:idx + num].any(): coll_mat[idx:idx + num] = False coll_mat[:, idx:idx + num] = False else: for i in range(num): if self._enable_global_rot: sampled[idx - num_gt + i]["box3d_lidar"][:2] = boxes[idx + i, :2] sampled[idx - num_gt + i]["box3d_lidar"][-1] = boxes[idx + i, -1] sampled[idx - num_gt + i]["rot_transform"] = ( boxes[idx + i, -1] - sp_boxes[idx + i - num_gt, -1]) valid_samples.append(sampled[idx - num_gt + i]) idx += num return valid_samples
def sample_class_v2(self, name, num, gt_boxes): # sample num gt_boxes from gt_database sampled = self._sampler_dict[name].sample(num) sampled = copy.deepcopy(sampled) num_sampled = len(sampled) # get all boxes: gt_boxes + sp_boxes num_gt = gt_boxes.shape[0] gt_boxes_bv = box_np_ops.center_to_corner_box2d( gt_boxes[:, 0:2], gt_boxes[:, 3:5], gt_boxes[:, -1]) sp_boxes = np.stack([i["box3d_lidar"] for i in sampled], axis=0) # todo: need modification here boxes = np.concatenate([gt_boxes, sp_boxes], axis=0).copy() # unused: get mask valid_mask = np.zeros([gt_boxes.shape[0]], dtype=np.bool_) valid_mask = np.concatenate( [valid_mask, np.ones([sp_boxes.shape[0]], dtype=np.bool_)], axis=0) if self._enable_global_rot: # False prep.noise_per_object_v3_(boxes, None, valid_mask, 0, 0, self._global_rot_range, num_try=100) # get all boxes_bev: gt_boxes_bev + sampled_boxes_bev sp_boxes_new = boxes[gt_boxes.shape[0]:] sp_boxes_bv = box_np_ops.center_to_corner_box2d( sp_boxes_new[:, 0:2], sp_boxes_new[:, 3:5], sp_boxes_new[:, -1]) total_bv = np.concatenate([gt_boxes_bv, sp_boxes_bv], axis=0) # collision test on bev (stricter than 3d) coll_mat = prep.box_collision_test(total_bv, total_bv) # todo: too slow here diag = np.arange(total_bv.shape[0]) coll_mat[diag, diag] = False # get valid samples valid_samples = [] for i in range( num_gt, num_gt + num_sampled ): # todo: without multiple try times, sometimes, the overall box num may not meet the requirement if coll_mat[i].any(): # i-th sampled box is not considered into gt-boxes. coll_mat[i] = False coll_mat[:, i] = False else: if self._enable_global_rot: # False sampled[i - num_gt]["box3d_lidar"][:2] = boxes[ i, :2] # boxes has got noise sampled[i - num_gt]["box3d_lidar"][-1] = boxes[i, -1] sampled[i - num_gt]["rot_transform"] = ( boxes[i, -1] - sp_boxes[i - num_gt, -1]) # i-th sampled box is considered into gt-boxes. valid_samples.append(sampled[i - num_gt]) return valid_samples