Exemple #1
0
def fpn_anchor_target_opr_core_impl(gt_boxes,
                                    im_info,
                                    anchors,
                                    allow_low_quality_matches=True):

    ignore_label = config.ignore_label
    # get the gt boxes
    gtboxes = gt_boxes[:im_info[5].astype(np.int32)]
    ignore_mask = F.equal(gtboxes[:, 4], config.ignore_label)

    # find the valid gtboxes
    _, index = F.cond_take(1 - ignore_mask > 0, ignore_mask)
    valid_gt_boxes = gtboxes[index.astype(np.int32)]

    # compute the iou matrix
    overlaps = box_overlap_opr(anchors, valid_gt_boxes[:, :4])
    # match the dtboxes
    a_shp0 = anchors.shape[0]
    argmax_overlaps = F.argmax(overlaps, axis=1)
    max_overlaps = F.nn.indexing_one_hot(overlaps,
                                         argmax_overlaps.astype(np.int32), 1)

    labels = F.ones(a_shp0).astype(np.int32) * ignore_label
    # set negative ones
    labels = labels * (max_overlaps >= config.rpn_negative_overlap).astype(
        np.float32)

    # set positive ones
    fg_mask = (max_overlaps >= config.rpn_positive_overlap)
    const_one = mge.tensor(1.0)

    if allow_low_quality_matches:

        # match the max gt
        gt_max_overlaps = F.max(overlaps, axis=0)
        gt_argmax_overlaps = F.argmax(overlaps, axis=0)
        gt_argmax_overlaps = gt_argmax_overlaps.astype(np.int32)

        max_overlaps[gt_argmax_overlaps] = 1.
        m = gt_max_overlaps.shape[0]
        argmax_overlaps[gt_argmax_overlaps] = F.linspace(0, m - 1,
                                                         m).astype(np.int32)
        fg_mask = (max_overlaps >= config.rpn_positive_overlap)

    labels[fg_mask] = 1
    # compute the bbox targets
    bbox_targets = bbox_transform_opr(anchors,
                                      valid_gt_boxes[argmax_overlaps, :4])
    if config.rpn_bbox_normalize_targets:

        std_opr = mge.tensor(config.bbox_normalize_stds[None, :]).to(
            anchors.device)
        mean_opr = mge.tensor(config.bbox_normalize_means[None, :]).to(
            anchors.device)
        minus_opr = mean_opr / std_opr
        bbox_targets = bbox_targets / std_opr - minus_opr
    return labels, bbox_targets
def fpn_anchor_target_opr_core_impl(gt_boxes,
                                    im_info,
                                    anchors,
                                    allow_low_quality_matches=True):
    ignore_label = config.ignore_label
    # get the gt boxes
    valid_gt_boxes = gt_boxes[:im_info[5], :]
    non_ignore_mask = valid_gt_boxes[:, -1] > 0
    non_ignore_inds = mask_to_inds(non_ignore_mask)
    valid_gt_boxes = valid_gt_boxes.ai[non_ignore_inds]
    # compute the iou matrix
    overlaps = box_overlap_opr(anchors, valid_gt_boxes[:, :4])
    # match the dtboxes
    a_shp0 = anchors.shape[0]
    max_overlaps = F.max(overlaps, axis=1)
    argmax_overlaps = F.argmax(overlaps, axis=1)
    # all ignore
    labels = mge.ones(a_shp0).astype(np.int32) * ignore_label
    # set negative ones
    labels = labels * (max_overlaps >= config.rpn_negative_overlap)
    # set positive ones
    fg_mask = (max_overlaps >= config.rpn_positive_overlap)
    const_one = mge.tensor(1.0)
    if allow_low_quality_matches:
        # match the max gt
        gt_max_overlaps = F.max(overlaps, axis=0)
        gt_argmax_overlaps = F.argmax(overlaps, axis=0)
        g_shp0 = valid_gt_boxes.shapeof()[0]
        gt_id = F.linspace(0, g_shp0 - 1, g_shp0).astype(np.int32)
        argmax_overlaps = argmax_overlaps.set_ai(gt_id)[gt_argmax_overlaps]
        max_overlaps = max_overlaps.set_ai(
            const_one.broadcast(g_shp0))[gt_argmax_overlaps]
        fg_mask = (max_overlaps >= config.rpn_positive_overlap)
    # set positive ones
    fg_mask_ind = mask_to_inds(fg_mask)
    labels = labels.set_ai(const_one.broadcast(
        fg_mask_ind.shapeof()))[fg_mask_ind]
    # compute the targets
    bbox_targets = bbox_transform_opr(anchors,
                                      valid_gt_boxes.ai[argmax_overlaps, :4])
    if config.rpn_bbox_normalize_targets:
        std_opr = mge.tensor(config.bbox_normalize_stds[None, :])
        mean_opr = mge.tensor(config.bbox_normalize_means[None, :])
        minus_opr = mean_opr / std_opr
        bbox_targets = bbox_targets / std_opr - minus_opr
    return labels, bbox_targets
