Пример #1
0
    def forward(self, feat, proposals):
        ms_scores = []
        rois = proposals
        for i in range(self.num_stages):
            bbox_results = self._bbox_forward(i, feat, rois)
            ms_scores.append(bbox_results['cls_score'])
            bbox_pred = bbox_results['bbox_pred']

            if i < self.num_stages - 1:
                bbox_label = bbox_results['cls_score'].argmax(dim=1)
                rois = self.regress_by_class(i, rois, bbox_label, bbox_pred)

        ### bbox_head.get_boxes
        cls_score = sum(ms_scores) / self.num_stages

        if isinstance(cls_score, list):
            cls_score = sum(cls_score) / float(len(cls_score))
        scores = F.softmax(cls_score, dim=1)
        bbox_head = self.bbox_head[-1]
        bboxes = delta2bbox(rois, bbox_pred, bbox_head.bbox_coder.means,
                            bbox_head.bbox_coder.stds)

        num_bboxes = bboxes.shape[0]
        scores = scores.unsqueeze(0)
        bboxes = bboxes.view(1, num_bboxes, -1, 4)
        bboxes = bboxes.repeat(1, 1, bbox_head.num_classes, 1)
        bboxes_ext = bboxes.new_zeros((1, num_bboxes, 1, 4))
        bboxes = torch.cat([bboxes, bboxes_ext], 2)
        num_detections, det_boxes, det_scores, det_classes = self.rcnn_nms(
            scores, bboxes, num_bboxes, self.test_cfg.max_per_img)

        return num_detections, det_boxes, det_scores, det_classes
    def forward(self, feat, proposals):
        batch_size = proposals.shape[0]
        num_proposals = proposals.shape[1]
        rois_pad = mm2trt_util.arange_by_input(proposals, 0).unsqueeze(1)
        rois_pad = rois_pad.repeat(1, num_proposals).view(-1, 1)
        proposals = proposals.view(-1, 4)
        rois = torch.cat([rois_pad, proposals], dim=1)

        roi_feats = self.bbox_roi_extractor(
            feat[:len(self.bbox_roi_extractor.featmap_strides)], rois)
        if self.shared_head is not None:
            roi_feats = self.shared_head(roi_feats)
        # rcnn
        cls_score, bbox_pred = self.bbox_head(roi_feats)

        if isinstance(cls_score, list):
            cls_score = sum(cls_score) / float(len(cls_score))
        scores = F.softmax(cls_score, dim=1)
        bboxes = delta2bbox(proposals, bbox_pred,
                            self.bbox_head.bbox_coder.means,
                            self.bbox_head.bbox_coder.stds)

        scores = scores.view(batch_size, num_proposals, -1)
        bboxes = bboxes.view(batch_size, num_proposals, -1, 4)
        num_bboxes = bboxes.shape[1]
        bboxes_ext = bboxes[:, :, 0:1, :] * 0
        bboxes = torch.cat([bboxes, bboxes_ext], 2)
        num_detections, det_boxes, det_scores, det_classes = self.rcnn_nms(
            scores, bboxes, num_bboxes, self.test_cfg.max_per_img)

        return num_detections, det_boxes, det_scores, det_classes
Пример #3
0
    def forward(self, feat, proposals):
        zeros = proposals.new_zeros([proposals.shape[0], 1])
        rois = torch.cat([zeros, proposals], dim=1)

        bbox_cls_feats = self.bbox_roi_extractor(
            feat[:self.bbox_roi_extractor.num_inputs], rois)
        bbox_reg_feats = self.bbox_roi_extractor(
            feat[:self.bbox_roi_extractor.num_inputs],
            rois,
            roi_scale_factor=self.reg_roi_scale_factor)
        if self.shared_head is not None:
            bbox_cls_feats = self.shared_head(bbox_cls_feats)
            bbox_reg_feats = self.shared_head(bbox_reg_feats)

        # rcnn
        cls_score, bbox_pred = self.bbox_head(bbox_cls_feats, bbox_reg_feats)

        if isinstance(cls_score, list):
            cls_score = sum(cls_score) / float(len(cls_score))
        scores = F.softmax(cls_score, dim=1)
        bboxes = delta2bbox(proposals, bbox_pred,
                            self.bbox_head.bbox_coder.means,
                            self.bbox_head.bbox_coder.stds)

        num_bboxes = bboxes.shape[0]
        scores = scores.unsqueeze(0)
        bboxes = bboxes.view(1, num_bboxes, -1, 4)
        bboxes_ext = bboxes.new_zeros((1, num_bboxes, 1, 4))
        bboxes = torch.cat([bboxes, bboxes_ext], 2)
        num_detections, det_boxes, det_scores, det_classes = self.rcnn_nms(
            scores, bboxes, num_bboxes, self.test_cfg.max_per_img)

        return num_detections, det_boxes, det_scores, det_classes
    def regress_by_class(self, stage, rois, label, bbox_pred):
        bbox_head = self.bbox_head[stage]
        reg_class_agnostic = bbox_head.reg_class_agnostic

        if not reg_class_agnostic:
            label = label * 4
            inds = torch.stack((label, label + 1, label + 2, label + 3), 1)
            bbox_pred = torch.gather(bbox_pred, 1, inds)

        means = bbox_head.bbox_coder.means
        stds = bbox_head.bbox_coder.stds

        new_rois = delta2bbox(rois, bbox_pred, means, stds)
        return new_rois
