def filter_detections(boxes, scores, is_training):
    """
    :param boxes: [-1, 4]
    :param scores: [-1, ]
    :param labels: [-1, ]
    :return:
    """
    if is_training:
        indices = tf.reshape(tf.where(tf.greater(scores, cfgs.VIS_SCORE)), [-1, ])
    else:
        indices = tf.reshape(tf.where(tf.greater(scores, cfgs.FILTERED_SCORE)), [-1, ])

    if cfgs.NMS:
        filtered_boxes = tf.gather(boxes, indices)
        filtered_scores = tf.gather(scores, indices)

        # perform NMS

        nms_indices = nms_rotate.nms_rotate(decode_boxes=filtered_boxes,
                                            scores=filtered_scores,
                                            iou_threshold=cfgs.NMS_IOU_THRESHOLD,
                                            max_output_size=100,
                                            use_angle_condition=False,
                                            angle_threshold=15,
                                            use_gpu=False)

        # filter indices based on NMS
        indices = tf.gather(indices, nms_indices)

    # add indices to list of all indices
    return indices
    def fast_rcnn_proposals_rotate(self, decode_boxes, scores):
        '''
        mutilclass NMS
        :param decode_boxes: [N, num_classes*5]
        :param scores: [N, num_classes+1]
        :return:
        detection_boxes : [-1, 5]
        scores : [-1, ]
        '''

        with tf.variable_scope('fast_rcnn_proposals'):
            category = tf.argmax(scores, axis=1)

            object_mask = tf.cast(tf.not_equal(category, 0), tf.float32)

            decode_boxes = decode_boxes * tf.expand_dims(object_mask, axis=1)  # make background box is [0 0 0 0, 0]
            scores = scores * tf.expand_dims(object_mask, axis=1)

            decode_boxes = tf.reshape(decode_boxes, [-1, self.num_classes, 5])  # [N, num_classes, 5]

            decode_boxes_list = tf.unstack(decode_boxes, axis=1)
            score_list = tf.unstack(scores[:, 1:], axis=1)
            after_nms_boxes = []
            after_nms_scores = []
            category_list = []
            for per_class_decode_boxes, per_class_scores in zip(decode_boxes_list, score_list):

                valid_indices = nms_rotate.nms_rotate(decode_boxes=per_class_decode_boxes,
                                                      scores=per_class_scores,
                                                      iou_threshold=self.fast_rcnn_nms_iou_threshold,
                                                      max_output_size=self.fast_rcnn_nms_max_boxes_per_class,
                                                      use_angle_condition=False,
                                                      angle_threshold=15,
                                                      use_gpu=cfgs.ROTATE_NMS_USE_GPU)

                after_nms_boxes.append(tf.gather(per_class_decode_boxes, valid_indices))
                after_nms_scores.append(tf.gather(per_class_scores, valid_indices))
                tmp_category = tf.gather(category, valid_indices)

                category_list.append(tmp_category)

            all_nms_boxes = tf.concat(after_nms_boxes, axis=0)
            all_nms_scores = tf.concat(after_nms_scores, axis=0)
            all_category = tf.concat(category_list, axis=0)

            # all_nms_boxes = boxes_utils.clip_boxes_to_img_boundaries(all_nms_boxes,
            #                                                          img_shape=self.img_shape)

            scores_large_than_threshold_indices = \
                tf.reshape(tf.where(tf.greater(all_nms_scores, self.show_detections_score_threshold)), [-1])

            all_nms_boxes = tf.gather(all_nms_boxes, scores_large_than_threshold_indices)
            all_nms_scores = tf.gather(all_nms_scores, scores_large_than_threshold_indices)
            all_category = tf.gather(all_category, scores_large_than_threshold_indices)

            return all_nms_boxes, all_nms_scores, tf.shape(all_nms_boxes)[0], all_category
