Exemplo n.º 1
0
def compute_track_rpn_targets(track_anchors,
                              det_boxes,
                              bbox_overlaps=None,
                              bound=None,
                              rpn_conv_size=10,
                              K=6):
    anchor_cls_targets = np.zeros((0, K * rpn_conv_size, rpn_conv_size),
                                  dtype=np.int32)
    anchor_bbox_targets = np.zeros((0, 4 * K, rpn_conv_size, rpn_conv_size),
                                   dtype=np.float32)
    bbox_weights = np.zeros((0, 4 * K, rpn_conv_size, rpn_conv_size),
                            dtype=np.float32)

    num_boxes = np.ones(len(track_anchors), dtype=np.int32).tolist()
    if bbox_overlaps is None:
        #        bbox_overlaps=bbox_overlaps_per_image(track_anchors, det_box.reshape(1,-1), branch='rpn').ravel()
        bbox_overlaps = bbox_overlaps_batch(track_anchors,
                                            det_boxes,
                                            num_boxes,
                                            branch='rpn')

    fg_anchor_inds = []
    bg_anchor_inds = []

    start = 0
    for i, box_size in enumerate(num_boxes):
        left = start
        right = start + box_size

        anchors = np.vstack(track_anchors[left:right])
        bbox_overlap = np.asarray(bbox_overlaps[i])
        max_overlap = np.max(bbox_overlap, axis=1)
        '''cls start'''
        anchor_cls_target = np.zeros(
            anchors.shape[0], dtype=np.int32) - 100  #box_size*A*K-->N*A*K
        fg_anchor_ind = np.where(
            max_overlap >= cfg[cfg.PHASE].TRACK_RPN_POSITIVE_THRESH)[0]
        bg_anchor_ind = np.where(
            max_overlap < cfg[cfg.PHASE].TRACK_RPN_NEGATIVE_THRESH)[0]

        fg_anchor_inds.append(fg_anchor_ind)
        bg_anchor_inds.append(bg_anchor_ind)

        anchor_cls_target[fg_anchor_ind] = 1
        anchor_cls_target[bg_anchor_ind] = 0

        anchor_cls_target=anchor_cls_target.reshape(box_size, rpn_conv_size, rpn_conv_size, K).\
            transpose(0,3,2,1).reshape(box_size, K*rpn_conv_size, rpn_conv_size)
        anchor_cls_targets = np.append(anchor_cls_targets, anchor_cls_target,
                                       0)
        '''cls end'''
        '''bbox start'''
        bbox_loss_inds = fg_anchor_ind
        mask_inds = np.zeros((anchors.shape[0], 4), dtype=np.float32)
        mask_inds[bbox_loss_inds, :] = 1
        gt_boxes_sample = det_boxes[left:right]

        gt_rois = gt_boxes_sample[np.argmax(bbox_overlap, axis=1)]

        bbox_deltas = bbox_transform(anchors, gt_rois)
        if cfg.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
            bbox_deltas = bbox_deltas / cfg.RPN_BBOX_STD_DEV

        bbox_deltas = bbox_deltas.reshape(box_size, rpn_conv_size,
                                          rpn_conv_size, 4 * K).transpose(
                                              (0, 3, 1, 2))
        anchor_bbox_targets = np.append(anchor_bbox_targets, bbox_deltas, 0)
        '''bbox end'''
        '''bbox weights'''
        mask_inds = mask_inds.reshape(box_size, rpn_conv_size, rpn_conv_size,
                                      4 * K).transpose(0, 3, 1, 2)
        bbox_weights = np.append(bbox_weights, mask_inds, 0)
        start += box_size

    return anchor_cls_targets, anchor_bbox_targets, bbox_weights, fg_anchor_inds
