def show_train_res(self, img, gt_bboxes, img_metas, outs, score_threshold): from configs.common import DIM_PARSE img = img[0].permute(1,2,0).cpu().data.numpy() _gt_bboxes = [g.cpu().data.numpy() for g in gt_bboxes][0:1] rescale = False bbox_inputs = outs + (img_metas, self.test_cfg, rescale) bbox_list = self.bbox_head.get_bboxes(*bbox_inputs) det_bboxes, det_labels, _ = bbox_list[0] _det_bboxes0 = det_bboxes.cpu().data.numpy() assert not 'background' in img_metas[0]['classes'] dim_parse = DIM_PARSE( self.obj_rep, len(img_metas[0]['classes'])+1 ) _det_bboxes1 = dim_parse.clean_bboxes_out(_det_bboxes0,'final', 'line_ave' ) _det_bboxes = [_det_bboxes1] ngt = len(_gt_bboxes[0]) ndt = len(_det_bboxes[0]) print(f'gt num={ngt}, det num={ndt}') mask0 = _det_bboxes[0][:,5] >= score_threshold[0] mask1 = _det_bboxes[0][:,5] <= score_threshold[1] mask = mask0 * mask1 #debug_utils._show_lines_ls_points_ls((512,512), _det_bboxes) #debug_utils._show_lines_ls_points_ls((512,512), _gt_bboxes) _show_objs_ls_points_ls((512,512), objs_ls = [_gt_bboxes[0][:,:5], _det_bboxes[0][mask][:,:5]], obj_scores_ls = [None, _det_bboxes[0][mask][:,5]], obj_colors=['red','green'], obj_rep=self.obj_rep ) _show_objs_ls_points_ls(img[:,:,0], [_gt_bboxes[0]], obj_rep=self.obj_rep) pass
def simple_test(self, img, img_meta, rescale=False, gt_bboxes=None, gt_labels=None, gt_relations=None): from configs.common import DIM_PARSE record_time = DEBUG_CFG.RECORD_TEST_TIME if record_time: t0 = time.time() if DEBUG_CFG.DISABLE_RESCALE: rescale = False #_show_objs_ls_points_ls(img[0].permute(1,2,0).cpu().data.numpy(), [gt_bboxes[0][0].cpu().data.numpy()], 'RoLine2D_UpRight_xyxy_sin2a') x = self.extract_feat(img, gt_bboxes) if record_time: t1 = time.time() self.update_dynamic_shape(x, img_meta) #update_img_shape_for_pcl(x, img_meta[0], self.point_strides) outs = self.bbox_head(x) if record_time: t2 = time.time() bbox_inputs = outs + (img_meta, self.test_cfg, rescale) bbox_list = self.bbox_head.get_bboxes(*bbox_inputs) bbox_results = [ bbox2result(det_bboxes, det_labels, self.bbox_head.num_classes) for det_bboxes, det_labels ,_ in bbox_list ] if bbox_list[0][2] is not None: relation_scores = [self.bbox_head.get_relations(relation_scores).cpu().numpy() for _,_, relation_scores in bbox_list] else: relation_scores = [None] results = dict( det_bboxes=bbox_results[0], gt_bboxes=gt_bboxes, gt_labels=gt_labels, img = img, det_relations = relation_scores[0]) if record_time: t3 = time.time() print(f'\nTime\nbackbone:\t {t1-t0:.4f}') print(f'head prediction:\t {t2-t1:.4f}') print(f'NMS: {t3-t2:.4f}') pass if 0: dim_parse = DIM_PARSE( len(img_meta[0]['classes'])+1 ) det_bboxes = dim_parse.clean_bboxes_out( bbox_results[0][0],'final', 'line_ave' )[:,:5] _show_objs_ls_points_ls(img[0].permute(1,2,0).cpu().data.numpy(), [gt_bboxes[0][0].cpu().data.numpy(), det_bboxes], 'RoLine2D_UpRight_xyxy_sin2a') _show_objs_ls_points_ls(img[0].permute(1,2,0).cpu().data.numpy(), [gt_bboxes[0][0].cpu().data.numpy(), ], 'RoLine2D_UpRight_xyxy_sin2a') return results
def multiclass_nms(multi_bboxes, multi_scores, score_thr, nms_cfg, max_num=-1, score_factors=None, obj_rep=None): """NMS for multi-class bboxes. Args: multi_bboxes (Tensor): shape (n, #class*4) or (n, 4) multi_scores (Tensor): shape (n, #class), where the 0th column contains scores of the background class, but this will be ignored. score_thr (float): bbox threshold, bboxes with scores lower than it will not be considered. nms_thr (float): NMS IoU threshold max_num (int): if there are more than max_num bboxes after NMS, only top max_num will be kept. score_factors (Tensor): The factors multiplied to scores before applying NMS Returns: tuple: (bboxes, labels), tensors of shape (k, 5) and (k, 1). Labels are 0-based. """ num_classes_inc_bg = multi_scores.shape[1] # include bg dim_parse = DIM_PARSE(obj_rep, num_classes_inc_bg) nms_in_dim = dim_parse.NMS_IN_DIM assert multi_bboxes.shape[1] == nms_in_dim num_classes = multi_scores.shape[1] bboxes, labels = [], [] nms_cfg_ = nms_cfg.copy() nms_type = nms_cfg_.pop('type', 'nms') nms_op = getattr(nms_wrapper, nms_type) nms_inds = [] for i in range(1, num_classes): cls_inds = multi_scores[:, i] > score_thr if not cls_inds.any(): continue # get bboxes and scores of this class if multi_bboxes.shape[1] == nms_in_dim: _bboxes = multi_bboxes[cls_inds, :] else: _bboxes = multi_bboxes[cls_inds, i * nms_in_dim:(i + 1) * nms_in_dim] _scores = multi_scores[cls_inds, i] if score_factors is not None: _scores *= score_factors[cls_inds] cls_dets = torch.cat([_bboxes, _scores[:, None]], dim=1) cls_dets, inds = nms_op(cls_dets, obj_rep, **nms_cfg_) cls_labels = multi_bboxes.new_full((cls_dets.shape[0], ), i - 1, dtype=torch.long) bboxes.append(cls_dets) labels.append(cls_labels) # add for relation detection nms_inds.append(torch.nonzero(cls_inds).squeeze(1)[inds]) pass if bboxes: bboxes = torch.cat(bboxes) labels = torch.cat(labels) nms_inds = torch.cat(nms_inds) if bboxes.shape[0] > max_num: _, inds = bboxes[:, -1].sort(descending=True) inds = inds[:max_num] bboxes = bboxes[inds] labels = labels[inds] nms_inds = nms_inds[inds] else: bboxes = multi_bboxes.new_zeros((0, nms_in_dim + 1)) labels = multi_bboxes.new_zeros((0, ), dtype=torch.long) return bboxes, labels, nms_inds
#_transform_method='moment_XYXYSin2WZ0Z1' _obj_rep_out = _obj_rep if 'room' in classes: _obj_rep = 'XYXYSin2WZ0Z1' _transform_method = [ 'XYDRSin2Cos2Z0Z1', 'moment_std_XYDRSin2Cos2Z0Z1', 'moment_max_XYDRSin2Cos2Z0Z1' ][1] _obj_rep_out = 'XYDRSin2Cos2Z0Z1' elif DATA == 'stanford2d': _obj_rep = 'Rect4CornersZ0Z1' _transform_method = 'sort_4corners' dim_parse = DIM_PARSE(_obj_rep, len(classes) + 1) _obj_dim = dim_parse.OBJ_DIM #******************************************************************************* cls_groups = None #cls_groups = [[1], [2]] #******************************************************************************* norm_cfg = dict(type='GN', num_groups=32, requires_grad=True) model = dict(type='StrPointsDetector', pretrained=None, backbone=dict(type='ResNet', depth=50, in_channels=4, num_stages=4, out_indices=(0, 1, 2, 3),