コード例 #1
0
    def anchor_target_single(self,
                             anchors,
                             gt_bboxes,
                             gt_bboxes_ignore,
                             gt_labels,
                             img_meta,
                             target_means,
                             target_stds,
                             cfg,
                             label_channels=1,
                             sampling=True,
                             unmap_outputs=True):
        if sampling:
            assign_result, sampling_result = assign_and_sample(
                anchors, gt_bboxes, gt_bboxes_ignore, None, cfg)
        else:
            bbox_assigner = build_assigner(cfg.assigner)
            assign_result = bbox_assigner.assign(anchors, gt_bboxes,
                                                 gt_bboxes_ignore, gt_labels)
            bbox_sampler = PseudoSampler()
            sampling_result = bbox_sampler.sample(assign_result, anchors,
                                                  gt_bboxes)

        num_valid_anchors = anchors.shape[0]
        bbox_targets = torch.zeros_like(anchors)
        bbox_weights = torch.zeros_like(anchors)
        labels = anchors.new_zeros(num_valid_anchors, dtype=torch.long)
        label_weights = anchors.new_zeros(num_valid_anchors, dtype=torch.float)

        pos_inds = sampling_result.pos_inds
        neg_inds = sampling_result.neg_inds
        if len(pos_inds) > 0:
            pos_bbox_targets = bbox2delta(sampling_result.pos_bboxes,
                                          sampling_result.pos_gt_bboxes,
                                          target_means, target_stds)
            bbox_targets[pos_inds, :] = pos_bbox_targets
            bbox_weights[pos_inds, :] = 1.0
            if gt_labels is None:
                labels[pos_inds] = 1
            else:
                labels[pos_inds] = gt_labels[
                    sampling_result.pos_assigned_gt_inds]
            if cfg.pos_weight <= 0:
                label_weights[pos_inds] = 1.0
            else:
                label_weights[pos_inds] = cfg.pos_weight
        if len(neg_inds) > 0:
            label_weights[neg_inds] = 1.0

        return (labels, label_weights, bbox_targets, bbox_weights, pos_inds,
                neg_inds)
