def simple_test(self, x, proposal_list, img_metas, proposals=None, rescale=False): rois = arb2roi(proposal_list, bbox_type=self.bbox_head.start_bbox_type) bbox_results = self._bbox_forward(x, rois) img_shape = img_metas[0]['img_shape'] scale_factor = img_metas[0]['scale_factor'] det_bboxes, det_labels = self.bbox_head.get_bboxes( rois, bbox_results['cls_score'], bbox_results['bbox_pred'], bbox_results['fix_pred'], bbox_results['ratio_pred'], img_shape, scale_factor, rescale=rescale, cfg=self.test_cfg) bbox_results = arb2result(det_bboxes, det_labels, self.bbox_head.num_classes, bbox_type=self.bbox_head.end_bbox_type) return bbox_results
def aug_test(self, x, proposal_list, img_metas, rescale=False): """Test with augmentations. If rescale is False, then returned bboxes and masks will fit the scale of imgs[0]. """ if self.with_mask: raise NotImplementedError # recompute feats to save memory det_bboxes, det_labels = self.aug_test_bboxes(x, img_metas, proposal_list, self.test_cfg) if rescale: _det_bboxes = det_bboxes else: _det_bboxes = det_bboxes.clone() scale_factor = _det_bboxes.new_tensor( img_metas[0][0]['scale_factor']) if self.bbox_head.end_bbox_type == 'poly': _det_bboxes[:, :8] *= scale_factor.repeat(2) else: _det_bboxes[:, :4] *= scale_factor bbox_results = arb2result(_det_bboxes, det_labels, self.bbox_head.num_classes, bbox_type=self.bbox_head.end_bbox_type) return bbox_results
def simple_test(self, x, proposal_list, img_metas, proposals=None, rescale=False): """Test without augmentation.""" assert self.with_bbox, 'Bbox head must be implemented.' det_bboxes, det_labels = self.simple_test_bboxes( x, img_metas, proposal_list, self.test_cfg, rescale=rescale) bbox_results = arb2result(det_bboxes, det_labels, self.bbox_head.num_classes, bbox_type=self.bbox_head.end_bbox_type) if not self.with_mask: return bbox_results else: segm_results = self.simple_test_mask( x, img_metas, det_bboxes, det_labels, rescale=rescale) return bbox_results, segm_results
def simple_test(self, img, img_metas, rescale=False): """Test function without test time augmentation Args: imgs (list[torch.Tensor]): List of multiple images img_metas (list[dict]): List of image information. rescale (bool, optional): Whether to rescale the results. Defaults to False. Returns: np.ndarray: proposals """ x = self.extract_feat(img) outs = self.bbox_head(x) bbox_list = self.bbox_head.get_bboxes(*outs, img_metas, rescale=rescale) bbox_type = getattr(self.bbox_head, 'bbox_type', 'hbb') bbox_results = [ arb2result(det_bboxes, det_labels, self.bbox_head.num_classes, bbox_type) for det_bboxes, det_labels in bbox_list ] return bbox_results[0]
def simple_test(self, x, proposal_list, img_metas, rescale=False): """Test without augmentation.""" assert self.with_bbox, 'Bbox head must be implemented.' img_shape = img_metas[0]['img_shape'] ori_shape = img_metas[0]['ori_shape'] scale_factor = img_metas[0]['scale_factor'] # "ms" in variable names means multi-stage ms_scores = [] rcnn_test_cfg = self.test_cfg rois = arb2roi(proposal_list, bbox_type=self.bbox_head[0].start_bbox_type) for i in range(self.num_stages): bbox_results = self._bbox_forward(i, x, rois) ms_scores.append(bbox_results['cls_score']) if i < self.num_stages - 1: bbox_label = bbox_results['cls_score'].argmax(dim=1) rois = self.bbox_head[i].regress_by_class( rois, bbox_label, bbox_results['bbox_pred'], img_metas[0]) score_weights = rcnn_test_cfg.score_weights assert len(score_weights) == len(ms_scores) cls_score = sum([w * s for w, s in zip(score_weights, ms_scores)]) \ / sum(score_weights) det_bboxes, det_labels = self.bbox_head[-1].get_bboxes( rois, cls_score, bbox_results['bbox_pred'], img_shape, scale_factor, rescale=rescale, cfg=rcnn_test_cfg) results = arb2result(det_bboxes, det_labels, self.bbox_head[-1].num_classes, bbox_type=self.bbox_head[-1].end_bbox_type) return results
def aug_test(self, features, proposal_list, img_metas, rescale=False): rcnn_test_cfg = self.test_cfg aug_bboxes = [] aug_scores = [] for x, img_meta in zip(features, img_metas): # only one image in the batch img_shape = img_meta[0]['img_shape'] scale_factor = img_meta[0]['scale_factor'] angle = img_meta[0].get('angle', 0) matrix = img_meta[0].get('matrix', np.eye(3)) rotate_after_flip = img_meta[0].get('rotate_after_flip', True) if 'flip' in img_meta[0]: direction = img_meta[0]['flip_direction'] h_flip = img_meta[0]['flip'] if direction == 'horizontal' else False v_flip = img_meta[0]['flip'] if direction == 'vertical' else False else: h_flip, v_flip = img_meta[0]['h_flip'], img_meta[0]['v_flip'] proposals = hbb_mapping(proposal_list[0][:, :4], img_shape, scale_factor, h_flip, v_flip, rotate_after_flip, angle, matrix) # "ms" in variable names means multi-stage ms_scores = [] rois = arb2roi([proposals], bbox_type=self.bbox_head[0].start_bbox_type) for i in range(self.num_stages): bbox_results = self._bbox_forward(i, x, rois) ms_scores.append(bbox_results['cls_score']) if i < self.num_stages - 1: bbox_label = bbox_results['cls_score'].argmax(dim=1) rois = self.bbox_head[i].regress_by_class( rois, bbox_label, bbox_results['bbox_pred'], img_meta[0]) score_weights = rcnn_test_cfg.score_weights assert len(score_weights) == len(ms_scores) cls_score = sum([w * s for w, s in zip(score_weights, ms_scores)]) \ / sum(score_weights) cls_score = ms_scores[0] bboxes, scores = self.bbox_head[-1].get_bboxes( rois, cls_score, bbox_results['bbox_pred'], img_shape, scale_factor, rescale=False, cfg=None) aug_bboxes.append(bboxes) aug_scores.append(scores) # after merging, bboxes will be rescaled to the original image size end_bbox_type = self.bbox_head[-1].end_bbox_type merged_bboxes, merged_scores = merge_rotate_aug_arb( aug_bboxes, aug_scores, img_metas, rcnn_test_cfg, merge_type='avg', bbox_type=end_bbox_type) det_bboxes, det_labels = multiclass_arb_nms( merged_bboxes, merged_scores, rcnn_test_cfg.score_thr, rcnn_test_cfg.nms, rcnn_test_cfg.max_per_img, bbox_type=end_bbox_type) if rescale: _det_bboxes = det_bboxes else: _det_bboxes = det_bboxes.clone() scale_factor = _det_bboxes.new_tensor( img_metas[0][0]['scale_factor']) if end_bbox_type == 'poly': _det_bboxes[:, :8] *= scale_factor.repeat(2) else: _det_bboxes[:, :4] *= scale_factor bbox_result = arb2result( det_bboxes, det_labels, self.bbox_head[-1].num_classes, bbox_type=self.bbox_head[-1].end_bbox_type) return bbox_result
def aug_test(self, feats, proposal_list, img_metas, rescale=False): aug_bboxes = [] aug_scores = [] for x, img_meta in zip(feats, img_metas): # only one image in the batch img_shape = img_meta[0]['img_shape'] scale_factor = img_meta[0]['scale_factor'] angle = img_meta[0].get('angle', 0) matrix = img_meta[0].get('matrix', np.eye(3)) rotate_after_flip = img_meta[0].get('rotate_after_flip', True) if 'flip' in img_meta[0]: direction = img_meta[0]['flip_direction'] h_flip = img_meta[0][ 'flip'] if direction == 'horizontal' else False v_flip = img_meta[0][ 'flip'] if direction == 'vertical' else False else: h_flip, v_flip = img_meta[0]['h_flip'], img_meta[0]['v_flip'] # TODO more flexible proposals = arb_mapping(proposal_list[0][:, :-1], img_shape, scale_factor, h_flip, v_flip, rotate_after_flip, angle, matrix, bbox_type=self.bbox_head.start_bbox_type) rois = arb2roi([proposals], bbox_type=self.bbox_head.start_bbox_type) # recompute feature maps to save GPU memory bbox_results = self._bbox_forward(x, rois) bboxes, scores = self.bbox_head.get_bboxes( rois, bbox_results['cls_score'], bbox_results['bbox_pred'], bbox_results['fix_pred'], bbox_results['ratio_pred'], img_shape, scale_factor, rescale=False, cfg=None) aug_bboxes.append(bboxes) aug_scores.append(scores) # after merging, bboxes will be rescaled to the original image size merged_bboxes, merged_scores = merge_rotate_aug_arb( aug_bboxes, aug_scores, img_metas, self.test_cfg, merge_type='avg', bbox_type=self.bbox_head.end_bbox_type) det_bboxes, det_labels = multiclass_arb_nms( merged_bboxes, merged_scores, self.test_cfg.score_thr, self.test_cfg.nms, self.test_cfg.max_per_img, bbox_type=self.bbox_head.end_bbox_type) if rescale: _det_bboxes = det_bboxes else: _det_bboxes = det_bboxes.clone() scale_factor = _det_bboxes.new_tensor( img_metas[0][0]['scale_factor']) _det_bboxes[:, :8] *= scale_factor.repeat(2) bbox_result = arb2result(det_bboxes, det_labels, self.bbox_head.num_classes, bbox_type='poly') return bbox_result