def get_anchor_labels(self, anchors, gt_boxes, labels):
        # This function will modify labels and return the filtered inds

        NA, NB = len(anchors), len(gt_boxes)
        assert NB > 0  # empty images should have been filtered already
        # ##########

        anchor_state = np.zeros((NA, ), dtype='int32') - 1

        anchor_labels = np.zeros((NA, ), dtype='int32')
        anchor_boxes = np.zeros((NA, 4), dtype='float32')

        box_ious = np_iou(anchors, gt_boxes)  # NA x NB

        # for each anchor box choose the groundtruth box with largest iou, set iou<0.4 as backgroud, ignore 0.4-0.5
        max_iou = box_ious.max(axis=1)  # NA

        positive_anchor_indices = np.where(
            max_iou > cfg.ANCHOR.POSITIVE_ANCHOR_THRESH)[0]
        negative_anchor_indices = np.where(
            max_iou < cfg.ANCHOR.NEGATIVE_ANCHOR_THRESH)[0]

        positive_iou = box_ious[positive_anchor_indices]
        matched_gt_box_indices = positive_iou.argmax(axis=1)

        anchor_labels[positive_anchor_indices] = labels[matched_gt_box_indices]
        anchor_state[positive_anchor_indices] = 1
        anchor_boxes[positive_anchor_indices] = gt_boxes[
            matched_gt_box_indices]

        anchor_state[negative_anchor_indices] = 0

        fg_boxes = anchor_boxes[anchor_state == 1]

        matched_anchors = anchors[anchor_state == 1]

        ##select and normlised the box coordinate
        fg_boxes[:, 0::2] = fg_boxes[:, 0::2] / self.max_size[1]
        fg_boxes[:, 1::2] = fg_boxes[:, 1::2] / self.max_size[0]

        matched_anchors[:, 0::2] = matched_anchors[:, 0::2] / self.max_size[1]
        matched_anchors[:, 1::2] = matched_anchors[:, 1::2] / self.max_size[0]

        encode_fg_boxes = encode(fg_boxes, matched_anchors)
        anchor_boxes[anchor_state == 1] = encode_fg_boxes

        anchor_labels = np.stack([anchor_labels, anchor_state])

        #
        return anchor_labels, anchor_boxes
Пример #2
0
    def get_anchor_labels(self, anchors, gt_boxes, labels):
        # This function will modify labels and return the filtered inds

        NA, NB = len(anchors), len(gt_boxes)
        assert NB > 0  # empty images should have been filtered already
        # ##########
        anchor_matched_already = np.zeros((NA, ), dtype='int32')
        gt_boxes_mathed_already = np.zeros((NB, ), dtype='int32')
        anchor_labels = np.zeros((NA, ), dtype='int32')
        anchor_boxes = np.zeros((NA, 4), dtype='float32')

        box_ious = np_iou(anchors, gt_boxes)  # NA x NB

        # for each anchor box choose the groundtruth box with largest iou
        max_iou = box_ious.max(axis=1)  # NA
        positive_anchor_indices = np.where(
            max_iou > cfg.ANCHOR.POSITIVE_ANCHOR_THRESH)[0]
        # negative_anchor_indices = np.where(max_iou < cfg.ANCHOR.NEGATIVE_ANCHOR_THRESH)[0]

        positive_iou = box_ious[positive_anchor_indices]
        matched_gt_box_indices = positive_iou.argmax(axis=1)

        anchor_labels[positive_anchor_indices] = labels[matched_gt_box_indices]
        anchor_boxes[positive_anchor_indices] = gt_boxes[
            matched_gt_box_indices]
        anchor_matched_already[
            positive_anchor_indices] = 1  #### marked as matched
        gt_boxes_mathed_already[
            matched_gt_box_indices] = 1  #### marked as matched

        if np.sum(anchor_matched_already) > 0:
            n = np.sum(anchor_matched_already) / np.sum(
                gt_boxes_mathed_already)
        else:
            n = cfg.ANCHOR.AVG_MATCHES
        n = n if n > cfg.ANCHOR.AVG_MATCHES else cfg.ANCHOR.AVG_MATCHES
        if not cfg.ANCHOR.super_match:
            n = cfg.ANCHOR.AVG_MATCHES
        # some gt_boxes may not matched, find them and match them with n anchors for each gt box
        box_ious[box_ious < cfg.ANCHOR.NEGATIVE_ANCHOR_THRESH] = 0
        sorted_ious = np.argsort(-box_ious, axis=0)

        sorted_ious = sorted_ious[np.logical_not(anchor_matched_already)]

        for i in range(0, len(gt_boxes_mathed_already)):
            matched_count = np.sum(
                matched_gt_box_indices == gt_boxes_mathed_already[i])

            if matched_count >= n:
                continue
            else:
                for j in range(0, int(n - matched_count)):
                    if box_ious[sorted_ious[j]
                                [i]][i] > cfg.ANCHOR.NEGATIVE_ANCHOR_THRESH:
                        anchor_labels[sorted_ious[j][i]] = labels[i]
                        anchor_boxes[sorted_ious[j][i]] = gt_boxes[i]

                        anchor_matched_already[sorted_ious[j][i]] = 1

                        gt_boxes_mathed_already[i] = 1

        fg_boxes = anchor_boxes[anchor_matched_already.astype(np.bool)]

        matched_anchors = anchors[anchor_matched_already.astype(np.bool)]

        ##select and normlised the box coordinate
        fg_boxes[:, 0::2] = fg_boxes[:, 0::2] / self.max_size[0]
        fg_boxes[:, 1::2] = fg_boxes[:, 1::2] / self.max_size[1]

        matched_anchors[:, 0::2] = matched_anchors[:, 0::2] / self.max_size[0]
        matched_anchors[:, 1::2] = matched_anchors[:, 1::2] / self.max_size[1]

        encode_fg_boxes = encode(fg_boxes, matched_anchors)
        anchor_boxes[anchor_matched_already.astype(np.bool)] = encode_fg_boxes
        # assert len(fg_inds) + np.sum(anchor_labels == 0) == cfg.ANCHOR.BATCH_PER_IM
        return anchor_labels, anchor_boxes