コード例 #2
0
def dense_reppoints_target_sinle(flat_proposals,
                                 flat_proposals_pts,
                                 num_level_proposals,
                                 valid_flags,
                                 gt_bboxes,
                                 gt_masks,
                                 gt_bboxes_ignore,
                                 gt_labels,
                                 cfg,
                                 label_channels=1,
                                 sampling=True,
                                 unmap_outputs=True,
                                 num_pts=49):
    inside_flags = valid_flags
    num_level_proposals_inside = get_num_level_proposals_inside(
        num_level_proposals, inside_flags)

    if not inside_flags.any():
        return (None, ) * 8
    # assign gt and sample points
    proposals = flat_proposals[inside_flags, :]
    proposals_pts = flat_proposals_pts[inside_flags, :]
    if sampling:
        assign_result, sampling_result = assign_and_sample(
            proposals, gt_bboxes, gt_bboxes_ignore, None, cfg)
    else:
        bbox_assigner = build_assigner(cfg.assigner)
        if cfg.assigner.type != "ATSSAssigner":
            assign_result = bbox_assigner.assign(proposals, gt_bboxes, None,
                                                 gt_labels)
        else:
            assign_result = bbox_assigner.assign(proposals,
                                                 num_level_proposals_inside,
                                                 gt_bboxes, None, gt_labels)
        bbox_sampler = PseudoSampler()
        sampling_result = bbox_sampler.sample(assign_result, proposals,
                                              gt_bboxes)

    gt_ind = sampling_result.pos_assigned_gt_inds.cpu().numpy()
    sample_func = cfg.get('sample_func', 'distance_sample_pts')
    gt_pts_numpy = eval(sample_func)(gt_bboxes, gt_masks, cfg, num_pts)

    pts_label_list = []
    proposals_pos_pts = proposals_pts[
        sampling_result.pos_inds, :].detach().cpu().numpy().round().astype(
            np.long)
    for i in range(len(gt_ind)):
        gt_mask = gt_masks[gt_ind[i]]
        h, w = gt_mask.shape
        pts_long = proposals_pos_pts[i]
        _pts_label = gt_mask[pts_long[1::2].clip(0, h - 1),
                             pts_long[0::2].clip(0, w - 1)]
        pts_label_list.append(_pts_label)
    del proposals_pos_pts

    if len(gt_ind) != 0:
        gt_pts = gt_bboxes.new_tensor(gt_pts_numpy)
        pos_gt_pts = gt_pts[gt_ind]
        pts_label = np.stack(pts_label_list, 0)
        pos_gt_pts_label = gt_bboxes.new_tensor(pts_label)
    else:
        pos_gt_pts = None
        pos_gt_pts_label = None

    num_valid_proposals = proposals.shape[0]
    bbox_gt = proposals.new_zeros([num_valid_proposals, 4])
    mask_gt = proposals.new_zeros([0, num_pts * 2])
    mask_gt_label = proposals.new_zeros([0, num_pts]).long()
    mask_gt_index = proposals.new_zeros([
        num_valid_proposals,
    ],
                                        dtype=torch.long)
    pos_proposals = torch.zeros_like(proposals)
    proposals_weights = proposals.new_zeros([num_valid_proposals, 4])
    labels = proposals.new_zeros(num_valid_proposals, dtype=torch.long)
    label_weights = proposals.new_zeros(num_valid_proposals, dtype=torch.float)

    pos_inds = sampling_result.pos_inds
    neg_inds = sampling_result.neg_inds
    if len(pos_inds) > 0:
        pos_gt_bboxes = sampling_result.pos_gt_bboxes
        bbox_gt[pos_inds, :] = pos_gt_bboxes
        if pos_gt_pts is not None:
            mask_gt = pos_gt_pts.type(bbox_gt.type())
            mask_gt_index[pos_inds] = torch.arange(
                len(pos_inds)).long().cuda() + 1
        if pos_gt_pts_label is not None:
            mask_gt_label = pos_gt_pts_label.long()
        pos_proposals[pos_inds, :] = proposals[pos_inds, :]
        proposals_weights[pos_inds, :] = 1.0
        if gt_labels is None:
            labels[pos_inds] = 1
        else:
            labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds]
        if cfg.pos_weight <= 0:
            label_weights[pos_inds] = 1.0
        else:
            label_weights[pos_inds] = cfg.pos_weight
    if len(neg_inds) > 0:
        label_weights[neg_inds] = 1.0

    # map up to original set of grids
    if unmap_outputs:
        num_total_proposals = flat_proposals.size(0)
        labels = unmap(labels, num_total_proposals, inside_flags)
        label_weights = unmap(label_weights, num_total_proposals, inside_flags)
        bbox_gt = unmap(bbox_gt, num_total_proposals, inside_flags)
        mask_gt_index = unmap(mask_gt_index, num_total_proposals, inside_flags)
        pos_proposals = unmap(pos_proposals, num_total_proposals, inside_flags)
        proposals_weights = unmap(proposals_weights, num_total_proposals,
                                  inside_flags)

    return (labels, label_weights, bbox_gt, mask_gt_index, mask_gt,
            mask_gt_label, pos_proposals, proposals_weights, pos_inds,
            neg_inds)