Exemple #3
0
def cascade_roi_target(rpn_rois, im_info, gt_boxes, pos_threshold=0.5, top_k=1):
    return_rois = []
    return_labels = []
    return_bbox_targets = []
    # get per image proposals and gt_boxes
    for bid in range(config.batch_per_gpu):
        gt_boxes_perimg = gt_boxes[bid, :im_info[bid, 5], :]
        batch_inds = mge.ones((gt_boxes_perimg.shapeof()[0], 1)) * bid
        #if config.proposal_append_gt:
        gt_rois = F.concat([batch_inds, gt_boxes_perimg[:, :4]], axis=1)
        batch_roi_mask = rpn_rois[:, 0] == bid
        batch_roi_inds = mask_to_inds(batch_roi_mask)
        all_rois = F.concat([rpn_rois.ai[batch_roi_inds], gt_rois], axis=0)
        overlaps_normal, overlaps_ignore = box_overlap_ignore_opr(
                all_rois[:, 1:5], gt_boxes_perimg)
        overlaps_normal, overlaps_normal_indices = F.argsort(overlaps_normal, descending=True)
        overlaps_ignore, overlaps_ignore_indices = F.argsort(overlaps_ignore, descending=True)
        # gt max and indices, ignore max and indices
        max_overlaps_normal = overlaps_normal[:, :top_k].reshape(-1)
        gt_assignment_normal = overlaps_normal_indices[:, :top_k].reshape(-1)
        max_overlaps_ignore = overlaps_ignore[:, :top_k].reshape(-1)
        gt_assignment_ignore = overlaps_ignore_indices[:, :top_k].reshape(-1)
        # cons masks
        ignore_assign_mask = (max_overlaps_normal < config.fg_threshold) * (
                max_overlaps_ignore > max_overlaps_normal)
        max_overlaps = max_overlaps_normal * (1 - ignore_assign_mask) + \
                max_overlaps_ignore * ignore_assign_mask
        gt_assignment = gt_assignment_normal * (1- ignore_assign_mask) + \
                gt_assignment_ignore * ignore_assign_mask
        gt_assignment = gt_assignment.astype(np.int32)
        labels = gt_boxes_perimg.ai[gt_assignment, 4]
        fg_mask = (max_overlaps >= config.fg_threshold) * (1 - F.equal(labels, config.ignore_label))
        bg_mask = (max_overlaps < config.bg_threshold_high) * (
                max_overlaps >= config.bg_threshold_low)
        fg_mask = fg_mask.reshape(-1, top_k)
        bg_mask = bg_mask.reshape(-1, top_k)
        #pos_max = config.num_rois * config.fg_ratio
        #fg_inds_mask = _bernoulli_sample_masks(fg_mask[:, 0], pos_max, 1)
        #neg_max = config.num_rois - fg_inds_mask.sum()
        #bg_inds_mask = _bernoulli_sample_masks(bg_mask[:, 0], neg_max, 1)
        labels = labels * fg_mask.reshape(-1)
        #keep_mask = fg_inds_mask + bg_inds_mask
        #keep_inds = mask_to_inds(keep_mask)
        #keep_inds = keep_inds[:F.minimum(config.num_rois, keep_inds.shapeof()[0])]
        # labels
        labels = labels.reshape(-1, top_k)
        gt_assignment = gt_assignment.reshape(-1, top_k).reshape(-1)
        target_boxes = gt_boxes_perimg.ai[gt_assignment, :4]
        #rois = all_rois.ai[keep_inds]
        target_shape = (all_rois.shapeof()[0], top_k, all_rois.shapeof()[-1])
        target_rois = F.add_axis(all_rois, 1).broadcast(target_shape).reshape(-1, all_rois.shapeof()[-1])
        bbox_targets = bbox_transform_opr(target_rois[:, 1:5], target_boxes)
        if config.rcnn_bbox_normalize_targets:
            std_opr = mge.tensor(config.bbox_normalize_stds[None, :])
            mean_opr = mge.tensor(config.bbox_normalize_means[None, :])
            minus_opr = mean_opr / std_opr
            bbox_targets = bbox_targets / std_opr - minus_opr
        bbox_targets = bbox_targets.reshape(-1, top_k * 4)
        return_rois.append(all_rois)
        return_labels.append(labels)
        return_bbox_targets.append(bbox_targets)
    if config.batch_per_gpu == 1:
        return F.zero_grad(all_rois), F.zero_grad(labels), F.zero_grad(bbox_targets)
    else:
        return_rois = F.concat(return_rois, axis=0)
        return_labels = F.concat(return_labels, axis=0)
        return_bbox_targets = F.concat(return_bbox_targets, axis=0)
        return F.zero_grad(return_rois), F.zero_grad(return_labels), F.zero_grad(return_bbox_targets)