Exemplo n.º 3
0
def filter_detections(boxes, scores, is_training, gpu_id):
    """
    :param boxes: [-1, 4]
    :param scores: [-1, ]
    :param labels: [-1, ]
    :return:
    """
    if is_training:
        indices = tf.reshape(tf.where(tf.greater(scores, cfgs.VIS_SCORE)), [
            -1,
        ])
    else:
        indices = tf.reshape(tf.where(tf.greater(scores, cfgs.FILTERED_SCORE)),
                             [
                                 -1,
                             ])

    if cfgs.NMS:
        filtered_boxes = tf.gather(boxes, indices)
        filtered_scores = tf.gather(scores, indices)

        if cfgs.ANGLE_RANGE == 180:
            # _, _, _, _, theta = tf.unstack(boxes_pred, axis=1)
            # indx = tf.reshape(tf.where(tf.logical_and(tf.less(theta, 0), tf.greater_equal(theta, -180))), [-1, ])
            # boxes_pred = tf.gather(boxes_pred, indx)
            # scores = tf.gather(scores, indx)

            filtered_boxes = tf.py_func(coordinate_present_convert,
                                        inp=[filtered_boxes, 1],
                                        Tout=[tf.float32])
            filtered_boxes = tf.reshape(filtered_boxes, [-1, 5])

        # perform NMS
        max_output_size = 4000 if 'DOTA' in cfgs.NET_NAME else 200
        nms_indices = nms_rotate.nms_rotate(
            decode_boxes=filtered_boxes,
            scores=filtered_scores,
            iou_threshold=cfgs.NMS_IOU_THRESHOLD,
            max_output_size=100 if is_training else max_output_size,
            use_angle_condition=False,
            angle_threshold=15,
            use_gpu=True,
            gpu_id=gpu_id)

        # filter indices based on NMS
        indices = tf.gather(indices, nms_indices)

    # add indices to list of all indices
    return indices