コード例 #3
0
def anchor_target_single(flat_anchors,
                         valid_flags,
                         gt_bboxes,
                         gt_bboxes_ignore,
                         gt_labels,
                         img_meta,
                         target_means,
                         target_stds,
                         cfg,
                         label_channels=1,
                         sampling=True,
                         unmap_outputs=True,
                         encoder=None):
    inside_flags = anchor_inside_flags(flat_anchors, valid_flags,
                                       img_meta['img_shape'][:2],
                                       cfg.allowed_border)
    if not inside_flags.any():
        return (None, ) * 6
    # assign gt and sample anchors
    anchors = flat_anchors[inside_flags, :]

    if sampling:
        assign_result, sampling_result = assign_and_sample(
            anchors, gt_bboxes, gt_bboxes_ignore, None, cfg)
    else:
        bbox_assigner = build_fsaf_assigner(cfg.assigner)
        assign_result = bbox_assigner.assign(anchors, gt_bboxes,
                                             gt_bboxes_ignore, gt_labels)
        bbox_sampler = PseudoSampler()
        sampling_result = bbox_sampler.sample(assign_result, anchors,
                                              gt_bboxes)

    num_valid_anchors = anchors.shape[0]
    bbox_targets = torch.zeros_like(anchors)
    bbox_weights = torch.zeros_like(anchors)
    labels = anchors.new_zeros(num_valid_anchors, dtype=torch.long)
    label_weights = anchors.new_zeros(num_valid_anchors, dtype=torch.float)
    pos_assigned_gt_inds = anchors.new_zeros(num_valid_anchors,
                                             dtype=torch.long) - 1

    pos_inds = sampling_result.pos_inds
    neg_inds = sampling_result.neg_inds
    if len(pos_inds) > 0:
        if encoder is None:
            pos_bbox_targets = bbox2delta(sampling_result.pos_bboxes,
                                          sampling_result.pos_gt_bboxes,
                                          target_means, target_stds)
        else:
            pos_bbox_targets = encoder(sampling_result.pos_bboxes,
                                       sampling_result.pos_gt_bboxes)
        bbox_targets[pos_inds, :] = pos_bbox_targets
        bbox_weights[pos_inds, :] = 1.0
        pos_assigned_gt_inds[pos_inds] = sampling_result.pos_assigned_gt_inds
        if gt_labels is None:
            labels[pos_inds] = 1
        else:
            labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds]
        if cfg.pos_weight <= 0:
            label_weights[pos_inds] = 1.0
        else:
            label_weights[pos_inds] = cfg.pos_weight
    if len(neg_inds) > 0:
        label_weights[neg_inds] = 1.0

    # map up to original set of anchors
    if unmap_outputs:
        num_total_anchors = flat_anchors.size(0)
        labels = unmap(labels, num_total_anchors, inside_flags)
        label_weights = unmap(label_weights, num_total_anchors, inside_flags)
        bbox_targets = unmap(bbox_targets, num_total_anchors, inside_flags)
        bbox_weights = unmap(bbox_weights, num_total_anchors, inside_flags)
        pos_assigned_gt_inds = unmap(pos_assigned_gt_inds,
                                     num_total_anchors,
                                     inside_flags,
                                     fill=-1)

    return (labels, label_weights, bbox_targets, bbox_weights, pos_inds,
            neg_inds, pos_assigned_gt_inds)