Exemple #4
0
def fpn_roi_target(rpn_rois,
                   im_info,
                   gt_boxes,
                   fg_threshold=config.fg_threshold,
                   top_k=1):

    return_rois, return_labels = [], []
    return_bbox_targets = []
    # get per image proposals and gt_boxes
    batch_per_gpu = im_info.shape[0]
    sampling = True
    # is_sample = True if top_k < 2 else False
    for bid in range(batch_per_gpu):

        gt_boxes_perimg = gt_boxes[bid, :im_info[bid, 5].astype(np.int32), :]
        dummy_gt = F.ones([1, gt_boxes_perimg.shape[1]])

        batch_inds = F.ones((gt_boxes_perimg.shape[0], 1)) * bid
        #if config.proposal_append_gt:
        gt_rois = F.concat([batch_inds, gt_boxes_perimg[:, :4]], axis=1)
        batch_rois_mask = F.equal(rpn_rois[:, 0], bid) > 0
        _, batch_rois_index = F.cond_take(batch_rois_mask, batch_rois_mask)

        # batch_roi_mask = rpn_rois[:, 0] == bid
        # batch_roi_inds = mask_to_inds(batch_roi_mask)
        all_rois= F.concat([rpn_rois[batch_rois_index], gt_rois], axis=0) if sampling \
            else rpn_rois[batch_rois_index]
        # all_rois = F.concat([rpn_rois.ai[batch_roi_inds], gt_rois], axis=0)

        gt_boxes_perimg = F.concat([gt_boxes_perimg, dummy_gt], axis=0)
        overlaps_normal, overlaps_ignore = box_overlap_ignore_opr(
            all_rois[:, 1:5], gt_boxes_perimg)

        # overlaps_normal, overlaps_normal_indices = F.argsort(overlaps_normal, descending=True)
        # overlaps_ignore, overlaps_ignore_indices = F.argsort(overlaps_ignore, descending=True)
        overlaps_normal_indices = F.argsort(overlaps_normal, descending=True)
        overlaps_normal = F.gather(overlaps_normal, 1, overlaps_normal_indices)
        # overlaps_normal = F.nn.indexing_one_hot(overlaps_normal, overlaps_normal_indices, 1)
        overlaps_ignore_indices = F.argsort(overlaps_ignore, descending=True)
        overlaps_ignore = F.gather(overlaps_ignore, 1, overlaps_ignore_indices)
        # overlaps_ignore = F.nn.indexing_one_hot(overlaps_ignore, overlaps_ignore_indices, 1)

        # gt max and indices, ignore max and indices
        max_overlaps_normal = overlaps_normal[:, :top_k].flatten()
        gt_assignment_normal = overlaps_normal_indices[:, :top_k].flatten()
        max_overlaps_ignore = overlaps_ignore[:, :top_k].flatten()
        gt_assignment_ignore = overlaps_ignore_indices[:, :top_k].flatten()
        # cons masks

        ignore_assign_mask = (max_overlaps_normal < fg_threshold).astype(
            np.float32) * (max_overlaps_ignore > max_overlaps_normal).astype(
                np.float32)
        max_overlaps = max_overlaps_normal * (1 - ignore_assign_mask).astype(np.float32) + \
                max_overlaps_ignore * ignore_assign_mask


        gt_assignment = gt_assignment_normal * (1- ignore_assign_mask) + \
                gt_assignment_ignore * ignore_assign_mask

        gt_assignment = gt_assignment.astype(np.int32)

        labels = gt_boxes_perimg[gt_assignment, 4]
        fg_mask = (max_overlaps >= fg_threshold).astype(
            np.float32) * (1 - F.equal(labels, config.ignore_label))
        bg_mask = (max_overlaps < config.bg_threshold_high).astype(
            np.float32) * (max_overlaps >= config.bg_threshold_low).astype(
                np.float32)

        fg_mask = fg_mask.reshape(-1, top_k)
        bg_mask = bg_mask.reshape(-1, top_k)
        pos_max = config.num_rois * config.fg_ratio
        fg_inds_mask = _bernoulli_sample_masks(
            fg_mask[:,
                    0], pos_max, 1) if sampling else F.equal(fg_mask[:, 0], 0)
        neg_max = config.num_rois - fg_inds_mask.sum()
        bg_inds_mask = _bernoulli_sample_masks(
            bg_mask[:,
                    0], neg_max, 1) if sampling else F.equal(bg_mask[:, 0], 0)
        labels = labels * fg_mask.reshape(-1)

        keep_mask = fg_inds_mask + bg_inds_mask
        keep_mask = keep_mask + F.equal(keep_mask.sum(), 0)
        # keep_inds = mask_to_inds(keep_mask)
        _, keep_inds = F.cond_take(keep_mask > 0, keep_mask)
        #keep_inds = keep_inds[:F.minimum(config.num_rois, keep_inds.shapeof()[0])]
        # labels
        labels = labels.reshape(-1, top_k)[keep_inds]
        gt_assignment = gt_assignment.reshape(
            -1, top_k)[keep_inds].reshape(-1).astype(np.int32)
        target_boxes = gt_boxes_perimg[gt_assignment, :4]
        # rois = all_rois.ai[keep_inds]
        rois = all_rois[keep_inds]
        # target_shape = (rois.shapeof()[0], top_k, rois.shapeof()[-1])
        n, c = rois.shape[0], rois.shape[1]
        target_rois = F.broadcast_to(F.expand_dims(rois, 1),
                                     (n, top_k, c)).reshape(-1, c)
        # target_rois = F.add_axis(rois, 1).broadcast(target_shape).reshape(-1, rois.shapeof()[-1])
        bbox_targets = bbox_transform_opr(target_rois[:, 1:5],
                                          target_boxes[:, :4])
        if config.rcnn_bbox_normalize_targets:
            std_opr = mge.tensor(config.bbox_normalize_stds[None, :]).to(
                rois.device)
            mean_opr = mge.tensor(config.bbox_normalize_means[None, :]).to(
                rois.device)
            minus_opr = mean_opr / std_opr
            bbox_targets = bbox_targets / std_opr - minus_opr
        bbox_targets = bbox_targets.reshape(-1, top_k * 4)
        return_rois.append(rois)
        return_labels.append(labels)
        return_bbox_targets.append(bbox_targets)
    if config.batch_per_gpu == 1:
        rois, labels, bbox_targets = rois.detach(), labels.detach(
        ), bbox_targets.detach()
        return rois, labels, bbox_targets
        # return F.zero_grad(rois), F.zero_grad(labels), F.zero_grad(bbox_targets)
    else:
        return_rois = F.concat(return_rois, axis=0)
        return_labels = F.concat(return_labels, axis=0)
        return_bbox_targets = F.concat(return_bbox_targets, axis=0)

        return_rois = return_rois.detach()
        return_labels = return_labels.detach()
        return_bbox_targets = return_bbox_targets.detach()
        return return_rois, return_labels, return_bbox_targets
