Esempio n. 1
0
def test_assigner_sampler():
    try:
        from mmdet.core.bbox import build_assigner, build_sampler
    except (ImportError, ModuleNotFoundError):
        raise ImportError(
            'Failed to import `build_assigner` and `build_sampler` '
            'from `mmdet.core.bbox`. The two APIs are required for '
            'the testing in `test_bbox.py`! ')
    data_prefix = osp.normpath(
        osp.join(osp.dirname(__file__), '../data/eval_detection'))
    ann_file = osp.join(data_prefix, 'gt.csv')
    label_file = osp.join(data_prefix, 'action_list.txt')
    proposal_file = osp.join(data_prefix, 'proposal.pkl')
    dataset = AVADataset(ann_file=ann_file,
                         exclude_file=None,
                         pipeline=[],
                         label_file=label_file,
                         proposal_file=proposal_file,
                         num_classes=4)

    assigner = dict(type='MaxIoUAssignerAVA',
                    pos_iou_thr=0.5,
                    neg_iou_thr=0.5,
                    min_pos_iou=0.5)
    assigner = build_assigner(assigner)
    proposal = torch.tensor(dataset[0]['proposals'])

    gt_bboxes = torch.tensor(dataset[0]['gt_bboxes'])
    gt_labels = torch.tensor(dataset[0]['gt_labels'])
    assign_result = assigner.assign(bboxes=proposal,
                                    gt_bboxes=gt_bboxes,
                                    gt_bboxes_ignore=None,
                                    gt_labels=gt_labels)
    assert assign_result.num_gts == 4
    assert torch.all(
        assign_result.gt_inds == torch.tensor([0, 0, 3, 3, 0, 0, 0, 1, 0, 0]))
    assert torch.all(
        torch.isclose(
            assign_result.max_overlaps,
            torch.tensor([
                0.40386841, 0.47127257, 0.53544776, 0.58797631, 0.29281288,
                0.40979504, 0.45902917, 0.50093938, 0.21560125, 0.32948171
            ],
                         dtype=torch.float64)))
    assert torch.all(
        torch.isclose(
            assign_result.labels,
            torch.tensor([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 1., 0., 0.],
                          [0., 1., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.],
                          [0., 0., 0., 0.], [0., 0., 0., 1.], [0., 0., 0., 0.],
                          [0., 0., 0., 0.]])))
    sampler = dict(type='RandomSampler', num=32, pos_fraction=1)
    sampler = build_sampler(sampler)
    sampling_result = sampler.sample(assign_result, proposal, gt_bboxes,
                                     gt_labels)
    assert (sampling_result.pos_inds.shape[0] ==
            sampling_result.pos_bboxes.shape[0])
    assert (sampling_result.neg_inds.shape[0] ==
            sampling_result.neg_bboxes.shape[0])
    return sampling_result
Esempio n. 2
0
def test_assigner_sampler():
    data_prefix = osp.join(osp.dirname(__file__), 'data/test_eval_detection')
    ann_file = osp.join(data_prefix, 'gt.csv')
    label_file = osp.join(data_prefix, 'action_list.txt')
    proposal_file = osp.join(data_prefix, 'proposal.pkl')
    dataset = AVADataset(ann_file=ann_file,
                         exclude_file=None,
                         pipeline=[],
                         label_file=label_file,
                         proposal_file=proposal_file,
                         num_classes=4)

    assigner = dict(type='MaxIoUAssignerAVA',
                    pos_iou_thr=0.5,
                    neg_iou_thr=0.5,
                    min_pos_iou=0.5)
    assigner = build_assigner(assigner)
    proposal = torch.tensor(dataset[0]['proposals'])

    gt_bboxes = torch.tensor(dataset[0]['gt_bboxes'])
    gt_labels = torch.tensor(dataset[0]['gt_labels'])
    assign_result = assigner.assign(bboxes=proposal,
                                    gt_bboxes=gt_bboxes,
                                    gt_bboxes_ignore=None,
                                    gt_labels=gt_labels)
    assert assign_result.num_gts == 4
    assert torch.all(
        assign_result.gt_inds == torch.tensor([0, 0, 3, 3, 0, 0, 0, 1, 0, 0]))
    assert torch.all(
        torch.isclose(
            assign_result.max_overlaps,
            torch.tensor([
                0.40386841, 0.47127257, 0.53544776, 0.58797631, 0.29281288,
                0.40979504, 0.45902917, 0.50093938, 0.21560125, 0.32948171
            ],
                         dtype=torch.float64)))
    assert torch.all(
        torch.isclose(
            assign_result.labels,
            torch.tensor([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 1., 0., 0.],
                          [0., 1., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.],
                          [0., 0., 0., 0.], [0., 0., 0., 1.], [0., 0., 0., 0.],
                          [0., 0., 0., 0.]])))
    sampler = dict(type='RandomSampler', num=32, pos_fraction=1)
    sampler = build_sampler(sampler)
    sampling_result = sampler.sample(assign_result, proposal, gt_bboxes,
                                     gt_labels)
    assert (sampling_result.pos_inds.shape[0] ==
            sampling_result.pos_bboxes.shape[0])
    assert (sampling_result.neg_inds.shape[0] ==
            sampling_result.neg_bboxes.shape[0])
    return sampling_result
    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)