def postprocess_detctions(rpn_bbox_pred, rpn_cls_prob, rpn_angle_prob, anchors,
                          is_training):

    # return_boxes_pred = []
    return_boxes_pred_angle = []
    return_scores = []
    return_labels = []
    for j in range(0, cfgs.CLASS_NUM):
        scores = rpn_cls_prob[:, j]
        if is_training:
            indices = tf.reshape(tf.where(tf.greater(scores, cfgs.VIS_SCORE)),
                                 [
                                     -1,
                                 ])
        else:
            indices = tf.reshape(
                tf.where(tf.greater(scores, cfgs.FILTERED_SCORE)), [
                    -1,
                ])

        anchors_ = tf.gather(anchors, indices)
        rpn_bbox_pred_ = tf.gather(rpn_bbox_pred, indices)
        scores = tf.gather(scores, indices)
        rpn_angle_prob_ = tf.gather(rpn_angle_prob, indices)

        angle_cls = tf.py_func(angle_label_decode,
                               inp=[
                                   rpn_angle_prob_, cfgs.ANGLE_RANGE,
                                   cfgs.OMEGA, cfgs.ANGLE_MODE
                               ],
                               Tout=[tf.float32])
        angle_cls = tf.reshape(angle_cls, [
            -1,
        ]) * -1

        if cfgs.METHOD == 'H':
            x_c = (anchors_[:, 2] + anchors_[:, 0]) / 2
            y_c = (anchors_[:, 3] + anchors_[:, 1]) / 2
            h = anchors_[:, 2] - anchors_[:, 0] + 1
            w = anchors_[:, 3] - anchors_[:, 1] + 1
            theta = -90 * tf.ones_like(x_c)
            anchors_ = tf.transpose(tf.stack([x_c, y_c, w, h, theta]))

        if cfgs.ANGLE_RANGE == 180:
            anchors_ = tf.py_func(coordinate_present_convert,
                                  inp=[anchors_, -1],
                                  Tout=[tf.float32])
            anchors_ = tf.reshape(anchors_, [-1, 5])

        boxes_pred = bbox_transform.rbbox_transform_inv_dcl(
            boxes=anchors_, deltas=rpn_bbox_pred_)
        boxes_pred = tf.reshape(boxes_pred, [-1, 4])

        x, y, w, h = tf.unstack(boxes_pred, axis=1)
        boxes_pred_angle = tf.transpose(tf.stack([x, y, w, h, angle_cls]))

        if cfgs.ANGLE_RANGE == 180:

            # _, _, _, _, theta = tf.unstack(boxes_pred, axis=1)
            # indx = tf.reshape(tf.where(tf.logical_and(tf.less(theta, 0), tf.greater_equal(theta, -180))), [-1, ])
            # boxes_pred = tf.gather(boxes_pred, indx)
            # scores = tf.gather(scores, indx)

            # boxes_pred = tf.py_func(coordinate_present_convert,
            #                         inp=[boxes_pred, 1],
            #                         Tout=[tf.float32])
            # boxes_pred = tf.reshape(boxes_pred, [-1, 5])

            boxes_pred_angle = tf.py_func(coordinate_present_convert,
                                          inp=[boxes_pred_angle, 1],
                                          Tout=[tf.float32])
            boxes_pred_angle = tf.reshape(boxes_pred_angle, [-1, 5])

        nms_indices = nms_rotate.nms_rotate(
            decode_boxes=boxes_pred_angle,
            scores=scores,
            iou_threshold=cfgs.NMS_IOU_THRESHOLD,
            max_output_size=100 if is_training else 1000,
            use_angle_condition=False,
            angle_threshold=15,
            use_gpu=False)

        # tmp_boxes_pred = tf.reshape(tf.gather(boxes_pred, nms_indices), [-1, 5])
        tmp_boxes_pred_angle = tf.reshape(
            tf.gather(boxes_pred_angle, nms_indices), [-1, 5])
        tmp_scores = tf.reshape(tf.gather(scores, nms_indices), [
            -1,
        ])

        # return_boxes_pred.append(tmp_boxes_pred)
        return_boxes_pred_angle.append(tmp_boxes_pred_angle)
        return_scores.append(tmp_scores)
        return_labels.append(tf.ones_like(tmp_scores) * (j + 1))

    # return_boxes_pred = tf.concat(return_boxes_pred, axis=0)
    return_boxes_pred_angle = tf.concat(return_boxes_pred_angle, axis=0)
    return_scores = tf.concat(return_scores, axis=0)
    return_labels = tf.concat(return_labels, axis=0)

    return return_scores, return_labels, return_boxes_pred_angle
    def postprocess_fastrcnn(self, rois, bbox_ppred, scores, img_shape):
        '''

        :param rois:[-1, 4]
        :param bbox_ppred: [-1, (cfgs.Class_num+1) * 5]
        :param scores: [-1, cfgs.Class_num + 1]
        :return:
        '''

        with tf.name_scope('postprocess_fastrcnn'):
            rois = tf.stop_gradient(rois)
            scores = tf.stop_gradient(scores)
            bbox_ppred = tf.reshape(bbox_ppred, [-1, cfgs.CLASS_NUM + 1, 5])
            bbox_ppred = tf.stop_gradient(bbox_ppred)

            bbox_pred_list = tf.unstack(bbox_ppred, axis=1)
            score_list = tf.unstack(scores, axis=1)

            allclasses_boxes = []
            allclasses_scores = []
            categories = []
            for i in range(1, cfgs.CLASS_NUM+1):

                # 1. decode boxes in each class
                tmp_encoded_box = bbox_pred_list[i]
                tmp_score = score_list[i]
                tmp_decoded_boxes = encode_and_decode.decode_boxes_rotate(encode_boxes=tmp_encoded_box,
                                                                          reference_boxes=rois,
                                                                          scale_factors=cfgs.ROI_SCALE_FACTORS)
                # tmp_decoded_boxes = encode_and_decode.decode_boxes(boxes=rois,
                #                                                    deltas=tmp_encoded_box,
                #                                                    scale_factor=cfgs.ROI_SCALE_FACTORS)

                # 2. clip to img boundaries
                # tmp_decoded_boxes = boxes_utils.clip_boxes_to_img_boundaries(decode_boxes=tmp_decoded_boxes,
                #                                                              img_shape=img_shape)

                # 3. NMS
                keep = nms_rotate.nms_rotate(decode_boxes=tmp_decoded_boxes,
                                             scores=tmp_score,
                                             iou_threshold=cfgs.FAST_RCNN_NMS_IOU_THRESHOLD,
                                             max_output_size=cfgs.FAST_RCNN_NMS_MAX_BOXES_PER_CLASS,
                                             use_angle_condition=False,
                                             angle_threshold=15,
                                             use_gpu=True)

                perclass_boxes = tf.gather(tmp_decoded_boxes, keep)
                perclass_scores = tf.gather(tmp_score, keep)

                allclasses_boxes.append(perclass_boxes)
                allclasses_scores.append(perclass_scores)
                categories.append(tf.ones_like(perclass_scores) * i)

            final_boxes = tf.concat(allclasses_boxes, axis=0)
            final_scores = tf.concat(allclasses_scores, axis=0)
            final_category = tf.concat(categories, axis=0)

            # if self.is_training:
            '''
            in training. We should show the detecitons in the tensorboard. So we add this.
            '''
            kept_indices = tf.reshape(tf.where(tf.greater_equal(final_scores, cfgs.SHOW_SCORE_THRSHOLD)), [-1])
            final_boxes = tf.gather(final_boxes, kept_indices)
            final_scores = tf.gather(final_scores, kept_indices)
            final_category = tf.gather(final_category, kept_indices)

        return final_boxes, final_scores, final_category