Exemple #5
0
def fpn_roi_target(rpn_rois,
                   rpn_rois_inds,
                   im_info,
                   gt_boxes,
                   top_k_default=1):
    return_rois = []
    return_labels = []
    return_bbox_targets = []
    # get per image proposals and gt_boxes
    for bid in range(config.train_batch_per_gpu):
        gt_boxes_perimg = gt_boxes[bid, :int(im_info[bid, 5]), :]  #1,5
        top_k = top_k_default if int(im_info[bid, 5]) > 1 else 1
        batch_inds = torch.ones(
            (gt_boxes_perimg.shape[0], 1)).type_as(gt_boxes_perimg) * bid  #1,1
        #if config.proposal_append_gt:
        gt_rois = torch.cat([batch_inds, gt_boxes_perimg[:, :4]],
                            axis=1)  #1,1 cat 1,4 = 1,5

        if bid == 0:
            all_rois = torch.cat([rpn_rois[:rpn_rois_inds[0]], gt_rois],
                                 axis=0)
        else:
            all_rois = torch.cat(
                [rpn_rois[rpn_rois_inds[bid - 1]:rpn_rois_inds[bid]], gt_rois],
                axis=0)

        overlaps_normal, overlaps_ignore = box_overlap_ignore_opr(
            all_rois[:, 1:5], gt_boxes_perimg)
        overlaps_normal, overlaps_normal_indices = overlaps_normal.sort(
            descending=True, dim=1)  #2001,1 2039,39
        overlaps_ignore, overlaps_ignore_indices = overlaps_ignore.sort(
            descending=True, dim=1)
        # gt max and indices, ignore max and indices
        max_overlaps_normal = overlaps_normal[:, :top_k].flatten(
        )  #2001(???) 4078(2039*2)
        gt_assignment_normal = overlaps_normal_indices[:, :top_k].flatten()
        max_overlaps_ignore = overlaps_ignore[:, :top_k].flatten()
        gt_assignment_ignore = overlaps_ignore_indices[:, :top_k].flatten()

        # cons masks
        ignore_assign_mask = (max_overlaps_normal < config.fg_threshold) * (
            max_overlaps_ignore > max_overlaps_normal)
        max_overlaps = max_overlaps_normal * ~ignore_assign_mask + \
                max_overlaps_ignore * ignore_assign_mask
        gt_assignment = gt_assignment_normal * ~ignore_assign_mask + \
                gt_assignment_ignore * ignore_assign_mask

        labels = gt_boxes_perimg[gt_assignment, 4]
        fg_mask = (max_overlaps >= config.fg_threshold) * (labels !=
                                                           config.ignore_label)
        bg_mask = (max_overlaps < config.bg_threshold_high) * (
            max_overlaps >= config.bg_threshold_low)

        labels[~fg_mask] = 0
        fg_mask = fg_mask.reshape(-1, top_k)
        bg_mask = bg_mask.reshape(-1, top_k)

        pos_max = config.num_rois * config.fg_ratio
        fg_inds_mask = _bernoulli_sample_masks(fg_mask[:, 0], pos_max, True)
        neg_max = config.num_rois - fg_inds_mask.sum()
        bg_inds_mask = _bernoulli_sample_masks(bg_mask[:, 0], neg_max, True)
        keep_mask = fg_inds_mask + bg_inds_mask
        # labels
        labels = labels.reshape(-1, top_k)[keep_mask]
        gt_assignment = gt_assignment.reshape(-1, top_k)[keep_mask].flatten()
        target_boxes = gt_boxes_perimg[gt_assignment, :4]
        rois = all_rois[keep_mask]
        target_rois = rois.repeat(1, top_k).reshape(-1, all_rois.shape[-1])
        bbox_targets = bbox_transform_opr(target_rois[:, 1:5], target_boxes)
        if config.rcnn_bbox_normalize_targets:
            std_opr = torch.tensor(
                config.bbox_normalize_stds[None, :]).type_as(bbox_targets)
            mean_opr = torch.tensor(
                config.bbox_normalize_means[None, :]).type_as(bbox_targets)
            minus_opr = mean_opr / std_opr
            bbox_targets = bbox_targets / std_opr - minus_opr
        bbox_targets = bbox_targets.reshape(-1, top_k * 4)
        return_rois.append(rois)
        return_labels.append(labels)
        return_bbox_targets.append(bbox_targets)
    if config.train_batch_per_gpu == 1:
        return rois, labels, bbox_targets
    else:
        return_rois = torch.cat(return_rois, axis=0)
        return_labels = torch.cat(return_labels, axis=0)
        return_bbox_targets = torch.cat(return_bbox_targets, axis=0)
        return return_rois, return_labels, return_bbox_targets