Пример #5
0
    def forward(self, feat, proposals):
        ms_scores = []
        batch_size = proposals.shape[0]
        num_proposals = proposals.shape[1]
        rois_pad = mm2trt_util.arange_by_input(proposals, 0).unsqueeze(1)
        rois_pad = rois_pad.repeat(1, num_proposals).view(-1, 1)
        proposals = proposals.view(-1, 4)
        rois = proposals

        for i in range(self.num_stages):
            bbox_results = self._bbox_forward(
                i, feat, torch.cat([rois_pad, rois], dim=1))
            ms_scores.append(bbox_results['cls_score'])
            bbox_pred = bbox_results['bbox_pred']

            if i < self.num_stages - 1:
                bbox_label = bbox_results['cls_score'].argmax(dim=1)
                rois = self.regress_by_class(i, rois, bbox_label, bbox_pred)

        ### bbox_head.get_boxes
        cls_score = sum(ms_scores) / self.num_stages

        if isinstance(cls_score, list):
            cls_score = sum(cls_score) / float(len(cls_score))
        scores = F.softmax(cls_score, dim=1)
        bbox_head = self.bbox_head[-1]
        bboxes = delta2bbox(rois, bbox_pred, bbox_head.bbox_coder.means,
                            bbox_head.bbox_coder.stds)

        scores = scores.view(batch_size, num_proposals, -1)
        bboxes = bboxes.view(batch_size, num_proposals, -1, 4)
        bboxes = bboxes.repeat(1, 1, bbox_head.num_classes, 1)
        bboxes_ext = bboxes[:, :, 0:1, :] * 0
        bboxes = torch.cat([bboxes, bboxes_ext], 2)
        num_bboxes = bboxes.shape[1]
        num_detections, det_boxes, det_scores, det_classes = self.rcnn_nms(
            scores, bboxes, num_bboxes, self.test_cfg.max_per_img)

        return num_detections, det_boxes, det_scores, det_classes
def delta2bbox_custom_func(cls_scores, bbox_preds, anchors,
                      min_num_bboxes, num_classes, use_sigmoid_cls,
                      target_mean, target_std, input_x=None):
                      
    rpn_cls_score = cls_scores.squeeze(0)
    rpn_bbox_pred = bbox_preds.squeeze(0)
    anchors = anchors.squeeze(0)
    # rpn_cls_score = rpn_cls_score.permute(0, 2, 3, 1)
    rpn_cls_score = rpn_cls_score.permute(1, 2, 0)
    if use_sigmoid_cls:
        # rpn_cls_score = rpn_cls_score.reshape(-1)
        scores = rpn_cls_score.sigmoid()
    else:
        rpn_cls_score = rpn_cls_score.reshape(-1, num_classes)
        scores = rpn_cls_score.softmax(dim=1)

    rpn_bbox_pred = rpn_bbox_pred.permute(1, 2, 0).reshape(-1, 4)
    
    # anchors = mlvl_anchors[idx]
    img_meta = None
    if input_x is not None:
        height, width = input_x.shape[2:]
        img_meta = (height, width)
    proposals = delta2bbox(anchors, rpn_bbox_pred, target_mean,
                            target_std, img_meta)

    scores = scores.contiguous().view(1,-1, num_classes)
    proposals = proposals.view(1, -1, 4)

    if scores.shape[1]<min_num_bboxes:
        pad_size = min_num_bboxes-scores.shape[1]
        scores = torch.nn.functional.pad(scores, [0, 0, 0, pad_size, 0, 0], mode='constant', value=0)
        proposals = torch.nn.functional.pad(proposals, [0, 0, 0, pad_size, 0, 0], mode='constant', value=0)
        
    proposals = proposals.view(1, -1, 1, 4)

    return scores, proposals