Exemplo n.º 6
0
    def rpn_proposals(self):
        with tf.variable_scope('rpn_proposals'):
            rpn_decode_boxes = encode_and_decode.decode_boxes(
                encode_boxes=self.rpn_encode_boxes,
                reference_boxes=self.anchors,
                scale_factors=self.scale_factors)

            # if not self.is_training:  # when test, clip proposals to img boundaries
            #     img_shape = tf.shape(self.img_batch)
            #     rpn_decode_boxes = boxes_utils.clip_boxes_to_img_boundaries(rpn_decode_boxes, img_shape)

            rpn_softmax_scores = slim.softmax(self.rpn_scores)
            rpn_object_score = rpn_softmax_scores[:,
                                                  1]  # second column represent object

            if self.top_k_nms:
                rpn_object_score, top_k_indices = tf.nn.top_k(rpn_object_score,
                                                              k=self.top_k_nms)
                rpn_decode_boxes = tf.gather(rpn_decode_boxes, top_k_indices)

            if not cfgs.USE_HORIZONTAL_NMS:
                valid_indices = nms_rotate.nms_rotate(
                    decode_boxes=rpn_decode_boxes,
                    scores=rpn_object_score,
                    iou_threshold=self.rpn_nms_iou_threshold,
                    max_output_size=self.max_proposals_num,
                    use_angle_condition=self.use_angles_condition,
                    angle_threshold=self.anchor_angle_threshold,
                    use_gpu=cfgs.NMS_USE_GPU)

            ############################################################################################################
            else:
                rpn_decode_boxes_convert = tf.py_func(
                    coordinate_convert.forward_convert,
                    inp=[rpn_decode_boxes],
                    Tout=tf.float32)

                rpn_decode_boxes_convert = tf.reshape(
                    rpn_decode_boxes_convert,
                    [tf.shape(rpn_decode_boxes)[0], 8])
                x1, y1, x2, y2, x3, y3, x4, y4 = tf.unstack(
                    rpn_decode_boxes_convert, axis=1)
                x = tf.transpose(tf.stack([x1, x2, x3, x4]))
                y = tf.transpose(tf.stack([y1, y2, y3, y4]))
                min_x = tf.reduce_min(x, axis=1)
                max_x = tf.reduce_max(x, axis=1)
                min_y = tf.reduce_min(y, axis=1)
                max_y = tf.reduce_max(y, axis=1)
                rpn_decode_boxes_convert = tf.transpose(
                    tf.stack([min_x, min_y, max_x, max_y]))

                valid_indices = tf.image.non_max_suppression(
                    boxes=rpn_decode_boxes_convert,
                    scores=rpn_object_score,
                    max_output_size=self.max_proposals_num,
                    iou_threshold=self.rpn_nms_iou_threshold,
                    name='rpn_horizontal_nms')

            ############################################################################################################

            valid_boxes = tf.gather(rpn_decode_boxes, valid_indices)
            valid_scores = tf.gather(rpn_object_score, valid_indices)
            rpn_proposals_boxes, rpn_proposals_scores = tf.cond(
                tf.less(tf.shape(valid_boxes)[0], self.max_proposals_num),
                lambda: boxes_utils.padd_boxes_with_zeros(
                    valid_boxes, valid_scores, self.max_proposals_num), lambda:
                (valid_boxes, valid_scores))

            return rpn_proposals_boxes, rpn_proposals_scores