Exemplo n.º 2
0
def compute_rpn_targets(anchors,
                        gt_boxes,
                        out_size,
                        K=9,
                        bbox_overlaps=None,
                        batch_size=1):
    anchor_cls_targets = np.zeros((0, K * out_size[1], out_size[0]),
                                  dtype=np.int32)
    anchor_bbox_targets = np.zeros((0, 4 * K, out_size[1], out_size[0]),
                                   dtype=np.float32)
    fg_anchor_inds = []
    bg_anchor_inds = []

    if bbox_overlaps is None:
        bbox_overlaps = bbox_overlaps_batch(anchors,
                                            gt_boxes,
                                            batch_size,
                                            branch='frcnn')

    bbox_weights = np.zeros((0, 4 * K, out_size[1], out_size[0]),
                            dtype=np.float32)

    anchors_per_image = np.split(anchors, batch_size, axis=0)
    for i in range(batch_size):
        anchors_this_image = anchors_per_image[
            i]  #all anchors per ref box!!!!!
        bbox_overlap = np.asarray(bbox_overlaps[i])
        max_overlap = np.max(bbox_overlap, axis=1)
        '''cls start'''
        anchor_cls_target = np.zeros(
            anchors_this_image.shape[0],
            dtype=np.int32) - 100  #box_size*A*K-->N*A*K
        #        print(max_overlap)
        fg_anchor_ind = np.where(
            max_overlap >= cfg[cfg.PHASE].RPN_POSITIVE_THRESH)[0]
        bg_anchor_ind = np.where(
            max_overlap < cfg[cfg.PHASE].RPN_NEGATIVE_THRESH)[0]

        fg_anchor_inds.append(fg_anchor_ind)
        bg_anchor_inds.append(bg_anchor_ind)

        anchor_cls_target[fg_anchor_ind] = 1
        anchor_cls_target[bg_anchor_ind] = 0

        anchor_cls_target=anchor_cls_target.reshape(1, out_size[1], out_size[0], K).\
            transpose(0,3,2,1).reshape(1, K*out_size[1], out_size[0])
        anchor_cls_targets = np.append(anchor_cls_targets, anchor_cls_target,
                                       0)
        '''cls end'''
        '''bbox start'''
        #        bbox_loss_inds=np.where(max_overlap>=cfg[cfg.PHASE].RPN_BBOX_THRESH)[0]
        bbox_loss_inds = fg_anchor_ind
        mask_inds = np.zeros((anchors_this_image.shape[0], 4),
                             dtype=np.float32)
        mask_inds[bbox_loss_inds, :] = 1
        gt_boxes_sample = gt_boxes[i]

        gt_rois = gt_boxes_sample[np.argmax(bbox_overlap, axis=1)]
        bbox_deltas = bbox_transform(anchors_this_image, gt_rois)
        if cfg.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
            bbox_deltas = bbox_deltas / cfg.RPN_BBOX_STD_DEV

#        bbox_deltas*=mask_inds
        bbox_deltas = bbox_deltas.reshape(1, out_size[1], out_size[0],
                                          4 * K).transpose((0, 3, 1, 2))
        anchor_bbox_targets = np.append(anchor_bbox_targets, bbox_deltas, 0)
        '''bbox end'''
        '''bbox weights'''
        mask_inds = mask_inds.reshape(1, out_size[1], out_size[0],
                                      4 * K).transpose(0, 3, 1, 2)
        bbox_weights = np.append(bbox_weights, mask_inds, 0)
    return anchor_cls_targets, fg_anchor_inds, bg_anchor_inds, anchor_bbox_targets, bbox_weights