Esempio n. 4
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):
    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_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
    # import pdb
    # print('in anchor target')
    # pdb.set_trace()
    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

    # 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)
Esempio n. 5
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)
Esempio n. 6
0
def radius_target_single(flat_anchors,
                         valid_flags,
                         bbox_pred,
                         gt_bboxes,
                         gt_cheby,
                         gt_skeleton,
                         gt_bboxes_ignore,
                         gt_labels,
                         img_meta,
                         target_means,
                         target_stds,
                         num_coords,
                         cfg,
                         label_channels=1,
                         sampling=True,
                         unmap_outputs=True):
    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, :]
    #     print('at cheby_target, gt_bboxes_ignore:', gt_bboxes_ignore)
    if sampling:
        assign_result, sampling_result = assign_and_sample(
            anchors, gt_bboxes, gt_cheby, gt_skeleton, 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]

    labels = anchors.new_zeros(num_valid_anchors, dtype=torch.long)
    label_weights = anchors.new_zeros(num_valid_anchors, dtype=torch.float)
    bbox_targets = torch.zeros((num_valid_anchors, num_coords - 3),
                               dtype=torch.float).cuda()
    bbox_weights = torch.zeros((num_valid_anchors, num_coords - 3),
                               dtype=torch.float).cuda()
    ctr_targets = torch.zeros((num_valid_anchors, 3), dtype=torch.float).cuda()
    ctr_weights = torch.zeros((num_valid_anchors, 3), dtype=torch.float).cuda()

    pos_inds = sampling_result.pos_inds
    neg_inds = sampling_result.neg_inds
    if len(pos_inds) > 0:
        deltas, weights = bbox2radius(sampling_result.pos_bboxes,
                                      sampling_result.pos_gt_bboxes,
                                      sampling_result.pos_gt_skeleton,
                                      num_coords, target_means, target_stds)

        bbox_targets[pos_inds, :] = deltas[:, :-3]
        bbox_weights[pos_inds, :] = weights.unsqueeze(
            1) if cfg.use_centerness else 1.0
        ctr_targets[pos_inds, :] = deltas[:, -3:]
        ctr_weights[pos_inds, :] = weights.unsqueeze(
            1) if cfg.use_centerness else 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] = weights if cfg.use_centerness else 1.0
        else:
            label_weights[pos_inds] = cfg.pos_weight
        # print("pos:", len(pos_inds), "neg:", len(neg_inds),  weights)
    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)
        ctr_targets = unmap(ctr_targets, num_total_anchors, inside_flags)
        ctr_weights = unmap(ctr_weights, num_total_anchors, inside_flags)
    return (labels, label_weights, bbox_targets, bbox_weights, ctr_targets,
            ctr_weights, pos_inds, neg_inds)
Esempio n. 7
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)
Esempio n. 8
0
def offset_target_single(flat_anchors,
                         valid_flags,
                         bbox_pred,
                         gt_bboxes,
                         gt_cheby,
                         gt_skeleton,
                         gt_bboxes_ignore,
                         gt_labels,
                         img_meta,
                         target_means,
                         target_stds,
                         num_coords,
                         cfg,
                         label_channels=1,
                         sampling=True,
                         unmap_outputs=True):
    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_cheby, gt_skeleton, 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]

    labels = anchors.new_zeros(num_valid_anchors, dtype=torch.long)
    label_weights = anchors.new_zeros(num_valid_anchors, dtype=torch.float)
    bbox_targets = torch.zeros((num_valid_anchors, num_coords),
                               dtype=torch.float).cuda()
    bbox_weights = torch.zeros((num_valid_anchors, num_coords),
                               dtype=torch.float).cuda()

    pos_inds = sampling_result.pos_inds
    neg_inds = sampling_result.neg_inds
    if len(pos_inds) > 0:
        deltas, weights = bbox2offset(sampling_result.pos_bboxes,
                                      sampling_result.pos_gt_bboxes,
                                      target_means, target_stds)
        bbox_targets[pos_inds, :] = deltas
        bbox_weights[pos_inds, :] = weights.unsqueeze(
            1) if cfg.use_centerness else 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] = weights if cfg.use_centerness else 1.0
        else:
            label_weights[pos_inds] = cfg.pos_weight
        # start adaptive weights
        use_adaptive_weights = False
        if use_adaptive_weights:
            pos_weights = torch.norm(pos_bbox_targets[:, -2:], dim=1)
            pos_weights = 1.0 / (0.5 + pos_weights)
            bbox_weights[pos_inds, :] = pos_weights.reshape(-1, 1)
            label_weights[pos_inds] = pos_weights
        # end of adaptive weights
    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)