Exemplo n.º 7
0
    def postprocess_fastrcnn(self, rois, bbox_ppred, scores, gpu_id):
        '''
        :param rois:[-1, 4]
        :param bbox_ppred: [-1, (cfgs.Class_num+1) * 5]
        :param scores: [-1, cfgs.Class_num + 1]
        :return:
        '''

        with tf.name_scope('postprocess_fastrcnn'):
            rois = tf.stop_gradient(rois)
            scores = tf.stop_gradient(scores)
            bbox_ppred = tf.reshape(bbox_ppred, [-1, cfgs.CLASS_NUM + 1, 5])
            bbox_ppred = tf.stop_gradient(bbox_ppred)

            bbox_pred_list = tf.unstack(bbox_ppred, axis=1)
            score_list = tf.unstack(scores, axis=1)

            allclasses_boxes = []
            allclasses_scores = []
            categories = []
            for i in range(1, cfgs.CLASS_NUM + 1):

                # 1. decode boxes in each class
                tmp_encoded_box = bbox_pred_list[i]
                tmp_score = score_list[i]
                tmp_decoded_boxes = encode_and_decode.decode_boxes_rotate(
                    encode_boxes=tmp_encoded_box,
                    reference_boxes=rois,
                    scale_factors=cfgs.ROI_SCALE_FACTORS)
                # tmp_decoded_boxes = encode_and_decode.decode_boxes(boxes=rois,
                #                                                    deltas=tmp_encoded_box,
                #                                                    scale_factor=cfgs.ROI_SCALE_FACTORS)

                # 2. clip to img boundaries
                # tmp_decoded_boxes = boxes_utils.clip_boxes_to_img_boundaries(decode_boxes=tmp_decoded_boxes,
                #                                                              img_shape=img_shape)
                threshold = {
                    'roundabout': 0.3,
                    'tennis-court': 0.3,
                    'swimming-pool': 0.3,
                    'storage-tank': 0.2,
                    'soccer-ball-field': 0.3,
                    'small-vehicle': 0.3,
                    'ship': 0.1,
                    'plane': 0.3,
                    'large-vehicle': 0.15,
                    'helicopter': 0.3,
                    'harbor': 0.1,
                    'ground-track-field': 0.3,
                    'bridge': 0.1,
                    'basketball-court': 0.3,
                    'baseball-diamond': 0.3,
                    'container-crane': 0.3
                }

                # 3. NMS
                if cfgs.SOFT_NMS:
                    print("Using Soft NMS.......")
                    raise NotImplementedError(
                        "soft NMS for rotate has not implemented")

                else:
                    keep = nms_rotate.nms_rotate(
                        decode_boxes=tmp_decoded_boxes,
                        scores=tmp_score,
                        iou_threshold=threshold[LABEL_NAME_MAP[i]],
                        max_output_size=cfgs.FAST_RCNN_NMS_MAX_BOXES_PER_CLASS,
                        use_angle_condition=False,
                        angle_threshold=15,
                        use_gpu=cfgs.ROTATE_NMS_USE_GPU,
                        gpu_id=gpu_id)

                perclass_boxes = tf.gather(tmp_decoded_boxes, keep)
                perclass_scores = tf.gather(tmp_score, keep)

                allclasses_boxes.append(perclass_boxes)
                allclasses_scores.append(perclass_scores)
                categories.append(tf.ones_like(perclass_scores) * i)

            final_boxes = tf.concat(allclasses_boxes, axis=0)
            final_scores = tf.concat(allclasses_scores, axis=0)
            final_category = tf.concat(categories, axis=0)

            if self.is_training:
                '''
                in training. We should show the detecitons in the tensorboard. So we add this.
                '''
                kept_indices = tf.reshape(
                    tf.where(
                        tf.greater_equal(final_scores,
                                         cfgs.SHOW_SCORE_THRSHOLD)), [-1])
                final_boxes = tf.gather(final_boxes, kept_indices)
                final_scores = tf.gather(final_scores, kept_indices)
                final_category = tf.gather(final_category, kept_indices)

            return final_boxes, final_scores, final_category