Пример #7
0
def bbox_target_single_tsd(
    pos_bboxes,
    neg_bboxes,
    pos_gt_bboxes,
    pos_gt_labels,
    rois,
    delta_c,
    delta_r,
    cls_score_,
    bbox_pred_,
    TSD_cls_score_,
    TSD_bbox_pred_,
    cfg,
    reg_classes=1,
    cls_pc_margin=0.2,
    loc_pc_margin=0.2,
    target_means=[0.0, 0.0, 0.0, 0.0],
    target_stds=[1.0, 1.0, 1.0, 1.0],
):
    num_pos = pos_bboxes.size(0)
    num_neg = neg_bboxes.size(0)
    num_samples = num_pos + num_neg
    labels = pos_bboxes.new_zeros(num_samples, dtype=torch.long)
    label_weights = pos_bboxes.new_zeros(num_samples)
    bbox_targets = pos_bboxes.new_zeros(num_samples, 4)
    bbox_weights = pos_bboxes.new_zeros(num_samples, 4)

    TSD_labels = pos_bboxes.new_zeros(num_samples, dtype=torch.long)
    TSD_label_weights = pos_bboxes.new_zeros(num_samples)
    TSD_bbox_targets = pos_bboxes.new_zeros(num_samples, 4)
    TSD_bbox_weights = pos_bboxes.new_zeros(num_samples, 4)

    # generte P_r according to delta_r and rois
    w = rois[:, 3] - rois[:, 1] + 1
    h = rois[:, 4] - rois[:, 2] + 1
    scale = 0.1
    rois_r = rois.new_zeros(rois.shape[0], rois.shape[1])
    rois_r[:, 0] = rois[:, 0]
    rois_r[:, 1] = rois[:, 1] + delta_r[:, 0] * scale * w
    rois_r[:, 2] = rois[:, 2] + delta_r[:, 1] * scale * h
    rois_r[:, 3] = rois[:, 3] + delta_r[:, 0] * scale * w
    rois_r[:, 4] = rois[:, 4] + delta_r[:, 1] * scale * h
    TSD_pos_rois = rois_r[:num_pos]
    pos_rois = rois[:num_pos]
    pc_cls_loss = rois.new_zeros(1)
    pc_loc_loss = rois.new_zeros(1)
    if num_pos > 0:
        labels[:num_pos] = pos_gt_labels
        TSD_labels[:num_pos] = pos_gt_labels
        pos_weight = 1.0 if cfg.pos_weight <= 0 else cfg.pos_weight
        label_weights[:num_pos] = pos_weight
        TSD_label_weights[:num_pos] = pos_weight
        pos_bbox_targets = bbox2delta(pos_bboxes, pos_gt_bboxes, target_means,
                                      target_stds)
        TSD_pos_bbox_targets = bbox2delta(TSD_pos_rois[:, 1:], pos_gt_bboxes,
                                          target_means, target_stds)
        bbox_targets[:num_pos, :] = pos_bbox_targets
        bbox_weights[:num_pos, :] = 1
        TSD_bbox_targets[:num_pos, :] = TSD_pos_bbox_targets
        TSD_bbox_weights[:num_pos, :] = 1

        # compute PC for TSD
        # 1. compute the PC for classification
        cls_score_soft = F.softmax(cls_score_, dim=1)
        TSD_cls_score_soft = F.softmax(TSD_cls_score_, dim=1)
        cls_pc_margin = (torch.tensor(cls_pc_margin).to(
            labels.device).to(dtype=cls_score_soft.dtype))
        cls_pc_margin = torch.min(
            1 - cls_score_soft[np.arange(len(TSD_labels)), labels],
            cls_pc_margin).detach()
        pc_cls_loss = F.relu(
            -(TSD_cls_score_soft[np.arange(len(TSD_labels)), TSD_labels] -
              cls_score_soft[np.arange(len(TSD_labels)), labels].detach() -
              cls_pc_margin))

        # 2. compute the PC for localization
        N = bbox_pred_.shape[0]
        bbox_pred_ = bbox_pred_.view(N, -1, 4)
        TSD_bbox_pred_ = TSD_bbox_pred_.view(N, -1, 4)

        sibling_head_bboxes = delta2bbox(
            pos_bboxes,
            bbox_pred_[np.arange(num_pos), labels[:num_pos]],
            means=target_means,
            stds=target_stds,
        )
        TSD_head_bboxes = delta2bbox(
            TSD_pos_rois[:, 1:],
            TSD_bbox_pred_[np.arange(num_pos), TSD_labels[:num_pos]],
            means=target_means,
            stds=target_stds,
        )

        ious, gious = iou_overlaps(sibling_head_bboxes, pos_gt_bboxes)
        TSD_ious, TSD_gious = iou_overlaps(TSD_head_bboxes, pos_gt_bboxes)
        loc_pc_margin = torch.tensor(loc_pc_margin).to(
            ious.device).to(dtype=ious.dtype)
        loc_pc_margin = torch.min(1 - ious.detach(), loc_pc_margin).detach()
        pc_loc_loss = F.relu(-(TSD_ious - ious.detach() - loc_pc_margin))

    if num_neg > 0:
        label_weights[-num_neg:] = 1.0
        TSD_label_weights[-num_neg:] = 1.0

    return (
        labels,
        label_weights,
        bbox_targets,
        bbox_weights,
        TSD_labels,
        TSD_label_weights,
        TSD_bbox_targets,
        TSD_bbox_weights,
        pc_cls_loss,
        pc_loc_loss,
    )