コード例 #4
0
def anchor_target_rbbox_single(flat_anchors,
                               valid_flags,
                               gt_bboxes,
                               gt_masks,
                               gt_bboxes_ignore,
                               gt_labels,
                               img_meta,
                               target_means,
                               target_stds,
                               cfg,
                               label_channels=1,
                               sampling=True,
                               unmap_outputs=True,
                               with_module=True,
                               hbb_trans='hbb2obb_v2'):
    inside_flags = anchor_inside_flags(flat_anchors, valid_flags,
                                       img_meta['img_shape'][:2],
                                       cfg.allowed_border)
    if not inside_flags.any():
        return (None, ) * 6
    # assign gt and sample anchors
    # import pdb
    # print('in anchor target rbbox single')
    # pdb.set_trace()
    anchors = flat_anchors[inside_flags, :]

    if sampling:
        assign_result, sampling_result = assign_and_sample(
            anchors, gt_bboxes, gt_bboxes_ignore, None, cfg)
    else:
        bbox_assigner = build_assigner(cfg.assigner)
        assign_result = bbox_assigner.assign(anchors, gt_bboxes,
                                             gt_bboxes_ignore, gt_labels)
        bbox_sampler = PseudoSampler()
        sampling_result = bbox_sampler.sample(assign_result, anchors,
                                              gt_bboxes)

    num_valid_anchors = anchors.shape[0]
    # anchors shape, [num_anchors, 4]
    # bbox_targets = torch.zeros_like(anchors)
    # bbox_weights = torch.zeros_like(anchors)
    bbox_targets = torch.zeros(num_valid_anchors, 5).to(anchors.device)
    bbox_weights = torch.zeros(num_valid_anchors, 5).to(anchors.device)

    labels = anchors.new_zeros(num_valid_anchors, dtype=torch.long)
    label_weights = anchors.new_zeros(num_valid_anchors, dtype=torch.float)

    pos_inds = sampling_result.pos_inds
    neg_inds = sampling_result.neg_inds

    # TODO: copy the code in mask target to here. trans gt_masks to gt_rbboxes
    pos_assigned_gt_inds = sampling_result.pos_assigned_gt_inds
    # implementation A
    # pos_gt_masks = gt_masks[pos_assigned_gt_inds.cpu().numpy()]
    # pos_gt_obbs = gt_mask_bp_obbs(pos_gt_masks)
    # pos_gt_obbs_ts = torch.from_numpy(pos_gt_obbs).to(sampling_result.pos_bboxes.device)
    # implementation B
    gt_obbs = gt_mask_bp_obbs(gt_masks, with_module)
    gt_obbs_ts = torch.from_numpy(gt_obbs).to(
        sampling_result.pos_bboxes.device)
    pos_gt_obbs_ts = gt_obbs_ts[pos_assigned_gt_inds]
    if len(pos_inds) > 0:
        # pos_bbox_targets = bbox2delta(sampling_result.pos_bboxes,
        #                               sampling_result.pos_gt_bboxes,
        #                               target_means, target_stds)
        # if hbb_trans == 'hbb2obb':
        #     pos_ext_bboxes = hbb2obb(sampling_result.pos_bboxes)
        # elif hbb_trans == 'hbbpolyobb':
        #     pos_ext_bboxes = hbbpolyobb(sampling_result.pos_bboxes)
        # elif hbb_trans == 'hbb2obb_v2':
        #     pos_ext_bboxes = hbb2obb_v2(sampling_result.pos_bboxes)
        # else:
        #     print('no such hbb2obb trans function')
        #     raise Exception
        pos_ext_bboxes = hbb2obb_v2(sampling_result.pos_bboxes)
        if with_module:
            pos_bbox_targets = dbbox2delta(pos_ext_bboxes, pos_gt_obbs_ts,
                                           target_means, target_stds)
        else:
            pos_bbox_targets = dbbox2delta_v3(pos_ext_bboxes, pos_gt_obbs_ts,
                                              target_means, target_stds)
        bbox_targets[pos_inds, :] = pos_bbox_targets
        bbox_weights[pos_inds, :] = 1.0
        if gt_labels is None:
            labels[pos_inds] = 1
        else:
            labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds]
        if cfg.pos_weight <= 0:
            label_weights[pos_inds] = 1.0
        else:
            label_weights[pos_inds] = cfg.pos_weight
    if len(neg_inds) > 0:
        label_weights[neg_inds] = 1.0

    # map up to original set of anchors
    if unmap_outputs:
        num_total_anchors = flat_anchors.size(0)
        labels = unmap(labels, num_total_anchors, inside_flags)
        label_weights = unmap(label_weights, num_total_anchors, inside_flags)
        bbox_targets = unmap(bbox_targets, num_total_anchors, inside_flags)
        bbox_weights = unmap(bbox_weights, num_total_anchors, inside_flags)

    return (labels, label_weights, bbox_targets, bbox_weights, pos_inds,
            neg_inds)