def postprocess_rpn_proposals(rpn_bbox_pred, rpn_cls_prob, img_shape, anchors,
                              is_training):
    '''

    :param rpn_bbox_pred: [-1, 4]
    :param rpn_cls_prob: [-1, 2]
    :param img_shape:
    :param anchors:[-1, 4]
    :param is_training:
    :return:
    '''

    if is_training:
        pre_nms_topN = cfgs.RPN_TOP_K_NMS_TRAIN
        post_nms_topN = cfgs.RPN_MAXIMUM_PROPOSAL_TARIN
        nms_thresh = cfgs.RPN_NMS_IOU_THRESHOLD
    else:
        pre_nms_topN = cfgs.RPN_TOP_K_NMS_TEST
        post_nms_topN = cfgs.RPN_MAXIMUM_PROPOSAL_TEST
        nms_thresh = cfgs.RPN_NMS_IOU_THRESHOLD

    cls_prob = rpn_cls_prob[:, 1]

    # 1. decode boxes
    decode_boxes = encode_and_decode.decode_boxes_rotate(
        encode_boxes=rpn_bbox_pred,
        reference_boxes=anchors,
        scale_factors=cfgs.ANCHOR_SCALE_FACTORS)

    # decode_boxes = encode_and_decode.decode_boxes(boxes=anchors,
    #                                               deltas=rpn_bbox_pred,
    #                                               scale_factor=None)

    # 2. clip to img boundaries
    # decode_boxes = boxes_utils.clip_boxes_to_img_boundaries(decode_boxes=decode_boxes,
    #                                                         img_shape=img_shape)

    # 3. get top N to NMS
    if pre_nms_topN > 0:
        pre_nms_topN = tf.minimum(pre_nms_topN,
                                  tf.shape(decode_boxes)[0],
                                  name='avoid_unenough_boxes')
        cls_prob, top_k_indices = tf.nn.top_k(cls_prob, k=pre_nms_topN)
        decode_boxes = tf.gather(decode_boxes, top_k_indices)

    # 4. NMS
    # keep = tf.image.non_max_suppression(boxes=decode_boxes,
    #                                     scores=cls_prob,
    #                                     max_output_size=post_nms_topN,
    #                                     iou_threshold=nms_thresh)
    keep = nms_rotate.nms_rotate(decode_boxes=decode_boxes,
                                 scores=cls_prob,
                                 iou_threshold=nms_thresh,
                                 max_output_size=post_nms_topN,
                                 use_angle_condition=False,
                                 angle_threshold=15,
                                 use_gpu=True)

    final_boxes = tf.gather(decode_boxes, keep)
    final_probs = tf.gather(cls_prob, keep)

    return final_boxes, final_probs