Exemple #6
0
def _anchor_double_target(gt_boxes, im_info, all_anchors):

    gt_boxes, im_info = gt_boxes.detach(), im_info.detach()
    all_anchors = all_anchors.detach()

    gt_boxes = gt_boxes[:im_info[5].astype(np.int32), :]
    dummy = -F.ones([1, gt_boxes.shape[1]]).to(gt_boxes.device)
    gt_boxes = F.concat([gt_boxes, dummy], axis=0)
    valid_mask = 1 - (gt_boxes[:, 4] < 0).astype(np.float32)

    anchor_centers = _compute_center(all_anchors)
    gtboxes_centers = _compute_center(gt_boxes)
    # gtboxes_centers = gtboxes_centers * valid_mask.unsqueeze(1)
    gtboxes_centers = gtboxes_centers * F.expand_dims(valid_mask, axis=1)

    N, K = all_anchors.shape[0], gt_boxes.shape[0]
    an_centers = F.expand_dims(anchor_centers, axis=1)
    gt_centers = F.expand_dims(gtboxes_centers, axis=0)
    # an_centers = anchor_centers.unsqueeze(1).repeat(1, K, 1)
    # gt_centers = gtboxes_centers.unsqueeze(0).repeat(N, 1, 1)

    distance = F.abs(an_centers - gt_centers)
    distance = F.sqrt(F.pow(distance, 2).sum(axis=2))

    start = 0
    end = 5
    overlaps = box_overlap_opr(all_anchors[:, :4], gt_boxes[:, :4])
    overlaps *= F.expand_dims(valid_mask, axis=0)
    default_num = 16

    ious_list = []

    for l in range(start, end):

        _, index = F.cond_take(all_anchors[:, 4] == l, all_anchors[:, 4])

        level_dist = distance[index, :].transpose(1, 0)
        ious = overlaps[index, :].transpose(1, 0)
        sorted_index = F.argsort(level_dist, descending=False)
        n = min(sorted_index.shape[1], default_num)
        ious = F.gather(ious, 1, sorted_index[:, :n]).transpose(1, 0)

        ious_list.append(ious)

    ious = F.concat(ious_list, axis=0)
    mean_var = F.mean(ious, axis=0)
    std_var = F.std(ious, 0)
    iou_thresh_per_gt = mean_var + std_var

    iou_thresh_per_gt = F.maximum(iou_thresh_per_gt, 0.2)

    # limits the anchor centers in the gtboxes
    N, K = all_anchors.shape[0], gt_boxes.shape[0]
    anchor_points = an_centers
    pos_area = _compute_pos_area(gt_boxes, 0.3)
    # pos_area = pos_area.unsqueeze(0).repeat(N, 1, 1)
    pos_area = F.broadcast_to(F.expand_dims(pos_area, axis=0),
                              (N, K, pos_area.shape[-1]))

    l = anchor_points[:, :, 0] - pos_area[:, :, 0]
    r = pos_area[:, :, 2] - anchor_points[:, :, 0]
    t = anchor_points[:, :, 1] - pos_area[:, :, 1]
    b = pos_area[:, :, 3] - anchor_points[:, :, 1]

    is_in_gt = F.stack([l, r, t, b], axis=2)
    is_in_gt = is_in_gt.min(axis=2) > 0.1
    valid_mask = (overlaps >= F.expand_dims(
        iou_thresh_per_gt, axis=0)) * is_in_gt.astype(np.float32)
    ious = overlaps * valid_mask

    sorted_index = F.argsort(ious, 1)
    sorted_overlaps = F.gather(ious, 1, sorted_index)
    max_overlaps = sorted_overlaps[:, :2].flatten()
    argmax_overlaps = sorted_index[:, :2].flatten()

    n, c = all_anchors.shape
    device = all_anchors.device
    labels = -F.ones(2 * n).to(device)
    positive_mask = (max_overlaps >= 0.2).to(device).astype(np.float32)
    negative_mask = (max_overlaps < 0.2).to(device).astype(np.float32)
    labels = positive_mask + labels * (1 - positive_mask) * (1 - negative_mask)

    bbox_targets = gt_boxes[argmax_overlaps, :4]
    all_anchors = F.broadcast_to(F.expand_dims(all_anchors, axis=1),
                                 (n, 2, c)).reshape(-1, c)

    bbox_targets = bbox_transform_opr(all_anchors[:, :4], bbox_targets)

    labels_cat = gt_boxes[argmax_overlaps, 4]
    labels_cat = labels_cat * (1 - F.equal(labels, -1).astype(
        np.float32)) - F.equal(labels, -1).astype(np.float32)

    return labels, bbox_targets, labels_cat