Exemplo n.º 3
0
def get_proposal_target(proposal_batch, gt_boxes, gt_classes, batch_size):
    rois_per_image = cfg[cfg.PHASE].BATCH_SIZE
    fg_rois_per_image = int(rois_per_image * cfg[cfg.PHASE].FG_FRACTION)

    num_fg_rois = 0
    num_bg_rois = 0

    proposal_cls_targets = np.zeros(0, dtype=np.int32)
    proposal_bbox_targets = np.zeros((0, 4 * cfg.NUM_CLASSES),
                                     dtype=np.float32)
    all_bbox_weights = np.zeros((0, 4 * cfg.NUM_CLASSES), dtype=np.float32)
    all_proposals = np.zeros((0, 5), dtype=np.float32)

    #    labels=np.zeros(0, dtype=np.int32)
    labels = []

    for i in range(batch_size):
        proposals_this_image = proposal_batch[i]
        #        proposals_this_image=np.vstack(proposal_batch[i])
        gt_boxes_this_image = gt_boxes[i]
        gt_classes_this_image = gt_classes[i]

        bbox_overlaps = bbox_overlaps_per_image(proposals_this_image,
                                                gt_boxes_this_image)
        argmax_overlaps = np.argmax(bbox_overlaps, axis=1)

        max_overlaps = np.max(bbox_overlaps, axis=1)

        fg_inds = np.where(max_overlaps >= cfg[cfg.PHASE].FG_THRESH)[0]
        if cfg.CHOOSE_PROPOSAL:
            fg_rois_this_image = min(fg_rois_per_image, fg_inds.size)
        else:
            fg_rois_this_image = fg_inds.size

        num_fg_rois += fg_rois_this_image

        if cfg.CHOOSE_PROPOSAL and fg_inds.size > 0:
            fg_inds = np.random.choice(fg_inds,
                                       size=fg_rois_this_image,
                                       replace=False)

        bg_inds = np.where(
            np.bitwise_and(bbox_overlaps < cfg[cfg.PHASE].BG_THRESH_HI,
                           bbox_overlaps >= cfg[cfg.PHASE].BG_THRESH_LO) ==
            1)[0]
        if cfg.CHOOSE_PROPOSAL:
            bg_rois_per_image = rois_per_image - fg_rois_this_image
            bg_rois_this_image = min(bg_rois_per_image, bg_inds.size)
        else:
            bg_rois_this_image = bg_inds.size

        num_bg_rois += bg_rois_this_image

        if cfg.CHOOSE_PROPOSAL and bg_inds.size > 0:
            bg_inds = np.random.choice(bg_inds,
                                       size=bg_rois_this_image,
                                       replace=False)
        '''discard rois with too small overlaps (less than BG_THRESH_LO)'''
        keep_inds = np.append(fg_inds, bg_inds)

        assert fg_inds.size == fg_rois_this_image
        assert bg_inds.size == bg_rois_this_image

        proposals_keep = proposals_this_image[keep_inds]
        all_proposals = np.append(all_proposals, proposals_keep, 0)

        max_overlap_boxes = gt_boxes_this_image[argmax_overlaps[keep_inds]]
        gt_cls_inds = gt_classes_this_image[argmax_overlaps[keep_inds]]
        #        print(gt_inds)
        targets = bbox_transform(proposals_keep, max_overlap_boxes)
        if cfg.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
            # Optionally normalize targets by a precomputed mean and stdev
            targets = targets / cfg.BBOX_STD_DEV

        proposal_cls_target = np.zeros(len(proposals_keep), dtype=np.int32)
        bbox_deltas_classes = np.zeros(
            (len(proposals_keep), 4 * cfg.NUM_CLASSES), dtype=np.float32)

        #        print(gt_cls_inds)
        for j in range(len(keep_inds)):
            proposal_cls_target[j] = gt_cls_inds[j]
            bbox_deltas_classes[j, gt_cls_inds[j] * 4:gt_cls_inds[j] * 4 +
                                4] = targets[j][...]

        bbox_weights = np.ones((len(proposals_keep), 4 * cfg.NUM_CLASSES),
                               dtype=np.float32)
        proposal_cls_target[fg_rois_this_image:] = 0
        bbox_deltas_classes[fg_rois_this_image:, :] = 0.0
        bbox_weights[fg_rois_this_image:, :] = 0.0

        labels_this_image = np.zeros(len(keep_inds), dtype=np.int32)
        labels_this_image[:
                          fg_rois_this_image] = gt_cls_inds[:
                                                            fg_rois_this_image]
        labels_this_image[fg_rois_this_image:] = 0
        labels.append(labels_this_image)

        proposal_cls_targets = np.append(proposal_cls_targets,
                                         proposal_cls_target, 0)
        proposal_bbox_targets = np.append(proposal_bbox_targets,
                                          bbox_deltas_classes, 0)
        all_bbox_weights = np.append(all_bbox_weights, bbox_weights, 0)

#    print(labels)
    return all_proposals, proposal_cls_targets, proposal_bbox_targets, all_bbox_weights, labels