Exemplo n.º 9
0
    def fast_rcnn_proposals_rotate(self, decode_boxes, scores, head_quadrant):
        '''
        mutilclass NMS
        :param decode_boxes: [N, num_classes*5]
        :param scores: [N, num_classes+1]
        :return:
        detection_boxes : [-1, 5]
        scores : [-1, ]

        '''

        with tf.variable_scope('fast_rcnn_proposals'):
            category = tf.argmax(scores, axis=1)

            object_mask = tf.cast(tf.not_equal(category, 0), tf.float32)

            decode_boxes = decode_boxes * tf.expand_dims(
                object_mask, axis=1)  # make background box is [0 0 0 0, 0]
            scores = scores * tf.expand_dims(object_mask, axis=1)
            # Class-agnostic regression
            # decode_boxes = tf.reshape(decode_boxes, [-1, self.num_classes, 5])  # [N, num_classes, 5]
            # head_quadrant = tf.reshape(head_quadrant, [-1, self.num_classes, 4])
            # decode_boxes_list = tf.unstack(decode_boxes, axis=1)
            # head_quadrant_list = tf.unstack(head_quadrant, axis=1)
            # score_list = tf.unstack(scores[:, 1:], axis=1)
            # after_nms_boxes = []
            # after_nms_scores = []
            # after_nms_head_quadrant = []
            # category_list = []
            # for per_class_decode_boxes, per_head_quadrant, per_class_scores in zip(decode_boxes_list, head_quadrant_list, score_list):

            #     valid_indices = nms_rotate.nms_rotate(decode_boxes=per_class_decode_boxes,
            #                                           scores=per_class_scores,
            #                                           iou_threshold=self.fast_rcnn_nms_iou_threshold,
            #                                           max_output_size=self.fast_rcnn_nms_max_boxes_per_class,
            #                                           use_angle_condition=False,
            #                                           angle_threshold=15,
            #                                           use_gpu=cfgs.ROTATE_NMS_USE_GPU)

            #     after_nms_boxes.append(tf.gather(per_class_decode_boxes, valid_indices))
            #     after_nms_scores.append(tf.gather(per_class_scores, valid_indices))
            #     after_nms_head_quadrant.append(tf.gather(per_head_quadrant, valid_indices))
            #     tmp_category = tf.gather(category, valid_indices)

            #     category_list.append(tmp_category)
            # all_nms_boxes = tf.concat(after_nms_boxes, axis=0)
            # all_nms_scores = tf.concat(after_nms_scores, axis=0)
            # all_nms_head_quadrant = tf.concat(after_nms_head_quadrant, axis=0)
            # all_category = tf.concat(category_list, axis=0)

            ## all_nms_boxes = boxes_utils.clip_boxes_to_img_boundaries(all_nms_boxes,
            ##                                                          img_shape=self.img_shape)
            scores, _ = tf.nn.top_k(
                scores[:, 1:],
                1)  # select the max score in all classes as nms score
            scores = tf.squeeze(scores)
            valid_indices = nms_rotate.nms_rotate(
                decode_boxes=decode_boxes,
                scores=scores,
                iou_threshold=self.fast_rcnn_nms_iou_threshold,
                max_output_size=self.fast_rcnn_nms_max_boxes_per_class,
                use_angle_condition=False,
                angle_threshold=15,
                use_gpu=cfgs.ROTATE_NMS_USE_GPU)
            all_nms_boxes = tf.gather(decode_boxes, valid_indices)
            all_nms_scores = tf.gather(scores, valid_indices)
            all_nms_head_quadrant = tf.gather(head_quadrant, valid_indices)
            all_category = tf.gather(category, valid_indices)

            scores_large_than_threshold_indices = \
                tf.reshape(tf.where(tf.greater(all_nms_scores, self.show_detections_score_threshold)), [-1])

            all_nms_boxes = tf.gather(all_nms_boxes,
                                      scores_large_than_threshold_indices)
            all_nms_scores = tf.gather(all_nms_scores,
                                       scores_large_than_threshold_indices)
            all_nms_head_quadrant = tf.gather(
                all_nms_head_quadrant, scores_large_than_threshold_indices)
            all_category = tf.gather(all_category,
                                     scores_large_than_threshold_indices)

            return all_nms_boxes, all_nms_scores, all_nms_head_quadrant, tf.shape(
                all_nms_boxes)[0], all_category