Exemple #7
0
def rpn_anchor_target_opr_impl(gt_boxes,
                               im_info,
                               anchors,
                               clobber_positives=True,
                               ignore_label=-1,
                               background_label=0):

    gt_boxes, im_info = gt_boxes.detach(), im_info.detach()
    anchors = anchors.detach()

    # NOTE: For multi-gpu version, this function should be re-written
    a_shp0 = anchors.shape[0]
    valid_gt_boxes = gt_boxes[:im_info[5], :]
    valid_mask = (gt_boxes[:im_info[5], 4] > 0).astype(np.float32)
    overlaps = box_overlap_opr(anchors[:, :4], valid_gt_boxes[:, :4])
    overlaps = overlaps * valid_mask.unsqueeze(0)

    argmax_overlaps = torch.argmax(overlaps, axis=1)
    max_overlaps = torch.gather(overlaps, 1, argmax_overlaps.unsqueeze(1))
    gt_argmax_overlaps = torch.argmax(overlaps, axis=0)
    gt_argmax_overlaps = torch.gather(overlaps, 1,
                                      gt_argmax_overlaps.unsqueeze(0))

    cond_max_overlaps = overlaps.eq(gt_argmax_overlaps).astype(np.float32)
    cmo_shape1 = cond_max_overlaps.shape[1]

    gt_argmax_overlaps = torch.nonzero(cond_max_overlaps.flatten(),
                                       as_tuple=False)
    gt_argmax_overlaps = gt_argmax_overlaps // cmo_shape1

    labels = ignore_label * F.ones(a_shp0)
    fg_mask = (max_overlaps >= config.rpn_positive_overlap).astype(np.float32)
    fg_mask[gt_argmax_overlaps] = 1
    index = torch.nonzero(fg_mask, as_tuple=False).reshape(-1).long()
    labels[index] = 1

    bbox_targets = bbox_transform_opr(anchors, valid_gt_boxes[index, :4])

    # fg_mask[gt_argmax_overlaps]

    # --- megbrain fashion code ---
    # argmax_overlaps = O.Argmax(overlaps, axis=1)
    # max_overlaps = O.IndexingOneHot(overlaps, 1, argmax_overlaps)
    # gt_argmax_overlaps = O.Argmax(overlaps, axis=0)
    # gt_max_overlaps = O.IndexingOneHot(overlaps, 0, gt_argmax_overlaps)

    # cond_max_overlaps = overlaps.eq(gt_max_overlaps.add_axis(0))
    # cmo_shape1 = cond_max_overlaps.shape[1]

    # gt_argmax_overlaps = \
    #     O.CondTake(cond_max_overlaps.flatten(), cond_max_overlaps.flatten(),
    #                'EQ',1).outputs[1]
    # # why should be divided by the cmo_shape1
    # gt_argmax_overlaps = gt_argmax_overlaps // cmo_shape1

    # labels = O.ones(a_shp0) * ignore_label
    # const_one = O.ConstProvider(1.0)
    # if not clobber_positives:
    #     labels = labels * (max_overlaps >= config.rpn_negative_overlap)

    # fg_mask = (max_overlaps >= config.rpn_positive_overlap)
    # fg_mask = fg_mask.set_ai[gt_argmax_overlaps](
    #     const_one.broadcast(gt_argmax_overlaps.shape))

    # fg_mask_ind = O.CondTake(fg_mask, fg_mask, 'EQ', 1).outputs[1]
    # labels = labels.set_ai[fg_mask_ind](const_one.broadcast(fg_mask_ind.shape))

    # if clobber_positives:
    #     labels = labels * (max_overlaps >= config.rpn_negative_overlap)

    # Here, we compute the targets for each anchors
    # bbox_targets = bbox_transform_opr(
    #     anchors, valid_gt_boxes.ai[argmax_overlaps, :4])

    return labels, bbox_targets
Exemple #8
0
def _anchor_target(gt_boxes, im_info, all_anchors):

    gt_boxes, im_info = gt_boxes.detach(), im_info.detach()
    all_anchors = all_anchors.detach()

    gt_boxes = gt_boxes[:im_info[5], :]
    valid_mask = 1 - (gt_boxes[:, 4] < 0).astype(np.float32)

    anchor_centers = _compute_center(all_anchors)
    gtboxes_centers = _compute_center(gt_boxes) * F.expand_dims(valid_mask,
                                                                axis=0)

    N, K = all_anchors.shape[0], gt_boxes.shape[0]
    # an_centers = anchor_centers.unsqueeze(1).repeat(1, K, 1)
    an_centers = F.expand_dims(anchor_centers, axis=1)
    gt_centers = F.expand_dims(gtboxes_centers, axis=0)
    # gt_centers = gtboxes_centers.unsqueeze(0).repeat(N, 1, 1)

    distance = F.abs(an_centers - gt_centers)
    distance = F.sqrt(F.pow(distance, 2).sum(axis=2))

    start = 0
    end = 5
    overlaps = box_overlap_opr(all_anchors[:, :4], gt_boxes[:, :4])
    overlaps = overlaps * valid_mask.unsqueeze(0)
    default_num = 9

    ious_list = []
    for l in range(start, end):

        index = torch.nonzero(all_anchors[:, 4].eq(l), as_tuple=False)[:, 0]
        level_dist = level_dist[index, :].transpose(1, 0)
        ious = distance[index, :].transpose(1, 0)
        sorted_index = torch.argsort(ious, 1, descending=False)
        n = min(default_num, sorted_index.shape[1])
        ious = torch.gather(ious, 1, sorted_index[:, :n]).transpose(1, 0)
        ious_list.append(ious)

    ious = F.concat(ious_list, axis=0)
    mean_var = ious.mean(0)
    std_var = ious.std(0)
    iou_thresh_per_gt = mean_var + std_var

    iou_thresh_per_gt = torch.clamp(iou_thresh_per_gt, 0.35)
    n = iou_thresh_per_gt.shape[0]

    # limits the anchor centers in the gtboxes
    N, K = all_anchors.shape[0], gt_boxes.shape[0]
    anchor_points = an_centers
    proxies = gt_boxes.unsqueeze(0).repeat(N, 1, 1)
    l = anchor_points[:, :, 0] - proxies[:, :, 0]
    r = proxies[:, :, 2] - anchor_points[:, :, 0]
    t = anchor_points[:, :, 1] - proxies[:, :, 1]
    b = proxies[:, :, 3] - anchor_points[:, :, 1]

    is_in_gt = F.stack([l, r, t, b], axis=2)
    is_in_gt = is_in_gt.min(axis=2) > 0.1
    valid_mask = (overlaps >= iou_thresh_per_gt.unsqueeze(0)) * is_in_gt
    ious = overlaps * valid_mask

    argmax_overlaps = torch.argmax(ious, axis=1)
    max_overlaps = torch.gather(ious, 1, argmax_overlaps.unsqueeze(1))

    n = all_anchors.shape[0]
    labels = -F.ones(n)
    positive_mask = max_overlaps > 0
    negative_mask = max_overlaps < config.rpn_negative_overlap
    labels = positive_mask + labels * (1 - positive_mask) * (1 - negative_mask)

    bbox_targets = gt_boxes[argmax_overlaps, :4]
    bbox_targets = bbox_transform_opr(all_anchors[:, :4], bbox_targets)

    labels_cat = gt_boxes[argmax_overlaps, 4]
    labels_cat = labels_cat * (1 - labels.eq(0).astype(np.float32))
    labels_cat = labels_cat * (1 - labels.eq(-1).astype(
        np.float32)) - labels.eq(-1).astype(np.float32)

    return labels, bbox_targets, labels_cat