Exemplo n.º 10
0
def postprocess_detctions(rpn_bbox_pred, rpn_cls_prob, anchors, is_training,
                          gpu_id):

    return_boxes_pred = []
    return_scores = []
    return_labels = []
    for j in range(0, cfgs.CLASS_NUM):
        scores = rpn_cls_prob[:, j]
        if is_training:
            indices = tf.reshape(tf.where(tf.greater(scores, cfgs.VIS_SCORE)),
                                 [
                                     -1,
                                 ])
        else:
            indices = tf.reshape(
                tf.where(tf.greater(scores, cfgs.FILTERED_SCORE)), [
                    -1,
                ])

        anchors_ = tf.gather(anchors, indices)
        rpn_bbox_pred_ = tf.gather(rpn_bbox_pred, indices)
        scores = tf.gather(scores, indices)

        if cfgs.METHOD == 'H':
            x_c = (anchors_[:, 2] + anchors_[:, 0]) / 2
            y_c = (anchors_[:, 3] + anchors_[:, 1]) / 2
            h = anchors_[:, 2] - anchors_[:, 0] + 1
            w = anchors_[:, 3] - anchors_[:, 1] + 1
            theta = -90 * tf.ones_like(x_c)
            anchors_ = tf.transpose(tf.stack([x_c, y_c, w, h, theta]))

        if cfgs.ANGLE_RANGE == 180:
            anchors_ = tf.py_func(coordinate_present_convert,
                                  inp=[anchors_, -1],
                                  Tout=[tf.float32])
            anchors_ = tf.reshape(anchors_, [-1, 5])

        boxes_pred = bbox_transform.rbbox_transform_inv(boxes=anchors_,
                                                        deltas=rpn_bbox_pred_)

        if cfgs.ANGLE_RANGE == 180:

            _, _, _, _, theta = tf.unstack(boxes_pred, axis=1)
            indx = tf.reshape(
                tf.where(
                    tf.logical_and(tf.less(theta, 0),
                                   tf.greater_equal(theta, -180))), [
                                       -1,
                                   ])
            boxes_pred = tf.gather(boxes_pred, indx)
            scores = tf.gather(scores, indx)

            boxes_pred = tf.py_func(coordinate_present_convert,
                                    inp=[boxes_pred, 1],
                                    Tout=[tf.float32])
            boxes_pred = tf.reshape(boxes_pred, [-1, 5])

        max_output_size = 4000 if 'DOTA' in cfgs.NET_NAME else 200
        nms_indices = nms_rotate.nms_rotate(
            decode_boxes=boxes_pred,
            scores=scores,
            iou_threshold=cfgs.NMS_IOU_THRESHOLD,
            max_output_size=100 if is_training else max_output_size,
            use_angle_condition=False,
            angle_threshold=15,
            use_gpu=True,
            gpu_id=gpu_id)

        tmp_boxes_pred = tf.reshape(tf.gather(boxes_pred, nms_indices),
                                    [-1, 5])
        tmp_scores = tf.reshape(tf.gather(scores, nms_indices), [
            -1,
        ])

        return_boxes_pred.append(tmp_boxes_pred)
        return_scores.append(tmp_scores)
        return_labels.append(tf.ones_like(tmp_scores) * (j + 1))

    return_boxes_pred = tf.concat(return_boxes_pred, axis=0)
    return_scores = tf.concat(return_scores, axis=0)
    return_labels = tf.concat(return_labels, axis=0)

    return return_boxes_pred, return_scores, return_labels